[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH 1/3] Utility functions to produce an IPv4 broadcast address



On 12/31/2010 08:08 AM, Stefan Berger wrote:
On 12/31/2010 06:33 AM, Laine Stump wrote:
These functions work only for IPv4, becasue IPv6 doesn't have the same
concept of "broadcast address" as IPv4. They merely OR the inverse of
the netmask with the given host address, thus turning on all the host
bits.
---
  src/libvirt_private.syms |    2 +
src/util/network.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++
  src/util/network.h       |    6 +++++
  3 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a959ad9..19e581c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -558,6 +558,8 @@ virShrinkN;


  # network.h
+virSocketAddrBroadcast;
+virSocketAddrBroadcastByPrefix;
  virSocketAddrIsNetmask;
  virSocketAddrMask;
  virSocketAddrMaskByPrefix;
diff --git a/src/util/network.c b/src/util/network.c
index 61044fc..f58986e 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -341,6 +341,61 @@ virSocketAddrMaskByPrefix(virSocketAddrPtr addr, unsigned int prefix)
  }

  /**
+ * virSocketAddrBroadcast:
+ * @addr: address that needs to be turned into broadcast address (IPv4 only)
+ * @netmask: the netmask address
+ * @broadcast: virSocketAddr to recieve the broadcast address
+ *
+ * Mask ON the host bits of @addr according to @netmask, turning it
+ * into a broadcast address.
+ *
+ * Returns 0 in case of success, or -1 on error.
+ */
+int
+virSocketAddrBroadcast(const virSocketAddrPtr addr,
+                       const virSocketAddrPtr netmask,
+                       virSocketAddrPtr       broadcast)
+{
+    if ((addr->data.stor.ss_family != AF_INET) ||
+        (netmask->data.stor.ss_family != AF_INET)) {
+        broadcast->data.stor.ss_family = AF_UNSPEC;
+        return -1;
+    }
+
+    broadcast->data.stor.ss_family = AF_INET;
+    broadcast->len = addr->len;
+    broadcast->data.inet4.sin_addr.s_addr
+        = (addr->data.inet4.sin_addr.s_addr
+           | ~netmask->data.inet4.sin_addr.s_addr);
+    return 0;
+}
+
+/**
+ * virSocketAddrBroadcastByPrefix:
+ * @addr: address that needs to be turned into broadcast address (IPv4 only)
+ * @prefix: prefix (# of 1 bits) of netmask to apply
+ * @broadcast: virSocketAddr to recieve the broadcast address
+ *
+ * Mask off the host bits of @addr according to @prefix, turning it
+ * into a network address.
+ *
+ * Returns 0 in case of success, or -1 on error.
+ */
+int
+virSocketAddrBroadcastByPrefix(const virSocketAddrPtr addr,
+                               unsigned int           prefix,
+                               virSocketAddrPtr       broadcast)
+{
+    virSocketAddr netmask;
+
+    if (virSocketAddrPrefixToNetmask(prefix,&netmask,
+                                     addr->data.stor.ss_family)<  0)
+        return -1;
+
+    return virSocketAddrBroadcast(addr,&netmask, broadcast);
+}
+
+/**
   * virSocketCheckNetmask:
   * @addr1: a first network address
   * @addr2: a second network address
diff --git a/src/util/network.h b/src/util/network.h
index 2fcee43..bcbc607 100644
--- a/src/util/network.h
+++ b/src/util/network.h
@@ -77,6 +77,12 @@ int virSocketAddrMask     (virSocketAddrPtr addr,
                             const virSocketAddrPtr netmask);
  int virSocketAddrMaskByPrefix(virSocketAddrPtr addr,
                                unsigned int prefix);
+int virSocketAddrBroadcast(const virSocketAddrPtr addr,
+                           const virSocketAddrPtr netmask,
+                           virSocketAddrPtr       broadcast);
+int virSocketAddrBroadcastByPrefix(const virSocketAddrPtr addr,
+                                   unsigned int           prefix,
+                                   virSocketAddrPtr       broadcast);

  int virSocketGetNumNetmaskBits(const virSocketAddrPtr netmask);
  int virSocketAddrPrefixToNetmask(unsigned int prefix,
ACK.

Thanks, Stefan. Pushed.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]