Commit 04ae880c authored by Olivier Crête's avatar Olivier Crête 👻 Committed by Olivier Crête
Browse files

address: Make the private IP address detector more complete

Also adds a unit test

Fixes #67
parent f6b4a73b
......@@ -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"
......
......@@ -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);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment