]> sjero.net Git - dccp2tcp/blobdiff - connections.c
Connection closing improvements and explicit cleanup code
[dccp2tcp] / connections.c
index 511692fc48ecca9cfc9049c5236c76888fae69cf..b5d04ca4a9c9591e6ea6133607fb285d0f473da1 100644 (file)
@@ -9,7 +9,7 @@ Description: Functions for differentiating different DCCP connections.
 #include "dccp2tcp.h"
 
 /*Lookup a connection. If it doesn't exist, add a new connection and return it.*/
-int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host *fwd, struct host *rev){
+int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host **fwd, struct host **rev){
        struct connection *ptr;
 
        /*Empty list*/
@@ -17,22 +17,24 @@ int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, str
                if(add_connection(src_id, dest_id, src_port, dest_port)==NULL){
                        return 1;
                }
-               fwd=&chead->A;
-               rev=&chead->B;
+               *fwd=&chead->A;
+               *rev=&chead->B;
                return 0;
        }
 
        /*Loop list looking for connection*/
        ptr=chead;
        while(ptr!=NULL){
-               if(ptr->A.id==src_id && ptr->A.port==src_port && ptr->A.state!=CLOSE){
-                       fwd=&ptr->A;
-                       rev=&ptr->B;
+               if(ptr->A.id==src_id && ptr->A.port==src_port &&
+                               !(ptr->A.state==CLOSE && ptr->B.state==CLOSE)){
+                       *fwd=&ptr->A;
+                       *rev=&ptr->B;
                        return 0;
                }
-               if(ptr->B.id==src_id && ptr->B.port==src_port && ptr->B.state!=CLOSE){
-                       fwd=&ptr->B;
-                       rev=&ptr->A;
+               if(ptr->B.id==src_id && ptr->B.port==src_port &&
+                               !(ptr->B.state==CLOSE && ptr->A.state==CLOSE)){
+                       *fwd=&ptr->B;
+                       *rev=&ptr->A;
                        return 0;
                }
                ptr=ptr->next;
@@ -43,8 +45,8 @@ int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, str
        if(ptr==NULL){
                return 1;
        }
-       fwd=&ptr->A;
-       rev=&ptr->B;
+       *fwd=&ptr->A;
+       *rev=&ptr->B;
        return 0;
 }
 
@@ -54,7 +56,7 @@ struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_por
        struct connection *prev;
 
        /*Allocate memory*/
-       if(chead){
+       if(chead==NULL){
                ptr=chead=malloc(sizeof(struct connection));
        }else{
                ptr=chead;
@@ -73,6 +75,7 @@ struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_por
 
        /*Initialize*/
        ptr->A.id=src_id;
+       ptr->A.port=src_port;
        ptr->A.state=INIT;
        ptr->B.id=dest_id;
        ptr->B.port=dest_port;
@@ -89,3 +92,17 @@ int update_state(struct host* hst, enum con_state st){
        hst->state=st;
        return 0;
 }
+
+/*Free all connections*/
+void cleanup_connections(){
+       struct connection *ptr;
+       struct connection *prev;
+       prev=ptr=chead;
+
+       while(ptr!=NULL){
+               prev=ptr;
+               ptr=ptr->next;
+               free(prev);
+       }
+return;
+}