2
0

TestEthernetTapFactory.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "TestEthernetTapFactory.hpp"
  28. #include "TestEthernetTap.hpp"
  29. namespace ZeroTier {
  30. TestEthernetTapFactory::TestEthernetTapFactory()
  31. {
  32. }
  33. TestEthernetTapFactory::~TestEthernetTapFactory()
  34. {
  35. Mutex::Lock _l1(_taps_m);
  36. Mutex::Lock _l2(_tapsByMac_m);
  37. Mutex::Lock _l3(_tapsByNwid_m);
  38. for(std::set<EthernetTap *>::iterator t(_taps.begin());t!=_taps.end();++t)
  39. delete *t;
  40. }
  41. EthernetTap *TestEthernetTapFactory::open(
  42. const MAC &mac,
  43. unsigned int mtu,
  44. unsigned int metric,
  45. uint64_t nwid,
  46. const char *desiredDevice,
  47. const char *friendlyName,
  48. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  49. void *arg)
  50. {
  51. TestEthernetTap *tap = new TestEthernetTap(mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  52. Mutex::Lock _l1(_taps_m);
  53. Mutex::Lock _l2(_tapsByMac_m);
  54. Mutex::Lock _l3(_tapsByNwid_m);
  55. _taps.insert(tap);
  56. _tapsByMac[mac] = tap;
  57. _tapsByNwid[nwid] = tap;
  58. return tap;
  59. }
  60. void TestEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  61. {
  62. Mutex::Lock _l1(_taps_m);
  63. Mutex::Lock _l2(_tapsByMac_m);
  64. Mutex::Lock _l3(_tapsByNwid_m);
  65. if (!tap)
  66. return;
  67. _taps.erase(tap);
  68. _tapsByMac.erase(tap->mac());
  69. _tapsByNwid.erase(((TestEthernetTap *)tap)->nwid());
  70. delete tap;
  71. }
  72. } // namespace ZeroTier