GoGlue.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Copyright (c)2019 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: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by vergnn 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 "../../osdep/OSUtils.hpp"
  21. #include "../../osdep/EthernetTap.hpp"
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #ifndef __WINDOWS__
  26. #include <errno.h>
  27. #include <signal.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32. #include <sys/select.h>
  33. #include <sys/socket.h>
  34. #include <sys/un.h>
  35. #include <arpa/inet.h>
  36. #include <netinet/in.h>
  37. #include <netinet/ip6.h>
  38. #include <netinet/tcp.h>
  39. #ifdef __BSD__
  40. #include <net/if.h>
  41. #endif
  42. #ifdef __LINUX__
  43. #ifndef IPV6_DONTFRAG
  44. #define IPV6_DONTFRAG 62
  45. #endif
  46. #endif
  47. #endif // !__WINDOWS__
  48. #include <thread>
  49. #include <mutex>
  50. #include <map>
  51. #include <vector>
  52. #include <array>
  53. #include <set>
  54. #include <memory>
  55. #ifdef __WINDOWS__
  56. #define SETSOCKOPT_FLAG_TYPE BOOL
  57. #define SETSOCKOPT_FLAG_TRUE TRUE
  58. #define SETSOCKOPT_FLAG_FALSE FALSE
  59. #else
  60. #define SETSOCKOPT_FLAG_TYPE int
  61. #define SETSOCKOPT_FLAG_TRUE 1
  62. #define SETSOCKOPT_FLAG_FALSE 0
  63. #endif
  64. #ifndef MSG_DONTWAIT
  65. #define MSG_DONTWAIT 0
  66. #endif
  67. using namespace ZeroTier;
  68. struct ZT_GoNodeThread
  69. {
  70. std::string ip;
  71. int port;
  72. int af;
  73. std::atomic_bool run;
  74. std::thread thr;
  75. };
  76. struct ZT_GoNode_Impl
  77. {
  78. Node *node;
  79. volatile int64_t nextBackgroundTaskDeadline;
  80. std::string path;
  81. std::atomic_bool run;
  82. std::map< ZT_SOCKET,ZT_GoNodeThread > threads;
  83. std::mutex threads_l;
  84. std::map< uint64_t,std::shared_ptr<EthernetTap> > taps;
  85. std::mutex taps_l;
  86. std::thread backgroundTaskThread;
  87. };
  88. /****************************************************************************/
  89. /* These functions are implemented in Go in pkg/ztnode/node-callbacks.go */
  90. extern "C" int goPathCheckFunc(ZT_GoNode *,uint64_t,int,const void *,int);
  91. extern "C" int goPathLookupFunc(ZT_GoNode *,uint64_t,int,int *,uint8_t [16],int *);
  92. extern "C" void goStateObjectPutFunc(ZT_GoNode *,int,const uint64_t [2],const void *,int);
  93. extern "C" int goStateObjectGetFunc(ZT_GoNode *,int,const uint64_t [2],void *,unsigned int);
  94. extern "C" void goDNSResolverFunc(ZT_GoNode *,const uint8_t *,int,const char *,uintptr_t);
  95. extern "C" int goVirtualNetworkConfigFunc(ZT_GoNode *,ZT_GoTap *,uint64_t,int,const ZT_VirtualNetworkConfig *);
  96. extern "C" void goZtEvent(ZT_GoNode *,int,const void *);
  97. extern "C" void goHandleTapAddedMulticastGroup(ZT_GoNode *,ZT_GoTap *,uint64_t,uint64_t,uint32_t);
  98. extern "C" void goHandleTapRemovedMulticastGroup(ZT_GoNode *,ZT_GoTap *,uint64_t,uint64_t,uint32_t);
  99. static int ZT_GoNode_VirtualNetworkConfigFunction(
  100. ZT_Node *node,
  101. void *uptr,
  102. void *tptr,
  103. uint64_t nwid,
  104. void **nptr,
  105. enum ZT_VirtualNetworkConfigOperation op,
  106. const ZT_VirtualNetworkConfig *cfg)
  107. {
  108. return goVirtualNetworkConfigFunc(reinterpret_cast<ZT_GoNode *>(uptr),reinterpret_cast<ZT_GoTap *>(*nptr),nwid,op,cfg);
  109. }
  110. static void ZT_GoNode_VirtualNetworkFrameFunction(
  111. ZT_Node *node,
  112. void *uptr,
  113. void *tptr,
  114. uint64_t nwid,
  115. void **nptr,
  116. uint64_t srcMac,
  117. uint64_t destMac,
  118. unsigned int etherType,
  119. unsigned int vlanId,
  120. const void *data,
  121. unsigned int len)
  122. {
  123. if (*nptr)
  124. reinterpret_cast<EthernetTap *>(*nptr)->put(MAC(srcMac),MAC(destMac),etherType,data,len);
  125. }
  126. static void ZT_GoNode_EventCallback(
  127. ZT_Node *node,
  128. void *uptr,
  129. void *tptr,
  130. enum ZT_Event et,
  131. const void *data)
  132. {
  133. goZtEvent(reinterpret_cast<ZT_GoNode *>(uptr),et,data);
  134. }
  135. static void ZT_GoNode_StatePutFunction(
  136. ZT_Node *node,
  137. void *uptr,
  138. void *tptr,
  139. enum ZT_StateObjectType objType,
  140. const uint64_t id[2],
  141. const void *data,
  142. int len)
  143. {
  144. goStateObjectPutFunc(reinterpret_cast<ZT_GoNode *>(uptr),objType,id,data,len);
  145. }
  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 *buf,
  153. unsigned int buflen)
  154. {
  155. return goStateObjectGetFunc(
  156. reinterpret_cast<ZT_GoNode *>(uptr),
  157. (int)objType,
  158. id,
  159. buf,
  160. buflen);
  161. }
  162. static ZT_ALWAYS_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 ((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 ((localSocket != -1)&&(localSocket != ZT_INVALID_SOCKET)) {
  199. doUdpSend((ZT_SOCKET)localSocket,addr,data,len,ipTTL);
  200. } else {
  201. ZT_GoNode *const gn = reinterpret_cast<ZT_GoNode *>(uptr);
  202. std::set<std::string> ipsSentFrom;
  203. std::lock_guard<std::mutex> l(gn->threads_l);
  204. for(auto t=gn->threads.begin();t!=gn->threads.end();++t) {
  205. if (t->second.af == addr->ss_family) {
  206. if (ipsSentFrom.insert(t->second.ip).second) {
  207. doUdpSend(t->first,addr,data,len,ipTTL);
  208. }
  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. int64_t localSocket,
  220. const struct sockaddr_storage *sa)
  221. {
  222. switch(sa->ss_family) {
  223. case AF_INET:
  224. return goPathCheckFunc(
  225. reinterpret_cast<ZT_GoNode *>(uptr),
  226. ztAddress,
  227. AF_INET,
  228. &(reinterpret_cast<const struct sockaddr_in *>(sa)->sin_addr.s_addr),
  229. Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in *>(sa)->sin_port));
  230. case AF_INET6:
  231. return goPathCheckFunc(
  232. reinterpret_cast<ZT_GoNode *>(uptr),
  233. ztAddress,
  234. AF_INET6,
  235. reinterpret_cast<const struct sockaddr_in6 *>(sa)->sin6_addr.s6_addr,
  236. Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in6 *>(sa)->sin6_port));
  237. }
  238. return 0;
  239. }
  240. static int ZT_GoNode_PathLookupFunction(
  241. ZT_Node *node,
  242. void *uptr,
  243. void *tptr,
  244. uint64_t ztAddress,
  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),
  253. ztAddress,
  254. desiredAddressFamily,
  255. &family,
  256. ip,
  257. &port
  258. );
  259. if (result != 0) {
  260. switch(family) {
  261. case AF_INET:
  262. reinterpret_cast<struct sockaddr_in *>(sa)->sin_family = AF_INET;
  263. memcpy(&(reinterpret_cast<struct sockaddr_in *>(sa)->sin_addr.s_addr),ip,4);
  264. reinterpret_cast<struct sockaddr_in *>(sa)->sin_port = Utils::hton((uint16_t)port);
  265. return 1;
  266. case AF_INET6:
  267. reinterpret_cast<struct sockaddr_in6 *>(sa)->sin6_family = AF_INET6;
  268. memcpy(reinterpret_cast<struct sockaddr_in6 *>(sa)->sin6_addr.s6_addr,ip,16);
  269. reinterpret_cast<struct sockaddr_in6 *>(sa)->sin6_port = Utils::hton((uint16_t)port);
  270. return 1;
  271. }
  272. }
  273. return 0;
  274. }
  275. static void ZT_GoNode_DNSResolver(
  276. ZT_Node *node,
  277. void *uptr,
  278. void *tptr,
  279. const enum ZT_DNSRecordType *types,
  280. unsigned int numTypes,
  281. const char *name,
  282. uintptr_t requestId)
  283. {
  284. uint8_t t[256];
  285. for(unsigned int i=0;(i<numTypes)&&(i<256);++i) t[i] = (uint8_t)types[i];
  286. goDNSResolverFunc(reinterpret_cast<ZT_GoNode *>(uptr),t,(int)numTypes,name,requestId);
  287. }
  288. /****************************************************************************/
  289. extern "C" ZT_GoNode *ZT_GoNode_new(const char *workingPath)
  290. {
  291. try {
  292. struct ZT_Node_Callbacks cb;
  293. cb.statePutFunction = &ZT_GoNode_StatePutFunction;
  294. cb.stateGetFunction = &ZT_GoNode_StateGetFunction;
  295. cb.wirePacketSendFunction = &ZT_GoNode_WirePacketSendFunction;
  296. cb.virtualNetworkFrameFunction = &ZT_GoNode_VirtualNetworkFrameFunction;
  297. cb.virtualNetworkConfigFunction = &ZT_GoNode_VirtualNetworkConfigFunction;
  298. cb.eventCallback = &ZT_GoNode_EventCallback;
  299. cb.dnsResolver = &ZT_GoNode_DNSResolver;
  300. cb.pathCheckFunction = &ZT_GoNode_PathCheckFunction;
  301. cb.pathLookupFunction = &ZT_GoNode_PathLookupFunction;
  302. ZT_GoNode_Impl *gn = new ZT_GoNode_Impl;
  303. const int64_t now = OSUtils::now();
  304. gn->node = new Node(reinterpret_cast<void *>(gn),nullptr,&cb,now);
  305. gn->nextBackgroundTaskDeadline = now;
  306. gn->path = workingPath;
  307. gn->run = true;
  308. gn->backgroundTaskThread = std::thread([gn] {
  309. int64_t lastScannedMulticastGroups = 0;
  310. while (gn->run) {
  311. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  312. const int64_t now = OSUtils::now();
  313. if (now >= gn->nextBackgroundTaskDeadline)
  314. gn->node->processBackgroundTasks(nullptr,now,&(gn->nextBackgroundTaskDeadline));
  315. if ((now - lastScannedMulticastGroups) > 5000) {
  316. lastScannedMulticastGroups = now;
  317. std::vector<MulticastGroup> added,removed;
  318. std::lock_guard<std::mutex> tl(gn->taps_l);
  319. for(auto t=gn->taps.begin();t!=gn->taps.end();++t) {
  320. added.clear();
  321. removed.clear();
  322. t->second->scanMulticastGroups(added,removed);
  323. for(auto g=added.begin();g!=added.end();++g)
  324. goHandleTapAddedMulticastGroup(gn,(ZT_GoTap *)t->second.get(),t->first,g->mac().toInt(),g->adi());
  325. for(auto g=removed.begin();g!=removed.end();++g)
  326. goHandleTapRemovedMulticastGroup(gn,(ZT_GoTap *)t->second.get(),t->first,g->mac().toInt(),g->adi());
  327. }
  328. }
  329. }
  330. });
  331. return gn;
  332. } catch ( ... ) {
  333. fprintf(stderr,"FATAL: unable to create new instance of Node (out of memory?)" ZT_EOL_S);
  334. exit(1);
  335. }
  336. }
  337. extern "C" void ZT_GoNode_delete(ZT_GoNode *gn)
  338. {
  339. gn->run = false;
  340. gn->threads_l.lock();
  341. for(auto t=gn->threads.begin();t!=gn->threads.end();++t) {
  342. t->second.run = false;
  343. shutdown(t->first,SHUT_RDWR);
  344. close(t->first);
  345. t->second.thr.join();
  346. }
  347. gn->threads_l.unlock();
  348. gn->taps_l.lock();
  349. for(auto t=gn->taps.begin();t!=gn->taps.end();++t)
  350. gn->node->leave(t->first,nullptr,nullptr);
  351. gn->taps.clear();
  352. gn->taps_l.unlock();
  353. gn->backgroundTaskThread.join();
  354. delete gn->node;
  355. delete gn;
  356. }
  357. extern "C" ZT_Node *ZT_GoNode_getNode(ZT_GoNode *gn)
  358. {
  359. return gn->node;
  360. }
  361. // Sets flags and socket options common to both IPv4 and IPv6 UDP sockets
  362. static void setCommonUdpSocketSettings(ZT_SOCKET udpSock,const char *dev)
  363. {
  364. int bufSize = 1048576;
  365. while (bufSize > 131072) {
  366. if (setsockopt(udpSock,SOL_SOCKET,SO_RCVBUF,(const char *)&bufSize,sizeof(bufSize)) == 0)
  367. break;
  368. bufSize -= 131072;
  369. }
  370. bufSize = 1048576;
  371. while (bufSize > 131072) {
  372. if (setsockopt(udpSock,SOL_SOCKET,SO_SNDBUF,(const char *)&bufSize,sizeof(bufSize)) == 0)
  373. break;
  374. bufSize -= 131072;
  375. }
  376. SETSOCKOPT_FLAG_TYPE fl;
  377. #ifdef SO_REUSEPORT
  378. fl = SETSOCKOPT_FLAG_TRUE;
  379. setsockopt(udpSock,SOL_SOCKET,SO_REUSEPORT,(void *)&fl,sizeof(fl));
  380. #endif
  381. #ifndef __LINUX__ // linux wants just SO_REUSEPORT
  382. fl = SETSOCKOPT_FLAG_TRUE;
  383. setsockopt(udpSock,SOL_SOCKET,SO_REUSEADDR,(void *)&fl,sizeof(fl));
  384. #endif
  385. fl = SETSOCKOPT_FLAG_TRUE;
  386. setsockopt(udpSock,SOL_SOCKET,SO_BROADCAST,(void *)&fl,sizeof(fl));
  387. #ifdef IP_DONTFRAG
  388. fl = SETSOCKOPT_FLAG_FALSE;
  389. setsockopt(udpSock,IPPROTO_IP,IP_DONTFRAG,(void *)&fl,sizeof(fl));
  390. #endif
  391. #ifdef IP_MTU_DISCOVER
  392. fl = SETSOCKOPT_FLAG_FALSE;
  393. setsockopt(udpSock,IPPROTO_IP,IP_MTU_DISCOVER,(void *)&fl,sizeof(fl));
  394. #endif
  395. #ifdef SO_BINDTODEVICE
  396. if ((dev)&&(strlen(dev)))
  397. setsockopt(udpSock,SOL_SOCKET,SO_BINDTODEVICE,dev,strlen(dev));
  398. #endif
  399. #if defined(__BSD__) && defined(IP_BOUND_IF)
  400. if ((dev)&&(strlen(dev))) {
  401. int idx = if_nametoindex(dev);
  402. if (idx != 0)
  403. setsockopt(udpSock,IPPROTO_IP,IP_BOUND_IF,(void *)&idx,sizeof(idx));
  404. }
  405. #endif
  406. }
  407. extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn,const char *dev,const char *ip,const int port)
  408. {
  409. if (strchr(ip,':')) {
  410. struct sockaddr_in6 in6;
  411. memset(&in6,0,sizeof(in6));
  412. in6.sin6_family = AF_INET6;
  413. if (inet_pton(AF_INET6,ip,&(in6.sin6_addr)) <= 0)
  414. return errno;
  415. in6.sin6_port = htons((uint16_t)port);
  416. ZT_SOCKET udpSock = socket(AF_INET6,SOCK_DGRAM,0);
  417. if (udpSock == ZT_INVALID_SOCKET)
  418. return errno;
  419. setCommonUdpSocketSettings(udpSock,dev);
  420. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  421. setsockopt(udpSock,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&fl,sizeof(fl));
  422. #ifdef IPV6_DONTFRAG
  423. fl = SETSOCKOPT_FLAG_FALSE;
  424. setsockopt(udpSock,IPPROTO_IPV6,IPV6_DONTFRAG,&fl,sizeof(fl));
  425. #endif
  426. if (bind(udpSock,reinterpret_cast<const struct sockaddr *>(&in6),sizeof(in6)) != 0)
  427. return errno;
  428. {
  429. std::lock_guard<std::mutex> l(gn->threads_l);
  430. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  431. gnt.ip = ip;
  432. gnt.port = port;
  433. gnt.af = AF_INET6;
  434. gnt.run = true;
  435. gnt.thr = std::thread([udpSock,gn,&gnt] {
  436. struct sockaddr_in6 in6;
  437. socklen_t salen;
  438. char buf[16384];
  439. while (gnt.run) {
  440. salen = sizeof(in6);
  441. int s = (int)recvfrom(udpSock,buf,sizeof(buf),0,reinterpret_cast<struct sockaddr *>(&in6),&salen);
  442. if (s > 0) {
  443. gn->node->processWirePacket(&gnt,OSUtils::now(),(int64_t)udpSock,reinterpret_cast<const struct sockaddr_storage *>(&in6),buf,(unsigned int)s,&(gn->nextBackgroundTaskDeadline));
  444. }
  445. }
  446. });
  447. }
  448. } else {
  449. struct sockaddr_in in;
  450. memset(&in,0,sizeof(in));
  451. in.sin_family = AF_INET;
  452. if (inet_pton(AF_INET,ip,&(in.sin_addr)) <= 0)
  453. return errno;
  454. in.sin_port = htons((uint16_t)port);
  455. ZT_SOCKET udpSock = socket(AF_INET,SOCK_DGRAM,0);
  456. if (udpSock == ZT_INVALID_SOCKET)
  457. return errno;
  458. setCommonUdpSocketSettings(udpSock,dev);
  459. #ifdef SO_NO_CHECK
  460. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  461. setsockopt(udpSock,SOL_SOCKET,SO_NO_CHECK,&fl,sizeof(fl));
  462. #endif
  463. if (bind(udpSock,reinterpret_cast<const struct sockaddr *>(&in),sizeof(in)) != 0)
  464. return errno;
  465. {
  466. std::lock_guard<std::mutex> l(gn->threads_l);
  467. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  468. gnt.ip = ip;
  469. gnt.port = port;
  470. gnt.af = AF_INET6;
  471. gnt.run = true;
  472. gnt.thr = std::thread([udpSock,gn,&gnt] {
  473. struct sockaddr_in in4;
  474. socklen_t salen;
  475. char buf[16384];
  476. while (gnt.run) {
  477. salen = sizeof(in4);
  478. int s = (int)recvfrom(udpSock,buf,sizeof(buf),0,reinterpret_cast<struct sockaddr *>(&in4),&salen);
  479. if (s > 0) {
  480. gn->node->processWirePacket(&gnt,OSUtils::now(),(int64_t)udpSock,reinterpret_cast<const struct sockaddr_storage *>(&in4),buf,(unsigned int)s,&(gn->nextBackgroundTaskDeadline));
  481. }
  482. }
  483. });
  484. }
  485. }
  486. return 0;
  487. }
  488. extern "C" int ZT_GoNode_phyStopListen(ZT_GoNode *gn,const char *dev,const char *ip,const int port)
  489. {
  490. {
  491. std::lock_guard<std::mutex> l(gn->threads_l);
  492. for(auto t=gn->threads.begin();t!=gn->threads.end();) {
  493. if ((t->second.ip == ip)&&(t->second.port == port)) {
  494. t->second.run = false;
  495. shutdown(t->first,SHUT_RDWR);
  496. close(t->first);
  497. t->second.thr.join();
  498. gn->threads.erase(t++);
  499. } else ++t;
  500. }
  501. }
  502. return 0;
  503. }
  504. 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)
  505. {
  506. ZT_GoNode *const gn = reinterpret_cast<ZT_GoNode *>(uptr);
  507. gn->node->processVirtualNetworkFrame(tptr,OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&(gn->nextBackgroundTaskDeadline));
  508. }
  509. extern "C" ZT_GoTap *ZT_GoNode_join(ZT_GoNode *gn,uint64_t nwid)
  510. {
  511. try {
  512. std::lock_guard<std::mutex> l(gn->taps_l);
  513. auto existingTap = gn->taps.find(nwid);
  514. if (existingTap != gn->taps.end())
  515. return (ZT_GoTap *)existingTap->second.get();
  516. char tmp[256];
  517. OSUtils::ztsnprintf(tmp,sizeof(tmp),"ZeroTier Network %.16llx",(unsigned long long)nwid);
  518. 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));
  519. if (!tap)
  520. return nullptr;
  521. gn->taps[nwid] = tap;
  522. gn->node->join(nwid,tap.get(),nullptr);
  523. return (ZT_GoTap *)tap.get();
  524. } catch ( ... ) {
  525. return nullptr;
  526. }
  527. }
  528. extern "C" void ZT_GoNode_leave(ZT_GoNode *gn,uint64_t nwid)
  529. {
  530. std::lock_guard<std::mutex> l(gn->taps_l);
  531. auto existingTap = gn->taps.find(nwid);
  532. if (existingTap != gn->taps.end()) {
  533. gn->node->leave(nwid,nullptr,nullptr);
  534. gn->taps.erase(existingTap);
  535. }
  536. }
  537. /****************************************************************************/
  538. extern "C" void ZT_GoTap_setEnabled(ZT_GoTap *tap,int enabled)
  539. {
  540. reinterpret_cast<EthernetTap *>(tap)->setEnabled(enabled != 0);
  541. }
  542. extern "C" int ZT_GoTap_addIp(ZT_GoTap *tap,int af,const void *ip,int netmaskBits)
  543. {
  544. switch(af) {
  545. case AF_INET:
  546. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip,4,(unsigned int)netmaskBits)) ? 1 : 0);
  547. case AF_INET6:
  548. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip,16,(unsigned int)netmaskBits)) ? 1 : 0);
  549. }
  550. return 0;
  551. }
  552. extern "C" int ZT_GoTap_removeIp(ZT_GoTap *tap,int af,const void *ip,int netmaskBits)
  553. {
  554. switch(af) {
  555. case AF_INET:
  556. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip,4,(unsigned int)netmaskBits)) ? 1 : 0);
  557. case AF_INET6:
  558. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip,16,(unsigned int)netmaskBits)) ? 1 : 0);
  559. }
  560. return 0;
  561. }
  562. extern "C" int ZT_GoTap_ips(ZT_GoTap *tap,void *buf,unsigned int bufSize)
  563. {
  564. auto ips = reinterpret_cast<EthernetTap *>(tap)->ips();
  565. unsigned int p = 0;
  566. uint8_t *const b = reinterpret_cast<uint8_t *>(buf);
  567. for(auto ip=ips.begin();ip!=ips.end();++ip) {
  568. if ((p + 6) > bufSize)
  569. break;
  570. const uint8_t *const ipd = reinterpret_cast<const uint8_t *>(ip->rawIpData());
  571. if (ip->isV4()) {
  572. b[p++] = AF_INET;
  573. b[p++] = ipd[0];
  574. b[p++] = ipd[1];
  575. b[p++] = ipd[2];
  576. b[p++] = ipd[3];
  577. b[p++] = (uint8_t)ip->netmaskBits();
  578. } else if (ip->isV6()) {
  579. if ((p + 18) <= bufSize) {
  580. b[p++] = AF_INET6;
  581. for(int j=0;j<16;++j)
  582. b[p++] = ipd[j];
  583. b[p++] = (uint8_t)ip->netmaskBits();
  584. }
  585. }
  586. }
  587. return (int)p;
  588. }
  589. extern "C" void ZT_GoTap_deviceName(ZT_GoTap *tap,char nbuf[256])
  590. {
  591. Utils::scopy(nbuf,256,reinterpret_cast<EthernetTap *>(tap)->deviceName().c_str());
  592. }
  593. extern "C" void ZT_GoTap_setFriendlyName(ZT_GoTap *tap,const char *friendlyName)
  594. {
  595. reinterpret_cast<EthernetTap *>(tap)->setFriendlyName(friendlyName);
  596. }
  597. extern "C" void ZT_GoTap_setMtu(ZT_GoTap *tap,unsigned int mtu)
  598. {
  599. reinterpret_cast<EthernetTap *>(tap)->setMtu(mtu);
  600. }