[libvirt] [PATCH] 1/3 Add IPv4 and IPv6 parsing routines

Daniel P. Berrange berrange at redhat.com
Wed Oct 14 09:49:20 UTC 2009


On Tue, Oct 13, 2009 at 04:12:47PM +0200, Daniel Veillard wrote:
>     * src/util/util.h src/util/util.c: two new functions virParseIPv4
>       and virParseIPv6

I think this should just be a thin wrapper around getaddrinfo()
which already knows how to parse all possible address types.
If we avoid a custom typedef, and just use 'struct sockaddr_storage'
this in turn makes it easy for callers to pass the result straight
into any socket API calls they might use. eg this could all be done
with a couple of lines of code

  int virSocketAddr(const char *str, struct sockaddr_storage *ss)
  {
    int len;
    struct addrinfo hints = { 0 };
    struct addrinfo *res = NULL;

    hints.ai_flags = AI_NUMERICHOST;

    if (getaddrinfo(str, NULL, &hints, &res) != 0 || !res)
      return -1;

    len = res->addrlen;
    memcpy(ss, res->ai_addr, len);

    freeaddrinfo(res);
    return len;
  }

That would automatically cope with both IPv4 / 6 addresses. If
we want to restrict it we could add a 3rd argument, 'int family'
and use that to set  hints.ai_family field - the caller would
just pass AF_INET or AF_INET6 to specify a particular type, or
leave it at 0 to allow any type.

It is probably worth introducing a 'src/util/socketaddr.h' file
for this, since I think we'll need more socket addr functions
later

Daniel
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list