filled out hendlers

This commit is contained in:
Joseph Henry 2015-09-10 20:34:48 -04:00
parent e4dc46741f
commit cc4a2bb0c3
4 changed files with 58 additions and 80 deletions

View File

@ -220,14 +220,11 @@ void NetconEthernetTap::threadMain()
unsigned long curr_time; unsigned long curr_time;
unsigned long since_tcp; unsigned long since_tcp;
unsigned long since_etharp; unsigned long since_etharp;
struct timeval tv; struct timeval tv;
//struct timeval tv_sel;
while (_run) { while (_run) {
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000; curr_time = (unsigned long)(tv.tv_sec) * 1000 + (unsigned long)(tv.tv_usec) / 1000;
since_tcp = curr_time - prev_tcp_time; since_tcp = curr_time - prev_tcp_time;
since_etharp = curr_time - prev_etharp_time; since_etharp = curr_time - prev_etharp_time;
int min_time = min(since_tcp, since_etharp) * 1000; // usec int min_time = min(since_tcp, since_etharp) * 1000; // usec
@ -237,16 +234,11 @@ void NetconEthernetTap::threadMain()
prev_tcp_time = curr_time+1; prev_tcp_time = curr_time+1;
lwipstack->tcp_tmr(); lwipstack->tcp_tmr();
} }
if(since_etharp > etharp_time) if(since_etharp > etharp_time)
{ {
prev_etharp_time = curr_time; prev_etharp_time = curr_time;
lwipstack->etharp_tmr(); lwipstack->etharp_tmr();
} }
// should be set every time since tv_sel is modified after each select() call
//tv_sel.tv_sec = 0;
//tv_sel.tv_usec = min_time;
_phy.poll(min_time * 1000); // conversion from usec to millisec, TODO: double check _phy.poll(min_time * 1000); // conversion from usec to millisec, TODO: double check
} }
@ -256,12 +248,24 @@ void NetconEthernetTap::threadMain()
void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr) void NetconEthernetTap::phyOnSocketPairEndpointClose(PhySocket *sock, void **uptr)
{ {
NetconClient *client = (NetconClient*)*uptr;
client->closeConnection(sock);
} }
void NetconEthernetTap::phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n) void NetconEthernetTap::phyOnSocketPairEndpointData(PhySocket *sock, void **uptr, void *buf, unsigned long n)
{ {
int r;
NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock);
if(c) {
if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
c->idx += r;
handle_write(c);
}
}
}
} }
void NetconEthernetTap::phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr) void NetconEthernetTap::phyOnSocketPairEndpointWritable(PhySocket *sock, void **uptr)
{ {
@ -290,60 +294,41 @@ void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
{ {
unsigned char *buf = (unsigned char*)data; unsigned char *buf = (unsigned char*)data;
NetconClient *client = (NetconClient*)*uptr;
NetconConnection *c = ((NetconClient*)*uptr)->getConnection(sock); switch(buf[0])
int r;
if(c->type == BUFFER) {
if(c) {
if(c->idx < DEFAULT_READ_BUFFER_SIZE) {
if((r = read(_phy.getDescriptor(c->sock), (&c->buf)+c->idx, DEFAULT_READ_BUFFER_SIZE-(c->idx))) > 0) {
c->idx += r;
handle_write(c);
}
}
}
else {
// can't find connection for this fd
}
}
if(c->type == RPC)
{ {
NetconClient *client = (NetconClient*)*uptr; case RPC_SOCKET:
switch(buf[0]) struct socket_st socket_rpc;
{ memcpy(&socket_rpc, &buf[1], sizeof(struct socket_st));
case RPC_SOCKET: client->tid = socket_rpc.__tid;
struct socket_st socket_rpc; handle_socket(client, &socket_rpc);
memcpy(&socket_rpc, &buf[1], sizeof(struct socket_st)); break;
client->tid = socket_rpc.__tid; case RPC_LISTEN:
handle_socket(client, &socket_rpc); struct listen_st listen_rpc;
break; memcpy(&listen_rpc, &buf[1], sizeof(struct listen_st));
case RPC_LISTEN: client->tid = listen_rpc.__tid;
struct listen_st listen_rpc; handle_listen(client, &listen_rpc);
memcpy(&listen_rpc, &buf[1], sizeof(struct listen_st)); break;
client->tid = listen_rpc.__tid; case RPC_BIND:
handle_listen(client, &listen_rpc); struct bind_st bind_rpc;
break; memcpy(&bind_rpc, &buf[1], sizeof(struct bind_st));
case RPC_BIND: client->tid = bind_rpc.__tid;
struct bind_st bind_rpc; handle_bind(client, &bind_rpc);
memcpy(&bind_rpc, &buf[1], sizeof(struct bind_st)); break;
client->tid = bind_rpc.__tid; case RPC_KILL_INTERCEPT:
handle_bind(client, &bind_rpc); client->closeClient();
break; break;
case RPC_KILL_INTERCEPT: case RPC_CONNECT:
client->closeClient(); struct connect_st connect_rpc;
break; memcpy(&connect_rpc, &buf[1], sizeof(struct connect_st));
case RPC_CONNECT: client->tid = connect_rpc.__tid;
struct connect_st connect_rpc; handle_connect(client, &connect_rpc);
memcpy(&connect_rpc, &buf[1], sizeof(struct connect_st)); break;
client->tid = connect_rpc.__tid; case RPC_FD_MAP_COMPLETION:
handle_connect(client, &connect_rpc); handle_retval(client, buf);
break; break;
case RPC_FD_MAP_COMPLETION: default:
handle_retval(client, buf); break;
break;
default:
break;
}
} }
} }
@ -403,20 +388,13 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
struct pbuf* q = p; struct pbuf* q = p;
int our_fd = tap->_phy.getDescriptor(c->sock); int our_fd = tap->_phy.getDescriptor(c->sock);
if(c) { if(!c) {
//dwr(c->owner->tid, "nc_recved(%d)\n", (intptr_t)arg);
}
else {
//dwr(-1, "nc_recved(%d): unable to locate connection\n", (intptr_t)arg);
return ERR_OK; // ? return ERR_OK; // ?
} }
if(p == NULL) { if(p == NULL) {
//dwr(c->owner->tid, "nc_recved()\n");
if(c) { if(c) {
//dwr(c->owner->tid, "closing connection\n");
nc_close(tpcb); nc_close(tpcb);
close(our_fd); /* TODO: Check logic */ close(our_fd); // TODO: Check logic
//nc_service->remove_connection(c);
c->owner->closeConnection(c); c->owner->closeConnection(c);
} }
else { else {

View File

@ -86,6 +86,7 @@ public:
private: private:
// LWIP callbacks
static err_t nc_poll(void* arg, struct tcp_pcb *tpcb); static err_t nc_poll(void* arg, struct tcp_pcb *tpcb);
static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err); static err_t nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err); static err_t nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
@ -132,21 +133,15 @@ private:
return conn_addr; return conn_addr;
} }
void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int); void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
void *_arg; void *_arg;
// client helpers // Client helpers
NetconConnection *getConnectionByThisFD(int fd); NetconConnection *getConnectionByThisFD(int fd);
NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb); NetconConnection *getConnectionByPCB(struct tcp_pcb *pcb);
NetconClient *getClientByPCB(struct tcp_pcb *pcb); NetconClient *getClientByPCB(struct tcp_pcb *pcb);
void closeClient(NetconClient *client); void closeClient(NetconClient *client);
// Logging helper
Phy<NetconEthernetTap *> _phy; Phy<NetconEthernetTap *> _phy;
PhySocket *_unixListenSocket; PhySocket *_unixListenSocket;

View File

@ -136,6 +136,11 @@ namespace ZeroTier {
// -- PhySocket // -- PhySocket
} }
void closeConnection(PhySocket *sock)
{
}
void closeClient() void closeClient()
{ {