提交 af3d4eec 编写于 作者: D Daniel P. Berrange

Include length with virSocketAddr data

Some operations on socket addresses need to know the length of
the sockaddr struct for the particular address family. This
info was being discarded when passing around virSocketAddr
instances. Turn it from a union into a struct containing
union+socklen_t fields, so length is always kept around.

* src/util/network.h: Add socklen_t field to virSocketAddr
* src/util/network.c, src/network/bridge_driver.c,
  src/conf/domain_conf.c: Update to take account of new
  struct definition.
上级 9e42b40a
...@@ -2522,7 +2522,7 @@ virDomainChrDefParseTargetXML(virCapsPtr caps, ...@@ -2522,7 +2522,7 @@ virDomainChrDefParseTargetXML(virCapsPtr caps,
goto error; goto error;
} }
if (def->target.addr->stor.ss_family != AF_INET) { if (def->target.addr->data.stor.ss_family != AF_INET) {
virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED, virDomainReportError(VIR_ERR_CONFIG_UNSUPPORTED,
"%s", _("guestfwd channel only supports " "%s", _("guestfwd channel only supports "
"IPv4 addresses")); "IPv4 addresses"));
......
...@@ -1060,14 +1060,14 @@ static int networkCheckRouteCollision(virNetworkObjPtr network) ...@@ -1060,14 +1060,14 @@ static int networkCheckRouteCollision(virNetworkObjPtr network)
goto error; goto error;
} }
if (inaddress.stor.ss_family != AF_INET || if (inaddress.data.stor.ss_family != AF_INET ||
innetmask.stor.ss_family != AF_INET) { innetmask.data.stor.ss_family != AF_INET) {
/* Only support collision check for IPv4 */ /* Only support collision check for IPv4 */
goto out; goto out;
} }
net_dest = (inaddress.inet4.sin_addr.s_addr & net_dest = (inaddress.data.inet4.sin_addr.s_addr &
innetmask.inet4.sin_addr.s_addr); innetmask.data.inet4.sin_addr.s_addr);
/* Read whole routing table into memory */ /* Read whole routing table into memory */
if ((len = virFileReadAll(PROC_NET_ROUTE, MAX_ROUTE_SIZE, &buf)) < 0) if ((len = virFileReadAll(PROC_NET_ROUTE, MAX_ROUTE_SIZE, &buf)) < 0)
...@@ -1120,7 +1120,7 @@ static int networkCheckRouteCollision(virNetworkObjPtr network) ...@@ -1120,7 +1120,7 @@ static int networkCheckRouteCollision(virNetworkObjPtr network)
addr_val &= mask_val; addr_val &= mask_val;
if ((net_dest == addr_val) && if ((net_dest == addr_val) &&
(innetmask.inet4.sin_addr.s_addr == mask_val)) { (innetmask.data.inet4.sin_addr.s_addr == mask_val)) {
networkReportError(VIR_ERR_INTERNAL_ERROR, networkReportError(VIR_ERR_INTERNAL_ERROR,
_("Network %s/%s is already in use by " _("Network %s/%s is already in use by "
"interface %s"), "interface %s"),
......
...@@ -27,10 +27,10 @@ static int getIPv4Addr(virSocketAddrPtr addr, virIPv4AddrPtr tab) { ...@@ -27,10 +27,10 @@ static int getIPv4Addr(virSocketAddrPtr addr, virIPv4AddrPtr tab) {
unsigned long val; unsigned long val;
int i; int i;
if ((addr == NULL) || (tab == NULL) || (addr->stor.ss_family != AF_INET)) if ((addr == NULL) || (tab == NULL) || (addr->data.stor.ss_family != AF_INET))
return(-1); return(-1);
val = ntohl(addr->inet4.sin_addr.s_addr); val = ntohl(addr->data.inet4.sin_addr.s_addr);
for (i = 0;i < 4;i++) { for (i = 0;i < 4;i++) {
(*tab)[3 - i] = val & 0xFF; (*tab)[3 - i] = val & 0xFF;
...@@ -43,12 +43,12 @@ static int getIPv4Addr(virSocketAddrPtr addr, virIPv4AddrPtr tab) { ...@@ -43,12 +43,12 @@ static int getIPv4Addr(virSocketAddrPtr addr, virIPv4AddrPtr tab) {
static int getIPv6Addr(virSocketAddrPtr addr, virIPv6AddrPtr tab) { static int getIPv6Addr(virSocketAddrPtr addr, virIPv6AddrPtr tab) {
int i; int i;
if ((addr == NULL) || (tab == NULL) || (addr->stor.ss_family != AF_INET6)) if ((addr == NULL) || (tab == NULL) || (addr->data.stor.ss_family != AF_INET6))
return(-1); return(-1);
for (i = 0;i < 8;i++) { for (i = 0;i < 8;i++) {
(*tab)[i] = ((addr->inet6.sin6_addr.s6_addr[2 * i] << 8) | (*tab)[i] = ((addr->data.inet6.sin6_addr.s6_addr[2 * i] << 8) |
addr->inet6.sin6_addr.s6_addr[2 * i + 1]); addr->data.inet6.sin6_addr.s6_addr[2 * i + 1]);
} }
return(0); return(0);
...@@ -81,8 +81,10 @@ virSocketParseAddr(const char *val, virSocketAddrPtr addr, int hint) { ...@@ -81,8 +81,10 @@ virSocketParseAddr(const char *val, virSocketAddrPtr addr, int hint) {
} }
len = res->ai_addrlen; len = res->ai_addrlen;
if (addr != NULL) if (addr != NULL) {
memcpy(&addr->stor, res->ai_addr, len); memcpy(&addr->data.stor, res->ai_addr, len);
addr->len = res->ai_addrlen;
}
freeaddrinfo(res); freeaddrinfo(res);
return(len); return(len);
...@@ -133,14 +135,14 @@ virSocketFormatAddr(virSocketAddrPtr addr) { ...@@ -133,14 +135,14 @@ virSocketFormatAddr(virSocketAddrPtr addr) {
if (addr == NULL) if (addr == NULL)
return NULL; return NULL;
if (addr->stor.ss_family == AF_INET) { if (addr->data.stor.ss_family == AF_INET) {
outlen = INET_ADDRSTRLEN; outlen = INET_ADDRSTRLEN;
inaddr = &addr->inet4.sin_addr; inaddr = &addr->data.inet4.sin_addr;
} }
else if (addr->stor.ss_family == AF_INET6) { else if (addr->data.stor.ss_family == AF_INET6) {
outlen = INET6_ADDRSTRLEN; outlen = INET6_ADDRSTRLEN;
inaddr = &addr->inet6.sin6_addr; inaddr = &addr->data.inet6.sin6_addr;
} }
else { else {
...@@ -150,7 +152,7 @@ virSocketFormatAddr(virSocketAddrPtr addr) { ...@@ -150,7 +152,7 @@ virSocketFormatAddr(virSocketAddrPtr addr) {
if (VIR_ALLOC_N(out, outlen) < 0) if (VIR_ALLOC_N(out, outlen) < 0)
return NULL; return NULL;
if (inet_ntop(addr->stor.ss_family, inaddr, out, outlen) == NULL) { if (inet_ntop(addr->data.stor.ss_family, inaddr, out, outlen) == NULL) {
VIR_FREE(out); VIR_FREE(out);
return NULL; return NULL;
} }
...@@ -174,12 +176,12 @@ virSocketSetPort(virSocketAddrPtr addr, int port) { ...@@ -174,12 +176,12 @@ virSocketSetPort(virSocketAddrPtr addr, int port) {
port = htons(port); port = htons(port);
if(addr->stor.ss_family == AF_INET) { if(addr->data.stor.ss_family == AF_INET) {
addr->inet4.sin_port = port; addr->data.inet4.sin_port = port;
} }
else if(addr->stor.ss_family == AF_INET6) { else if(addr->data.stor.ss_family == AF_INET6) {
addr->inet6.sin6_port = port; addr->data.inet6.sin6_port = port;
} }
else { else {
...@@ -201,12 +203,12 @@ virSocketGetPort(virSocketAddrPtr addr) { ...@@ -201,12 +203,12 @@ virSocketGetPort(virSocketAddrPtr addr) {
if (addr == NULL) if (addr == NULL)
return -1; return -1;
if(addr->stor.ss_family == AF_INET) { if(addr->data.stor.ss_family == AF_INET) {
return ntohs(addr->inet4.sin_port); return ntohs(addr->data.inet4.sin_port);
} }
else if(addr->stor.ss_family == AF_INET6) { else if(addr->data.stor.ss_family == AF_INET6) {
return ntohs(addr->inet6.sin6_port); return ntohs(addr->data.inet6.sin6_port);
} }
return -1; return -1;
...@@ -245,14 +247,14 @@ int virSocketCheckNetmask(virSocketAddrPtr addr1, virSocketAddrPtr addr2, ...@@ -245,14 +247,14 @@ int virSocketCheckNetmask(virSocketAddrPtr addr1, virSocketAddrPtr addr2,
if ((addr1 == NULL) || (addr2 == NULL) || (netmask == NULL)) if ((addr1 == NULL) || (addr2 == NULL) || (netmask == NULL))
return(-1); return(-1);
if ((addr1->stor.ss_family != addr2->stor.ss_family) || if ((addr1->data.stor.ss_family != addr2->data.stor.ss_family) ||
(addr1->stor.ss_family != netmask->stor.ss_family)) (addr1->data.stor.ss_family != netmask->data.stor.ss_family))
return(-1); return(-1);
if (virSocketAddrIsNetmask(netmask) != 0) if (virSocketAddrIsNetmask(netmask) != 0)
return(-1); return(-1);
if (addr1->stor.ss_family == AF_INET) { if (addr1->data.stor.ss_family == AF_INET) {
virIPv4Addr t1, t2, tm; virIPv4Addr t1, t2, tm;
if ((getIPv4Addr(addr1, &t1) < 0) || if ((getIPv4Addr(addr1, &t1) < 0) ||
...@@ -265,7 +267,7 @@ int virSocketCheckNetmask(virSocketAddrPtr addr1, virSocketAddrPtr addr2, ...@@ -265,7 +267,7 @@ int virSocketCheckNetmask(virSocketAddrPtr addr1, virSocketAddrPtr addr2,
return(0); return(0);
} }
} else if (addr1->stor.ss_family == AF_INET) { } else if (addr1->data.stor.ss_family == AF_INET) {
virIPv6Addr t1, t2, tm; virIPv6Addr t1, t2, tm;
if ((getIPv6Addr(addr1, &t1) < 0) || if ((getIPv6Addr(addr1, &t1) < 0) ||
...@@ -301,10 +303,10 @@ int virSocketGetRange(virSocketAddrPtr start, virSocketAddrPtr end) { ...@@ -301,10 +303,10 @@ int virSocketGetRange(virSocketAddrPtr start, virSocketAddrPtr end) {
if ((start == NULL) || (end == NULL)) if ((start == NULL) || (end == NULL))
return(-1); return(-1);
if (start->stor.ss_family != end->stor.ss_family) if (start->data.stor.ss_family != end->data.stor.ss_family)
return(-1); return(-1);
if (start->stor.ss_family == AF_INET) { if (start->data.stor.ss_family == AF_INET) {
virIPv4Addr t1, t2; virIPv4Addr t1, t2;
if ((getIPv4Addr(start, &t1) < 0) || if ((getIPv4Addr(start, &t1) < 0) ||
...@@ -319,7 +321,7 @@ int virSocketGetRange(virSocketAddrPtr start, virSocketAddrPtr end) { ...@@ -319,7 +321,7 @@ int virSocketGetRange(virSocketAddrPtr start, virSocketAddrPtr end) {
if (ret < 0) if (ret < 0)
return(-1); return(-1);
ret++; ret++;
} else if (start->stor.ss_family == AF_INET6) { } else if (start->data.stor.ss_family == AF_INET6) {
virIPv6Addr t1, t2; virIPv6Addr t1, t2;
if ((getIPv6Addr(start, &t1) < 0) || if ((getIPv6Addr(start, &t1) < 0) ||
...@@ -355,7 +357,7 @@ int virSocketGetNumNetmaskBits(const virSocketAddrPtr netmask) ...@@ -355,7 +357,7 @@ int virSocketGetNumNetmaskBits(const virSocketAddrPtr netmask)
int i, j; int i, j;
int c = 0; int c = 0;
if (netmask->stor.ss_family == AF_INET) { if (netmask->data.stor.ss_family == AF_INET) {
virIPv4Addr tm; virIPv4Addr tm;
uint8_t bit; uint8_t bit;
...@@ -389,7 +391,7 @@ int virSocketGetNumNetmaskBits(const virSocketAddrPtr netmask) ...@@ -389,7 +391,7 @@ int virSocketGetNumNetmaskBits(const virSocketAddrPtr netmask)
} }
return c; return c;
} else if (netmask->stor.ss_family == AF_INET6) { } else if (netmask->data.stor.ss_family == AF_INET6) {
virIPv6Addr tm; virIPv6Addr tm;
uint16_t bit; uint16_t bit;
......
...@@ -17,11 +17,15 @@ ...@@ -17,11 +17,15 @@
# include <sys/socket.h> # include <sys/socket.h>
# include <netdb.h> # include <netdb.h>
typedef union { typedef struct {
union {
struct sockaddr_storage stor; struct sockaddr_storage stor;
struct sockaddr_in inet4; struct sockaddr_in inet4;
struct sockaddr_in6 inet6; struct sockaddr_in6 inet6;
} data;
socklen_t len;
} virSocketAddr; } virSocketAddr;
typedef virSocketAddr *virSocketAddrPtr; typedef virSocketAddr *virSocketAddrPtr;
int virSocketParseAddr (const char *val, int virSocketParseAddr (const char *val,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册