ZeroTierOne.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. #if defined(_WIN32) || defined(_WIN64)
  35. #include <WinSock2.h>
  36. #include <WS2tcpip.h>
  37. #include <Windows.h>
  38. #else /* not Windows */
  39. #include <arpa/inet.h>
  40. #include <netinet/in.h>
  41. #endif /* Windows or not */
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /****************************************************************************/
  46. /* Core constants */
  47. /****************************************************************************/
  48. /**
  49. * Maximum frame MTU
  50. */
  51. #define ZT1_MAX_MTU 2800
  52. /**
  53. * Maximum length of a wire message packet in bytes
  54. */
  55. #define ZT1_MAX_WIRE_MESSAGE_LENGTH 1500
  56. /****************************************************************************/
  57. /* Structures and other types */
  58. /****************************************************************************/
  59. /**
  60. * Function return values: OK or various error conditions
  61. */
  62. enum ZT1_ResultCode
  63. {
  64. /**
  65. * Operation completed normally
  66. */
  67. ZT1_RESULT_OK = 0,
  68. /**
  69. * Our identity collides with another on the network
  70. *
  71. * This is profoundly unlikely: once in about 2^39 identities. If this
  72. * happens to you, delete identity.public and identity.secret from your
  73. * data store / home path and restart. You might also avoid shark infested
  74. * waters, hide during thunderstorms, and consider playing the lottery.
  75. */
  76. ZT1_RESULT_ERROR_IDENTITY_COLLISION = 1,
  77. /**
  78. * Ran out of memory
  79. */
  80. ZT1_RESULT_ERROR_OUT_OF_MEMORY = 2,
  81. /**
  82. * Data store is not writable or has failed
  83. */
  84. ZT1_RESULT_ERROR_DATA_STORE_FAILED = 3
  85. };
  86. /**
  87. * Status codes sent to status update callback when things happen
  88. */
  89. enum ZT1_NodeStatusCode
  90. {
  91. /**
  92. * Node is online
  93. */
  94. ZT1_NODE_STATUS_ONLINE = 1,
  95. /**
  96. * Node is offline -- nothing is reachable
  97. */
  98. ZT1_NODE_STATUS_OFFLINE = 2,
  99. /**
  100. * The desperation level has changed
  101. *
  102. * 'extra' will point to an int containing the new level.
  103. */
  104. ZT1_NODE_STATUS_DESPERATION_CHANGE = 3
  105. };
  106. /**
  107. * Current node status
  108. */
  109. typedef struct
  110. {
  111. /**
  112. * 40-bit ZeroTier address of this node
  113. */
  114. uint64_t address;
  115. /**
  116. * Public identity in string-serialized form (safe to send to others)
  117. *
  118. * This pointer will remain valid as long as the node exists.
  119. */
  120. const char *publicIdentity;
  121. /**
  122. * Full identity including secret key in string-serialized form
  123. *
  124. * This pointer will remain valid as long as the node exists.
  125. */
  126. const char *secretIdentity;
  127. /**
  128. * True if some kind of connectivity appears available
  129. */
  130. int online;
  131. /**
  132. * Current maximum link desperation metric
  133. */
  134. int desperation;
  135. } ZT1_NodeStatus;
  136. /**
  137. * A message to or from a physical address (e.g. IP or physical Ethernet)
  138. */
  139. typedef struct
  140. {
  141. /**
  142. * Socket address
  143. */
  144. struct sockaddr_storage address;
  145. /**
  146. * Link desperation -- higher equals "worse" or "slower"
  147. *
  148. * This is very similar to an interface metric. Higher values indicate
  149. * worse links. For incoming wire messages, it should be sent to the
  150. * desperation metric for the originating socket. For outgoing wire
  151. * messages, ZeroTier will increment this from zero as it grows more
  152. * and more desperate to communicate.
  153. *
  154. * In other words, this value controls fallback to things like TCP
  155. * tunnels to relays. As desperation increases, ZeroTier becomes
  156. * more and more willing to use these links.
  157. *
  158. * Desperation values shouldn't be arbitrary. They should be tied to
  159. * specific transport types. For example: 0 might be UDP, 1 might be
  160. * TCP, and 2 might be HTTP relay via a ZeroTier relay server. There
  161. * should be no gaps. Negative values are permitted and may refer to
  162. * better-than-normal links such as direct raw Ethernet framing over
  163. * a trusted backplane.
  164. */
  165. int desperation;
  166. /**
  167. * If nonzero (true), spam this message across paths up to 'desperation'
  168. *
  169. * This works with 'desperation' to allow fall-forward to less desperate
  170. * paths. When this flag is set, this message should be sent across all
  171. * applicable transports up to and including the specified level of
  172. * desperation.
  173. *
  174. * For example, if spam==1 and desperation==2 the packet might be sent
  175. * via both UDP and HTTP tunneling.
  176. */
  177. int spam;
  178. /**
  179. * Packet data
  180. */
  181. const char packetData[ZT1_MAX_WIRE_MESSAGE_LENGTH];
  182. /**
  183. * Length of packet
  184. */
  185. unsigned int packetLength;
  186. } ZT1_WireMessage;
  187. /**
  188. * A message to or from a virtual LAN port
  189. */
  190. typedef struct
  191. {
  192. /**
  193. * ZeroTier network ID of virtual LAN port
  194. */
  195. uint64_t nwid;
  196. /**
  197. * Source MAC address
  198. */
  199. uint64_t sourceMac;
  200. /**
  201. * Destination MAC address
  202. */
  203. uint64_t destMac;
  204. /**
  205. * 16-bit Ethernet frame type
  206. */
  207. unsigned int etherType;
  208. /**
  209. * 10-bit VLAN ID or 0 for none
  210. */
  211. unsigned int vlanId;
  212. /**
  213. * Ethernet frame data
  214. */
  215. const char frameData[ZT1_MAX_MTU];
  216. /**
  217. * Ethernet frame length
  218. */
  219. unsigned int frameLength;
  220. } ZT1_VirtualNetworkFrame;
  221. /**
  222. * Virtual network status codes
  223. */
  224. enum ZT1_VirtualNetworkStatus
  225. {
  226. /**
  227. * Waiting for network configuration (also means revision == 0)
  228. */
  229. ZT1_NETWORK_STATUS_WAITING = 0,
  230. /**
  231. * Configuration received and we are authorized
  232. */
  233. ZT1_NETWORK_STATUS_AUTHORIZED = 1,
  234. /**
  235. * Netconf master told us 'nope'
  236. */
  237. ZT1_NETWORK_STATUS_ACCESS_DENIED = 2,
  238. /**
  239. * Netconf master exists, but this virtual network does not
  240. */
  241. ZT1_NETWORK_STATUS_NOT_FOUND = 3
  242. };
  243. /**
  244. * Virtual network type codes
  245. */
  246. enum ZT1_VirtualNetworkType
  247. {
  248. /**
  249. * Private networks are authorized via certificates of membership
  250. */
  251. ZT1_NETWORK_TYPE_PRIVATE = 0,
  252. /**
  253. * Public networks have no access control -- they'll always be AUTHORIZED
  254. */
  255. ZT1_NETWORK_TYPE_PUBLIC = 1
  256. };
  257. /**
  258. * Virtual LAN configuration
  259. */
  260. typedef struct
  261. {
  262. /**
  263. * 64-bit ZeroTier network ID
  264. */
  265. uint64_t nwid;
  266. /**
  267. * Ethernet MAC (40 bits) that should be assigned to port
  268. */
  269. uint64_t mac;
  270. /**
  271. * Network configuration request status
  272. */
  273. enum ZT1_VirtualNetworkStatus status;
  274. /**
  275. * Network type
  276. */
  277. enum ZT1_VirtualNetworkType type;
  278. /**
  279. * Maximum interface MTU
  280. */
  281. unsigned int mtu;
  282. /**
  283. * If nonzero, the network this port belongs to indicates DHCP availability
  284. *
  285. * This is a suggestion. The underlying implementation is free to ignore it
  286. * for security or other reasons. This is simply a netconf parameter that
  287. * means 'DHCP is available on this network.'
  288. */
  289. int dhcp;
  290. /**
  291. * If nonzero, this port is allowed to bridge to other networks
  292. *
  293. * This is informational. If this is false (0), bridged packets will simply
  294. * be dropped and bridging won't work.
  295. */
  296. int bridge;
  297. /**
  298. * Network config revision as reported by netconf master
  299. *
  300. * If this is zero, it means we're still waiting for our netconf.
  301. */
  302. unsigned long netconfRevision;
  303. /**
  304. * ZeroTier-assigned addresses (in sockaddr_storage structures)
  305. *
  306. * For IP, the port number of the sockaddr_XX structure contains the number
  307. * of bits in the address netmask. Only the IP address and port are used.
  308. * Other fields like interface number can be ignored.
  309. *
  310. * This is only used for ZeroTier-managed address assignments sent by the
  311. * virtual network's configuration master.
  312. */
  313. const struct sockaddr_storage *assignedAddresses;
  314. /**
  315. * Number of assigned addresses
  316. */
  317. unsigned int assignedAddressCount;
  318. /**
  319. * Network name (from network configuration master)
  320. */
  321. const char *networkName;
  322. } ZT1_VirtualNetworkConfig;
  323. /**
  324. * A list of networks
  325. */
  326. typedef struct
  327. {
  328. ZT1_VirtualNetworkConfig *networks;
  329. unsigned long networkCount;
  330. } ZT1_VirtualNetworkList;
  331. /**
  332. * Physical network path to a peer
  333. */
  334. typedef struct
  335. {
  336. /**
  337. * Address of endpoint
  338. */
  339. struct sockaddr_storage address;
  340. /**
  341. * Time since last send in milliseconds or -1 for never
  342. */
  343. long lastSend;
  344. /**
  345. * Time since last receive in milliseconds or -1 for never
  346. */
  347. long lastReceive;
  348. /**
  349. * Time since last ping sent in milliseconds or -1 for never
  350. */
  351. long lastPing;
  352. /**
  353. * Time since last firewall opener sent in milliseconds or -1 for never
  354. */
  355. long lastFirewallOpener;
  356. /**
  357. * Total bytes sent
  358. */
  359. uint64_t bytesSent;
  360. /**
  361. * Total bytes received
  362. */
  363. uint64_t bytesReceived;
  364. /**
  365. * This path's desperation metric (higher == worse)
  366. */
  367. int desperation;
  368. /**
  369. * Is path fixed? (i.e. not learned, static)
  370. */
  371. int fixed;
  372. } ZT1_PeerPhysicalPath;
  373. /**
  374. * What trust hierarchy role does this device have?
  375. */
  376. enum ZT1_PeerRole {
  377. ZT1_PEER_ROLE_SUPERNODE = 0, // planetary supernode
  378. ZT1_PEER_ROLE_HUB = 1, // locally federated hub
  379. ZT1_PEER_ROLE_NODE = 2 // ordinary node
  380. };
  381. /**
  382. * Peer status result buffer
  383. */
  384. typedef struct
  385. {
  386. /**
  387. * ZeroTier binary address (40 bits)
  388. */
  389. uint64_t address;
  390. /**
  391. * Remote major version or -1 if not known
  392. */
  393. int versionMajor;
  394. /**
  395. * Remote minor version or -1 if not known
  396. */
  397. int versionMinor;
  398. /**
  399. * Remote revision or -1 if not known
  400. */
  401. int versionRev;
  402. /**
  403. * Last measured latency in milliseconds or zero if unknown
  404. */
  405. unsigned int latency;
  406. /**
  407. * What trust hierarchy role does this device have?
  408. */
  409. enum ZT1_PeerRole role;
  410. /**
  411. * Array of network paths to peer
  412. */
  413. ZT1_PeerPhysicalPath *paths;
  414. /**
  415. * Number of paths (size of paths[])
  416. */
  417. unsigned long pathCount;
  418. } ZT1_Peer;
  419. /**
  420. * List of peers
  421. */
  422. typedef struct
  423. {
  424. ZT1_Peer *peers;
  425. unsigned long peerCount;
  426. } ZT1_PeerList;
  427. /**
  428. * An instance of a ZeroTier One node (opaque)
  429. */
  430. typedef void ZT1_Node;
  431. /****************************************************************************/
  432. /* Callbacks used by Node API */
  433. /****************************************************************************/
  434. /**
  435. * Callback called to update virtual port configuration
  436. *
  437. * This can be called at any time to update the configuration of a virtual
  438. * network port. If a port is deleted (via leave() or otherwise) this is
  439. * called with a NULL config parameter.
  440. *
  441. * This in turn should be used by the underlying implementation to create
  442. * and configure tap devices to handle frames, etc.
  443. *
  444. * The supplied config pointer is not guaranteed to remain valid, so make
  445. * a copy if you want one.
  446. */
  447. typedef void (*ZT1_VirtualNetworkConfigCallback)(ZT1_Node *,uint64_t,const ZT1_VirtualNetworkConfig *);
  448. /**
  449. * Callback for status messages
  450. *
  451. * This is called whenever the node's status changes in some significant way.
  452. */
  453. typedef void (*ZT1_StatusCallback)(ZT1_Node *,enum ZT1_NodeStatusCode);
  454. /**
  455. * Function to get an object from the data store
  456. *
  457. * Parameters: (1) object name, (2) buffer to fill, (3) size of buffer, (4)
  458. * index in object to start reading, (5) result parameter that must be set
  459. * to the actual size of the object if it exists.
  460. *
  461. * Object names can contain forward slash (/) path separators. They will
  462. * never contain .. or backslash (\), so this is safe to map as a Unix-style
  463. * path if the underlying storage permits. For security reasons we recommend
  464. * returning errors if .. or \ are used.
  465. *
  466. * The function must return the actual number of bytes read. If the object
  467. * doesn't exist, it should return -1. -2 should be returned on other errors
  468. * such as errors accessing underlying storage.
  469. *
  470. * If the read doesn't fit in the buffer, the max number of bytes should be
  471. * read. The caller may call the function multiple times to read the whole
  472. * object.
  473. */
  474. typedef long (*ZT1_DataStoreGetFunction)(ZT1_Node *,const char *,void *,unsigned long,unsigned long,unsigned long *);
  475. /**
  476. * Function to store an object in the data store
  477. *
  478. * Parameters: (1) object name, (2) object data, (3) object size. Naming
  479. * semantics are the same as the get function. This must return zero on
  480. * success. You can return any OS-specific error code on failure, as these
  481. * may be visible in logs or error messages and might aid in debugging.
  482. *
  483. * A call to write 0 bytes can safely be interpreted as a delete operation.
  484. */
  485. typedef int (*ZT1_DataStorePutFunction)(ZT1_Node *,const char *,const void *,unsigned long);
  486. /****************************************************************************/
  487. /* C Node API */
  488. /****************************************************************************/
  489. /**
  490. * Create a new ZeroTier One node
  491. *
  492. * Note that this can take a few seconds the first time it's called, as it
  493. * will generate an identity.
  494. *
  495. * @param node Result: pointer is set to new node instance on success
  496. * @param dataStoreGetFunction Function called to get objects from persistent storage
  497. * @param dataStorePutFunction Function called to put objects in persistent storage
  498. * @param networkConfigCallback Function to be called when virtual LANs are created, deleted, or their config parameters change
  499. * @param statusCallback Function to receive status updates and non-fatal error notices
  500. * @return OK (0) or error code if a fatal error condition has occurred
  501. */
  502. enum ZT1_ResultCode ZT1_Node_new(
  503. ZT1_Node **node,
  504. ZT1_DataStoreGetFunction *dataStoreGetFunction,
  505. ZT1_DataStorePutFunction *dataStorePutFunction,
  506. ZT1_VirtualNetworkConfigCallback *networkConfigCallback,
  507. ZT1_StatusCallback *statusCallback);
  508. /**
  509. * Process wire messages and/or LAN frames
  510. *
  511. * This runs the ZeroTier core loop once with input packets and frames and
  512. * returns zero or more resulting packets or frames. It also sets a max
  513. * interval value. The calling code must call run() again after no more
  514. * than this many milliseconds of inactivity. If no packets have been
  515. * received, it's fine to call run() with no inputs after the inactivity
  516. * timeout.
  517. *
  518. * In addition to normal inputs and outputs, any callbacks registered
  519. * with the ZeroTier One core may also be called such as virtual network
  520. * endpoint configuration update or diagnostic message handlers.
  521. *
  522. * The supplied time must be at millisecond resolution and must increment
  523. * monotonically from the time the Node is created. Other than that, there
  524. * are no other restrictions. On normal systems this is usually the system
  525. * clock measured in milliseconds since the epoch.
  526. *
  527. * @param node Node instance
  528. * @param now Current time at millisecond resolution (typically since epoch)
  529. * @param inputWireMessages ZeroTier transport packets from the wire
  530. * @param inputWireMessageCount Number of packets received
  531. * @param inputLanFrames Frames read from virtual LAN tap device
  532. * @param inputLanFrameCount Number of frames read
  533. * @param outputWireMessages Result: set to array of wire messages to be sent
  534. * @param outputWireMessageCount Result: set to size of *outputWireMessages[]
  535. * @param outputLanFrames Result: set to array of LAN frames to post to tap device
  536. * @param outputLanFrameCount Result: set to size of outputLanFrames[]
  537. * @param maxNextInterval Result: maximum number of milliseconds before next call to run() is needed
  538. * @return OK (0) or error code if a fatal error condition has occurred
  539. */
  540. enum ZT1_ResultCode ZT1_Node_run(
  541. ZT1_Node *node,
  542. uint64_t now,
  543. const ZT1_WireMessage *inputWireMessages,
  544. unsigned int inputWireMessageCount,
  545. const ZT1_VirtualNetworkFrame *inputFrames,
  546. unsigned int inputFrameCount,
  547. const ZT1_WireMessage **outputWireMessages,
  548. unsigned int *outputWireMessageCount,
  549. const ZT1_VirtualNetworkFrame **outputFrames,
  550. unsigned int *outputLanFrameCount,
  551. unsigned long *maxNextInterval);
  552. /**
  553. * Join a network
  554. *
  555. * This may generate calls to the port config callback before it returns,
  556. * or these may be deffered if a netconf is not available yet.
  557. *
  558. * @param node Node instance
  559. * @param nwid 64-bit ZeroTIer network ID
  560. * @return OK (0) or error code if a fatal error condition has occurred
  561. */
  562. enum ZT1_ResultCode ZT1_Node_join(ZT1_Node *node,uint64_t nwid);
  563. /**
  564. * Leave a network
  565. *
  566. * If a port has been configured for this network this will generate a call
  567. * to the port config callback with a NULL second parameter to indicate that
  568. * the port is now deleted.
  569. *
  570. * @param node Node instance
  571. * @param nwid 64-bit network ID
  572. * @return OK (0) or error code if a fatal error condition has occurred
  573. */
  574. enum ZT1_ResultCode ZT1_Node_leave(ZT1_Node *node,uint64_t nwid);
  575. /**
  576. * Get the status of this node
  577. *
  578. * @param node Node instance
  579. * @param status Buffer to fill with current node status
  580. */
  581. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status);
  582. /**
  583. * Get a list of known peer nodes
  584. *
  585. * The pointer returned here must be freed with freeQueryResult()
  586. * when you are done with it.
  587. *
  588. * @param node Node instance
  589. * @return List of known peers or NULL on failure
  590. */
  591. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node);
  592. /**
  593. * Get the status of a virtual network
  594. *
  595. * The pointer returned here must be freed with freeQueryResult()
  596. * when you are done with it.
  597. *
  598. * @param node Node instance
  599. * @param nwid 64-bit network ID
  600. * @return Network configuration or NULL if we are not a member of this network
  601. */
  602. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid);
  603. /**
  604. * Enumerate and get status of all networks
  605. *
  606. * @param node Node instance
  607. * @return List of networks or NULL on failure
  608. */
  609. ZT1_VirtualNetworkList *ZT1_Node_listNetworks(ZT1_Node *node);
  610. /**
  611. * Free a query result buffer
  612. *
  613. * Use this to free the return values of listNetworks(), listPeers(), etc.
  614. *
  615. * @param qr Query result buffer
  616. */
  617. void ZT1_Node_freeQueryResult(void *qr);
  618. /**
  619. * Set a network configuration master instance for this node
  620. *
  621. * Normal nodes should not need to use this. This is for nodes with
  622. * special compiled-in support for acting as network configuration
  623. * masters / controllers.
  624. *
  625. * The supplied instance must be a C++ object that inherits from the
  626. * NetworkConfigMaster base class in node/. No type checking is performed,
  627. * so a pointer to anything else will result in a crash.
  628. *
  629. * @param node ZertTier One node
  630. * @param networkConfigMasterInstance Instance of NetworkConfigMaster C++ class or NULL to disable
  631. * @return OK (0) or error code if a fatal error condition has occurred
  632. */
  633. enum ZT1_ResultCode ZT1_Node_setNetconfMaster(
  634. ZT1_Node *node,
  635. void *networkConfigMasterInstance);
  636. /**
  637. * Get ZeroTier One version
  638. *
  639. * @param major Result: major version
  640. * @param minor Result: minor version
  641. * @param revision Result: revision
  642. */
  643. void ZT1_version(int *major,int *minor,int *revision);
  644. #ifdef __cplusplus
  645. }
  646. #endif
  647. #endif