LWIPStack.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "lwip/mem.h"
  28. #include "lwip/pbuf.h"
  29. #include "lwip/ip_addr.h"
  30. #include "lwip/netif.h"
  31. /*
  32. #include "lwip/timers.h"
  33. #include "lwip/opt.h"
  34. #include "lwip/init.h"
  35. #include "lwip/mem.h"
  36. #include "lwip/memp.h"
  37. #include "lwip/sys.h"
  38. #include "lwip/stats.h"
  39. //#include "lwip/tcp_impl.h"
  40. //#include "lwip/inet_chksum.h"
  41. #include "lwip/tcpip.h"
  42. //#include "lwip/ip_addr.h"
  43. #include "lwip/debug.h"
  44. //#include "lwip/ip.h"
  45. //#include "lwip/ip_frag.h"
  46. */
  47. #include "../node/Mutex.hpp"
  48. #include <stdio.h>
  49. #include <dlfcn.h>
  50. #ifndef LWIPSTACK_H
  51. #define LWIPSTACK_H
  52. #ifdef D_GNU_SOURCE
  53. #define _GNU_SOURCE
  54. #endif
  55. typedef ip_addr ip_addr_t;
  56. struct tcp_pcb;
  57. #define TCP_WRITE_SIG struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags
  58. #define TCP_SENT_SIG struct tcp_pcb * pcb, err_t (* sent)(void * arg, struct tcp_pcb * tpcb, u16_t len)
  59. #define TCP_NEW_SIG void
  60. #define TCP_SNDBUF_SIG struct tcp_pcb * pcb
  61. #define TCP_CONNECT_SIG struct tcp_pcb * pcb, struct ip_addr * ipaddr, u16_t port, err_t (* connected)(void * arg, struct tcp_pcb * tpcb, err_t err)
  62. #define TCP_RECV_SIG struct tcp_pcb * pcb, err_t (* recv)(void * arg, struct tcp_pcb * tpcb, struct pbuf * p, err_t err)
  63. #define TCP_RECVED_SIG struct tcp_pcb * pcb, u16_t len
  64. #define TCP_ERR_SIG struct tcp_pcb * pcb, void (* err)(void * arg, err_t err)
  65. #define TCP_POLL_SIG struct tcp_pcb * pcb, err_t (* poll)(void * arg, struct tcp_pcb * tpcb), u8_t interval
  66. #define TCP_ARG_SIG struct tcp_pcb * pcb, void * arg
  67. #define TCP_CLOSE_SIG struct tcp_pcb * pcb
  68. #define TCP_ABORT_SIG struct tcp_pcb * pcb
  69. #define TCP_OUTPUT_SIG struct tcp_pcb * pcb
  70. #define TCP_ACCEPT_SIG struct tcp_pcb * pcb, err_t (* accept)(void * arg, struct tcp_pcb * newpcb, err_t err)
  71. #define TCP_LISTEN_SIG struct tcp_pcb * pcb
  72. #define TCP_LISTEN_WITH_BACKLOG_SIG struct tcp_pcb * pcb, u8_t backlog
  73. #define TCP_BIND_SIG struct tcp_pcb * pcb, struct ip_addr * ipaddr, u16_t port
  74. #define PBUF_FREE_SIG struct pbuf *p
  75. #define PBUF_ALLOC_SIG pbuf_layer layer, u16_t length, pbuf_type type
  76. #define LWIP_HTONS_SIG u16_t x
  77. #define LWIP_NTOHS_SIG u16_t x
  78. #define IPADDR_NTOA_SIG const ip_addr_t *addr
  79. #define ETHARP_OUTPUT_SIG struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr
  80. #define ETHERNET_INPUT_SIG struct pbuf *p, struct netif *netif
  81. #define TCP_INPUT_SIG struct pbuf *p, struct netif *inp
  82. #define IP_INPUT_SIG struct pbuf *p, struct netif *inp
  83. #define NETIF_SET_DEFAULT_SIG struct netif *netif
  84. #define NETIF_ADD_SIG struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input
  85. #define NETIF_SET_UP_SIG struct netif *netif
  86. #define NETIF_POLL_SIG struct netif *netif
  87. namespace ZeroTier {
  88. /**
  89. * Loads an instance of liblwip.so in a private memory arena
  90. *
  91. * This uses dlmopen() to load an instance of the LWIP stack into its
  92. * own private memory space. This is done to get around the stack's
  93. * lack of thread-safety or multi-instance support. The alternative
  94. * would be to massively refactor the stack so everything lives in a
  95. * state object instead of static memory space.
  96. */
  97. class LWIPStack
  98. {
  99. public:
  100. void *_libref;
  101. void (*_lwip_init)();
  102. err_t (*_tcp_write)(TCP_WRITE_SIG);
  103. void (*_tcp_sent)(TCP_SENT_SIG);
  104. struct tcp_pcb * (*_tcp_new)(TCP_NEW_SIG);
  105. u16_t (*_tcp_sndbuf)(TCP_SNDBUF_SIG);
  106. err_t (*_tcp_connect)(TCP_CONNECT_SIG);
  107. void (*_tcp_recv)(TCP_RECV_SIG);
  108. void (*_tcp_recved)(TCP_RECVED_SIG);
  109. void (*_tcp_err)(TCP_ERR_SIG);
  110. void (*_tcp_poll)(TCP_POLL_SIG);
  111. void (*_tcp_arg)(TCP_ARG_SIG);
  112. err_t (*_tcp_close)(TCP_CLOSE_SIG);
  113. void (*_tcp_abort)(TCP_ABORT_SIG);
  114. err_t (*_tcp_output)(TCP_OUTPUT_SIG);
  115. void (*_tcp_accept)(TCP_ACCEPT_SIG);
  116. struct tcp_pcb * (*_tcp_listen)(TCP_LISTEN_SIG);
  117. struct tcp_pcb * (*_tcp_listen_with_backlog)(TCP_LISTEN_WITH_BACKLOG_SIG);
  118. err_t (*_tcp_bind)(TCP_BIND_SIG);
  119. void (*_etharp_tmr)(void);
  120. void (*_tcp_tmr)(void);
  121. u8_t (*_pbuf_free)(PBUF_FREE_SIG);
  122. struct pbuf * (*_pbuf_alloc)(PBUF_ALLOC_SIG);
  123. u16_t (*_lwip_htons)(LWIP_HTONS_SIG);
  124. u16_t (*_lwip_ntohs)(LWIP_NTOHS_SIG);
  125. char* (*_ipaddr_ntoa)(IPADDR_NTOA_SIG);
  126. err_t (*_etharp_output)(ETHARP_OUTPUT_SIG);
  127. err_t (*_ethernet_input)(ETHERNET_INPUT_SIG);
  128. void (*_tcp_input)(TCP_INPUT_SIG);
  129. err_t (*_ip_input)(IP_INPUT_SIG);
  130. void (*_netif_set_default)(NETIF_SET_DEFAULT_SIG);
  131. struct netif * (*_netif_add)(NETIF_ADD_SIG);
  132. void (*_netif_set_up)(NETIF_SET_UP_SIG);
  133. void (*_netif_poll)(NETIF_POLL_SIG);
  134. Mutex _lock;
  135. LWIPStack(const char* path) :
  136. _libref(NULL)
  137. {
  138. _libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
  139. if(_libref == NULL)
  140. printf("dlerror(): %s\n", dlerror());
  141. _lwip_init = (void(*)(void))dlsym(_libref, "lwip_init");
  142. _tcp_write = (err_t(*)(TCP_WRITE_SIG))dlsym(_libref, "tcp_write");
  143. _tcp_sent = (void(*)(TCP_SENT_SIG))dlsym(_libref, "tcp_sent");
  144. _tcp_new = (struct tcp_pcb*(*)(TCP_NEW_SIG))dlsym(_libref, "tcp_new");
  145. _tcp_sndbuf = (u16_t(*)(TCP_SNDBUF_SIG))dlsym(_libref, "tcp_sndbuf");
  146. _tcp_connect = (err_t(*)(TCP_CONNECT_SIG))dlsym(_libref, "tcp_connect");
  147. _tcp_recv = (void(*)(TCP_RECV_SIG))dlsym(_libref, "tcp_recv");
  148. _tcp_recved = (void(*)(TCP_RECVED_SIG))dlsym(_libref, "tcp_recved");
  149. _tcp_err = (void(*)(TCP_ERR_SIG))dlsym(_libref, "tcp_err");
  150. _tcp_poll = (void(*)(TCP_POLL_SIG))dlsym(_libref, "tcp_poll");
  151. _tcp_arg = (void(*)(TCP_ARG_SIG))dlsym(_libref, "tcp_arg");
  152. _tcp_close = (err_t(*)(TCP_CLOSE_SIG))dlsym(_libref, "tcp_close");
  153. _tcp_abort = (void(*)(TCP_ABORT_SIG))dlsym(_libref, "tcp_abort");
  154. _tcp_output = (err_t(*)(TCP_OUTPUT_SIG))dlsym(_libref, "tcp_output");
  155. _tcp_accept = (void(*)(TCP_ACCEPT_SIG))dlsym(_libref, "tcp_accept");
  156. _tcp_listen = (struct tcp_pcb*(*)(TCP_LISTEN_SIG))dlsym(_libref, "tcp_listen");
  157. _tcp_listen_with_backlog = (struct tcp_pcb*(*)(TCP_LISTEN_WITH_BACKLOG_SIG))dlsym(_libref, "tcp_listen_with_backlog");
  158. _tcp_bind = (err_t(*)(TCP_BIND_SIG))dlsym(_libref, "tcp_bind");
  159. _etharp_tmr = (void(*)(void))dlsym(_libref, "etharp_tmr");
  160. _tcp_tmr = (void(*)(void))dlsym(_libref, "tcp_tmr");
  161. _pbuf_free = (u8_t(*)(PBUF_FREE_SIG))dlsym(_libref, "pbuf_free");
  162. _pbuf_alloc = (struct pbuf*(*)(PBUF_ALLOC_SIG))dlsym(_libref, "pbuf_alloc");
  163. _lwip_htons = (u16_t(*)(LWIP_HTONS_SIG))dlsym(_libref, "lwip_htons");
  164. _lwip_ntohs = (u16_t(*)(LWIP_NTOHS_SIG))dlsym(_libref, "lwip_ntohs");
  165. _ipaddr_ntoa = (char*(*)(IPADDR_NTOA_SIG))dlsym(_libref, "ipaddr_ntoa");
  166. _etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))dlsym(_libref, "etharp_output");
  167. _ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))dlsym(_libref, "ethernet_input");
  168. _tcp_input = (void(*)(TCP_INPUT_SIG))dlsym(_libref, "tcp_input");
  169. _ip_input = (err_t(*)(IP_INPUT_SIG))dlsym(_libref, "ip_input");
  170. _netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(_libref, "netif_set_default");
  171. _netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(_libref, "netif_add");
  172. _netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(_libref, "netif_set_up");
  173. _netif_poll = (void(*)(NETIF_POLL_SIG))dlsym(_libref, "netif_poll");
  174. }
  175. ~LWIPStack()
  176. {
  177. if (_libref)
  178. dlclose(_libref);
  179. }
  180. inline void lwip_init() throw() { Mutex::Lock _l(_lock); return _lwip_init(); }
  181. inline err_t tcp_write(TCP_WRITE_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_write(pcb,arg,len,apiflags); }
  182. inline void tcp_sent(TCP_SENT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_sent(pcb,sent); }
  183. inline struct tcp_pcb * tcp_new(TCP_NEW_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_new(); }
  184. inline u16_t tcp_sndbuf(TCP_SNDBUF_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_sndbuf(pcb); }
  185. inline err_t tcp_connect(TCP_CONNECT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_connect(pcb,ipaddr,port,connected); }
  186. inline void tcp_recv(TCP_RECV_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_recv(pcb,recv); }
  187. inline void tcp_recved(TCP_RECVED_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_recved(pcb,len); }
  188. inline void tcp_err(TCP_ERR_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_err(pcb,err); }
  189. inline void tcp_poll(TCP_POLL_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_poll(pcb,poll,interval); }
  190. inline void tcp_arg(TCP_ARG_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_arg(pcb,arg); }
  191. inline err_t tcp_close(TCP_CLOSE_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_close(pcb); }
  192. inline void tcp_abort(TCP_ABORT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_abort(pcb); }
  193. inline err_t tcp_output(TCP_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_output(pcb); }
  194. inline void tcp_accept(TCP_ACCEPT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_accept(pcb,accept); }
  195. inline struct tcp_pcb * tcp_listen(TCP_LISTEN_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_listen(pcb); }
  196. inline struct tcp_pcb * tcp_listen_with_backlog(TCP_LISTEN_WITH_BACKLOG_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_listen_with_backlog(pcb,backlog); }
  197. inline err_t tcp_bind(TCP_BIND_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_bind(pcb,ipaddr,port); }
  198. inline void etharp_tmr(void) throw() { Mutex::Lock _l(_lock); return _etharp_tmr(); }
  199. inline void tcp_tmr(void) throw() { Mutex::Lock _l(_lock); return _tcp_tmr(); }
  200. inline u8_t pbuf_free(PBUF_FREE_SIG) throw() { Mutex::Lock _l(_lock); return _pbuf_free(p); }
  201. inline struct pbuf * pbuf_alloc(PBUF_ALLOC_SIG) throw() { Mutex::Lock _l(_lock); return _pbuf_alloc(layer,length,type); }
  202. inline u16_t lwip_htons(LWIP_HTONS_SIG) throw() { Mutex::Lock _l(_lock); return _lwip_htons(x); }
  203. inline u16_t lwip_ntohs(LWIP_NTOHS_SIG) throw() { Mutex::Lock _l(_lock); return _lwip_ntohs(x); }
  204. inline char* ipaddr_ntoa(IPADDR_NTOA_SIG) throw() { Mutex::Lock _l(_lock); return _ipaddr_ntoa(addr); }
  205. inline err_t etharp_output(ETHARP_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _etharp_output(netif,q,ipaddr); }
  206. inline err_t ethernet_input(ETHERNET_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ethernet_input(p,netif); }
  207. inline void tcp_input(TCP_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_input(p,inp); }
  208. inline err_t ip_input(IP_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ip_input(p,inp); }
  209. inline void netif_set_default(NETIF_SET_DEFAULT_SIG) throw() { Mutex::Lock _l(_lock); return _netif_set_default(netif); }
  210. inline struct netif * netif_add(NETIF_ADD_SIG) throw() { Mutex::Lock _l(_lock); return _netif_add(netif,ipaddr,netmask,gw,state,init,input); }
  211. inline void netif_set_up(NETIF_SET_UP_SIG) throw() { Mutex::Lock _l(_lock); return _netif_set_up(netif); }
  212. inline void netif_poll(NETIF_POLL_SIG) throw() { Mutex::Lock _l(_lock); return _netif_poll(netif); }
  213. };
  214. } // namespace ZeroTier
  215. #endif