GoGlue.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 <cstring>
  15. #include <cstdlib>
  16. #include <cerrno>
  17. #include "../../node/Constants.hpp"
  18. #include "../../node/InetAddress.hpp"
  19. #include "../../node/Node.hpp"
  20. #include "../../node/Utils.hpp"
  21. #include "../../node/MAC.hpp"
  22. #include "../../node/Address.hpp"
  23. #include "../../osdep/OSUtils.hpp"
  24. #include "../../osdep/EthernetTap.hpp"
  25. #ifndef __WINDOWS__
  26. #include <unistd.h>
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #include <arpa/inet.h>
  30. #include <netinet/in.h>
  31. #ifdef __BSD__
  32. #include <net/if.h>
  33. #endif
  34. #ifdef __LINUX__
  35. #ifndef IPV6_DONTFRAG
  36. #define IPV6_DONTFRAG 62
  37. #endif
  38. #endif
  39. #endif // !__WINDOWS__
  40. #include <thread>
  41. #include <mutex>
  42. #include <map>
  43. #include <vector>
  44. #include <memory>
  45. #include <atomic>
  46. #ifdef __WINDOWS__
  47. #define SETSOCKOPT_FLAG_TYPE BOOL
  48. #define SETSOCKOPT_FLAG_TRUE TRUE
  49. #define SETSOCKOPT_FLAG_FALSE FALSE
  50. #else
  51. #define SETSOCKOPT_FLAG_TYPE int
  52. #define SETSOCKOPT_FLAG_TRUE 1
  53. #define SETSOCKOPT_FLAG_FALSE 0
  54. #endif
  55. #ifndef MSG_DONTWAIT
  56. #define MSG_DONTWAIT 0
  57. #endif
  58. using namespace ZeroTier;
  59. struct ZT_GoNodeThread
  60. {
  61. std::string ip;
  62. int port;
  63. int af;
  64. bool primary;
  65. std::atomic<bool> run;
  66. std::thread thr;
  67. };
  68. struct ZT_GoNode_Impl
  69. {
  70. void *goUserPtr;
  71. Node *node;
  72. volatile int64_t nextBackgroundTaskDeadline;
  73. std::string path;
  74. std::atomic<bool> run;
  75. std::map< ZT_SOCKET,ZT_GoNodeThread > threads;
  76. std::mutex threads_l;
  77. std::map< uint64_t,std::shared_ptr<EthernetTap> > taps;
  78. std::mutex taps_l;
  79. std::thread backgroundTaskThread;
  80. };
  81. static const std::string defaultHomePath(OSUtils::platformDefaultHomePath());
  82. const char *ZT_PLATFORM_DEFAULT_HOMEPATH = defaultHomePath.c_str();
  83. /****************************************************************************/
  84. /* These functions are implemented in Go in pkg/zerotier/node.go */
  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_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 > 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. /****************************************************************************/
  277. extern "C" ZT_GoNode *ZT_GoNode_new(const char *workingPath,uintptr_t userPtr)
  278. {
  279. try {
  280. struct ZT_Node_Callbacks cb;
  281. cb.statePutFunction = &ZT_GoNode_StatePutFunction;
  282. cb.stateGetFunction = &ZT_GoNode_StateGetFunction;
  283. cb.wirePacketSendFunction = &ZT_GoNode_WirePacketSendFunction;
  284. cb.virtualNetworkFrameFunction = &ZT_GoNode_VirtualNetworkFrameFunction;
  285. cb.virtualNetworkConfigFunction = &ZT_GoNode_VirtualNetworkConfigFunction;
  286. cb.eventCallback = &ZT_GoNode_EventCallback;
  287. cb.pathCheckFunction = &ZT_GoNode_PathCheckFunction;
  288. cb.pathLookupFunction = &ZT_GoNode_PathLookupFunction;
  289. ZT_GoNode_Impl *gn = new ZT_GoNode_Impl;
  290. const int64_t now = OSUtils::now();
  291. gn->goUserPtr = reinterpret_cast<void *>(userPtr);
  292. gn->node = new Node(reinterpret_cast<void *>(gn),nullptr,&cb,now);
  293. gn->nextBackgroundTaskDeadline = now;
  294. gn->path = workingPath;
  295. gn->run = true;
  296. gn->backgroundTaskThread = std::thread([gn] {
  297. int64_t lastCheckedTaps = 0;
  298. while (gn->run) {
  299. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  300. const int64_t now = OSUtils::now();
  301. if (now >= gn->nextBackgroundTaskDeadline)
  302. gn->node->processBackgroundTasks(nullptr,now,&(gn->nextBackgroundTaskDeadline));
  303. if ((now - lastCheckedTaps) > 10000) {
  304. lastCheckedTaps = now;
  305. std::vector<MulticastGroup> added,removed;
  306. std::lock_guard<std::mutex> tl(gn->taps_l);
  307. for(auto t=gn->taps.begin();t!=gn->taps.end();++t) {
  308. added.clear();
  309. removed.clear();
  310. t->second->scanMulticastGroups(added,removed);
  311. for(auto g=added.begin();g!=added.end();++g)
  312. goHandleTapAddedMulticastGroup(gn,(ZT_GoTap *)t->second.get(),t->first,g->mac().toInt(),g->adi());
  313. for(auto g=removed.begin();g!=removed.end();++g)
  314. goHandleTapRemovedMulticastGroup(gn,(ZT_GoTap *)t->second.get(),t->first,g->mac().toInt(),g->adi());
  315. t->second->syncRoutes();
  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. shutdown(t->first,SHUT_RDWR);
  333. close(t->first);
  334. t->second.thr.join();
  335. }
  336. gn->threads_l.unlock();
  337. gn->taps_l.lock();
  338. for(auto t=gn->taps.begin();t!=gn->taps.end();++t)
  339. gn->node->leave(t->first,nullptr,nullptr);
  340. gn->taps.clear();
  341. gn->taps_l.unlock();
  342. gn->backgroundTaskThread.join();
  343. gn->node->shutdown(nullptr);
  344. delete gn->node;
  345. delete gn;
  346. }
  347. extern "C" ZT_Node *ZT_GoNode_getNode(ZT_GoNode *gn)
  348. {
  349. return gn->node;
  350. }
  351. // Sets flags and socket options common to both IPv4 and IPv6 UDP sockets
  352. static void setCommonUdpSocketSettings(ZT_SOCKET udpSock,const char *dev)
  353. {
  354. int bufSize = 1048576;
  355. while (bufSize > 131072) {
  356. if (setsockopt(udpSock,SOL_SOCKET,SO_RCVBUF,(const char *)&bufSize,sizeof(bufSize)) == 0)
  357. break;
  358. bufSize -= 131072;
  359. }
  360. bufSize = 1048576;
  361. while (bufSize > 131072) {
  362. if (setsockopt(udpSock,SOL_SOCKET,SO_SNDBUF,(const char *)&bufSize,sizeof(bufSize)) == 0)
  363. break;
  364. bufSize -= 131072;
  365. }
  366. SETSOCKOPT_FLAG_TYPE fl;
  367. #ifdef SO_REUSEPORT
  368. fl = SETSOCKOPT_FLAG_TRUE;
  369. setsockopt(udpSock,SOL_SOCKET,SO_REUSEPORT,(void *)&fl,sizeof(fl));
  370. #endif
  371. #ifndef __LINUX__ // linux wants just SO_REUSEPORT
  372. fl = SETSOCKOPT_FLAG_TRUE;
  373. setsockopt(udpSock,SOL_SOCKET,SO_REUSEADDR,(void *)&fl,sizeof(fl));
  374. #endif
  375. fl = SETSOCKOPT_FLAG_TRUE;
  376. setsockopt(udpSock,SOL_SOCKET,SO_BROADCAST,(void *)&fl,sizeof(fl));
  377. #ifdef IP_DONTFRAG
  378. fl = SETSOCKOPT_FLAG_FALSE;
  379. setsockopt(udpSock,IPPROTO_IP,IP_DONTFRAG,(void *)&fl,sizeof(fl));
  380. #endif
  381. #ifdef IP_MTU_DISCOVER
  382. fl = SETSOCKOPT_FLAG_FALSE;
  383. setsockopt(udpSock,IPPROTO_IP,IP_MTU_DISCOVER,(void *)&fl,sizeof(fl));
  384. #endif
  385. #ifdef SO_BINDTODEVICE
  386. if ((dev)&&(strlen(dev)))
  387. setsockopt(udpSock,SOL_SOCKET,SO_BINDTODEVICE,dev,strlen(dev));
  388. #endif
  389. #if defined(__BSD__) && defined(IP_BOUND_IF)
  390. if ((dev)&&(strlen(dev))) {
  391. int idx = if_nametoindex(dev);
  392. if (idx != 0)
  393. setsockopt(udpSock,IPPROTO_IP,IP_BOUND_IF,(void *)&idx,sizeof(idx));
  394. }
  395. #endif
  396. }
  397. extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn,const char *dev,const char *ip,const int port,const int primary)
  398. {
  399. if (strchr(ip,':')) {
  400. struct sockaddr_in6 in6;
  401. memset(&in6,0,sizeof(in6));
  402. in6.sin6_family = AF_INET6;
  403. if (inet_pton(AF_INET6,ip,&(in6.sin6_addr)) <= 0)
  404. return errno;
  405. in6.sin6_port = htons((uint16_t)port);
  406. ZT_SOCKET udpSock = socket(AF_INET6,SOCK_DGRAM,0);
  407. if (udpSock == ZT_INVALID_SOCKET)
  408. return errno;
  409. setCommonUdpSocketSettings(udpSock,dev);
  410. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  411. setsockopt(udpSock,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&fl,sizeof(fl));
  412. #ifdef IPV6_DONTFRAG
  413. fl = SETSOCKOPT_FLAG_FALSE;
  414. setsockopt(udpSock,IPPROTO_IPV6,IPV6_DONTFRAG,&fl,sizeof(fl));
  415. #endif
  416. if (bind(udpSock,reinterpret_cast<const struct sockaddr *>(&in6),sizeof(in6)) != 0)
  417. return errno;
  418. {
  419. std::lock_guard<std::mutex> l(gn->threads_l);
  420. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  421. gnt.ip = ip;
  422. gnt.port = port;
  423. gnt.af = AF_INET6;
  424. gnt.primary = (primary != 0);
  425. gnt.run = true;
  426. gnt.thr = std::thread([udpSock,gn,&gnt] {
  427. struct sockaddr_in6 in6;
  428. socklen_t salen;
  429. char buf[16384];
  430. while (gnt.run) {
  431. salen = sizeof(in6);
  432. int s = (int)recvfrom(udpSock,buf,sizeof(buf),0,reinterpret_cast<struct sockaddr *>(&in6),&salen);
  433. if (s > 0) {
  434. gn->node->processWirePacket(&gnt,OSUtils::now(),(int64_t)udpSock,reinterpret_cast<const struct sockaddr_storage *>(&in6),buf,(unsigned int)s,&(gn->nextBackgroundTaskDeadline));
  435. } else {
  436. // If something goes bad with this socket such as its interface vanishing, it
  437. // will eventually be closed by higher level (Go) code. Until then prevent the
  438. // system from consuming too much CPU.
  439. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  440. }
  441. }
  442. });
  443. }
  444. } else {
  445. struct sockaddr_in in;
  446. memset(&in,0,sizeof(in));
  447. in.sin_family = AF_INET;
  448. if (inet_pton(AF_INET,ip,&(in.sin_addr)) <= 0)
  449. return errno;
  450. in.sin_port = htons((uint16_t)port);
  451. ZT_SOCKET udpSock = socket(AF_INET,SOCK_DGRAM,0);
  452. if (udpSock == ZT_INVALID_SOCKET)
  453. return errno;
  454. setCommonUdpSocketSettings(udpSock,dev);
  455. #ifdef SO_NO_CHECK
  456. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  457. setsockopt(udpSock,SOL_SOCKET,SO_NO_CHECK,&fl,sizeof(fl));
  458. #endif
  459. if (bind(udpSock,reinterpret_cast<const struct sockaddr *>(&in),sizeof(in)) != 0)
  460. return errno;
  461. {
  462. std::lock_guard<std::mutex> l(gn->threads_l);
  463. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  464. gnt.ip = ip;
  465. gnt.port = port;
  466. gnt.af = AF_INET6;
  467. gnt.primary = (primary != 0);
  468. gnt.run = true;
  469. gnt.thr = std::thread([udpSock,gn,&gnt] {
  470. struct sockaddr_in in4;
  471. socklen_t salen;
  472. char buf[16384];
  473. while (gnt.run) {
  474. salen = sizeof(in4);
  475. int s = (int)recvfrom(udpSock,buf,sizeof(buf),0,reinterpret_cast<struct sockaddr *>(&in4),&salen);
  476. if (s > 0) {
  477. gn->node->processWirePacket(&gnt,OSUtils::now(),(int64_t)udpSock,reinterpret_cast<const struct sockaddr_storage *>(&in4),buf,(unsigned int)s,&(gn->nextBackgroundTaskDeadline));
  478. }
  479. }
  480. });
  481. }
  482. }
  483. return 0;
  484. }
  485. extern "C" int ZT_GoNode_phyStopListen(ZT_GoNode *gn,const char *dev,const char *ip,const int port)
  486. {
  487. {
  488. std::lock_guard<std::mutex> l(gn->threads_l);
  489. for(auto t=gn->threads.begin();t!=gn->threads.end();) {
  490. if ((t->second.ip == ip)&&(t->second.port == port)) {
  491. t->second.run = false;
  492. shutdown(t->first,SHUT_RDWR);
  493. close(t->first);
  494. t->second.thr.join();
  495. gn->threads.erase(t++);
  496. } else ++t;
  497. }
  498. }
  499. return 0;
  500. }
  501. 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)
  502. {
  503. ZT_GoNode *const gn = reinterpret_cast<ZT_GoNode *>(uptr);
  504. gn->node->processVirtualNetworkFrame(tptr,OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&(gn->nextBackgroundTaskDeadline));
  505. }
  506. extern "C" ZT_GoTap *ZT_GoNode_join(ZT_GoNode *gn,uint64_t nwid)
  507. {
  508. try {
  509. std::lock_guard<std::mutex> l(gn->taps_l);
  510. auto existingTap = gn->taps.find(nwid);
  511. if (existingTap != gn->taps.end())
  512. return (ZT_GoTap *)existingTap->second.get();
  513. char tmp[256];
  514. OSUtils::ztsnprintf(tmp,sizeof(tmp),"ZeroTier Network %.16llx",(unsigned long long)nwid);
  515. 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));
  516. if (!tap)
  517. return nullptr;
  518. gn->taps[nwid] = tap;
  519. gn->node->join(nwid,tap.get(),nullptr);
  520. return (ZT_GoTap *)tap.get();
  521. } catch ( ... ) {
  522. return nullptr;
  523. }
  524. }
  525. extern "C" void ZT_GoNode_leave(ZT_GoNode *gn,uint64_t nwid)
  526. {
  527. std::lock_guard<std::mutex> l(gn->taps_l);
  528. auto existingTap = gn->taps.find(nwid);
  529. if (existingTap != gn->taps.end()) {
  530. gn->node->leave(nwid,nullptr,nullptr);
  531. gn->taps.erase(existingTap);
  532. }
  533. }
  534. /****************************************************************************/
  535. extern "C" void ZT_GoTap_setEnabled(ZT_GoTap *tap,int enabled)
  536. {
  537. reinterpret_cast<EthernetTap *>(tap)->setEnabled(enabled != 0);
  538. }
  539. extern "C" int ZT_GoTap_addIp(ZT_GoTap *tap,int af,const void *ip,int netmaskBits)
  540. {
  541. switch(af) {
  542. case AF_INET:
  543. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip,4,(unsigned int)netmaskBits)) ? 1 : 0);
  544. case AF_INET6:
  545. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip,16,(unsigned int)netmaskBits)) ? 1 : 0);
  546. }
  547. return 0;
  548. }
  549. extern "C" int ZT_GoTap_removeIp(ZT_GoTap *tap,int af,const void *ip,int netmaskBits)
  550. {
  551. switch(af) {
  552. case AF_INET:
  553. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip,4,(unsigned int)netmaskBits)) ? 1 : 0);
  554. case AF_INET6:
  555. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip,16,(unsigned int)netmaskBits)) ? 1 : 0);
  556. }
  557. return 0;
  558. }
  559. extern "C" int ZT_GoTap_ips(ZT_GoTap *tap,void *buf,unsigned int bufSize)
  560. {
  561. auto ips = reinterpret_cast<EthernetTap *>(tap)->ips();
  562. unsigned int p = 0;
  563. uint8_t *const b = reinterpret_cast<uint8_t *>(buf);
  564. for(auto ip=ips.begin();ip!=ips.end();++ip) {
  565. if ((p + 6) > bufSize)
  566. break;
  567. const uint8_t *const ipd = reinterpret_cast<const uint8_t *>(ip->rawIpData());
  568. if (ip->isV4()) {
  569. b[p++] = AF_INET;
  570. b[p++] = ipd[0];
  571. b[p++] = ipd[1];
  572. b[p++] = ipd[2];
  573. b[p++] = ipd[3];
  574. b[p++] = (uint8_t)ip->netmaskBits();
  575. } else if (ip->isV6()) {
  576. if ((p + 18) <= bufSize) {
  577. b[p++] = AF_INET6;
  578. for(int j=0;j<16;++j)
  579. b[p++] = ipd[j];
  580. b[p++] = (uint8_t)ip->netmaskBits();
  581. }
  582. }
  583. }
  584. return (int)p;
  585. }
  586. extern "C" void ZT_GoTap_deviceName(ZT_GoTap *tap,char nbuf[256])
  587. {
  588. Utils::scopy(nbuf,256,reinterpret_cast<EthernetTap *>(tap)->deviceName().c_str());
  589. }
  590. extern "C" void ZT_GoTap_setFriendlyName(ZT_GoTap *tap,const char *friendlyName)
  591. {
  592. reinterpret_cast<EthernetTap *>(tap)->setFriendlyName(friendlyName);
  593. }
  594. extern "C" void ZT_GoTap_setMtu(ZT_GoTap *tap,unsigned int mtu)
  595. {
  596. reinterpret_cast<EthernetTap *>(tap)->setMtu(mtu);
  597. }
  598. extern "C" int ZT_GoTap_addRoute(ZT_GoTap *tap,int targetAf,const void *targetIp,int targetNetmaskBits,int viaAf,const void *viaIp,unsigned int metric)
  599. {
  600. InetAddress target,via;
  601. switch(targetAf) {
  602. case AF_INET:
  603. target.set(targetIp,4,(unsigned int)targetNetmaskBits);
  604. break;
  605. case AF_INET6:
  606. target.set(targetIp,16,(unsigned int)targetNetmaskBits);
  607. break;
  608. }
  609. switch(viaAf) {
  610. case AF_INET:
  611. via.set(viaIp,4,0);
  612. break;
  613. case AF_INET6:
  614. via.set(viaIp,16,0);
  615. break;
  616. }
  617. return reinterpret_cast<EthernetTap *>(tap)->addRoute(target,via,metric);
  618. }
  619. extern "C" int ZT_GoTap_removeRoute(ZT_GoTap *tap,int targetAf,const void *targetIp,int targetNetmaskBits,int viaAf,const void *viaIp,unsigned int metric)
  620. {
  621. InetAddress target,via;
  622. switch(targetAf) {
  623. case AF_INET:
  624. target.set(targetIp,4,(unsigned int)targetNetmaskBits);
  625. break;
  626. case AF_INET6:
  627. target.set(targetIp,16,(unsigned int)targetNetmaskBits);
  628. break;
  629. }
  630. switch(viaAf) {
  631. case AF_INET:
  632. via.set(viaIp,4,0);
  633. break;
  634. case AF_INET6:
  635. via.set(viaIp,16,0);
  636. break;
  637. }
  638. return reinterpret_cast<EthernetTap *>(tap)->removeRoute(target,via,metric);
  639. }