BSDEthernetTapFactory.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <sys/stat.h>
  32. #include "BSDEthernetTapFactory.hpp"
  33. #include "BSDEthernetTap.hpp"
  34. #include "../node/Utils.hpp"
  35. namespace ZeroTier {
  36. BSDEthernetTapFactory::BSDEthernetTapFactory()
  37. {
  38. struct stat stattmp;
  39. if (!stat("/sbin/ifconfig",&stattmp))
  40. _pathToIfconfig = "/sbin/ifconfig";
  41. else if (!stat("/usr/sbin/ifconfig",&stattmp))
  42. _pathToIfconfig = "/usr/sbin/ifconfig";
  43. else throw std::runtime_error("can't find ifconfig");
  44. }
  45. BSDEthernetTapFactory::~BSDEthernetTapFactory()
  46. {
  47. Mutex::Lock _l(_devices_m);
  48. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d)
  49. delete *d;
  50. }
  51. EthernetTap *BSDEthernetTapFactory::open(
  52. const MAC &mac,
  53. unsigned int mtu,
  54. unsigned int metric,
  55. uint64_t nwid,
  56. const char *desiredDevice,
  57. const char *friendlyName,
  58. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  59. void *arg)
  60. {
  61. Mutex::Lock _l(_devices_m);
  62. EthernetTap *t = new BSDEthernetTap(mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  63. _devices.push_back(t);
  64. return t;
  65. }
  66. void BSDEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  67. {
  68. {
  69. Mutex::Lock _l(_devices_m);
  70. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d) {
  71. if (*d == tap) {
  72. _devices.erase(d);
  73. break;
  74. }
  75. }
  76. }
  77. delete tap;
  78. }
  79. } // namespace ZeroTier