ZeroTierOne.h 30 KB

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