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
--- Changes since v12: - fixed number of bytes compared when checking addresses for equality --- src/libvirt_private.syms | 2 ++ src/util/virsocketaddr.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/util/virsocketaddr.h | 4 ++++ 3 files changed, 51 insertions(+) Index: libvirt/src/libvirt_private.syms =================================================================== --- libvirt.orig/src/libvirt_private.syms +++ libvirt/src/libvirt_private.syms @@ -1452,6 +1452,7 @@ virRandomInitialize; virSocketAddrBroadcast; virSocketAddrBroadcastByPrefix; virSocketAddrCheckNetmask; +virSocketAddrEqual; virSocketAddrFormat; virSocketAddrFormatFull; virSocketAddrGetPort; @@ -1463,6 +1464,7 @@ virSocketAddrParse; virSocketAddrParseIPv4; virSocketAddrParseIPv6; virSocketAddrPrefixToNetmask; +virSocketAddrSetIPv4Addr; virSocketAddrSetPort; Index: libvirt/src/util/virsocketaddr.c =================================================================== --- libvirt.orig/src/util/virsocketaddr.c +++ libvirt/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, + sizeof(s1->data.inet4.sin_addr.s_addr)) == 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, + sizeof(s1->data.inet6.sin6_addr.s6_addr)) == 0 && + s1->data.inet6.sin6_port == s2->data.inet6.sin6_port); + } + return false; +} + +/* * virSocketAddrFormat: * @addr: an initialized virSocketAddrPtr * Index: libvirt/src/util/virsocketaddr.h =================================================================== --- libvirt.orig/src/util/virsocketaddr.h +++ libvirt/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@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list