提交 573c41a2 编写于 作者: M Martin Kletzander

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: NMartin Kletzander <mkletzan@redhat.com>
上级 6d28ef91
......@@ -2168,7 +2168,9 @@ virSocketAddrParseIPv4;
virSocketAddrParseIPv6;
virSocketAddrPrefixToNetmask;
virSocketAddrSetIPv4Addr;
virSocketAddrSetIPv4AddrNetOrder;
virSocketAddrSetIPv6Addr;
virSocketAddrSetIPv6AddrNetOrder;
virSocketAddrSetPort;
# util/virstats.h
......
......@@ -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;
......
......@@ -172,6 +172,22 @@ virSocketAddrParseIPv6(virSocketAddrPtr addr, const char *val)
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:
* @addr: the location to store the result
......@@ -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);
}
/*
......
......@@ -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);
......
......@@ -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))) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册