Browse code

sip: added tcp_connect_timeout & tcp_idle_timeout

Both options can be set per signalling interface ([tcp-option]_[interface name]=[time in milliseconds]). When set to 0, the feature is disabled.

Raphael Coeffic authored on 13/11/2013 17:33:32
Showing 6 changed files
... ...
@@ -770,6 +770,13 @@ static int readSIPInterface(AmConfigReader& cfg, const string& i_name)
770 770
     intf.SigSockOpts = opts;
771 771
   }
772 772
 
773
+  intf.tcp_connect_timeout =
774
+    cfg.getParameterInt("tcp_connect_timeout" + suffix,
775
+			DEFAULT_TCP_CONNECT_TIMEOUT);
776
+
777
+  intf.tcp_idle_timeout =
778
+    cfg.getParameterInt("tcp_idle_timeout" + suffix, DEFAULT_TCP_IDLE_TIMEOUT);
779
+
773 780
   if(!i_name.empty())
774 781
     intf.name = i_name;
775 782
   else
... ...
@@ -1172,9 +1179,11 @@ void AmConfig::dump_Ifs()
1172 1179
     SIP_interface& it_ref = SIP_Ifs[i];
1173 1180
 
1174 1181
     INFO("\t(%i) name='%s'" ";LocalIP='%s'" 
1175
-	 ";LocalPort='%u'" ";PublicIP='%s'",
1182
+	 ";LocalPort='%u'" ";PublicIP='%s';TCP=%u/%u",
1176 1183
 	 i,it_ref.name.c_str(),it_ref.LocalIP.c_str(),
1177
-	 it_ref.LocalPort,it_ref.PublicIP.c_str());
1184
+	 it_ref.LocalPort,it_ref.PublicIP.c_str(),
1185
+	 it_ref.tcp_connect_timeout,
1186
+	 it_ref.tcp_idle_timeout);
1178 1187
   }
1179 1188
   
1180 1189
   INFO("Signaling address map:");
... ...
@@ -110,6 +110,9 @@ struct AmConfig
110 110
      */
111 111
     unsigned int SigSockOpts;
112 112
 
113
+    unsigned int tcp_connect_timeout;
114
+    unsigned int tcp_idle_timeout;
115
+
113 116
     /** RTP interface index */
114 117
     int RtpInterface;
115 118
 
... ...
@@ -130,9 +130,13 @@ int _SipCtrlInterface::alloc_tcp_structs()
130 130
 int _SipCtrlInterface::init_tcp_servers(int if_num)
131 131
 {
132 132
     tcp_server_socket* tcp_socket = new tcp_server_socket(if_num);
133
-    // if(!AmConfig::SIP_Ifs[if_num].PublicIP.empty()) {
134
-    // 	tcp_socket->set_public_ip(AmConfig::SIP_Ifs[if_num].PublicIP);
135
-    // }
133
+
134
+    if(!AmConfig::SIP_Ifs[if_num].PublicIP.empty()) {
135
+     	tcp_socket->set_public_ip(AmConfig::SIP_Ifs[if_num].PublicIP);
136
+    }
137
+
138
+    tcp_socket->set_connect_timeout(AmConfig::SIP_Ifs[if_num].tcp_connect_timeout);
139
+    tcp_socket->set_idle_timeout(AmConfig::SIP_Ifs[if_num].tcp_idle_timeout);
136 140
 
137 141
     if(tcp_socket->bind(AmConfig::SIP_Ifs[if_num].LocalIP,
138 142
 			AmConfig::SIP_Ifs[if_num].LocalPort) < 0){
... ...
@@ -100,12 +100,7 @@ void tcp_trsp_socket::add_read_event_ul()
100 100
 
101 101
 void tcp_trsp_socket::add_read_event()
102 102
 {
103
-  // TODO: add connection idle-timeout here
104
-  struct timeval idle_timer;
105
-  idle_timer.tv_sec = 10;
106
-  idle_timer.tv_usec = 0;
107
-
108
-  event_add(read_ev, &idle_timer);
103
+  event_add(read_ev, server_sock->get_idle_timeout());
109 104
 }
110 105
 
111 106
 void tcp_trsp_socket::add_write_event_ul(struct timeval* timeout)
... ...
@@ -210,10 +205,8 @@ int tcp_trsp_socket::check_connection()
210 205
 	return -1;
211 206
       }
212 207
 
213
-      struct timeval connect_timeout;
214
-      connect_timeout.tv_sec = 0;
215
-      connect_timeout.tv_usec = 200000; /* 200ms */
216
-      add_write_event_ul(&connect_timeout);
208
+      add_write_event_ul(server_sock->get_connect_timeout());
209
+
217 210
       DBG("connect event added...");
218 211
     }
