BSDEthernetTapFactory.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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 "BSDEthernetTapFactory.hpp"
  31. #include "BSDEthernetTap.hpp"
  32. namespace ZeroTier {
  33. BSDEthernetTapFactory::BSDEthernetTapFactory()
  34. {
  35. }
  36. BSDEthernetTapFactory::~BSDEthernetTapFactory()
  37. {
  38. Mutex::Lock _l(_devices_m);
  39. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d)
  40. delete *d;
  41. }
  42. EthernetTap *BSDEthernetTapFactory::open(
  43. const MAC &mac,
  44. unsigned int mtu,
  45. unsigned int metric,
  46. uint64_t nwid,
  47. const char *desiredDevice,
  48. const char *friendlyName,
  49. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  50. void *arg)
  51. {
  52. Mutex::Lock _l(_devices_m);
  53. EthernetTap *t = new BSDEthernetTap(mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  54. _devices.push_back(t);
  55. return t;
  56. }
  57. void BSDEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  58. {
  59. {
  60. Mutex::Lock _l(_devices_m);
  61. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d) {
  62. if (*d == tap) {
  63. _devices.erase(d);
  64. break;
  65. }
  66. }
  67. }
  68. delete tap;
  69. }
  70. } // namespace ZeroTier