LWIPStack.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <stdio.h>
  48. #include <dlfcn.h>
  49. #ifndef LWIPSTACK_H
  50. #define LWIPSTACK_H
  51. #ifdef D_GNU_SOURCE
  52. #define _GNU_SOURCE
  53. #endif
  54. typedef ip_addr ip_addr_t;
  55. #define TCP_WRITE_SIG struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags
  56. #define TCP_SENT_SIG struct tcp_pcb * pcb, err_t (* sent)(void * arg, struct tcp_pcb * tpcb, u16_t len)
  57. #define TCP_NEW_SIG void
  58. #define TCP_SNDBUF_SIG struct tcp_pcb * pcb
  59. #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)
  60. #define TCP_RECV_SIG struct tcp_pcb * pcb, err_t (* recv)(void * arg, struct tcp_pcb * tpcb, struct pbuf * p, err_t err)
  61. #define TCP_RECVED_SIG struct tcp_pcb * pcb, u16_t len
  62. #define TCP_ERR_SIG struct tcp_pcb * pcb, void (* err)(void * arg, err_t err)
  63. #define TCP_POLL_SIG struct tcp_pcb * pcb, err_t (* poll)(void * arg, struct tcp_pcb * tpcb), u8_t interval
  64. #define TCP_ARG_SIG struct tcp_pcb * pcb, void * arg
  65. #define TCP_CLOSE_SIG struct tcp_pcb * pcb
  66. #define TCP_ABORT_SIG struct tcp_pcb * pcb
  67. #define TCP_OUTPUT_SIG struct tcp_pcb * pcb
  68. #define TCP_ACCEPT_SIG struct tcp_pcb * pcb, err_t (* accept)(void * arg, struct tcp_pcb * newpcb, err_t err)
  69. #define TCP_LISTEN_SIG struct tcp_pcb * pcb
  70. #define TCP_LISTEN_WITH_BACKLOG_SIG struct tcp_pcb * pcb, u8_t backlog
  71. #define TCP_BIND_SIG struct tcp_pcb * pcb, struct ip_addr * ipaddr, u16_t port
  72. //#define TAPIF_INIT_SIG struct netif *netif
  73. //#define TAPIF_INPUT_SIG LWIPStack* ls, struct netif *netif
  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. /**
  88. * Loads an instance of liblwip.so in a private memory arena
  89. *
  90. * This uses dlmopen() to load an instance of the LWIP stack into its
  91. * own private memory space. This is done to get around the stack's
  92. * lack of thread-safety or multi-instance support. The alternative
  93. * would be to massively refactor the stack so everything lives in a
  94. * state object instead of static memory space.
  95. */
  96. class LWIPStack
  97. {
  98. private:
  99. void* libref;
  100. public:
  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. //err_t (*tapif_init)(TAPIF_INIT_SIG);
  122. //void (*tapif_input)(TAPIF_INPUT_SIG);
  123. u8_t (*pbuf_free)(PBUF_FREE_SIG);
  124. struct pbuf * (*pbuf_alloc)(PBUF_ALLOC_SIG);
  125. u16_t (*lwip_htons)(LWIP_HTONS_SIG);
  126. u16_t (*lwip_ntohs)(LWIP_NTOHS_SIG);
  127. char* (*ipaddr_ntoa)(IPADDR_NTOA_SIG);
  128. err_t (*etharp_output)(ETHARP_OUTPUT_SIG);
  129. err_t (*ethernet_input)(ETHERNET_INPUT_SIG);
  130. void (*tcp_input)(TCP_INPUT_SIG);
  131. err_t (*ip_input)(IP_INPUT_SIG);
  132. void (*netif_set_default)(NETIF_SET_DEFAULT_SIG);
  133. struct netif * (*netif_add)(NETIF_ADD_SIG);
  134. void (*netif_set_up)(NETIF_SET_UP_SIG);
  135. void (*netif_poll)(NETIF_POLL_SIG);
  136. LWIPStack(const char* path)
  137. {
  138. libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
  139. if(libref == NULL)
  140. printf("dlerror(): %s\n", dlerror());
  141. /* assign function pointers to symbols in dynamically-loaded lib */
  142. lwip_init = (void(*)(void))dlsym(libref, "lwip_init");
  143. tcp_write = (err_t(*)(TCP_WRITE_SIG))dlsym(libref, "tcp_write");
  144. tcp_sent = (void(*)(TCP_SENT_SIG))dlsym(libref, "tcp_sent");
  145. tcp_new = (struct tcp_pcb*(*)(TCP_NEW_SIG))dlsym(libref, "tcp_new");
  146. tcp_sndbuf = (u16_t(*)(TCP_SNDBUF_SIG))dlsym(libref, "tcp_sndbuf");
  147. tcp_connect = (err_t(*)(TCP_CONNECT_SIG))dlsym(libref, "tcp_connect");
  148. tcp_recv = (void(*)(TCP_RECV_SIG))dlsym(libref, "tcp_recv");
  149. tcp_recved = (void(*)(TCP_RECVED_SIG))dlsym(libref, "tcp_recved");
  150. tcp_err = (void(*)(TCP_ERR_SIG))dlsym(libref, "tcp_err");
  151. tcp_poll = (void(*)(TCP_POLL_SIG))dlsym(libref, "tcp_poll");
  152. tcp_arg = (void(*)(TCP_ARG_SIG))dlsym(libref, "tcp_arg");
  153. tcp_close = (err_t(*)(TCP_CLOSE_SIG))dlsym(libref, "tcp_close");
  154. tcp_abort = (void(*)(TCP_ABORT_SIG))dlsym(libref, "tcp_abort");
  155. tcp_output = (err_t(*)(TCP_OUTPUT_SIG))dlsym(libref, "tcp_output");
  156. tcp_accept = (void(*)(TCP_ACCEPT_SIG))dlsym(libref, "tcp_accept");
  157. tcp_listen = (struct tcp_pcb*(*)(TCP_LISTEN_SIG))dlsym(libref, "tcp_listen");
  158. tcp_listen_with_backlog = (struct tcp_pcb*(*)(TCP_LISTEN_WITH_BACKLOG_SIG))dlsym(libref, "tcp_listen_with_backlog");
  159. tcp_bind = (err_t(*)(TCP_BIND_SIG))dlsym(libref, "tcp_bind");
  160. etharp_tmr = (void(*)(void))dlsym(libref, "etharp_tmr");
  161. tcp_tmr = (void(*)(void))dlsym(libref, "tcp_tmr");
  162. //tapif_init = (err_t(*)(TAPIF_INIT_SIG))dlsym(libref, "tapif_init");
  163. //tapif_input = (void(*)(TAPIF_INPUT_SIG))dlsym(libref, "tapif_input");
  164. pbuf_free = (u8_t(*)(PBUF_FREE_SIG))dlsym(libref, "pbuf_free");
  165. pbuf_alloc = (struct pbuf*(*)(PBUF_ALLOC_SIG))dlsym(libref, "pbuf_alloc");
  166. lwip_htons = (u16_t(*)(LWIP_HTONS_SIG))dlsym(libref, "lwip_htons");
  167. lwip_ntohs = (u16_t(*)(LWIP_NTOHS_SIG))dlsym(libref, "lwip_ntohs");
  168. ipaddr_ntoa = (char*(*)(IPADDR_NTOA_SIG))dlsym(libref, "ipaddr_ntoa");
  169. etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))dlsym(libref, "etharp_output");
  170. ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))dlsym(libref, "ethernet_input");
  171. tcp_input = (void(*)(TCP_INPUT_SIG))dlsym(libref, "tcp_input");
  172. ip_input = (err_t(*)(IP_INPUT_SIG))dlsym(libref, "ip_input");
  173. netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(libref, "netif_set_default");
  174. netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(libref, "netif_add");
  175. netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(libref, "netif_set_up");
  176. netif_poll = (void(*)(NETIF_POLL_SIG))dlsym(libref, "netif_poll");
  177. }
  178. ~LWIPStack()
  179. {
  180. if (libref)
  181. dlclose(libref);
  182. }
  183. };
  184. #endif