libnl: /data/src/libnl3/lib/mpls.c Source File

libnl 3.7.0

mpls.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Adapted from mpls_ntop and mpls_pton copied from iproute2,
4 * lib/mpls_ntop.c and lib/mpls_pton.c
5 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <netinet/in.h>
10 #include <netlink/netlink-compat.h>
11 #include <netlink-private/route/mpls.h>
12 #include <linux-private/linux/mpls.h>
13
14 static const char *mpls_ntop1(const struct mpls_label *addr,
15 char *buf, size_t buflen)
16 {
17 size_t destlen = buflen;
18 char *dest = buf;
19 int count = 0;
20
21 while (1) {
22 uint32_t entry = ntohl(addr[count++].entry);
23 uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
24 int len = snprintf(dest, destlen, "%u", label);
25
26 if (len >= destlen)
27 break;
28
29 /* Is this the end? */
30 if (entry & MPLS_LS_S_MASK)
31 return buf;
32
33 dest += len;
34 destlen -= len;
35 if (destlen) {
36 *dest = '/';
37 dest++;
38 destlen--;
39 }
40 }
41 errno = E2BIG;
42
43 return NULL;
44 }
45
46 const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen)
47 {
48 switch(af) {
49 case AF_MPLS:
50 errno = 0;
51 return mpls_ntop1((struct mpls_label *)addr, buf, buflen);
52 }
53
54 errno = EINVAL;
55 return NULL;
56 }
57
58 static int mpls_pton1(const char *name, struct mpls_label *addr,
59 unsigned int maxlabels)
60 {
61 char *endp;
62 unsigned count;
63
64 for (count = 0; count < maxlabels; count++) {
65 unsigned long label;
66
67 label = strtoul(name, &endp, 0);
68 /* Fail when the label value is out or range */
69 if (label >= (1 << 20))
70 return 0;
71
72 if (endp == name) /* no digits */
73 return 0;
74
75 addr->entry = htonl(label << MPLS_LS_LABEL_SHIFT);
76 if (*endp == '\0') {
77 addr->entry |= htonl(1 << MPLS_LS_S_SHIFT);
78 return (count + 1) * sizeof(struct mpls_label);
79 }
80
81 /* Bad character in the address */
82 if (*endp != '/')
83 return 0;
84
85 name = endp + 1;
86 addr += 1;
87 }
88
89 /* The address was too long */
90 return 0;
91 }
92
93 int mpls_pton(int af, const char *src, void *addr, size_t alen)
94 {
95 unsigned int maxlabels = alen / sizeof(struct mpls_label);
96 int err;
97
98 switch(af) {
99 case AF_MPLS:
100 errno = 0;
101 err = mpls_pton1(src, (struct mpls_label *)addr, maxlabels);
102 break;
103 default:
104 errno = EAFNOSUPPORT;
105 err = -1;
106 }
107
108 return err;
109 }