refactor in progress

This commit is contained in:
Joseph Henry 2015-09-10 18:19:43 -04:00
parent c1f1530d54
commit e391bc004b
3 changed files with 57 additions and 47 deletions

View file

@ -165,19 +165,33 @@ NetconConnection *NetconEthernetTap::getConnectionByPCB(struct tcp_pcb *pcb)
return c; return c;
} }
} }
return NULL;
} }
NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd) NetconConnection *NetconEthernetTap::getConnectionByThisFD(int fd)
{ {
NetconConnection *c; NetconConnection *c;
for(int i=0; i<clients.size(); i++) { for(int i=0; i<clients.size(); i++) {
c = clients[i]->getConnectionByThisFD(fd); for(int j=0; j<clients[i]->connections[j].size(); j++) {
if(c) { if(_phy.getDescriptor(clients[i]->connection[j]->sock) == fd) {
return c; return clients[i]->connections[j];
}
} }
} }
return NULL;
} }
NetconClient *NetconEthernetTap::getClientByPCB(struct tcp_pcb *pcb)
{
for(int i=0; i<clients.size(); i++) {
if(clients[i].containsPCB(pcb)) {
return clients[i];
}
}
return NULL;
}
void NetconEthernetTap::closeClient(NetconClient *client) void NetconEthernetTap::closeClient(NetconClient *client)
{ {
// erase from clients vector // erase from clients vector
@ -357,41 +371,35 @@ err_t NetconEthernetTap::nc_poll(void* arg, struct tcp_pcb *tpcb)
err_t NetconEthernetTap::nc_accept(void* arg, struct tcp_pcb *newpcb, err_t err) err_t NetconEthernetTap::nc_accept(void* arg, struct tcp_pcb *newpcb, err_t err)
{ {
NetconConnection *c = nc_service->get_connection_by_buf_sock((intptr_t)arg); NetconClient *client = getConnectionByThisFD((intptr_t)arg);
if(c && c->owner) {
// Generate new socketpair and Connection. Use newly-allocated PCB if(client)
int fd[2]; {
socketpair(PF_LOCAL, SOCK_STREAM, 0, fd); int *their_fd;
NetconConnection *new_connection = nc_service->add_connection(c->owner, c->owner->tid, fd[0], -1, newpcb); int our_fd;
//dwr(c->owner->tid, "socketpair { fd[0]=%d, fd[1]=%d }\n", fd[0], fd[1]); PhySocket *new_sock = _phy.createSocketPair(*their_fd, client)
if(new_connection == NULL) { NetconConnection c = addConnection(NetconConnectionType.BUFFER, new_sock);
//printf("error adding new connection\n"); our_fd = _phy.getDescriptor(c->sock);
return -1; if(c == NULL) return -1;
}
new_connection->owner->unmapped_conn = new_connection; c->owner->unmapped_conn = c;
// write byte to let accept call know we have a new connection // write byte to let accept call know we have a new connection
int n = write(c->our_fd, "z", 1); int n = write(our_fd, "z", 1);
if(n > 0) { if(n > 0) {
//dwr(c->owner->tid, "sending socketpair fd... %d\n", fd[1]); sock_fd_write(c->owner->rpc, *their_fd);
sock_fd_write(c->owner->rpc, fd[1]);
} }
else { else {
//dwr(c->owner->tid, "nc_accept() - unknown error writing signal byte to listening socket\n"); // unknown error writing signal byte to listening socket
return -1; return -1;
} }
// Set PCB-specific callbacks lwipstack->tcp_arg(newpcb, (void*)(intptr_t)(our_fd));
//dwr(c->owner->tid, "tcp_arg(pcb, %d)\n", new_connection->our_fd);
lwipstack->tcp_arg(newpcb, (void*)(intptr_t)(new_connection->our_fd));
lwipstack->tcp_recv(newpcb, nc_recved); lwipstack->tcp_recv(newpcb, nc_recved);
lwipstack->tcp_err(newpcb, nc_err); lwipstack->tcp_err(newpcb, nc_err);
lwipstack->tcp_sent(newpcb, nc_sent); lwipstack->tcp_sent(newpcb, nc_sent);
lwipstack->tcp_poll(newpcb, nc_poll, APPLICATION_POLL_FREQ); lwipstack->tcp_poll(newpcb, nc_poll, APPLICATION_POLL_FREQ);
tcp_accepted(c->pcb); tcp_accepted(c->pcb);
return ERR_OK; return ERR_OK;
} }
else {
//dwr("can't locate Connection object for PCB\n");
}
return -1; return -1;
} }
@ -399,7 +407,9 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
{ {
int n; int n;
struct pbuf* q = p; struct pbuf* q = p;
NetconConnection *c = nc_service->get_connection_by_buf_sock((intptr_t)arg); NetconConnection *c = getConnectionByPCB(tpcb); // TODO: make sure this works, if not, use arg as "buf sock"
int our_fd = _phy.getDescriptor(c->sock);
if(c) { if(c) {
//dwr(c->owner->tid, "nc_recved(%d)\n", (intptr_t)arg); //dwr(c->owner->tid, "nc_recved(%d)\n", (intptr_t)arg);
} }
@ -414,8 +424,9 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
if(c) { if(c) {
//dwr(c->owner->tid, "closing connection\n"); //dwr(c->owner->tid, "closing connection\n");
nc_close(tpcb); nc_close(tpcb);
close(c->our_fd); /* TODO: Check logic */ close(our_fd); /* TODO: Check logic */
nc_service->remove_connection(c); //nc_service->remove_connection(c);
c->owner->close(c);
} }
else { else {
//dwr(-1, "can't locate connection via (arg)\n"); //dwr(-1, "can't locate connection via (arg)\n");
@ -427,7 +438,7 @@ err_t NetconEthernetTap::nc_recved(void *arg, struct tcp_pcb *tpcb, struct pbuf
//dwr(c->owner->tid, "writing data to mapped sock (%d)\n", c->our_fd); //dwr(c->owner->tid, "writing data to mapped sock (%d)\n", c->our_fd);
if(p->len <= 0) if(p->len <= 0)
break; // ? break; // ?
if((n = write(c->our_fd, p->payload, p->len)) > 0) { if((n = write(our_fd, p->payload, p->len)) > 0) {
if(n < p->len) { if(n < p->len) {
//dwr(c->owner->tid, "ERROR: unable to write entire pbuf to buffer\n"); //dwr(c->owner->tid, "ERROR: unable to write entire pbuf to buffer\n");
} }
@ -446,8 +457,9 @@ void NetconEthernetTap::nc_err(void *arg, err_t err)
{ {
NetconConnection *c = getConnectionByThisFD((intptr)arg); NetconConnection *c = getConnectionByThisFD((intptr)arg);
if(c) { if(c) {
nc_service->remove_connection(c); //nc_service->remove_connection(c);
//tcp_close(c->pcb); c->owner->close(c);
//tcp_close(c->pcb);
} }
else { else {
// can't locate connection object for PCB // can't locate connection object for PCB
@ -456,7 +468,7 @@ void NetconEthernetTap::nc_err(void *arg, err_t err)
void NetconEthernetTap::nc_close(struct tcp_pcb* tpcb) void NetconEthernetTap::nc_close(struct tcp_pcb* tpcb)
{ {
NetconConnection *c = getConnectionByPCB(tpcb); //NetconConnection *c = getConnectionByPCB(tpcb);
lwipstack->tcp_arg(tpcb, NULL); lwipstack->tcp_arg(tpcb, NULL);
lwipstack->tcp_sent(tpcb, NULL); lwipstack->tcp_sent(tpcb, NULL);
lwipstack->tcp_recv(tpcb, NULL); lwipstack->tcp_recv(tpcb, NULL);
@ -472,9 +484,9 @@ err_t NetconEthernetTap::nc_send(struct tcp_pcb *tpcb)
err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len) err_t NetconEthernetTap::nc_sent(void* arg, struct tcp_pcb *tpcb, u16_t len)
{ {
NetconConnection *c = getConnectionByPCB(tpcb); //NetconConnection *c = _phy->getConnectionByPCB(tpcb);
if(c) //if(c)
c->data_sent += len; //c->data_sent += len;
return len; return len;
} }
@ -566,10 +578,10 @@ void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* so
struct tcp_pcb *pcb = lwipstack->tcp_new(); struct tcp_pcb *pcb = lwipstack->tcp_new();
if(pcb != NULL) { if(pcb != NULL) {
int *their_fd; int *their_fd;
client->addConnection(NetconConnectionType.BUFFER, _phy.createSocketPair(&their_fd, client)); NetconConnection *new_conn = client->addConnection(NetconConnectionType.BUFFER, _phy.createSocketPair(*their_fd, client));
new_conn->their_fd = their_fd; new_conn->their_fd = *their_fd;
new_conn->pcb = pcb; new_conn->pcb = pcb;
sock_fd_write(_phy.getDescriptor(client->rpc->sock), their_fd); sock_fd_write(_phy.getDescriptor(client->rpc->sock), *their_fd);
client->unmapped_conn = new_conn; client->unmapped_conn = new_conn;
} }
else { else {
@ -585,13 +597,14 @@ void NetconEthernetTap::handle_connect(NetconClient *client, struct connect_st*
int conn_port = lwipstack->ntohs(connaddr->sin_port); int conn_port = lwipstack->ntohs(connaddr->sin_port);
ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr); ip_addr_t conn_addr = convert_ip((struct sockaddr_in *)&connect_rpc->__addr);
NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd); NetconConnection *c = client->getConnectionByTheirFD(connect_rpc->__fd);
int our_fd = _phy.getDescriptor(c->sock);
if(c!= NULL) { if(c!= NULL) {
lwipstack->tcp_sent(c->pcb, nc_sent); // FIXME: Move? lwipstack->tcp_sent(c->pcb, nc_sent); // FIXME: Move?
lwipstack->tcp_recv(c->pcb, nc_recved); lwipstack->tcp_recv(c->pcb, nc_recved);
lwipstack->tcp_err(c->pcb, nc_err); lwipstack->tcp_err(c->pcb, nc_err);
lwipstack->tcp_poll(c->pcb, nc_poll, APPLICATION_POLL_FREQ); lwipstack->tcp_poll(c->pcb, nc_poll, APPLICATION_POLL_FREQ);
lwipstack->tcp_arg(c->pcb,(void*)(intptr_t)c->our_fd); lwipstack->tcp_arg(c->pcb,(void*)(intptr_t)our_fd);
int err = 0; int err = 0;
if((err = lwipstack->tcp_connect(c->pcb,&conn_addr,conn_port, nc_connected)) < 0) if((err = lwipstack->tcp_connect(c->pcb,&conn_addr,conn_port, nc_connected)) < 0)

View file

@ -122,6 +122,7 @@ private:
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);
void closeClient(NetconClient *client); void closeClient(NetconClient *client);
// Logging helper // Logging helper

View file

@ -96,6 +96,7 @@ namespace ZeroTier {
for(int i=0; i<connections.size(); i++) { for(int i=0; i<connections.size(); i++) {
if(connections[i]->their_fd == fd) { return connections[i]; } if(connections[i]->their_fd == fd) { return connections[i]; }
} }
return NULL;
} }
// //
@ -104,6 +105,7 @@ namespace ZeroTier {
for(int i=0; i<connections.size(); i++) { for(int i=0; i<connections.size(); i++) {
if(connections[i]->pcb = pcb) { return connections[i]; } if(connections[i]->pcb = pcb) { return connections[i]; }
} }
return NULL;
} }
NetconConnection *containsPCB(struct tcp_pcb *pcb) NetconConnection *containsPCB(struct tcp_pcb *pcb)
@ -111,13 +113,7 @@ namespace ZeroTier {
for(int i=0; i<connections.size(); i++) { for(int i=0; i<connections.size(); i++) {
if(connections[i]->pcb = pcb) { return connections[i]; } if(connections[i]->pcb = pcb) { return connections[i]; }
} }
} return NULL;
NetconConnection *containsPCB(struct tcp_pcb *pcb)
{
for(int i=0; i<connections.size(); i++) {
if(connections[i]->pcb = pcb) { return connections[i]; }
}
} }
void close() void close()