Browse Source

Fixed their_fd closure bug

Joseph Henry 10 years ago
parent
commit
8a8264bc39
2 changed files with 8 additions and 13 deletions
  1. 6 4
      netcon/NetconEthernetTap.cpp
  2. 2 9
      netcon/NetconService.hpp

+ 6 - 4
netcon/NetconEthernetTap.cpp

@@ -302,9 +302,9 @@ void NetconEthernetTap::closeConnection(NetconConnection *conn)
   lwipstack->_tcp_err(conn->pcb, NULL);
   lwipstack->_tcp_poll(conn->pcb, NULL, 0);
 	close(_phy.getDescriptor(conn->sock));
+	close(conn->their_fd);
 	_phy.close(conn->sock);
 	lwipstack->_tcp_close(conn->pcb);
-	client->removeConnection(conn->sock);
 	delete conn;
 }
 
@@ -564,6 +564,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
 		NetconConnection *new_conn = client->addConnection(BUFFER, tap->_phy.wrapSocket(fds[0], client));
 		client->connections.push_back(new_conn);
 		new_conn->pcb = newpcb;
+		new_conn->their_fd = fds[1];
 		int send_fd = tap->_phy.getDescriptor(client->rpc->sock);
 		int n = write(larg_fd, "z", 1);
     if(n > 0) {
@@ -577,7 +578,7 @@ err_t NetconEthernetTap::nc_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
 			}
     }
     else {
-      fprintf(stderr, "nc_accept(%d): error writing signal byte (send_fd = %d, their_fd = %d)\n", larg_fd, send_fd, fds[1]);
+      fprintf(stderr, "nc_accept(%d): error writing signal byte (send_fd = %d, perceived_fd = %d)\n", larg_fd, send_fd, fds[1]);
       return -1;
     }
     tap->lwipstack->_tcp_arg(newpcb, new Larg(tap, new_conn->sock));
@@ -797,8 +798,8 @@ void NetconEthernetTap::handle_listen(NetconClient *client, struct listen_st *li
 void NetconEthernetTap::handle_retval(NetconClient *client, unsigned char* buf)
 {
 	if(client->unmapped_conn != NULL) {
-		memcpy(&(client->unmapped_conn->their_fd), &buf[1], sizeof(int));
-		fprintf(stderr, "handle_retval(): Mapping [our=%d -> their=%d]\n", _phy.getDescriptor(client->unmapped_conn->sock), client->unmapped_conn->their_fd);
+		memcpy(&(client->unmapped_conn->perceived_fd), &buf[1], sizeof(int));
+		fprintf(stderr, "handle_retval(): Mapping [our=%d -> their=%d]\n", _phy.getDescriptor(client->unmapped_conn->sock), client->unmapped_conn->perceived_fd);
 		client->connections.push_back(client->unmapped_conn);
 		client->unmapped_conn = NULL;
 	}
@@ -824,6 +825,7 @@ void NetconEthernetTap::handle_socket(NetconClient *client, struct socket_st* so
 		socketpair(PF_LOCAL, SOCK_STREAM, 0, fds);
 		NetconConnection *new_conn = client->addConnection(BUFFER, _phy.wrapSocket(fds[0], client));
 		new_conn->pcb = pcb;
+	  new_conn->their_fd = fds[1];
 		PhySocket *sock = client->rpc->sock;
     sock_fd_write(_phy.getDescriptor(sock), fds[1]);
 		fprintf(stderr, "handle_socket(): socketpair = { our=%d, their=%d}\n", fds[0], fds[1]);

+ 2 - 9
netcon/NetconService.hpp

@@ -66,6 +66,7 @@ namespace ZeroTier {
   class NetconConnection
   {
   public:
+    int perceived_fd;
     int their_fd;
     unsigned char buf[DEFAULT_READ_BUFFER_SIZE];
     int idx;
@@ -113,7 +114,7 @@ namespace ZeroTier {
     NetconConnection *getConnectionByTheirFD(int fd)
     {
       for(size_t i=0; i<connections.size(); i++) {
-        if(connections[i]->their_fd == fd) return connections[i];
+        if(connections[i]->perceived_fd == fd) return connections[i];
       }
       return NULL;
     }
@@ -134,14 +135,6 @@ namespace ZeroTier {
       }
       return NULL;
     }
-
-    void removeConnection(PhySocket *sock)
-    {
-      for(size_t i=0; i<connections.size(); i++) {
-        if(connections[i]->sock == sock)
-          connections.erase(connections.begin() + i);
-      }
-    }
   };
 } // namespace ZeroTier