mirror of
https://github.com/openharmony/third_party_libnl.git
synced 2026-07-20 23:45:55 -04:00
cee0b1b50c
Software Package Data Exchange identifiers help to detect source file licenses and hence simplify the FOSS compliance process. Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com> https://github.com/thom311/libnl/pull/219 --- FILES=( lib/addr.c lib/attr.c lib/cache.c lib/cache_mngr.c lib/cache_mngt.c lib/data.c lib/error.c lib/fib_lookup/lookup.c lib/fib_lookup/request.c lib/genl/ctrl.c lib/genl/family.c lib/genl/genl.c lib/genl/mngt.c lib/handlers.c lib/hash.c lib/hashtable.c lib/idiag/idiag.c lib/idiag/idiag_meminfo_obj.c lib/idiag/idiag_msg_obj.c lib/idiag/idiag_req_obj.c lib/idiag/idiag_vegasinfo_obj.c lib/mpls.c lib/msg.c lib/netfilter/ct.c lib/netfilter/ct_obj.c lib/netfilter/exp.c lib/netfilter/exp_obj.c lib/netfilter/log.c lib/netfilter/log_msg.c lib/netfilter/log_msg_obj.c lib/netfilter/log_obj.c lib/netfilter/netfilter.c lib/netfilter/nfnl.c lib/netfilter/queue.c lib/netfilter/queue_msg.c lib/netfilter/queue_msg_obj.c lib/netfilter/queue_obj.c lib/nl.c lib/object.c lib/route/act.c lib/route/addr.c lib/route/class.c lib/route/classid.c lib/route/cls.c lib/route/link.c lib/route/neigh.c lib/route/neightbl.c lib/route/netconf.c lib/route/nexthop.c lib/route/nexthop_encap.c lib/route/nh_encap_mpls.c lib/route/pktloc.c lib/route/qdisc.c lib/route/route.c lib/route/route_obj.c lib/route/route_utils.c lib/route/rtnl.c lib/route/rule.c lib/route/tc.c lib/socket.c lib/utils.c lib/version.c lib/xfrm/ae.c lib/xfrm/lifetime.c lib/xfrm/sa.c lib/xfrm/selector.c lib/xfrm/sp.c lib/xfrm/template.c src/genl-ctrl-list.c src/idiag-socket-details.c src/lib/addr.c src/lib/class.c src/lib/cls.c src/lib/ct.c src/lib/exp.c src/lib/link.c src/lib/neigh.c src/lib/qdisc.c src/lib/route.c src/lib/rule.c src/lib/tc.c src/lib/utils.c src/nf-ct-add.c src/nf-ct-events.c src/nf-ct-list.c src/nf-exp-add.c src/nf-exp-delete.c src/nf-exp-list.c src/nf-log.c src/nf-monitor.c src/nf-queue.c src/nl-addr-add.c src/nl-addr-delete.c src/nl-addr-list.c src/nl-class-add.c src/nl-class-delete.c src/nl-class-list.c src/nl-classid-lookup.c src/nl-cls-add.c src/nl-cls-delete.c src/nl-cls-list.c src/nl-fib-lookup.c src/nl-link-enslave.c src/nl-link-ifindex2name.c src/nl-link-list.c src/nl-link-name2ifindex.c src/nl-link-release.c src/nl-link-set.c src/nl-link-stats.c src/nl-list-caches.c src/nl-list-sockets.c src/nl-monitor.c src/nl-neigh-add.c src/nl-neigh-delete.c src/nl-neigh-list.c src/nl-neightbl-list.c src/nl-pktloc-lookup.c src/nl-qdisc-add.c src/nl-qdisc-delete.c src/nl-qdisc-list.c src/nl-route-add.c src/nl-route-delete.c src/nl-route-get.c src/nl-route-list.c src/nl-rule-list.c src/nl-tctree-list.c src/nl-util-addr.c ) sed '1s#^#/* SPDX-License-Identifier: LGPL-2.1-only */\n#' "${FILES[@]}" -i
110 lines
2.1 KiB
C
110 lines
2.1 KiB
C
/* SPDX-License-Identifier: LGPL-2.1-only */
|
|
/*
|
|
* Adapted from mpls_ntop and mpls_pton copied from iproute2,
|
|
* lib/mpls_ntop.c and lib/mpls_pton.c
|
|
*/
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <netinet/in.h>
|
|
#include <netlink/netlink-compat.h>
|
|
#include <netlink-private/route/mpls.h>
|
|
#include <linux-private/linux/mpls.h>
|
|
|
|
static const char *mpls_ntop1(const struct mpls_label *addr,
|
|
char *buf, size_t buflen)
|
|
{
|
|
size_t destlen = buflen;
|
|
char *dest = buf;
|
|
int count = 0;
|
|
|
|
while (1) {
|
|
uint32_t entry = ntohl(addr[count++].entry);
|
|
uint32_t label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
|
|
int len = snprintf(dest, destlen, "%u", label);
|
|
|
|
if (len >= destlen)
|
|
break;
|
|
|
|
/* Is this the end? */
|
|
if (entry & MPLS_LS_S_MASK)
|
|
return buf;
|
|
|
|
dest += len;
|
|
destlen -= len;
|
|
if (destlen) {
|
|
*dest = '/';
|
|
dest++;
|
|
destlen--;
|
|
}
|
|
}
|
|
errno = E2BIG;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
const char *mpls_ntop(int af, const void *addr, char *buf, size_t buflen)
|
|
{
|
|
switch(af) {
|
|
case AF_MPLS:
|
|
errno = 0;
|
|
return mpls_ntop1((struct mpls_label *)addr, buf, buflen);
|
|
}
|
|
|
|
errno = EINVAL;
|
|
return NULL;
|
|
}
|
|
|
|
static int mpls_pton1(const char *name, struct mpls_label *addr,
|
|
unsigned int maxlabels)
|
|
{
|
|
char *endp;
|
|
unsigned count;
|
|
|
|
for (count = 0; count < maxlabels; count++) {
|
|
unsigned long label;
|
|
|
|
label = strtoul(name, &endp, 0);
|
|
/* Fail when the label value is out or range */
|
|
if (label >= (1 << 20))
|
|
return 0;
|
|
|
|
if (endp == name) /* no digits */
|
|
return 0;
|
|
|
|
addr->entry = htonl(label << MPLS_LS_LABEL_SHIFT);
|
|
if (*endp == '\0') {
|
|
addr->entry |= htonl(1 << MPLS_LS_S_SHIFT);
|
|
return (count + 1) * sizeof(struct mpls_label);
|
|
}
|
|
|
|
/* Bad character in the address */
|
|
if (*endp != '/')
|
|
return 0;
|
|
|
|
name = endp + 1;
|
|
addr += 1;
|
|
}
|
|
|
|
/* The address was too long */
|
|
return 0;
|
|
}
|
|
|
|
int mpls_pton(int af, const char *src, void *addr, size_t alen)
|
|
{
|
|
unsigned int maxlabels = alen / sizeof(struct mpls_label);
|
|
int err;
|
|
|
|
switch(af) {
|
|
case AF_MPLS:
|
|
errno = 0;
|
|
err = mpls_pton1(src, (struct mpls_label *)addr, maxlabels);
|
|
break;
|
|
default:
|
|
errno = EAFNOSUPPORT;
|
|
err = -1;
|
|
}
|
|
|
|
return err;
|
|
}
|