libnl 1.1
Abstract Address
Utilities

Creating Abstract Addresses

struct nl_addr * nl_addr_alloc (size_t maxsize)
 Allocate new abstract address object.
struct nl_addr * nl_addr_build (int family, void *buf, size_t size)
 Allocate new abstract address object based on a binary address.
struct nl_addr * nl_addr_parse (const char *addrstr, int hint)
 Allocate abstract address object based on a character string.
struct nl_addr * nl_addr_clone (struct nl_addr *addr)
 Clone existing abstract address object.

Destroying Abstract Addresses

void nl_addr_destroy (struct nl_addr *addr)
 Destroy abstract address object.

Managing Usage References

struct nl_addr * nl_addr_get (struct nl_addr *addr)
void nl_addr_put (struct nl_addr *addr)
int nl_addr_shared (struct nl_addr *addr)
 Check whether an abstract address object is shared.

Miscellaneous

int nl_addr_cmp (struct nl_addr *a, struct nl_addr *b)
 Compares two abstract address objects.
int nl_addr_cmp_prefix (struct nl_addr *a, struct nl_addr *b)
 Compares the prefix of two abstract address objects.
int nl_addr_iszero (struct nl_addr *addr)
 Returns true if the address consists of all zeros.
int nl_addr_valid (char *addr, int family)
 Check if an address matches a certain family.
int nl_addr_guess_family (struct nl_addr *addr)
 Guess address family of an abstract address object based on address size.
int nl_addr_fill_sockaddr (struct nl_addr *addr, struct sockaddr *sa, socklen_t *salen)
 Fill out sockaddr structure with values from abstract address object.

Getting Information About Addresses

struct addrinfo * nl_addr_info (struct nl_addr *addr)
 Call getaddrinfo() for an abstract address object.
int nl_addr_resolve (struct nl_addr *addr, char *host, size_t hostlen)
 Resolve abstract address object to a name using getnameinfo().

Attributes

void nl_addr_set_family (struct nl_addr *addr, int family)
int nl_addr_get_family (struct nl_addr *addr)
int nl_addr_set_binary_addr (struct nl_addr *addr, void *buf, size_t len)
 Set binary address of abstract address object.
void * nl_addr_get_binary_addr (struct nl_addr *addr)
 Get binary address of abstract address object.
unsigned int nl_addr_get_len (struct nl_addr *addr)
 Get length of binary address of abstract address object.
void nl_addr_set_prefixlen (struct nl_addr *addr, int prefixlen)
unsigned int nl_addr_get_prefixlen (struct nl_addr *addr)
 Get prefix length of abstract address object.

Translations to Strings

char * nl_addr2str (struct nl_addr *addr, char *buf, size_t size)
 Convert abstract address object to character string.

Address Family Transformations

char * nl_af2str (int family, char *buf, size_t size)
int nl_str2af (const char *name)

Detailed Description

1) Transform character string to abstract address
 struct nl_addr *a = nl_addr_parse("::1", AF_UNSPEC);
 printf("Address family: %s\n", nl_af2str(nl_addr_get_family(a)));
 nl_addr_put(a);
 a = nl_addr_parse("11:22:33:44:55:66", AF_UNSPEC);
 printf("Address family: %s\n", nl_af2str(nl_addr_get_family(a)));
 nl_addr_put(a);

Function Documentation

struct nl_addr* nl_addr_alloc ( size_t  maxsize) [read]
Parameters:
maxsizeMaximum size of the binary address.
Returns:
Newly allocated address object or NULL

Definition at line 164 of file addr.c.

Referenced by nl_addr_build(), and nl_addr_parse().

{
        struct nl_addr *addr;
        
        addr = calloc(1, sizeof(*addr) + maxsize);
        if (!addr) {
                nl_errno(ENOMEM);
                return NULL;
        }

        addr->a_refcnt = 1;
        addr->a_maxsize = maxsize;

        return addr;
}
struct nl_addr* nl_addr_build ( int  family,
void *  buf,
size_t  size 
) [read]
Parameters:
familyAddress family.
bufBuffer containing the binary address.
sizeLength of binary address buffer.
Returns:
Newly allocated address handle or NULL

