blob: 6d2e6a69436a785695fda047167a6414874742ab [file] [log] [blame]
Eric W. Biedermandacc5d42015-03-15 14:53:45 -05001#include <errno.h>
2#include <string.h>
3#include <sys/types.h>
4#include <netinet/in.h>
5#include <linux/mpls.h>
6
7#include "utils.h"
8
9
David Ahern4af44712017-05-13 19:27:02 -060010static int mpls_pton1(const char *name, struct mpls_label *addr,
11 unsigned int maxlabels)
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050012{
13 char *endp;
14 unsigned count;
15
David Ahern4af44712017-05-13 19:27:02 -060016 for (count = 0; count < maxlabels; count++) {
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050017 unsigned long label;
18
19 label = strtoul(name, &endp, 0);
20 /* Fail when the label value is out or range */
21 if (label >= (1 << 20))
22 return 0;
23
24 if (endp == name) /* no digits */
25 return 0;
26
27 addr->entry = htonl(label << MPLS_LS_LABEL_SHIFT);
28 if (*endp == '\0') {
29 addr->entry |= htonl(1 << MPLS_LS_S_SHIFT);
30 return 1;
31 }
32
33 /* Bad character in the address */
34 if (*endp != '/')
35 return 0;
36
37 name = endp + 1;
38 addr += 1;
39 }
40 /* The address was too long */
David Ahern4af44712017-05-13 19:27:02 -060041 fprintf(stderr, "Error: too many labels.\n");
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050042 return 0;
43}
44
David Ahern4af44712017-05-13 19:27:02 -060045int mpls_pton(int af, const char *src, void *addr, size_t alen)
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050046{
David Ahern4af44712017-05-13 19:27:02 -060047 unsigned int maxlabels = alen / sizeof(struct mpls_label);
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050048 int err;
49
50 switch(af) {
51 case AF_MPLS:
52 errno = 0;
David Ahern4af44712017-05-13 19:27:02 -060053 err = mpls_pton1(src, (struct mpls_label *)addr, maxlabels);
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050054 break;
55 default:
56 errno = EAFNOSUPPORT;
57 err = -1;
58 }
59
60 return err;
61}