ZeroTierOne.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 One, the core network
  29. * virtualization 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. #endif /* Windows or not */
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /****************************************************************************/
  47. /* Core constants */
  48. /****************************************************************************/
  49. /**
  50. * Default port for the ZeroTier service
  51. */
  52. #define ZT1_DEFAULT_PORT 9993
  53. /**
  54. * Maximum MTU for ZeroTier virtual networks
  55. *
  56. * This is pretty much an unchangeable global constant. To make it change
  57. * across nodes would require logic to send ICMP packet too big messages,
  58. * which would complicate things. 1500 has been good enough on most LANs
  59. * for ages, so a larger MTU should be fine for the forseeable future. This
  60. * typically results in two UDP packets per single large frame. Experimental
  61. * results seem to show that this is good. Larger MTUs resulting in more
  62. * fragments seemed too brittle on slow/crummy links for no benefit.
  63. *
  64. * If this does change, also change it in tap.h in the tuntaposx code under
  65. * mac-tap.
  66. *
  67. * Overhead for a normal frame split into two packets:
  68. *
  69. * 1414 = 1444 (typical UDP MTU) - 28 (packet header) - 2 (ethertype)
  70. * 1428 = 1444 (typical UDP MTU) - 16 (fragment header)
  71. * SUM: 2842
  72. *
  73. * We use 2800, which leaves some room for other payload in other types of
  74. * messages such as multicast propagation or future support for bridging.
  75. */
  76. #define ZT1_MAX_MTU 2800
  77. /**
  78. * Maximum length of network short name
  79. */
  80. #define ZT1_MAX_NETWORK_SHORT_NAME_LENGTH 255
  81. /**
  82. * Maximum number of statically assigned IP addresses per network endpoint using ZT address management (not DHCP)
  83. */
  84. #define ZT1_MAX_ZT_ASSIGNED_ADDRESSES 16
  85. /**
  86. * Maximum number of multicast group subscriptions per network
  87. */
  88. #define ZT1_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS 4096
  89. /**
  90. * Maximum number of direct network paths to a given peer
  91. */
  92. #define ZT1_MAX_PEER_NETWORK_PATHS 4
  93. /**
  94. * Feature flag: ZeroTier One was built to be thread-safe -- concurrent processXXX() calls are okay
  95. */
  96. #define ZT1_FEATURE_FLAG_THREAD_SAFE 0x00000001
  97. /**
  98. * Feature flag: FIPS compliant build (not available yet, but reserved for future use if we ever do this)
  99. */
  100. #define ZT1_FEATURE_FLAG_FIPS 0x00000002
  101. /****************************************************************************/
  102. /* Structures and other types */
  103. /****************************************************************************/
  104. /**
  105. * Function return code: OK (0) or error results
  106. *
  107. * Use ZT1_ResultCode_isFatal() to check for a fatal error. If a fatal error
  108. * occurs, the node should be considered to not be working correctly. These
  109. * indicate serious problems like an inaccessible data store or a compile
  110. * problem.
  111. */
  112. enum ZT1_ResultCode
  113. {
  114. /**
  115. * Operation completed normally
  116. */
  117. ZT1_RESULT_OK = 0,
  118. // Fatal errors (>0, <1000)
  119. /**
  120. * Ran out of memory
  121. */
  122. ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY = 1,
  123. /**
  124. * Data store is not writable or has failed
  125. */
  126. ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED = 2,
  127. /**
  128. * Internal error (e.g. unexpected exception indicating bug or build problem)
  129. */
  130. ZT1_RESULT_FATAL_ERROR_INTERNAL = 3,
  131. // Non-fatal errors (>1000)
  132. /**
  133. * Network ID not valid
  134. */
  135. ZT1_RESULT_ERROR_NETWORK_NOT_FOUND = 1000
  136. };
  137. /**
  138. * @param x Result code
  139. * @return True if result code indicates a fatal error
  140. */
  141. #define ZT1_ResultCode_isFatal(x) ((((int)(x)) > 0)&&(((int)(x)) < 1000))
  142. /**
  143. * Status codes sent to status update callback when things happen
  144. */
  145. enum ZT1_Event
  146. {
  147. /**
  148. * Node has been initialized
  149. *
  150. * This is the first event generated, and is always sent. It may occur
  151. * before Node's constructor returns.
  152. *
  153. * Meta-data: none
  154. */
  155. ZT1_EVENT_UP = 0,
  156. /**
  157. * Node is offline -- network does not seem to be reachable by any available strategy
  158. *
  159. * Meta-data: none
  160. */
  161. ZT1_EVENT_OFFLINE = 1,
  162. /**
  163. * Node is online -- at least one upstream node appears reachable
  164. *
  165. * Meta-data: none
  166. */
  167. ZT1_EVENT_ONLINE = 2,
  168. /**
  169. * Node is shutting down
  170. *
  171. * This is generated within Node's destructor when it is being shut down.
  172. * It's done for convenience, since cleaning up other state in the event
  173. * handler may appear more idiomatic.
  174. *
  175. * Meta-data: none
  176. */
  177. ZT1_EVENT_DOWN = 3,
  178. /**
  179. * Your identity has collided with another node's ZeroTier address
  180. *
  181. * This happens if two different public keys both hash (via the algorithm
  182. * in Identity::generate()) to the same 40-bit ZeroTier address.
  183. *
  184. * This is something you should "never" see, where "never" is defined as
  185. * once per 2^39 new node initializations / identity creations. If you do
  186. * see it, you're going to see it very soon after a node is first
  187. * initialized.
  188. *
  189. * This is reported as an event rather than a return code since it's
  190. * detected asynchronously via error messages from authoritative nodes.
  191. *
  192. * If this occurs, you must shut down and delete the node, delete the
  193. * identity.secret record/file from the data store, and restart to generate
  194. * a new identity. If you don't do this, you will not be able to communicate
  195. * with other nodes.
  196. *
  197. * We'd automate this process, but we don't think silently deleting
  198. * private keys or changing our address without telling the calling code
  199. * is good form. It violates the principle of least surprise.
  200. *
  201. * You can technically get away with not handling this, but we recommend
  202. * doing so in a mature reliable application. Besides, handling this
  203. * condition is a good way to make sure it never arises. It's like how
  204. * umbrellas prevent rain and smoke detectors prevent fires. They do, right?
  205. *
  206. * Meta-data: none
  207. */
  208. ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION = 4,
  209. /**
  210. * A more recent version was observed on the network
  211. *
  212. * Right now this is only triggered if a hub or supernode reports a
  213. * more recent version, and only once. It can be used to trigger a
  214. * software update check.
  215. *
  216. * Meta-data: unsigned int[3], more recent version number
  217. */
  218. ZT1_EVENT_SAW_MORE_RECENT_VERSION = 5,
  219. /**
  220. * A packet failed authentication
  221. *
  222. * Meta-data: struct sockaddr_storage containing origin address of packet
  223. */
  224. ZT1_EVENT_AUTHENTICATION_FAILURE = 6,
  225. /**
  226. * A received packet was not valid
  227. *
  228. * Meta-data: struct sockaddr_storage containing origin address of packet
  229. */
  230. ZT1_EVENT_INVALID_PACKET = 7,
  231. /**
  232. * Trace (debugging) message
  233. *
  234. * These events are only generated if this is a TRACE-enabled build.
  235. *
  236. * Meta-data: C string, TRACE message
  237. */
  238. ZT1_EVENT_TRACE = 8
  239. };
  240. /**
  241. * Current node status
  242. */
  243. typedef struct
  244. {
  245. /**
  246. * 40-bit ZeroTier address of this node
  247. */
  248. uint64_t address;
  249. /**
  250. * Public identity in string-serialized form (safe to send to others)
  251. *
  252. * This pointer will remain valid as long as the node exists.
  253. */
  254. const char *publicIdentity;
  255. /**
  256. * Full identity including secret key in string-serialized form
  257. *
  258. * This pointer will remain valid as long as the node exists.
  259. */
  260. const char *secretIdentity;
  261. /**
  262. * True if some kind of connectivity appears available
  263. */
  264. int online;
  265. } ZT1_NodeStatus;
  266. /**
  267. * Virtual network status codes
  268. */
  269. enum ZT1_VirtualNetworkStatus
  270. {
  271. /**
  272. * Waiting for network configuration (also means revision == 0)
  273. */
  274. ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0,
  275. /**
  276. * Configuration received and we are authorized
  277. */
  278. ZT1_NETWORK_STATUS_OK = 1,
  279. /**
  280. * Netconf master told us 'nope'
  281. */
  282. ZT1_NETWORK_STATUS_ACCESS_DENIED = 2,
  283. /**
  284. * Netconf master exists, but this virtual network does not
  285. */
  286. ZT1_NETWORK_STATUS_NOT_FOUND = 3,
  287. /**
  288. * Initialization of network failed or other internal error
  289. */
  290. ZT1_NETWORK_STATUS_PORT_ERROR = 4,
  291. /**
  292. * ZeroTier One version too old
  293. */
  294. ZT1_NETWORK_STATUS_CLIENT_TOO_OLD = 5
  295. };
  296. /**
  297. * Virtual network type codes
  298. */
  299. enum ZT1_VirtualNetworkType
  300. {
  301. /**
  302. * Private networks are authorized via certificates of membership
  303. */
  304. ZT1_NETWORK_TYPE_PRIVATE = 0,
  305. /**
  306. * Public networks have no access control -- they'll always be AUTHORIZED
  307. */
  308. ZT1_NETWORK_TYPE_PUBLIC = 1
  309. };
  310. /**
  311. * An Ethernet multicast group
  312. */
  313. typedef struct
  314. {
  315. /**
  316. * MAC address (least significant 48 bits)
  317. */
  318. uint64_t mac;
  319. /**
  320. * Additional distinguishing information (usually zero)
  321. */
  322. unsigned long adi;
  323. } ZT1_MulticastGroup;
  324. /**
  325. * Virtual network configuration update type
  326. */
  327. enum ZT1_VirtualNetworkConfigOperation
  328. {
  329. /**
  330. * Network is coming up (either for the first time or after service restart)
  331. */
  332. ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_UP = 1,
  333. /**
  334. * Network configuration has been updated
  335. */
  336. ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE = 2,
  337. /**
  338. * Network is going down (not permanently)
  339. */
  340. ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN = 3,
  341. /**
  342. * Network is going down permanently (leave/delete)
  343. */
  344. ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY = 4
  345. };
  346. /**
  347. * Virtual network configuration
  348. */
  349. typedef struct
  350. {
  351. /**
  352. * 64-bit ZeroTier network ID
  353. */
  354. uint64_t nwid;
  355. /**
  356. * Ethernet MAC (40 bits) that should be assigned to port
  357. */
  358. uint64_t mac;
  359. /**
  360. * Network name (from network configuration master)
  361. */
  362. char name[ZT1_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  363. /**
  364. * Network configuration request status
  365. */
  366. enum ZT1_VirtualNetworkStatus status;
  367. /**
  368. * Network type
  369. */
  370. enum ZT1_VirtualNetworkType type;
  371. /**
  372. * Maximum interface MTU
  373. */
  374. unsigned int mtu;
  375. /**
  376. * If nonzero, the network this port belongs to indicates DHCP availability
  377. *
  378. * This is a suggestion. The underlying implementation is free to ignore it
  379. * for security or other reasons. This is simply a netconf parameter that
  380. * means 'DHCP is available on this network.'
  381. */
  382. int dhcp;
  383. /**
  384. * If nonzero, this port is allowed to bridge to other networks
  385. *
  386. * This is informational. If this is false (0), bridged packets will simply
  387. * be dropped and bridging won't work.
  388. */
  389. int bridge;
  390. /**
  391. * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
  392. */
  393. int broadcastEnabled;
  394. /**
  395. * If the network is in PORT_ERROR state, this is the error most recently returned by the port config callback
  396. */
  397. int portError;
  398. /**
  399. * Is this network enabled? If not, all frames to/from are dropped.
  400. */
  401. int enabled;
  402. /**
  403. * Network config revision as reported by netconf master
  404. *
  405. * If this is zero, it means we're still waiting for our netconf.
  406. */
  407. unsigned long netconfRevision;
  408. /**
  409. * Number of multicast group subscriptions
  410. */
  411. unsigned int multicastSubscriptionCount;
  412. /**
  413. * Multicast group subscriptions
  414. */
  415. ZT1_MulticastGroup multicastSubscriptions[ZT1_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS];
  416. /**
  417. * Number of assigned addresses
  418. */
  419. unsigned int assignedAddressCount;
  420. /**
  421. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  422. *
  423. * For IP, the port number of the sockaddr_XX structure contains the number
  424. * of bits in the address netmask. Only the IP address and port are used.
  425. * Other fields like interface number can be ignored.
  426. *
  427. * This is only used for ZeroTier-managed address assignments sent by the
  428. * virtual network's configuration master.
  429. */
  430. struct sockaddr_storage assignedAddresses[ZT1_MAX_ZT_ASSIGNED_ADDRESSES];
  431. } ZT1_VirtualNetworkConfig;
  432. /**
  433. * A list of networks
  434. */
  435. typedef struct
  436. {
  437. ZT1_VirtualNetworkConfig *networks;
  438. unsigned long networkCount;
  439. } ZT1_VirtualNetworkList;
  440. /**
  441. * Physical network path to a peer
  442. */
  443. typedef struct
  444. {
  445. /**
  446. * Address of endpoint
  447. */
  448. struct sockaddr_storage address;
  449. /**
  450. * Time of last send in milliseconds or 0 for never
  451. */
  452. uint64_t lastSend;
  453. /**
  454. * Time of last receive in milliseconds or 0 for never
  455. */
  456. uint64_t lastReceive;
  457. /**
  458. * Is path fixed? (i.e. not learned, static)
  459. */
  460. int fixed;
  461. /**
  462. * Is path active?
  463. */
  464. int active;
  465. /**
  466. * Is path preferred?
  467. */
  468. int preferred;
  469. } ZT1_PeerPhysicalPath;
  470. /**
  471. * What trust hierarchy role does this peer have?
  472. */
  473. enum ZT1_PeerRole {
  474. ZT1_PEER_ROLE_LEAF = 0, // ordinary node
  475. ZT1_PEER_ROLE_HUB = 1, // locally federated hub
  476. ZT1_PEER_ROLE_SUPERNODE = 2 // planetary supernode
  477. };
  478. /**
  479. * Peer status result buffer
  480. */
  481. typedef struct
  482. {
  483. /**
  484. * ZeroTier address (40 bits)
  485. */
  486. uint64_t address;
  487. /**
  488. * Time we last received a unicast frame from this peer
  489. */
  490. uint64_t lastUnicastFrame;
  491. /**
  492. * Time we last received a multicast rame from this peer
  493. */
  494. uint64_t lastMulticastFrame;
  495. /**
  496. * Remote major version or -1 if not known
  497. */
  498. int versionMajor;
  499. /**
  500. * Remote minor version or -1 if not known
  501. */
  502. int versionMinor;
  503. /**
  504. * Remote revision or -1 if not known
  505. */
  506. int versionRev;
  507. /**
  508. * Last measured latency in milliseconds or zero if unknown
  509. */
  510. unsigned int latency;
  511. /**
  512. * What trust hierarchy role does this device have?
  513. */
  514. enum ZT1_PeerRole role;
  515. /**
  516. * Number of paths (size of paths[])
  517. */
  518. unsigned int pathCount;
  519. /**
  520. * Known network paths to peer
  521. */
  522. ZT1_PeerPhysicalPath paths[ZT1_MAX_PEER_NETWORK_PATHS];
  523. } ZT1_Peer;
  524. /**
  525. * List of peers
  526. */
  527. typedef struct
  528. {
  529. ZT1_Peer *peers;
  530. unsigned long peerCount;
  531. } ZT1_PeerList;
  532. /**
  533. * An instance of a ZeroTier One node (opaque)
  534. */
  535. typedef void ZT1_Node;
  536. /****************************************************************************/
  537. /* Callbacks used by Node API */
  538. /****************************************************************************/
  539. /**
  540. * Callback called to update virtual network port configuration
  541. *
  542. * This can be called at any time to update the configuration of a virtual
  543. * network port. The parameter after the network ID specifies whether this
  544. * port is being brought up, updated, brought down, or permanently deleted.
  545. *
  546. * This in turn should be used by the underlying implementation to create
  547. * and configure tap devices at the OS (or virtual network stack) layer.
  548. *
  549. * The supplied config pointer is not guaranteed to remain valid, so make
  550. * a copy if you want one.
  551. *
  552. * This should not call multicastSubscribe() or other network-modifying
  553. * methods, as this could cause a deadlock in multithreaded or interrupt
  554. * driven environments.
  555. *
  556. * This must return 0 on success. It can return any OS-dependent error code
  557. * on failure, and this results in the network being placed into the
  558. * PORT_ERROR state.
  559. */
  560. typedef int (*ZT1_VirtualNetworkConfigFunction)(ZT1_Node *,void *,uint64_t,enum ZT1_VirtualNetworkConfigOperation,const ZT1_VirtualNetworkConfig *);
  561. /**
  562. * Function to send a frame out to a virtual network port
  563. *
  564. * Parameters: (1) node, (2) user ptr, (3) network ID, (4) source MAC,
  565. * (5) destination MAC, (6) ethertype, (7) VLAN ID, (8) frame data,
  566. * (9) frame length.
  567. */
  568. typedef void (*ZT1_VirtualNetworkFrameFunction)(ZT1_Node *,void *,uint64_t,uint64_t,uint64_t,unsigned int,unsigned int,const void *,unsigned int);
  569. /**
  570. * Callback for events
  571. *
  572. * Events are generated when the node's status changes in a significant way
  573. * and on certain non-fatal errors and events of interest. The final void
  574. * parameter points to event meta-data. The type of event meta-data (and
  575. * whether it is present at all) is event type dependent. See the comments
  576. * in the definition of ZT1_Event.
  577. */
  578. typedef void (*ZT1_EventCallback)(ZT1_Node *,void *,enum ZT1_Event,const void *);
  579. /**
  580. * Function to get an object from the data store
  581. *
  582. * Parameters: (1) object name, (2) buffer to fill, (3) size of buffer, (4)
  583. * index in object to start reading, (5) result parameter that must be set
  584. * to the actual size of the object if it exists.
  585. *
  586. * Object names can contain forward slash (/) path separators. They will
  587. * never contain .. or backslash (\), so this is safe to map as a Unix-style
  588. * path if the underlying storage permits. For security reasons we recommend
  589. * returning errors if .. or \ are used.
  590. *
  591. * The function must return the actual number of bytes read. If the object
  592. * doesn't exist, it should return -1. -2 should be returned on other errors
  593. * such as errors accessing underlying storage.
  594. *
  595. * If the read doesn't fit in the buffer, the max number of bytes should be
  596. * read. The caller may call the function multiple times to read the whole
  597. * object.
  598. */
  599. typedef long (*ZT1_DataStoreGetFunction)(ZT1_Node *,void *,const char *,void *,unsigned long,unsigned long,unsigned long *);
  600. /**
  601. * Function to store an object in the data store
  602. *
  603. * Parameters: (1) node, (2) user ptr, (3) object name, (4) object data,
  604. * (5) object size, (6) secure? (bool).
  605. *
  606. * If secure is true, the file should be set readable and writable only
  607. * to the user running ZeroTier One. What this means is platform-specific.
  608. *
  609. * Name semantics are the same as the get function. This must return zero on
  610. * success. You can return any OS-specific error code on failure, as these
  611. * may be visible in logs or error messages and might aid in debugging.
  612. *
  613. * If the data pointer is null, this must be interpreted as a delete
  614. * operation.
  615. */
  616. typedef int (*ZT1_DataStorePutFunction)(ZT1_Node *,void *,const char *,const void *,unsigned long,int);
  617. /**
  618. * Function to send a ZeroTier packet out over the wire
  619. *
  620. * Parameters: (1) node, (2) user ptr, (3) address, (4) link desperation,
  621. * (5) packet data, (6) packet data length.
  622. *
  623. * The function must return zero on success and may return any error code
  624. * on failure. Note that success does not (of course) guarantee packet
  625. * delivery. It only means that the packet appears to have been sent.
  626. */
  627. typedef int (*ZT1_WirePacketSendFunction)(ZT1_Node *,void *,const struct sockaddr_storage *,unsigned int,const void *,unsigned int);
  628. /****************************************************************************/
  629. /* C Node API */
  630. /****************************************************************************/
  631. /**
  632. * Create a new ZeroTier One node
  633. *
  634. * Note that this can take a few seconds the first time it's called, as it
  635. * will generate an identity.
  636. *
  637. * @param node Result: pointer is set to new node instance on success
  638. * @param uptr User pointer to pass to functions/callbacks
  639. * @param now Current clock in milliseconds
  640. * @param dataStoreGetFunction Function called to get objects from persistent storage
  641. * @param dataStorePutFunction Function called to put objects in persistent storage
  642. * @param virtualNetworkConfigFunction Function to be called when virtual LANs are created, deleted, or their config parameters change
  643. * @param eventCallback Function to receive status updates and non-fatal error notices
  644. * @param overrideRootTopology If not NULL, must contain string-serialize root topology (for testing, default: NULL)
  645. * @return OK (0) or error code if a fatal error condition has occurred
  646. */
  647. enum ZT1_ResultCode ZT1_Node_new(
  648. ZT1_Node **node,
  649. void *uptr,
  650. uint64_t now,
  651. ZT1_DataStoreGetFunction dataStoreGetFunction,
  652. ZT1_DataStorePutFunction dataStorePutFunction,
  653. ZT1_WirePacketSendFunction wirePacketSendFunction,
  654. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  655. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  656. ZT1_EventCallback eventCallback,
  657. const char *overrideRootTopology = (const char *)0);
  658. /**
  659. * Delete a node and free all resources it consumes
  660. *
  661. * If you are using multiple threads, all other threads must be shut down
  662. * first. This can crash if processXXX() methods are in progress.
  663. *
  664. * @param node Node to delete
  665. */
  666. void ZT1_Node_delete(ZT1_Node *node);
  667. /**
  668. * Process a packet received from the physical wire
  669. *
  670. * @param node Node instance
  671. * @param now Current clock in milliseconds
  672. * @param remoteAddress Origin of packet
  673. * @param linkDesperation Link desperation metric for link or protocol over which packet arrived
  674. * @param packetData Packet data
  675. * @param packetLength Packet length
  676. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  677. * @return OK (0) or error code if a fatal error condition has occurred
  678. */
  679. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  680. ZT1_Node *node,
  681. uint64_t now,
  682. const struct sockaddr_storage *remoteAddress,
  683. unsigned int linkDesperation,
  684. const void *packetData,
  685. unsigned int packetLength,
  686. volatile uint64_t *nextBackgroundTaskDeadline);
  687. /**
  688. * Process a frame from a virtual network port (tap)
  689. *
  690. * @param node Node instance
  691. * @param now Current clock in milliseconds
  692. * @param nwid ZeroTier 64-bit virtual network ID
  693. * @param sourceMac Source MAC address (least significant 48 bits)
  694. * @param destMac Destination MAC address (least significant 48 bits)
  695. * @param etherType 16-bit Ethernet frame type
  696. * @param vlanId 10-bit VLAN ID or 0 if none
  697. * @param frameData Frame payload data
  698. * @param frameLength Frame payload length
  699. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  700. * @return OK (0) or error code if a fatal error condition has occurred
  701. */
  702. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  703. ZT1_Node *node,
  704. uint64_t now,
  705. uint64_t nwid,
  706. uint64_t sourceMac,
  707. uint64_t destMac,
  708. unsigned int etherType,
  709. unsigned int vlanId,
  710. const void *frameData,
  711. unsigned int frameLength,
  712. volatile uint64_t *nextBackgroundTaskDeadline);
  713. /**
  714. * Perform periodic background operations
  715. *
  716. * @param node Node instance
  717. * @param now Current clock in milliseconds
  718. * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
  719. * @return OK (0) or error code if a fatal error condition has occurred
  720. */
  721. enum ZT1_ResultCode ZT1_Node_processBackgroundTasks(ZT1_Node *node,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
  722. /**
  723. * Join a network
  724. *
  725. * This may generate calls to the port config callback before it returns,
  726. * or these may be deffered if a netconf is not available yet.
  727. *
  728. * If we are already a member of the network, nothing is done and OK is
  729. * returned.
  730. *
  731. * @param node Node instance
  732. * @param nwid 64-bit ZeroTier network ID
  733. * @return OK (0) or error code if a fatal error condition has occurred
  734. */
  735. enum ZT1_ResultCode ZT1_Node_join(ZT1_Node *node,uint64_t nwid);
  736. /**
  737. * Leave a network
  738. *
  739. * If a port has been configured for this network this will generate a call
  740. * to the port config callback with a NULL second parameter to indicate that
  741. * the port is now deleted.
  742. *
  743. * @param node Node instance
  744. * @param nwid 64-bit network ID
  745. * @return OK (0) or error code if a fatal error condition has occurred
  746. */
  747. enum ZT1_ResultCode ZT1_Node_leave(ZT1_Node *node,uint64_t nwid);
  748. /**
  749. * Subscribe to an Ethernet multicast group
  750. *
  751. * ADI stands for additional distinguishing information. This defaults to zero
  752. * and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
  753. * and this must be done.
  754. *
  755. * For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
  756. * broadcast address) but with an ADI equal to each IPv4 address in host
  757. * byte order. This converts ARP from a non-scalable broadcast protocol to
  758. * a scalable multicast protocol with perfect address specificity.
  759. *
  760. * If this is not done, ARP will not work reliably.
  761. *
  762. * Multiple calls to subscribe to the same multicast address will have no
  763. * effect. It is perfectly safe to do this.
  764. *
  765. * This does not generate an update call to networkConfigCallback().
  766. *
  767. * @param node Node instance
  768. * @param nwid 64-bit network ID
  769. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  770. * @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
  771. * @return OK (0) or error code if a fatal error condition has occurred
  772. */
  773. enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi = 0);
  774. /**
  775. * Unsubscribe from an Ethernet multicast group (or all groups)
  776. *
  777. * If multicastGroup is zero (0), this will unsubscribe from all groups. If
  778. * you are not subscribed to a group this has no effect.
  779. *
  780. * This does not generate an update call to networkConfigCallback().
  781. *
  782. * @param node Node instance
  783. * @param nwid 64-bit network ID
  784. * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
  785. * @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
  786. * @return OK (0) or error code if a fatal error condition has occurred
  787. */
  788. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi = 0);
  789. /**
  790. * Get this node's 40-bit ZeroTier address
  791. *
  792. * @param node Node instance
  793. * @return ZeroTier address (least significant 40 bits of 64-bit int)
  794. */
  795. uint64_t ZT1_Node_address(ZT1_Node *node);
  796. /**
  797. * Get the status of this node
  798. *
  799. * @param node Node instance
  800. * @param status Buffer to fill with current node status
  801. */
  802. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status);
  803. /**
  804. * Get a list of known peer nodes
  805. *
  806. * The pointer returned here must be freed with freeQueryResult()
  807. * when you are done with it.
  808. *
  809. * @param node Node instance
  810. * @return List of known peers or NULL on failure
  811. */
  812. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node);
  813. /**
  814. * Get the status of a virtual network
  815. *
  816. * The pointer returned here must be freed with freeQueryResult()
  817. * when you are done with it.
  818. *
  819. * @param node Node instance
  820. * @param nwid 64-bit network ID
  821. * @return Network configuration or NULL if we are not a member of this network
  822. */
  823. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid);
  824. /**
  825. * Enumerate and get status of all networks
  826. *
  827. * @param node Node instance
  828. * @return List of networks or NULL on failure
  829. */
  830. ZT1_VirtualNetworkList *ZT1_Node_networks(ZT1_Node *node);
  831. /**
  832. * Free a query result buffer
  833. *
  834. * Use this to free the return values of listNetworks(), listPeers(), etc.
  835. *
  836. * @param node Node instance
  837. * @param qr Query result buffer
  838. */
  839. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr);
  840. /**
  841. * Set a network configuration master instance for this node
  842. *
  843. * Normal nodes should not need to use this. This is for nodes with
  844. * special compiled-in support for acting as network configuration
  845. * masters / controllers.
  846. *
  847. * The supplied instance must be a C++ object that inherits from the
  848. * NetworkConfigMaster base class in node/. No type checking is performed,
  849. * so a pointer to anything else will result in a crash.
  850. *
  851. * @param node ZertTier One node
  852. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  853. * @return OK (0) or error code if a fatal error condition has occurred
  854. */
  855. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkConfigMasterInstance);
  856. /**
  857. * Get ZeroTier One version
  858. *
  859. * @param major Result: major version
  860. * @param minor Result: minor version
  861. * @param revision Result: revision
  862. * @param featureFlags: Result: feature flag bitmap
  863. */
  864. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags);
  865. #ifdef __cplusplus
  866. }
  867. #endif
  868. #endif