ZeroTierOne.h 26 KB

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