blob: bd448cfcf14ac2cd5083a69eeb5edfb09e7f5d3e [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
10static int mpls_pton1(const char *name, struct mpls_label *addr)
11{
12 char *endp;
13 unsigned count;
14
15 for (count = 0; count < MPLS_MAX_LABELS; count++) {
16 unsigned long label;
17
18 label = strtoul(name, &endp, 0);
19 /* Fail when the label value is out or range */
20 if (label >= (1 << 20))
21 return 0;
22
23 if (endp == name) /* no digits */
24 return 0;
25
26 addr->entry = htonl(label << MPLS_LS_LABEL_SHIFT);
27 if (*endp == '\0') {
28 addr->entry |= htonl(1 << MPLS_LS_S_SHIFT);
29 return 1;
30 }
31
32 /* Bad character in the address */
33 if (*endp != '/')
34 return 0;
35
36 name = endp + 1;
37 addr += 1;
38 }
39 /* The address was too long */
40 return 0;
41}
42
43int mpls_pton(int af, const char *src, void *addr)
44{
45 int err;
46
47 switch(af) {
48 case AF_MPLS:
49 errno = 0;
50 err = mpls_pton1(src, (struct mpls_label *)addr);
51 break;
52 default:
53 errno = EAFNOSUPPORT;
54 err = -1;
55 }
56
57 return err;
58}