]> sjero.net Git - dccp2tcp/blob - connections.c
Connection closing improvements and explicit cleanup code
[dccp2tcp] / connections.c
1 /******************************************************************************
2 Author: Samuel Jero
3
4 Date: 7/2011
5
6 Description: Functions for differentiating different DCCP connections.
7
8 ******************************************************************************/
9 #include "dccp2tcp.h"
10
11 /*Lookup a connection. If it doesn't exist, add a new connection and return it.*/
12 int get_host(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port, struct host **fwd, struct host **rev){
13         struct connection *ptr;
14
15         /*Empty list*/
16         if(chead==NULL){
17                 if(add_connection(src_id, dest_id, src_port, dest_port)==NULL){
18                         return 1;
19                 }
20                 *fwd=&chead->A;
21                 *rev=&chead->B;
22                 return 0;
23         }
24
25         /*Loop list looking for connection*/
26         ptr=chead;
27         while(ptr!=NULL){
28                 if(ptr->A.id==src_id && ptr->A.port==src_port &&
29                                 !(ptr->A.state==CLOSE && ptr->B.state==CLOSE)){
30                         *fwd=&ptr->A;
31                         *rev=&ptr->B;
32                         return 0;
33                 }
34                 if(ptr->B.id==src_id && ptr->B.port==src_port &&
35                                 !(ptr->B.state==CLOSE && ptr->A.state==CLOSE)){
36                         *fwd=&ptr->B;
37                         *rev=&ptr->A;
38                         return 0;
39                 }
40                 ptr=ptr->next;
41         }
42
43         /*Add new connection*/
44         ptr=add_connection(src_id, dest_id, src_port, dest_port);
45         if(ptr==NULL){
46                 return 1;
47         }
48         *fwd=&ptr->A;
49         *rev=&ptr->B;
50         return 0;
51 }
52
53 /*Add a connection. Return it. On failure, return NULL*/
54 struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port){
55         struct connection *ptr;
56         struct connection *prev;
57
58         /*Allocate memory*/
59         if(chead==NULL){
60                 ptr=chead=malloc(sizeof(struct connection));
61         }else{
62                 ptr=chead;
63                 prev=chead;
64                 while(ptr!=NULL){
65                         prev=ptr;
66                         ptr=ptr->next;
67                 }
68                 ptr=prev->next=malloc(sizeof(struct connection));
69         }
70
71         if(ptr==NULL){
72                 dbgprintf(0,"Error: Couldn't allocate Memory\n");
73                 exit(1);
74         }
75
76         /*Initialize*/
77         ptr->A.id=src_id;
78         ptr->A.port=src_port;
79         ptr->A.state=INIT;
80         ptr->B.id=dest_id;
81         ptr->B.port=dest_port;
82         ptr->B.state=INIT;
83
84         return ptr;
85 }
86
87 /*Update the state on a host*/
88 int update_state(struct host* hst, enum con_state st){
89         if(!hst){
90                 return 1;
91         }
92         hst->state=st;
93         return 0;
94 }
95
96 /*Free all connections*/
97 void cleanup_connections(){
98         struct connection *ptr;
99         struct connection *prev;
100         prev=ptr=chead;
101
102         while(ptr!=NULL){
103                 prev=ptr;
104                 ptr=ptr->next;
105                 free(prev);
106         }
107 return;
108 }