summaryrefslogtreecommitdiffstats
path: root/agents/virt/common/ip_lookup.c
blob: aded8ea5b8b44da6d2d64490fdfd233e66b9d102 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
  Copyright Red Hat, Inc. 2004, 2006

  The Magma Cluster API Library is free software; you can redistribute
  it and/or modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either version
  2.1 of the License, or (at your option) any later version.

  The Magma Cluster API Library is distributed in the hope that it will
  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
 */
/** @file
 * Build lists of IPs on the system, excepting loopback ipv6 link-local
 */

#include "config.h"

#include <asm/types.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>

/* Local includes */
#include "ip_lookup.h"
#include "debug.h"

static int
send_addr_dump(int fd, int family)
{
	struct nlmsghdr *nh;
	struct rtgenmsg *g;
	char buf[256];
	struct sockaddr_nl addr;

	memset(&addr,0,sizeof(addr));
	addr.nl_family = PF_NETLINK;

	memset(buf, 0, sizeof(buf));
	nh = (struct nlmsghdr *)buf;
	g = (struct rtgenmsg *)(buf + sizeof(struct nlmsghdr));

	nh->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
	nh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP;
	nh->nlmsg_type = RTM_GETADDR;
	g->rtgen_family = family;

	return sendto(fd, buf, nh->nlmsg_len, 0, (struct sockaddr *)&addr,
	   	      sizeof(addr));
}


static int
add_ip(ip_list_t *ipl, char *ipaddr, char family)
{
	ip_addr_t *ipa;

	if (family == PF_INET6) {
		/* Avoid loopback */
		if (!strcmp(ipaddr, "::1"))
			return -1;

		/* Avoid link-local addresses */
		if (!strncmp(ipaddr, "fe80", 4))
			return -1;
		if (!strncmp(ipaddr, "fe90", 4))
			return -1;
		if (!strncmp(ipaddr, "fea0", 4))
			return -1;
		if (!strncmp(ipaddr, "feb0", 4))
			return -1;
	}
	
	ipa = calloc(1, sizeof(*ipa));
	if (!ipa)
		return -1;
	ipa->ipa_family = family;
	ipa->ipa_address = strdup(ipaddr);

	dbg_printf(4, "Adding IP %s to list (family %d)\n", ipaddr, family);
	TAILQ_INSERT_TAIL(ipl, ipa, ipa_entries);

	return 0;
}


static int
add_ip_addresses(int family, ip_list_t *ipl)
{
	/* List ipv4 addresses */
	struct nlmsghdr *nh;
	struct ifaddrmsg *ifa;
	struct rtattr *rta, *nrta;
	struct nlmsgerr *err;
	char buf[10240];
	char outbuf[256];
	char label[256];
	int x, fd, len;

	dbg_printf(5, "Connecting to Netlink...\n");
	fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
	if (fd < 0) {
		perror("socket");
		exit(1);
	}

	dbg_printf(5, "Sending address dump request\n");
	if (send_addr_dump(fd, family) < 0) {
		perror("sendto");
		close(fd);
		return -1;
	}
	memset(buf, 0, sizeof(buf));

	dbg_printf(5, "Waiting for response\n");
	x = recvfrom(fd, buf, sizeof(buf), 0, NULL, 0);
	if (x < 0) {
		perror("recvfrom");
		close(fd);
		return -1;
	}

	dbg_printf(5, "Received %d bytes\n", x);

	nh = (struct nlmsghdr *)buf;
	while (NLMSG_OK(nh, x)) {

		switch(nh->nlmsg_type) {
		case NLMSG_DONE:
			close(fd);
    			return 0;

		case NLMSG_ERROR:
			err = (struct nlmsgerr*)NLMSG_DATA(nh);
			if (nh->nlmsg_len <
			    NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
				fprintf(stderr, "ERROR truncated");
			} else {
				errno = -err->error;
				perror("RTNETLINK answers");
			}
			close(fd);
			return -1;

		case RTM_NEWADDR:
			break;

		default:
			nh = NLMSG_NEXT(nh, x);
			continue;
		}

		/* RTM_NEWADDR */
		len = NLMSG_PAYLOAD(nh,0);
		ifa = NLMSG_DATA(nh);

		/* Make sure we got the type we expect back */
		if (ifa->ifa_family != family) {
			nh = NLMSG_NEXT(nh, x);
			continue;
		}

		rta = IFA_RTA(ifa);
		len -= sizeof(struct ifaddrmsg);
		do {
			/* Make sure we've got a valid rtaddr field */
			if (!RTA_OK(rta, len)) {
				dbg_printf(5, "!RTA_OK(rta, len)\n");
				break;
			}

			if (rta->rta_type == IFA_ADDRESS) {
				inet_ntop(family, RTA_DATA(rta), outbuf,
					  sizeof(outbuf) );
				add_ip(ipl, outbuf, family);
			}

			if (rta->rta_type == IFA_LABEL) {
				memset(label, 0, sizeof(label));
				strncpy(label, (char *)RTA_DATA(rta), sizeof(label) - 1);
				dbg_printf(5, "Skipping label: %s\n",
					label);
			}

			nrta = RTA_NEXT(rta, len);
			len -= (nrta - rta);
			rta = nrta;
		} while (RTA_OK(rta, len));

		nh = NLMSG_NEXT(nh, x);
	}

	dbg_printf(5, "Closing Netlink connection\n");
	close(fd);
	return 0;
}


