GoGlue.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #define _WIN32_WINNT 0x06010000
  14. #include "GoGlue.h"
  15. #include "../core/Constants.hpp"
  16. #include "../core/InetAddress.hpp"
  17. #include "../core/Node.hpp"
  18. #include "../core/Utils.hpp"
  19. #include "../core/MAC.hpp"
  20. #include "../core/Address.hpp"
  21. #include "../core/Containers.hpp"
  22. #include "../osdep/OSUtils.hpp"
  23. #include "../osdep/EthernetTap.hpp"
  24. #ifndef __WINDOWS__
  25. #include <unistd.h>
  26. #include <sys/socket.h>
  27. #include <sys/un.h>
  28. #include <sys/types.h>
  29. #include <sys/ioctl.h>
  30. #include <ifaddrs.h>
  31. #include <net/if.h>
  32. #include <netinet/in.h>
  33. #ifdef __BSD__
  34. #include <netinet6/in6_var.h>
  35. #endif
  36. #include <arpa/inet.h>
  37. #include <errno.h>
  38. #ifdef __LINUX__
  39. #ifndef IPV6_DONTFRAG
  40. #define IPV6_DONTFRAG 62
  41. #endif
  42. #endif
  43. #endif // !__WINDOWS__
  44. #include <thread>
  45. #include <mutex>
  46. #include <memory>
  47. #include <atomic>
  48. #ifdef __WINDOWS__
  49. #define SETSOCKOPT_FLAG_TYPE BOOL
  50. #define SETSOCKOPT_FLAG_TRUE TRUE
  51. #define SETSOCKOPT_FLAG_FALSE FALSE
  52. #else
  53. #define SETSOCKOPT_FLAG_TYPE int
  54. #define SETSOCKOPT_FLAG_TRUE 1
  55. #define SETSOCKOPT_FLAG_FALSE 0
  56. #endif
  57. #ifndef MSG_DONTWAIT
  58. #define MSG_DONTWAIT 0
  59. #endif
  60. using namespace ZeroTier;
  61. struct ZT_GoNodeThread
  62. {
  63. String ip;
  64. int port;
  65. int af;
  66. bool primary;
  67. std::atomic< bool > run;
  68. std::thread thr;
  69. };
  70. struct ZT_GoNode_Impl
  71. {
  72. void *goUserPtr;
  73. Node *node;
  74. volatile int64_t nextBackgroundTaskDeadline;
  75. String path;
  76. std::atomic< bool > run;
  77. Map< ZT_SOCKET, ZT_GoNodeThread > threads;
  78. Map< uint64_t, std::shared_ptr< EthernetTap > > taps;
  79. std::mutex threads_l;
  80. std::mutex taps_l;
  81. std::thread backgroundTaskThread;
  82. };
  83. static const String defaultHomePath(OSUtils::platformDefaultHomePath());
  84. const char *const ZT_PLATFORM_DEFAULT_HOMEPATH = defaultHomePath.c_str();
  85. // These are implemented in Go code.
  86. extern "C" int goPathCheckFunc(void *, const ZT_Identity *, int, const void *, int);
  87. extern "C" int goPathLookupFunc(void *, uint64_t, int, const ZT_Identity *, int *, uint8_t [16], int *);
  88. extern "C" void goStateObjectPutFunc(void *, int, const uint64_t *, const void *, int);
  89. extern "C" int goStateObjectGetFunc(void *, int, const uint64_t *, void **);
  90. extern "C" void goVirtualNetworkConfigFunc(void *, ZT_GoTap *, uint64_t, int, const ZT_VirtualNetworkConfig *);
  91. extern "C" void goZtEvent(void *, int, const void *);
  92. extern "C" void goHandleTapAddedMulticastGroup(void *, ZT_GoTap *, uint64_t, uint64_t, uint32_t);
  93. extern "C" void goHandleTapRemovedMulticastGroup(void *, ZT_GoTap *, uint64_t, uint64_t, uint32_t);
  94. static void ZT_GoNode_VirtualNetworkConfigFunction(
  95. ZT_Node *node,
  96. void *uptr,
  97. void *tptr,
  98. uint64_t nwid,
  99. void **nptr,
  100. enum ZT_VirtualNetworkConfigOperation op,
  101. const ZT_VirtualNetworkConfig *cfg)
  102. {
  103. goVirtualNetworkConfigFunc(reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr, reinterpret_cast<ZT_GoTap *>(*nptr), nwid, op, cfg);
  104. }
  105. static void ZT_GoNode_VirtualNetworkFrameFunction(
  106. ZT_Node *node,
  107. void *uptr,
  108. void *tptr,
  109. uint64_t nwid,
  110. void **nptr,
  111. uint64_t srcMac,
  112. uint64_t destMac,
  113. unsigned int etherType,
  114. unsigned int vlanId,
  115. const void *data,
  116. unsigned int len)
  117. {
  118. if (*nptr)
  119. reinterpret_cast<EthernetTap *>(*nptr)->put(MAC(srcMac), MAC(destMac), etherType, data, len);
  120. }
  121. static void ZT_GoNode_EventCallback(
  122. ZT_Node *node,
  123. void *uptr,
  124. void *tptr,
  125. enum ZT_Event et,
  126. const void *data)
  127. {
  128. goZtEvent(reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr, et, data);
  129. }
  130. static void ZT_GoNode_StatePutFunction(
  131. ZT_Node *node,
  132. void *uptr,
  133. void *tptr,
  134. enum ZT_StateObjectType objType,
  135. const uint64_t id[2],
  136. const void *data,
  137. int len)
  138. {
  139. goStateObjectPutFunc(
  140. reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr,
  141. objType,
  142. id,
  143. data,
  144. len);
  145. }
  146. static void _freeFunc(void *p)
  147. { if (p) free(p); }
  148. static int ZT_GoNode_StateGetFunction(
  149. ZT_Node *node,
  150. void *uptr,
  151. void *tptr,
  152. enum ZT_StateObjectType objType,
  153. const uint64_t id[2],
  154. void **data,
  155. void (**freeFunc)(void *))
  156. {
  157. *freeFunc = &_freeFunc;
  158. return goStateObjectGetFunc(
  159. reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr,
  160. (int)objType,
  161. id,
  162. data);
  163. }
  164. static ZT_INLINE void doUdpSend(ZT_SOCKET sock, const struct sockaddr_storage *addr, const void *data, const unsigned int len, const unsigned int ipTTL)
  165. {
  166. switch (addr->ss_family) {
  167. case AF_INET:
  168. if (unlikely((ipTTL > 0) && (ipTTL < 255))) {
  169. #ifdef __WINDOWS__
  170. DWORD tmp = (DWORD)ipTTL;
  171. #else
  172. int tmp = (int)ipTTL;
  173. #endif
  174. setsockopt(sock, IPPROTO_IP, IP_TTL, reinterpret_cast<const char *>(&tmp), sizeof(tmp));
  175. sendto(sock, reinterpret_cast<const char *>(data), len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in));
  176. tmp = 255;
  177. setsockopt(sock, IPPROTO_IP, IP_TTL, reinterpret_cast<const char *>(&tmp), sizeof(tmp));
  178. } else {
  179. sendto(sock, reinterpret_cast<const char *>(data), len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in));
  180. }
  181. break;
  182. case AF_INET6:
  183. // The ipTTL option isn't currently used with IPv6. It's only used
  184. // with IPv4 "firewall opener" / "NAT buster" preamble packets as part
  185. // of IPv4 NAT traversal.
  186. sendto(sock, reinterpret_cast<const char *>(data), len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in6));
  187. break;
  188. }
  189. }
  190. static int ZT_GoNode_WirePacketSendFunction(
  191. ZT_Node *node,
  192. void *uptr,
  193. void *tptr,
  194. int64_t localSocket,
  195. const struct sockaddr_storage *addr,
  196. const void *data,
  197. unsigned int len,
  198. unsigned int ipTTL)
  199. {
  200. if (likely(localSocket > 0)) {
  201. doUdpSend((ZT_SOCKET)localSocket, addr, data, len, ipTTL);
  202. } else {
  203. ZT_GoNode *const gn = reinterpret_cast<ZT_GoNode *>(uptr);
  204. std::lock_guard< std::mutex > l(gn->threads_l);
  205. for (auto t = gn->threads.begin(); t != gn->threads.end(); ++t) {
  206. if ((t->second.af == addr->ss_family) && (t->second.primary)) {
  207. doUdpSend(t->first, addr, data, len, ipTTL);
  208. break;
  209. }
  210. }
  211. }
  212. return 0;
  213. }
  214. static int ZT_GoNode_PathCheckFunction(
  215. ZT_Node *node,
  216. void *uptr,
  217. void *tptr,
  218. uint64_t ztAddress,
  219. const ZT_Identity *id,
  220. int64_t localSocket,
  221. const struct sockaddr_storage *sa)
  222. {
  223. switch (sa->ss_family) {
  224. case AF_INET:
  225. return goPathCheckFunc(
  226. reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr,
  227. id,
  228. AF_INET,
  229. &(reinterpret_cast<const struct sockaddr_in *>(sa)->sin_addr.s_addr),
  230. Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in *>(sa)->sin_port));
  231. case AF_INET6:
  232. return goPathCheckFunc(
  233. reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr,
  234. id,
  235. AF_INET6,
  236. reinterpret_cast<const struct sockaddr_in6 *>(sa)->sin6_addr.s6_addr,
  237. Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in6 *>(sa)->sin6_port));
  238. }
  239. return 0;
  240. }
  241. static int ZT_GoNode_PathLookupFunction(
  242. ZT_Node *node,
  243. void *uptr,
  244. void *tptr,
  245. uint64_t ztAddress,
  246. const ZT_Identity *id,
  247. int desiredAddressFamily,
  248. struct sockaddr_storage *sa)
  249. {
  250. int family = 0;
  251. uint8_t ip[16];
  252. int port = 0;
  253. const int result = goPathLookupFunc(
  254. reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr,
  255. ztAddress,
  256. desiredAddressFamily,
  257. id,
  258. &family,
  259. ip,
  260. &port
  261. );
  262. if (result != 0) {
  263. switch (family) {
  264. case AF_INET:
  265. reinterpret_cast<struct sockaddr_in *>(sa)->sin_family = AF_INET;
  266. memcpy(&(reinterpret_cast<struct sockaddr_in *>(sa)->sin_addr.s_addr), ip, 4);
  267. reinterpret_cast<struct sockaddr_in *>(sa)->sin_port = Utils::hton((uint16_t)port);
  268. return 1;
  269. case AF_INET6:
  270. reinterpret_cast<struct sockaddr_in6 *>(sa)->sin6_family = AF_INET6;
  271. memcpy(reinterpret_cast<struct sockaddr_in6 *>(sa)->sin6_addr.s6_addr, ip, 16);
  272. reinterpret_cast<struct sockaddr_in6 *>(sa)->sin6_port = Utils::hton((uint16_t)port);
  273. return 1;
  274. }
  275. }
  276. return 0;
  277. }
  278. extern "C" ZT_GoNode *ZT_GoNode_new(const char *workingPath, uintptr_t userPtr)
  279. {
  280. try {
  281. struct ZT_Node_Callbacks cb;
  282. cb.statePutFunction = &ZT_GoNode_StatePutFunction;
  283. cb.stateGetFunction = &ZT_GoNode_StateGetFunction;
  284. cb.wirePacketSendFunction = &ZT_GoNode_WirePacketSendFunction;
  285. cb.virtualNetworkFrameFunction = &ZT_GoNode_VirtualNetworkFrameFunction;
  286. cb.virtualNetworkConfigFunction = &ZT_GoNode_VirtualNetworkConfigFunction;
  287. cb.eventCallback = &ZT_GoNode_EventCallback;
  288. cb.pathCheckFunction = &ZT_GoNode_PathCheckFunction;
  289. cb.pathLookupFunction = &ZT_GoNode_PathLookupFunction;
  290. ZT_GoNode_Impl *gn = new ZT_GoNode_Impl;
  291. const int64_t now = OSUtils::now();
  292. gn->goUserPtr = reinterpret_cast<void *>(userPtr);
  293. gn->node = new Node(reinterpret_cast<void *>(gn), nullptr, &cb, now);
  294. gn->nextBackgroundTaskDeadline = now;
  295. gn->path = workingPath;
  296. gn->run = true;
  297. gn->backgroundTaskThread = std::thread([gn] {
  298. int64_t lastCheckedTaps = 0;
  299. while (gn->run) {
  300. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  301. const int64_t now = OSUtils::now();
  302. if (now >= gn->nextBackgroundTaskDeadline)
  303. gn->node->processBackgroundTasks(nullptr, now, &(gn->nextBackgroundTaskDeadline));
  304. if ((now - lastCheckedTaps) > 10000) {
  305. lastCheckedTaps = now;
  306. std::vector< MulticastGroup > added, removed;
  307. std::lock_guard< std::mutex > tl(gn->taps_l);
  308. for (auto t = gn->taps.begin(); t != gn->taps.end(); ++t) {
  309. added.clear();
  310. removed.clear();
  311. t->second->scanMulticastGroups(added, removed);
  312. for (auto g = added.begin(); g != added.end(); ++g)
  313. goHandleTapAddedMulticastGroup(gn, (ZT_GoTap *)t->second.get(), t->first, g->mac().toInt(), g->adi());
  314. for (auto g = removed.begin(); g != removed.end(); ++g)
  315. goHandleTapRemovedMulticastGroup(gn, (ZT_GoTap *)t->second.get(), t->first, g->mac().toInt(), g->adi());
  316. }
  317. }
  318. }
  319. });
  320. return gn;
  321. } catch (...) {
  322. fprintf(stderr, "FATAL: unable to create new instance of Node (out of memory?)" ZT_EOL_S);
  323. exit(1);
  324. }
  325. }
  326. extern "C" void ZT_GoNode_delete(ZT_GoNode *gn)
  327. {
  328. gn->run = false;
  329. gn->threads_l.lock();
  330. for (auto t = gn->threads.begin(); t != gn->threads.end(); ++t) {
  331. t->second.run = false;
  332. #ifdef __WINDOWS__
  333. shutdown(t->first, SD_BOTH);
  334. closesocket(t->first);
  335. #else
  336. shutdown(t->first, SHUT_RDWR);
  337. close(t->first);
  338. #endif
  339. t->second.thr.join();
  340. }
  341. gn->threads_l.unlock();
  342. gn->taps_l.lock();
  343. for (auto t = gn->taps.begin(); t != gn->taps.end(); ++t)
  344. gn->node->leave(t->first, nullptr, nullptr);
  345. gn->taps.clear();
  346. gn->taps_l.unlock();
  347. gn->backgroundTaskThread.join();
  348. gn->node->shutdown(nullptr);
  349. delete gn->node;
  350. delete gn;
  351. }
  352. extern "C" ZT_Node *ZT_GoNode_getNode(ZT_GoNode *gn)
  353. {
  354. return gn->node;
  355. }
  356. static void setCommonUdpSocketSettings(ZT_SOCKET udpSock, const char *dev)
  357. {
  358. int bufSize = 1048576;
  359. while (bufSize > 131072) {
  360. if (setsockopt(udpSock, SOL_SOCKET, SO_RCVBUF, (const char *)&bufSize, sizeof(bufSize)) == 0)
  361. break;
  362. bufSize -= 131072;
  363. }
  364. bufSize = 1048576;
  365. while (bufSize > 131072) {
  366. if (setsockopt(udpSock, SOL_SOCKET, SO_SNDBUF, (const char *)&bufSize, sizeof(bufSize)) == 0)
  367. break;
  368. bufSize -= 131072;
  369. }
  370. SETSOCKOPT_FLAG_TYPE fl;
  371. #ifdef SO_REUSEPORT
  372. fl = SETSOCKOPT_FLAG_TRUE;
  373. setsockopt(udpSock, SOL_SOCKET, SO_REUSEPORT, &fl, sizeof(fl));
  374. #endif
  375. #ifndef __LINUX__ // linux wants just SO_REUSEPORT
  376. fl = SETSOCKOPT_FLAG_TRUE;
  377. setsockopt(udpSock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&fl), sizeof(fl));
  378. #endif
  379. fl = SETSOCKOPT_FLAG_TRUE;
  380. setsockopt(udpSock, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<const char *>(&fl), sizeof(fl));
  381. #ifdef IP_DONTFRAG
  382. fl = SETSOCKOPT_FLAG_FALSE;
  383. setsockopt(udpSock,IPPROTO_IP,IP_DONTFRAG,&fl,sizeof(fl));
  384. #endif
  385. #ifdef IP_MTU_DISCOVER
  386. fl = SETSOCKOPT_FLAG_FALSE;
  387. setsockopt(udpSock,IPPROTO_IP,IP_MTU_DISCOVER,&fl,sizeof(fl));
  388. #endif
  389. #ifdef SO_BINDTODEVICE
  390. if ((dev)&&(strlen(dev)))
  391. setsockopt(udpSock,SOL_SOCKET,SO_BINDTODEVICE,dev,strlen(dev));
  392. #endif
  393. #if defined(__BSD__) && defined(IP_BOUND_IF)
  394. if ((dev) && (strlen(dev))) {
  395. int idx = if_nametoindex(dev);
  396. if (idx != 0)
  397. setsockopt(udpSock, IPPROTO_IP, IP_BOUND_IF, (void *)&idx, sizeof(idx));
  398. }
  399. #endif
  400. }
  401. extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn, const char *dev, const char *ip, const int port, const int primary)
  402. {
  403. if (strchr(ip, ':')) {
  404. struct sockaddr_in6 in6;
  405. memset(&in6, 0, sizeof(in6));
  406. in6.sin6_family = AF_INET6;
  407. if (inet_pton(AF_INET6, ip, &(in6.sin6_addr)) <= 0)
  408. return errno;
  409. in6.sin6_port = htons((uint16_t)port);
  410. ZT_SOCKET udpSock = socket(AF_INET6, SOCK_DGRAM, 0);
  411. if (udpSock == ZT_INVALID_SOCKET)
  412. return errno;
  413. setCommonUdpSocketSettings(udpSock, dev);
  414. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  415. setsockopt(udpSock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&fl, sizeof(fl));
  416. #ifdef IPV6_DONTFRAG
  417. fl = SETSOCKOPT_FLAG_FALSE;
  418. setsockopt(udpSock, IPPROTO_IPV6, IPV6_DONTFRAG, reinterpret_cast<const char *>(&fl), sizeof(fl));
  419. #endif
  420. if (bind(udpSock, reinterpret_cast<const struct sockaddr *>(&in6), sizeof(in6)) != 0)
  421. return errno;
  422. {
  423. std::lock_guard< std::mutex > l(gn->threads_l);
  424. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  425. gnt.ip = ip;
  426. gnt.port = port;
  427. gnt.af = AF_INET6;
  428. gnt.primary = (primary != 0);
  429. gnt.run = true;
  430. gnt.thr = std::thread([udpSock, gn, &gnt] {
  431. struct sockaddr_in6 in6;
  432. socklen_t salen;
  433. while (gnt.run) {
  434. salen = sizeof(in6);
  435. void *buf = ZT_getBuffer();
  436. if (buf) {
  437. int s = (int)recvfrom(udpSock, reinterpret_cast<char *>(buf), 16384, 0, reinterpret_cast<struct sockaddr *>(&in6), &salen);
  438. if (s > 0) {
  439. ZT_Node_processWirePacket(
  440. reinterpret_cast<ZT_Node *>(gn->node),
  441. nullptr,
  442. OSUtils::now(),
  443. (int64_t)udpSock,
  444. reinterpret_cast<const struct sockaddr_storage *>(&in6),
  445. buf,
  446. (unsigned int)s,
  447. 1,
  448. &(gn->nextBackgroundTaskDeadline));
  449. } else {
  450. ZT_freeBuffer(buf);
  451. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  452. }
  453. }
  454. }
  455. });
  456. }
  457. } else {
  458. struct sockaddr_in in;
  459. memset(&in, 0, sizeof(in));
  460. in.sin_family = AF_INET;
  461. if (inet_pton(AF_INET, ip, &(in.sin_addr)) <= 0)
  462. return errno;
  463. in.sin_port = htons((uint16_t)port);
  464. ZT_SOCKET udpSock = socket(AF_INET, SOCK_DGRAM, 0);
  465. if (udpSock == ZT_INVALID_SOCKET)
  466. return errno;
  467. setCommonUdpSocketSettings(udpSock, dev);
  468. #ifdef SO_NO_CHECK
  469. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  470. setsockopt(udpSock,SOL_SOCKET,SO_NO_CHECK,&fl,sizeof(fl));
  471. #endif
  472. if (bind(udpSock, reinterpret_cast<const struct sockaddr *>(&in), sizeof(in)) != 0)
  473. return errno;
  474. {
  475. std::lock_guard< std::mutex > l(gn->threads_l);
  476. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  477. gnt.ip = ip;
  478. gnt.port = port;
  479. gnt.af = AF_INET6;
  480. gnt.primary = (primary != 0);
  481. gnt.run = true;
  482. gnt.thr = std::thread([udpSock, gn, &gnt] {
  483. struct sockaddr_in in4;
  484. socklen_t salen;
  485. while (gnt.run) {
  486. salen = sizeof(in4);
  487. void *buf = ZT_getBuffer();
  488. if (buf) {
  489. int s = (int)recvfrom(udpSock, reinterpret_cast<char *>(buf), sizeof(buf), 0, reinterpret_cast<struct sockaddr *>(&in4), &salen);
  490. if (s > 0) {
  491. ZT_Node_processWirePacket(
  492. reinterpret_cast<ZT_Node *>(gn->node),
  493. nullptr,
  494. OSUtils::now(),
  495. (int64_t)udpSock,
  496. reinterpret_cast<const struct sockaddr_storage *>(&in4),
  497. buf,
  498. (unsigned int)s,
  499. 1,
  500. &(gn->nextBackgroundTaskDeadline));
  501. } else {
  502. ZT_freeBuffer(buf);
  503. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  504. }
  505. }
  506. }
  507. });
  508. }
  509. }
  510. return 0;
  511. }
  512. extern "C" int ZT_GoNode_phyStopListen(ZT_GoNode *gn, const char *dev, const char *ip, const int port)
  513. {
  514. {
  515. std::lock_guard< std::mutex > l(gn->threads_l);
  516. for (auto t = gn->threads.begin(); t != gn->threads.end();) {
  517. if ((t->second.ip == ip) && (t->second.port == port)) {
  518. t->second.run = false;
  519. #ifdef __WINDOWS__
  520. shutdown(t->first, SD_BOTH);
  521. closesocket(t->first);
  522. #else
  523. shutdown(t->first, SHUT_RDWR);
  524. close(t->first);
  525. #endif
  526. t->second.thr.join();
  527. gn->threads.erase(t++);
  528. } else ++t;
  529. }
  530. }
  531. return 0;
  532. }
  533. static void tapFrameHandler(void *uptr, void *tptr, uint64_t nwid, const MAC &from, const MAC &to, unsigned int etherType, unsigned int vlanId, const void *data, unsigned int len)
  534. {
  535. ZT_Node_processVirtualNetworkFrame(
  536. reinterpret_cast<ZT_Node *>(reinterpret_cast<ZT_GoNode *>(uptr)->node),
  537. tptr,
  538. OSUtils::now(),
  539. nwid,
  540. from.toInt(),
  541. to.toInt(),
  542. etherType,
  543. vlanId,
  544. data,
  545. len,
  546. 0,
  547. &(reinterpret_cast<ZT_GoNode *>(uptr)->nextBackgroundTaskDeadline));
  548. }
  549. extern "C" ZT_GoTap *ZT_GoNode_join(ZT_GoNode *gn, uint64_t nwid, const ZT_Fingerprint *const controllerFingerprint)
  550. {
  551. try {
  552. std::lock_guard< std::mutex > l(gn->taps_l);
  553. auto existingTap = gn->taps.find(nwid);
  554. if (existingTap != gn->taps.end())
  555. return (ZT_GoTap *)existingTap->second.get();
  556. char tmp[256];
  557. OSUtils::ztsnprintf(tmp, sizeof(tmp), "ZeroTier Network %.16llx", (unsigned long long)nwid);
  558. std::shared_ptr< EthernetTap > tap(EthernetTap::newInstance(nullptr, gn->path.c_str(), MAC(Address(gn->node->address()), nwid), ZT_DEFAULT_MTU, 0, nwid, tmp, &tapFrameHandler, gn));
  559. if (!tap)
  560. return nullptr;
  561. gn->taps[nwid] = tap;
  562. gn->node->join(nwid, controllerFingerprint, tap.get(), nullptr);
  563. return (ZT_GoTap *)tap.get();
  564. } catch (...) {
  565. return nullptr;
  566. }
  567. }
  568. extern "C" void ZT_GoNode_leave(ZT_GoNode *gn, uint64_t nwid)
  569. {
  570. std::lock_guard< std::mutex > l(gn->taps_l);
  571. auto existingTap = gn->taps.find(nwid);
  572. if (existingTap != gn->taps.end()) {
  573. gn->node->leave(nwid, nullptr, nullptr);
  574. gn->taps.erase(existingTap);
  575. }
  576. }
  577. extern "C" void ZT_GoTap_setEnabled(ZT_GoTap *tap, int enabled)
  578. {
  579. reinterpret_cast<EthernetTap *>(tap)->setEnabled(enabled != 0);
  580. }
  581. extern "C" int ZT_GoTap_addIp(ZT_GoTap *tap, int af, const void *ip, int netmaskBits)
  582. {
  583. switch (af) {
  584. case AF_INET:
  585. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip, 4, (unsigned int)netmaskBits)) ? 1 : 0);
  586. case AF_INET6:
  587. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip, 16, (unsigned int)netmaskBits)) ? 1 : 0);
  588. }
  589. return 0;
  590. }
  591. extern "C" int ZT_GoTap_removeIp(ZT_GoTap *tap, int af, const void *ip, int netmaskBits)
  592. {
  593. switch (af) {
  594. case AF_INET:
  595. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip, 4, (unsigned int)netmaskBits)) ? 1 : 0);
  596. case AF_INET6:
  597. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip, 16, (unsigned int)netmaskBits)) ? 1 : 0);
  598. }
  599. return 0;
  600. }
  601. extern "C" int ZT_GoTap_ips(ZT_GoTap *tap, void *buf, unsigned int bufSize)
  602. {
  603. auto ips = reinterpret_cast<EthernetTap *>(tap)->ips();
  604. unsigned int p = 0;
  605. uint8_t *const b = reinterpret_cast<uint8_t *>(buf);
  606. for (auto ip = ips.begin(); ip != ips.end(); ++ip) {
  607. if ((p + 6) > bufSize)
  608. break;
  609. const uint8_t *const ipd = reinterpret_cast<const uint8_t *>(ip->rawIpData());
  610. if (ip->isV4()) {
  611. b[p++] = AF_INET;
  612. b[p++] = ipd[0];
  613. b[p++] = ipd[1];
  614. b[p++] = ipd[2];
  615. b[p++] = ipd[3];
  616. b[p++] = (uint8_t)ip->netmaskBits();
  617. } else if (ip->isV6()) {
  618. if ((p + 18) <= bufSize) {
  619. b[p++] = AF_INET6;
  620. for (int j = 0; j < 16; ++j)
  621. b[p++] = ipd[j];
  622. b[p++] = (uint8_t)ip->netmaskBits();
  623. }
  624. }
  625. }
  626. return (int)p;
  627. }
  628. extern "C" void ZT_GoTap_deviceName(ZT_GoTap *tap, char nbuf[256])
  629. {
  630. Utils::scopy(nbuf, 256, reinterpret_cast<EthernetTap *>(tap)->deviceName().c_str());
  631. }
  632. extern "C" void ZT_GoTap_setFriendlyName(ZT_GoTap *tap, const char *friendlyName)
  633. {
  634. reinterpret_cast<EthernetTap *>(tap)->setFriendlyName(friendlyName);
  635. }
  636. extern "C" void ZT_GoTap_setMtu(ZT_GoTap *tap, unsigned int mtu)
  637. {
  638. reinterpret_cast<EthernetTap *>(tap)->setMtu(mtu);
  639. }
  640. #if defined(IFA_F_SECONDARY) && !defined(IFA_F_TEMPORARY)
  641. #define IFA_F_TEMPORARY IFA_F_SECONDARY
  642. #endif
  643. extern "C" int ZT_isTemporaryV6Address(const char *ifname, const struct sockaddr_storage *a)
  644. {
  645. #if defined(IN6_IFF_TEMPORARY) && defined(SIOCGIFAFLAG_IN6)
  646. static ZT_SOCKET s_tmpV6Socket = ZT_INVALID_SOCKET;
  647. static std::mutex s_lock;
  648. std::lock_guard< std::mutex > l(s_lock);
  649. if (s_tmpV6Socket == ZT_INVALID_SOCKET) {
  650. s_tmpV6Socket = socket(AF_INET6, SOCK_DGRAM, 0);
  651. if (s_tmpV6Socket <= 0)
  652. return 0;
  653. }
  654. struct in6_ifreq ifr;
  655. memset(&ifr, 0, sizeof(ifr));
  656. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  657. memcpy(&(ifr.ifr_addr), a, sizeof(sockaddr_in6));
  658. if (ioctl(s_tmpV6Socket, SIOCGIFAFLAG_IN6, &ifr) < 0) {
  659. return 0;
  660. }
  661. return ((ifr.ifr_ifru.ifru_flags6 & IN6_IFF_TEMPORARY) != 0) ? 1 : 0;
  662. #else
  663. return 0;
  664. #endif
  665. }
  666. extern "C" void *ZT_malloc(unsigned long s)
  667. { return (void *)malloc((size_t)s); }