mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-10-30 01:43:23 +00:00
util: Add virSocketAddrSetIPv[46]AddrNetOrder and use it
This allows setting the address in host and/or network order and makes the naming consistent. Now you don't need to call [hn]to[nh]l() functions as that is taken care of by these functions. Also, now the *NetOrder take the address in network order, the other functions in host order so the naming and usage is consistent. Some places were having the address in network order and calling ntohl() just so the original function can call htonl() again. This makes it nicer to read. Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
parent
6d28ef912c
commit
573c41a275
@ -2168,7 +2168,9 @@ virSocketAddrParseIPv4;
|
|||||||
virSocketAddrParseIPv6;
|
virSocketAddrParseIPv6;
|
||||||
virSocketAddrPrefixToNetmask;
|
virSocketAddrPrefixToNetmask;
|
||||||
virSocketAddrSetIPv4Addr;
|
virSocketAddrSetIPv4Addr;
|
||||||
|
virSocketAddrSetIPv4AddrNetOrder;
|
||||||
virSocketAddrSetIPv6Addr;
|
virSocketAddrSetIPv6Addr;
|
||||||
|
virSocketAddrSetIPv6AddrNetOrder;
|
||||||
virSocketAddrSetPort;
|
virSocketAddrSetPort;
|
||||||
|
|
||||||
# util/virstats.h
|
# util/virstats.h
|
||||||
|
@ -1025,10 +1025,10 @@ virNWFilterSnoopDHCPDecode(virNWFilterSnoopReqPtr req,
|
|||||||
memset(&ipl, 0, sizeof(ipl));
|
memset(&ipl, 0, sizeof(ipl));
|
||||||
|
|
||||||
memcpy(&nwint, &pd->d_yiaddr, sizeof(nwint));
|
memcpy(&nwint, &pd->d_yiaddr, sizeof(nwint));
|
||||||
virSocketAddrSetIPv4Addr(&ipl.ipAddress, ntohl(nwint));
|
virSocketAddrSetIPv4AddrNetOrder(&ipl.ipAddress, nwint);
|
||||||
|
|
||||||
memcpy(&nwint, &pd->d_siaddr, sizeof(nwint));
|
memcpy(&nwint, &pd->d_siaddr, sizeof(nwint));
|
||||||
virSocketAddrSetIPv4Addr(&ipl.ipServer, ntohl(nwint));
|
virSocketAddrSetIPv4AddrNetOrder(&ipl.ipServer, nwint);
|
||||||
|
|
||||||
if (leasetime == ~0)
|
if (leasetime == ~0)
|
||||||
ipl.timeout = ~0;
|
ipl.timeout = ~0;
|
||||||
|
@ -172,6 +172,22 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val)
|
|||||||
return virSocketAddrParse(addr, val, AF_INET6);
|
return virSocketAddrParse(addr, val, AF_INET6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* virSocketAddrSetIPv4AddrNetOrder:
|
||||||
|
* @addr: the location to store the result
|
||||||
|
* @val: the 32bit 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:
|
* virSocketAddrSetIPv4Addr:
|
||||||
* @addr: the location to store the result
|
* @addr: the location to store the result
|
||||||
@ -183,9 +199,22 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val)
|
|||||||
void
|
void
|
||||||
virSocketAddrSetIPv4Addr(virSocketAddrPtr addr, uint32_t val)
|
virSocketAddrSetIPv4Addr(virSocketAddrPtr addr, uint32_t val)
|
||||||
{
|
{
|
||||||
addr->data.stor.ss_family = AF_INET;
|
virSocketAddrSetIPv4AddrNetOrder(addr, htonl(val));
|
||||||
addr->data.inet4.sin_addr.s_addr = htonl(val);
|
}
|
||||||
addr->len = sizeof(struct sockaddr_in);
|
|
||||||
|
/*
|
||||||
|
* 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])
|
void virSocketAddrSetIPv6Addr(virSocketAddrPtr addr, uint32_t val[4])
|
||||||
{
|
{
|
||||||
addr->data.stor.ss_family = AF_INET6;
|
size_t i = 0;
|
||||||
memcpy(addr->data.inet6.sin6_addr.s6_addr, val, 4 * sizeof(*val));
|
uint32_t host_val[4];
|
||||||
addr->len = sizeof(struct sockaddr_in6);
|
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
host_val[i] = htonl(val[i]);
|
||||||
|
|
||||||
|
virSocketAddrSetIPv6AddrNetOrder(addr, host_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -84,7 +84,9 @@ int virSocketAddrParseIPv4(virSocketAddrPtr addr,
|
|||||||
int virSocketAddrParseIPv6(virSocketAddrPtr addr,
|
int virSocketAddrParseIPv6(virSocketAddrPtr addr,
|
||||||
const char *val);
|
const char *val);
|
||||||
|
|
||||||
|
void virSocketAddrSetIPv4AddrNetOrder(virSocketAddrPtr s, uint32_t addr);
|
||||||
void virSocketAddrSetIPv4Addr(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]);
|
void virSocketAddrSetIPv6Addr(virSocketAddrPtr s, uint32_t addr[4]);
|
||||||
|
|
||||||
char *virSocketAddrFormat(const virSocketAddr *addr);
|
char *virSocketAddrFormat(const virSocketAddr *addr);
|
||||||
|
@ -126,15 +126,14 @@ testGetHostByName(const void *opaque)
|
|||||||
while (*addrList) {
|
while (*addrList) {
|
||||||
virSocketAddr sa;
|
virSocketAddr sa;
|
||||||
char *ipAddr;
|
char *ipAddr;
|
||||||
|
void *address = *addrList;
|
||||||
|
|
||||||
memset(&sa, 0, sizeof(sa));
|
memset(&sa, 0, sizeof(sa));
|
||||||
|
|
||||||
if (resolved.h_addrtype == AF_INET) {
|
if (resolved.h_addrtype == AF_INET) {
|
||||||
/* For some reason, virSocketAddrSetIPv4Addr does htonl() conversion.
|
virSocketAddrSetIPv4AddrNetOrder(&sa, *((uint32_t *) address));
|
||||||
* But the data we already have is in network order. */
|
|
||||||
virSocketAddrSetIPv4Addr(&sa, ntohl(*((uint32_t *) *addrList)));
|
|
||||||
} else {
|
} else {
|
||||||
virSocketAddrSetIPv6Addr(&sa, (uint32_t *) *addrList);
|
virSocketAddrSetIPv6AddrNetOrder(&sa, address);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(ipAddr = virSocketAddrFormat(&sa))) {
|
if (!(ipAddr = virSocketAddrFormat(&sa))) {
|
||||||
|
Loading…
Reference in New Issue
Block a user