[PATCH 1/2] util: Add virSocketAddrSetIPv[46]AddrNetOrder and use it

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

 



This allows setting the address if you already have the address in
network byte order.  Some places were having the address in network
order and calling ntohl() just so the original function can call htonl()
again.  Let's call the new one to make clear what's happening.

Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx>
---
 src/libvirt_private.syms          |  2 ++
 src/nwfilter/nwfilter_dhcpsnoop.c |  4 ++--
 src/util/virsocketaddr.c          | 45 +++++++++++++++++++++++++++++++++------
 src/util/virsocketaddr.h          |  2 ++
 tests/nsstest.c                   |  7 +++---
 5 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ff803f996ac3..cfa3835c4ac4 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2168,7 +2168,9 @@ virSocketAddrParseIPv4;
 virSocketAddrParseIPv6;
 virSocketAddrPrefixToNetmask;
 virSocketAddrSetIPv4Addr;
+virSocketAddrSetIPv4AddrNetOrder;
 virSocketAddrSetIPv6Addr;
+virSocketAddrSetIPv6AddrNetOrder;
 virSocketAddrSetPort;

 # util/virstats.h
diff --git a/src/nwfilter/nwfilter_dhcpsnoop.c b/src/nwfilter/nwfilter_dhcpsnoop.c
index c671cd88184d..702abe18d122 100644
--- a/src/nwfilter/nwfilter_dhcpsnoop.c
+++ b/src/nwfilter/nwfilter_dhcpsnoop.c
@@ -1025,10 +1025,10 @@ virNWFilterSnoopDHCPDecode(virNWFilterSnoopReqPtr req,
     memset(&ipl, 0, sizeof(ipl));

     memcpy(&nwint, &pd->d_yiaddr, sizeof(nwint));
-    virSocketAddrSetIPv4Addr(&ipl.ipAddress, ntohl(nwint));
+    virSocketAddrSetIPv4AddrNetOrder(&ipl.ipAddress, nwint);

     memcpy(&nwint, &pd->d_siaddr, sizeof(nwint));
-    virSocketAddrSetIPv4Addr(&ipl.ipServer, ntohl(nwint));
+    virSocketAddrSetIPv4AddrNetOrder(&ipl.ipServer, nwint);

     if (leasetime == ~0)
         ipl.timeout = ~0;
diff --git a/src/util/virsocketaddr.c b/src/util/virsocketaddr.c
index b44d12e65216..53a8af898ead 100644
--- a/src/util/virsocketaddr.c
+++ b/src/util/virsocketaddr.c
@@ -173,6 +173,22 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val)
 }

 /*
+ * virSocketAddrSetIPv4AddrNetOrder:
+ * @addr: the location to store the result
+ * @val: the 128bit integer in network byte order representing the IPv4 address
+ *
+ * Set the IPv4 address given an integer in network order. This function does not
+ * touch any previously set port.
+ */
+void
+virSocketAddrSetIPv4AddrNetOrder(virSocketAddrPtr addr, uint32_t val)
+{
+    addr->data.stor.ss_family = AF_INET;
+    addr->data.inet4.sin_addr.s_addr = val;
+    addr->len = sizeof(struct sockaddr_in);
+}
+
+/*
  * virSocketAddrSetIPv4Addr:
  * @addr: the location to store the result
  * @val: the 32bit integer in host byte order representing the IPv4 address
@@ -183,9 +199,22 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val)
 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);
+    virSocketAddrSetIPv4AddrNetOrder(addr, htonl(val));
+}
+
+/*
+ * virSocketAddrSetIPv6AddrNetOrder:
+ * @addr: the location to store the result
+ * @val: the 128bit integer in network byte order representing the IPv6 address
+ *
+ * Set the IPv6 address given an integer in network order. This function does not
+ * touch any previously set port.
+ */
+void virSocketAddrSetIPv6AddrNetOrder(virSocketAddrPtr addr, uint32_t val[4])
+{
+    addr->data.stor.ss_family = AF_INET6;
+    memcpy(addr->data.inet6.sin6_addr.s6_addr, val, 4 * sizeof(*val));
+    addr->len = sizeof(struct sockaddr_in6);
 }

 /*
@@ -198,9 +227,13 @@ virSocketAddrSetIPv4Addr(virSocketAddrPtr addr, uint32_t val)
  */
 void virSocketAddrSetIPv6Addr(virSocketAddrPtr addr, uint32_t val[4])
 {
-    addr->data.stor.ss_family = AF_INET6;
-    memcpy(addr->data.inet6.sin6_addr.s6_addr, val, 4 * sizeof(*val));
-    addr->len = sizeof(struct sockaddr_in6);
+    size_t i = 0;
+    uint32_t host_val[4];
+
+    for (i = 0; i < 4; i++)
+        host_val[i] = htonl(val[i]);
+
+    virSocketAddrSetIPv6AddrNetOrder(addr, host_val);
 }

 /*
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index 9a6e1ecfa08a..c7aaa613b910 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -84,7 +84,9 @@ int virSocketAddrParseIPv4(virSocketAddrPtr addr,
 int virSocketAddrParseIPv6(virSocketAddrPtr addr,
                            const char *val);

+void virSocketAddrSetIPv4AddrNetOrder(virSocketAddrPtr s, uint32_t addr);
 void virSocketAddrSetIPv4Addr(virSocketAddrPtr s, uint32_t addr);
+void virSocketAddrSetIPv6AddrNetOrder(virSocketAddrPtr s, uint32_t addr[4]);
 void virSocketAddrSetIPv6Addr(virSocketAddrPtr s, uint32_t addr[4]);

 char *virSocketAddrFormat(const virSocketAddr *addr);
diff --git a/tests/nsstest.c b/tests/nsstest.c
index 340f313616c5..68f1c600f6ec 100644
--- a/tests/nsstest.c
+++ b/tests/nsstest.c
@@ -126,15 +126,14 @@ testGetHostByName(const void *opaque)
     while (*addrList) {
         virSocketAddr sa;
         char *ipAddr;
+        void *address = *addrList;

         memset(&sa, 0, sizeof(sa));

         if (resolved.h_addrtype == AF_INET) {
-            /* For some reason, virSocketAddrSetIPv4Addr does htonl() conversion.
-             * But the data we already have is in network order. */
-            virSocketAddrSetIPv4Addr(&sa, ntohl(*((uint32_t *) *addrList)));
+            virSocketAddrSetIPv4AddrNetOrder(&sa, *((uint32_t *) address));
         } else {
-            virSocketAddrSetIPv6Addr(&sa, (uint32_t *) *addrList);
+            virSocketAddrSetIPv6AddrNetOrder(&sa, address);
         }

         if (!(ipAddr = virSocketAddrFormat(&sa))) {
-- 
2.7.3

--
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]