]> sjero.net Git - dccp2tcp/blob - connections.c
Multiple Connection Support
[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 && ptr->A.state!=CLOSE){
29                         fwd=&ptr->A;
30                         rev=&ptr->B;
31                         return 0;
32                 }
33                 if(ptr->B.id==src_id && ptr->B.port==src_port && ptr->B.state!=CLOSE){
34                         fwd=&ptr->B;
35                         rev=&ptr->A;
36                         return 0;
37                 }
38                 ptr=ptr->next;
39         }
40
41         /*Add new connection*/
42         ptr=add_connection(src_id, dest_id, src_port, dest_port);
43         if(ptr==NULL){
44                 return 1;
45         }
46         fwd=&ptr->A;
47         rev=&ptr->B;
48         return 0;
49 }
50
51 /*Add a connection. Return it. On failure, return NULL*/
52 struct connection *add_connection(uint32_t src_id, uint32_t dest_id, int src_port, int dest_port){
53         struct connection *ptr;
54         struct connection *prev;
55
56         /*Allocate memory*/
57         if(chead){
58                 ptr=chead=malloc(sizeof(struct connection));
59         }else{
60                 ptr=chead;
61                 prev=chead;
62                 while(ptr!=NULL){
63                         prev=ptr;
64                         ptr=ptr->next;
65                 }
66                 ptr=prev->next=malloc(sizeof(struct connection));
67         }
68
69         if(ptr==NULL){
70                 dbgprintf(0,"Error: Couldn't allocate Memory\n");
71                 exit(1);
72         }
73
74         /*Initialize*/
75         ptr->A.id=src_id;
76         ptr->A.state=INIT;
77         ptr->B.id=dest_id;
78         ptr->B.port=dest_port;
79         ptr->B.state=INIT;
80
81         return ptr;
82 }
83
84 /*Update the state on a host*/
85 int update_state(struct host* hst, enum con_state st){
86         if(!hst){
87                 return 1;
88         }
89         hst->state=st;
90         return 0;
91 }