EthernetTapFactory.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifndef ZT_ETHERNETTAPFACTORY_HPP
  28. #define ZT_ETHERNETTAPFACTORY_HPP
  29. #include <stdint.h>
  30. #include <stdexcept>
  31. #include "MAC.hpp"
  32. namespace ZeroTier {
  33. class EthernetTap;
  34. /**
  35. * Ethernet tap factory
  36. *
  37. * This serves up tap implementations for a given platform. It should never be
  38. * deleted until the Node using it is shut down, since doing so may invalidate
  39. * any tap devices it manages.
  40. *
  41. * Using a factory pattern will faciliatate packaging ZeroTier as a library,
  42. * as well as moving toward a design that makes unit testing the entire app
  43. * quite a bit easier.
  44. */
  45. class EthernetTapFactory
  46. {
  47. public:
  48. EthernetTapFactory() {}
  49. virtual ~EthernetTapFactory() {}
  50. /**
  51. * Create / open an Ethernet tap device
  52. *
  53. * On some platforms (Windows) this can be a time-consuming operation.
  54. *
  55. * Note that close() must be used. Do not just delete the tap instance,
  56. * since this may leave orphaned resources or cause other problems.
  57. *
  58. * @param mac MAC address
  59. * @param mtu Device MTU
  60. * @param metric Interface metric (higher = lower priority, may not be supported on all OSes)
  61. * @param nwid ZeroTier network ID
  62. * @param desiredDevice Desired system device name or NULL for no preference
  63. * @param friendlyName Friendly name of this interface or NULL for none (not used on all platforms)
  64. * @param handler Function to call when packets are received
  65. * @param arg First argument to provide to handler
  66. * @return EthernetTap instance
  67. * @throws std::runtime_error Unable to initialize tap device
  68. */
  69. virtual EthernetTap *open(
  70. const MAC &mac,
  71. unsigned int mtu,
  72. unsigned int metric,
  73. uint64_t nwid,
  74. const char *desiredDevice,
  75. const char *friendlyName,
  76. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  77. void *arg) = 0;
  78. /**
  79. * Close an ethernet tap device
  80. *
  81. * @param tap Tap instance
  82. */
  83. virtual void close(EthernetTap *tap) = 0;
  84. };
  85. } // namespace ZeroTier
  86. #endif