--- calltable.cpp.orig	2007-03-22 16:16:01.000000000 +0100
+++ calltable.cpp	2011-04-24 12:29:17.000000000 +0200
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <unistd.h>
 #include "calltable.h"
 
 #define MAX(x,y) ((x) > (y) ? (x) : (y))
@@ -35,6 +36,7 @@
 {
     table_size=0;
     table=(calltable_element*)malloc(sizeof(calltable_element)*calltable_max);
+    erase_non_t38=0;
 }
 
 int calltable::add(
@@ -42,7 +44,7 @@
 	unsigned long call_id_len,
 	time_t time)
 {
-    unsigned long idx=-1,i,found_empty=0;
+    uint32_t idx=(uint32_t)-1,i,found_empty=0;
     for (i=0;i<table_size;i++){
 	if(table[i].is_used==0){
 	    idx=i;
@@ -55,6 +57,8 @@
 	table_size++;
     }
     table[idx].is_used=1;
+    table[idx].had_t38=0;
+    table[idx].had_bye=0;
     memcpy(table[idx].call_id,call_id,MIN(call_id_len,32));
     table[idx].call_id_len=call_id_len;
     table[idx].ip_n=0;
@@ -120,6 +124,30 @@
     return -1;
 }
 
+//returns 1 if found or 0 if not found, and updates idx_leg and idx_rtp
+int calltable::find_ip_port_ssrc(
+            in_addr_t addr,
+            unsigned short port,
+            uint32_t ssrc,
+            int *idx_leg,
+            int *idx_rtp)
+{
+    int i_leg,i_rtp;
+    for(i_leg=0;i_leg<(int)table_size;i_leg++){
+        for(i_rtp=0;i_rtp<table[i_leg].ip_n;i_rtp++){
+            if(table[i_leg].port[i_rtp]==port && table[i_leg].ip[i_rtp]==addr){
+                if (!table[i_leg].had_bye || table[i_leg].ssrc[i_rtp]==ssrc){
+                    table[i_leg].ssrc[i_rtp]=ssrc;
+                    *idx_leg=i_leg;
+                    *idx_rtp=i_rtp;
+                    return 1;
+                }
+            }
+        }
+    }
+    return 0;
+}
+
 int calltable::do_cleanup( time_t currtime ){
     int idx;
     for(idx=0;idx<(int)table_size;idx++){
@@ -130,9 +158,12 @@
 	    if (table[idx].f!=NULL){
 		fclose(table[idx].f);
 	    }
+	    if (erase_non_t38 && !table[idx].had_t38){
+	        unlink(table[idx].fn_pcap);
+	    }
 	    memset((void*)&table[idx],0,sizeof(table[idx]));
 	    table[idx].is_used=0;
-	} 
+	}
     }
     return 0;
 }
