GoGlue.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 <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 <set>
  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. std::string ip;
  63. int port;
  64. int af;
  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/ztnode/node-callbacks.go */
  85. extern "C" int goPathCheckFunc(void *,uint64_t,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 *,unsigned int);
  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 int ZT_GoNode_StateGetFunction(
  146. ZT_Node *node,
  147. void *uptr,
  148. void *tptr,
  149. enum ZT_StateObjectType objType,
  150. const uint64_t id[2],
  151. void *buf,
  152. unsigned int buflen)
  153. {
  154. return goStateObjectGetFunc(
  155. reinterpret_cast<ZT_GoNode *>(uptr)->goUserPtr,
  156. (int)objType,
  157. id,
  158. buf,
  159. buflen);
  160. }
  161. 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)
  162. {
  163. switch(addr->ss_family) {
  164. case AF_INET:
  165. if ((ipTTL > 0)&&(ipTTL < 255)) {
  166. #ifdef __WINDOWS__
  167. DWORD tmp = (DWORD)ipTTL;
  168. #else
  169. int tmp = (int)ipTTL;
  170. #endif
  171. setsockopt(sock,IPPROTO_IP,IP_TTL,&tmp,sizeof(tmp));
  172. sendto(sock,data,len,MSG_DONTWAIT,(const sockaddr *)addr,sizeof(struct sockaddr_in));
  173. tmp = 255;
  174. setsockopt(sock,IPPROTO_IP,IP_TTL,&tmp,sizeof(tmp));
  175. } else {
  176. sendto(sock,data,len,MSG_DONTWAIT,(const sockaddr *)addr,sizeof(struct sockaddr_in));
  177. }
  178. break;
  179. case AF_INET6:
  180. // The ipTTL option isn't currently used with IPv6. It's only used
  181. // with IPv4 "firewall opener" / "NAT buster" preamble packets as part
  182. // of IPv4 NAT traversal.
  183. sendto(sock,data,len,MSG_DONTWAIT,(const sockaddr *)addr,sizeof(struct sockaddr_in6));
  184. break;
  185. }
  186. }
  187. static int ZT_GoNode_WirePacketSendFunction(
  188. ZT_Node *node,
  189. void *uptr,
  190. void *tptr,
  191. int64_t localSocket,
  192. const struct sockaddr_storage *addr,
  193. const void *data,
  194. unsigned int len,
  195. unsigned int ipTTL)
  196. {
  197. if ((localSocket != -1)&&(localSocket != ZT_INVALID_SOCKET)) {
  198. doUdpSend((ZT_SOCKET)localSocket,addr,data,len,ipTTL);
  199. } else {
  200. ZT_GoNode *const gn = reinterpret_cast<ZT_GoNode *>(uptr);
  201. std::set<std::string> ipsSentFrom;
  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) {
  205. if (ipsSentFrom.insert(t->second.ip).second) {
  206. doUdpSend(t->first,addr,data,len,ipTTL);
  207. }
  208. }
  209. }
  210. }
  211. return 0;
  212. }
  213. static int ZT_GoNode_PathCheckFunction(
  214. ZT_Node *node,
  215. void *uptr,
  216. void *tptr,
  217. uint64_t ztAddress,
  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. ztAddress,
  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. ztAddress,
  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. delete gn->node;
  344. delete gn;
  345. }
  346. extern "C" ZT_Node *ZT_GoNode_getNode(ZT_GoNode *gn)
  347. {
  348. return gn->node;
  349. }
  350. // Sets flags and socket options common to both IPv4 and IPv6 UDP sockets
  351. static void setCommonUdpSocketSettings(ZT_SOCKET udpSock,const char *dev)
  352. {
  353. int bufSize = 1048576;
  354. while (bufSize > 131072) {
  355. if (setsockopt(udpSock,SOL_SOCKET,SO_RCVBUF,(const char *)&bufSize,sizeof(bufSize)) == 0)
  356. break;
  357. bufSize -= 131072;
  358. }
  359. bufSize = 1048576;
  360. while (bufSize > 131072) {
  361. if (setsockopt(udpSock,SOL_SOCKET,SO_SNDBUF,(const char *)&bufSize,sizeof(bufSize)) == 0)
  362. break;
  363. bufSize -= 131072;
  364. }
  365. SETSOCKOPT_FLAG_TYPE fl;
  366. #ifdef SO_REUSEPORT
  367. fl = SETSOCKOPT_FLAG_TRUE;
  368. setsockopt(udpSock,SOL_SOCKET,SO_REUSEPORT,(void *)&fl,sizeof(fl));
  369. #endif
  370. #ifndef __LINUX__ // linux wants just SO_REUSEPORT
  371. fl = SETSOCKOPT_FLAG_TRUE;
  372. setsockopt(udpSock,SOL_SOCKET,SO_REUSEADDR,(void *)&fl,sizeof(fl));
  373. #endif
  374. fl = SETSOCKOPT_FLAG_TRUE;
  375. setsockopt(udpSock,SOL_SOCKET,SO_BROADCAST,(void *)&fl,sizeof(fl));
  376. #ifdef IP_DONTFRAG
  377. fl = SETSOCKOPT_FLAG_FALSE;
  378. setsockopt(udpSock,IPPROTO_IP,IP_DONTFRAG,(void *)&fl,sizeof(fl));
  379. #endif
  380. #ifdef IP_MTU_DISCOVER
  381. fl = SETSOCKOPT_FLAG_FALSE;
  382. setsockopt(udpSock,IPPROTO_IP,IP_MTU_DISCOVER,(void *)&fl,sizeof(fl));
  383. #endif
  384. #ifdef SO_BINDTODEVICE
  385. if ((dev)&&(strlen(dev)))
  386. setsockopt(udpSock,SOL_SOCKET,SO_BINDTODEVICE,dev,strlen(dev));
  387. #endif
  388. #if defined(__BSD__) && defined(IP_BOUND_IF)
  389. if ((dev)&&(strlen(dev))) {
  390. int idx = if_nametoindex(dev);
  391. if (idx != 0)
  392. setsockopt(udpSock,IPPROTO_IP,IP_BOUND_IF,(void *)&idx,sizeof(idx));
  393. }
  394. #endif
  395. }
  396. extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn,const char *dev,const char *ip,const int port)
  397. {
  398. if (strchr(ip,':')) {
  399. struct sockaddr_in6 in6;
  400. memset(&in6,0,sizeof(in6));
  401. in6.sin6_family = AF_INET6;
  402. if (inet_pton(AF_INET6,ip,&(in6.sin6_addr)) <= 0)
  403. return errno;
  404. in6.sin6_port = htons((uint16_t)port);
  405. ZT_SOCKET udpSock = socket(AF_INET6,SOCK_DGRAM,0);
  406. if (udpSock == ZT_INVALID_SOCKET)
  407. return errno;
  408. setCommonUdpSocketSettings(udpSock,dev);
  409. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  410. setsockopt(udpSock,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&fl,sizeof(fl));
  411. #ifdef IPV6_DONTFRAG
  412. fl = SETSOCKOPT_FLAG_FALSE;
  413. setsockopt(udpSock,IPPROTO_IPV6,IPV6_DONTFRAG,&fl,sizeof(fl));
  414. #endif
  415. if (bind(udpSock,reinterpret_cast<const struct sockaddr *>(&in6),sizeof(in6)) != 0)
  416. return errno;
  417. {
  418. std::lock_guard<std::mutex> l(gn->threads_l);
  419. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  420. gnt.ip = ip;
  421. gnt.port = port;
  422. gnt.af = AF_INET6;
  423. gnt.run = true;
  424. gnt.thr = std::thread([udpSock,gn,&gnt] {
  425. struct sockaddr_in6 in6;
  426. socklen_t salen;
  427. char buf[16384];
  428. while (gnt.run) {
  429. salen = sizeof(in6);
  430. int s = (int)recvfrom(udpSock,buf,sizeof(buf),0,reinterpret_cast<struct sockaddr *>(&in6),&salen);
  431. if (s > 0) {
  432. gn->node->processWirePacket(&gnt,OSUtils::now(),(int64_t)udpSock,reinterpret_cast<const struct sockaddr_storage *>(&in6),buf,(unsigned int)s,&(gn->nextBackgroundTaskDeadline));
  433. } else {
  434. // If something goes bad with this socket such as its interface vanishing, it
  435. // will eventually be closed by higher level (Go) code. Until then prevent the
  436. // system from consuming too much CPU.
  437. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  438. }
  439. }
  440. });
  441. }
  442. } else {
  443. struct sockaddr_in in;
  444. memset(&in,0,sizeof(in));
  445. in.sin_family = AF_INET;
  446. if (inet_pton(AF_INET,ip,&(in.sin_addr)) <= 0)
  447. return errno;
  448. in.sin_port = htons((uint16_t)port);
  449. ZT_SOCKET udpSock = socket(AF_INET,SOCK_DGRAM,0);
  450. if (udpSock == ZT_INVALID_SOCKET)
  451. return errno;
  452. setCommonUdpSocketSettings(udpSock,dev);
  453. #ifdef SO_NO_CHECK
  454. SETSOCKOPT_FLAG_TYPE fl = SETSOCKOPT_FLAG_TRUE;
  455. setsockopt(udpSock,SOL_SOCKET,SO_NO_CHECK,&fl,sizeof(fl));
  456. #endif
  457. if (bind(udpSock,reinterpret_cast<const struct sockaddr *>(&in),sizeof(in)) != 0)
  458. return errno;
  459. {
  460. std::lock_guard<std::mutex> l(gn->threads_l);
  461. ZT_GoNodeThread &gnt = gn->threads[udpSock];
  462. gnt.ip = ip;
  463. gnt.port = port;
  464. gnt.af = AF_INET6;
  465. gnt.run = true;
  466. gnt.thr = std::thread([udpSock,gn,&gnt] {
  467. struct sockaddr_in in4;
  468. socklen_t salen;
  469. char buf[16384];
  470. while (gnt.run) {
  471. salen = sizeof(in4);
  472. int s = (int)recvfrom(udpSock,buf,sizeof(buf),0,reinterpret_cast<struct sockaddr *>(&in4),&salen);
  473. if (s > 0) {
  474. gn->node->processWirePacket(&gnt,OSUtils::now(),(int64_t)udpSock,reinterpret_cast<const struct sockaddr_storage *>(&in4),buf,(unsigned int)s,&(gn->nextBackgroundTaskDeadline));
  475. }
  476. }
  477. });
  478. }
  479. }
  480. return 0;
  481. }
  482. extern "C" int ZT_GoNode_phyStopListen(ZT_GoNode *gn,const char *dev,const char *ip,const int port)
  483. {
  484. {
  485. std::lock_guard<std::mutex> l(gn->threads_l);
  486. for(auto t=gn->threads.begin();t!=gn->threads.end();) {
  487. if ((t->second.ip == ip)&&(t->second.port == port)) {
  488. t->second.run = false;
  489. shutdown(t->first,SHUT_RDWR);
  490. close(t->first);
  491. t->second.thr.join();
  492. gn->threads.erase(t++);
  493. } else ++t;
  494. }
  495. }
  496. return 0;
  497. }
  498. 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)
  499. {
  500. ZT_GoNode *const gn = reinterpret_cast<ZT_GoNode *>(uptr);
  501. gn->node->processVirtualNetworkFrame(tptr,OSUtils::now(),nwid,from.toInt(),to.toInt(),etherType,vlanId,data,len,&(gn->nextBackgroundTaskDeadline));
  502. }
  503. extern "C" ZT_GoTap *ZT_GoNode_join(ZT_GoNode *gn,uint64_t nwid)
  504. {
  505. try {
  506. std::lock_guard<std::mutex> l(gn->taps_l);
  507. auto existingTap = gn->taps.find(nwid);
  508. if (existingTap != gn->taps.end())
  509. return (ZT_GoTap *)existingTap->second.get();
  510. char tmp[256];
  511. OSUtils::ztsnprintf(tmp,sizeof(tmp),"ZeroTier Network %.16llx",(unsigned long long)nwid);
  512. 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));
  513. if (!tap)
  514. return nullptr;
  515. gn->taps[nwid] = tap;
  516. gn->node->join(nwid,tap.get(),nullptr);
  517. return (ZT_GoTap *)tap.get();
  518. } catch ( ... ) {
  519. return nullptr;
  520. }
  521. }
  522. extern "C" void ZT_GoNode_leave(ZT_GoNode *gn,uint64_t nwid)
  523. {
  524. std::lock_guard<std::mutex> l(gn->taps_l);
  525. auto existingTap = gn->taps.find(nwid);
  526. if (existingTap != gn->taps.end()) {
  527. gn->node->leave(nwid,nullptr,nullptr);
  528. gn->taps.erase(existingTap);
  529. }
  530. }
  531. /****************************************************************************/
  532. extern "C" void ZT_GoTap_setEnabled(ZT_GoTap *tap,int enabled)
  533. {
  534. reinterpret_cast<EthernetTap *>(tap)->setEnabled(enabled != 0);
  535. }
  536. extern "C" int ZT_GoTap_addIp(ZT_GoTap *tap,int af,const void *ip,int netmaskBits)
  537. {
  538. switch(af) {
  539. case AF_INET:
  540. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip,4,(unsigned int)netmaskBits)) ? 1 : 0);
  541. case AF_INET6:
  542. return (reinterpret_cast<EthernetTap *>(tap)->addIp(InetAddress(ip,16,(unsigned int)netmaskBits)) ? 1 : 0);
  543. }
  544. return 0;
  545. }
  546. extern "C" int ZT_GoTap_removeIp(ZT_GoTap *tap,int af,const void *ip,int netmaskBits)
  547. {
  548. switch(af) {
  549. case AF_INET:
  550. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip,4,(unsigned int)netmaskBits)) ? 1 : 0);
  551. case AF_INET6:
  552. return (reinterpret_cast<EthernetTap *>(tap)->removeIp(InetAddress(ip,16,(unsigned int)netmaskBits)) ? 1 : 0);
  553. }
  554. return 0;
  555. }
  556. extern "C" int ZT_GoTap_ips(ZT_GoTap *tap,void *buf,unsigned int bufSize)
  557. {
  558. auto ips = reinterpret_cast<EthernetTap *>(tap)->ips();
  559. unsigned int p = 0;
  560. uint8_t *const b = reinterpret_cast<uint8_t *>(buf);
  561. for(auto ip=ips.begin();ip!=ips.end();++ip) {
  562. if ((p + 6) > bufSize)
  563. break;
  564. const uint8_t *const ipd = reinterpret_cast<const uint8_t *>(ip->rawIpData());
  565. if (ip->isV4()) {
  566. b[p++] = AF_INET;
  567. b[p++] = ipd[0];
  568. b[p++] = ipd[1];
  569. b[p++] = ipd[2];
  570. b[p++] = ipd[3];
  571. b[p++] = (uint8_t)ip->netmaskBits();
  572. } else if (ip->isV6()) {
  573. if ((p + 18) <= bufSize) {
  574. b[p++] = AF_INET6;
  575. for(int j=0;j<16;++j)
  576. b[p++] = ipd[j];
  577. b[p++] = (uint8_t)ip->netmaskBits();
  578. }
  579. }
  580. }
  581. return (int)p;
  582. }
  583. extern "C" void ZT_GoTap_deviceName(ZT_GoTap *tap,char nbuf[256])
  584. {
  585. Utils::scopy(nbuf,256,reinterpret_cast<EthernetTap *>(tap)->deviceName().c_str());
  586. }
  587. extern "C" void ZT_GoTap_setFriendlyName(ZT_GoTap *tap,const char *friendlyName)
  588. {
  589. reinterpret_cast<EthernetTap *>(tap)->setFriendlyName(friendlyName);
  590. }
  591. extern "C" void ZT_GoTap_setMtu(ZT_GoTap *tap,unsigned int mtu)
  592. {
  593. reinterpret_cast<EthernetTap *>(tap)->setMtu(mtu);
  594. }
  595. extern "C" int ZT_GoTap_addRoute(ZT_GoTap *tap,int targetAf,const void *targetIp,int targetNetmaskBits,int viaAf,const void *viaIp,unsigned int metric)
  596. {
  597. InetAddress target,via;
  598. switch(targetAf) {
  599. case AF_INET:
  600. target.set(targetIp,4,(unsigned int)targetNetmaskBits);
  601. break;
  602. case AF_INET6:
  603. target.set(targetIp,16,(unsigned int)targetNetmaskBits);
  604. break;
  605. }
  606. switch(viaAf) {
  607. case AF_INET:
  608. via.set(viaIp,4,0);
  609. break;
  610. case AF_INET6:
  611. via.set(viaIp,16,0);
  612. break;
  613. }
  614. return reinterpret_cast<EthernetTap *>(tap)->addRoute(target,via,metric);
  615. }
  616. extern "C" int ZT_GoTap_removeRoute(ZT_GoTap *tap,int targetAf,const void *targetIp,int targetNetmaskBits,int viaAf,const void *viaIp,unsigned int metric)
  617. {
  618. InetAddress target,via;
  619. switch(targetAf) {
  620. case AF_INET:
  621. target.set(targetIp,4,(unsigned int)targetNetmaskBits);
  622. break;
  623. case AF_INET6:
  624. target.set(targetIp,16,(unsigned int)targetNetmaskBits);
  625. break;
  626. }
  627. switch(viaAf) {
  628. case AF_INET:
  629. via.set(viaIp,4,0);
  630. break;
  631. case AF_INET6:
  632. via.set(viaIp,16,0);
  633. break;
  634. }
  635. return reinterpret_cast<EthernetTap *>(tap)->removeRoute(target,via,metric);
  636. }