int
ip_search(ip_list_t *ipl, char *ip_name)
{
	ip_addr_t *ipa;
	
	dbg_printf(5, "Looking for IP address %s in IP list %p...", ip_name, ipl);
	ipa = ipl->tqh_first;
	for (ipa = ipl->tqh_first; ipa; ipa = ipa->ipa_entries.tqe_next) {
		if (!strcmp(ip_name, ipa->ipa_address)) {
			dbg_printf(4,"Found\n");
			return 0;
		}
	}
	dbg_printf(5, "Not found\n");
	return 1;
}


int
ip_free_list(ip_list_t *ipl)
{
	ip_addr_t *ipa;
	
	dbg_printf(5, "Tearing down IP list @ %p\n", ipl);
	while ((ipa = ipl->tqh_first)) {
		TAILQ_REMOVE(ipl, ipa, ipa_entries);
		free(ipa->ipa_address);
		free(ipa);
	}
	return 0;
}


int
ip_build_list(ip_list_t *ipl)
{
	dbg_printf(5, "Build IP address list\n");
	TAILQ_INIT(ipl);
	if (add_ip_addresses(PF_INET6, ipl) < 0) {
		ip_free_list(ipl);
		return -1;
	}
	if (add_ip_addresses(PF_INET, ipl) < 0) {
		ip_free_list(ipl);
		return -1;
	}
	return 0;
}


/**
  Look up the interface name which corresponds to the given hostname and
  return the list of matching attrinfo structures.  We do this by looking
  up all the possible physical and virtual network interfaces on the machine
  and checking the hostname/IP mappings for each active IP address incurred.

  @param nodename	Interface name
  @param ret_ai		Structure pointer to allocate & return.
  @return		-1 on failure or 0 on success.
 */
int
ip_lookup(char *nodename, struct addrinfo **ret_ai)
{
	char ip_name[256];
	struct addrinfo *ai = NULL;
	struct addrinfo *n;
	void *p;
	ip_list_t ipl;
	int ret = -1;

	dbg_printf(5, "Looking for IP matching %s\n", nodename);
	/* Build list of IP addresses configured locally */
	if (ip_build_list(&ipl) < 0)
		return -1;

	/* Get list of addresses for the host-name/ip */
	if (getaddrinfo(nodename, NULL, NULL, &ai) != 0) 
		return -1;
	

	/* Traverse list of addresses for given host-name/ip */
	for (n = ai; n; n = n->ai_next) {
		if (n->ai_family != PF_INET && n->ai_family != PF_INET6)
			continue;

		if (n->ai_family == PF_INET)
			p = &(((struct sockaddr_in *)n->ai_addr)->sin_addr);
		else
			p = &(((struct sockaddr_in6 *)n->ai_addr)->sin6_addr);

		if (!inet_ntop(n->ai_family, p, ip_name,
			       sizeof(ip_name)))
			continue;

		/* Search local interfaces for this IP address */
		if (ip_search(&ipl, ip_name) != 0)
			continue;

		/* Found it */
		ret = 0;
		break;
	}

	/* Clean up */
	if (!ret_ai)
		freeaddrinfo(ai);
	else
		*ret_ai = ai;

	ip_free_list(&ipl);

	return ret;
}