EthernetTapFactory.hpp 3.4 KB

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