summaryrefslogtreecommitdiff
path: root/netcon/LWIPStack.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'netcon/LWIPStack.hpp')
-rw-r--r--netcon/LWIPStack.hpp199
1 files changed, 118 insertions, 81 deletions
diff --git a/netcon/LWIPStack.hpp b/netcon/LWIPStack.hpp
index b93de87d..c1492b6f 100644
--- a/netcon/LWIPStack.hpp
+++ b/netcon/LWIPStack.hpp
@@ -48,6 +48,8 @@
//#include "lwip/ip_frag.h"
*/
+#include "../node/Mutex.hpp"
+
#include <stdio.h>
#include <dlfcn.h>
@@ -58,10 +60,8 @@
#define _GNU_SOURCE
#endif
-
typedef ip_addr ip_addr_t;
-
#define TCP_WRITE_SIG struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags
#define TCP_SENT_SIG struct tcp_pcb * pcb, err_t (* sent)(void * arg, struct tcp_pcb * tpcb, u16_t len)
#define TCP_NEW_SIG void
@@ -97,6 +97,8 @@ typedef ip_addr ip_addr_t;
#define NETIF_SET_UP_SIG struct netif *netif
#define NETIF_POLL_SIG struct netif *netif
+namespace ZeroTier {
+
/**
* Loads an instance of liblwip.so in a private memory arena
*
@@ -109,90 +111,88 @@ typedef ip_addr ip_addr_t;
class LWIPStack
{
private:
- void* libref;
+ void *_libref;
+
+ void (*_lwip_init)();
+ err_t (*_tcp_write)(TCP_WRITE_SIG);
+ void (*_tcp_sent)(TCP_SENT_SIG);
+ struct tcp_pcb * (*_tcp_new)(TCP_NEW_SIG);
+ u16_t (*_tcp_sndbuf)(TCP_SNDBUF_SIG);
+ err_t (*_tcp_connect)(TCP_CONNECT_SIG);
+ void (*_tcp_recv)(TCP_RECV_SIG);
+ void (*_tcp_recved)(TCP_RECVED_SIG);
+ void (*_tcp_err)(TCP_ERR_SIG);
+ void (*_tcp_poll)(TCP_POLL_SIG);
+ void (*_tcp_arg)(TCP_ARG_SIG);
+ err_t (*_tcp_close)(TCP_CLOSE_SIG);
+ void (*_tcp_abort)(TCP_ABORT_SIG);
+ err_t (*_tcp_output)(TCP_OUTPUT_SIG);
+ void (*_tcp_accept)(TCP_ACCEPT_SIG);
+ struct tcp_pcb * (*_tcp_listen)(TCP_LISTEN_SIG);
+ struct tcp_pcb * (*_tcp_listen_with_backlog)(TCP_LISTEN_WITH_BACKLOG_SIG);
+ err_t (*_tcp_bind)(TCP_BIND_SIG);
+ void (*_etharp_tmr)(void);
+ void (*_tcp_tmr)(void);
+ u8_t (*_pbuf_free)(PBUF_FREE_SIG);
+ struct pbuf * (*_pbuf_alloc)(PBUF_ALLOC_SIG);
+ u16_t (*_lwip_htons)(LWIP_HTONS_SIG);
+ u16_t (*_lwip_ntohs)(LWIP_NTOHS_SIG);
+ char* (*_ipaddr_ntoa)(IPADDR_NTOA_SIG);
+ err_t (*_etharp_output)(ETHARP_OUTPUT_SIG);
+ err_t (*_ethernet_input)(ETHERNET_INPUT_SIG);
+ void (*_tcp_input)(TCP_INPUT_SIG);
+ err_t (*_ip_input)(IP_INPUT_SIG);
+
+ void (*_netif_set_default)(NETIF_SET_DEFAULT_SIG);
+ struct netif * (*_netif_add)(NETIF_ADD_SIG);
+ void (*_netif_set_up)(NETIF_SET_UP_SIG);
+ void (*_netif_poll)(NETIF_POLL_SIG);
+
+ Mutex _lock;
public:
- void (*lwip_init)();
- err_t (*tcp_write)(TCP_WRITE_SIG);
- void (*tcp_sent)(TCP_SENT_SIG);
- struct tcp_pcb * (*tcp_new)(TCP_NEW_SIG);
- u16_t (*tcp_sndbuf)(TCP_SNDBUF_SIG);
- err_t (*tcp_connect)(TCP_CONNECT_SIG);
- void (*tcp_recv)(TCP_RECV_SIG);
- void (*tcp_recved)(TCP_RECVED_SIG);
- void (*tcp_err)(TCP_ERR_SIG);
- void (*tcp_poll)(TCP_POLL_SIG);
- void (*tcp_arg)(TCP_ARG_SIG);
- err_t (*tcp_close)(TCP_CLOSE_SIG);
- void (*tcp_abort)(TCP_ABORT_SIG);
- err_t (*tcp_output)(TCP_OUTPUT_SIG);
- void (*tcp_accept)(TCP_ACCEPT_SIG);
- struct tcp_pcb * (*tcp_listen)(TCP_LISTEN_SIG);
- struct tcp_pcb * (*tcp_listen_with_backlog)(TCP_LISTEN_WITH_BACKLOG_SIG);
- err_t (*tcp_bind)(TCP_BIND_SIG);
- void (*etharp_tmr)(void);
- void (*tcp_tmr)(void);
- //err_t (*tapif_init)(TAPIF_INIT_SIG);
- //void (*tapif_input)(TAPIF_INPUT_SIG);
- u8_t (*pbuf_free)(PBUF_FREE_SIG);
- struct pbuf * (*pbuf_alloc)(PBUF_ALLOC_SIG);
- u16_t (*lwip_htons)(LWIP_HTONS_SIG);
- u16_t (*lwip_ntohs)(LWIP_NTOHS_SIG);
- char* (*ipaddr_ntoa)(IPADDR_NTOA_SIG);
- err_t (*etharp_output)(ETHARP_OUTPUT_SIG);
- err_t (*ethernet_input)(ETHERNET_INPUT_SIG);
- void (*tcp_input)(TCP_INPUT_SIG);
- err_t (*ip_input)(IP_INPUT_SIG);
-
- void (*netif_set_default)(NETIF_SET_DEFAULT_SIG);
- struct netif * (*netif_add)(NETIF_ADD_SIG);
- void (*netif_set_up)(NETIF_SET_UP_SIG);
- void (*netif_poll)(NETIF_POLL_SIG);
-
- LWIPStack(const char* path)
+ LWIPStack(const char* path) :
+ _libref(NULL)
{
- libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
- if(libref == NULL)
+ _libref = dlmopen(LM_ID_NEWLM, path, RTLD_NOW);
+ if(_libref == NULL)
printf("dlerror(): %s\n", dlerror());
- /* assign function pointers to symbols in dynamically-loaded lib */
- lwip_init = (void(*)(void))dlsym(libref, "lwip_init");
- tcp_write = (err_t(*)(TCP_WRITE_SIG))dlsym(libref, "tcp_write");
- tcp_sent = (void(*)(TCP_SENT_SIG))dlsym(libref, "tcp_sent");
- tcp_new = (struct tcp_pcb*(*)(TCP_NEW_SIG))dlsym(libref, "tcp_new");
- tcp_sndbuf = (u16_t(*)(TCP_SNDBUF_SIG))dlsym(libref, "tcp_sndbuf");
- tcp_connect = (err_t(*)(TCP_CONNECT_SIG))dlsym(libref, "tcp_connect");
- tcp_recv = (void(*)(TCP_RECV_SIG))dlsym(libref, "tcp_recv");
- tcp_recved = (void(*)(TCP_RECVED_SIG))dlsym(libref, "tcp_recved");
- tcp_err = (void(*)(TCP_ERR_SIG))dlsym(libref, "tcp_err");
- tcp_poll = (void(*)(TCP_POLL_SIG))dlsym(libref, "tcp_poll");
- tcp_arg = (void(*)(TCP_ARG_SIG))dlsym(libref, "tcp_arg");
- tcp_close = (err_t(*)(TCP_CLOSE_SIG))dlsym(libref, "tcp_close");
- tcp_abort = (void(*)(TCP_ABORT_SIG))dlsym(libref, "tcp_abort");
- tcp_output = (err_t(*)(TCP_OUTPUT_SIG))dlsym(libref, "tcp_output");
- tcp_accept = (void(*)(TCP_ACCEPT_SIG))dlsym(libref, "tcp_accept");
- tcp_listen = (struct tcp_pcb*(*)(TCP_LISTEN_SIG))dlsym(libref, "tcp_listen");
- tcp_listen_with_backlog = (struct tcp_pcb*(*)(TCP_LISTEN_WITH_BACKLOG_SIG))dlsym(libref, "tcp_listen_with_backlog");
- tcp_bind = (err_t(*)(TCP_BIND_SIG))dlsym(libref, "tcp_bind");
- etharp_tmr = (void(*)(void))dlsym(libref, "etharp_tmr");
- tcp_tmr = (void(*)(void))dlsym(libref, "tcp_tmr");
- //tapif_init = (err_t(*)(TAPIF_INIT_SIG))dlsym(libref, "tapif_init");
- //tapif_input = (void(*)(TAPIF_INPUT_SIG))dlsym(libref, "tapif_input");
- pbuf_free = (u8_t(*)(PBUF_FREE_SIG))dlsym(libref, "pbuf_free");
- pbuf_alloc = (struct pbuf*(*)(PBUF_ALLOC_SIG))dlsym(libref, "pbuf_alloc");
- lwip_htons = (u16_t(*)(LWIP_HTONS_SIG))dlsym(libref, "lwip_htons");
- lwip_ntohs = (u16_t(*)(LWIP_NTOHS_SIG))dlsym(libref, "lwip_ntohs");
- ipaddr_ntoa = (char*(*)(IPADDR_NTOA_SIG))dlsym(libref, "ipaddr_ntoa");
- etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))dlsym(libref, "etharp_output");
- ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))dlsym(libref, "ethernet_input");
-
- tcp_input = (void(*)(TCP_INPUT_SIG))dlsym(libref, "tcp_input");
- ip_input = (err_t(*)(IP_INPUT_SIG))dlsym(libref, "ip_input");
-
- netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(libref, "netif_set_default");
- netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(libref, "netif_add");
- netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(libref, "netif_set_up");
- netif_poll = (void(*)(NETIF_POLL_SIG))dlsym(libref, "netif_poll");
+ _lwip_init = (void(*)(void))dlsym(libref, "lwip_init");
+ _tcp_write = (err_t(*)(TCP_WRITE_SIG))dlsym(libref, "tcp_write");
+ _tcp_sent = (void(*)(TCP_SENT_SIG))dlsym(libref, "tcp_sent");
+ _tcp_new = (struct tcp_pcb*(*)(TCP_NEW_SIG))dlsym(libref, "tcp_new");
+ _tcp_sndbuf = (u16_t(*)(TCP_SNDBUF_SIG))dlsym(libref, "tcp_sndbuf");
+ _tcp_connect = (err_t(*)(TCP_CONNECT_SIG))dlsym(libref, "tcp_connect");
+ _tcp_recv = (void(*)(TCP_RECV_SIG))dlsym(libref, "tcp_recv");
+ _tcp_recved = (void(*)(TCP_RECVED_SIG))dlsym(libref, "tcp_recved");
+ _tcp_err = (void(*)(TCP_ERR_SIG))dlsym(libref, "tcp_err");
+ _tcp_poll = (void(*)(TCP_POLL_SIG))dlsym(libref, "tcp_poll");
+ _tcp_arg = (void(*)(TCP_ARG_SIG))dlsym(libref, "tcp_arg");
+ _tcp_close = (err_t(*)(TCP_CLOSE_SIG))dlsym(libref, "tcp_close");
+ _tcp_abort = (void(*)(TCP_ABORT_SIG))dlsym(libref, "tcp_abort");
+ _tcp_output = (err_t(*)(TCP_OUTPUT_SIG))dlsym(libref, "tcp_output");
+ _tcp_accept = (void(*)(TCP_ACCEPT_SIG))dlsym(libref, "tcp_accept");
+ _tcp_listen = (struct tcp_pcb*(*)(TCP_LISTEN_SIG))dlsym(libref, "tcp_listen");
+ _tcp_listen_with_backlog = (struct tcp_pcb*(*)(TCP_LISTEN_WITH_BACKLOG_SIG))dlsym(libref, "tcp_listen_with_backlog");
+ _tcp_bind = (err_t(*)(TCP_BIND_SIG))dlsym(libref, "tcp_bind");
+ _etharp_tmr = (void(*)(void))dlsym(libref, "etharp_tmr");
+ _tcp_tmr = (void(*)(void))dlsym(libref, "tcp_tmr");
+ _pbuf_free = (u8_t(*)(PBUF_FREE_SIG))dlsym(libref, "pbuf_free");
+ _pbuf_alloc = (struct pbuf*(*)(PBUF_ALLOC_SIG))dlsym(libref, "pbuf_alloc");
+ _lwip_htons = (u16_t(*)(LWIP_HTONS_SIG))dlsym(libref, "lwip_htons");
+ _lwip_ntohs = (u16_t(*)(LWIP_NTOHS_SIG))dlsym(libref, "lwip_ntohs");
+ _ipaddr_ntoa = (char*(*)(IPADDR_NTOA_SIG))dlsym(libref, "ipaddr_ntoa");
+ _etharp_output = (err_t(*)(ETHARP_OUTPUT_SIG))dlsym(libref, "etharp_output");
+ _ethernet_input = (err_t(*)(ETHERNET_INPUT_SIG))dlsym(libref, "ethernet_input");
+
+ _tcp_input = (void(*)(TCP_INPUT_SIG))dlsym(libref, "tcp_input");
+ _ip_input = (err_t(*)(IP_INPUT_SIG))dlsym(libref, "ip_input");
+
+ _netif_set_default = (void(*)(NETIF_SET_DEFAULT_SIG))dlsym(libref, "netif_set_default");
+ _netif_add = (struct netif*(*)(NETIF_ADD_SIG))dlsym(libref, "netif_add");
+ _netif_set_up = (void(*)(NETIF_SET_UP_SIG))dlsym(libref, "netif_set_up");
+ _netif_poll = (void(*)(NETIF_POLL_SIG))dlsym(libref, "netif_poll");
}
~LWIPStack()
@@ -200,6 +200,43 @@ public:
if (libref)
dlclose(libref);
}
+
+ inline void lwip_init() throw() { Mutex::Lock _l(_lock); return _lwip_init(); }
+ inline err_t tcp_write(TCP_WRITE_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_write(pcb,arg,len,apiflags); }
+ inline void tcp_sent(TCP_SENT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_sent(pcb,sent); }
+ inline struct tcp_pcb * tcp_new(TCP_NEW_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_new(); }
+ inline u16_t tcp_sndbuf(TCP_SNDBUF_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_sndbuf(pcb); }
+ inline err_t tcp_connect(TCP_CONNECT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_connect(pcb,ipaddr,port,connected); }
+ inline void tcp_recv(TCP_RECV_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_recv(pcb,recv); }
+ inline void tcp_recved(TCP_RECVED_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_recved(pcb,len); }
+ inline void tcp_err(TCP_ERR_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_err(pcb,err); }
+ inline void tcp_poll(TCP_POLL_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_poll(pcb,poll); }
+ inline void tcp_arg(TCP_ARG_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_arg(pcb,arg); }
+ inline err_t tcp_close(TCP_CLOSE_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_close(pcb); }
+ inline void tcp_abort(TCP_ABORT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_abort(pcb); }
+ inline err_t tcp_output(TCP_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_output(pcb); }
+ inline void tcp_accept(TCP_ACCEPT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_accept(pcb,accept); }
+ inline struct tcp_pcb * tcp_listen(TCP_LISTEN_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_listen(pcb); }
+ 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); }
+ inline err_t tcp_bind(TCP_BIND_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_bind(pcb,ipaddr,port); }
+ inline void etharp_tmr(void) throw() { Mutex::Lock _l(_lock); return _etharp_tmr(); }
+ inline void tcp_tmr(void) throw() { Mutex::Lock _l(_lock); return _tcp_tmr(); }
+ inline u8_t pbuf_free(PBUF_FREE_SIG) throw() { Mutex::Lock _l(_lock); return _pbuf_free(p); }
+ inline struct pbuf * pbuf_alloc(PBUF_ALLOC_SIG) throw() { Mutex::Lock _l(_lock); return _pbuf_alloc(layer,length,type); }
+ inline u16_t lwip_htons(LWIP_HTONS_SIG) throw() { Mutex::Lock _l(_lock); return _lwip_htons(x); }
+ inline u16_t lwip_ntohs(LWIP_NTOHS_SIG) throw() { Mutex::Lock _l(_lock); return _lwip_ntohs(x); }
+ inline char* ipaddr_ntoa(IPADDR_NTOA_SIG) throw() { Mutex::Lock _l(_lock); return _ipaddr_ntoa(addr); }
+ inline err_t etharp_output(ETHARP_OUTPUT_SIG) throw() { Mutex::Lock _l(_lock); return _etharp_output(netif,q,ipaddr); }
+ inline err_t ethernet_input(ETHERNET_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ethernet_input(p,netif); }
+ inline void tcp_input(TCP_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _tcp_input(p,inp); }
+ inline err_t ip_input(IP_INPUT_SIG) throw() { Mutex::Lock _l(_lock); return _ip_input(p,inp); }
+
+ inline void netif_set_default(NETIF_SET_DEFAULT_SIG) throw() { Mutex::Lock _l(_lock); return _netif_set_default(netif); }
+ inline struct netif * netif_add(NETIF_ADD_SIG) throw() { Mutex::Lock _l(_lock); return _netif_add(netif,ipaddr,netmask,gw,state,init,input); }
+ inline void netif_set_up(NETIF_SET_UP_SIG) throw() { Mutex::Lock _l(_lock); return _netif_set_up(netif); }
+ inline void netif_poll(NETIF_POLL_SIG) throw() { Mutex::Lock _l(_lock); return _netif_poll(netif); }
};
+} // namespace ZeroTier
+
#endif