mailing list of musl libc
 help / color / mirror / code / Atom feed
d4e8b2dde88921f0118993f77b6188321ebce1af blob 1871 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
#define _GNU_SOURCE
#include <net/if.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "__netlink.h"

struct ifnamemap {
	unsigned int index;
	unsigned char namelen;
	char name[IFNAMSIZ];
};

struct ifnameindexctx {
	unsigned int num;
	unsigned int str_bytes;
	struct ifnamemap *list;
};

static int __handle_link(void *pctx, struct nlmsghdr *h)
{
	struct ifnameindexctx *ctx = pctx;
	struct ifinfomsg *ifim = NLMSG_DATA(h);
	struct rtattr *rta;
	struct ifnamemap *e;

	for (rta = NLMSG_RTA(h, sizeof(*ifim)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
		if (rta->rta_type != IFLA_IFNAME) continue;
		if (RTA_DATALEN(rta) > IFNAMSIZ) return -ENOBUFS;

		ctx->num++;
		ctx->str_bytes += RTA_DATALEN(rta) + 1;
		e = realloc(ctx->list, sizeof(struct ifnamemap[ctx->num]));
		if (e == 0) return -ENOMEM;
		ctx->list = e;

		e = &ctx->list[ctx->num-1];
		e->index = ifim->ifi_index;
		e->namelen = RTA_DATALEN(rta);
		memcpy(e->name,  RTA_DATA(rta), IFNAMSIZ);
	}

	return 0;
}

struct if_nameindex *if_nameindex()
{
	struct ifnameindexctx _ctx, *ctx = &_ctx;
	struct if_nameindex *ifs = NULL;
	struct __netlink_handle *nh;
	int r, i;
	char *p;

	nh = __netlink_open(NETLINK_ROUTE);
	if (!nh) goto err;
	memset(ctx, 0, sizeof(*ctx));
	r = __netlink_enumerate(nh, RTM_GETLINK, __handle_link, ctx);
	__netlink_close(nh);
	if (r < 0) goto err;

	ifs = malloc(sizeof(struct if_nameindex[ctx->num+1]) + ctx->str_bytes);
	if (ifs == 0) goto err;

	p = (char*)ifs + sizeof(struct if_nameindex[ctx->num+1]);
	for (i = 0; i < ctx->num; i++) {
		ifs[i].if_index = ctx->list[i].index;
		ifs[i].if_name = p;
		memcpy(p, ctx->list[i].name, ctx->list[i].namelen);
		p += ctx->list[i].namelen;
		*p++ = 0;
	}
	ifs[i].if_index = 0;
	ifs[i].if_name = 0;
err:
	free(ctx->list);
	if (ifs == NULL) errno = ENOBUFS;
	return ifs;
}
debug log:

solving d4e8b2d ...
found d4e8b2d in https://inbox.vuxu.org/musl/20140409170222.786b7bee@vostro/
found 53b80b2 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 53b80b21dc5570d84ea6bc44b9f94f6010e2a847	src/network/if_nameindex.c

applying [1/1] https://inbox.vuxu.org/musl/20140409170222.786b7bee@vostro/
diff --git a/src/network/if_nameindex.c b/src/network/if_nameindex.c
index 53b80b2..d4e8b2d 100644

Checking patch src/network/if_nameindex.c...
Applied patch src/network/if_nameindex.c cleanly.

index at:
100644 d4e8b2dde88921f0118993f77b6188321ebce1af	src/network/if_nameindex.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).