mailing list of musl libc
 help / color / mirror / code / Atom feed
5b1ebe76ebd1a96dc197ce7284f4e45926db05ea blob 5508 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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
 
#define _GNU_SOURCE
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ifaddrs.h>
#include <net/if.h>
#include "__netlink.h"

/* getifaddrs() uses PF_PACKET to relay hardware addresses.
 * But Infiniband socket address length is longer, so use this hack
 * (like glibc) to return it anyway. */
struct sockaddr_ll_hack {
	unsigned short sll_family, sll_protocol;
	int sll_ifindex;
	unsigned short sll_hatype;
	unsigned char sll_pkttype, sll_halen;
	unsigned char sll_addr[24];
};

union sockany {
	struct sockaddr sa;
	struct sockaddr_ll_hack ll;
	struct sockaddr_in v4;
	struct sockaddr_in6 v6;
};

struct ifaddrs_storage {
	struct ifaddrs ifa;
	struct ifaddrs_storage *hash_next;
	union sockany addr, netmask, ifu;
	unsigned int index;
	char name[IFNAMSIZ+1];
};

#define IFADDRS_HASH_SIZE 64
struct ifaddrs_ctx {
	struct ifaddrs_storage *first;
	struct ifaddrs_storage *last;
	struct ifaddrs_storage *hash[IFADDRS_HASH_SIZE];
};

void freeifaddrs(struct ifaddrs *ifp)
{
	struct ifaddrs *n;
	while (ifp) {
		n = ifp->ifa_next;
		free(ifp);
		ifp = n;
	}
}

static void addifaddrs(struct ifaddrs_ctx *ctx, struct ifaddrs_storage *add)
{
	if (!add->ifa.ifa_name) {
		free(add);
		return;
	}
	if (!ctx->first) ctx->first = add;
	if (ctx->last) ctx->last->ifa.ifa_next = &add->ifa;
	ctx->last = add;
}

static struct sockaddr* copy_lladdr(union sockany *sa, struct rtattr *rta, struct ifinfomsg *ifi)
{
	if (RTA_DATALEN(rta) > sizeof(sa->ll.sll_addr)) return 0;
	sa->ll.sll_family = AF_PACKET;
	sa->ll.sll_ifindex = ifi->ifi_index;
	sa->ll.sll_hatype = ifi->ifi_type;
	sa->ll.sll_halen = RTA_DATALEN(rta);
	memcpy(sa->ll.sll_addr, RTA_DATA(rta), RTA_DATALEN(rta));
	return &sa->sa;
}

static uint8_t *sockany_addr(int af, union sockany *sa, int *len)
{
	switch (af) {
	case AF_INET: *len = 4; return (uint8_t*) &sa->v4.sin_addr;
	case AF_INET6: *len = 16; return (uint8_t*) &sa->v6.sin6_addr;
	}
	return 0;
}

static struct sockaddr* copy_addr(int af, union sockany *sa, struct rtattr *rta)
{
	int len;
	uint8_t *dst = sockany_addr(af, sa, &len);
	if (!dst || RTA_DATALEN(rta) != len) return 0;
	sa->sa.sa_family = af;
	memcpy(dst, RTA_DATA(rta), len);
	return &sa->sa;
}

static struct sockaddr *gen_netmask(int af, union sockany *sa, int prefixlen)
{
	int maxlen, i;
	uint8_t *dst = sockany_addr(af, sa, &maxlen);
	if (!dst) return 0;
	sa->sa.sa_family = af;
	if (prefixlen > 8*maxlen) prefixlen = 8*maxlen;
	i = prefixlen / 8;
	memset(dst, 0xff, i);
	if (i<maxlen) {
		dst[i++] = 0xff << (8 - (prefixlen % 8));
		if (i<maxlen) memset(&dst[i+1], 0x00, maxlen-i);
	}
	return &sa->sa;
}

