Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 1 | #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 Ahern | 4af4471 | 2017-05-13 19:27:02 -0600 | [diff] [blame] | 10 | static int mpls_pton1(const char *name, struct mpls_label *addr, |
| 11 | unsigned int maxlabels) |
Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 12 | { |
| 13 | char *endp; |
| 14 | unsigned count; |
| 15 | |
David Ahern | 4af4471 | 2017-05-13 19:27:02 -0600 | [diff] [blame] | 16 | for (count = 0; count < maxlabels; count++) { |
Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 17 | 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 Ahern | 4af4471 | 2017-05-13 19:27:02 -0600 | [diff] [blame] | 41 | fprintf(stderr, "Error: too many labels.\n"); |
Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
David Ahern | 4af4471 | 2017-05-13 19:27:02 -0600 | [diff] [blame] | 45 | int mpls_pton(int af, const char *src, void *addr, size_t alen) |
Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 46 | { |
David Ahern | 4af4471 | 2017-05-13 19:27:02 -0600 | [diff] [blame] | 47 | unsigned int maxlabels = alen / sizeof(struct mpls_label); |
Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 48 | int err; |
| 49 | |
| 50 | switch(af) { |
| 51 | case AF_MPLS: |
| 52 | errno = 0; |
David Ahern | 4af4471 | 2017-05-13 19:27:02 -0600 | [diff] [blame] | 53 | err = mpls_pton1(src, (struct mpls_label *)addr, maxlabels); |
Eric W. Biederman | dacc5d4 | 2015-03-15 14:53:45 -0500 | [diff] [blame] | 54 | break; |
| 55 | default: |
| 56 | errno = EAFNOSUPPORT; |
| 57 | err = -1; |
| 58 | } |
| 59 | |
| 60 | return err; |
| 61 | } |