Definition at line 187 of file addr.c.

References nl_addr_alloc().

Referenced by nl_addr_clone(), and nla_get_addr().

{
        struct nl_addr *addr;

        addr = nl_addr_alloc(size);
        if (!addr)
                return NULL;

        addr->a_family = family;
        addr->a_len = size;
        addr->a_prefixlen = size*8;

        if (size)
                memcpy(addr->a_addr, buf, size);

        return addr;
}
struct nl_addr* nl_addr_parse ( const char *  addrstr,
int  hint 
) [read]
Parameters:
addrstrAddress represented as character string.
hintAddress family hint or AF_UNSPEC.

Regognizes the following address formats:

  Format                      Len                Family
  ----------------------------------------------------------------
  IPv6 address format         16                 AF_INET6
  ddd.ddd.ddd.ddd             4                  AF_INET
  HH:HH:HH:HH:HH:HH           6                  AF_LLC
  AA{.|,}NNNN                 2                  AF_DECnet
  HH:HH:HH:...                variable           AF_UNSPEC

Special values:

  • none: All bits and length set to 0.
  • {default|all|any}: All bits set to 0, length based on hint or AF_INET if no hint is given.

The prefix length may be appened at the end prefixed with a slash, e.g. 10.0.0.0/8.

Returns:
Newly allocated abstract address object or NULL.

Definition at line 231 of file addr.c.

References nl_addr_alloc(), nl_addr_destroy(), and nl_addr_set_binary_addr().

{
        int err, copy = 0, len = 0, family = AF_UNSPEC;
        char *str, *prefix, buf[32];
        struct nl_addr *addr = NULL; /* gcc ain't that smart */

        str = strdup(addrstr);
        if (!str) {
                err = nl_errno(ENOMEM);
                goto errout;
        }

        prefix = strchr(str, '/');
        if (prefix)
                *prefix = '\0';

        if (!strcasecmp(str, "none")) {
                family = hint;
                goto prefix;
        }

        if (!strcasecmp(str, "default") ||
            !strcasecmp(str, "all") ||
            !strcasecmp(str, "any")) {
                        
                switch (hint) {
                        case AF_INET:
                        case AF_UNSPEC:
                                /* Kind of a hack, we assume that if there is
                                 * no hint given the user wants to have a IPv4
                                 * address given back. */
                                family = AF_INET;
                                len = 4;
                                goto prefix;

                        case AF_INET6:
                                family = AF_INET6;
                                len = 16;
                                goto prefix;

                        case AF_LLC:
                                family = AF_LLC;
                                len = 6;
                                goto prefix;

                        default:
                                err = nl_error(EINVAL, "Unsuported address" \
                                    "family for default address");
                                goto errout;
                }
        }

        copy = 1;

        if (hint == AF_INET || hint == AF_UNSPEC) {
                if (inet_pton(AF_INET, str, buf) > 0) {
                        family = AF_INET;
                        len = 4;
                        goto prefix;
                }
                if (hint == AF_INET) {
                        err = nl_error(EINVAL, "Invalid IPv4 address");
                        goto errout;
                }
        }

        if (hint == AF_INET6 || hint == AF_UNSPEC) {
                if (inet_pton(AF_INET6, str, buf) > 0) {
                        family = AF_INET6;
                        len = 16;
                        goto prefix;
                }
                if (hint == AF_INET6) {
                        err = nl_error(EINVAL, "Invalid IPv6 address");
                        goto errout;
                }
        }

        if ((hint == AF_LLC || hint == AF_UNSPEC) && strchr(str, ':')) {
                unsigned int a, b, c, d, e, f;

                if (sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
                    &a, &b, &c, &d, &e, &f) == 6) {
                        family = AF_LLC;
                        len = 6;
                        buf[0] = (unsigned char) a;
                        buf[1] = (unsigned char) b;
                        buf[2] = (unsigned char) c;
                        buf[3] = (unsigned char) d;
                        buf[4] = (unsigned char) e;
                        buf[5] = (unsigned char) f;
                        goto prefix;
                }

                if (hint == AF_LLC) {
                        err = nl_error(EINVAL, "Invalid link layer address");
                        goto errout;
                }
        }

        if ((hint == AF_DECnet || hint == AF_UNSPEC) &&
            (strchr(str, '.') || strchr(str, ','))) {
                if (dnet_pton(str, buf) > 0) {
                        family = AF_DECnet;
                        len = 2;
                        goto prefix;
                }
                if (hint == AF_DECnet) {
                        err = nl_error(EINVAL, "Invalid DECnet address");
                        goto errout;
                }
        }

        if (hint == AF_UNSPEC && strchr(str, ':')) {
                int i = 0;
                char *s = str, *p;
                for (;;) {
                        long l = strtol(s, &p, 16);

                        if (s == p || l > 0xff || i >= sizeof(buf)) {
                                err = -EINVAL;
                                goto errout;
                        }

                        buf[i++] = (unsigned char) l;
                        if (*p == '\0')
                                break;
                        s = ++p;
                }

                len = i;
                family = AF_UNSPEC;
                goto prefix;
        }

        err = nl_error(EINVAL, "Invalid address");
        goto errout;