static int __handle_link(void *pctx, struct nlmsghdr *h)
{
	struct ifaddrs_ctx *ctx = pctx;
	struct ifaddrs_storage *ifs;
	struct ifinfomsg *ifi = NLMSG_DATA(h);
	struct rtattr *rta;
	int stats_len = 0;

	for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
		if (rta->rta_type != IFLA_STATS) continue;
		stats_len = RTA_DATALEN(rta);
		break;
	}

	ifs = calloc(1, sizeof(struct ifaddrs_storage) + stats_len);
	if (ifs == 0) return -1;

	ifs->index = ifi->ifi_index;
	ifs->ifa.ifa_flags = ifi->ifi_flags;

	for (rta = NLMSG_RTA(h, sizeof(*ifi)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
		switch (rta->rta_type) {
		case IFLA_IFNAME:
			if (RTA_DATALEN(rta) <= IFNAMSIZ) {
				strncpy(ifs->name, RTA_DATA(rta), RTA_DATALEN(rta));
				ifs->ifa.ifa_name = ifs->name;
			}
			break;
		case IFLA_ADDRESS:
			ifs->ifa.ifa_addr = copy_lladdr(&ifs->addr, rta, ifi);
			break;
		case IFLA_BROADCAST:
			ifs->ifa.ifa_broadaddr = copy_lladdr(&ifs->ifu, rta, ifi);
			break;
		case IFLA_STATS:
			ifs->ifa.ifa_data = (void*)(ifs+1);
			memcpy(ifs->ifa.ifa_data, RTA_DATA(rta), RTA_DATALEN(rta));
			break;
		}
	}
	if (ifs->ifa.ifa_name) {
		ifs->hash_next = ctx->hash[ifs->index%IFADDRS_HASH_SIZE];
		ctx->hash[ifs->index%IFADDRS_HASH_SIZE] = ifs;
	}
	addifaddrs(ctx, ifs);
	return 0;
}

static int __handle_addr(void *pctx, struct nlmsghdr *h)
{
	struct ifaddrs_ctx *ctx = pctx;
	struct ifaddrs_storage *ifs, *ifs0;
	struct ifaddrmsg *ifa = NLMSG_DATA(h);
	struct rtattr *rta;

	ifs = calloc(1, sizeof(struct ifaddrs_storage));
	if (ifs == 0) return -1;

	for (ifs0 = ctx->hash[ifa->ifa_index%IFADDRS_HASH_SIZE]; ifs0; ifs0 = ifs0->hash_next)
		if (ifs0->index == ifa->ifa_index)
			break;
	if (!ifs0) return 0;

	ifs->ifa.ifa_name = ifs0->ifa.ifa_name;
	ifs->ifa.ifa_flags = ifs0->ifa.ifa_flags;
	for (rta = NLMSG_RTA(h, sizeof(*ifa)); NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
		switch (rta->rta_type) {
		case IFA_ADDRESS:
			ifs->ifa.ifa_addr = copy_addr(ifa->ifa_family, &ifs->addr, rta);
			if (ifs->ifa.ifa_addr)
				ifs->ifa.ifa_netmask = gen_netmask(ifa->ifa_family, &ifs->netmask, ifa->ifa_prefixlen);
			break;
		case IFA_BROADCAST:
			/* For point-to-point links this is peer, but ifa_broadaddr
			 * and ifa_dstaddr are union, so this works for both.  */
			ifs->ifa.ifa_broadaddr = copy_addr(ifa->ifa_family, &ifs->ifu, rta);
			break;
		}
	}

	addifaddrs(ctx, ifs);
	return 0;
}

int getifaddrs(struct ifaddrs **ifap)
{
	struct ifaddrs_ctx _ctx, *ctx = &_ctx;
	struct __netlink_handle *nh;
	int r = 0;

	nh = __netlink_open(NETLINK_ROUTE);
	if (!nh) return -1;
	memset(ctx, 0, sizeof(*ctx));
	if (__netlink_enumerate(nh, RTM_GETLINK, __handle_link, ctx)) r = -1;
	if (__netlink_enumerate(nh, RTM_GETADDR, __handle_addr, ctx)) r = -1;
	__netlink_close(nh);
	if (r == 0) *ifap = &ctx->first->ifa;
	else freeifaddrs(&ctx->first->ifa);
	return r;
}
debug log:

solving 5b1ebe7 ...
found 5b1ebe7 in https://inbox.vuxu.org/musl/20140409170222.786b7bee@vostro/
found 5a94cc7 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 5a94cc7c4496fe4edda2938dc672c9a3301d0552	src/network/getifaddrs.c

applying [1/1] https://inbox.vuxu.org/musl/20140409170222.786b7bee@vostro/
diff --git a/src/network/getifaddrs.c b/src/network/getifaddrs.c
index 5a94cc7..5b1ebe7 100644

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

index at:
100644 5b1ebe76ebd1a96dc197ce7284f4e45926db05ea	src/network/getifaddrs.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).