2
0

ZeroTierOne.h 30 KB

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