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
447 * Request undestroyable reference of abstract address object.
448 * @arg addr Abstract address object.
449 * @return Abstract address object of which the reference was given.
451 struct nl_addr *nl_addr_get(struct nl_addr *addr)
459 * Give back reference of abstract address object.
460 * @arg addr Abstract address object.
462 * Decrements the reference counter and destroys the object if the
463 * last reference was given back.
465 void nl_addr_put(struct nl_addr *addr)
470 if (addr->a_refcnt == 1)
471 nl_addr_destroy(addr);
477 * Check whether an abstract address object is shared.
478 * @arg addr Abstract address object.
479 * @return Non-zero if the abstract address object is shared, otherwise 0.
481 int nl_addr_shared(struct nl_addr *addr)
483 return addr->a_refcnt > 1;
489 * @name Miscellaneous
494 * Compares two abstract address objects.
495 * @arg a A abstract address object.
496 * @arg b Another abstract address object.
498 * @return Integer less than, equal to or greather than zero if \c is found,
499 * respectively to be less than, to, or be greater than \c b.
501 int nl_addr_cmp(struct nl_addr *a, struct nl_addr *b)
503 int d = a->a_family - b->a_family;
506 d = a->a_len - b->a_len;
508 if (a->a_len && d == 0)
509 return memcmp(a->a_addr, b->a_addr, a->a_len);
516 * Check if an address matches a certain family.
517 * @arg addr Address represented as character string.
518 * @arg family Desired address family.
520 * @return 1 if the address is of the desired address family,
521 * otherwise 0 is returned.
523 int nl_addr_valid(char *addr, int family)
531 ret = inet_pton(family, addr, buf);
537 ret = dnet_pton(addr, buf);
543 if (sscanf(addr, "%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
552 * Guess address family of an abstract address object based on address size.
553 * @arg addr Abstract address object.
554 * @return Address family or AF_UNSPEC if guessing wasn't successful.
556 int nl_addr_guess_family(struct nl_addr *addr)
558 switch (addr->a_len) {
571 * Fill out sockaddr structure with values from abstract address object.
572 * @arg addr Abstract address object.
573 * @arg sa Destination sockaddr structure buffer.
574 * @arg salen Length of sockaddr structure buffer.
576 * Fills out the specified sockaddr structure with the data found in the
577 * specified abstract address. The salen argument needs to be set to the
578 * size of sa but will be modified to the actual size used during before
579 * the function exits.
581 * @return 0 on success or a negative error code
583 int nl_addr_fill_sockaddr(struct nl_addr *addr, struct sockaddr *sa,
586 switch (addr->a_family) {
588 struct sockaddr_in *sai = (struct sockaddr_in *) sa;
590 if (*salen < sizeof(*sai))
593 sai->sin_family = addr->a_family;
594 memcpy(&sai->sin_addr, addr->a_addr, 4);
595 *salen = sizeof(*sai);
600 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
602 if (*salen < sizeof(*sa6))
605 sa6->sin6_family = addr->a_family;
606 memcpy(&sa6->sin6_addr, addr->a_addr, 16);
607 *salen = sizeof(*sa6);
622 * @name Getting Information About Addresses
627 * Call getaddrinfo() for an abstract address object.
628 * @arg addr Abstract address object.
630 * Calls getaddrinfo() for the specified abstract address in AI_NUMERICHOST
633 * @note The caller is responsible for freeing the linked list using the
634 * interface provided by getaddrinfo(3).
636 * @return A linked list of addrinfo handles or NULL with an error message
639 struct addrinfo *nl_addr_info(struct nl_addr *addr)
642 struct addrinfo *res;
643 char buf[INET6_ADDRSTRLEN+5];
644 struct addrinfo hint = {
645 .ai_flags = AI_NUMERICHOST,
646 .ai_family = addr->a_family,
649 nl_addr2str(addr, buf, sizeof(buf));
651 err = getaddrinfo(buf, NULL, &hint, &res);
653 nl_error(err, gai_strerror(err));
661 * Resolve abstract address object to a name using getnameinfo().
662 * @arg addr Abstract address object.
663 * @arg host Destination buffer for host name.
664 * @arg hostlen Length of destination buffer.
666 * Resolves the abstract address to a name and writes the looked up result
667 * into the host buffer. getnameinfo() is used to perform the lookup and
668 * is put into NI_NAMEREQD mode so the function will fail if the lookup
669 * couldn't be performed.
671 * @return 0 on success or a negative error code.
673 int nl_addr_resolve(struct nl_addr *addr, char *host, size_t hostlen)
676 struct sockaddr_in6 buf;
677 socklen_t salen = sizeof(buf);
679 err = nl_addr_fill_sockaddr(addr, (struct sockaddr *) &buf, &salen);
683 return getnameinfo((struct sockaddr *) &buf, salen,
684 host, hostlen, NULL, 0, NI_NAMEREQD);
690 * @name Attribute Access
695 * Set address family of abstract address object.
696 * @arg addr Abstract address object.
697 * @arg family New address family.
699 void nl_addr_set_family(struct nl_addr *addr, int family)
701 addr->a_family = family;
705 * Get address family of abstract address object.
706 * @arg addr Abstract address object.
708 int nl_addr_get_family(struct nl_addr *addr)
710 return addr->a_family;
714 * Set binary address of abstract address object.
715 * @arg addr Abstract address object.
716 * @arg buf Buffer containing binary address.
717 * @arg len Length of buffer containing binary address.
719 int nl_addr_set_binary_addr(struct nl_addr *addr, void *buf, size_t len)
721 if (len > addr->a_maxsize)
725 memcpy(addr->a_addr, buf, len);
731 * Get binary address of abstract address object.
732 * @arg addr Abstract address object.
734 void *nl_addr_get_binary_addr(struct nl_addr *addr)
740 * Get length of binary address of abstract address object.
741 * @arg addr Abstract address object.
743 unsigned int nl_addr_get_len(struct nl_addr *addr)
749 * Set prefix length of abstract address object.
750 * @arg addr Abstract address object.
751 * @arg prefixlen New prefix length.
753 void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
755 addr->a_prefixlen = prefixlen;
759 * Get prefix length of abstract address object.
760 * @arg addr Abstract address object.
762 unsigned int nl_addr_get_prefixlen(struct nl_addr *addr)
764 return addr->a_prefixlen;
770 * @name Translations to Strings
775 * Convert abstract address object to character string.
776 * @arg addr Abstract address object.
777 * @arg buf Destination buffer.
778 * @arg size Size of destination buffer.
780 * Converts an abstract address to a character string and stores
781 * the result in the specified destination buffer.
783 * @return Address represented in ASCII stored in destination buffer.
785 char *nl_addr2str(struct nl_addr *addr, char *buf, size_t size)
791 snprintf(buf, size, "none");
795 switch (addr->a_family) {
797 inet_ntop(AF_INET, addr->a_addr, buf, size);
801 inet_ntop(AF_INET6, addr->a_addr, buf, size);
805 dnet_ntop(addr->a_addr, addr->a_len, buf, size);
810 snprintf(buf, size, "%02x", addr->a_addr[0]);
811 for (i = 1; i < addr->a_len; i++) {
812 snprintf(tmp, sizeof(tmp), ":%02x",
814 strncat(buf, tmp, size - strlen(buf) - 1);
820 if (addr->a_prefixlen != (8 * addr->a_len)) {
821 snprintf(tmp, sizeof(tmp), "/%u", addr->a_prefixlen);
822 strncat(buf, tmp, size - strlen(buf) - 1);
831 * @name Address Family Transformations
835 static struct trans_tbl afs[] = {
836 __ADD(AF_UNSPEC,unspec)
838 __ADD(AF_LOCAL,local)
842 __ADD(AF_APPLETALK,appletalk)
843 __ADD(AF_NETROM,netrom)
844 __ADD(AF_BRIDGE,bridge)
845 __ADD(AF_ATMPVC,atmpvc)
847 __ADD(AF_INET6,inet6)
849 __ADD(AF_DECnet,decnet)
850 __ADD(AF_NETBEUI,netbeui)
851 __ADD(AF_SECURITY,security)
853 __ADD(AF_NETLINK,netlink)
854 __ADD(AF_ROUTE,route)
855 __ADD(AF_PACKET,packet)
857 __ADD(AF_ECONET,econet)
858 __ADD(AF_ATMSVC,atmsvc)
861 __ADD(AF_PPPOX,pppox)
862 __ADD(AF_WANPIPE,wanpipe)
864 __ADD(AF_BLUETOOTH,bluetooth)
869 * Convert address family to character string.
870 * @arg family Address family.
871 * @arg buf Destination buffer.
872 * @arg size Size of destination buffer.
874 * Converts an address family to a character string and stores it in the
877 * @return The destination buffer or the type encoded in hexidecimal form
878 * if no match was found.
880 char *nl_af2str(char family, char *buf, size_t size)
882 return __type2str(family, buf, size, afs, ARRAY_SIZE(afs));
886 * Convert character string to address family.
887 * @arg name Name of address family.
889 * Converts the provided character string specifying an address family
890 * to the corresponding numeric value.
892 * @return Address family as number or \c AF_UNSPEC.
894 char nl_str2af(const char *name)
896 char fam = __str2type(name, afs, ARRAY_SIZE(afs));
897 return fam >= 0 ? fam : AF_UNSPEC;