UnixEthernetTap.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_UNIXETHERNETTAP_HPP
  28. #define ZT_UNIXETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdexcept>
  32. #include "EthernetTap.hpp"
  33. #include "Mutex.hpp"
  34. #include "Thread.hpp"
  35. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. /**
  38. * Tap device using Unix taps (contains special case code for Linux, OSX, etc. but they're all similar)
  39. */
  40. class UnixEthernetTap : public EthernetTap
  41. {
  42. public:
  43. /**
  44. * Construct a new TAP device
  45. *
  46. * Handler arguments: arg,from,to,etherType,data
  47. *
  48. * @param renv Runtime environment
  49. * @param tryToGetDevice Unix device name (e.g. zt0)
  50. * @param mac MAC address of device
  51. * @param mtu MTU of device
  52. * @param desc If non-NULL, a description (not used on all OSes)
  53. * @param handler Handler function to be called when data is received from the tap
  54. * @param arg First argument to handler function
  55. * @throws std::runtime_error Unable to allocate tap device
  56. */
  57. UnixEthernetTap(
  58. const RuntimeEnvironment *renv,
  59. const char *tryToGetDevice,
  60. const MAC &mac,
  61. unsigned int mtu,
  62. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  63. void *arg)
  64. throw(std::runtime_error);
  65. virtual ~UnixEthernetTap();
  66. virtual void setEnabled(bool en);
  67. virtual bool enabled() const;
  68. virtual void setDisplayName(const char *dn);
  69. virtual bool addIP(const InetAddress &ip);
  70. virtual bool removeIP(const InetAddress &ip);
  71. virtual std::set<InetAddress> ips() const;
  72. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  73. virtual std::string deviceName() const;
  74. virtual std::string persistentId() const;
  75. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  76. /**
  77. * Thread main method; do not call elsewhere
  78. */
  79. void threadMain()
  80. throw();
  81. private:
  82. const RuntimeEnvironment *_r;
  83. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  84. void *_arg;
  85. Thread _thread;
  86. std::string _dev;
  87. int _fd;
  88. int _shutdownSignalPipe[2];
  89. volatile bool _enabled;
  90. };
  91. } // namespace ZeroTier
  92. #endif