Remove all use of inet_pton and inet_ntop

The  inet_pton and inet_ntop functions are obsolete, replaced
by getaddrinfo+getnameinfo with the AI_NUMERICHOST flag set.
These can be accessed via the virSocket APIs.

The bridge.c code had methods for fetching the IP address of
a bridge which used inet_ntop. Aside from the use of inet_ntop
these methods are broken, because a NIC can have multiple
addresses and this only returns one address. Since the methods
are never used, just remove them.

* src/conf/network_conf.c, src/nwfilter/nwfilter_learnipaddr.c:
  Replace inet_pton and inet_ntop with virSocket APIs
* src/util/bridge.c, src/util/bridge.h: Remove unused methods
  which called inet_ntop.
This commit is contained in:
Daniel P. Berrange 2010-10-21 11:13:05 +01:00
parent 640c5f1984
commit a8ae7d19f4
4 changed files with 43 additions and 131 deletions

View File

@ -278,7 +278,7 @@ virNetworkDHCPRangeDefParseXML(virNetworkDefPtr def,
xmlStrEqual(cur->name, BAD_CAST "host")) {
xmlChar *mac, *name, *ip;
unsigned char addr[6];
struct in_addr inaddress;
virSocketAddr inaddr;
mac = xmlGetProp(cur, BAD_CAST "mac");
if ((mac != NULL) &&
@ -305,10 +305,7 @@ virNetworkDHCPRangeDefParseXML(virNetworkDefPtr def,
continue;
}
ip = xmlGetProp(cur, BAD_CAST "ip");
if (inet_pton(AF_INET, (const char *) ip, &inaddress) <= 0) {
virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot parse IP address '%s'"),
ip);
if (virSocketParseAddr((const char *)ip, &inaddr, AF_UNSPEC) < 0) {
VIR_FREE(ip);
VIR_FREE(mac);
VIR_FREE(name);
@ -428,31 +425,34 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
def->netmask = virXPathString("string(./ip[1]/@netmask)", ctxt);
if (def->ipAddress &&
def->netmask) {
/* XXX someday we want IPv6 too, so inet_aton won't work there */
struct in_addr inaddress, innetmask;
virSocketAddr inaddress, innetmask;
char *netaddr;
xmlNodePtr ip;
if (inet_pton(AF_INET, def->ipAddress, &inaddress) <= 0) {
virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot parse IP address '%s'"),
def->ipAddress);
if (virSocketParseAddr(def->ipAddress, &inaddress, AF_UNSPEC) < 0)
goto error;
}
if (inet_pton(AF_INET, def->netmask, &innetmask) <= 0) {
virNetworkReportError(VIR_ERR_INTERNAL_ERROR,
_("cannot parse netmask '%s'"),
def->netmask);
if (virSocketParseAddr(def->netmask, &innetmask, AF_UNSPEC) < 0)
goto error;
/* XXX someday we want IPv6, so will need to relax this */
if (inaddress.data.sa.sa_family != AF_INET ||
innetmask.data.sa.sa_family != AF_INET) {
virNetworkReportError(VIR_ERR_CONFIG_UNSUPPORTED,
"%s", _("Only IPv4 addresses are supported"));
goto error;
}
inaddress.s_addr &= innetmask.s_addr;
netaddr = inet_ntoa(inaddress);
inaddress.data.inet4.sin_addr.s_addr &=
innetmask.data.inet4.sin_addr.s_addr;
if (!(netaddr = virSocketFormatAddr(&inaddress)))
goto error;
if (virAsprintf(&def->network, "%s/%s", netaddr, def->netmask) < 0) {
VIR_FREE(netaddr);
virReportOOMError();
goto error;
}
VIR_FREE(netaddr);
if ((ip = virXPathNode("./ip[1]", ctxt)) &&
virNetworkIPParseXML(def, ip) < 0)

View File

@ -627,22 +627,27 @@ learnIPAddressThread(void *arg)
if (req->status == 0) {
int ret;
char inetaddr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &vmaddr, inetaddr, sizeof(inetaddr));
virSocketAddr sa;
sa.len = sizeof(sa.data.inet4);
sa.data.inet4.sin_family = AF_INET;
sa.data.inet4.sin_addr.s_addr = vmaddr;
char *inetaddr;
virNWFilterAddIpAddrForIfname(req->ifname, strdup(inetaddr));
if ((inetaddr = virSocketFormatAddr(&sa))!= NULL) {
virNWFilterAddIpAddrForIfname(req->ifname, inetaddr);
ret = virNWFilterInstantiateFilterLate(NULL,
req->ifname,
req->ifindex,
req->linkdev,
req->nettype,
req->macaddr,
req->filtername,
req->filterparams,
req->driver);
VIR_DEBUG("Result from applying firewall rules on "
"%s with IP addr %s : %d\n", req->ifname, inetaddr, ret);
ret = virNWFilterInstantiateFilterLate(NULL,
req->ifname,
req->ifindex,
req->linkdev,
req->nettype,
req->macaddr,
req->filtername,
req->filterparams,
req->driver);
VIR_DEBUG("Result from applying firewall rules on "
"%s with IP addr %s : %d\n", req->ifname, inetaddr, ret);
}
} else {
if (showError)
virReportSystemError(req->status,

View File

@ -48,6 +48,7 @@
# include "memory.h"
# include "util.h"
# include "logging.h"
# include "network.h"
# define JIFFIES_TO_MS(j) (((j)*1000)/HZ)
# define MS_TO_JIFFIES(ms) (((ms)*HZ)/1000)
@ -660,13 +661,8 @@ brSetInetAddr(brControl *ctl,
int cmd,
const char *addr)
{
union {
struct sockaddr sa;
struct sockaddr_in sa_in;
} s;
virSocketAddr sa;
struct ifreq ifr;
struct in_addr inaddr;
int ret;
if (!ctl || !ctl->fd || !ifname || !addr)
return EINVAL;
@ -676,15 +672,13 @@ brSetInetAddr(brControl *ctl,
if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL)
return EINVAL;
if ((ret = inet_pton(AF_INET, addr, &inaddr)) < 0)
return errno;
else if (ret == 0)
if (virSocketParseAddr(addr, &sa, AF_UNSPEC) < 0)
return EINVAL;
s.sa_in.sin_family = AF_INET;
s.sa_in.sin_addr = inaddr;
if (sa.data.sa.sa_family != AF_INET)
return EINVAL;
ifr.ifr_addr = s.sa;
ifr.ifr_addr = sa.data.sa;
if (ioctl(ctl->fd, cmd, &ifr) < 0)
return errno;
@ -692,38 +686,6 @@ brSetInetAddr(brControl *ctl,
return 0;
}
static int
brGetInetAddr(brControl *ctl,
const char *ifname,
int cmd,
char *addr,
int maxlen)
{
struct ifreq ifr;
struct in_addr *inaddr;
if (!ctl || !ctl->fd || !ifname || !addr)
return EINVAL;
memset(&ifr, 0, sizeof(struct ifreq));
if (virStrcpyStatic(ifr.ifr_name, ifname) == NULL)
return EINVAL;
if (ioctl(ctl->fd, cmd, &ifr) < 0)
return errno;
if (maxlen < BR_INET_ADDR_MAXLEN || ifr.ifr_addr.sa_family != AF_INET)
return EFAULT;
inaddr = &((struct sockaddr_in *)&ifr.ifr_data)->sin_addr;
if (!inet_ntop(AF_INET, inaddr, addr, maxlen))
return errno;
return 0;
}
/**
* brSetInetAddress:
* @ctl: bridge control pointer
@ -745,29 +707,6 @@ brSetInetAddress(brControl *ctl,
return brSetInetAddr(ctl, ifname, SIOCSIFADDR, addr);
}
/**
* brGetInetAddress:
* @ctl: bridge control pointer
* @ifname: the interface name
* @addr: the array for the string representation of the IP address
* @maxlen: size of @addr in bytes
*
* Function to get the IP address of an interface, it should handle
* IPV4 and IPv6. The returned string for addr would be of the form
* "ddd.ddd.ddd.ddd" assuming the common IPv4 format.
*
* Returns 0 in case of success or an errno code in case of failure.
*/
int
brGetInetAddress(brControl *ctl,
const char *ifname,
char *addr,
int maxlen)
{
return brGetInetAddr(ctl, ifname, SIOCGIFADDR, addr, maxlen);
}
/**
* brSetInetNetmask:
* @ctl: bridge control pointer
@ -789,30 +728,6 @@ brSetInetNetmask(brControl *ctl,
return brSetInetAddr(ctl, ifname, SIOCSIFNETMASK, addr);
}
/**
* brGetInetNetmask:
* @ctl: bridge control pointer
* @ifname: the interface name
* @addr: the array for the string representation of the netmask
* @maxlen: size of @addr in bytes
*
* Function to get the netmask of an interface, it should handle
* IPV4 and IPv6. The returned string for addr would be of the form
* "ddd.ddd.ddd.ddd" assuming the common IPv4 format.
*
* Returns 0 in case of success or an errno code in case of failure.
*/
int
brGetInetNetmask(brControl *ctl,
const char *ifname,
char *addr,
int maxlen)
{
return brGetInetAddr(ctl, ifname, SIOCGIFNETMASK, addr, maxlen);
}
/**
* brSetForwardDelay:
* @ctl: bridge control pointer

View File

@ -85,17 +85,9 @@ int brGetInterfaceUp (brControl *ctl,
int brSetInetAddress (brControl *ctl,
const char *ifname,
const char *addr);
int brGetInetAddress (brControl *ctl,
const char *ifname,
char *addr,
int maxlen);
int brSetInetNetmask (brControl *ctl,
const char *ifname,
const char *netmask);
int brGetInetNetmask (brControl *ctl,
const char *ifname,
char *netmask,
int maxlen);
int brSetForwardDelay (brControl *ctl,
const char *bridge,