[PATCH V12 1/5] [PATCH] Add new functions to virSocketAddr

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

 



Add 2 new functions to the virSocketAddr 'class':

- virSocketAddrEqual: tests whether two IP addresses and their ports are equal
- virSocketaddSetIPv4Addr: set a virSocketAddr given a 32 bit int

---
 src/libvirt_private.syms |    2 ++
 src/util/virsocketaddr.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 src/util/virsocketaddr.h |    4 ++++
 3 files changed, 51 insertions(+)

Index: libvirt-acl/src/libvirt_private.syms
===================================================================
--- libvirt-acl.orig/src/libvirt_private.syms
+++ libvirt-acl/src/libvirt_private.syms
@@ -1448,6 +1448,7 @@ virRandomInitialize;
 virSocketAddrBroadcast;
 virSocketAddrBroadcastByPrefix;
 virSocketAddrCheckNetmask;
+virSocketAddrEqual;
 virSocketAddrFormat;
 virSocketAddrFormatFull;
 virSocketAddrGetPort;
@@ -1459,6 +1460,7 @@ virSocketAddrParse;
 virSocketAddrParseIPv4;
 virSocketAddrParseIPv6;
 virSocketAddrPrefixToNetmask;
+virSocketAddrSetIPv4Addr;
 virSocketAddrSetPort;
 
 
Index: libvirt-acl/src/util/virsocketaddr.c
===================================================================
--- libvirt-acl.orig/src/util/virsocketaddr.c
+++ libvirt-acl/src/util/virsocketaddr.c
@@ -152,6 +152,51 @@ virSocketAddrParseIPv6(virSocketAddrPtr 
 }
 
 /*
+ * virSocketAddrSetIPv4Addr:
+ * @addr: the location to store the result
+ * @val: the 32bit integer in host byte order representing the IPv4 address
+ *
+ * Set the IPv4 address given an integer in host order. This function does not
+ * touch any previously set port.
+ */
+void
+virSocketAddrSetIPv4Addr(virSocketAddrPtr addr, uint32_t val)
+{
+    addr->data.stor.ss_family = AF_INET;
+    addr->data.inet4.sin_addr.s_addr = htonl(val);
+    addr->len = sizeof(struct sockaddr_in);
+}
+
+/*
+ * virSocketAddrEqual:
+ * @s1: the location of the one IP address
+ * @s2: the location of the other IP address
+ *
+ * Compare two IP addresses for equality. Two addresses are equal
+ * if their IP addresses and ports are equal.
+ */
+bool
+virSocketAddrEqual(const virSocketAddrPtr s1, const virSocketAddrPtr s2)
+{
+    if (s1->data.stor.ss_family != s2->data.stor.ss_family)
+        return false;
+
+    switch (s1->data.stor.ss_family) {
+    case AF_INET:
+        return (memcmp(&s1->data.inet4.sin_addr.s_addr,
+                       &s2->data.inet4.sin_addr.s_addr,
+                       4) == 0 &&
+                s1->data.inet4.sin_port == s2->data.inet4.sin_port);
+    case AF_INET6:
+        return (memcmp(&s1->data.inet6.sin6_addr.s6_addr,
+                       &s2->data.inet6.sin6_addr.s6_addr,
+                       4) == 0 &&
+                s1->data.inet6.sin6_port == s2->data.inet6.sin6_port);
+    }
+    return false;
+}
+
+/*
  * virSocketAddrFormat:
  * @addr: an initialized virSocketAddrPtr
  *
Index: libvirt-acl/src/util/virsocketaddr.h
===================================================================
--- libvirt-acl.orig/src/util/virsocketaddr.h
+++ libvirt-acl/src/util/virsocketaddr.h
@@ -66,6 +66,8 @@ int virSocketAddrParseIPv4(virSocketAddr
 int virSocketAddrParseIPv6(virSocketAddrPtr addr,
                            const char *val);
 
+void virSocketAddrSetIPv4Addr(const virSocketAddrPtr s, uint32_t addr);
+
 char * virSocketAddrFormat(virSocketAddrPtr addr);
 char * virSocketAddrFormatFull(virSocketAddrPtr addr,
                                bool withService,
@@ -100,5 +102,7 @@ int virSocketAddrGetNumNetmaskBits(const
 int virSocketAddrPrefixToNetmask(unsigned int prefix,
                                  virSocketAddrPtr netmask,
                                  int family);
+bool virSocketAddrEqual(const virSocketAddrPtr s1,
+                        const virSocketAddrPtr s2);
 
 #endif /* __VIR_SOCKETADDR_H__ */

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]