NetconEthernetTap.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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_NETCONETHERNETTAP_HPP
  28. #define ZT_NETCONETHERNETTAP_HPP
  29. #ifdef ZT_ENABLE_NETCON
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string>
  33. #include <vector>
  34. #include <stdexcept>
  35. #include "../node/Constants.hpp"
  36. #include "../node/MulticastGroup.hpp"
  37. #include "../node/Mutex.hpp"
  38. #include "../node/InetAddress.hpp"
  39. #include "../osdep/Thread.hpp"
  40. #include "../osdep/Phy.hpp"
  41. #include "NetconService.hpp"
  42. #include "NetconUtilities.hpp"
  43. namespace ZeroTier {
  44. class NetconEthernetTap;
  45. /**
  46. * Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
  47. */
  48. class NetconEthernetTap
  49. {
  50. friend class Phy<NetconEthernetTap *>;
  51. public:
  52. NetconEthernetTap(
  53. const char *homePath,
  54. const MAC &mac,
  55. unsigned int mtu,
  56. unsigned int metric,
  57. uint64_t nwid,
  58. const char *friendlyName,
  59. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  60. void *arg);
  61. ~NetconEthernetTap();
  62. void setEnabled(bool en);
  63. bool enabled() const;
  64. bool addIp(const InetAddress &ip);
  65. bool removeIp(const InetAddress &ip);
  66. std::vector<InetAddress> ips() const;
  67. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  68. std::string deviceName() const;
  69. void setFriendlyName(const char *friendlyName);
  70. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  71. void threadMain()
  72. throw();
  73. LWIPStack *lwipstack;
  74. private:
  75. // LWIP callbacks
  76. static err_t nc_poll(void* arg, struct tcp_pcb *tpcb);
  77. static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
  78. static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
  79. static void nc_err(void *arg, err_t err);
  80. static void nc_close(struct tcp_pcb *tpcb);
  81. static err_t nc_send(struct tcp_pcb *tpcb);
  82. static err_t nc_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
  83. static err_t nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
  84. // RPC handlers (from NetconIntercept)
  85. void handle_bind(NetconClient *client, struct bind_st *bind_rpc);
  86. void handle_listen(NetconClient *client, struct listen_st *listen_rpc);
  87. void handle_retval(NetconClient *client, unsigned char* buf);
  88. void handle_socket(NetconClient *client, struct socket_st* socket_rpc);
  89. void handle_connect(NetconClient *client, struct connect_st* connect_rpc);
  90. void handle_write(NetconConnection *c);
  91. void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
  92. void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
  93. void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
  94. void phyOnTcpClose(PhySocket *sock,void **uptr);
  95. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  96. void phyOnTcpWritable(PhySocket *sock,void **uptr);
  97. void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
  98. void phyOnUnixClose(PhySocket *sock,void **uptr);
  99. void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  100. void phyOnUnixWritable(PhySocket *sock,void **uptr);
  101. void phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr);
  102. void phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n);
  103. void phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr);
  104. int send_return_value(NetconClient *client, int retval);
  105. ip_addr_t convert_ip(struct sockaddr_in * addr)
  106. {
  107. ip_addr_t conn_addr;
  108. struct sockaddr_in *ipv4 = addr;
  109. short a = ip4_addr1(&(ipv4->sin_addr));
  110. short b = ip4_addr2(&(ipv4->sin_addr));
  111. short c = ip4_addr3(&(ipv4->sin_addr));
  112. short d = ip4_addr4(&(ipv4->sin_addr));
  113. IP4_ADDR(&conn_addr, a,b,c,d);
  114. return conn_addr;
  115. }
  116. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  117. void *_arg;
  118. // Client helpers
  119. NetconConnection *getConnectionByThisFD(int fd);
  120. NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb);
  121. NetconClient *getClientByPCB(struct tcp_pcb *pcb);
  122. void closeClient(NetconClient *client);
  123. void closeConnection(NetconConnection *conn);
  124. Phy<NetconEthernetTap *> _phy;
  125. PhySocket *_unixListenSocket;
  126. std::vector<NetconClient*> clients;
  127. uint64_t _nwid;
  128. Thread _thread;
  129. std::string _homePath;
  130. std::string _dev; // path to Unix domain socket
  131. std::vector<MulticastGroup> _lastMulticastGroupList;
  132. Mutex _lastMulticastGroupList_m;
  133. std::vector<InetAddress> _ips;
  134. Mutex _ips_m;
  135. unsigned int _mtu;
  136. volatile bool _enabled;
  137. volatile bool _run;
  138. };
  139. } // namespace ZeroTier
  140. #endif // ZT_ENABLE_NETCON
  141. #endif