Browse code

core: fix socket list iteration

- socket list iteration didn't work properly on ser startup when some
of the protocols where compile-time disabled (no support for them compiled).
Symptomps: ser -l lo -f ser.cfg prints a BUG message and doesn't start.
- small socket list related cleanups

Andrei Pelinescu-Onciul authored on 15/10/2008 10:27:47
Showing 1 changed files
... ...
@@ -37,6 +37,8 @@
37 37
  *  2007-08-23  added detection for INADDR_ANY types of sockets (andrei)
38 38
  *  2008-08-08  sctp support (andrei)
39 39
  *  2008-08-15  support for handling sctp multihomed sockets (andrei)
40
+ *  2008-10-15  fixed protocol list iteration when some protocols are
41
+ *               compile time disabled (andrei)
40 42
  */
41 43
 
42 44
 
... ...
@@ -296,6 +298,9 @@ static char* get_proto_name(unsigned short proto)
296 296
 }
297 297
 
298 298
 
299
+
300
+/* returns 0 if support for the protocol is not compiled or if proto is 
301
+   invalid */
299 302
 static struct socket_info** get_sock_info_list(unsigned short proto)
300 303
 {
301 304
 	
... ...
@@ -303,21 +308,21 @@ static struct socket_info** get_sock_info_list(unsigned short proto)
303 303
 		case PROTO_UDP:
304 304
 			return &udp_listen;
305 305
 			break;
306
-#ifdef USE_TCP
307 306
 		case PROTO_TCP:
307
+#ifdef USE_TCP
308 308
 			return &tcp_listen;
309
-			break;
310 309
 #endif
311
-#ifdef USE_TLS
310
+			break;
312 311
 		case PROTO_TLS:
312
+#ifdef USE_TLS
313 313
 			return &tls_listen;
314
-			break;
315 314
 #endif
316
-#ifdef USE_SCTP
315
+			break;
317 316
 		case PROTO_SCTP:
317
+#ifdef USE_SCTP
318 318
 			return &sctp_listen;
319
-			break;
320 319
 #endif
320
+			break;
321 321
 		default:
322 322
 			LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
323 323
 	}
... ...
@@ -394,19 +399,13 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
394 394
 		hname.len-=2;
395 395
 	}
396 396
 #endif
397
-	c_proto=proto?proto:PROTO_UDP;
397
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
398 398
 	do{
399 399
 		/* get the proper sock_list */
400
-		if (c_proto==PROTO_NONE)
401
-			list=&udp_listen;
402
-		else
403
-			list=get_sock_info_list(c_proto);
400
+		list=get_sock_info_list(c_proto);
404 401
 	
405
-		if (list==0){
406
-			LOG(L_WARN, "WARNING: grep_sock_info: "
407
-						"unknown proto %d\n", c_proto);
408
-			goto not_found; /* false */
409
-		}
402
+		if (list==0) /* disabled or unknown protocol */
403
+			continue;
410 404
 		for (si=*list; si; si=si->next){
411 405
 			DBG("grep_sock_info - checking if host==us: %d==%d && "
412 406
 					" [%.*s] == [%.*s]\n", 
... ...
@@ -432,7 +431,7 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
432 432
 					goto found;
433 433
 		}
434 434
 	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
435
-not_found:
435
+/* not_found: */
436 436
 	return 0;
437 437
 found:
438 438
 	return si;
... ...
@@ -453,19 +452,14 @@ struct socket_info* grep_sock_info_by_port(unsigned short port,
453 453
 	if (!port) {
454 454
 		goto not_found;
455 455
 	}
456
-	c_proto=proto?proto:PROTO_UDP;
456
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
457 457
 	do{
458 458
 		/* get the proper sock_list */
459
-		if (c_proto==PROTO_NONE)
460
-			list=&udp_listen;
461
-		else
462
-			list=get_sock_info_list(c_proto);
459
+		list=get_sock_info_list(c_proto);
463 460
 	
464
-		if (list==0){
465
-			LOG(L_WARN, "WARNING: grep_sock_info_by_port: "
466
-						"unknown proto %d\n", c_proto);
467
-			goto not_found; /* false */
468
-		}
461
+		if (list==0) /* disabled or unknown protocol */
462
+			continue;
463
+		
469 464
 		for (si=*list; si; si=si->next){
470 465
 			DBG("grep_sock_info_by_port - checking if port %d matches"
471 466
 					" port %d\n", si->port_no, port);
... ...
@@ -499,19 +493,14 @@ struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
499 499
 	struct addr_info* ai;
500 500
 	unsigned short c_proto;
501 501
 	
502
-	c_proto=proto?proto:PROTO_UDP;
502
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
503 503
 	do{
504 504
 		/* get the proper sock_list */
505
-		if (c_proto==PROTO_NONE)
506
-			list=&udp_listen;
507
-		else
508
-			list=get_sock_info_list(c_proto);
505
+		list=get_sock_info_list(c_proto);
509 506
 	
510
-		if (list==0){
511
-			LOG(L_WARN, "WARNING: grep_sock_info: "
512
-						"unknown proto %d\n", c_proto);
513
-			goto not_found; /* false */
514
-		}
507
+		if (list==0) /* disabled or unknown protocol */
508
+			continue;
509
+		
515 510
 		for (si=*list; si; si=si->next){
516 511
 			if (port) {
517 512
 				if (si->port_no!=port) {
... ...
@@ -525,7 +514,7 @@ struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
525 525
 					goto found;
526 526
 		}
527 527
 	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
528
-not_found:
528
+/* not_found: */
529 529
 	return 0;
530 530
 found:
531 531
 	return si;
... ...
@@ -589,14 +578,12 @@ int add_listen_iface(char* name, struct name_lst* addr_l,
589 589
 	struct name_lst* a_l;
590 590
 	unsigned short c_port;
591 591
 	
592
-	c_proto=(proto)?proto:PROTO_UDP;
592
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
593 593
 	do{
594 594
 		list=get_sock_info_list(c_proto);
595
-		if (list==0){
596
-			LOG(L_ERR, "ERROR: add_listen_iface: get_sock_info_list"
597
-						" failed\n");
598
-			goto error;
599
-		}
595
+		if (list==0) /* disabled or unknown protocol */
596
+			continue;
597
+		
600 598
 		if (port==0){ /* use default port */
601 599
 			c_port=
602 600
 #ifdef USE_TLS