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-2006 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);
175 addr->a_maxsize = maxsize;
181 * Allocate new abstract address object based on a binary address.
182 * @arg family Address family.
183 * @arg buf Buffer containing the binary address.
184 * @arg size Length of binary address buffer.
185 * @return Newly allocated address handle or NULL
187 struct nl_addr *nl_addr_build(int family, void *buf, size_t size)
189 struct nl_addr *addr;
191 addr = nl_addr_alloc(size);
195 addr->a_family = family;
197 addr->a_prefixlen = size*8;
200 memcpy(addr->a_addr, buf, size);
206 * Allocate abstract address object based on a character string
207 * @arg addrstr Address represented as character string.
208 * @arg hint Address family hint or AF_UNSPEC.
210 * Regognizes the following address formats:
213 * ----------------------------------------------------------------
214 * IPv6 address format 16 AF_INET6
215 * ddd.ddd.ddd.ddd 4 AF_INET
216 * HH:HH:HH:HH:HH:HH 6 AF_LLC
217 * AA{.|,}NNNN 2 AF_DECnet
218 * HH:HH:HH:... variable AF_UNSPEC
222 * - none: All bits and length set to 0.
223 * - {default|all|any}: All bits set to 0, length based on hint or
224 * AF_INET if no hint is given.
226 * The prefix length may be appened at the end prefixed with a
227 * slash, e.g. 10.0.0.0/8.
229 * @return Newly allocated abstract address object or NULL.
231 struct nl_addr *nl_addr_parse(const char *addrstr, int hint)
233 int err, copy = 0, len = 0, family = AF_UNSPEC;
234 char *str, *prefix, buf[32];
235 struct nl_addr *addr = NULL; /* gcc ain't that smart */
237 str = strdup(addrstr);
239 err = nl_errno(ENOMEM);
243 prefix = strchr(str, '/');
247 if (!strcasecmp(str, "none")) {
252 if (!strcasecmp(str, "default") ||
253 !strcasecmp(str, "all") ||
254 !strcasecmp(str, "any")) {
259 /* Kind of a hack, we assume that if there is
260 * no hint given the user wants to have a IPv4
261 * address given back. */
277 err = nl_error(EINVAL, "Unsuported address" \
278 "family for default address");
285 if (hint == AF_INET || hint == AF_UNSPEC) {
286 if (inet_pton(AF_INET, str, buf) > 0) {
291 if (hint == AF_INET) {
292 err = nl_error(EINVAL, "Invalid IPv4 address");
297 if (hint == AF_INET6 || hint == AF_UNSPEC) {
298 if (inet_pton(AF_INET6, str, buf) > 0) {
303 if (hint == AF_INET6) {
304 err = nl_error(EINVAL, "Invalid IPv6 address");
309 if ((hint == AF_LLC || hint == AF_UNSPEC) && strchr(str, ':')) {
310 unsigned int a, b, c, d, e, f;
312 if (sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
313 &a, &b, &c, &d, &e, &f) == 6) {
316 buf[0] = (unsigned char) a;
317 buf[1] = (unsigned char) b;
318 buf[2] = (unsigned char) c;
319 buf[3] = (unsigned char) d;
320 buf[4] = (unsigned char) e;
321 buf[5] = (unsigned char) f;
325 if (hint == AF_LLC) {
326 err = nl_error(EINVAL, "Invalid link layer address");
331 if ((hint == AF_DECnet || hint == AF_UNSPEC) &&
332 (strchr(str, '.') || strchr(str, ','))) {
333 if (dnet_pton(str, buf) > 0) {
338 if (hint == AF_DECnet) {
339 err = nl_error(EINVAL, "Invalid DECnet address");
344 if (hint == AF_UNSPEC && strchr(str, ':')) {
348 long l = strtol(s, &p, 16);
350 if (s == p || l > 0xff || i >= sizeof(buf)) {
355 buf[i++] = (unsigned char) l;
366 err = nl_error(EINVAL, "Invalid address");
370 addr = nl_addr_alloc(len);
372 err = nl_errno(ENOMEM);
376 nl_addr_set_family(addr, family);
379 nl_addr_set_binary_addr(addr, buf, len);
383 long pl = strtol(++prefix, &p, 0);
385 nl_addr_destroy(addr);
389 nl_addr_set_prefixlen(addr, pl);
391 nl_addr_set_prefixlen(addr, len * 8);
397 return err ? NULL : addr;
401 * Clone existing abstract address object.
402 * @arg addr Abstract address object.
403 * @return Newly allocated abstract address object being a duplicate of the
404 * specified address object or NULL if a failure occured.
406 struct nl_addr *nl_addr_clone(struct nl_addr *addr)
410 new = nl_addr_build(addr->a_family, addr->a_addr, addr->a_len);
412 new->a_prefixlen = addr->a_prefixlen;
420 * @name Destroying Abstract Addresses
425 * Destroy abstract address object.
426 * @arg addr Abstract address object.
428 void nl_addr_destroy(struct nl_addr *addr)
433 if (addr->a_refcnt != 1)
442 * @name Managing Usage References
446 struct nl_addr *nl_addr_get(struct nl_addr *addr)
453 void nl_addr_put(struct nl_addr *addr)
458 if (addr->a_refcnt == 1)
459 nl_addr_destroy(addr);
465 * Check whether an abstract address object is shared.
466 * @arg addr Abstract address object.
467 * @return Non-zero if the abstract address object is shared, otherwise 0.
469 int nl_addr_shared(struct nl_addr *addr)
471 return addr->a_refcnt > 1;
477 * @name Miscellaneous
482 * Compares two abstract address objects.
483 * @arg a A abstract address object.
484 * @arg b Another abstract address object.
486 * @return Integer less than, equal to or greather than zero if \c is found,
487 * respectively to be less than, to, or be greater than \c b.
489 int nl_addr_cmp(struct nl_addr *a, struct nl_addr *b)
491 int d = a->a_family - b->a_family;
494 d = a->a_len - b->a_len;
496 if (a->a_len && d == 0)
497 return memcmp(a->a_addr, b->a_addr, a->a_len);
504 * Compares the prefix of two abstract address objects.
505 * @arg a A abstract address object.
506 * @arg b Another abstract address object.
508 * @return Integer less than, equal to or greather than zero if \c is found,
509 * respectively to be less than, to, or be greater than \c b.
511 int nl_addr_cmp_prefix(struct nl_addr *a, struct nl_addr *b)
513 int d = a->a_family - b->a_family;
516 int len = min(a->a_prefixlen, b->a_prefixlen);
519 d = memcmp(a->a_addr, b->a_addr, bytes);
521 int mask = (1UL << (len % 8)) - 1UL;
523 d = (a->a_addr[bytes] & mask) -
524 (b->a_addr[bytes] & mask);
532 * Returns true if the address consists of all zeros
533 * @arg addr Address to look at.
535 int nl_addr_iszero(struct nl_addr *addr)
539 for (i = 0; i < addr->a_len; i++)
547 * Check if an address matches a certain family.
548 * @arg addr Address represented as character string.
549 * @arg family Desired address family.
551 * @return 1 if the address is of the desired address family,
552 * otherwise 0 is returned.
554 int nl_addr_valid(char *addr, int family)
562 ret = inet_pton(family, addr, buf);
568 ret = dnet_pton(addr, buf);
574 if (sscanf(addr, "%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
583 * Guess address family of an abstract address object based on address size.
584 * @arg addr Abstract address object.
585 * @return Address family or AF_UNSPEC if guessing wasn't successful.
587 int nl_addr_guess_family(struct nl_addr *addr)
589 switch (addr->a_len) {
602 * Fill out sockaddr structure with values from abstract address object.
603 * @arg addr Abstract address object.
604 * @arg sa Destination sockaddr structure buffer.
605 * @arg salen Length of sockaddr structure buffer.
607 * Fills out the specified sockaddr structure with the data found in the
608 * specified abstract address. The salen argument needs to be set to the
609 * size of sa but will be modified to the actual size used during before
610 * the function exits.
612 * @return 0 on success or a negative error code
614 int nl_addr_fill_sockaddr(struct nl_addr *addr, struct sockaddr *sa,
617 switch (addr->a_family) {
619 struct sockaddr_in *sai = (struct sockaddr_in *) sa;
621 if (*salen < sizeof(*sai))
624 sai->sin_family = addr->a_family;
625 memcpy(&sai->sin_addr, addr->a_addr, 4);
626 *salen = sizeof(*sai);
631 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
633 if (*salen < sizeof(*sa6))
636 sa6->sin6_family = addr->a_family;
637 memcpy(&sa6->sin6_addr, addr->a_addr, 16);
638 *salen = sizeof(*sa6);
653 * @name Getting Information About Addresses
658 * Call getaddrinfo() for an abstract address object.
659 * @arg addr Abstract address object.
661 * Calls getaddrinfo() for the specified abstract address in AI_NUMERICHOST
664 * @note The caller is responsible for freeing the linked list using the
665 * interface provided by getaddrinfo(3).
667 * @return A linked list of addrinfo handles or NULL with an error message
670 struct addrinfo *nl_addr_info(struct nl_addr *addr)
673 struct addrinfo *res;
674 char buf[INET6_ADDRSTRLEN+5];
675 struct addrinfo hint = {
676 .ai_flags = AI_NUMERICHOST,
677 .ai_family = addr->a_family,
680 nl_addr2str(addr, buf, sizeof(buf));
682 err = getaddrinfo(buf, NULL, &hint, &res);
684 nl_error(err, gai_strerror(err));
692 * Resolve abstract address object to a name using getnameinfo().
693 * @arg addr Abstract address object.
694 * @arg host Destination buffer for host name.
695 * @arg hostlen Length of destination buffer.
697 * Resolves the abstract address to a name and writes the looked up result
698 * into the host buffer. getnameinfo() is used to perform the lookup and
699 * is put into NI_NAMEREQD mode so the function will fail if the lookup
700 * couldn't be performed.
702 * @return 0 on success or a negative error code.
704 int nl_addr_resolve(struct nl_addr *addr, char *host, size_t hostlen)
707 struct sockaddr_in6 buf;
708 socklen_t salen = sizeof(buf);
710 err = nl_addr_fill_sockaddr(addr, (struct sockaddr *) &buf, &salen);
714 return getnameinfo((struct sockaddr *) &buf, salen,
715 host, hostlen, NULL, 0, NI_NAMEREQD);
725 void nl_addr_set_family(struct nl_addr *addr, int family)
727 addr->a_family = family;
730 int nl_addr_get_family(struct nl_addr *addr)
732 return addr->a_family;
736 * Set binary address of abstract address object.
737 * @arg addr Abstract address object.
738 * @arg buf Buffer containing binary address.
739 * @arg len Length of buffer containing binary address.
741 int nl_addr_set_binary_addr(struct nl_addr *addr, void *buf, size_t len)
743 if (len > addr->a_maxsize)
747 memcpy(addr->a_addr, buf, len);
753 * Get binary address of abstract address object.
754 * @arg addr Abstract address object.
756 void *nl_addr_get_binary_addr(struct nl_addr *addr)
762 * Get length of binary address of abstract address object.
763 * @arg addr Abstract address object.
765 unsigned int nl_addr_get_len(struct nl_addr *addr)
770 void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
772 addr->a_prefixlen = prefixlen;
776 * Get prefix length of abstract address object.
777 * @arg addr Abstract address object.
779 unsigned int nl_addr_get_prefixlen(struct nl_addr *addr)
781 return addr->a_prefixlen;
787 * @name Translations to Strings
792 * Convert abstract address object to character string.
793 * @arg addr Abstract address object.
794 * @arg buf Destination buffer.
795 * @arg size Size of destination buffer.
797 * Converts an abstract address to a character string and stores
798 * the result in the specified destination buffer.
800 * @return Address represented in ASCII stored in destination buffer.
802 char *nl_addr2str(struct nl_addr *addr, char *buf, size_t size)
808 snprintf(buf, size, "none");
812 switch (addr->a_family) {
814 inet_ntop(AF_INET, addr->a_addr, buf, size);
818 inet_ntop(AF_INET6, addr->a_addr, buf, size);
822 dnet_ntop(addr->a_addr, addr->a_len, buf, size);
827 snprintf(buf, size, "%02x",
828 (unsigned char) addr->a_addr[0]);
829 for (i = 1; i < addr->a_len; i++) {
830 snprintf(tmp, sizeof(tmp), ":%02x",
831 (unsigned char) addr->a_addr[i]);
832 strncat(buf, tmp, size - strlen(buf) - 1);
838 if (addr->a_prefixlen != (8 * addr->a_len)) {
839 snprintf(tmp, sizeof(tmp), "/%u", addr->a_prefixlen);
840 strncat(buf, tmp, size - strlen(buf) - 1);
849 * @name Address Family Transformations
853 static struct trans_tbl afs[] = {
854 __ADD(AF_UNSPEC,unspec)
856 __ADD(AF_LOCAL,local)
860 __ADD(AF_APPLETALK,appletalk)
861 __ADD(AF_NETROM,netrom)
862 __ADD(AF_BRIDGE,bridge)
863 __ADD(AF_ATMPVC,atmpvc)
865 __ADD(AF_INET6,inet6)
867 __ADD(AF_DECnet,decnet)
868 __ADD(AF_NETBEUI,netbeui)
869 __ADD(AF_SECURITY,security)
871 __ADD(AF_NETLINK,netlink)
872 __ADD(AF_ROUTE,route)
873 __ADD(AF_PACKET,packet)
875 __ADD(AF_ECONET,econet)
876 __ADD(AF_ATMSVC,atmsvc)
879 __ADD(AF_PPPOX,pppox)
880 __ADD(AF_WANPIPE,wanpipe)
882 __ADD(AF_BLUETOOTH,bluetooth)
885 char *nl_af2str(int family, char *buf, size_t size)
887 return __type2str(family, buf, size, afs, ARRAY_SIZE(afs));
890 int nl_str2af(const char *name)
892 int fam = __str2type(name, afs, ARRAY_SIZE(afs));
893 return fam >= 0 ? fam : AF_UNSPEC;