On Tue, Aug 10, 2010 at 07:42:49PM +1000, Justin Clift wrote: > --- > src/util/network.c | 37 +++++++++++++++++++++++++++++++++++++ > src/util/network.h | 2 ++ > 2 files changed, 39 insertions(+), 0 deletions(-) > > diff --git a/src/util/network.c b/src/util/network.c > index 6e24792..2e0cf9c 100644 > --- a/src/util/network.c > +++ b/src/util/network.c > @@ -54,6 +54,43 @@ static int getIPv6Addr(virSocketAddrPtr addr, virIPv6AddrPtr tab) { > } > > /** > + * virIsNumericIPAddr: > + * @address: a text string containing a host name or IPv4/IPv6 address > + * > + * Given a text string, determines whether it contains a valid IPv4/IPv6 > + * address. > + * > + * Returns 0 if the given string is an IPv4 or IPv6 address, or non-zero > + * for any other condition. > + */ > +int > +virIsNumericIPAddr(const char *address) { > + int returnCode; > + struct addrinfo hints; > + struct addrinfo *res = NULL; > + > + /* Sanity check we've been given a text string */ > + if (address == NULL) > + return(-1); > + > + /* Do a name lookup on the given string, and see what we're given back */ > + memset(&hints, 0, sizeof(hints)); > + hints.ai_family = AF_UNSPEC; /* Doesn't matter whether IPv4 or IPv6 */ > + hints.ai_socktype = 0; /* Socket type doesn't matter */ > + hints.ai_flags = AI_NUMERICHOST; /* Force a numeric IP address check */ > + hints.ai_protocol = 0; /* Doesn't matter which protocol */ > + returnCode = getaddrinfo (address, NULL, &hints, &res); > + if (0 == returnCode) { > + /* A result was returned. This means the text string we were > + * given is a valid IP address. We now free the result(s) we > + * were given, and pass back the return code */ > + freeaddrinfo(res); > + } > + > + return returnCode; > +} Hum ... I think that it's better to allow virSocketParseAddr() to take a NULL Addr pointer it's very simple, I'm attaching the patch and then virIsNumericIPAddr(foo) can be replaced by virSocketParseAddr(foo, NULL, 0) it also has the extensibility of being able to cope with IPv4 or IPv6 only if one need, just by adjusting the hint. Am I right ? I see you do more initializations on hints.ai_family but the memset means the two functions are just the same for ai_socktype and ai_protocol, Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@xxxxxxxxxxxx | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/
diff --git a/src/util/network.c b/src/util/network.c index 6e24792..b17d419 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -56,7 +56,7 @@ static int getIPv6Addr(virSocketAddrPtr addr, virIPv6AddrPtr tab) { /** * virSocketParseAddr: * @val: a numeric network address IPv4 or IPv6 - * @addr: where to store the return value. + * @addr: where to store the return value, optional. * @hint: optional hint to pass down to getaddrinfo * * Mostly a wrapper for getaddrinfo() extracting the address storage @@ -70,7 +70,7 @@ virSocketParseAddr(const char *val, virSocketAddrPtr addr, int hint) { struct addrinfo hints; struct addrinfo *res = NULL; - if ((val == NULL) || (addr == NULL)) + if (val == NULL) return(-1); memset(&hints, 0, sizeof(hints)); @@ -80,7 +80,8 @@ virSocketParseAddr(const char *val, virSocketAddrPtr addr, int hint) { } len = res->ai_addrlen; - memcpy(&addr->stor, res->ai_addr, len); + if (addr != NULL) + memcpy(&addr->stor, res->ai_addr, len); freeaddrinfo(res); return(len);
-- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list