LWIPStack.hpp 11 KB

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