219 212
     else {
... ...
@@ -282,7 +275,8 @@ void tcp_trsp_socket::generate_transport_errors()
282 275
 void tcp_trsp_socket::on_read(short ev)
283 276
 {
284 277
   if(ev & EV_TIMEOUT) {
285
-    DBG("************ idle timeout **********");
278
+    DBG("************ idle timeout: closing connection **********");
279
+    close();
286 280
     return;
287 281
   }
288 282
 
... ...
@@ -616,6 +610,38 @@ int tcp_server_socket::send(const sockaddr_storage* sa, const char* msg,
616 610
   return ret;
617 611
 }
618 612
 
613
+void tcp_server_socket::set_connect_timeout(unsigned int ms)
614
+{
615
+  connections_mut.lock();
616
+  connect_timeout.tv_sec = ms / 1000;
617
+  connect_timeout.tv_usec = (ms % 1000) * 1000;
618
+  connections_mut.unlock();
619
+}
620
+
621
+void tcp_server_socket::set_idle_timeout(unsigned int ms)
622
+{
623
+  connections_mut.lock();
624
+  idle_timeout.tv_sec = ms / 1000;
625
+  idle_timeout.tv_usec = (ms % 1000) * 1000;
626
+  connections_mut.unlock();
627
+}
628
+
629
+struct timeval* tcp_server_socket::get_connect_timeout()
630
+{
631
+  if(connect_timeout.tv_sec || connect_timeout.tv_usec)
632
+    return &connect_timeout;
633
+
634
+  return NULL;
635
+}
636
+
637
+struct timeval* tcp_server_socket::get_idle_timeout()
638
+{
639
+  if(idle_timeout.tv_sec || idle_timeout.tv_usec)
640
+    return &idle_timeout;
641
+
642
+  return NULL;
643
+}
644
+
619 645
 /** @see trsp_socket */
620 646
 
621 647
 tcp_trsp::tcp_trsp(tcp_server_socket* sock)
... ...
@@ -180,6 +180,16 @@ class tcp_server_socket: public trsp_socket
180 180
   AmMutex                 send_q_mut;
181 181
   deque<tcp_trsp_socket*> send_q;
182 182
 
183
+  /**
184
+   * Timeout while connecting to a remote peer.
185
+   */
186
+  struct timeval connect_timeout;
187
+
188
+  /**
189
+   * Idle Timeout before closing a connection.
190
+   */
191
+  struct timeval idle_timeout;
192
+
183 193
 public:
184 194
   tcp_server_socket(unsigned short if_num);
185 195
   ~tcp_server_socket() {}
... ...
@@ -198,6 +208,22 @@ public:
198 208
 
199 209
   void add_connection(tcp_trsp_socket* client_sock);
200 210
   void remove_connection(tcp_trsp_socket* client_sock);
211
+
212
+  /**
213
+   * Set timeout in milliseconds for the connection
214
+   * establishement handshake.
215
+   */
216
+  void set_connect_timeout(unsigned int ms);
217
+
218
+  /**
219
+   * Set idle timeout in milliseconds for news connections.
220
+   * If during this period of time no packet is received,
221
+   * the connection will be closed.
222
+   */
223
+  void set_idle_timeout(unsigned int ms);
224
+
225
+  struct timeval* get_connect_timeout();
226
+  struct timeval* get_idle_timeout();
201 227
 };
202 228
 
203 229
 class tcp_trsp: public transport
... ...
@@ -36,6 +36,9 @@
36 36
 #include <string>
37 37
 using std::string;
38 38
 
39
+#define DEFAULT_TCP_CONNECT_TIMEOUT 2000 /* 2 seconds */
40
+#define DEFAULT_TCP_IDLE_TIMEOUT 3600000 /* 1 hour */
41
+
39 42
 class trsp_socket
40 43
     : public atomic_ref_cnt
41 44
 {