Browse code

sip: add reference counting to transport sockets

allows for dynamic socket creation/deletion.

Raphael Coeffic authored on 28/04/2013 12:27:27
Showing 5 changed files
... ...
@@ -116,6 +116,9 @@ sip_msg::~sip_msg()
116 116
 	    delete u.reply;
117 117
 	}
118 118
     }
119
+    
120
+    if(local_socket)
121
+	dec_ref(local_socket);
119 122
 }
120 123
 
121 124
 int sip_msg::send()
... ...
@@ -80,6 +80,9 @@ sip_trans::~sip_trans()
80 80
     reset_all_timers();
81 81
     delete msg;
82 82
     delete [] retr_buf;
83
+    if(retr_socket){
84
+	dec_ref(retr_socket);
85
+    }
83 86
     if((type == TT_UAC) && to_tag.s){
84 87
 	delete [] to_tag.s;
85 88
     }
... ...
@@ -401,6 +401,13 @@ int _trans_layer::send_reply(const trans_ticket* tt, const cstring& dialog_id,
401 401
 
402 402
     sockaddr_storage remote_ip;
403 403
     trsp_socket* local_socket = req->local_socket;
404
+    if(!local_socket) {
405
+	
406
+	ERROR("request to be replied has no transport socket set\n");
407
+	delete [] reply_buf;
408
+	goto end;
409
+    }
410
+
404 411
     memcpy(&remote_ip,&req->remote_ip,sizeof(sockaddr_storage));
405 412
 
406 413
     // force_via_address option? send to 1st via
... ...
@@ -462,6 +469,8 @@ int _trans_layer::send_reply(const trans_ticket* tt, const cstring& dialog_id,
462 469
     t->retr_buf = reply_buf;
463 470
     t->retr_len = reply_len;
464 471
     memcpy(&t->retr_addr,&remote_ip,sizeof(sockaddr_storage));
472
+    inc_ref(local_socket);
473
+    if(t->retr_socket) dec_ref(t->retr_socket);
465 474
     t->retr_socket = local_socket;
466 475
 
467 476
     update_uas_reply(bucket,t,reply_code);
... ...
@@ -922,7 +931,9 @@ int _trans_layer::send_request(sip_msg* msg, trans_ticket* tt,
922 931
 
923 932
     // rco: should we overwrite the socket from the request in all cases???
924 933
     if((out_interface >= 0) && ((unsigned int)out_interface < transports.size())){
934
+	if(msg->local_socket) dec_ref(msg->local_socket);
925 935
 	msg->local_socket = transports[out_interface];
936
+	inc_ref(msg->local_socket);
926 937
     }
927 938
     // no socket yet, find one
928 939
     else if(!msg->local_socket) {
... ...
@@ -934,6 +945,7 @@ int _trans_layer::send_request(sip_msg* msg, trans_ticket* tt,
934 945
 	}
935 946
 
936 947
 	msg->local_socket = s;
948
+	inc_ref(msg->local_socket);
937 949
     }
938 950
 
939 951
     tt->_bucket = 0;
... ...
@@ -1012,6 +1024,7 @@ int _trans_layer::send_request(sip_msg* msg, trans_ticket* tt,
1012 1024
     // copy msg->remote_ip
1013 1025
     memcpy(&p_msg->remote_ip,&msg->remote_ip,sizeof(sockaddr_storage));
1014 1026
     p_msg->local_socket = msg->local_socket;
1027
+    inc_ref(p_msg->local_socket);
1015 1028
 
1016 1029
     DBG("Sending to %s:%i <%.*s...>\n",
1017 1030
 	get_addr_str(&p_msg->remote_ip).c_str(),
... ...
@@ -1188,6 +1201,7 @@ int _trans_layer::cancel(trans_ticket* tt, const cstring& hdrs)
1188 1201
 
1189 1202
     memcpy(&p_msg->remote_ip,&req->remote_ip,sizeof(sockaddr_storage));
1190 1203
     p_msg->local_socket = req->local_socket;
1204
+    inc_ref(p_msg->local_socket);
1191 1205
 
1192 1206
     DBG("Sending to %s:%i:\n<%.*s>\n",
1193 1207
 	get_addr_str(&p_msg->remote_ip).c_str(),
... ...
@@ -1656,6 +1670,8 @@ int _trans_layer::update_uac_request(trans_bucket* bucket, sip_trans*& t, sip_ms
1656 1670
 	
1657 1671
 	// copy destination address
1658 1672
 	memcpy(&t->retr_addr,&msg->remote_ip,sizeof(sockaddr_storage));
1673
+	inc_ref(msg->local_socket);
1674
+	if(t->retr_socket) dec_ref(t->retr_socket);
1659 1675
 	t->retr_socket = msg->local_socket;
1660 1676
 
1661 1677
 	// remove the message;
... ...
@@ -30,12 +30,14 @@
30 30
 #define _transport_h_
31 31
 
32 32
 #include "../AmThread.h"
33
+#include "../atomic_types.h"
33 34
 #include <sys/socket.h>
34 35
 
35 36
 #include <string>
36 37
 using std::string;
37 38
 
38 39
 class trsp_socket
40
+    : public atomic_ref_cnt
39 41
 {
40 42
 public:
41 43
     enum socket_options {
... ...
@@ -341,6 +341,8 @@ void udp_trsp::run()
341 341
 	}
342 342
 
343 343
 	s_msg->local_socket = sock;
344
+	inc_ref(sock);
345
+
344 346
 	for (cmsgptr = CMSG_FIRSTHDR(&msg);
345 347
              cmsgptr != NULL;
346 348
              cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {