ZeroTierOne.h 51 KB

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