GoGlue.cpp 19 KB

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