]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: new virSocketAddrIsPrivate function
authorLaine Stump <laine@laine.org>
Thu, 22 Nov 2012 02:17:30 +0000 (21:17 -0500)
committerLaine Stump <laine@laine.org>
Fri, 30 Nov 2012 02:36:45 +0000 (21:36 -0500)
This new function returns true if the given address is in the range of
any "private" or "local" networks as defined in RFC1918 (IPv4) or
RFC3484/RFC4193 (IPv6), otherwise they return false.

These ranges are:

   192.168.0.0/16
   172.16.0.0/16
   10.0.0.0/24
   FC00::/7
   FEC0::/10

(cherry picked from commit bf402e77b6d53a4e569b3aa76aef9c7d589c0cf2)
Conflicts:
    src/util/virsocketaddr.c
    src/util/virsocketaddr.h
     * both of these files had new functions that had been added
       at the same place virSocketAddrIsPrivate was being added,
       so the context on the branch didn't match.

src/libvirt_private.syms
src/util/virsocketaddr.c
src/util/virsocketaddr.h

index b21104c3e451d9ecd77b8be2bd7833c0d0bae548..7a1d935fee00d85ad3d3a08bdafbe4f3d1e4c6e1 100644 (file)
@@ -1465,6 +1465,7 @@ virSocketAddrFormatFull;
 virSocketAddrGetPort;
 virSocketAddrGetRange;
 virSocketAddrIsNetmask;
+virSocketAddrIsPrivate;
 virSocketAddrMask;
 virSocketAddrMaskByPrefix;
 virSocketAddrParse;
index b3104e74c6137ea6da148727d33c0ae4b080da15..62c474efb57b1dd44fc6eec676f2fb62f576f363 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2011 Red Hat, Inc.
+ * Copyright (C) 2009-2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -151,6 +151,39 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val) {
     return virSocketAddrParse(addr, val, AF_INET6);
 }
 
+/*
+ * virSocketAddrIsPrivate:
+ * @s: the location of the IP address
+ *
+ * Return true if this address is in its family's defined
+ * "private/local" address space. For IPv4, private addresses are in
+ * the range of 192.168.0.0/16, 172.16.0.0/16, or 10.0.0.0/8.  For
+ * IPv6, local addresses are in the range of FC00::/7 or FEC0::/10
+ * (that last one is deprecated, but still in use).
+ *
+ * See RFC1918, RFC3484, and RFC4193 for details.
+ */
+bool
+virSocketAddrIsPrivate(const virSocketAddrPtr addr)
+{
+    unsigned long val;
+
+    switch (addr->data.stor.ss_family) {
+    case AF_INET:
+       val = ntohl(addr->data.inet4.sin_addr.s_addr);
+
+       return ((val & 0xFFFF0000) == ((192L << 24) + (168 << 16)) ||
+               (val & 0xFFFF0000) == ((172L << 24) + (16  << 16)) ||
+               (val & 0xFF000000) == ((10L  << 24)));
+
+    case AF_INET6:
+        return ((addr->data.inet6.sin6_addr.s6_addr[0] & 0xFE) == 0xFC ||
+                ((addr->data.inet6.sin6_addr.s6_addr[0] & 0xFF) == 0xFE &&
+                 (addr->data.inet6.sin6_addr.s6_addr[1] & 0xC0) == 0xC0));
+    }
+    return false;
+}
+
 /*
  * virSocketAddrFormat:
  * @addr: an initialized virSocketAddrPtr
index 3af6fe56fbbda3b0997cb20c2b064ce3e2a2d2ae..98ec9be5cd2d29eb1aecde2603605f8d487fe584 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2011 Red Hat, Inc.
+ * Copyright (C) 2009-2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -100,5 +100,6 @@ int virSocketAddrGetNumNetmaskBits(const virSocketAddrPtr netmask);
 int virSocketAddrPrefixToNetmask(unsigned int prefix,
                                  virSocketAddrPtr netmask,
                                  int family);
+bool virSocketAddrIsPrivate(const virSocketAddrPtr addr);
 
 #endif /* __VIR_SOCKETADDR_H__ */