ZeroTierOne.h 29 KB

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