[sr-dev] git:master:61b32b46: core: count and store global index for listen sockets at startup

Daniel-Constantin Mierla miconda at gmail.com
Wed Apr 6 14:04:20 CEST 2022


Module: kamailio
Branch: master
Commit: 61b32b46afd7cc0a82f33aa3d92a5e76c4d87fee
URL: https://github.com/kamailio/kamailio/commit/61b32b46afd7cc0a82f33aa3d92a5e76c4d87fee

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2022-04-06T13:54:59+02:00

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

---

Modified: src/core/ip_addr.h
Modified: src/core/socket_info.c
Modified: src/core/socket_info.h
Modified: src/main.c

---

Diff:  https://github.com/kamailio/kamailio/commit/61b32b46afd7cc0a82f33aa3d92a5e76c4d87fee.diff
Patch: https://github.com/kamailio/kamailio/commit/61b32b46afd7cc0a82f33aa3d92a5e76c4d87fee.patch

---

diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h
index 9205def123..c3f423123e 100644
--- a/src/core/ip_addr.h
+++ b/src/core/ip_addr.h
@@ -110,6 +110,7 @@ typedef struct advertise_info {
 
 typedef struct socket_info {
 	int socket;
+	int gindex; /* global index in the lists of all sockets */
 	str name; /* name - eg.: foo.bar or 10.0.0.1 */
 	struct ip_addr address; /* ip address */
 	str address_str;        /*ip address converted to string -- optimization*/
diff --git a/src/core/socket_info.c b/src/core/socket_info.c
index 500945a22a..a60c4c5678 100644
--- a/src/core/socket_info.c
+++ b/src/core/socket_info.c
@@ -762,6 +762,54 @@ struct socket_info* grep_sock_info(str* host, unsigned short port,
 	return si;
 }
 
+/**
+ *
+ */
+static int _ksr_sockets_no = 0;
+
+/**
+ *
+ */
+int ksr_sockets_no_get(void)
+{
+	return _ksr_sockets_no;
+}
+
+/**
+ *
+ */
+void ksr_sockets_index(void)
+{
+	socket_info_t *si = NULL;
+	struct socket_info** list;
+	unsigned short c_proto;
+
+	if(_ksr_sockets_no > 0) {
+		return;
+	}
+
+	c_proto = PROTO_UDP;
+	do {
+		/* get the proper sock_list */
+		list=get_sock_info_list(c_proto);
+
+		if (list==0) {
+			/* disabled or unknown protocol */
+			continue;
+		}
+
+		for (si=*list; si; si=si->next) {
+			if(si->sockname.s == NULL) {
+				continue;
+			}
+			si->gindex = _ksr_sockets_no;
+			_ksr_sockets_no++;
+		}
+	} while((c_proto = next_proto(c_proto))!=0);
+
+	LM_DBG("number of listen sockets: %d\n", _ksr_sockets_no);
+}
+
 socket_info_t* ksr_get_socket_by_name(str *sockname)
 {
 	socket_info_t *si = NULL;
diff --git a/src/core/socket_info.h b/src/core/socket_info.h
index 50e4a063c1..77baeb42fb 100644
--- a/src/core/socket_info.h
+++ b/src/core/socket_info.h
@@ -163,4 +163,7 @@ int parse_protohostport(str* ins, sr_phostp_t *r);
 
 unsigned int ipv6_get_netif_scope(char *ipval);
 
+int ksr_sockets_no_get(void);
+void ksr_sockets_index(void);
+
 #endif
diff --git a/src/main.c b/src/main.c
index 016a90cabe..2a268a689b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2851,6 +2851,7 @@ int main(int argc, char** argv)
 		fprintf(stderr,  "failed to initialize list addresses\n");
 		goto error;
 	}
+	ksr_sockets_index();
 	if (default_core_cfg.dns_try_ipv6 && !(socket_types & SOCKET_T_IPV6)){
 		/* if we are not listening on any ipv6 address => no point
 		 * to try to resovle ipv6 addresses */




More information about the sr-dev mailing list