NetconEthernetTap.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string>
  32. #include <vector>
  33. #include <utility>
  34. #include <stdexcept>
  35. #include <stdint.h>
  36. #include "../node/Constants.hpp"
  37. #include "../node/MulticastGroup.hpp"
  38. #include "../node/Mutex.hpp"
  39. #include "../node/InetAddress.hpp"
  40. #include "../osdep/Thread.hpp"
  41. #include "../osdep/Phy.hpp"
  42. #include "netif/etharp.h"
  43. #define DEFAULT_READ_BUFFER_SIZE 1024 * 1024 * 2
  44. struct tcp_pcb;
  45. struct socket_st;
  46. struct listen_st;
  47. struct bind_st;
  48. struct connect_st;
  49. struct getsockname_st;
  50. struct accept_st;
  51. namespace ZeroTier {
  52. class NetconEthernetTap;
  53. class TcpConnection;
  54. class Larg;
  55. class LWIPStack;
  56. /**
  57. * Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
  58. */
  59. class NetconEthernetTap
  60. {
  61. friend class Phy<NetconEthernetTap *>;
  62. public:
  63. NetconEthernetTap(
  64. const char *homePath,
  65. const MAC &mac,
  66. unsigned int mtu,
  67. unsigned int metric,
  68. uint64_t nwid,
  69. const char *friendlyName,
  70. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  71. void *arg);
  72. ~NetconEthernetTap();
  73. void setEnabled(bool en);
  74. bool enabled() const;
  75. bool addIp(const InetAddress &ip);
  76. bool removeIp(const InetAddress &ip);
  77. std::vector<InetAddress> ips() const;
  78. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  79. std::string deviceName() const;
  80. void setFriendlyName(const char *friendlyName);
  81. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  82. void threadMain()
  83. throw();
  84. LWIPStack *lwipstack;
  85. uint64_t _nwid;
  86. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  87. void *_arg;
  88. private:
  89. // LWIP callbacks
  90. static err_t nc_poll(void* arg, struct tcp_pcb *tpcb);
  91. static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
  92. static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
  93. static void nc_err(void *arg, err_t err);
  94. static void nc_close(struct tcp_pcb *tpcb);
  95. static err_t nc_send(struct tcp_pcb *tpcb);
  96. static err_t nc_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
  97. static err_t nc_connected(void *arg, struct tcp_pcb *tpcb, err_t err);
  98. // RPC handlers (from NetconIntercept)
  99. void unload_rpc(void *data, pid_t &pid, pid_t &tid,
  100. int &rpc_count, char (timestamp[20]), char (magic[sizeof(uint64_t)]), char &cmd, void* &payload);
  101. void handle_getsockname(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct getsockname_st *getsockname_rpc);
  102. void handle_bind(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct bind_st *bind_rpc);
  103. void handle_listen(PhySocket *sock, PhySocket *rpcsock, void **uptr, struct listen_st *listen_rpc);
  104. TcpConnection * handle_socket(PhySocket *sock, void **uptr, struct socket_st* socket_rpc);
  105. void handle_connect(PhySocket *sock, PhySocket *rpcsock, TcpConnection *conn, struct connect_st* connect_rpc);
  106. void handle_write(TcpConnection *conn);
  107. int send_return_value(PhySocket *sock, int retval, int _errno);
  108. int send_return_value(int fd, int retval, int _errno);
  109. void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
  110. void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
  111. void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
  112. void phyOnTcpClose(PhySocket *sock,void **uptr);
  113. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  114. void phyOnTcpWritable(PhySocket *sock,void **uptr);
  115. void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
  116. void phyOnUnixClose(PhySocket *sock,void **uptr);
  117. void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  118. void phyOnUnixWritable(PhySocket *sock,void **uptr);
  119. void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable);
  120. TcpConnection *getConnection(PhySocket *sock);
  121. void addConnection(TcpConnection *conn);
  122. void removeConnection(TcpConnection *conn);
  123. void closeConnection(PhySocket *sock);
  124. ip_addr_t convert_ip(struct sockaddr_in * addr)
  125. {
  126. ip_addr_t conn_addr;
  127. struct sockaddr_in *ipv4 = addr;
  128. short a = ip4_addr1(&(ipv4->sin_addr));
  129. short b = ip4_addr2(&(ipv4->sin_addr));
  130. short c = ip4_addr3(&(ipv4->sin_addr));
  131. short d = ip4_addr4(&(ipv4->sin_addr));
  132. IP4_ADDR(&conn_addr, a,b,c,d);
  133. return conn_addr;
  134. }
  135. Phy<NetconEthernetTap *> _phy;
  136. PhySocket *_unixListenSocket;
  137. std::vector<TcpConnection*> tcp_connections;
  138. std::map<PhySocket*, pid_t> pidmap;
  139. char rcq[DEFAULT_READ_BUFFER_SIZE];
  140. int rcqidx;
  141. std::map<uint64_t, std::pair<PhySocket*, void*> > jobmap;
  142. pid_t rpc_counter;
  143. netif interface;
  144. MAC _mac;
  145. Thread _thread;
  146. std::string _homePath;
  147. std::string _dev; // path to Unix domain socket
  148. std::vector<MulticastGroup> _multicastGroups;
  149. Mutex _multicastGroups_m;
  150. std::vector<InetAddress> _ips;
  151. Mutex _ips_m, _tcpconns_m;
  152. unsigned int _mtu;
  153. volatile bool _enabled;
  154. volatile bool _run;
  155. };
  156. } // namespace ZeroTier
  157. #endif