#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*/
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;
if(ptr==NULL){
return 1;
}
- fwd=&ptr->A;
- rev=&ptr->B;
+ *fwd=&ptr->A;
+ *rev=&ptr->B;
return 0;
}
struct connection *prev;
/*Allocate memory*/
- if(chead){
+ if(chead==NULL){
ptr=chead=malloc(sizeof(struct connection));
}else{
ptr=chead;
/*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;
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;
+}