ZeroTierOne.h 51 KB

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