mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
Add new functions to virSocketAddr
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
This commit is contained in:
parent
f86880082d
commit
1614970ec5
@ -1452,6 +1452,7 @@ virRandomInitialize;
|
||||
virSocketAddrBroadcast;
|
||||
virSocketAddrBroadcastByPrefix;
|
||||
virSocketAddrCheckNetmask;
|
||||
virSocketAddrEqual;
|
||||
virSocketAddrFormat;
|
||||
virSocketAddrFormatFull;
|
||||
virSocketAddrGetPort;
|
||||
@ -1463,6 +1464,7 @@ virSocketAddrParse;
|
||||
virSocketAddrParseIPv4;
|
||||
virSocketAddrParseIPv6;
|
||||
virSocketAddrPrefixToNetmask;
|
||||
virSocketAddrSetIPv4Addr;
|
||||
virSocketAddrSetPort;
|
||||
|
||||
|
||||
|
@ -151,6 +151,51 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val) {
|
||||
return virSocketAddrParse(addr, val, AF_INET6);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
|
@ -66,6 +66,8 @@ int virSocketAddrParseIPv4(virSocketAddrPtr addr,
|
||||
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 virSocketAddrPtr netmask);
|
||||
int virSocketAddrPrefixToNetmask(unsigned int prefix,
|
||||
virSocketAddrPtr netmask,
|
||||
int family);
|
||||
bool virSocketAddrEqual(const virSocketAddrPtr s1,
|
||||
const virSocketAddrPtr s2);
|
||||
|
||||
#endif /* __VIR_SOCKETADDR_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user