prefix:
        addr = nl_addr_alloc(len);
        if (!addr) {
                err = nl_errno(ENOMEM);
                goto errout;
        }

        nl_addr_set_family(addr, family);

        if (copy)
                nl_addr_set_binary_addr(addr, buf, len);

        if (prefix) {
                char *p;
                long pl = strtol(++prefix, &p, 0);
                if (p == prefix) {
                        nl_addr_destroy(addr);
                        err = -EINVAL;
                        goto errout;
                }
                nl_addr_set_prefixlen(addr, pl);
        } else
                nl_addr_set_prefixlen(addr, len * 8);

        err = 0;
errout:
        free(str);

        return err ? NULL : addr;
}
struct nl_addr* nl_addr_clone ( struct nl_addr *  addr) [read]
Parameters:
addrAbstract address object.
Returns:
Newly allocated abstract address object being a duplicate of the specified address object or NULL if a failure occured.

Definition at line 406 of file addr.c.

References nl_addr_build().

{
        struct nl_addr *new;

        new = nl_addr_build(addr->a_family, addr->a_addr, addr->a_len);
        if (new)
                new->a_prefixlen = addr->a_prefixlen;

        return new;
}
void nl_addr_destroy ( struct nl_addr *  addr)
Parameters:
addrAbstract address object.

Definition at line 428 of file addr.c.

Referenced by nl_addr_parse().

{
        if (!addr)
                return;

        if (addr->a_refcnt != 1)
                BUG();

        free(addr);
}
int nl_addr_shared ( struct nl_addr *  addr)
Parameters:
addrAbstract address object.
Returns:
Non-zero if the abstract address object is shared, otherwise 0.

Definition at line 469 of file addr.c.

{
        return addr->a_refcnt > 1;
}
int nl_addr_cmp ( struct nl_addr *  a,
struct nl_addr *  b 
)
Parameters:
aA abstract address object.
bAnother abstract address object.
Returns:
Integer less than, equal to or greather than zero if is found, respectively to be less than, to, or be greater than b.

Definition at line 489 of file addr.c.

Referenced by rtnl_neigh_get().

{
        int d = a->a_family - b->a_family;

        if (d == 0) {
                d = a->a_len - b->a_len;

                if (a->a_len && d == 0)
                        return memcmp(a->a_addr, b->a_addr, a->a_len);
        }

        return d;
}
int nl_addr_cmp_prefix ( struct nl_addr *  a,
struct nl_addr *  b 
)
Parameters:
aA abstract address object.
bAnother abstract address object.
Returns:
Integer less than, equal to or greather than zero if is found, respectively to be less than, to, or be greater than b.

