OSXEthernetTapFactory.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "OSXEthernetTapFactory.hpp"
  32. #include "OSXEthernetTap.hpp"
  33. namespace ZeroTier {
  34. OSXEthernetTapFactory::OSXEthernetTapFactory(const char *pathToTapKext,const char *tapKextName)
  35. _pathToTapKext((pathToTapKext) ? pathToTapKext : ""),
  36. _tapKextName((tapKextName) ? tapKextName : "")
  37. {
  38. struct stat stattmp;
  39. if ((_pathToTapKext.length())&&(_tapKextName.length())) {
  40. if (stat("/dev/zt0",&stattmp)) {
  41. long kextpid = (long)vfork();
  42. if (kextpid == 0) {
  43. ::chdir(_pathToTapKext.c_str());
  44. ::execl("/sbin/kextload","/sbin/kextload","-q","-repository",_pathToTapKext.c_str(),_tapKextName.c_str(),(const char *)0);
  45. ::_exit(-1);
  46. } else if (kextpid > 0) {
  47. int exitcode = -1;
  48. ::waitpid(kextpid,&exitcode,0);
  49. } else throw std::runtime_error("unable to create subprocess with fork()");
  50. }
  51. }
  52. if (stat("/dev/zt0",&stattmp)) {
  53. ::usleep(500); // give tap device driver time to start up and try again
  54. if (stat("/dev/zt0",&stattmp))
  55. throw std::runtime_error("/dev/zt# tap devices do not exist and unable to load kernel extension");
  56. }
  57. }
  58. OSXEthernetTapFactory::~OSXEthernetTapFactory()
  59. {
  60. Mutex::Lock _l(_devices_m);
  61. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d)
  62. delete *d;
  63. if ((_pathToTapKext.length())&&(_tapKextName.length())) {
  64. // Attempt to unload kext. If anything else is using a /dev/zt# node, this
  65. // fails and the kext stays in the kernel.
  66. char tmp[16384];
  67. sprintf(tmp,"%s/%s",_pathToTapKext.c_str(),_tapKextName.c_str());
  68. long kextpid = (long)vfork();
  69. if (kextpid == 0) {
  70. ::execl("/sbin/kextunload","/sbin/kextunload",tmp,(const char *)0);
  71. ::_exit(-1);
  72. } else if (kextpid > 0) {
  73. int exitcode = -1;
  74. ::waitpid(kextpid,&exitcode,0);
  75. }
  76. }
  77. }
  78. EthernetTap *OSXEthernetTapFactory::open(
  79. const MAC &mac,
  80. unsigned int mtu,
  81. unsigned int metric,
  82. uint64_t nwid,
  83. const char *desiredDevice,
  84. const char *friendlyName,
  85. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  86. void *arg)
  87. {
  88. Mutex::Lock _l(_devices_m);
  89. EthernetTap *t = new OSXEthernetTap(mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  90. _devices.push_back(t);
  91. return t;
  92. }
  93. void OSXEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  94. {
  95. {
  96. Mutex::Lock _l(_devices_m);
  97. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d) {
  98. if (*d == tap) {
  99. _devices.erase(d);
  100. break;
  101. }
  102. }
  103. }
  104. delete tap;
  105. }
  106. std::vector<std::string> allTapDeviceNames() const
  107. {
  108. std::vector<std::string> dn;
  109. Mutex::Lock _l(_devices_m);
  110. for(std::vector<EthernetTap *>::const_iterator d(_devices.begin());d!=_devices.end();++d)
  111. dn.push_back(d->deviceName());
  112. return dn;
  113. }
  114. } // namespace ZeroTier