2 * lib/addr.c Abstract Address
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup addr Abstract Address
16 * @par 1) Transform character string to abstract address
18 * struct nl_addr *a = nl_addr_parse("::1", AF_UNSPEC);
19 * printf("Address family: %s\n", nl_af2str(nl_addr_get_family(a)));
21 * a = nl_addr_parse("11:22:33:44:55:66", AF_UNSPEC);
22 * printf("Address family: %s\n", nl_af2str(nl_addr_get_family(a)));
28 #include <netlink-local.h>
29 #include <netlink/netlink.h>
30 #include <netlink/utils.h>
31 #include <netlink/addr.h>
32 #include <linux/socket.h>
34 /* All this DECnet stuff is stolen from iproute2, thanks to whoever wrote
35 * this, probably Alexey. */
36 static inline uint16_t dn_ntohs(uint16_t addr)
45 return ((uint16_t) u.byte[0]) | (((uint16_t) u.byte[1]) << 8);
48 static inline int do_digit(char *str, uint16_t *addr, uint16_t scale,
49 size_t *pos, size_t len, int *started)
51 uint16_t tmp = *addr / scale;
56 if (((tmp) > 0) || *started || (scale == 1)) {
60 *addr -= (tmp * scale);
66 static const char *dnet_ntop(char *addrbuf, size_t addrlen, char *str,
69 uint16_t addr = dn_ntohs(*(uint16_t *)addrbuf);
70 uint16_t area = addr >> 10;
82 if (do_digit(str + pos, &area, 10, &pos, len, &started))
85 if (do_digit(str + pos, &area, 1, &pos, len, &started))
95 if (do_digit(str + pos, &addr, 1000, &pos, len, &started))
98 if (do_digit(str + pos, &addr, 100, &pos, len, &started))
101 if (do_digit(str + pos, &addr, 10, &pos, len, &started))
104 if (do_digit(str + pos, &addr, 1, &pos, len, &started))
115 static int dnet_num(const char *src, uint16_t * dst)
121 while ((tmp = *src++) != 0) {
123 if ((tmp < 0) || (tmp > 9))
134 static inline int dnet_pton(const char *src, char *addrbuf)
140 pos = dnet_num(src, &area);
141 if ((pos == 0) || (area > 63) ||
142 ((*(src + pos) != '.') && (*(src + pos) != ',')))
145 pos = dnet_num(src + pos + 1, &node);
146 if ((pos == 0) || (node > 1023))
149 *(uint16_t *)addrbuf = dn_ntohs((area << 10) | node);
155 * @name Creating Abstract Addresses
160 * Allocate new abstract address object.
161 * @arg maxsize Maximum size of the binary address.
162 * @return Newly allocated address object or NULL
164 struct nl_addr *nl_addr_alloc(size_t maxsize)
166 struct nl_addr *addr;
168 addr = calloc(1, sizeof(*addr) + maxsize);
173 addr->a_maxsize = maxsize;
179 * Allocate new abstract address object based on a binary address.
180 * @arg family Address family.
181 * @arg buf Buffer containing the binary address.
182 * @arg size Length of binary address buffer.
183 * @return Newly allocated address handle or NULL
185 struct nl_addr *nl_addr_build(int family, void *buf, size_t size)
187 struct nl_addr *addr;
189 addr = nl_addr_alloc(size);
193 addr->a_family = family;
195 addr->a_prefixlen = size*8;
198 memcpy(addr->a_addr, buf, size);
204 * Allocate abstract address based on netlink attribute.
205 * @arg nla Netlink attribute of unspecific type.
206 * @arg family Address family.
208 * Considers the netlink attribute payload a address of the specified
209 * family and allocates a new abstract address based on it.
211 * @return Newly allocated address handle or NULL.
213 struct nl_addr *nl_addr_alloc_attr(struct nlattr *nla, int family)
215 return nl_addr_build(family, nla_data(nla), nla_len(nla));
219 * Allocate abstract address object based on a character string
220 * @arg addrstr Address represented as character string.
221 * @arg hint Address family hint or AF_UNSPEC.
222 * @arg result Pointer to store resulting address.
224 * Regognizes the following address formats:
227 * ----------------------------------------------------------------
228 * IPv6 address format 16 AF_INET6
229 * ddd.ddd.ddd.ddd 4 AF_INET
230 * HH:HH:HH:HH:HH:HH 6 AF_LLC
231 * AA{.|,}NNNN 2 AF_DECnet
232 * HH:HH:HH:... variable AF_UNSPEC
236 * - none: All bits and length set to 0.
237 * - {default|all|any}: All bits set to 0, length based on hint or
238 * AF_INET if no hint is given.
240 * The prefix length may be appened at the end prefixed with a
241 * slash, e.g. 10.0.0.0/8.
243 * @return 0 on success or a negative error code.
245 int nl_addr_parse(const char *addrstr, int hint, struct nl_addr **result)
247 int err, copy = 0, len = 0, family = AF_UNSPEC;
248 char *str, *prefix, buf[32];
249 struct nl_addr *addr = NULL; /* gcc ain't that smart */
251 str = strdup(addrstr);
257 prefix = strchr(str, '/');
261 if (!strcasecmp(str, "none")) {
266 if (!strcasecmp(str, "default") ||
267 !strcasecmp(str, "all") ||
268 !strcasecmp(str, "any")) {
273 /* Kind of a hack, we assume that if there is
274 * no hint given the user wants to have a IPv4
275 * address given back. */
291 err = -NLE_AF_NOSUPPORT;
298 if (hint == AF_INET || hint == AF_UNSPEC) {
299 if (inet_pton(AF_INET, str, buf) > 0) {
304 if (hint == AF_INET) {
310 if (hint == AF_INET6 || hint == AF_UNSPEC) {
311 if (inet_pton(AF_INET6, str, buf) > 0) {
316 if (hint == AF_INET6) {
322 if ((hint == AF_LLC || hint == AF_UNSPEC) && strchr(str, ':')) {
323 unsigned int a, b, c, d, e, f;
325 if (sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
326 &a, &b, &c, &d, &e, &f) == 6) {
329 buf[0] = (unsigned char) a;
330 buf[1] = (unsigned char) b;
331 buf[2] = (unsigned char) c;
332 buf[3] = (unsigned char) d;
333 buf[4] = (unsigned char) e;
334 buf[5] = (unsigned char) f;
338 if (hint == AF_LLC) {
344 if ((hint == AF_DECnet || hint == AF_UNSPEC) &&
345 (strchr(str, '.') || strchr(str, ','))) {
346 if (dnet_pton(str, buf) > 0) {
351 if (hint == AF_DECnet) {
357 if (hint == AF_UNSPEC && strchr(str, ':')) {
361 long l = strtol(s, &p, 16);
363 if (s == p || l > 0xff || i >= sizeof(buf)) {
368 buf[i++] = (unsigned char) l;
383 addr = nl_addr_alloc(len);
389 nl_addr_set_family(addr, family);
392 nl_addr_set_binary_addr(addr, buf, len);
396 long pl = strtol(++prefix, &p, 0);
398 nl_addr_destroy(addr);
402 nl_addr_set_prefixlen(addr, pl);
404 nl_addr_set_prefixlen(addr, len * 8);
415 * Clone existing abstract address object.
416 * @arg addr Abstract address object.
417 * @return Newly allocated abstract address object being a duplicate of the
418 * specified address object or NULL if a failure occured.
420 struct nl_addr *nl_addr_clone(struct nl_addr *addr)
424 new = nl_addr_build(addr->a_family, addr->a_addr, addr->a_len);
426 new->a_prefixlen = addr->a_prefixlen;
434 * @name Destroying Abstract Addresses
439 * Destroy abstract address object.
440 * @arg addr Abstract address object.
442 void nl_addr_destroy(struct nl_addr *addr)
447 if (addr->a_refcnt != 1)
456 * @name Managing Usage References
460 struct nl_addr *nl_addr_get(struct nl_addr *addr)
467 void nl_addr_put(struct nl_addr *addr)
472 if (addr->a_refcnt == 1)
473 nl_addr_destroy(addr);
479 * Check whether an abstract address object is shared.
480 * @arg addr Abstract address object.
481 * @return Non-zero if the abstract address object is shared, otherwise 0.
483 int nl_addr_shared(struct nl_addr *addr)
485 return addr->a_refcnt > 1;
491 * @name Miscellaneous
496 * Compares two abstract address objects.
497 * @arg a A abstract address object.
498 * @arg b Another abstract address object.
500 * @return Integer less than, equal to or greather than zero if \c is found,
501 * respectively to be less than, to, or be greater than \c b.
503 int nl_addr_cmp(struct nl_addr *a, struct nl_addr *b)
505 int d = a->a_family - b->a_family;
508 d = a->a_len - b->a_len;
510 if (a->a_len && d == 0)
511 return memcmp(a->a_addr, b->a_addr, a->a_len);
518 * Compares the prefix of two abstract address objects.
519 * @arg a A abstract address object.
520 * @arg b Another abstract address object.
522 * @return Integer less than, equal to or greather than zero if \c is found,
523 * respectively to be less than, to, or be greater than \c b.
525 int nl_addr_cmp_prefix(struct nl_addr *a, struct nl_addr *b)
527 int d = a->a_family - b->a_family;
530 int len = min(a->a_prefixlen, b->a_prefixlen);
533 d = memcmp(a->a_addr, b->a_addr, bytes);
535 int mask = (1UL << (len % 8)) - 1UL;
537 d = (a->a_addr[bytes] & mask) -
538 (b->a_addr[bytes] & mask);
546 * Returns true if the address consists of all zeros
547 * @arg addr Address to look at.
549 int nl_addr_iszero(struct nl_addr *addr)
553 for (i = 0; i < addr->a_len; i++)
561 * Check if an address matches a certain family.
562 * @arg addr Address represented as character string.
563 * @arg family Desired address family.
565 * @return 1 if the address is of the desired address family,
566 * otherwise 0 is returned.
568 int nl_addr_valid(char *addr, int family)
576 ret = inet_pton(family, addr, buf);
582 ret = dnet_pton(addr, buf);
588 if (sscanf(addr, "%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
597 * Guess address family of an abstract address object based on address size.
598 * @arg addr Abstract address object.
599 * @return Address family or AF_UNSPEC if guessing wasn't successful.
601 int nl_addr_guess_family(struct nl_addr *addr)
603 switch (addr->a_len) {
616 * Fill out sockaddr structure with values from abstract address object.
617 * @arg addr Abstract address object.
618 * @arg sa Destination sockaddr structure buffer.
619 * @arg salen Length of sockaddr structure buffer.
621 * Fills out the specified sockaddr structure with the data found in the
622 * specified abstract address. The salen argument needs to be set to the
623 * size of sa but will be modified to the actual size used during before
624 * the function exits.
626 * @return 0 on success or a negative error code
628 int nl_addr_fill_sockaddr(struct nl_addr *addr, struct sockaddr *sa,
631 switch (addr->a_family) {
633 struct sockaddr_in *sai = (struct sockaddr_in *) sa;
635 if (*salen < sizeof(*sai))
638 sai->sin_family = addr->a_family;
639 memcpy(&sai->sin_addr, addr->a_addr, 4);
640 *salen = sizeof(*sai);
645 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
647 if (*salen < sizeof(*sa6))
650 sa6->sin6_family = addr->a_family;
651 memcpy(&sa6->sin6_addr, addr->a_addr, 16);
652 *salen = sizeof(*sa6);
667 * @name Getting Information About Addresses
672 * Call getaddrinfo() for an abstract address object.
673 * @arg addr Abstract address object.
674 * @arg result Pointer to store resulting address list.
676 * Calls getaddrinfo() for the specified abstract address in AI_NUMERICHOST
679 * @note The caller is responsible for freeing the linked list using the
680 * interface provided by getaddrinfo(3).
682 * @return 0 on success or a negative error code.
684 int nl_addr_info(struct nl_addr *addr, struct addrinfo **result)
687 char buf[INET6_ADDRSTRLEN+5];
688 struct addrinfo hint = {
689 .ai_flags = AI_NUMERICHOST,
690 .ai_family = addr->a_family,
693 nl_addr2str(addr, buf, sizeof(buf));
695 err = getaddrinfo(buf, NULL, &hint, result);
698 case EAI_ADDRFAMILY: return -NLE_AF_NOSUPPORT;
699 case EAI_AGAIN: return -NLE_AGAIN;
700 case EAI_BADFLAGS: return -NLE_INVAL;
701 case EAI_FAIL: return -NLE_NOADDR;
702 case EAI_FAMILY: return -NLE_AF_NOSUPPORT;
703 case EAI_MEMORY: return -NLE_NOMEM;
704 case EAI_NODATA: return -NLE_NOADDR;
705 case EAI_NONAME: return -NLE_OBJ_NOTFOUND;
706 case EAI_SERVICE: return -NLE_OPNOTSUPP;
707 case EAI_SOCKTYPE: return -NLE_BAD_SOCK;
708 default: return -NLE_FAILURE;
716 * Resolve abstract address object to a name using getnameinfo().
717 * @arg addr Abstract address object.
718 * @arg host Destination buffer for host name.
719 * @arg hostlen Length of destination buffer.
721 * Resolves the abstract address to a name and writes the looked up result
722 * into the host buffer. getnameinfo() is used to perform the lookup and
723 * is put into NI_NAMEREQD mode so the function will fail if the lookup
724 * couldn't be performed.
726 * @return 0 on success or a negative error code.
728 int nl_addr_resolve(struct nl_addr *addr, char *host, size_t hostlen)
731 struct sockaddr_in6 buf;
732 socklen_t salen = sizeof(buf);
734 err = nl_addr_fill_sockaddr(addr, (struct sockaddr *) &buf, &salen);
738 err = getnameinfo((struct sockaddr *) &buf, salen, host, hostlen,
739 NULL, 0, NI_NAMEREQD);
741 return nl_syserr2nlerr(err);
753 void nl_addr_set_family(struct nl_addr *addr, int family)
755 addr->a_family = family;
758 int nl_addr_get_family(struct nl_addr *addr)
760 return addr->a_family;
764 * Set binary address of abstract address object.
765 * @arg addr Abstract address object.
766 * @arg buf Buffer containing binary address.
767 * @arg len Length of buffer containing binary address.
769 int nl_addr_set_binary_addr(struct nl_addr *addr, void *buf, size_t len)
771 if (len > addr->a_maxsize)
775 memcpy(addr->a_addr, buf, len);
781 * Get binary address of abstract address object.
782 * @arg addr Abstract address object.
784 void *nl_addr_get_binary_addr(struct nl_addr *addr)
790 * Get length of binary address of abstract address object.
791 * @arg addr Abstract address object.
793 unsigned int nl_addr_get_len(struct nl_addr *addr)
798 void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
800 addr->a_prefixlen = prefixlen;
804 * Get prefix length of abstract address object.
805 * @arg addr Abstract address object.
807 unsigned int nl_addr_get_prefixlen(struct nl_addr *addr)
809 return addr->a_prefixlen;
815 * @name Translations to Strings
820 * Convert abstract address object to character string.
821 * @arg addr Abstract address object.
822 * @arg buf Destination buffer.
823 * @arg size Size of destination buffer.
825 * Converts an abstract address to a character string and stores
826 * the result in the specified destination buffer.
828 * @return Address represented in ASCII stored in destination buffer.
830 char *nl_addr2str(struct nl_addr *addr, char *buf, size_t size)
835 if (!addr || !addr->a_len) {
836 snprintf(buf, size, "none");
843 switch (addr->a_family) {
845 inet_ntop(AF_INET, addr->a_addr, buf, size);
849 inet_ntop(AF_INET6, addr->a_addr, buf, size);
853 dnet_ntop(addr->a_addr, addr->a_len, buf, size);
858 snprintf(buf, size, "%02x",
859 (unsigned char) addr->a_addr[0]);
860 for (i = 1; i < addr->a_len; i++) {
861 snprintf(tmp, sizeof(tmp), ":%02x",
862 (unsigned char) addr->a_addr[i]);
863 strncat(buf, tmp, size - strlen(buf) - 1);
869 if (addr->a_prefixlen != (8 * addr->a_len)) {
870 snprintf(tmp, sizeof(tmp), "/%u", addr->a_prefixlen);
871 strncat(buf, tmp, size - strlen(buf) - 1);
880 * @name Address Family Transformations
884 static struct trans_tbl afs[] = {
885 __ADD(AF_UNSPEC,unspec)
887 __ADD(AF_LOCAL,local)
891 __ADD(AF_APPLETALK,appletalk)
892 __ADD(AF_NETROM,netrom)
893 __ADD(AF_BRIDGE,bridge)
894 __ADD(AF_ATMPVC,atmpvc)
896 __ADD(AF_INET6,inet6)
898 __ADD(AF_DECnet,decnet)
899 __ADD(AF_NETBEUI,netbeui)
900 __ADD(AF_SECURITY,security)
902 __ADD(AF_NETLINK,netlink)
903 __ADD(AF_ROUTE,route)
904 __ADD(AF_PACKET,packet)
906 __ADD(AF_ECONET,econet)
907 __ADD(AF_ATMSVC,atmsvc)
910 __ADD(AF_PPPOX,pppox)
911 __ADD(AF_WANPIPE,wanpipe)
913 __ADD(AF_BLUETOOTH,bluetooth)
916 char *nl_af2str(int family, char *buf, size_t size)
918 return __type2str(family, buf, size, afs, ARRAY_SIZE(afs));
921 int nl_str2af(const char *name)
923 int fam = __str2type(name, afs, ARRAY_SIZE(afs));
924 return fam >= 0 ? fam : AF_UNSPEC;