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