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 298
 }
297 299
 
298 300
 
301
+
302
+/* returns 0 if support for the protocol is not compiled or if proto is 
303
+   invalid */
299 304
 static struct socket_info** get_sock_info_list(unsigned short proto)
300 305
 {
301 306
 	
... ...
@@ -303,21 +308,21 @@ static struct socket_info** get_sock_info_list(unsigned short proto)
303 308
 		case PROTO_UDP:
304 309
 			return &udp_listen;
305 310
 			break;
306
-#ifdef USE_TCP
307 311
 		case PROTO_TCP:
312
+#ifdef USE_TCP
308 313
 			return &tcp_listen;
309
-			break;
310 314
 #endif
311
-#ifdef USE_TLS
315
+			break;
312 316
 		case PROTO_TLS:
317
+#ifdef USE_TLS
313 318
 			return &tls_listen;
314
-			break;
315 319
 #endif
316
-#ifdef USE_SCTP
320
+			break;
317 321
 		case PROTO_SCTP:
322
+#ifdef USE_SCTP
318 323
 			return &sctp_listen;
319
-			break;
320 324
 #endif
325
+			break;
321 326
 		default:
322 327
 			LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
323 328
 	}
... ...
@@ -394,19 +399,13 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
394 399
 		hname.len-=2;
395 400
 	}
396 401
 #endif
397
-	c_proto=proto?proto:PROTO_UDP;
402
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
398 403
 	do{
399 404
 		/* 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);
405
+		list=get_sock_info_list(c_proto);
404 406
 	
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
-		}
407
+		if (list==0) /* disabled or unknown protocol */
408
+			continue;
410 409
 		for (si=*list; si; si=si->next){
411 410
 			DBG("grep_sock_info - checking if host==us: %d==%d && "
412 411
 					" [%.*s] == [%.*s]\n", 
... ...
@@ -432,7 +431,7 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
432 431
 					goto found;
433 432
 		}
434 433
 	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
435
-not_found:
434
+/* not_found: */
436 435
 	return 0;
437 436
 found:
438 437
 	return si;
... ...
@@ -453,19 +452,14 @@ struct socket_info* grep_sock_info_by_port(unsigned short port,
453 452
 	if (!port) {
454 453
 		goto not_found;
455 454
 	}
456
-	c_proto=proto?proto:PROTO_UDP;
455
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
457 456
 	do{
458 457
 		/* 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);
458
+		list=get_sock_info_list(c_proto);
463 459
 	
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
-		}
460
+		if (list==0) /* disabled or unknown protocol */
461
+			continue;
462
+		
469 463
 		for (si=*list; si; si=si->next){
470 464
 			DBG("grep_sock_info_by_port - checking if port %d matches"
471 465
 					" port %d\n", si->port_no, port);
... ...
@@ -499,19 +493,14 @@ struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
499 493
 	struct addr_info* ai;
500 494
 	unsigned short c_proto;
501 495
 	
502
-	c_proto=proto?proto:PROTO_UDP;
496
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
503 497
 	do{
504 498
 		/* 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);
499
+		list=get_sock_info_list(c_proto);
509 500
 	
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
-		}
501
+		if (list==0) /* disabled or unknown protocol */
502
+			continue;
503
+		
515 504
 		for (si=*list; si; si=si->next){
516 505
 			if (port) {
517 506
 				if (si->port_no!=port) {
... ...
@@ -525,7 +514,7 @@ struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
525 514
 					goto found;
526 515
 		}
527 516
 	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
528
-not_found:
517
+/* not_found: */
529 518
 	return 0;
530 519
 found:
531 520
 	return si;
... ...
@@ -589,14 +578,12 @@ int add_listen_iface(char* name, struct name_lst* addr_l,
589 578
 	struct name_lst* a_l;
590 579
 	unsigned short c_port;
591 580
 	
592
-	c_proto=(proto)?proto:PROTO_UDP;
581
+	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
593 582
 	do{
594 583
 		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
-		}
584
+		if (list==0) /* disabled or unknown protocol */
585
+			continue;
586
+		
600 587
 		if (port==0){ /* use default port */
601 588
 			c_port=
602 589
 #ifdef USE_TLS