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 | static const char *mpls_ntop1(const struct mpls_label *addr, char *buf, size_t buflen) |
| 10 | { |
| 11 | size_t destlen = buflen; |
| 12 | char *dest = buf; |
| 13 | int count; |
| 14 | |
| 15 | for (count = 0; count < MPLS_MAX_LABELS; count++) { |
| 16 | uint32_t entry = ntohl(addr[count].entry); |
| 17 | uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; |
| 18 | int len = snprintf(dest, destlen, "%u", label); |
| 19 | |
| 20 | /* Is this the end? */ |
| 21 | if (entry & MPLS_LS_S_MASK) |
| 22 | return buf; |
| 23 | |
| 24 | |
| 25 | dest += len; |
| 26 | destlen -= len; |
| 27 | if (destlen) { |
| 28 | *dest = '/'; |
| 29 | dest++; |
| 30 | destlen--; |
| 31 | } |
| 32 | } |
| 33 | errno = -E2BIG; |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen) |
| 38 | { |
| 39 | switch(af) { |
| 40 | case AF_MPLS: |
| 41 | errno = 0; |
| 42 | return mpls_ntop1((struct mpls_label *)addr, buf, buflen); |
| 43 | default: |
| 44 | errno = EAFNOSUPPORT; |
| 45 | } |
| 46 | |
| 47 | return NULL; |
| 48 | } |