Browse code

core: more debug messages when matching myself

- coherent coding style for related functions

(cherry picked from commit 34b9b59b97f7d529025863992408fd60a27a941a)

Daniel-Constantin Mierla authored on 02/05/2020 06:46:28
Showing 3 changed files
... ...
@@ -406,15 +406,23 @@ int run_check_self_func(str* host, unsigned short port, unsigned short proto)
406 406
  */
407 407
 int check_self(str* host, unsigned short port, unsigned short proto)
408 408
 {
409
-	if (grep_sock_info(host, port, proto)) goto found;
409
+	int ret = 1;
410
+	if (grep_sock_info(host, port, proto)) {
411
+		goto done;
412
+	}
410 413
 	/* try to look into the aliases*/
411
-	if (grep_aliases(host->s, host->len, port, proto)==0){
412
-		LM_DBG("host != me\n");
413
-		return (_check_self_func_list==NULL)?0:run_check_self_func(host,
414
-														port, proto);
414
+	if (grep_aliases(host->s, host->len, port, proto)==0) {
415
+		ret = (_check_self_func_list==NULL)?0:run_check_self_func(host,
416
+					port, proto);
415 417
 	}
416
-found:
417
-	return 1;
418
+
419
+done:
420
+	if(ret==1) {
421
+		LM_DBG("host (%d:%.*s:%d) == me\n", proto, host->len, host->s, port);
422
+	} else {
423
+		LM_DBG("host (%d:%.*s:%d) != me\n", proto, host->len, host->s, port);
424
+	}
425
+	return ret;
418 426
 }
419 427
 
420 428
 /** checks if the proto:port is one of the ports we listen on;
... ...
@@ -423,11 +431,14 @@ found:
423 431
  */
424 432
 int check_self_port(unsigned short port, unsigned short proto)
425 433
 {
426
-	if (grep_sock_info_by_port(port, proto))
427
-		/* as aliases do not contain different ports we can skip them */
434
+	/* aliases do not contain different ports we can skip them */
435
+	if (grep_sock_info_by_port(port, proto)) {
436
+		LM_DBG("proto:port (%d:%d) == me\n", proto, port);
428 437
 		return 1;
429
-	else
438
+	} else {
439
+		LM_DBG("proto:port (%d:%d) != me\n", proto, port);
430 440
 		return 0;
441
+	}
431 442
 }
432 443
 
433 444
 
... ...
@@ -14,8 +14,8 @@
14 14
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 15
  * GNU General Public License for more details.
16 16
  *
17
- * You should have received a copy of the GNU General Public License 
18
- * along with this program; if not, write to the Free Software 
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 20
  */
21 21
 /*!
... ...
@@ -35,65 +35,75 @@
35 35
 #include "mem/mem.h"
36 36
 
37 37
 
38
-
39
-struct host_alias{
38
+typedef struct host_alias{
40 39
 	str alias;
41 40
 	unsigned short port;
42 41
 	unsigned short proto;
43 42
 	struct host_alias* next;
44
-};
43
+} host_alias_t;
45 44
 
46 45
 
47 46
 extern struct host_alias* aliases;
48 47
 
49 48
 
50
-
51 49
 /** returns 1 if  name is in the alias list; if port=0, port no is ignored
52 50
  * if proto=0, proto is ignored*/
53 51
 static inline int grep_aliases(char* name, int len, unsigned short port,
54 52
 								unsigned short proto)
55 53
 {
56
-	struct  host_alias* a;
57
-	
58
-	if ((len>2)&&((*name)=='[')&&(name[len-1]==']')){
54
+	struct host_alias* a;
55
+
56
+	if ((len>2)&&((*name)=='[')&&(name[len-1]==']')) {
59 57
 		/* ipv6 reference, skip [] */
60 58
 		name++;
61 59
 		len-=2;
62 60
 	}
63
-	for(a=aliases;a;a=a->next)
64
-		if ((a->alias.len==len) && ((a->port==0) || (port==0) || 
65
-				(a->port==port)) && ((a->proto==0) || (proto==0) || 
66
-				(a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0))
61
+	for(a=aliases;a;a=a->next) {
62
+		LM_DBG("matching (%d:%.*s:%d) vs. (%d:%.*s:%d)\n",
63
+				proto, len, name, port, a->proto, a->alias.len, a->alias.s,
64
+				a->port);
65
+		if ((a->alias.len==len) && ((a->port==0) || (port==0) ||
66
+				(a->port==port)) && ((a->proto==0) || (proto==0) ||
67
+				(a->proto==proto)) && (strncasecmp(a->alias.s, name, len)==0)) {
67 68
 			return 1;
69
+		}
70
+	}
68 71
 	return 0;
69 72
 }
70 73
 
71 74
 
72
-
73 75
 /** adds an alias to the list (only if it isn't already there)
74 76
  * if port==0, the alias will match all the ports
75 77
  * if proto==0, the alias will match all the protocols
76 78
  * returns 1 if a new alias was added, 0 if a matching alias was already on
77 79
  * the list and  -1 on error */
78
-static inline int add_alias(char* name, int len, unsigned short port, 
80
+static inline int add_alias(char* name, int len, unsigned short port,
79 81
 								unsigned short proto)
80 82
 {
81 83
 	struct host_alias* a;
82
-	
83
-	if ((port) && (proto)){
84
+
85
+	if ((port) && (proto)) {
84 86
 		/* don't add if there is already an alias matching it */
85
-		if (grep_aliases(name,len, port, proto)) return 0;
86
-	}else{
87
+		if (grep_aliases(name,len, port, proto)) {
88
+			return 0;
89
+		}
90
+	} else {
87 91
 		/* don't add if already in the list with port or proto ==0*/
88
-		for(a=aliases;a;a=a->next)
89
-			if ((a->alias.len==len) && (a->port==port) && (a->proto==proto) &&
90
-					(strncasecmp(a->alias.s, name, len)==0))
92
+		for(a=aliases;a;a=a->next) {
93
+			if ((a->alias.len==len) && (a->port==port) && (a->proto==proto)
94
+					&& (strncasecmp(a->alias.s, name, len)==0)) {
91 95
 				return 0;
96
+			}
97
+		}
92 98
 	}
93 99
 	a=(struct host_alias*)pkg_malloc(sizeof(struct host_alias));
94
-	if(a==0) goto error;
100
+	if(a==0) {
101
+		goto error;
102
+	}
95 103
 	a->alias.s=(char*)pkg_malloc(len+1);
96
-	if (a->alias.s==0) goto error;
104
+	if (a->alias.s==0) {
105
+		goto error;
106
+	}
97 107
 	a->alias.len=len;
98 108
 	memcpy(a->alias.s, name, len);
99 109
 	a->alias.s[len]=0; /* null terminate for easier printing*/
... ...
@@ -104,10 +114,10 @@ static inline int add_alias(char* name, int len, unsigned short port,
104 114
 	return 1;
105 115
 error:
106 116
 	PKG_MEM_ERROR;
107
-	if (a) pkg_free(a);
117
+	if (a) {
118
+		pkg_free(a);
119
+	}
108 120
 	return -1;
109 121
 }
110 122
 
111
-
112
-
113
-#endif
114 123
\ No newline at end of file
124
+#endif
... ...
@@ -608,23 +608,25 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
608 608
 	struct socket_info** list;
609 609
 	struct addr_info* ai;
610 610
 	unsigned short c_proto;
611
-	
611
+
612 612
 	hname=*host;
613
-	if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
614
-		/* ipv6 reference, skip [] */
613
+	if ((hname.len>2) && ((*hname.s)=='[') && (hname.s[hname.len-1]==']')) {
614
+		/* ipv6 - skip [] */
615 615
 		hname.s++;
616 616
 		hname.len-=2;
617 617
 	}
618 618
 
619 619
 	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
620 620
 retry:
621
-	do{
621
+	do {
622 622
 		/* get the proper sock_list */
623 623
 		list=get_sock_info_list(c_proto);
624
-	
625
-		if (list==0) /* disabled or unknown protocol */
624
+
625
+		if (list==0) {
626
+			/* disabled or unknown protocol */
626 627
 			continue;
627
-		for (si=*list; si; si=si->next){
628
+		}
629
+		for (si=*list; si; si=si->next) {
628 630
 			LM_DBG("checking if host==us: %d==%d && [%.*s] == [%.*s]\n",
629 631
 						hname.len,
630 632
 						si->name.len,
... ...
@@ -638,31 +640,34 @@ retry:
638 640
 					continue;
639 641
 				}
640 642
 			}
641
-			if (si_hname_cmp(&hname, &si->name, &si->address_str, 
642
-								&si->address, si->flags)==0)
643
+			if (si_hname_cmp(&hname, &si->name, &si->address_str,
644
+								&si->address, si->flags)==0) {
643 645
 				goto found;
644
-			if(si->useinfo.name.s!=NULL)
645
-			{
646
+			}
647
+			if(si->useinfo.name.s!=NULL) {
646 648
 				LM_DBG("checking advertise if host==us:"
647 649
 						" %d==%d && [%.*s] == [%.*s]\n",
648 650
 						hname.len,
649 651
 						si->useinfo.name.len,
650 652
 						hname.len, hname.s,
651 653
 						si->useinfo.name.len, si->useinfo.name.s
652
-				);
654
+					);
653 655
 				if (si_hname_cmp(&hname, &si->useinfo.name,
654 656
 							&si->useinfo.address_str, &si->useinfo.address,
655
-							si->flags)==0)
657
+							si->flags)==0) {
656 658
 					goto found;
659
+				}
657 660
 			}
658 661
 			/* try among the extra addresses */
659
-			for (ai=si->addr_info_lst; ai; ai=ai->next)
660
-				if (si_hname_cmp(&hname, &ai->name, &ai->address_str, 
661
-									&ai->address, ai->flags)==0)
662
+			for (ai=si->addr_info_lst; ai; ai=ai->next) {
663
+				if (si_hname_cmp(&hname, &ai->name, &ai->address_str,
664
+									&ai->address, ai->flags)==0) {
662 665
 					goto found;
666
+				}
667
+			}
663 668
 		}
664 669
 
665
-	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
670
+	} while( (proto==0) && (c_proto=next_proto(c_proto)) );
666 671
 
667 672
 #ifdef USE_TLS
668 673
 	if (unlikely(c_proto == PROTO_WS)) {
... ...
@@ -681,8 +686,8 @@ found:
681 686
  * if proto==0 (PROTO_NONE) the protocol is ignored
682 687
  * returns  0 if not found
683 688
  */
684
-struct socket_info* grep_sock_info_by_port(unsigned short port, 
685
-											unsigned short proto)
689
+struct socket_info* grep_sock_info_by_port(unsigned short port,
690
+		unsigned short proto)
686 691
 {
687 692
 	struct socket_info* si;
688 693
 	struct socket_info** list;
... ...
@@ -692,22 +697,24 @@ struct socket_info* grep_sock_info_by_port(unsigned short port,
692 697
 		goto not_found;
693 698
 	}
694 699
 	c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
695
-	do{
700
+	do {
696 701
 		/* get the proper sock_list */
697 702
 		list=get_sock_info_list(c_proto);
698
-	
703
+
699 704
 		if (list==0) /* disabled or unknown protocol */
700 705
 			continue;
701
-		
706
+
702 707
 		for (si=*list; si; si=si->next){
703 708
 			LM_DBG("checking if port %d matches port %d\n", si->port_no, port);
704 709
 			if (si->port_no==port) {
705 710
 				goto found;
706 711
 			}
707 712
 		}
708
-	}while( (proto==0) && (c_proto=next_proto(c_proto)) );
713
+	} while( (proto==0) && (c_proto=next_proto(c_proto)) );
714
+
709 715
 not_found:
710 716
 	return 0;
717
+
711 718
 found:
712 719
 	return si;
713 720
 }