[sr-dev] git:master: lib/kcore Fix IPv6 support in interface list

Olle E. Johansson oej at edvina.net
Sun Apr 7 09:47:33 CEST 2013


Module: sip-router
Branch: master
Commit: e28a06a7a8f5558bb81cfc09d5cb70b9c6938328
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=e28a06a7a8f5558bb81cfc09d5cb70b9c6938328

Author: Olle E. Johansson <oej at edvina.net>
Committer: Olle E. Johansson <oej at edvina.net>
Date:   Sat Apr  6 21:56:59 2013 +0200

lib/kcore Fix IPv6 support in interface list

Added a new function that takes family as an argument. In order to list both
IPv4 and IPv6 you need to call that function once per address family.
Keep the old IPv4-only function for backwards compatibility.

---

 lib/kcore/statistics.c |   55 ++++++++++++++++++++++++++++++++++++-----------
 lib/kcore/statistics.h |   10 +++++++-
 2 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/lib/kcore/statistics.c b/lib/kcore/statistics.c
index c94bde0..22fed2d 100644
--- a/lib/kcore/statistics.c
+++ b/lib/kcore/statistics.c
@@ -113,11 +113,37 @@ stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int out_codes)
  *       contents, to avoid a nasty memory leak.
  */
 int get_socket_list_from_proto(int **ipList, int protocol) {
+	return get_socket_list_from_proto_and_family(ipList, protocol, AF_INET);
+}
+
+
+/*!
+ * This function will retrieve a list of all ip addresses and ports that Kamailio
+ * is listening on, with respect to the transport protocol specified with
+ * 'protocol'. This function supports both IPv4 and IPv6
+ *
+ * The first parameter, ipList, is a pointer to a pointer. It will be assigned a
+ * new block of memory holding the IP Addresses and ports being listened to with
+ * respect to 'protocol'.  The array maps a 2D array into a 1 dimensional space,
+ * and is layed out as follows:
+ *
+ * The first NUM_IP_OCTETS indices will be the IP address, and the next index
+ * the port.  So if NUM_IP_OCTETS is equal to 4 and there are two IP addresses
+ * found, then:
+ *
+ *  - ipList[0] will be the first octet of the first ip address
+ *  - ipList[3] will be the last octet of the first ip address.
+ *  - iplist[4] will be the port of the first ip address
+ *  - 
+ *  - iplist[5] will be the first octet of the first ip address, 
+ *  - and so on.  
+ */
+int get_socket_list_from_proto_and_family(int **ipList, int protocol, int family) {
 
 	struct socket_info  *si;
 	struct socket_info** list;
 
-	int num_ip_octets   = 4;
+	int num_ip_octets   = family == AF_INET ? NUM_IP_OCTETS : NUM_IPV6_OCTETS;
 	int numberOfSockets = 0;
 	int currentRow      = 0;
 
@@ -136,6 +162,13 @@ int get_socket_list_from_proto(int **ipList, int protocol) {
 		return 0;
 	}
 #endif
+#ifndef USE_SCTP
+	if (protocol == PROTO_SCTP)
+	{
+		return 0;
+	}
+#endif
+	/* We have no "interfaces" for websockets */
 	if (protocol == PROTO_WS || protocol == PROTO_WSS)
 		return 0;
 
@@ -145,8 +178,7 @@ int get_socket_list_from_proto(int **ipList, int protocol) {
 	/* Find out how many sockets are in the list.  We need to know this so
 	 * we can malloc an array to assign to ipList. */
 	for(si=list?*list:0; si; si=si->next){
-		/* We only support IPV4 at this point. */
-		if (si->address.af == AF_INET) {
+		if (si->address.af == family) {
 			numberOfSockets++;
 		}
 	}
@@ -172,21 +204,18 @@ int get_socket_list_from_proto(int **ipList, int protocol) {
 
 	/* Extract out the IP Addresses and ports.  */
 	for(si=list?*list:0; si; si=si->next){
+		int i;
 
 		/* We currently only support IPV4. */
-		if (si->address.af != AF_INET) {
+		if (si->address.af != family) {
 			continue;
 		}
 
-		(*ipList)[currentRow*(num_ip_octets + 1)  ] = 
-			si->address.u.addr[0];
-		(*ipList)[currentRow*(num_ip_octets + 1)+1] = 
-			si->address.u.addr[1];
-		(*ipList)[currentRow*(num_ip_octets + 1)+2] = 
-			si->address.u.addr[2];
-		(*ipList)[currentRow*(num_ip_octets + 1)+3] = 
-			si->address.u.addr[3];
-		(*ipList)[currentRow*(num_ip_octets + 1)+4] = 
+		for (i = 0; i < num_ip_octets; i++) {
+			(*ipList)[currentRow*(num_ip_octets + 1) + i ] = 
+				si->address.u.addr[i];
+		}
+		(*ipList)[currentRow*(num_ip_octets + 1) + i] = 
 			si->port_no;
 		
 		currentRow++;
diff --git a/lib/kcore/statistics.h b/lib/kcore/statistics.h
index 08bf20d..110f02c 100644
--- a/lib/kcore/statistics.h
+++ b/lib/kcore/statistics.h
@@ -40,6 +40,7 @@
 
 
 #define NUM_IP_OCTETS 4
+#define NUM_IPV6_OCTETS 16
 
 
 #ifdef STATISTICS
@@ -63,7 +64,7 @@ stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int in_codes);
 /*!
  * This function will retrieve a list of all ip addresses and ports that Kamailio
  * is listening on, with respect to the transport protocol specified with
- * 'protocol'. 
+ * 'protocol'. This function only returns IPv4 addresses.
  *
  * The first parameter, ipList, is a pointer to a pointer. It will be assigned a
  * new block of memory holding the IP Addresses and ports being listened to with
@@ -93,6 +94,13 @@ stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int in_codes);
  */
 int get_socket_list_from_proto(int **ipList, int protocol);
 
+/*! \brief Function to get a list of all IP addresses and ports in a specific
+ * family, like AF_INET or AF_INET6
+ *
+ * For documentation see \ref get_socket_list_from_proto()
+ */
+int get_socket_list_from_proto_and_family(int **ipList, int protocol, int family);
+
 
 /*!
  * Returns the sum of the number of bytes waiting to be consumed on all network




More information about the sr-dev mailing list