address: Make the private IP address detector more complete
authorOlivier Crête <olivier.crete@collabora.com>
Wed, 6 May 2020 04:00:36 +0000 (00:00 -0400)
committerOlivier Crête <olivier.crete@ocrete.ca>
Wed, 6 May 2020 22:32:32 +0000 (22:32 +0000)
Also adds a unit test

Fixes #67

agent/address.c
tests/test-address.c

index 3c20220..8508c6f 100644 (file)
@@ -298,14 +298,18 @@ ipv4_address_is_private (guint32 addr)
 {
   addr = ntohl (addr);
 
-  /* http://tools.ietf.org/html/rfc3330 */
+  /* http://tools.ietf.org/html/rfc3330 
+   * https://tools.ietf.org/html/rfc3927
+   */
   return (
       /* 10.0.0.0/8 */
       ((addr & 0xff000000) == 0x0a000000) ||
-      /* 172.16.0.0/12 */
+      /* 172.16.0.0 - 172.31.255.255  = 172.16.0.0/12 */
       ((addr & 0xfff00000) == 0xac100000) ||
       /* 192.168.0.0/16 */
       ((addr & 0xffff0000) == 0xc0a80000) ||
+      /* 169.254.x.x/16  (for APIPA) */
+      ((addr & 0xffff0000) == 0xa9fe0000) ||
       /* 127.0.0.0/8 */
       ((addr & 0xff000000) == 0x7f000000));
 }
@@ -315,9 +319,11 @@ static gboolean
 ipv6_address_is_private (const guchar *addr)
 {
   return (
-      /* fe80::/10 */
+      /* fe80::/10 (link local addresses, needs scope) */
       ((addr[0] == 0xfe) && ((addr[1] & 0xc0) == 0x80)) ||
-      /* fc00::/7 */
+      /* fd00::/8 (official private IP block) */
+      (addr[0] == 0xfd) ||
+      /* fc00::/7 (those are ULA) */
       ((addr[0] & 0xfe) == 0xfc) ||
       /* ::1 loopback */
       ((memcmp (addr, "\x00\x00\x00\x00"
index 8e05050..583830e 100644 (file)
@@ -84,9 +84,71 @@ test_ipv4 (void)
   /* test private address check */
   {
     NiceAddress *heap_addr = nice_address_new ();
+
+    g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1.1") != TRUE);
+
     g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1") == TRUE);
     g_assert (nice_address_is_private (heap_addr) == TRUE);
-    g_assert (nice_address_set_from_string (heap_addr, "127.0.0.1.1") != TRUE);
+
+    g_assert (nice_address_set_from_string (heap_addr, "127.1.1.1") == TRUE);
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "192.168.2.0"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "192.168.15.69"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "192.169.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "192.167.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "10.2.1.2"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "11.0.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "9.255.255.255"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "172.15.255.255"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "172.16.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "172.31.255.255"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "172.32.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+    g_assert (nice_address_set_from_string(heap_addr, "172.63.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "169.253.255.255"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "169.254.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "169.254.255.255"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "169.255.0.0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "fe70::0"));
+    g_assert (nice_address_is_private (heap_addr) == FALSE);
+    
+    g_assert (nice_address_set_from_string(heap_addr, "fe80::0"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
+    g_assert (nice_address_set_from_string(heap_addr, "fe81::0"));
+    g_assert (nice_address_is_private (heap_addr) == TRUE);
+
     nice_address_free (heap_addr);
   }
 }