ZeroTierOne.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. /*
  28. * This defines the external C API for ZeroTier's core network virtualization
  29. * engine.
  30. */
  31. #ifndef ZT_ZEROTIERONE_H
  32. #define ZT_ZEROTIERONE_H
  33. #include <stdint.h>
  34. // For the struct sockaddr_storage structure
  35. #if defined(_WIN32) || defined(_WIN64)
  36. #include <WinSock2.h>
  37. #include <WS2tcpip.h>
  38. #include <Windows.h>
  39. #else /* not Windows */
  40. #include <arpa/inet.h>
  41. #include <netinet/in.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #endif /* Windows or not */
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /****************************************************************************/
  49. /* Core constants */
  50. /****************************************************************************/
  51. /**
  52. * Default UDP port for devices running a ZeroTier endpoint
  53. */
  54. #define ZT_DEFAULT_PORT 9993
  55. /**
  56. * Maximum MTU for ZeroTier virtual networks
  57. *
  58. * This is pretty much an unchangeable global constant. To make it change
  59. * across nodes would require logic to send ICMP packet too big messages,
  60. * which would complicate things. 1500 has been good enough on most LANs
  61. * for ages, so a larger MTU should be fine for the forseeable future. This
  62. * typically results in two UDP packets per single large frame. Experimental
  63. * results seem to show that this is good. Larger MTUs resulting in more
  64. * fragments seemed too brittle on slow/crummy links for no benefit.
  65. *
  66. * If this does change, also change it in tap.h in the tuntaposx code under
  67. * mac-tap.
  68. *
  69. * Overhead for a normal frame split into two packets:
  70. *
  71. * 1414 = 1444 (typical UDP MTU) - 28 (packet header) - 2 (ethertype)
  72. * 1428 = 1444 (typical UDP MTU) - 16 (fragment header)
  73. * SUM: 2842
  74. *
  75. * We use 2800, which leaves some room for other payload in other types of
  76. * messages such as multicast propagation or future support for bridging.
  77. */
  78. #define ZT_MAX_MTU 2800
  79. /**
  80. * Maximum length of network short name
  81. */
  82. #define ZT_MAX_NETWORK_SHORT_NAME_LENGTH 255
  83. /**
  84. * Maximum number of statically assigned IP addresses per network endpoint using ZT address management (not DHCP)
  85. */
  86. #define ZT_MAX_ZT_ASSIGNED_ADDRESSES 16
  87. /**
  88. * Maximum number of multicast group subscriptions per network
  89. */
  90. #define ZT_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS 4096
  91. /**
  92. * Maximum number of direct network paths to a given peer
  93. */
  94. #define ZT_MAX_PEER_NETWORK_PATHS 4
  95. /**
  96. * Feature flag: ZeroTier One was built to be thread-safe -- concurrent processXXX() calls are okay
  97. */
  98. #define ZT_FEATURE_FLAG_THREAD_SAFE 0x00000001
  99. /**
  100. * Feature flag: FIPS compliant build (not available yet, but reserved for future use if we ever do this)
  101. */
  102. #define ZT_FEATURE_FLAG_FIPS 0x00000002
  103. /**
  104. * Maximum number of hops in a ZeroTier circuit test
  105. *
  106. * This is more or less the max that can be fit in a given packet (with
  107. * fragmentation) and only one address per hop.
  108. */
  109. #define ZT_CIRCUIT_TEST_MAX_HOPS 512
  110. /**
  111. * Maximum number of addresses per hop in a circuit test
  112. */
  113. #define ZT_CIRCUIT_TEST_MAX_HOP_BREADTH 256
  114. /**
  115. * A null/empty sockaddr (all zero) to signify an unspecified socket address
  116. */
  117. extern const struct sockaddr_storage ZT_SOCKADDR_NULL;
  118. /****************************************************************************/
  119. /* Structures and other types */
  120. /****************************************************************************/
  121. /**
  122. * Function return code: OK (0) or error results
  123. *
  124. * Use ZT_ResultCode_isFatal() to check for a fatal error. If a fatal error
  125. * occurs, the node should be considered to not be working correctly. These
  126. * indicate serious problems like an inaccessible data store or a compile
  127. * problem.
  128. */
  129. enum ZT_ResultCode
  130. {
  131. /**
  132. * Operation completed normally
  133. */
  134. ZT_RESULT_OK = 0,
  135. // Fatal errors (>0, <1000)
  136. /**
  137. * Ran out of memory
  138. */
  139. ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 1,
  140. /**
  141. * Data store is not writable or has failed
  142. */
  143. ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 2,
  144. /**
  145. * Internal error (e.g. unexpected exception indicating bug or build problem)
  146. */
  147. ZT_RESULT_FATAL_ERROR_INTERNAL = 3,
  148. // Non-fatal errors (>1000)
  149. /**
  150. * Network ID not valid
  151. */
  152. ZT_RESULT_ERROR_NETWORK_NOT_FOUND = 1000
  153. };
  154. /**
  155. * @param x Result code
  156. * @return True if result code indicates a fatal error
  157. */
  158. #define ZT_ResultCode_isFatal(x) ((((int)(x)) > 0)&&(((int)(x)) < 1000))
  159. /**
  160. * Status codes sent to status update callback when things happen
  161. */
  162. enum ZT_Event
  163. {
  164. /**
  165. * Node has been initialized
  166. *
  167. * This is the first event generated, and is always sent. It may occur
  168. * before Node's constructor returns.
  169. *
  170. * Meta-data: none
  171. */
  172. ZT_EVENT_UP = 0,
  173. /**
  174. * Node is offline -- network does not seem to be reachable by any available strategy
  175. *
  176. * Meta-data: none
  177. */
  178. ZT_EVENT_OFFLINE = 1,
  179. /**
  180. * Node is online -- at least one upstream node appears reachable
  181. *
  182. * Meta-data: none
  183. */
  184. ZT_EVENT_ONLINE = 2,
  185. /**
  186. * Node is shutting down
  187. *
  188. * This is generated within Node's destructor when it is being shut down.
  189. * It's done for convenience, since cleaning up other state in the event
  190. * handler may appear more idiomatic.
  191. *
  192. * Meta-data: none
  193. */
  194. ZT_EVENT_DOWN = 3,
  195. /**
  196. * Your identity has collided with another node's ZeroTier address
  197. *
  198. * This happens if two different public keys both hash (via the algorithm
  199. * in Identity::generate()) to the same 40-bit ZeroTier address.
  200. *
  201. * This is something you should "never" see, where "never" is defined as
  202. * once per 2^39 new node initializations / identity creations. If you do
  203. * see it, you're going to see it very soon after a node is first
  204. * initialized.
  205. *
  206. * This is reported as an event rather than a return code since it's
  207. * detected asynchronously via error messages from authoritative nodes.
  208. *
  209. * If this occurs, you must shut down and delete the node, delete the
  210. * identity.secret record/file from the data store, and restart to generate
  211. * a new identity. If you don't do this, you will not be able to communicate
  212. * with other nodes.
  213. *
  214. * We'd automate this process, but we don't think silently deleting
  215. * private keys or changing our address without telling the calling code
  216. * is good form. It violates the principle of least surprise.
  217. *
  218. * You can technically get away with not handling this, but we recommend
  219. * doing so in a mature reliable application. Besides, handling this
  220. * condition is a good way to make sure it never arises. It's like how
  221. * umbrellas prevent rain and smoke detectors prevent fires. They do, right?
  222. *
  223. * Meta-data: none
  224. */
  225. ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION = 4,
  226. /**
  227. * Trace (debugging) message
  228. *
  229. * These events are only generated if this is a TRACE-enabled build.
  230. *
  231. * Meta-data: C string, TRACE message
  232. */
  233. ZT_EVENT_TRACE = 5
  234. };
  235. /**
  236. * Current node status
  237. */
  238. typedef struct
  239. {
  240. /**
  241. * 40-bit ZeroTier address of this node
  242. */
  243. uint64_t address;
  244. /**
  245. * Public identity in string-serialized form (safe to send to others)
  246. *
  247. * This pointer will remain valid as long as the node exists.
  248. */
  249. const char *publicIdentity;
  250. /**
  251. * Full identity including secret key in string-serialized form
  252. *
  253. * This pointer will remain valid as long as the node exists.
  254. */
  255. const char *secretIdentity;
  256. /**
  257. * True if some kind of connectivity appears available
  258. */
  259. int online;
  260. } ZT_NodeStatus;
  261. /**
  262. * Virtual network status codes
  263. */
  264. enum ZT_VirtualNetworkStatus
  265. {
  266. /**
  267. * Waiting for network configuration (also means revision == 0)
  268. */
  269. ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  270. /**
  271. * Configuration received and we are authorized
  272. */
  273. ZT_NETWORK_STATUS_OK = 1,
  274. /**
  275. * Netconf master told us 'nope'
  276. */
  277. ZT_NETWORK_STATUS_ACCESS_DENIED = 2,
  278. /**
  279. * Netconf master exists, but this virtual network does not
  280. */
  281. ZT_NETWORK_STATUS_NOT_FOUND = 3,
  282. /**
  283. * Initialization of network failed or other internal error
  284. */
  285. ZT_NETWORK_STATUS_PORT_ERROR = 4,
  286. /**
  287. * ZeroTier One version too old
  288. */
  289. ZT_NETWORK_STATUS_CLIENT_TOO_OLD = 5
  290. };
  291. /**
  292. * Virtual network type codes
  293. */
  294. enum ZT_VirtualNetworkType
  295. {
  296. /**
  297. * Private networks are authorized via certificates of membership
  298. */
  299. ZT_NETWORK_TYPE_PRIVATE = 0,
  300. /**
  301. * Public networks have no access control -- they'll always be AUTHORIZED
  302. */
  303. ZT_NETWORK_TYPE_PUBLIC = 1
  304. };
  305. /**
  306. * An Ethernet multicast group
  307. */
  308. typedef struct
  309. {
  310. /**
  311. * MAC address (least significant 48 bits)
  312. */
  313. uint64_t mac;
  314. /**
  315. * Additional distinguishing information (usually zero)
  316. */
  317. unsigned long adi;
  318. } ZT_MulticastGroup;
  319. /**
  320. * Virtual network configuration update type
  321. */
  322. enum ZT_VirtualNetworkConfigOperation
  323. {
  324. /**
  325. * Network is coming up (either for the first time or after service restart)
  326. */
  327. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  328. /**
  329. * Network configuration has been updated
  330. */
  331. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  332. /**
  333. * Network is going down (not permanently)
  334. */
  335. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  336. /**
  337. * Network is going down permanently (leave/delete)
  338. */
  339. ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  340. };
  341. /**
  342. * Local interface trust levels
  343. */
  344. enum ZT_LocalInterfaceAddressTrust {
  345. ZT_LOCAL_INTERFACE_ADDRESS_TRUST_NORMAL = 0,
  346. ZT_LOCAL_INTERFACE_ADDRESS_TRUST_PRIVACY = 10,
  347. ZT_LOCAL_INTERFACE_ADDRESS_TRUST_ULTIMATE = 20
  348. };
  349. /**
  350. * What trust hierarchy role does this peer have?
  351. */
  352. enum ZT_PeerRole {
  353. ZT_PEER_ROLE_LEAF = 0, // ordinary node
  354. ZT_PEER_ROLE_RELAY = 1, // relay node
  355. ZT_PEER_ROLE_ROOT = 2 // root server
  356. };
  357. /**
  358. * Vendor ID
  359. */
  360. enum ZT_Vendor {
  361. ZT_VENDOR_UNSPECIFIED = 0,
  362. ZT_VENDOR_ZEROTIER = 1
  363. };
  364. /**
  365. * Platform type
  366. */
  367. enum ZT_Platform {
  368. ZT_PLATFORM_UNSPECIFIED = 0,
  369. ZT_PLATFORM_LINUX = 1,
  370. ZT_PLATFORM_WINDOWS = 2,
  371. ZT_PLATFORM_MACOS = 3,
  372. ZT_PLATFORM_ANDROID = 4,
  373. ZT_PLATFORM_IOS = 5,
  374. ZT_PLATFORM_SOLARIS_SMARTOS = 6,
  375. ZT_PLATFORM_FREEBSD = 7,
  376. ZT_PLATFORM_NETBSD = 8,
  377. ZT_PLATFORM_OPENBSD = 9,
  378. ZT_PLATFORM_RISCOS = 10,
  379. ZT_PLATFORM_VXWORKS = 11,
  380. ZT_PLATFORM_FREERTOS = 12,
  381. ZT_PLATFORM_SYSBIOS = 13,
  382. ZT_PLATFORM_HURD = 14
  383. };
  384. /**
  385. * Architecture type
  386. */
  387. enum ZT_Architecture {
  388. ZT_ARCHITECTURE_UNSPECIFIED = 0,
  389. ZT_ARCHITECTURE_X86 = 1,
  390. ZT_ARCHITECTURE_X64 = 2,
  391. ZT_ARCHITECTURE_ARM32 = 3,
  392. ZT_ARCHITECTURE_ARM64 = 4,
  393. ZT_ARCHITECTURE_MIPS32 = 5,
  394. ZT_ARCHITECTURE_MIPS64 = 6,
  395. ZT_ARCHITECTURE_POWER32 = 7,
  396. ZT_ARCHITECTURE_POWER64 = 8
  397. };
  398. /**
  399. * Virtual network configuration
  400. */
  401. typedef struct
  402. {
  403. /**
  404. * 64-bit ZeroTier network ID
  405. */
  406. uint64_t nwid;
  407. /**
  408. * Ethernet MAC (48 bits) that should be assigned to port
  409. */
  410. uint64_t mac;
  411. /**
  412. * Network name (from network configuration master)
  413. */
  414. char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  415. /**
  416. * Network configuration request status
  417. */
  418. enum ZT_VirtualNetworkStatus status;
  419. /**
  420. * Network type
  421. */
  422. enum ZT_VirtualNetworkType type;
  423. /**
  424. * Maximum interface MTU
  425. */
  426. unsigned int mtu;
  427. /**
  428. * If nonzero, the network this port belongs to indicates DHCP availability
  429. *
  430. * This is a suggestion. The underlying implementation is free to ignore it
  431. * for security or other reasons. This is simply a netconf parameter that
  432. * means 'DHCP is available on this network.'
  433. */
  434. int dhcp;
  435. /**
  436. * If nonzero, this port is allowed to bridge to other networks
  437. *
  438. * This is informational. If this is false (0), bridged packets will simply
  439. * be dropped and bridging won't work.
  440. */
  441. int bridge;
  442. /**
  443. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  444. */
  445. int broadcastEnabled;
  446. /**
  447. * If the network is in PORT_ERROR state, this is the error most recently returned by the port config callback
  448. */
  449. int portError;
  450. /**
  451. * Is this network enabled? If not, all frames to/from are dropped.
  452. */
  453. int enabled;
  454. /**
  455. * Network config revision as reported by netconf master
  456. *
  457. * If this is zero, it means we're still waiting for our netconf.
  458. */
  459. unsigned long netconfRevision;
  460. /**
  461. * Number of multicast group subscriptions
  462. */
  463. unsigned int multicastSubscriptionCount;
  464. /**
  465. * Multicast group subscriptions
  466. */
  467. ZT_MulticastGroup multicastSubscriptions[ZT_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS];
  468. /**
  469. * Number of assigned addresses
  470. */
  471. unsigned int assignedAddressCount;
  472. /**
  473. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  474. *
  475. * For IP, the port number of the sockaddr_XX structure contains the number
  476. * of bits in the address netmask. Only the IP address and port are used.
  477. * Other fields like interface number can be ignored.
  478. *
  479. * This is only used for ZeroTier-managed address assignments sent by the
  480. * virtual network's configuration master.
  481. */
  482. struct sockaddr_storage assignedAddresses[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
  483. } ZT_VirtualNetworkConfig;
  484. /**
  485. * A list of networks
  486. */
  487. typedef struct
  488. {
  489. ZT_VirtualNetworkConfig *networks;
  490. unsigned long networkCount;
  491. } ZT_VirtualNetworkList;
  492. /**
  493. * Physical network path to a peer
  494. */
  495. typedef struct
  496. {
  497. /**
  498. * Address of endpoint
  499. */
  500. struct sockaddr_storage address;
  501. /**
  502. * Time of last send in milliseconds or 0 for never
  503. */
  504. uint64_t lastSend;
  505. /**
  506. * Time of last receive in milliseconds or 0 for never
  507. */
  508. uint64_t lastReceive;
  509. /**
  510. * Is path fixed? (i.e. not learned, static)
  511. */
  512. int fixed;
  513. /**
  514. * Is path active?
  515. */
  516. int active;
  517. /**
  518. * Is path preferred?
  519. */
  520. int preferred;
  521. } ZT_PeerPhysicalPath;
  522. /**
  523. * Peer status result buffer
  524. */
  525. typedef struct
  526. {
  527. /**
  528. * ZeroTier address (40 bits)
  529. */
  530. uint64_t address;
  531. /**
  532. * Time we last received a unicast frame from this peer
  533. */
  534. uint64_t lastUnicastFrame;
  535. /**
  536. * Time we last received a multicast rame from this peer
  537. */
  538. uint64_t lastMulticastFrame;
  539. /**
  540. * Remote major version or -1 if not known
  541. */
  542. int versionMajor;
  543. /**
  544. * Remote minor version or -1 if not known
  545. */
  546. int versionMinor;
  547. /**
  548. * Remote revision or -1 if not known
  549. */
  550. int versionRev;
  551. /**
  552. * Last measured latency in milliseconds or zero if unknown
  553. */
  554. unsigned int latency;
  555. /**
  556. * What trust hierarchy role does this device have?
  557. */
  558. enum ZT_PeerRole role;
  559. /**
  560. * Number of paths (size of paths[])
  561. */
  562. unsigned int pathCount;
  563. /**
  564. * Known network paths to peer
  565. */
  566. ZT_PeerPhysicalPath paths[ZT_MAX_PEER_NETWORK_PATHS];
  567. } ZT_Peer;
  568. /**
  569. * List of peers
  570. */
  571. typedef struct
  572. {
  573. ZT_Peer *peers;
  574. unsigned long peerCount;
  575. } ZT_PeerList;
  576. /**
  577. * ZeroTier circuit test configuration and path
  578. */
  579. typedef struct {
  580. /**
  581. * Test ID -- an arbitrary 64-bit identifier
  582. */
  583. uint64_t testId;
  584. /**
  585. * Timestamp -- sent with test and echoed back by each reporter
  586. */
  587. uint64_t timestamp;
  588. /**
  589. * Originator credential: network ID
  590. *
  591. * If this is nonzero, a network ID will be set for this test and
  592. * the originator must be its primary network controller. This is
  593. * currently the only authorization method available, so it must
  594. * be set to run a test.
  595. */
  596. uint64_t credentialNetworkId;
  597. /**
  598. * Hops in circuit test (a.k.a. FIFO for graph traversal)
  599. */
  600. struct {
  601. /**
  602. * Hop flags (currently unused, must be zero)
  603. */
  604. unsigned int flags;
  605. /**
  606. * Number of addresses in this hop (max: ZT_CIRCUIT_TEST_MAX_HOP_BREADTH)
  607. */
  608. unsigned int breadth;
  609. /**
  610. * 40-bit ZeroTier addresses (most significant 24 bits ignored)
  611. */
  612. uint64_t addresses[ZT_CIRCUIT_TEST_MAX_HOP_BREADTH];
  613. } hops[ZT_CIRCUIT_TEST_MAX_HOPS];
  614. /**
  615. * Number of hops (max: ZT_CIRCUIT_TEST_MAX_HOPS)
  616. */
  617. unsigned int hopCount;
  618. /**
  619. * If non-zero, circuit test will report back at every hop
  620. */
  621. int reportAtEveryHop;
  622. /**
  623. * An arbitrary user-settable pointer
  624. */
  625. void *ptr;
  626. /**
  627. * Pointer for internal use -- initialize to zero and do not modify
  628. */
  629. void *_internalPtr;
  630. } ZT_CircuitTest;
  631. /**
  632. * Circuit test result report
  633. */
  634. typedef struct {
  635. /**
  636. * Sender of report
  637. */
  638. uint64_t address;
  639. /**
  640. * 64-bit test ID
  641. */
  642. uint64_t testId;
  643. /**
  644. * Timestamp from original test (echoed back at each hop)
  645. */
  646. uint64_t timestamp;
  647. /**
  648. * Timestamp on remote device
  649. */
  650. uint64_t remoteTimestamp;
  651. /**
  652. * 64-bit packet ID of packet received by the reporting device
  653. */
  654. uint64_t sourcePacketId;
  655. /**
  656. * Flags (currently unused, will be zero)
  657. */
  658. uint64_t flags;
  659. /**
  660. * ZeroTier protocol-level hop count of packet received by reporting device (>0 indicates relayed)
  661. */
  662. unsigned int sourcePacketHopCount;
  663. /**
  664. * Error code (currently unused, will be zero)
  665. */
  666. unsigned int errorCode;
  667. /**
  668. * Remote device vendor ID
  669. */
  670. enum ZT_Vendor vendor;
  671. /**
  672. * Remote device protocol compliance version
  673. */
  674. unsigned int protocolVersion;
  675. /**
  676. * Software major version
  677. */
  678. unsigned int majorVersion;
  679. /**
  680. * Software minor version
  681. */
  682. unsigned int minorVersion;
  683. /**
  684. * Software revision
  685. */
  686. unsigned int revision;
  687. /**
  688. * Platform / OS
  689. */
  690. enum ZT_Platform platform;
  691. /**
  692. * System architecture
  693. */
  694. enum ZT_Architecture architecture;
  695. /**
  696. * Local device address on which packet was received by reporting device
  697. *
  698. * This may have ss_family equal to zero (null address) if unspecified.
  699. */
  700. struct sockaddr_storage receivedOnLocalAddress;
  701. /**
  702. * Remote address from which reporter received the test packet
  703. *
  704. * This may have ss_family set to zero (null address) if unspecified.
  705. */
  706. struct sockaddr_storage receivedFromRemoteAddress;
  707. /**
  708. * Next hops to which packets are being or will be sent by the reporter
  709. *
  710. * In addition to reporting back, the reporter may send the test on if
  711. * there are more recipients in the FIFO. If it does this, it can report
  712. * back the address(es) that make up the next hop and the physical address
  713. * for each if it has one. The physical address being null/unspecified
  714. * typically indicates that no direct path exists and the next packet
  715. * will be relayed.
  716. */
  717. struct {
  718. /**
  719. * 40-bit ZeroTier address
  720. */
  721. uint64_t address;
  722. /**
  723. * Physical address or null address (ss_family == 0) if unspecified or unknown
  724. */
  725. struct sockaddr_storage physicalAddress;
  726. } nextHops[ZT_CIRCUIT_TEST_MAX_HOP_BREADTH];
  727. /**
  728. * Number of next hops reported in nextHops[]
  729. */
  730. unsigned int nextHopCount;
  731. } ZT_CircuitTestReport;
  732. /**
  733. * An instance of a ZeroTier One node (opaque)
  734. */
  735. typedef void ZT_Node;
  736. /****************************************************************************/
  737. /* Callbacks used by Node API */
  738. /****************************************************************************/
  739. /**
  740. * Callback called to update virtual network port configuration
  741. *
  742. * This can be called at any time to update the configuration of a virtual
  743. * network port. The parameter after the network ID specifies whether this
  744. * port is being brought up, updated, brought down, or permanently deleted.
  745. *
  746. * This in turn should be used by the underlying implementation to create
  747. * and configure tap devices at the OS (or virtual network stack) layer.
  748. *
  749. * The supplied config pointer is not guaranteed to remain valid, so make
  750. * a copy if you want one.
  751. *
  752. * This should not call multicastSubscribe() or other network-modifying
  753. * methods, as this could cause a deadlock in multithreaded or interrupt
  754. * driven environments.
  755. *
  756. * This must return 0 on success. It can return any OS-dependent error code
  757. * on failure, and this results in the network being placed into the
  758. * PORT_ERROR state.
  759. */
  760. typedef int (*ZT_VirtualNetworkConfigFunction)(
  761. ZT_Node *,
  762. void *,
  763. uint64_t,
  764. enum ZT_VirtualNetworkConfigOperation,
  765. const ZT_VirtualNetworkConfig *);
  766. /**
  767. * Function to send a frame out to a virtual network port
  768. *
  769. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  770. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  771. * (9) frame length.
  772. */
  773. typedef void (*ZT_VirtualNetworkFrameFunction)(
  774. ZT_Node *,
  775. void *,
  776. uint64_t,
  777. uint64_t,
  778. uint64_t,
  779. unsigned int,
  780. unsigned int,
  781. const void *,
  782. unsigned int);
  783. /**
  784. * Callback for events
  785. *
  786. * Events are generated when the node's status changes in a significant way
  787. * and on certain non-fatal errors and events of interest. The final void
  788. * parameter points to event meta-data. The type of event meta-data (and
  789. * whether it is present at all) is event type dependent. See the comments
  790. * in the definition of ZT_Event.
  791. */
  792. typedef void (*ZT_EventCallback)(
  793. ZT_Node *,
  794. void *,
  795. enum ZT_Event,
  796. const void *);
  797. /**
  798. * Function to get an object from the data store
  799. *
  800. * Parameters: (1) object name, (2) buffer to fill, (3) size of buffer, (4)
  801. * index in object to start reading, (5) result parameter that must be set
  802. * to the actual size of the object if it exists.
  803. *
  804. * Object names can contain forward slash (/) path separators. They will
  805. * never contain .. or backslash (\), so this is safe to map as a Unix-style
  806. * path if the underlying storage permits. For security reasons we recommend
  807. * returning errors if .. or \ are used.
  808. *
  809. * The function must return the actual number of bytes read. If the object
  810. * doesn't exist, it should return -1. -2 should be returned on other errors
  811. * such as errors accessing underlying storage.
  812. *
  813. * If the read doesn't fit in the buffer, the max number of bytes should be
  814. * read. The caller may call the function multiple times to read the whole
  815. * object.
  816. */
  817. typedef long (*ZT_DataStoreGetFunction)(
  818. ZT_Node *,
  819. void *,
  820. const char *,
  821. void *,
  822. unsigned long,
  823. unsigned long,
  824. unsigned long *);
  825. /**
  826. * Function to store an object in the data store
  827. *
  828. * Parameters: (1) node, (2) user ptr, (3) object name, (4) object data,
  829. * (5) object size, (6) secure? (bool).
  830. *
  831. * If secure is true, the file should be set readable and writable only
  832. * to the user running ZeroTier One. What this means is platform-specific.
  833. *
  834. * Name semantics are the same as the get function. This must return zero on
  835. * success. You can return any OS-specific error code on failure, as these
  836. * may be visible in logs or error messages and might aid in debugging.
  837. *
  838. * If the data pointer is null, this must be interpreted as a delete
  839. * operation.
  840. */
  841. typedef int (*ZT_DataStorePutFunction)(
  842. ZT_Node *,
  843. void *,
  844. const char *,
  845. const void *,
  846. unsigned long,
  847. int);
  848. /**
  849. * Function to send a ZeroTier packet out over the wire
  850. *
  851. * Parameters:
  852. * (1) Node
  853. * (2) User pointer
  854. * (3) Local interface address
  855. * (4) Remote address
  856. * (5) Packet data
  857. * (6) Packet length
  858. *
  859. * If there is only one local interface it is safe to ignore the local
  860. * interface address. Otherwise if running with multiple interfaces, the
  861. * correct local interface should be chosen by address unless NULL. If
  862. * the ss_family field is zero (NULL address), a random or preferred
  863. * default interface should be used.
  864. *
  865. * The function must return zero on success and may return any error code
  866. * on failure. Note that success does not (of course) guarantee packet
  867. * delivery. It only means that the packet appears to have been sent.
  868. */
  869. typedef int (*ZT_WirePacketSendFunction)(
  870. ZT_Node *, /* Node */
  871. void *, /* User ptr */
  872. const struct sockaddr_storage *, /* Local address */
  873. const struct sockaddr_storage *, /* Remote address */
  874. const void *, /* Packet data */
  875. unsigned int); /* Packet length */
  876. /****************************************************************************/
  877. /* C Node API */
  878. /****************************************************************************/
  879. /**
  880. * Create a new ZeroTier One node
  881. *
  882. * Note that this can take a few seconds the first time it's called, as it
  883. * will generate an identity.
  884. *
  885. * @param node Result: pointer is set to new node instance on success
  886. * @param uptr User pointer to pass to functions/callbacks
  887. * @param now Current clock in milliseconds
  888. * @param dataStoreGetFunction Function called to get objects from persistent storage
  889. * @param dataStorePutFunction Function called to put objects in persistent storage
  890. * @param virtualNetworkConfigFunction Function to be called when virtual LANs are created, deleted, or their config parameters change
  891. * @param eventCallback Function to receive status updates and non-fatal error notices
  892. * @param overrideRootTopology Alternative root server topology or NULL for default (mostly for test/debug use)
  893. * @return OK (0) or error code if a fatal error condition has occurred
  894. */
  895. enum ZT_ResultCode ZT_Node_new(
  896. ZT_Node **node,
  897. void *uptr,
  898. uint64_t now,
  899. ZT_DataStoreGetFunction dataStoreGetFunction,
  900. ZT_DataStorePutFunction dataStorePutFunction,
  901. ZT_WirePacketSendFunction wirePacketSendFunction,
  902. ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  903. ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  904. ZT_EventCallback eventCallback,
  905. const char *overrideRootTopology);
  906. /**
  907. * Delete a node and free all resources it consumes
  908. *
  909. * If you are using multiple threads, all other threads must be shut down
  910. * first. This can crash if processXXX() methods are in progress.
  911. *
  912. * @param node Node to delete
  913. */
  914. void ZT_Node_delete(ZT_Node *node);
  915. /**
  916. * Process a packet received from the physical wire
  917. *
  918. * @param node Node instance
  919. * @param now Current clock in milliseconds
  920. * @param localAddress Local address, or point to ZT_SOCKADDR_NULL if unspecified
  921. * @param remoteAddress Origin of packet
  922. * @param packetData Packet data
  923. * @param packetLength Packet length
  924. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  925. * @return OK (0) or error code if a fatal error condition has occurred
  926. */
  927. enum ZT_ResultCode ZT_Node_processWirePacket(
  928. ZT_Node *node,
  929. uint64_t now,
  930. const struct sockaddr_storage *localAddress,
  931. const struct sockaddr_storage *remoteAddress,
  932. const void *packetData,
  933. unsigned int packetLength,
  934. volatile uint64_t *nextBackgroundTaskDeadline);
  935. /**
  936. * Process a frame from a virtual network port (tap)
  937. *
  938. * @param node Node instance
  939. * @param now Current clock in milliseconds
  940. * @param nwid ZeroTier 64-bit virtual network ID
  941. * @param sourceMac Source MAC address (least significant 48 bits)
  942. * @param destMac Destination MAC address (least significant 48 bits)
  943. * @param etherType 16-bit Ethernet frame type
  944. * @param vlanId 10-bit VLAN ID or 0 if none
  945. * @param frameData Frame payload data
  946. * @param frameLength Frame payload length
  947. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  948. * @return OK (0) or error code if a fatal error condition has occurred
  949. */
  950. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  951. ZT_Node *node,
  952. uint64_t now,
  953. uint64_t nwid,
  954. uint64_t sourceMac,
  955. uint64_t destMac,
  956. unsigned int etherType,
  957. unsigned int vlanId,
  958. const void *frameData,
  959. unsigned int frameLength,
  960. volatile uint64_t *nextBackgroundTaskDeadline);
  961. /**
  962. * Perform periodic background operations
  963. *
  964. * @param node Node instance
  965. * @param now Current clock in milliseconds
  966. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  967. * @return OK (0) or error code if a fatal error condition has occurred
  968. */
  969. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
  970. /**
  971. * Join a network
  972. *
  973. * This may generate calls to the port config callback before it returns,
  974. * or these may be deffered if a netconf is not available yet.
  975. *
  976. * If we are already a member of the network, nothing is done and OK is
  977. * returned.
  978. *
  979. * @param node Node instance
  980. * @param nwid 64-bit ZeroTier network ID
  981. * @return OK (0) or error code if a fatal error condition has occurred
  982. */
  983. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid);
  984. /**
  985. * Leave a network
  986. *
  987. * If a port has been configured for this network this will generate a call
  988. * to the port config callback with a NULL second parameter to indicate that
  989. * the port is now deleted.
  990. *
  991. * @param node Node instance
  992. * @param nwid 64-bit network ID
  993. * @return OK (0) or error code if a fatal error condition has occurred
  994. */
  995. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid);
  996. /**
  997. * Subscribe to an Ethernet multicast group
  998. *
  999. * ADI stands for additional distinguishing information. This defaults to zero
  1000. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  1001. * and this must be done.
  1002. *
  1003. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  1004. * broadcast address) but with an ADI equal to each IPv4 address in host
  1005. * byte order. This converts ARP from a non-scalable broadcast protocol to
  1006. * a scalable multicast protocol with perfect address specificity.
  1007. *
  1008. * If this is not done, ARP will not work reliably.
  1009. *
  1010. * Multiple calls to subscribe to the same multicast address will have no
  1011. * effect. It is perfectly safe to do this.
  1012. *
  1013. * This does not generate an update call to networkConfigCallback().
  1014. *
  1015. * @param node Node instance
  1016. * @param nwid 64-bit network ID
  1017. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1018. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1019. * @return OK (0) or error code if a fatal error condition has occurred
  1020. */
  1021. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1022. /**
  1023. * Unsubscribe from an Ethernet multicast group (or all groups)
  1024. *
  1025. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  1026. * you are not subscribed to a group this has no effect.
  1027. *
  1028. * This does not generate an update call to networkConfigCallback().
  1029. *
  1030. * @param node Node instance
  1031. * @param nwid 64-bit network ID
  1032. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  1033. * @param multicastAdi Multicast ADI (least significant 32 bits only, use 0 if not needed)
  1034. * @return OK (0) or error code if a fatal error condition has occurred
  1035. */
  1036. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  1037. /**
  1038. * Get this node's 40-bit ZeroTier address
  1039. *
  1040. * @param node Node instance
  1041. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  1042. */
  1043. uint64_t ZT_Node_address(ZT_Node *node);
  1044. /**
  1045. * Get the status of this node
  1046. *
  1047. * @param node Node instance
  1048. * @param status Buffer to fill with current node status
  1049. */
  1050. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status);
  1051. /**
  1052. * Get a list of known peer nodes
  1053. *
  1054. * The pointer returned here must be freed with freeQueryResult()
  1055. * when you are done with it.
  1056. *
  1057. * @param node Node instance
  1058. * @return List of known peers or NULL on failure
  1059. */
  1060. ZT_PeerList *ZT_Node_peers(ZT_Node *node);
  1061. /**
  1062. * Get the status of a virtual network
  1063. *
  1064. * The pointer returned here must be freed with freeQueryResult()
  1065. * when you are done with it.
  1066. *
  1067. * @param node Node instance
  1068. * @param nwid 64-bit network ID
  1069. * @return Network configuration or NULL if we are not a member of this network
  1070. */
  1071. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid);
  1072. /**
  1073. * Enumerate and get status of all networks
  1074. *
  1075. * @param node Node instance
  1076. * @return List of networks or NULL on failure
  1077. */
  1078. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node);
  1079. /**
  1080. * Free a query result buffer
  1081. *
  1082. * Use this to free the return values of listNetworks(), listPeers(), etc.
  1083. *
  1084. * @param node Node instance
  1085. * @param qr Query result buffer
  1086. */
  1087. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr);
  1088. /**
  1089. * Add a local interface address
  1090. *
  1091. * Local interface addresses may be added if you want remote peers
  1092. * with whom you have a trust relatinship (e.g. common network membership)
  1093. * to receive information about these endpoints as potential endpoints for
  1094. * direct communication.
  1095. *
  1096. * Take care that these are never ZeroTier interface addresses, otherwise
  1097. * strange things might happen or they simply won't work.
  1098. *
  1099. * Addresses can also be added here if they are the result of a UPnP or
  1100. * NAT-PMP port mapping or other discovery or mapping means.
  1101. *
  1102. * This returns a boolean indicating whether or not the address was
  1103. * accepted. ZeroTier will only communicate over certain address types
  1104. * and (for IP) address classes. Thus it's safe to just dump your OS's
  1105. * entire remote IP list (excluding ZeroTier interface IPs) into here
  1106. * and let ZeroTier determine which addresses it will use. It will
  1107. * reject bad, empty, and unusable addresses.
  1108. *
  1109. * @param addr Local interface address
  1110. * @param metric Local interface metric
  1111. * @param trust How much do you trust the local network under this interface?
  1112. * @return Boolean: non-zero if address was accepted and added
  1113. */
  1114. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr,int metric, enum ZT_LocalInterfaceAddressTrust trust);
  1115. /**
  1116. * Clear local interface addresses
  1117. */
  1118. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node);
  1119. /**
  1120. * Set a network configuration master instance for this node
  1121. *
  1122. * Normal nodes should not need to use this. This is for nodes with
  1123. * special compiled-in support for acting as network configuration
  1124. * masters / controllers.
  1125. *
  1126. * The supplied instance must be a C++ object that inherits from the
  1127. * NetworkConfigMaster base class in node/. No type checking is performed,
  1128. * so a pointer to anything else will result in a crash.
  1129. *
  1130. * @param node ZertTier One node
  1131. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  1132. * @return OK (0) or error code if a fatal error condition has occurred
  1133. */
  1134. void ZT_Node_setNetconfMaster(ZT_Node *node,void *networkConfigMasterInstance);
  1135. /**
  1136. * Initiate a VL1 circuit test
  1137. *
  1138. * This sends an initial VERB_CIRCUIT_TEST and reports results back to the
  1139. * supplied callback until circuitTestEnd() is called. The supplied
  1140. * ZT_CircuitTest structure should be initially zeroed and then filled
  1141. * in with settings and hops.
  1142. *
  1143. * It is the caller's responsibility to call circuitTestEnd() and then
  1144. * to dispose of the test structure. Otherwise this node will listen
  1145. * for results forever.
  1146. *
  1147. * @param node Node instance
  1148. * @param test Test configuration
  1149. * @param reportCallback Function to call each time a report is received
  1150. * @return OK or error if, for example, test is too big for a packet or support isn't compiled in
  1151. */
  1152. enum ZT_ResultCode ZT_Node_circuitTestBegin(ZT_Node *node,ZT_CircuitTest *test,void (*reportCallback)(ZT_Node *, ZT_CircuitTest *,const ZT_CircuitTestReport *));
  1153. /**
  1154. * Stop listening for results to a given circuit test
  1155. *
  1156. * This does not free the 'test' structure. The caller may do that
  1157. * after calling this method to unregister it.
  1158. *
  1159. * Any reports that are received for a given test ID after it is
  1160. * terminated are ignored.
  1161. *
  1162. * @param node Node instance
  1163. * @param test Test configuration to unregister
  1164. */
  1165. void ZT_Node_circuitTestEnd(ZT_Node *node,ZT_CircuitTest *test);
  1166. /**
  1167. * Get ZeroTier One version
  1168. *
  1169. * @param major Result: major version
  1170. * @param minor Result: minor version
  1171. * @param revision Result: revision
  1172. * @param featureFlags: Result: feature flag bitmap
  1173. */
  1174. void ZT_version(int *major,int *minor,int *revision,unsigned long *featureFlags);
  1175. #ifdef __cplusplus
  1176. }
  1177. #endif
  1178. #endif