Browse code

SO_REUSEADDR before calling bind to allow server restart, Nagle set on the (hopefuly) correct socket

Jiri Kuthan authored on 29/03/2003 20:16:12
Showing 1 changed files
... ...
@@ -32,6 +32,9 @@
32 32
  *  2003-01-20  locking fixes, hashtables (andrei)
33 33
  *  2003-02-20  s/lock_t/gen_lock_t/ to avoid a conflict on solaris (andrei)
34 34
  *  2003-02-25  Nagle is disabled if -DDISABLE_NAGLE (andrei)
35
+ *  2003-03-29  SO_REUSEADDR before calling bind to allow
36
+ *              server restart, Nagle set on the (hopefuly) 
37
+ *              correct socket
35 38
  */
36 39
 
37 40
 
... ...
@@ -161,7 +164,10 @@ struct tcp_connection* tcpconn_connect(union sockaddr_union* server)
161 161
 #ifdef DISABLE_NAGLE
162 162
 	flag=1;
163 163
 	if ( (tcp_proto_no!=-1) && 
164
-		  (setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
164
+/* fix:	I used to get here 
165
+ * ERROR: tcp_connect: could not disable Nagle: Protocol not available	  
166
+ * 			(setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY, */
167
+			  (setsockopt(s, tcp_proto_no , TCP_NODELAY,
165 168
 					&flag, sizeof(flag))<0) ){
166 169
 		LOG(L_ERR, "ERROR: tcp_connect: could not disable Nagle: %s\n",
167 170
 				strerror(errno));
... ...
@@ -441,6 +447,9 @@ void tcpconn_timeout(fd_set* set)
441 441
 int tcp_init(struct socket_info* sock_info)
442 442
 {
443 443
 	union sockaddr_union* addr;
444
+#if defined(SO_REUSEADDR) && !defined(TCP_DONT_REUSEADDR) 
445
+	int optval;
446
+#endif
444 447
 #ifdef DISABLE_NAGLE
445 448
 	int flag;
446 449
 	struct protoent* pe;
... ...
@@ -474,6 +483,27 @@ int tcp_init(struct socket_info* sock_info)
474 474
 				strerror(errno));
475 475
 	}
476 476
 #endif
477
+
478
+
479
+#if defined(SO_REUSEADDR) && !defined(TCP_DONT_REUSEADDR) 
480
+	/* Stevens, "Network Programming", Section 7.5, "Generic Socket
481
+     * Options": "...server started,..a child continues..on existing
482
+	 * connection..listening server is restarted...call to bind fails
483
+	 * ... ALL TCP servers should specify the SO_REUSEADDRE option 
484
+	 * to allow the server to be restarted in this situation
485
+	 *
486
+	 * Indeed, without this option, the server can't restart.
487
+	 *   -jiri
488
+	 */
489
+	optval=1;
490
+	if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
491
+				(void*)&optval, sizeof(optval))==-1) {
492
+		LOG(L_ERR, "ERROR: tcp_init: setsockopt %s\n",
493
+			strerror(errno));
494
+		goto error;
495
+	}
496
+#endif
497
+
477 498
 	if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
478 499
 		LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s: %s\n",
479 500
 				sock_info->socket, &addr->s,