24#include <netlink-private/netlink.h>
25#include <netlink/netlink.h>
26#include <netlink/utils.h>
27#include <netlink/addr.h>
28#include <netlink-private/route/mpls.h>
29#include <linux/socket.h>
33static inline uint16_t dn_ntohs(uint16_t addr)
42 return ((uint16_t) u.byte[0]) | (((uint16_t) u.byte[1]) << 8);
45static inline int do_digit(
char *str, uint16_t *addr, uint16_t scale,
46 size_t *pos,
size_t len,
int *started)
48 uint16_t tmp = *addr / scale;
53 if (((tmp) > 0) || *started || (scale == 1)) {
57 *addr -= (tmp * scale);
63static const char *dnet_ntop(
const char *addrbuf,
size_t addrlen,
char *str,
66 uint16_t addr = dn_ntohs(*(uint16_t *)addrbuf);
67 uint16_t area = addr >> 10;
79 if (do_digit(str + pos, &area, 10, &pos, len, &started))
82 if (do_digit(str + pos, &area, 1, &pos, len, &started))
92 if (do_digit(str + pos, &addr, 1000, &pos, len, &started))
95 if (do_digit(str + pos, &addr, 100, &pos, len, &started))
98 if (do_digit(str + pos, &addr, 10, &pos, len, &started))
101 if (do_digit(str + pos, &addr, 1, &pos, len, &started))
112static int dnet_num(
const char *src, uint16_t * dst)
118 while ((tmp = *src++) != 0) {
120 if ((tmp < 0) || (tmp > 9))
131static inline int dnet_pton(
const char *src,
char *addrbuf)
137 pos = dnet_num(src, &area);
138 if ((pos == 0) || (area > 63) ||
139 ((*(src + pos) !=
'.') && (*(src + pos) !=
',')))
142 pos = dnet_num(src + pos + 1, &node);
143 if ((pos == 0) || (node > 1023))
146 *(uint16_t *)addrbuf = dn_ntohs((area << 10) | node);
151static void addr_destroy(
struct nl_addr *addr)
156 if (addr->a_refcnt != 1)
182 struct nl_addr *addr;
184 addr = calloc(1,
sizeof(*addr) + maxsize);
189 addr->a_maxsize = maxsize;
213 struct nl_addr *addr;
219 addr->a_family = family;
223 addr->a_prefixlen = 20;
226 addr->a_prefixlen = size*8;
230 memcpy(addr->a_addr, buf, size);
294 int err, copy = 0, len = 0, family = AF_UNSPEC, plen = 0;
295 char *str, *prefix = NULL, buf[256];
296 struct nl_addr *addr = NULL;
298 str = strdup(addrstr);
304 if (hint != AF_MPLS) {
305 prefix = strchr(str,
'/');
310 if (!strcasecmp(str,
"none")) {
315 if (!strcasecmp(str,
"default") ||
316 !strcasecmp(str,
"all") ||
317 !strcasecmp(str,
"any")) {
342 err = -NLE_AF_NOSUPPORT;
349 if (hint == AF_INET || hint == AF_UNSPEC) {
350 if (inet_pton(AF_INET, str, buf) > 0) {
355 if (hint == AF_INET) {
361 if (hint == AF_INET6 || hint == AF_UNSPEC) {
362 if (inet_pton(AF_INET6, str, buf) > 0) {
367 if (hint == AF_INET6) {
373 if ((hint == AF_LLC || hint == AF_UNSPEC) && strchr(str,
':')) {
374 unsigned int a, b, c, d, e, f;
376 if (sscanf(str,
"%02x:%02x:%02x:%02x:%02x:%02x",
377 &a, &b, &c, &d, &e, &f) == 6) {
380 buf[0] = (
unsigned char) a;
381 buf[1] = (
unsigned char) b;
382 buf[2] = (
unsigned char) c;
383 buf[3] = (
unsigned char) d;
384 buf[4] = (
unsigned char) e;
385 buf[5] = (
unsigned char) f;
389 if (hint == AF_LLC) {
395 if ((hint == AF_DECnet || hint == AF_UNSPEC) &&
396 (strchr(str,
'.') || strchr(str,
','))) {
397 if (dnet_pton(str, buf) > 0) {
402 if (hint == AF_DECnet) {
408 if (hint == AF_MPLS) {
409 len = mpls_pton(AF_MPLS, str, buf,
sizeof(buf));
419 if (hint == AF_UNSPEC && strchr(str,
':')) {
423 long l = strtol(s, &p, 16);
425 if (s == p || l > 0xff || i >=
sizeof(buf)) {
430 buf[i++] = (
unsigned char) l;
460 long pl = strtol(++prefix, &p, 0);
496 new =
nl_addr_build(addr->a_family, addr->a_addr, addr->a_len);
498 new->a_prefixlen = addr->a_prefixlen;
543 if (addr->a_refcnt == 1)
557 return addr->a_refcnt > 1;
595 d = a->a_family - b->a_family;
597 d = a->a_len - b->a_len;
599 if (a->a_len && d == 0) {
600 d = memcmp(a->a_addr, b->a_addr, a->a_len);
603 return (a->a_prefixlen - b->a_prefixlen);
625 int d = a->a_family - b->a_family;
628 int len = min(a->a_prefixlen, b->a_prefixlen);
631 d = memcmp(a->a_addr, b->a_addr, bytes);
632 if (d == 0 && (len % 8) != 0) {
633 int mask = (0xFF00 >> (len % 8)) & 0xFF;
635 d = (a->a_addr[bytes] & mask) -
636 (b->a_addr[bytes] & mask);
653 for (i = 0; i < addr->a_len; i++)
676 ret = inet_pton(family, addr, buf);
682 ret = mpls_pton(family, addr, buf,
sizeof(buf));
688 ret = dnet_pton(addr, buf);
694 if (sscanf(addr,
"%*02x:%*02x:%*02x:%*02x:%*02x:%*02x") != 6)
710 switch (addr->a_len) {
738 switch (addr->a_family) {
740 struct sockaddr_in *sai = (
struct sockaddr_in *) sa;
742 if (*salen <
sizeof(*sai))
745 if (addr->a_len == 4)
746 memcpy(&sai->sin_addr, addr->a_addr, 4);
747 else if (addr->a_len != 0)
750 memset(&sai->sin_addr, 0, 4);
752 sai->sin_family = addr->a_family;
753 *salen =
sizeof(*sai);
758 struct sockaddr_in6 *sa6 = (
struct sockaddr_in6 *) sa;
760 if (*salen <
sizeof(*sa6))
763 if (addr->a_len == 16)
764 memcpy(&sa6->sin6_addr, addr->a_addr, 16);
765 else if (addr->a_len != 0)
768 memset(&sa6->sin6_addr, 0, 16);
770 sa6->sin6_family = addr->a_family;
771 *salen =
sizeof(*sa6);
806 char buf[INET6_ADDRSTRLEN+5];
807 struct addrinfo hint = {
808 .ai_flags = AI_NUMERICHOST,
809 .ai_family = addr->a_family,
814 err = getaddrinfo(buf, NULL, &hint, result);
817 case EAI_ADDRFAMILY:
return -NLE_AF_NOSUPPORT;
818 case EAI_AGAIN:
return -NLE_AGAIN;
819 case EAI_BADFLAGS:
return -NLE_INVAL;
820 case EAI_FAIL:
return -NLE_NOADDR;
821 case EAI_FAMILY:
return -NLE_AF_NOSUPPORT;
822 case EAI_MEMORY:
return -NLE_NOMEM;
823 case EAI_NODATA:
return -NLE_NOADDR;
824 case EAI_NONAME:
return -NLE_OBJ_NOTFOUND;
825 case EAI_SERVICE:
return -NLE_OPNOTSUPP;
826 case EAI_SOCKTYPE:
return -NLE_BAD_SOCK;
827 default:
return -NLE_FAILURE;
850 struct sockaddr_in6 buf;
851 socklen_t salen =
sizeof(buf);
857 err = getnameinfo((
struct sockaddr *) &buf, salen, host, hostlen,
858 NULL, 0, NI_NAMEREQD);
860 return nl_syserr2nlerr(err);
881 addr->a_family = family;
894 return addr->a_family;
919 if (len > addr->a_maxsize)
923 memset(addr->a_addr, 0, addr->a_maxsize);
926 memcpy(addr->a_addr, buf, len);
942 return (
void*)addr->a_addr;
966 addr->a_prefixlen = prefixlen;
977 return addr->a_prefixlen;
998char *
nl_addr2str(
const struct nl_addr *addr,
char *buf,
size_t size)
1003 if (!addr || !addr->a_len) {
1004 snprintf(buf, size,
"none");
1011 switch (addr->a_family) {
1013 inet_ntop(AF_INET, addr->a_addr, buf, size);
1017 inet_ntop(AF_INET6, addr->a_addr, buf, size);
1021 mpls_ntop(AF_MPLS, addr->a_addr, buf, size);
1025 dnet_ntop(addr->a_addr, addr->a_len, buf, size);
1030 snprintf(buf, size,
"%02x",
1031 (
unsigned char) addr->a_addr[0]);
1032 for (i = 1; i < addr->a_len; i++) {
1033 snprintf(tmp,
sizeof(tmp),
":%02x",
1034 (
unsigned char) addr->a_addr[i]);
1035 strncat(buf, tmp, size - strlen(buf) - 1);
1041 if (addr->a_family != AF_MPLS &&
1042 addr->a_prefixlen != (8 * addr->a_len)) {
1043 snprintf(tmp,
sizeof(tmp),
"/%u", addr->a_prefixlen);
1044 strncat(buf, tmp, size - strlen(buf) - 1);
1057static const struct trans_tbl afs[] = {
1058 __ADD(AF_UNSPEC,unspec),
1059 __ADD(AF_UNIX,unix),
1060 __ADD(AF_INET,inet),
1061 __ADD(AF_AX25,ax25),
1063 __ADD(AF_APPLETALK,appletalk),
1064 __ADD(AF_NETROM,netrom),
1065 __ADD(AF_BRIDGE,bridge),
1066 __ADD(AF_ATMPVC,atmpvc),
1068 __ADD(AF_INET6,inet6),
1069 __ADD(AF_ROSE,rose),
1070 __ADD(AF_DECnet,decnet),
1071 __ADD(AF_NETBEUI,netbeui),
1072 __ADD(AF_SECURITY,security),
1074 __ADD(AF_NETLINK,netlink),
1075 __ADD(AF_PACKET,packet),
1077 __ADD(AF_ECONET,econet),
1078 __ADD(AF_ATMSVC,atmsvc),
1083 __ADD(AF_IRDA,irda),
1084 __ADD(AF_PPPOX,pppox),
1085 __ADD(AF_WANPIPE,wanpipe),
1091 __ADD(AF_TIPC,tipc),
1093 __ADD(AF_BLUETOOTH,bluetooth),
1095 __ADD(AF_IUCV,iucv),
1098 __ADD(AF_RXRPC,rxrpc),
1101 __ADD(AF_ISDN,isdn),
1104 __ADD(AF_PHONET,phonet),
1107 __ADD(AF_IEEE802154,ieee802154),
1110 __ADD(AF_CAIF,caif),
1119 __ADD(AF_VSOCK,vsock),
1121 __ADD(AF_MPLS,mpls),
1124char *nl_af2str(
int family,
char *buf,
size_t size)
1126 return __type2str(family, buf, size, afs, ARRAY_SIZE(afs));
1129int nl_str2af(
const char *name)
1131 int fam = __str2type(name, afs, ARRAY_SIZE(afs));
1132 return fam >= 0 ? fam : -EINVAL;
int nl_addr_info(const struct nl_addr *addr, struct addrinfo **result)
Call getaddrinfo() for an abstract address object.
int nl_addr_iszero(const struct nl_addr *addr)
Returns true if the address consists of all zeros.
int nl_addr_fill_sockaddr(const struct nl_addr *addr, struct sockaddr *sa, socklen_t *salen)
Fill out sockaddr structure with values from abstract address object.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
int nl_addr_resolve(const struct nl_addr *addr, char *host, size_t hostlen)
Resolve abstract address object to a name using getnameinfo().
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
struct nl_addr * nl_addr_alloc(size_t maxsize)
Allocate empty abstract address.
int nl_addr_parse(const char *addrstr, int hint, struct nl_addr **result)
Allocate abstract address based on character string.
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
int nl_addr_set_binary_addr(struct nl_addr *addr, const void *buf, size_t len)
Set binary address of abstract address object.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
int nl_addr_cmp_prefix(const struct nl_addr *a, const struct nl_addr *b)
Compare the prefix of two abstract addresses.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
int nl_addr_valid(const char *addr, int family)
Check if address string is parseable for a specific address family.
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
int nl_addr_shared(const struct nl_addr *addr)
Check whether an abstract address is shared.
int nl_addr_guess_family(const struct nl_addr *addr)
Guess address family of abstract address based on address size.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int nla_len(const struct nlattr *nla)
Return length of the payload .