Browse code

core: count and store global index for listen sockets at startup

Daniel-Constantin Mierla authored on 06/04/2022 11:54:59
Showing 4 changed files
... ...
@@ -110,6 +110,7 @@ typedef struct advertise_info {
110 110
 
111 111
 typedef struct socket_info {
112 112
 	int socket;
113
+	int gindex; /* global index in the lists of all sockets */
113 114
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
114 115
 	struct ip_addr address; /* ip address */
115 116
 	str address_str;        /*ip address converted to string -- optimization*/
... ...
@@ -762,6 +762,54 @@ found:
762 762
 	return si;
763 763
 }
764 764
 
765
+/**
766
+ *
767
+ */
768
+static int _ksr_sockets_no = 0;
769
+
770
+/**
771
+ *
772
+ */
773
+int ksr_sockets_no_get(void)
774
+{
775
+	return _ksr_sockets_no;
776
+}
777
+
778
+/**
779
+ *
780
+ */
781
+void ksr_sockets_index(void)
782
+{
783
+	socket_info_t *si = NULL;
784
+	struct socket_info** list;
785
+	unsigned short c_proto;
786
+
787
+	if(_ksr_sockets_no > 0) {
788
+		return;
789
+	}
790
+
791
+	c_proto = PROTO_UDP;
792
+	do {
793
+		/* get the proper sock_list */
794
+		list=get_sock_info_list(c_proto);
795
+
796
+		if (list==0) {
797
+			/* disabled or unknown protocol */
798
+			continue;
799
+		}
800
+
801
+		for (si=*list; si; si=si->next) {
802
+			if(si->sockname.s == NULL) {
803
+				continue;
804
+			}
805
+			si->gindex = _ksr_sockets_no;
806
+			_ksr_sockets_no++;
807
+		}
808
+	} while((c_proto = next_proto(c_proto))!=0);
809
+
810
+	LM_DBG("number of listen sockets: %d\n", _ksr_sockets_no);
811
+}
812
+
765 813
 socket_info_t* ksr_get_socket_by_name(str *sockname)
766 814
 {
767 815
 	socket_info_t *si = NULL;
... ...
@@ -163,4 +163,7 @@ int parse_protohostport(str* ins, sr_phostp_t *r);
163 163
 
164 164
 unsigned int ipv6_get_netif_scope(char *ipval);
165 165
 
166
+int ksr_sockets_no_get(void);
167
+void ksr_sockets_index(void);
168
+
166 169
 #endif
... ...
@@ -2851,6 +2851,7 @@ try_again:
2851 2851
 		fprintf(stderr,  "failed to initialize list addresses\n");
2852 2852
 		goto error;
2853 2853
 	}
2854
+	ksr_sockets_index();
2854 2855
 	if (default_core_cfg.dns_try_ipv6 && !(socket_types & SOCKET_T_IPV6)){
2855 2856
 		/* if we are not listening on any ipv6 address => no point
2856 2857
 		 * to try to resovle ipv6 addresses */