]> sjero.net Git - dccp2tcp/blob - connections.c
Add GNU GPL headers to all files
[dccp2tcp] / connections.c
1 /******************************************************************************
2 Utility to convert a DCCP flow to a TCP flow for DCCP analysis via
3                 tcptrace. Functions for differentiating different DCCP connections.
4
5 Copyright (C) 2012  Samuel Jero <sj323707@ohio.edu>
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 Author: Samuel Jero <sj323707@ohio.edu>
21 Date: 11/2012
22
23 Notes:
24         1)CCID2 ONLY
25         2)DCCP MUST use 48 bit sequence numbers
26         3)Checksums are not computed (they are zeroed)
27         4)DCCP DATA packets are not implemented (Linux doesn't use them)
28         5)DCCP Ack packets show up as TCP packets containing one byte
29 ******************************************************************************/
30 #include "dccp2tcp.h"
31
32 /*Lookup a connection. If it doesn't exist, add a new connection and return it.*/
33 int get_host(u_char *src_id, u_char* dest_id, int id_len, int src_port, int dest_port,
34                 struct host **fwd, struct host **rev){
35         struct connection *ptr;
36
37         /*Empty list*/
38         if(chead==NULL){
39                 if(add_connection(src_id, dest_id, id_len, src_port, dest_port)==NULL){
40                         return 1;
41                 }
42                 *fwd=&chead->A;
43                 *rev=&chead->B;
44                 return 0;
45         }
46
47         /*Loop list looking for connection*/
48         ptr=chead;
49         while(ptr!=NULL){
50                 if(memcmp(ptr->A.id,src_id,id_len)==0 && ptr->A.port==src_port &&
51                                 !(ptr->A.state==CLOSE && ptr->B.state==CLOSE)){
52                         *fwd=&ptr->A;
53                         *rev=&ptr->B;
54                         return 0;
55                 }
56                 if(memcmp(ptr->B.id,src_id,id_len)==0 && ptr->B.port==src_port &&
57                                 !(ptr->B.state==CLOSE && ptr->A.state==CLOSE)){
58                         *fwd=&ptr->B;
59                         *rev=&ptr->A;
60                         return 0;
61                 }
62                 ptr=ptr->next;
63         }
64
65         /*Add new connection*/
66         ptr=add_connection(src_id, dest_id, id_len, src_port, dest_port);
67         if(ptr==NULL){
68                 return 1;
69         }
70         *fwd=&ptr->A;
71         *rev=&ptr->B;
72         return 0;
73 }
74
75 /*Add a connection. Return it. On failure, return NULL*/
76 struct connection *add_connection(u_char *src_id, u_char* dest_id, int id_len, int src_port, int dest_port){
77         struct connection *ptr;
78         struct connection *prev;
79
80         /*Allocate memory*/
81         if(chead==NULL){
82                 ptr=chead=malloc(sizeof(struct connection));
83         }else{
84                 ptr=chead;
85                 prev=chead;
86                 while(ptr!=NULL){
87                         prev=ptr;
88                         ptr=ptr->next;
89                 }
90                 ptr=prev->next=malloc(sizeof(struct connection));
91         }
92
93         if(ptr==NULL){
94                 dbgprintf(0,"Error: Couldn't allocate Memory\n");
95                 exit(1);
96         }
97
98         /*Initialize*/
99         ptr->A.id=malloc(id_len);
100         ptr->B.id=malloc(id_len);
101         if(ptr->A.id==NULL||ptr->B.id==NULL){
102                 dbgprintf(0,"Error: Couldn't allocate Memory\n");
103                 exit(1);
104         }
105         memcpy(ptr->A.id,src_id,id_len);
106         ptr->A.port=src_port;
107         ptr->A.state=INIT;
108         memcpy(ptr->B.id,dest_id,id_len);
109         ptr->B.port=dest_port;
110         ptr->B.state=INIT;
111
112         return ptr;
113 }
114
115 /*Update the state on a host*/
116 int update_state(struct host* hst, enum con_state st){
117         if(!hst){
118                 return 1;
119         }
120         hst->state=st;
121         return 0;
122 }
123
124 /*Free all connections*/
125 void cleanup_connections(){
126         struct connection *ptr;
127         struct connection *prev;
128         prev=ptr=chead;
129
130         while(ptr!=NULL){
131                 prev=ptr;
132                 free(ptr->A.id);
133                 free(ptr->B.id);
134                 ptr=ptr->next;
135                 free(prev);
136         }
137 return;
138 }