EthernetTapFactory.hpp 3.2 KB

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