EthernetTap.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <iostream>
  28. #include <string>
  29. #include "EthernetTap.hpp"
  30. #include "Logger.hpp"
  31. #include "RuntimeEnvironment.hpp"
  32. #include "Mutex.hpp"
  33. /* ======================================================================== */
  34. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  35. /* ======================================================================== */
  36. #include <stdint.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <signal.h>
  42. #include <fcntl.h>
  43. #include <errno.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/ioctl.h>
  47. #include <sys/wait.h>
  48. #include <netinet/in.h>
  49. #include <net/if_arp.h>
  50. #include <arpa/inet.h>
  51. #include <linux/if.h>
  52. #include <linux/if_tun.h>
  53. #include <linux/if_addr.h>
  54. #include <linux/if_ether.h>
  55. #define ZT_ETHERTAP_IP_COMMAND "/sbin/ip"
  56. #define ZT_ETHERTAP_SYSCTL_COMMAND "/sbin/sysctl"
  57. namespace ZeroTier {
  58. static Mutex __tapCreateLock;
  59. EthernetTap::EthernetTap(const RuntimeEnvironment *renv,const MAC &mac,unsigned int mtu)
  60. throw(std::runtime_error) :
  61. _mac(mac),
  62. _mtu(mtu),
  63. _r(renv),
  64. _putBuf((unsigned char *)0),
  65. _getBuf((unsigned char *)0),
  66. _fd(0),
  67. _isReading(false)
  68. {
  69. char procpath[128];
  70. Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
  71. _fd = ::open("/dev/net/tun",O_RDWR);
  72. if (_fd <= 0)
  73. throw std::runtime_error("could not open TUN/TAP device");
  74. struct ifreq ifr;
  75. memset(&ifr,0,sizeof(ifr));
  76. { // pick an unused device name
  77. int devno = 0;
  78. struct stat sbuf;
  79. do {
  80. sprintf(ifr.ifr_name,"zt%d",devno++);
  81. sprintf(procpath,"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name);
  82. } while (stat(procpath,&sbuf) == 0);
  83. }
  84. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  85. if (ioctl(_fd,TUNSETIFF,(void *)&ifr) < 0) {
  86. ::close(_fd);
  87. throw std::runtime_error("unable to configure TUN/TAP device for TAP operation");
  88. }
  89. strcpy(_dev,ifr.ifr_name);
  90. ioctl(_fd,TUNSETPERSIST,0); // valgrind may generate a false alarm here
  91. // Open an arbitrary socket to talk to netlink
  92. int sock = socket(AF_INET,SOCK_DGRAM,0);
  93. if (sock <= 0) {
  94. ::close(_fd);
  95. throw std::runtime_error("unable to open netlink socket");
  96. }
  97. // Set MAC address
  98. ifr.ifr_ifru.ifru_hwaddr.sa_family = ARPHRD_ETHER;
  99. memcpy(ifr.ifr_ifru.ifru_hwaddr.sa_data,mac.data,6);
  100. if (ioctl(sock,SIOCSIFHWADDR,(void *)&ifr) < 0) {
  101. ::close(_fd);
  102. ::close(sock);
  103. throw std::runtime_error("unable to configure TAP hardware (MAC) address");
  104. return;
  105. }
  106. // Set MTU
  107. ifr.ifr_ifru.ifru_mtu = (int)mtu;
  108. if (ioctl(sock,SIOCSIFMTU,(void *)&ifr) < 0) {
  109. ::close(_fd);
  110. ::close(sock);
  111. throw std::runtime_error("unable to configure TAP MTU");
  112. }
  113. if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
  114. ::close(_fd);
  115. throw std::runtime_error("unable to set flags on file descriptor for TAP device");
  116. }
  117. /* Bring interface up */
  118. if (ioctl(sock,SIOCGIFFLAGS,(void *)&ifr) < 0) {
  119. ::close(_fd);
  120. ::close(sock);
  121. throw std::runtime_error("unable to get TAP interface flags");
  122. }
  123. ifr.ifr_flags |= IFF_UP;
  124. if (ioctl(sock,SIOCSIFFLAGS,(void *)&ifr) < 0) {
  125. ::close(_fd);
  126. ::close(sock);
  127. throw std::runtime_error("unable to set TAP interface flags");
  128. }
  129. ::close(sock);
  130. _putBuf = new unsigned char[((mtu + 16) * 2)];
  131. _getBuf = _putBuf + (mtu + 16);
  132. TRACE("tap %s created",_dev);
  133. }
  134. EthernetTap::~EthernetTap()
  135. {
  136. this->close();
  137. delete [] _putBuf;
  138. }
  139. static bool ___removeIp(const char *_dev,std::set<InetAddress> &_ips,const InetAddress &ip)
  140. {
  141. long cpid;
  142. if ((cpid = (long)fork()) == 0) {
  143. execl(ZT_ETHERTAP_IP_COMMAND,ZT_ETHERTAP_IP_COMMAND,"addr","del",ip.toString().c_str(),"dev",_dev,(const char *)0);
  144. exit(1); /* not reached unless exec fails */
  145. } else {
  146. int exitcode = 1;
  147. waitpid(cpid,&exitcode,0);
  148. if (exitcode == 0) {
  149. _ips.erase(ip);
  150. return true;
  151. } else return false;
  152. }
  153. }
  154. bool EthernetTap::addIP(const InetAddress &ip)
  155. {
  156. Mutex::Lock _l(_ips_m);
  157. if (!ip)
  158. return false;
  159. if (_ips.count(ip) > 0)
  160. return true;
  161. // Remove and reconfigure if address is the same but netmask is different
  162. for(std::set<InetAddress>::iterator i(_ips.begin());i!=_ips.end();++i) {
  163. if (i->ipsEqual(ip)) {
  164. ___removeIp(_dev,_ips,*i);
  165. break;
  166. }
  167. }
  168. int cpid;
  169. if ((cpid = (int)fork()) == 0) {
  170. execl(ZT_ETHERTAP_IP_COMMAND,ZT_ETHERTAP_IP_COMMAND,"addr","add",ip.toString().c_str(),"dev",_dev,(const char *)0);
  171. exit(-1);
  172. } else {
  173. int exitcode = -1;
  174. waitpid(cpid,&exitcode,0);
  175. if (exitcode == 0) {
  176. _ips.insert(ip);
  177. return true;
  178. } else return false;
  179. }
  180. return false;
  181. }
  182. bool EthernetTap::removeIP(const InetAddress &ip)
  183. {
  184. Mutex::Lock _l(_ips_m);
  185. if (_ips.count(ip) > 0)
  186. return ___removeIp(_dev,_ips,ip);
  187. return false;
  188. }
  189. void EthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  190. {
  191. if ((_fd > 0)&&(len <= _mtu)) {
  192. for(int i=0;i<6;++i)
  193. _putBuf[i] = to.data[i];
  194. for(int i=0;i<6;++i)
  195. _putBuf[i+6] = from.data[i];
  196. *((uint16_t *)(_putBuf + 12)) = htons((uint16_t)etherType);
  197. memcpy(_putBuf + 14,data,len);
  198. ::write(_fd,_putBuf,len + 14);
  199. }
  200. }
  201. unsigned int EthernetTap::get(MAC &from,MAC &to,unsigned int &etherType,void *buf)
  202. {
  203. for(;;) {
  204. if (_fd > 0) {
  205. _isReading_m.lock();
  206. _isReading = true;
  207. _isReadingThreadId = pthread_self();
  208. _isReading_m.unlock();
  209. int n = (int)::read(_fd,_getBuf,_mtu + 14);
  210. _isReading_m.lock();
  211. _isReading = false;
  212. _isReading_m.unlock();
  213. if (n > 14) {
  214. for(int i=0;i<6;++i)
  215. to.data[i] = _getBuf[i];
  216. for(int i=0;i<6;++i)
  217. from.data[i] = _getBuf[i + 6];
  218. etherType = ntohs(((uint16_t *)_getBuf)[6]);
  219. n -= 14;
  220. memcpy(buf,_getBuf + 14,n);
  221. return (unsigned int)n;
  222. } else if (n < 0) {
  223. if (_fd <= 0)
  224. break;
  225. else if ((errno == EINTR)||(errno == ETIMEDOUT))
  226. continue;
  227. else {
  228. TRACE("unexpected error reading from tap: %s",strerror(errno));
  229. ::close(_fd);
  230. _fd = 0;
  231. break;
  232. }
  233. } else {
  234. TRACE("incomplete read from tap: %d bytes",n);
  235. continue;
  236. }
  237. }
  238. }
  239. return 0;
  240. }
  241. std::string EthernetTap::deviceName()
  242. {
  243. return std::string(_dev);
  244. }
  245. bool EthernetTap::open() const
  246. {
  247. return (_fd > 0);
  248. }
  249. void EthernetTap::close()
  250. {
  251. Mutex::Lock _l(__tapCreateLock); // also prevent create during close()
  252. if (_fd > 0) {
  253. int f = _fd;
  254. _fd = 0;
  255. ::close(f);
  256. _isReading_m.lock();
  257. if (_isReading)
  258. pthread_kill(_isReadingThreadId,SIGUSR2);
  259. _isReading_m.unlock();
  260. }
  261. }
  262. bool EthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  263. {
  264. char *ptr,*ptr2;
  265. unsigned char mac[6];
  266. std::set<MulticastGroup> newGroups;
  267. int fd = ::open("/proc/net/dev_mcast",O_RDONLY);
  268. if (fd > 0) {
  269. char buf[131072];
  270. int n = (int)::read(fd,buf,sizeof(buf));
  271. if ((n > 0)&&(n < (int)sizeof(buf))) {
  272. buf[n] = (char)0;
  273. for(char *l=strtok_r(buf,"\r\n",&ptr);(l);l=strtok_r((char *)0,"\r\n",&ptr)) {
  274. int fno = 0;
  275. char *devname = (char *)0;
  276. char *mcastmac = (char *)0;
  277. for(char *f=strtok_r(l," \t",&ptr2);(f);f=strtok_r((char *)0," \t",&ptr2)) {
  278. if (fno == 1)
  279. devname = f;
  280. else if (fno == 4)
  281. mcastmac = f;
  282. ++fno;
  283. }
  284. if ((devname)&&(!strcmp(devname,_dev))&&(mcastmac)&&(Utils::unhex(mcastmac,mac,6) == 6))
  285. newGroups.insert(MulticastGroup(MAC(mac),0));
  286. }
  287. }
  288. ::close(fd);
  289. }
  290. {
  291. Mutex::Lock _l(_ips_m);
  292. for(std::set<InetAddress>::const_iterator i(_ips.begin());i!=_ips.end();++i)
  293. newGroups.insert(MulticastGroup::deriveMulticastGroupForAddressResolution(*i));
  294. }
  295. bool changed = false;
  296. for(std::set<MulticastGroup>::iterator mg(newGroups.begin());mg!=newGroups.end();++mg) {
  297. if (!groups.count(*mg)) {
  298. groups.insert(*mg);
  299. changed = true;
  300. }
  301. }
  302. for(std::set<MulticastGroup>::iterator mg(groups.begin());mg!=groups.end();) {
  303. if (!newGroups.count(*mg)) {
  304. groups.erase(mg++);
  305. changed = true;
  306. } else ++mg;
  307. }
  308. return changed;
  309. }
  310. } // namespace ZeroTier
  311. /* ======================================================================== */
  312. #elif defined(__APPLE__) /* ----------------------------------------------- */
  313. /* ======================================================================== */
  314. #include <stdint.h>
  315. #include <stdio.h>
  316. #include <stdlib.h>
  317. #include <string.h>
  318. #include <unistd.h>
  319. #include <signal.h>
  320. #include <fcntl.h>
  321. #include <errno.h>
  322. #include <sys/types.h>
  323. #include <sys/stat.h>
  324. #include <sys/ioctl.h>
  325. #include <sys/uio.h>
  326. #include <sys/param.h>
  327. #include <sys/sysctl.h>
  328. #include <netinet/in.h>
  329. #include <arpa/inet.h>
  330. #include <net/route.h>
  331. #include <net/if_dl.h>
  332. #include <ifaddrs.h>
  333. #define ZT_ETHERTAP_IFCONFIG "/sbin/ifconfig"
  334. #define ZT_MAC_KEXTLOAD "/sbin/kextload"
  335. #define ZT_MAC_IPCONFIG "/usr/sbin/ipconfig"
  336. namespace ZeroTier {
  337. static Mutex __tapCreateLock;
  338. EthernetTap::EthernetTap(const RuntimeEnvironment *renv,const MAC &mac,unsigned int mtu)
  339. throw(std::runtime_error) :
  340. _mac(mac),
  341. _mtu(mtu),
  342. _r(renv),
  343. _putBuf((unsigned char *)0),
  344. _getBuf((unsigned char *)0),
  345. _fd(0),
  346. _isReading(false)
  347. {
  348. char devpath[64],ethaddr[64],mtustr[16];
  349. struct stat tmp;
  350. Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
  351. // Check for existence of ZT tap devices, try to load module if not there
  352. if (stat("/dev/zt0",&tmp)) {
  353. int kextpid;
  354. char tmp[4096];
  355. strcpy(tmp,_r->homePath.c_str());
  356. if ((kextpid = (int)fork()) == 0) {
  357. chdir(tmp);
  358. execl(ZT_MAC_KEXTLOAD,ZT_MAC_KEXTLOAD,"-q","-repository",tmp,"tap.kext",(const char *)0);
  359. exit(-1);
  360. } else {
  361. int exitcode = -1;
  362. waitpid(kextpid,&exitcode,0);
  363. usleep(500);
  364. }
  365. }
  366. if (stat("/dev/zt0",&tmp))
  367. throw std::runtime_error("/dev/zt# tap devices do not exist and unable to load kernel extension");
  368. // Open the first available device (ones in use will fail with resource busy)
  369. for(int i=0;i<256;++i) {
  370. sprintf(devpath,"/dev/zt%d",i);
  371. if (stat(devpath,&tmp))
  372. throw std::runtime_error("no more TAP devices available");
  373. _fd = ::open(devpath,O_RDWR);
  374. if (_fd > 0) {
  375. sprintf(_dev,"zt%d",i);
  376. break;
  377. }
  378. }
  379. if (_fd <= 0)
  380. throw std::runtime_error("unable to open TAP device or no more devices available");
  381. if (fcntl(_fd,F_SETFL,fcntl(_fd,F_GETFL) & ~O_NONBLOCK) == -1) {
  382. ::close(_fd);
  383. throw std::runtime_error("unable to set flags on file descriptor for TAP device");
  384. }
  385. sprintf(ethaddr,"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]);
  386. sprintf(mtustr,"%u",mtu);
  387. // Configure MAC address and MTU, bring interface up
  388. int cpid;
  389. if ((cpid = (int)fork()) == 0) {
  390. execl(ZT_ETHERTAP_IFCONFIG,ZT_ETHERTAP_IFCONFIG,_dev,"lladdr",ethaddr,"mtu",mtustr,"up",(const char *)0);
  391. exit(-1);
  392. } else {
  393. int exitcode = -1;
  394. waitpid(cpid,&exitcode,0);
  395. if (exitcode) {
  396. ::close(_fd);
  397. throw std::runtime_error("ifconfig failure setting link-layer address and activating tap interface");
  398. }
  399. }
  400. // OSX seems to require that IPv6 be turned on on tap devices
  401. if ((cpid = (int)fork()) == 0) {
  402. execl(ZT_MAC_IPCONFIG,ZT_MAC_IPCONFIG,"set",_dev,"AUTOMATIC-V6",(const char *)0);
  403. exit(-1);
  404. } else {
  405. int exitcode = -1;
  406. waitpid(cpid,&exitcode,0);
  407. if (exitcode) {
  408. ::close(_fd);
  409. throw std::runtime_error("ifconfig failure setting link-layer address and activating tap interface");
  410. }
  411. }
  412. _putBuf = new unsigned char[((mtu + 14) * 2)];
  413. _getBuf = _putBuf + (mtu + 14);
  414. }
  415. EthernetTap::~EthernetTap()
  416. {
  417. this->close();
  418. delete [] _putBuf;
  419. }
  420. // Helper function to actually remove IP from network device, execs ifconfig
  421. static bool ___removeIp(const char *_dev,const InetAddress &ip)
  422. {
  423. int cpid;
  424. if ((cpid = (int)fork()) == 0) {
  425. execl(ZT_ETHERTAP_IFCONFIG,ZT_ETHERTAP_IFCONFIG,_dev,"inet",ip.toIpString().c_str(),"-alias",(const char *)0);
  426. exit(-1);
  427. } else {
  428. int exitcode = -1;
  429. waitpid(cpid,&exitcode,0);
  430. return (exitcode == 0);
  431. }
  432. return false; // never reached, make compiler shut up about return value
  433. }
  434. bool EthernetTap::addIP(const InetAddress &ip)
  435. {
  436. Mutex::Lock _l(_ips_m);
  437. if (!ip)
  438. return false;
  439. if (_ips.count(ip) > 0)
  440. return true; // IP/netmask already assigned
  441. // Remove and reconfigure if address is the same but netmask is different
  442. for(std::set<InetAddress>::iterator i(_ips.begin());i!=_ips.end();++i) {
  443. if ((i->ipsEqual(ip))&&(i->netmaskBits() != ip.netmaskBits())) {
  444. if (___removeIp(_dev,*i)) {
  445. _ips.erase(i);
  446. break;
  447. } else {
  448. LOG("WARNING: failed to remove old IP/netmask %s to replace with %s",i->toString().c_str(),ip.toString().c_str());
  449. }
  450. }
  451. }
  452. int cpid;
  453. if ((cpid = (int)fork()) == 0) {
  454. execl(ZT_ETHERTAP_IFCONFIG,ZT_ETHERTAP_IFCONFIG,_dev,ip.isV4() ? "inet" : "inet6",ip.toString().c_str(),"alias",(const char *)0);
  455. exit(-1);
  456. } else {
  457. int exitcode = -1;
  458. waitpid(cpid,&exitcode,0);
  459. if (exitcode == 0) {
  460. _ips.insert(ip);
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. bool EthernetTap::removeIP(const InetAddress &ip)
  467. {
  468. Mutex::Lock _l(_ips_m);
  469. if (_ips.count(ip) > 0) {
  470. if (___removeIp(_dev,ip)) {
  471. _ips.erase(ip);
  472. return true;
  473. }
  474. }
  475. return false;
  476. }
  477. void EthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  478. {
  479. if ((_fd > 0)&&(len <= _mtu)) {
  480. for(int i=0;i<6;++i)
  481. _putBuf[i] = to.data[i];
  482. for(int i=0;i<6;++i)
  483. _putBuf[i+6] = from.data[i];
  484. *((uint16_t *)(_putBuf + 12)) = htons((uint16_t)etherType);
  485. memcpy(_putBuf + 14,data,len);
  486. len += 14;
  487. int n = (int)::write(_fd,_putBuf,len);
  488. if (n <= 0) {
  489. LOG("error writing packet to Ethernet tap device: %s",strerror(errno));
  490. } else if (n != (int)len) {
  491. // Saw this gremlin once, so log it if we see it again... OSX tap
  492. // or something seems to have goofy issues with certain MTUs.
  493. LOG("WARNING: Apple gremlin: tap write() wrote %d of %u bytes of frame",n,len);
  494. }
  495. }
  496. }
  497. unsigned int EthernetTap::get(MAC &from,MAC &to,unsigned int &etherType,void *buf)
  498. {
  499. for(;;) {
  500. if (_fd > 0) {
  501. _isReading_m.lock();
  502. _isReading = true;
  503. _isReadingThreadId = pthread_self();
  504. _isReading_m.unlock();
  505. int n = (int)::read(_fd,_getBuf,_mtu + 14);
  506. _isReading_m.lock();
  507. _isReading = false;
  508. _isReading_m.unlock();
  509. if (n > 14) {
  510. for(int i=0;i<6;++i)
  511. to.data[i] = _getBuf[i];
  512. for(int i=0;i<6;++i)
  513. from.data[i] = _getBuf[i + 6];
  514. etherType = ntohs(((uint16_t *)_getBuf)[6]);
  515. n -= 14;
  516. memcpy(buf,_getBuf + 14,n);
  517. return (unsigned int)n;
  518. } else if (n < 0) {
  519. if (_fd <= 0)
  520. break;
  521. else if ((errno == EINTR)||(errno == ETIMEDOUT))
  522. continue;
  523. else {
  524. TRACE("unexpected error reading from tap: %s",strerror(errno));
  525. ::close(_fd);
  526. _fd = 0;
  527. break;
  528. }
  529. } else {
  530. TRACE("incomplete read from tap: %d bytes",n);
  531. continue;
  532. }
  533. }
  534. }
  535. return 0;
  536. }
  537. std::string EthernetTap::deviceName()
  538. {
  539. return std::string(_dev);
  540. }
  541. bool EthernetTap::open() const
  542. {
  543. return (_fd > 0);
  544. }
  545. void EthernetTap::close()
  546. {
  547. Mutex::Lock _l(__tapCreateLock); // also prevent create during close()
  548. if (_fd > 0) {
  549. int f = _fd;
  550. _fd = 0;
  551. ::close(f);
  552. _isReading_m.lock();
  553. if (_isReading)
  554. pthread_kill(_isReadingThreadId,SIGUSR2);
  555. _isReading_m.unlock();
  556. }
  557. }
  558. bool EthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  559. {
  560. std::set<MulticastGroup> newGroups;
  561. struct ifmaddrs *ifmap = (struct ifmaddrs *)0;
  562. if (!getifmaddrs(&ifmap)) {
  563. struct ifmaddrs *p = ifmap;
  564. while (p) {
  565. if (p->ifma_addr->sa_family == AF_LINK) {
  566. struct sockaddr_dl *in = (struct sockaddr_dl *)p->ifma_name;
  567. struct sockaddr_dl *la = (struct sockaddr_dl *)p->ifma_addr;
  568. if ((la->sdl_alen == 6)&&(in->sdl_nlen <= sizeof(_dev))&&(!memcmp(_dev,in->sdl_data,in->sdl_nlen)))
  569. newGroups.insert(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen),0));
  570. }
  571. p = p->ifma_next;
  572. }
  573. freeifmaddrs(ifmap);
  574. }
  575. {
  576. Mutex::Lock _l(_ips_m);
  577. for(std::set<InetAddress>::const_iterator i(_ips.begin());i!=_ips.end();++i)
  578. newGroups.insert(MulticastGroup::deriveMulticastGroupForAddressResolution(*i));
  579. }
  580. bool changed = false;
  581. for(std::set<MulticastGroup>::iterator mg(newGroups.begin());mg!=newGroups.end();++mg) {
  582. if (!groups.count(*mg)) {
  583. groups.insert(*mg);
  584. changed = true;
  585. }
  586. }
  587. for(std::set<MulticastGroup>::iterator mg(groups.begin());mg!=groups.end();) {
  588. if (!newGroups.count(*mg)) {
  589. groups.erase(mg++);
  590. changed = true;
  591. } else ++mg;
  592. }
  593. return changed;
  594. }
  595. } // namespace ZeroTier
  596. /* ======================================================================== */
  597. #elif defined(_WIN32) /* -------------------------------------------------- */
  598. /* ======================================================================== */
  599. /* ======================================================================== */
  600. #endif
  601. /* ======================================================================== */