Browse code

core: function to get local socket by index

Daniel-Constantin Mierla authored on 12/04/2022 06:36:36
Showing 2 changed files
... ...
@@ -912,6 +912,40 @@ socket_info_t* ksr_get_socket_by_advertise(str *sockstr)
912 912
 	return NULL;
913 913
 }
914 914
 
915
+socket_info_t* ksr_get_socket_by_index(int idx)
916
+{
917
+	socket_info_t *si = NULL;
918
+	struct socket_info** list;
919
+	unsigned short c_proto;
920
+
921
+	if (idx<0) {
922
+		idx += _ksr_sockets_no;
923
+		if (idx < 0) {
924
+			LM_DBG("negative overall index\n");
925
+			return NULL;
926
+		}
927
+	}
928
+	c_proto = PROTO_UDP;
929
+	do {
930
+		/* get the proper sock_list */
931
+		list=get_sock_info_list(c_proto);
932
+
933
+		if (list==0) {
934
+			/* disabled or unknown protocol */
935
+			continue;
936
+		}
937
+
938
+		for (si=*list; si; si=si->next) {
939
+			if(idx==0) {
940
+				return si;
941
+			}
942
+			idx--;
943
+		}
944
+	} while((c_proto = next_proto(c_proto))!=0);
945
+
946
+	return NULL;
947
+}
948
+
915 949
 /* checks if the proto:port is one of the ports we listen on
916 950
  * and returns the corresponding socket_info structure.
917 951
  * if proto==0 (PROTO_NONE) the protocol is ignored
... ...
@@ -106,6 +106,7 @@ struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
106 106
 socket_info_t* ksr_get_socket_by_name(str *sockname);
107 107
 socket_info_t* ksr_get_socket_by_listen(str *sockstr);
108 108
 socket_info_t* ksr_get_socket_by_advertise(str *sockstr);
109
+socket_info_t* ksr_get_socket_by_index(int idx);
109 110
 
110 111
 struct socket_info** get_sock_info_list(unsigned short proto);
111 112