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
9static 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 = 0;
14
15	while (1) {
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		if (len >= destlen)
21			break;
22
23		/* Is this the end? */
24		if (entry & MPLS_LS_S_MASK)
25			return buf;
26
27		dest += len;
28		destlen -= len;
29		if (destlen) {
30			*dest = '/';
31			dest++;
32			destlen--;
33		}
34	}
35	errno = -E2BIG;
36	return NULL;
37}
38
39const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen)
40{
41	switch(af) {
42	case AF_MPLS:
43		errno = 0;
44		return mpls_ntop1((struct mpls_label *)addr, buf, buflen);
45	default:
46		errno = EAFNOSUPPORT;
47	}
48
49	return NULL;
50}
51