iproute2/lib/mpls_ntop.c
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2
   3#include <errno.h>
   4#include <string.h>
   5#include <sys/types.h>
   6#include <netinet/in.h>
   7#include <linux/mpls.h>
   8
   9#include "utils.h"
  10
  11static const char *mpls_ntop1(const struct mpls_label *addr, char *buf, size_t buflen)
  12{
  13        size_t destlen = buflen;
  14        char *dest = buf;
  15        int count = 0;
  16
  17        while (1) {
  18                uint32_t entry = ntohl(addr[count++].entry);
  19                uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
  20                int len = snprintf(dest, destlen, "%u", label);
  21
  22                if (len >= destlen)
  23                        break;
  24
  25                /* Is this the end? */
  26                if (entry & MPLS_LS_S_MASK)
  27                        return buf;
  28
  29                dest += len;
  30                destlen -= len;
  31                if (destlen) {
  32                        *dest = '/';
  33                        dest++;
  34                        destlen--;
  35                }
  36        }
  37        errno = -E2BIG;
  38        return NULL;
  39}
  40
  41const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen)
  42{
  43        switch(af) {
  44        case AF_MPLS:
  45                errno = 0;
  46                return mpls_ntop1((struct mpls_label *)addr, buf, buflen);
  47        default:
  48                errno = EAFNOSUPPORT;
  49        }
  50
  51        return NULL;
  52}
  53