ZeroTierOne.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. /*
  27. * This defines the external C API for ZeroTier's core network virtualization
  28. * engine.
  29. */
  30. #ifndef ZT_ZEROTIERONE_H
  31. #define ZT_ZEROTIERONE_H
  32. #include <stdint.h>
  33. // For the struct sockaddr_storage structure
  34. #if defined(_WIN32) || defined(_WIN64)
  35. #include <WinSock2.h>
  36. #include <WS2tcpip.h>
  37. #include <Windows.h>
  38. #else /* not Windows */
  39. #include <arpa/inet.h>
  40. #include <netinet/in.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #endif /* Windows or not */
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /****************************************************************************/
  48. /* Core constants */
  49. /****************************************************************************/
  50. /**
  51. * Default UDP port for devices running a ZeroTier endpoint
  52. */
  53. #define ZT_DEFAULT_PORT 9993
  54. /**
  55. * Minimum MTU, which is the minimum allowed by IPv6 and several specs
  56. */
  57. #define ZT_MIN_MTU 1280
  58. /**
  59. * Maximum MTU for ZeroTier virtual networks
  60. */
  61. #define ZT_MAX_MTU 10000
  62. /**
  63. * Maximum length of network short name
  64. */
  65. #define ZT_MAX_NETWORK_SHORT_NAME_LENGTH 127
  66. /**
  67. * Maximum number of pushed routes on a network
  68. */
  69. #define ZT_MAX_NETWORK_ROUTES 32
  70. /**
  71. * Maximum number of statically assigned IP addresses per network endpoint using ZT address management (not DHCP)
  72. */
  73. #define ZT_MAX_ZT_ASSIGNED_ADDRESSES 16
  74. /**
  75. * Maximum number of "specialists" on a network -- bridges, relays, etc.
  76. */
  77. #define ZT_MAX_NETWORK_SPECIALISTS 256
  78. /**
  79. * Maximum number of multicast group subscriptions per network
  80. */
  81. #define ZT_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS 4096
  82. /**
  83. * Rules engine revision ID, which specifies rules engine capabilities
  84. */
  85. #define ZT_RULES_ENGINE_REVISION 1
  86. /**
  87. * Maximum number of base (non-capability) network rules
  88. */
  89. #define ZT_MAX_NETWORK_RULES 1024
  90. /**
  91. * Maximum number of per-member capabilities per network
  92. */
  93. #define ZT_MAX_NETWORK_CAPABILITIES 128
  94. /**
  95. * Maximum number of per-member tags per network
  96. */
  97. #define ZT_MAX_NETWORK_TAGS 128
  98. /**
  99. * Maximum number of direct network paths to a given peer
  100. */
  101. #define ZT_MAX_PEER_NETWORK_PATHS 4
  102. /**
  103. * Maximum number of trusted physical network paths
  104. */
  105. #define ZT_MAX_TRUSTED_PATHS 16
  106. /**
  107. * Maximum number of rules per capability
  108. */
  109. #define ZT_MAX_CAPABILITY_RULES 64
  110. /**
  111. * Maximum number of certificates of ownership to assign to a single network member
  112. */
  113. #define ZT_MAX_CERTIFICATES_OF_OWNERSHIP 4
  114. /**
  115. * Global maximum length for capability chain of custody (including initial issue)
  116. */
  117. #define ZT_MAX_CAPABILITY_CUSTODY_CHAIN_LENGTH 7
  118. /**
  119. * Maximum value for link quality (min is 0)
  120. */
  121. #define ZT_PATH_LINK_QUALITY_MAX 0xff
  122. /**
  123. * Packet characteristics flag: packet direction, 1 if inbound 0 if outbound
  124. */
  125. #define ZT_RULE_PACKET_CHARACTERISTICS_INBOUND 0x8000000000000000ULL
  126. /**
  127. * Packet characteristics flag: multicast or broadcast destination MAC
  128. */
  129. #define ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST 0x4000000000000000ULL
  130. /**
  131. * Packet characteristics flag: broadcast destination MAC
  132. */
  133. #define ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST 0x2000000000000000ULL
  134. /**
  135. * Packet characteristics flag: sending IP address has a certificate of ownership
  136. */
  137. #define ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED 0x1000000000000000ULL
  138. /**
  139. * Packet characteristics flag: sending MAC address has a certificate of ownership
  140. */
  141. #define ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED 0x0800000000000000ULL
  142. /**
  143. * Packet characteristics flag: TCP left-most reserved bit
  144. */
  145. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_0 0x0000000000000800ULL
  146. /**
  147. * Packet characteristics flag: TCP middle reserved bit
  148. */
  149. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_1 0x0000000000000400ULL
  150. /**
  151. * Packet characteristics flag: TCP right-most reserved bit
  152. */
  153. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RESERVED_2 0x0000000000000200ULL
  154. /**
  155. * Packet characteristics flag: TCP NS flag
  156. */
  157. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_NS 0x0000000000000100ULL
  158. /**
  159. * Packet characteristics flag: TCP CWR flag
  160. */
  161. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_CWR 0x0000000000000080ULL
  162. /**
  163. * Packet characteristics flag: TCP ECE flag
  164. */
  165. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ECE 0x0000000000000040ULL
  166. /**
  167. * Packet characteristics flag: TCP URG flag
  168. */
  169. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_URG 0x0000000000000020ULL
  170. /**
  171. * Packet characteristics flag: TCP ACK flag
  172. */
  173. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK 0x0000000000000010ULL
  174. /**
  175. * Packet characteristics flag: TCP PSH flag
  176. */
  177. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_PSH 0x0000000000000008ULL
  178. /**
  179. * Packet characteristics flag: TCP RST flag
  180. */
  181. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_RST 0x0000000000000004ULL
  182. /**
  183. * Packet characteristics flag: TCP SYN flag
  184. */
  185. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN 0x0000000000000002ULL
  186. /**
  187. * Packet characteristics flag: TCP FIN flag
  188. */
  189. #define ZT_RULE_PACKET_CHARACTERISTICS_TCP_FIN 0x0000000000000001ULL
  190. /****************************************************************************/
  191. /* Structures and other types */
  192. /****************************************************************************/
  193. /**
  194. * Function return code: OK (0) or error results
  195. *
  196. * Use ZT_ResultCode_isFatal() to check for a fatal error. If a fatal error
  197. * occurs, the node should be considered to not be working correctly. These
  198. * indicate serious problems like an inaccessible data store or a compile
  199. * problem.
  200. */
  201. enum ZT_ResultCode
  202. {
  203. /**
  204. * Operation completed normally
  205. */
  206. ZT_RESULT_OK = 0,
  207. /**
  208. * Call produced no error but no action was taken
  209. */
  210. ZT_RESULT_OK_IGNORED = 1,
  211. // Fatal errors (>100, <1000)
  212. /**
  213. * Ran out of memory
  214. */
  215. ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 100,
  216. /**
  217. * Data store is not writable or has failed
  218. */
  219. ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 101,
  220. /**
  221. * Internal error (e.g. unexpected exception indicating bug or build problem)
  222. */
  223. ZT_RESULT_FATAL_ERROR_INTERNAL = 102,
  224. // Non-fatal errors (>1000)
  225. /**
  226. * Network ID not valid
  227. */
  228. ZT_RESULT_ERROR_NETWORK_NOT_FOUND = 1000,
  229. /**
  230. * The requested operation is not supported on this version or build
  231. */
  232. ZT_RESULT_ERROR_UNSUPPORTED_OPERATION = 1001,
  233. /**
  234. * The requestion operation was given a bad parameter or was called in an invalid state
  235. */
  236. ZT_RESULT_ERROR_BAD_PARAMETER = 1002
  237. };
  238. /**
  239. * @param x Result code
  240. * @return True if result code indicates a fatal error
  241. */
  242. #define ZT_ResultCode_isFatal(x) ((((int)(x)) >= 100)&&(((int)(x)) < 1000))
  243. /**
  244. * Status codes sent to status update callback when things happen
  245. */
  246. enum ZT_Event
  247. {
  248. /**
  249. * Node has been initialized
  250. *
  251. * This is the first event generated, and is always sent. It may occur
  252. * before Node's constructor returns.
  253. *
  254. * Meta-data: none
  255. */
  256. ZT_EVENT_UP = 0,
  257. /**
  258. * Node is offline -- network does not seem to be reachable by any available strategy
  259. *
  260. * Meta-data: none
  261. */
  262. ZT_EVENT_OFFLINE = 1,
  263. /**
  264. * Node is online -- at least one upstream node appears reachable
  265. *
  266. * Meta-data: none
  267. */
  268. ZT_EVENT_ONLINE = 2,
  269. /**
  270. * Node is shutting down
  271. *
  272. * This is generated within Node's destructor when it is being shut down.
  273. * It's done for convenience, since cleaning up other state in the event
  274. * handler may appear more idiomatic.
  275. *
  276. * Meta-data: none
  277. */
  278. ZT_EVENT_DOWN = 3,
  279. /**
  280. * Your identity has collided with another node's ZeroTier address
  281. *
  282. * This happens if two different public keys both hash (via the algorithm
  283. * in Identity::generate()) to the same 40-bit ZeroTier address.
  284. *
  285. * This is something you should "never" see, where "never" is defined as
  286. * once per 2^39 new node initializations / identity creations. If you do
  287. * see it, you're going to see it very soon after a node is first
  288. * initialized.
  289. *
  290. * This is reported as an event rather than a return code since it's
  291. * detected asynchronously via error messages from authoritative nodes.
  292. *
  293. * If this occurs, you must shut down and delete the node, delete the
  294. * identity.secret record/file from the data store, and restart to generate
  295. * a new identity. If you don't do this, you will not be able to communicate
  296. * with other nodes.
  297. *
  298. * We'd automate this process, but we don't think silently deleting
  299. * private keys or changing our address without telling the calling code
  300. * is good form. It violates the principle of least surprise.
  301. *
  302. * You can technically get away with not handling this, but we recommend
  303. * doing so in a mature reliable application. Besides, handling this
  304. * condition is a good way to make sure it never arises. It's like how
  305. * umbrellas prevent rain and smoke detectors prevent fires. They do, right?
  306. *
  307. * Meta-data: none
  308. */
  309. ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION = 4,
  310. /**
  311. * Trace (debugging) message
  312. *
  313. * These events are only generated if this is a TRACE-enabled build.
  314. *
  315. * Meta-data: C string, TRACE message
  316. */
  317. ZT_EVENT_TRACE = 5,
  318. /**
  319. * VERB_USER_MESSAGE received
  320. *
  321. * These are generated when a VERB_USER_MESSAGE packet is received via
  322. * ZeroTier VL1.
  323. *
  324. * Meta-data: ZT_UserMessage structure
  325. */
  326. ZT_EVENT_USER_MESSAGE = 6
  327. };
  328. /**
  329. * User message used with ZT_EVENT_USER_MESSAGE
  330. *
  331. * These are direct VL1 P2P messages for application use. Encryption and
  332. * authentication in the ZeroTier protocol will guarantee the origin
  333. * address and message content, but you are responsible for any other
  334. * levels of authentication or access control that are required. Any node
  335. * in the world can send you a user message! (Unless your network is air
  336. * gapped.)
  337. */
  338. typedef struct
  339. {
  340. /**
  341. * ZeroTier address of sender (least significant 40 bits)
  342. */
  343. uint64_t origin;
  344. /**
  345. * User message type ID
  346. */
  347. uint64_t typeId;
  348. /**
  349. * User message data (not including type ID)
  350. */
  351. const void *data;
  352. /**
  353. * Length of data in bytes
  354. */
  355. unsigned int length;
  356. } ZT_UserMessage;
  357. /**
  358. * Current node status
  359. */
  360. typedef struct
  361. {
  362. /**
  363. * 40-bit ZeroTier address of this node
  364. */
  365. uint64_t address;
  366. /**
  367. * Public identity in string-serialized form (safe to send to others)
  368. *
  369. * This pointer will remain valid as long as the node exists.
  370. */
  371. const char *publicIdentity;
  372. /**
  373. * Full identity including secret key in string-serialized form
  374. *
  375. * This pointer will remain valid as long as the node exists.
  376. */
  377. const char *secretIdentity;
  378. /**
  379. * True if some kind of connectivity appears available
  380. */
  381. int online;
  382. } ZT_NodeStatus;
  383. /**
  384. * Virtual network status codes
  385. */
  386. enum ZT_VirtualNetworkStatus
  387. {
  388. /**
  389. * Waiting for network configuration (also means revision == 0)
  390. */
  391. ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  392. /**
  393. * Configuration received and we are authorized
  394. */
  395. ZT_NETWORK_STATUS_OK = 1,
  396. /**
  397. * Netconf master told us 'nope'
  398. */
  399. ZT_NETWORK_STATUS_ACCESS_DENIED = 2,
  400. /**
  401. * Netconf master exists, but this virtual network does not
  402. */
  403. ZT_NETWORK_STATUS_NOT_FOUND = 3,
  404. /**
  405. * Initialization of network failed or other internal error
  406. */
  407. ZT_NETWORK_STATUS_PORT_ERROR = 4,
  408. /**
  409. * ZeroTier core version too old
  410. */
  411. ZT_NETWORK_STATUS_CLIENT_TOO_OLD = 5
  412. };
  413. /**
  414. * Virtual network type codes
  415. */
  416. enum ZT_VirtualNetworkType
  417. {
  418. /**
  419. * Private networks are authorized via certificates of membership
  420. */
  421. ZT_NETWORK_TYPE_PRIVATE = 0,
  422. /**
  423. * Public networks have no access control -- they'll always be AUTHORIZED
  424. */
  425. ZT_NETWORK_TYPE_PUBLIC = 1
  426. };
  427. /**
  428. * The type of a virtual network rules table entry
  429. *
  430. * These must be from 0 to 63 since the most significant two bits of each
  431. * rule type are NOT (MSB) and AND/OR.
  432. *
  433. * Each rule is composed of zero or more MATCHes followed by an ACTION.
  434. * An ACTION with no MATCHes is always taken.
  435. */
  436. enum ZT_VirtualNetworkRuleType
  437. {
  438. // 0 to 15 reserved for actions
  439. /**
  440. * Drop frame
  441. */
  442. ZT_NETWORK_RULE_ACTION_DROP = 0,
  443. /**
  444. * Accept and pass frame
  445. */
  446. ZT_NETWORK_RULE_ACTION_ACCEPT = 1,
  447. /**
  448. * Forward a copy of this frame to an observer (by ZT address)
  449. */
  450. ZT_NETWORK_RULE_ACTION_TEE = 2,
  451. /**
  452. * Exactly like TEE but mandates ACKs from observer
  453. */
  454. ZT_NETWORK_RULE_ACTION_WATCH = 3,
  455. /**
  456. * Drop and redirect this frame to another node (by ZT address)
  457. */
  458. ZT_NETWORK_RULE_ACTION_REDIRECT = 4,
  459. /**
  460. * Stop evaluating rule set (drops unless there are capabilities, etc.)
  461. */
  462. ZT_NETWORK_RULE_ACTION_BREAK = 5,
  463. /**
  464. * Maximum ID for an ACTION, anything higher is a MATCH
  465. */
  466. ZT_NETWORK_RULE_ACTION__MAX_ID = 15,
  467. // 16 to 63 reserved for match criteria
  468. ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS = 24,
  469. ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS = 25,
  470. ZT_NETWORK_RULE_MATCH_VLAN_ID = 26,
  471. ZT_NETWORK_RULE_MATCH_VLAN_PCP = 27,
  472. ZT_NETWORK_RULE_MATCH_VLAN_DEI = 28,
  473. ZT_NETWORK_RULE_MATCH_MAC_SOURCE = 29,
  474. ZT_NETWORK_RULE_MATCH_MAC_DEST = 30,
  475. ZT_NETWORK_RULE_MATCH_IPV4_SOURCE = 31,
  476. ZT_NETWORK_RULE_MATCH_IPV4_DEST = 32,
  477. ZT_NETWORK_RULE_MATCH_IPV6_SOURCE = 33,
  478. ZT_NETWORK_RULE_MATCH_IPV6_DEST = 34,
  479. ZT_NETWORK_RULE_MATCH_IP_TOS = 35,
  480. ZT_NETWORK_RULE_MATCH_IP_PROTOCOL = 36,
  481. ZT_NETWORK_RULE_MATCH_ETHERTYPE = 37,
  482. ZT_NETWORK_RULE_MATCH_ICMP = 38,
  483. ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE = 39,
  484. ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE = 40,
  485. ZT_NETWORK_RULE_MATCH_CHARACTERISTICS = 41,
  486. ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE = 42,
  487. ZT_NETWORK_RULE_MATCH_RANDOM = 43,
  488. ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE = 44,
  489. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND = 45,
  490. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR = 46,
  491. ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR = 47,
  492. ZT_NETWORK_RULE_MATCH_TAGS_EQUAL = 48,
  493. ZT_NETWORK_RULE_MATCH_TAG_SENDER = 49,
  494. ZT_NETWORK_RULE_MATCH_TAG_RECEIVER = 50,
  495. /**
  496. * Maximum ID allowed for a MATCH entry in the rules table
  497. */
  498. ZT_NETWORK_RULE_MATCH__MAX_ID = 63
  499. };
  500. /**
  501. * Network flow rule
  502. *
  503. * Rules are stored in a table in which one or more match entries is followed
  504. * by an action. If more than one match precedes an action, the rule is
  505. * the AND of all matches. An action with no match is always taken since it
  506. * matches anything. If nothing matches, the default action is DROP.
  507. *
  508. * This is designed to be a more memory-efficient way of storing rules than
  509. * a wide table, yet still fast and simple to access in code.
  510. */
  511. typedef struct
  512. {
  513. /**
  514. * Type and flags
  515. *
  516. * Bits are: NOTTTTTT
  517. *
  518. * N - If true, sense of match is inverted (no effect on actions)
  519. * O - If true, result is ORed with previous instead of ANDed (no effect on actions)
  520. * T - Rule or action type
  521. *
  522. * AND with 0x3f to get type, 0x80 to get NOT bit, and 0x40 to get OR bit.
  523. */
  524. uint8_t t;
  525. /**
  526. * Union containing the value of this rule -- which field is used depends on 't'
  527. */
  528. union {
  529. /**
  530. * IPv6 address in big-endian / network byte order and netmask bits
  531. */
  532. struct {
  533. uint8_t ip[16];
  534. uint8_t mask;
  535. } ipv6;
  536. /**
  537. * IPv4 address in big-endian / network byte order
  538. */
  539. struct {
  540. uint32_t ip;
  541. uint8_t mask;
  542. } ipv4;
  543. /**
  544. * Packet characteristic flags being matched
  545. */
  546. uint64_t characteristics;
  547. /**
  548. * IP port range -- start-end inclusive -- host byte order
  549. */
  550. uint16_t port[2];
  551. /**
  552. * 40-bit ZeroTier address (in least significant bits, host byte order)
  553. */
  554. uint64_t zt;
  555. /**
  556. * 0 = never, UINT32_MAX = always
  557. */
  558. uint32_t randomProbability;
  559. /**
  560. * 48-bit Ethernet MAC address in big-endian order
  561. */
  562. uint8_t mac[6];
  563. /**
  564. * VLAN ID in host byte order
  565. */
  566. uint16_t vlanId;
  567. /**
  568. * VLAN PCP (least significant 3 bits)
  569. */
  570. uint8_t vlanPcp;
  571. /**
  572. * VLAN DEI (single bit / boolean)
  573. */
  574. uint8_t vlanDei;
  575. /**
  576. * Ethernet type in host byte order
  577. */
  578. uint16_t etherType;
  579. /**
  580. * IP protocol
  581. */
  582. uint8_t ipProtocol;
  583. /**
  584. * IP type of service a.k.a. DSCP field
  585. */
  586. struct {
  587. uint8_t mask;
  588. uint8_t value[2];
  589. } ipTos;
  590. /**
  591. * Ethernet packet size in host byte order (start-end, inclusive)
  592. */
  593. uint16_t frameSize[2];
  594. /**
  595. * ICMP type and code
  596. */
  597. struct {
  598. uint8_t type; // ICMP type, always matched
  599. uint8_t code; // ICMP code if matched
  600. uint8_t flags; // flag 0x01 means also match code, otherwise only match type
  601. } icmp;
  602. /**
  603. * For tag-related rules
  604. */
  605. struct {
  606. uint32_t id;
  607. uint32_t value;
  608. } tag;
  609. /**
  610. * Destinations for TEE and REDIRECT
  611. */
  612. struct {
  613. uint64_t address;
  614. uint32_t flags;
  615. uint16_t length;
  616. } fwd;
  617. } v;
  618. } ZT_VirtualNetworkRule;
  619. /**
  620. * A route to be pushed on a virtual network
  621. */
  622. typedef struct
  623. {
  624. /**
  625. * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default
  626. */
  627. struct sockaddr_storage target;
  628. /**
  629. * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway)
  630. */
  631. struct sockaddr_storage via;
  632. /**
  633. * Route flags
  634. */
  635. uint16_t flags;
  636. /**
  637. * Route metric (not currently used)
  638. */
  639. uint16_t metric;
  640. } ZT_VirtualNetworkRoute;
  641. /**
  642. * An Ethernet multicast group
  643. */
  644. typedef struct
  645. {
  646. /**
  647. * MAC address (least significant 48 bits)
  648. */
  649. uint64_t mac;
  650. /**
  651. * Additional distinguishing information (usually zero)
  652. */
  653. unsigned long adi;
  654. } ZT_MulticastGroup;
  655. /**
  656. * Virtual network configuration update type
  657. */
  658. enum ZT_VirtualNetworkConfigOperation
  659. {
  660. /**
  661. * Network is coming up (either for the first time or after service restart)
  662. */
  663. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  664. /**
  665. * Network configuration has been updated
  666. */
  667. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  668. /**
  669. * Network is going down (not permanently)
  670. */
  671. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  672. /**
  673. * Network is going down permanently (leave/delete)
  674. */
  675. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  676. };
  677. /**
  678. * What trust hierarchy role does this peer have?
  679. */
  680. enum ZT_PeerRole
  681. {
  682. ZT_PEER_ROLE_LEAF = 0, // ordinary node
  683. ZT_PEER_ROLE_MOON = 1, // moon root
  684. ZT_PEER_ROLE_PLANET = 2 // planetary root
  685. };
  686. /**
  687. * Vendor ID
  688. */
  689. enum ZT_Vendor
  690. {
  691. ZT_VENDOR_UNSPECIFIED = 0,
  692. ZT_VENDOR_ZEROTIER = 1
  693. };
  694. /**
  695. * Platform type
  696. */
  697. enum ZT_Platform
  698. {
  699. ZT_PLATFORM_UNSPECIFIED = 0,
  700. ZT_PLATFORM_LINUX = 1,
  701. ZT_PLATFORM_WINDOWS = 2,
  702. ZT_PLATFORM_MACOS = 3,
  703. ZT_PLATFORM_ANDROID = 4,
  704. ZT_PLATFORM_IOS = 5,
  705. ZT_PLATFORM_SOLARIS_SMARTOS = 6,
  706. ZT_PLATFORM_FREEBSD = 7,
  707. ZT_PLATFORM_NETBSD = 8,
  708. ZT_PLATFORM_OPENBSD = 9,
  709. ZT_PLATFORM_RISCOS = 10,
  710. ZT_PLATFORM_VXWORKS = 11,
  711. ZT_PLATFORM_FREERTOS = 12,
  712. ZT_PLATFORM_SYSBIOS = 13,
  713. ZT_PLATFORM_HURD = 14,
  714. ZT_PLATFORM_WEB = 15
  715. };
  716. /**
  717. * Architecture type
  718. */
  719. enum ZT_Architecture
  720. {
  721. ZT_ARCHITECTURE_UNSPECIFIED = 0,
  722. ZT_ARCHITECTURE_X86 = 1,
  723. ZT_ARCHITECTURE_X64 = 2,
  724. ZT_ARCHITECTURE_ARM32 = 3,
  725. ZT_ARCHITECTURE_ARM64 = 4,
  726. ZT_ARCHITECTURE_MIPS32 = 5,
  727. ZT_ARCHITECTURE_MIPS64 = 6,
  728. ZT_ARCHITECTURE_POWER32 = 7,
  729. ZT_ARCHITECTURE_POWER64 = 8,
  730. ZT_ARCHITECTURE_OPENRISC32 = 9,
  731. ZT_ARCHITECTURE_OPENRISC64 = 10,
  732. ZT_ARCHITECTURE_SPARC32 = 11,
  733. ZT_ARCHITECTURE_SPARC64 = 12,
  734. ZT_ARCHITECTURE_DOTNET_CLR = 13,
  735. ZT_ARCHITECTURE_JAVA_JVM = 14,
  736. ZT_ARCHITECTURE_WEB = 15
  737. };
  738. /**
  739. * Virtual network configuration
  740. */
  741. typedef struct
  742. {
  743. /**
  744. * 64-bit ZeroTier network ID
  745. */
  746. uint64_t nwid;
  747. /**
  748. * Ethernet MAC (48 bits) that should be assigned to port
  749. */
  750. uint64_t mac;
  751. /**
  752. * Network name (from network configuration master)
  753. */
  754. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  755. /**
  756. * Network configuration request status
  757. */
  758. enum ZT_VirtualNetworkStatus status;
  759. /**
  760. * Network type
  761. */
  762. enum ZT_VirtualNetworkType type;
  763. /**
  764. * Maximum interface MTU
  765. */
  766. unsigned int mtu;
  767. /**
  768. * Recommended MTU to avoid fragmentation at the physical layer (hint)
  769. */
  770. unsigned int physicalMtu;
  771. /**
  772. * If nonzero, the network this port belongs to indicates DHCP availability
  773. *
  774. * This is a suggestion. The underlying implementation is free to ignore it
  775. * for security or other reasons. This is simply a netconf parameter that
  776. * means 'DHCP is available on this network.'
  777. */
  778. int dhcp;
  779. /**
  780. * If nonzero, this port is allowed to bridge to other networks
  781. *
  782. * This is informational. If this is false (0), bridged packets will simply
  783. * be dropped and bridging won't work.
  784. */
  785. int bridge;
  786. /**
  787. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  788. */
  789. int broadcastEnabled;
  790. /**
  791. * If the network is in PORT_ERROR state, this is the (negative) error code most recently reported
  792. */
  793. int portError;
  794. /**
  795. * Revision number as reported by controller or 0 if still waiting for config
  796. */
  797. unsigned long netconfRevision;
  798. /**
  799. * Number of assigned addresses
  800. */
  801. unsigned int assignedAddressCount;
  802. /**
  803. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  804. *
  805. * For IP, the port number of the sockaddr_XX structure contains the number
  806. * of bits in the address netmask. Only the IP address and port are used.
  807. * Other fields like interface number can be ignored.
  808. *
  809. * This is only used for ZeroTier-managed address assignments sent by the
  810. * virtual network's configuration master.
  811. */
  812. struct sockaddr_storage assignedAddresses[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  813. /**
  814. * Number of ZT-pushed routes
  815. */
  816. unsigned int routeCount;
  817. /**
  818. * Routes (excluding those implied by assigned addresses and their masks)
  819. */
  820. ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
  821. } ZT_VirtualNetworkConfig;
  822. /**
  823. * A list of networks
  824. */
  825. typedef struct
  826. {
  827. ZT_VirtualNetworkConfig *networks;
  828. unsigned long networkCount;
  829. } ZT_VirtualNetworkList;
  830. /**
  831. * Physical network path to a peer
  832. */
  833. typedef struct
  834. {
  835. /**
  836. * Address of endpoint
  837. */
  838. struct sockaddr_storage address;
  839. /**
  840. * Time of last send in milliseconds or 0 for never
  841. */
  842. uint64_t lastSend;
  843. /**
  844. * Time of last receive in milliseconds or 0 for never
  845. */
  846. uint64_t lastReceive;
  847. /**
  848. * Is this a trusted path? If so this will be its nonzero ID.
  849. */
  850. uint64_t trustedPathId;
  851. /**
  852. * Path link quality from 0 to 255 (always 255 if peer does not support)
  853. */
  854. int linkQuality;
  855. /**
  856. * Is path expired?
  857. */
  858. int expired;
  859. /**
  860. * Is path preferred?
  861. */
  862. int preferred;
  863. } ZT_PeerPhysicalPath;
  864. /**
  865. * Peer status result buffer
  866. */
  867. typedef struct
  868. {
  869. /**
  870. * ZeroTier address (40 bits)
  871. */
  872. uint64_t address;
  873. /**
  874. * Remote major version or -1 if not known
  875. */
  876. int versionMajor;
  877. /**
  878. * Remote minor version or -1 if not known
  879. */
  880. int versionMinor;
  881. /**
  882. * Remote revision or -1 if not known
  883. */
  884. int versionRev;
  885. /**
  886. * Last measured latency in milliseconds or zero if unknown
  887. */
  888. unsigned int latency;
  889. /**
  890. * What trust hierarchy role does this device have?
  891. */
  892. enum ZT_PeerRole role;
  893. /**
  894. * Number of paths (size of paths[])
  895. */
  896. unsigned int pathCount;
  897. /**
  898. * Known network paths to peer
  899. */
  900. ZT_PeerPhysicalPath paths[ZT_MAX_PEER_NETWORK_PATHS];
  901. } ZT_Peer;
  902. /**
  903. * List of peers
  904. */
  905. typedef struct
  906. {
  907. ZT_Peer *peers;
  908. unsigned long peerCount;
  909. } ZT_PeerList;
  910. /**
  911. * ZeroTier core state objects
  912. */
  913. enum ZT_StateObjectType
  914. {
  915. /**
  916. * Null object -- ignored
  917. */
  918. ZT_STATE_OBJECT_NULL = 0,
  919. /**
  920. * Public address and public key
  921. *
  922. * Object ID: this node's address if known, or 0 if unknown (first query)
  923. * Canonical path: <HOME>/identity.public
  924. * Persistence: required
  925. */
  926. ZT_STATE_OBJECT_IDENTITY_PUBLIC = 1,
  927. /**
  928. * Full identity with secret key
  929. *
  930. * Object ID: this node's address if known, or 0 if unknown (first query)
  931. * Canonical path: <HOME>/identity.secret
  932. * Persistence: required, should be stored with restricted permissions e.g. mode 0600 on *nix
  933. */
  934. ZT_STATE_OBJECT_IDENTITY_SECRET = 2,
  935. /**
  936. * The planet (there is only one per... well... planet!)
  937. *
  938. * Object ID: world ID of planet, or 0 if unknown (first query)
  939. * Canonical path: <HOME>/planet
  940. * Persistence: recommended
  941. */
  942. ZT_STATE_OBJECT_PLANET = 3,
  943. /**
  944. * A moon (federated root set)
  945. *
  946. * Object ID: world ID of moon
  947. * Canonical path: <HOME>/moons.d/<ID>.moon (16-digit hex ID)
  948. * Persistence: required if moon memberships should persist
  949. */
  950. ZT_STATE_OBJECT_MOON = 4,
  951. /**
  952. * Peer and related state
  953. *
  954. * Object ID: peer address
  955. * Canonical path: <HOME>/peers.d/<ID> (10-digit address
  956. * Persistence: optional, can be cleared at any time
  957. */
  958. ZT_STATE_OBJECT_PEER = 5,
  959. /**
  960. * Network configuration
  961. *
  962. * Object ID: peer address
  963. * Canonical path: <HOME>/networks.d/<NETWORKID>.conf (16-digit hex ID)
  964. * Persistence: required if network memberships should persist
  965. */
  966. ZT_STATE_OBJECT_NETWORK_CONFIG = 6
  967. };
  968. /**
  969. * An instance of a ZeroTier One node (opaque)
  970. */
  971. typedef void ZT_Node;
  972. /****************************************************************************/
  973. /* Callbacks used by Node API */
  974. /****************************************************************************/
  975. /**
  976. * Callback called to update virtual network port configuration
  977. *
  978. * This can be called at any time to update the configuration of a virtual
  979. * network port. The parameter after the network ID specifies whether this
  980. * port is being brought up, updated, brought down, or permanently deleted.
  981. *
  982. * This in turn should be used by the underlying implementation to create
  983. * and configure tap devices at the OS (or virtual network stack) layer.
  984. *
  985. * The supplied config pointer is not guaranteed to remain valid, so make
  986. * a copy if you want one.
  987. *
  988. * This should not call multicastSubscribe() or other network-modifying
  989. * methods, as this could cause a deadlock in multithreaded or interrupt
  990. * driven environments.
  991. *
  992. * This must return 0 on success. It can return any OS-dependent error code
  993. * on failure, and this results in the network being placed into the
  994. * PORT_ERROR state.
  995. */
  996. typedef int (*ZT_VirtualNetworkConfigFunction)(
  997. ZT_Node *, /* Node */
  998. void *, /* User ptr */
  999. void *, /* Thread ptr */
  1000. uint64_t, /* Network ID */
  1001. void **, /* Modifiable network user PTR */
  1002. enum ZT_VirtualNetworkConfigOperation, /* Config operation */
  1003. const ZT_VirtualNetworkConfig *); /* Network configuration */
  1004. /**
  1005. * Function to send a frame out to a virtual network port
  1006. *
  1007. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  1008. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  1009. * (9) frame length.
  1010. */
  1011. typedef void (*ZT_VirtualNetworkFrameFunction)(
  1012. ZT_Node *, /* Node */
  1013. void *, /* User ptr */
  1014. void *, /* Thread ptr */
  1015. uint64_t, /* Network ID */
  1016. void **, /* Modifiable network user PTR */
  1017. uint64_t, /* Source MAC */
  1018. uint64_t, /* Destination MAC */
  1019. unsigned int, /* Ethernet type */
  1020. unsigned int, /* VLAN ID (0 for none) */
  1021. const void *, /* Frame data */
  1022. unsigned int); /* Frame length */
  1023. /**
  1024. * Callback for events
  1025. *
  1026. * Events are generated when the node's status changes in a significant way
  1027. * and on certain non-fatal errors and events of interest. The final void
  1028. * parameter points to event meta-data. The type of event meta-data (and
  1029. * whether it is present at all) is event type dependent. See the comments
  1030. * in the definition of ZT_Event.
  1031. */
  1032. typedef void (*ZT_EventCallback)(
  1033. ZT_Node *, /* Node */
  1034. void *, /* User ptr */
  1035. void *, /* Thread ptr */
  1036. enum ZT_Event, /* Event type */
  1037. const void *); /* Event payload (if applicable) */
  1038. /**
  1039. * Callback for storing and/or publishing state information
  1040. *
  1041. * See ZT_StateObjectType docs for information about each state object type
  1042. * and when and if it needs to be persisted.
  1043. *
  1044. * An object of length -1 is sent to indicate that an object should be
  1045. * deleted.
  1046. */
  1047. typedef void (*ZT_StatePutFunction)(
  1048. ZT_Node *, /* Node */
  1049. void *, /* User ptr */
  1050. void *, /* Thread ptr */
  1051. enum ZT_StateObjectType, /* State object type */
  1052. const uint64_t [2], /* State object ID (if applicable) */
  1053. const void *, /* State object data */
  1054. int); /* Length of data or -1 to delete */
  1055. /**
  1056. * Callback for retrieving stored state information
  1057. *
  1058. * This function should return the number of bytes actually stored to the
  1059. * buffer or -1 if the state object was not found or the buffer was too
  1060. * small to store it.
  1061. */
  1062. typedef int (*ZT_StateGetFunction)(
  1063. ZT_Node *, /* Node */
  1064. void *, /* User ptr */
  1065. void *, /* Thread ptr */
  1066. enum ZT_StateObjectType, /* State object type */
  1067. const uint64_t [2], /* State object ID (if applicable) */
  1068. void *, /* Buffer to store state object data */
  1069. unsigned int); /* Length of data buffer in bytes */
  1070. /**
  1071. * Function to send a ZeroTier packet out over the physical wire (L2/L3)
  1072. *
  1073. * Parameters:
  1074. * (1) Node
  1075. * (2) User pointer
  1076. * (3) Local socket or -1 for "all" or "any"
  1077. * (4) Remote address
  1078. * (5) Packet data
  1079. * (6) Packet length
  1080. * (7) Desired IP TTL or 0 to use default
  1081. *
  1082. * If there is only one local socket, the local socket can be ignored.
  1083. * If the local socket is -1, the packet should be sent out from all
  1084. * bound local sockets or a random bound local socket.
  1085. *
  1086. * If TTL is nonzero, packets should have their IP TTL value set to this
  1087. * value if possible. If this is not possible it is acceptable to ignore
  1088. * this value and send anyway with normal or default TTL.
  1089. *
  1090. * The function must return zero on success and may return any error code
  1091. * on failure. Note that success does not (of course) guarantee packet
  1092. * delivery. It only means that the packet appears to have been sent.
  1093. */
  1094. typedef int (*ZT_WirePacketSendFunction)(
  1095. ZT_Node *, /* Node */
  1096. void *, /* User ptr */
  1097. void *, /* Thread ptr */
  1098. int64_t, /* Local socket */
  1099. const struct sockaddr_storage *, /* Remote address */
  1100. const void *, /* Packet data */
  1101. unsigned int, /* Packet length */
  1102. unsigned int); /* TTL or 0 to use default */
  1103. /**
  1104. * Function to check whether a path should be used for ZeroTier traffic
  1105. *
  1106. * Paramters:
  1107. * (1) Node
  1108. * (2) User pointer
  1109. * (3) ZeroTier address or 0 for none/any
  1110. * (4) Local socket or -1 if unknown
  1111. * (5) Remote address
  1112. *
  1113. * This function must return nonzero (true) if the path should be used.
  1114. *
  1115. * If no path check function is specified, ZeroTier will still exclude paths
  1116. * that overlap with ZeroTier-assigned and managed IP address blocks. But the
  1117. * use of a path check function is recommended to ensure that recursion does
  1118. * not occur in cases where addresses are assigned by the OS or managed by
  1119. * an out of band mechanism like DHCP. The path check function should examine
  1120. * all configured ZeroTier interfaces and check to ensure that the supplied
  1121. * addresses will not result in ZeroTier traffic being sent over a ZeroTier
  1122. * interface (recursion).
  1123. */
  1124. typedef int (*ZT_PathCheckFunction)(
  1125. ZT_Node *, /* Node */
  1126. void *, /* User ptr */
  1127. void *, /* Thread ptr */
  1128. uint64_t, /* ZeroTier address */
  1129. int64_t, /* Local socket or -1 if unknown */
  1130. const struct sockaddr_storage *); /* Remote address */
  1131. /**
  1132. * Function to get physical addresses for ZeroTier peers
  1133. *
  1134. * Parameters:
  1135. * (1) Node
  1136. * (2) User pointer
  1137. * (3) ZeroTier address (least significant 40 bits)
  1138. * (4) Desried address family or -1 for any
  1139. * (5) Buffer to fill with result
  1140. *
  1141. * If provided this function will be occasionally called to get physical
  1142. * addresses that might be tried to reach a ZeroTier address. It must
  1143. * return a nonzero (true) value if the result buffer has been filled
  1144. * with an address.
  1145. */
  1146. typedef int (*ZT_PathLookupFunction)(
  1147. ZT_Node *, /* Node */
  1148. void *, /* User ptr */
  1149. void *, /* Thread ptr */
  1150. uint64_t, /* ZeroTier address (40 bits) */
  1151. int, /* Desired ss_family or -1 for any */
  1152. struct sockaddr_storage *); /* Result buffer */
  1153. /****************************************************************************/
  1154. /* C Node API */
  1155. /****************************************************************************/
  1156. /**
  1157. * Structure for configuring ZeroTier core callback functions
  1158. */
  1159. struct ZT_Node_Callbacks
  1160. {
  1161. /**
  1162. * Struct version -- must currently be 0
  1163. */
  1164. long version;
  1165. /**
  1166. * REQUIRED: Function to store and/or replicate state objects
  1167. */
  1168. ZT_StatePutFunction statePutFunction;
  1169. /**
  1170. * REQUIRED: Function to retrieve state objects from an object store
  1171. */
  1172. ZT_StateGetFunction stateGetFunction;
  1173. /**
  1174. * REQUIRED: Function to send packets over the physical wire
  1175. */
  1176. ZT_WirePacketSendFunction wirePacketSendFunction;
  1177. /**
  1178. * REQUIRED: Function to inject frames into a virtual network's TAP
  1179. */
  1180. ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction;
  1181. /**
  1182. * REQUIRED: Function to be called when virtual networks are configured or changed
  1183. */
  1184. ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction;
  1185. /**
  1186. * REQUIRED: Function to be called to notify external code of important events
  1187. */
  1188. ZT_EventCallback eventCallback;
  1189. /**
  1190. * OPTIONAL: Function to check whether a given physical path should be used
  1191. */
  1192. ZT_PathCheckFunction pathCheckFunction;
  1193. /**
  1194. * OPTIONAL: Function to get hints to physical paths to ZeroTier addresses
  1195. */
  1196. ZT_PathLookupFunction pathLookupFunction;
  1197. };
  1198. /**
  1199. * Create a new ZeroTier node
  1200. *
  1201. * This will attempt to load its identity via the state get function in the
  1202. * callback struct. If that fails it will generate a new identity and store
  1203. * it. Identity generation can take anywhere from a few hundred milliseconds
  1204. * to a few seconds depending on your CPU speed.
  1205. *
  1206. * @param node Result: pointer is set to new node instance on success
  1207. * @param uptr User pointer to pass to functions/callbacks
  1208. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1209. * @param callbacks Callback function configuration
  1210. * @param now Current clock in milliseconds
  1211. * @return OK (0) or error code if a fatal error condition has occurred
  1212. */
  1213. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now);
  1214. /**
  1215. * Delete a node and free all resources it consumes
  1216. *
  1217. * If you are using multiple threads, all other threads must be shut down
  1218. * first. This can crash if processXXX() methods are in progress.
  1219. *
  1220. * @param node Node to delete
  1221. */
  1222. void ZT_Node_delete(ZT_Node *node);
  1223. /**
  1224. * Process a packet received from the physical wire
  1225. *
  1226. * @param node Node instance
  1227. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1228. * @param now Current clock in milliseconds
  1229. * @param localSocket Local socket (you can use 0 if only one local socket is bound and ignore this)
  1230. * @param remoteAddress Origin of packet
  1231. * @param packetData Packet data
  1232. * @param packetLength Packet length
  1233. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1234. * @return OK (0) or error code if a fatal error condition has occurred
  1235. */
  1236. enum ZT_ResultCode ZT_Node_processWirePacket(
  1237. ZT_Node *node,
  1238. void *tptr,
  1239. uint64_t now,
  1240. int64_t localSocket,
  1241. const struct sockaddr_storage *remoteAddress,
  1242. const void *packetData,
  1243. unsigned int packetLength,
  1244. volatile uint64_t *nextBackgroundTaskDeadline);
  1245. /**
  1246. * Process a frame from a virtual network port (tap)
  1247. *
  1248. * @param node Node instance
  1249. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1250. * @param now Current clock in milliseconds
  1251. * @param nwid ZeroTier 64-bit virtual network ID
  1252. * @param sourceMac Source MAC address (least significant 48 bits)
  1253. * @param destMac Destination MAC address (least significant 48 bits)
  1254. * @param etherType 16-bit Ethernet frame type
  1255. * @param vlanId 10-bit VLAN ID or 0 if none
  1256. * @param frameData Frame payload data
  1257. * @param frameLength Frame payload length
  1258. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1259. * @return OK (0) or error code if a fatal error condition has occurred
  1260. */
  1261. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  1262. ZT_Node *node,
  1263. void *tptr,
  1264. uint64_t now,
  1265. uint64_t nwid,
  1266. uint64_t sourceMac,
  1267. uint64_t destMac,
  1268. unsigned int etherType,
  1269. unsigned int vlanId,
  1270. const void *frameData,
  1271. unsigned int frameLength,
  1272. volatile uint64_t *nextBackgroundTaskDeadline);
  1273. /**
  1274. * Perform periodic background operations
  1275. *
  1276. * @param node Node instance
  1277. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1278. * @param now Current clock in milliseconds
  1279. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  1280. * @return OK (0) or error code if a fatal error condition has occurred
  1281. */
  1282. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
  1283. /**
  1284. * Join a network
  1285. *
  1286. * This may generate calls to the port config callback before it returns,
  1287. * or these may be deffered if a netconf is not available yet.
  1288. *
  1289. * If we are already a member of the network, nothing is done and OK is
  1290. * returned.
  1291. *
  1292. * @param node Node instance
  1293. * @param nwid 64-bit ZeroTier network ID
  1294. * @param uptr An arbitrary pointer to associate with this network (default: NULL)
  1295. * @return OK (0) or error code if a fatal error condition has occurred
  1296. */
  1297. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr);
  1298. /**
  1299. * Leave a network
  1300. *
  1301. * If a port has been configured for this network this will generate a call
  1302. * to the port config callback with a NULL second parameter to indicate that
  1303. * the port is now deleted.
  1304. *
  1305. * The uptr parameter is optional and is NULL by default. If it is not NULL,
  1306. * the pointer it points to is set to this network's uptr on success.
  1307. *
  1308. * @param node Node instance
  1309. * @param nwid 64-bit network ID
  1310. * @param uptr Target pointer is set to uptr (if not NULL)
  1311. * @return OK (0) or error code if a fatal error condition has occurred
  1312. */
  1313. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr);
  1314. /**
  1315. * Subscribe to an Ethernet multicast group
  1316. *
  1317. * ADI stands for additional distinguishing information. This defaults to zero
  1318. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  1319. * and this must be done.
  1320. *
  1321. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  1322. * broadcast address) but with an ADI equal to each IPv4 address in host
  1323. * byte order. This converts ARP from a non-scalable broadcast protocol to
  1324. * a scalable multicast protocol with perfect address specificity.
  1325. *
  1326. * If this is not done, ARP will not work reliably.
  1327. *
  1328. * Multiple calls to subscribe to the same multicast address will have no
  1329. * effect. It is perfectly safe to do this.
  1330. *
  1331. * This does not generate an update call to networkConfigCallback().
  1332. *
  1333. * @param node Node instance
  1334. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1335. * @param nwid 64-bit network ID
  1336. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1337. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1338. * @return OK (0) or error code if a fatal error condition has occurred
  1339. */
  1340. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1341. /**
  1342. * Unsubscribe from an Ethernet multicast group (or all groups)
  1343. *
  1344. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  1345. * you are not subscribed to a group this has no effect.
  1346. *
  1347. * This does not generate an update call to networkConfigCallback().
  1348. *
  1349. * @param node Node instance
  1350. * @param nwid 64-bit network ID
  1351. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1352. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1353. * @return OK (0) or error code if a fatal error condition has occurred
  1354. */
  1355. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1356. /**
  1357. * Add or update a moon
  1358. *
  1359. * Moons are persisted in the data store in moons.d/, so this can persist
  1360. * across invocations if the contents of moon.d are scanned and orbit is
  1361. * called for each on startup.
  1362. *
  1363. * @param node Node instance
  1364. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1365. * @param moonWorldId Moon's world ID
  1366. * @param moonSeed If non-zero, the ZeroTier address of any member of the moon to query for moon definition
  1367. * @param len Length of moonWorld in bytes
  1368. * @return Error if moon was invalid or failed to be added
  1369. */
  1370. enum ZT_ResultCode ZT_Node_orbit(ZT_Node *node,void *tptr,uint64_t moonWorldId,uint64_t moonSeed);
  1371. /**
  1372. * Remove a moon (does nothing if not present)
  1373. *
  1374. * @param node Node instance
  1375. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1376. * @param moonWorldId World ID of moon to remove
  1377. * @return Error if anything bad happened
  1378. */
  1379. enum ZT_ResultCode ZT_Node_deorbit(ZT_Node *node,void *tptr,uint64_t moonWorldId);
  1380. /**
  1381. * Get this node's 40-bit ZeroTier address
  1382. *
  1383. * @param node Node instance
  1384. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  1385. */
  1386. uint64_t ZT_Node_address(ZT_Node *node);
  1387. /**
  1388. * Get the status of this node
  1389. *
  1390. * @param node Node instance
  1391. * @param status Buffer to fill with current node status
  1392. */
  1393. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status);
  1394. /**
  1395. * Get a list of known peer nodes
  1396. *
  1397. * The pointer returned here must be freed with freeQueryResult()
  1398. * when you are done with it.
  1399. *
  1400. * @param node Node instance
  1401. * @return List of known peers or NULL on failure
  1402. */
  1403. ZT_PeerList *ZT_Node_peers(ZT_Node *node);
  1404. /**
  1405. * Get the status of a virtual network
  1406. *
  1407. * The pointer returned here must be freed with freeQueryResult()
  1408. * when you are done with it.
  1409. *
  1410. * @param node Node instance
  1411. * @param nwid 64-bit network ID
  1412. * @return Network configuration or NULL if we are not a member of this network
  1413. */
  1414. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid);
  1415. /**
  1416. * Enumerate and get status of all networks
  1417. *
  1418. * @param node Node instance
  1419. * @return List of networks or NULL on failure
  1420. */
  1421. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node);
  1422. /**
  1423. * Free a query result buffer
  1424. *
  1425. * Use this to free the return values of listNetworks(), listPeers(), etc.
  1426. *
  1427. * @param node Node instance
  1428. * @param qr Query result buffer
  1429. */
  1430. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr);
  1431. /**
  1432. * Add a local interface address
  1433. *
  1434. * This is used to make ZeroTier aware of those local interface addresses
  1435. * that you wish to use for ZeroTier communication. This is optional, and if
  1436. * it is not used ZeroTier will rely upon upstream peers (and roots) to
  1437. * perform empirical address discovery and NAT traversal. But the use of this
  1438. * method is recommended as it improves peer discovery when both peers are
  1439. * on the same LAN.
  1440. *
  1441. * It is the responsibility of the caller to take care that these are never
  1442. * ZeroTier interface addresses, whether these are assigned by ZeroTier or
  1443. * are otherwise assigned to an interface managed by this ZeroTier instance.
  1444. * This can cause recursion or other undesirable behavior.
  1445. *
  1446. * This returns a boolean indicating whether or not the address was
  1447. * accepted. ZeroTier will only communicate over certain address types
  1448. * and (for IP) address classes.
  1449. *
  1450. * @param addr Local interface address
  1451. * @return Boolean: non-zero if address was accepted and added
  1452. */
  1453. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr);
  1454. /**
  1455. * Clear local interface addresses
  1456. */
  1457. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node);
  1458. /**
  1459. * Send a VERB_USER_MESSAGE to another ZeroTier node
  1460. *
  1461. * There is no delivery guarantee here. Failure can occur if the message is
  1462. * too large or if dest is not a valid ZeroTier address.
  1463. *
  1464. * @param node Node instance
  1465. * @param tptr Thread pointer to pass to functions/callbacks resulting from this call
  1466. * @param dest Destination ZeroTier address
  1467. * @param typeId VERB_USER_MESSAGE type ID
  1468. * @param data Payload data to attach to user message
  1469. * @param len Length of data in bytes
  1470. * @return Boolean: non-zero on success, zero on failure
  1471. */
  1472. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len);
  1473. /**
  1474. * Set a network configuration master instance for this node
  1475. *
  1476. * Normal nodes should not need to use this. This is for nodes with
  1477. * special compiled-in support for acting as network configuration
  1478. * masters / controllers.
  1479. *
  1480. * The supplied instance must be a C++ object that inherits from the
  1481. * NetworkConfigMaster base class in node/. No type checking is performed,
  1482. * so a pointer to anything else will result in a crash.
  1483. *
  1484. * @param node ZertTier One node
  1485. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  1486. * @return OK (0) or error code if a fatal error condition has occurred
  1487. */
  1488. void ZT_Node_setNetconfMaster(ZT_Node *node,void *networkConfigMasterInstance);
  1489. /**
  1490. * Set trusted paths
  1491. *
  1492. * A trusted path is a physical network (network/bits) over which both
  1493. * encryption and authentication can be skipped to improve performance.
  1494. * Each trusted path must have a non-zero unique ID that is the same across
  1495. * all participating nodes.
  1496. *
  1497. * We don't recommend using trusted paths at all unless you really *need*
  1498. * near-bare-metal performance. Even on a LAN authentication and encryption
  1499. * are never a bad thing, and anything that introduces an "escape hatch"
  1500. * for encryption should be treated with the utmost care.
  1501. *
  1502. * Calling with NULL pointers for networks and ids and a count of zero clears
  1503. * all trusted paths.
  1504. *
  1505. * @param node Node instance
  1506. * @param networks Array of [count] networks
  1507. * @param ids Array of [count] corresponding non-zero path IDs (zero path IDs are ignored)
  1508. * @param count Number of trusted paths-- values greater than ZT_MAX_TRUSTED_PATHS are clipped
  1509. */
  1510. void ZT_Node_setTrustedPaths(ZT_Node *node,const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count);
  1511. /**
  1512. * Get ZeroTier One version
  1513. *
  1514. * @param major Result: major version
  1515. * @param minor Result: minor version
  1516. * @param revision Result: revision
  1517. */
  1518. void ZT_version(int *major,int *minor,int *revision);
  1519. #ifdef __cplusplus
  1520. }
  1521. #endif
  1522. #endif