Definition at line 511 of file addr.c.

{
        int d = a->a_family - b->a_family;

        if (d == 0) {
                int len = min(a->a_prefixlen, b->a_prefixlen);
                int bytes = len / 8;

                d = memcmp(a->a_addr, b->a_addr, bytes);
                if (d == 0) {
                        int mask = (1UL << (len % 8)) - 1UL;

                        d = (a->a_addr[bytes] & mask) -
                            (b->a_addr[bytes] & mask);
                }
        }

        return d;
}
int nl_addr_iszero ( struct nl_addr *  addr)
Parameters:
addrAddress to look at.

Definition at line 535 of file addr.c.

{
        int i;

        for (i = 0; i < addr->a_len; i++)
                if (addr->a_addr[i])
                        return 0;

        return 1;
}
int nl_addr_valid ( char *  addr,
int  family 
)
Parameters:
addrAddress represented as character string.
familyDesired address family.
Returns:
1 if the address is of the desired address family, otherwise 0 is returned.

Definition at line 554 of file addr.c.

{
        int ret;
        char buf[32];

        switch (family) {
        case AF_INET:
        case AF_INET6:
                ret = inet_pton(family, addr, buf);
                if (ret <= 0)
                        return 0;
                break;

        case AF_DECnet:
                ret = dnet_pton(addr, buf);
                if (ret <= 0)
                        return 0;
                break;

        case AF_LLC:
                if (sscanf(addr, "%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
                        return 0;
                break;
        }

        return 1;
}
int nl_addr_guess_family ( struct nl_addr *  addr)
Parameters:
addrAbstract address object.
Returns:
Address family or AF_UNSPEC if guessing wasn't successful.

Definition at line 587 of file addr.c.

{
        switch (addr->a_len) {
                case 4:
                        return AF_INET;
                case 6:
                        return AF_LLC;
                case 16:
                        return AF_INET6;
                default:
                        return AF_UNSPEC;
        }
}
int nl_addr_fill_sockaddr ( struct nl_addr *  addr,
struct sockaddr *  sa,
socklen_t *  salen 
)
Parameters:
addrAbstract address object.
saDestination sockaddr structure buffer.
salenLength of sockaddr structure buffer.

Fills out the specified sockaddr structure with the data found in the specified abstract address. The salen argument needs to be set to the size of sa but will be modified to the actual size used during before the function exits.

Returns:
0 on success or a negative error code

Definition at line 614 of file addr.c.

Referenced by nl_addr_resolve().

{
        switch (addr->a_family) {
        case AF_INET: {
                struct sockaddr_in *sai = (struct sockaddr_in *) sa;

                if (*salen < sizeof(*sai))
                        return -EINVAL;

                sai->sin_family = addr->a_family;
                memcpy(&sai->sin_addr, addr->a_addr, 4);
                *salen = sizeof(*sai);
        }
                break;

        case AF_INET6: {
                struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;

                if (*salen < sizeof(*sa6))
                        return -EINVAL;

                sa6->sin6_family = addr->a_family;
                memcpy(&sa6->sin6_addr, addr->a_addr, 16);
                *salen = sizeof(*sa6);
        }
                break;

        default:
                return -EINVAL;
        }

        return 0;
}
struct addrinfo* nl_addr_info ( struct nl_addr *  addr) [read]
Parameters:
addrAbstract address object.

Calls getaddrinfo() for the specified abstract address in AI_NUMERICHOST mode.

Note:
The caller is responsible for freeing the linked list using the interface provided by getaddrinfo(3).
Returns:
A linked list of addrinfo handles or NULL with an error message associated.

Definition at line 670 of file addr.c.

References nl_addr2str().

{
        int err;
        struct addrinfo *res;
        char buf[INET6_ADDRSTRLEN+5];
        struct addrinfo hint = {
                .ai_flags = AI_NUMERICHOST,
                .ai_family = addr->a_family,
        };

        nl_addr2str(addr, buf, sizeof(buf));

        err = getaddrinfo(buf, NULL, &hint, &res);
        if (err != 0) {
                nl_error(err, gai_strerror(err));
                return NULL;
        }

        return res;
}
int nl_addr_resolve ( struct nl_addr *  addr,
char *  host,
size_t  hostlen 
)
Parameters:
addrAbstract address object.
hostDestination buffer for host name.
hostlenLength of destination buffer.

Resolves the abstract address to a name and writes the looked up result into the host buffer. getnameinfo() is used to perform the lookup and is put into NI_NAMEREQD mode so the function will fail if the lookup couldn't be performed.

Returns:
0 on success or a negative error code.

Definition at line 704 of file addr.c.

References nl_addr_fill_sockaddr().

{
        int err;
        struct sockaddr_in6 buf;
        socklen_t salen = sizeof(buf);

        err = nl_addr_fill_sockaddr(addr, (struct sockaddr *) &buf, &salen);
        if (err < 0)
                return err;

        return getnameinfo((struct sockaddr *) &buf, salen,
                           host, hostlen, NULL, 0, NI_NAMEREQD);
}
int nl_addr_set_binary_addr ( struct nl_addr *  addr,
void *  buf,
size_t  len 
)
Parameters:
addrAbstract address object.
bufBuffer containing binary address.
lenLength of buffer containing binary address.

Definition at line 741 of file addr.c.

Referenced by nl_addr_parse().

{
        if (len > addr->a_maxsize)
                return -ERANGE;

        addr->a_len = len;
        memcpy(addr->a_addr, buf, len);

        return 0;
}
void* nl_addr_get_binary_addr ( struct nl_addr *  addr)
Parameters:
addrAbstract address object.

Definition at line 756 of file addr.c.

Referenced by flnl_lookup_build_request(), and nla_put_addr().

{
        return addr->a_addr;
}
unsigned int nl_addr_get_len ( struct nl_addr *  addr)
Parameters:
addrAbstract address object.

Definition at line 765 of file addr.c.

Referenced by nla_put_addr().

{
        return addr->a_len;
}
unsigned int nl_addr_get_prefixlen ( struct nl_addr *  addr)
Parameters:
addrAbstract address object.

Definition at line 779 of file addr.c.

{
        return addr->a_prefixlen;
}
char* nl_addr2str ( struct nl_addr *  addr,
char *  buf,
size_t  size 
)
Parameters:
addrAbstract address object.
bufDestination buffer.
sizeSize of destination buffer.

Converts an abstract address to a character string and stores the result in the specified destination buffer.

Returns:
Address represented in ASCII stored in destination buffer.

Definition at line 802 of file addr.c.

Referenced by nl_addr_info().

{
        int i;
        char tmp[16];

        if (!addr->a_len) {
                snprintf(buf, size, "none");
                goto prefix;
        }

        switch (addr->a_family) {
                case AF_INET:
                        inet_ntop(AF_INET, addr->a_addr, buf, size);
                        break;

                case AF_INET6:
                        inet_ntop(AF_INET6, addr->a_addr, buf, size);
                        break;

                case AF_DECnet:
                        dnet_ntop(addr->a_addr, addr->a_len, buf, size);
                        break;

                case AF_LLC:
                default:
                        snprintf(buf, size, "%02x",
                                 (unsigned char) addr->a_addr[0]);
                        for (i = 1; i < addr->a_len; i++) {
                                snprintf(tmp, sizeof(tmp), ":%02x",
                                         (unsigned char) addr->a_addr[i]);
                                strncat(buf, tmp, size - strlen(buf) - 1);
                        }
                        break;
        }

prefix:
        if (addr->a_prefixlen != (8 * addr->a_len)) {
                snprintf(tmp, sizeof(tmp), "/%u", addr->a_prefixlen);
                strncat(buf, tmp, size - strlen(buf) - 1);
        }

        return buf;
}