summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/lookip/lookip.c
blob: d473c7022e17eedb8e01a80d9fa34dca20d3b612 (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
/*
 * Copyright (C) 2012 Martin Willi
 * Copyright (C) 2012 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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 General Public License
 * for more details.
 */

#include "lookip_msg.h"

#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <arpa/inet.h>

/**
 * Connect to the daemon, return FD
 */
static int make_connection()
{
	union {
		struct sockaddr_un un;
		struct sockaddr_in in;
		struct sockaddr sa;
	} addr;
	int fd, len;

	if (getenv("TCP_PORT"))
	{
		addr.in.sin_family = AF_INET;
		addr.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
		addr.in.sin_port = htons(atoi(getenv("TCP_PORT")));
		len = sizeof(addr.in);
	}
	else
	{
		addr.un.sun_family = AF_UNIX;
		strcpy(addr.un.sun_path, LOOKIP_SOCKET);

		len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.un.sun_path);
	}
	fd = socket(addr.sa.sa_family, SOCK_STREAM, 0);
	if (fd < 0)
	{
		fprintf(stderr, "opening socket failed: %s\n", strerror(errno));
		return -1;
	}
	if (connect(fd, &addr.sa, len) < 0)
	{
		fprintf(stderr, "connecting failed: %s\n", strerror(errno));
		close(fd);
		return -1;
	}
	return fd;
}

static int read_all(int fd, void *buf, size_t len, int flags)
{
	ssize_t ret, done = 0;

	while (done < len)
	{
		ret = recv(fd, buf, len - done, flags);
		if (ret == -1 && errno == EINTR)
		{	/* interrupted, try again */
			continue;
		}
		if (ret == 0)
		{
			return 0;
		}
		if (ret < 0)
		{
			return -1;
		}
		done += ret;
		buf += ret;
	}
	return len;
}

static int write_all(int fd, void *buf, size_t len)
{
	ssize_t ret, done = 0;

	while (done < len)
	{
		ret = write(fd, buf, len - done);
		if (ret == -1 && errno == EINTR)
		{	/* interrupted, try again */
			continue;
		}
		if (ret < 0)
		{
			return -1;
		}
		done += ret;
		buf += ret;
	}
	return len;
}

/**
 * Send a request message
 */
static int send_request(int fd, int type, char *vip)
{
	lookip_request_t req = {
		.type = htonl(type),
	};

	if (vip)
	{
		snprintf(req.vip, sizeof(req.vip), "%s", vip);
	}
	if (write_all(fd, &req, sizeof(req)) != sizeof(req))
	{
		fprintf(stderr, "writing to socket failed: %s\n", strerror(errno));
		return 2;
	}
	return 0;
}

/**
 * Receive entries from fd. If block is != 0, the call blocks until closed
 */
static int receive(int fd, int block, int loop)
{
	lookip_response_t resp;
	char *label, name[32];
	int res;

	do
	{
		res = read_all(fd, &resp, sizeof(resp), block ? 0 : MSG_DONTWAIT);
		if (res == 0)
		{	/* closed by server */
			return 0;
		}
		if (res != sizeof(resp))
		{
			if (!block && (errno == EAGAIN || errno == EWOULDBLOCK))
			{	/* call would block, but we don't */
				return 0;
			}
			fprintf(stderr, "reading from socket failed: %s\n", strerror(errno));
			return 1;
		}
		switch (ntohl(resp.type))
		{
			case LOOKIP_ENTRY:
				label = "lookup:";
				break;
			case LOOKIP_NOT_FOUND:
				label = "not found:";
				break;
			case LOOKIP_NOTIFY_UP:
				label = "up:";
				break;
			case LOOKIP_NOTIFY_DOWN:
				label = "down:";
				break;
			default:
				fprintf(stderr, "received invalid message type: %d\n", resp.type);
				return 1;
		}
		resp.vip[sizeof(resp.vip) - 1] = '\0';
		resp.ip[sizeof(resp.ip) - 1] = '\0';
		resp.id[sizeof(resp.id) - 1] = '\0';
		resp.name[sizeof(resp.name) - 1] = '\0';

		snprintf(name, sizeof(name), "%s[%u]", resp.name, ntohl(resp.unique_id));
		printf("%-12s %16s %16s %20s %s\n",
			   label, resp.vip, resp.ip, name, resp.id);
	}
	while (loop);

	return 0;
}

/**
 * Interactive IP lookup shell
 */
static int interactive(int fd)
{
	printf("Enter IP address or 'quit'\n");

	while (1)
	{
		char line[64], *pos;
		int res;

		printf("> ");
		fflush(stdout);

		if (fgets(line, sizeof(line), stdin))
		{
			pos = strchr(line, '\n');
			if (pos)
			{
				*pos = '\0';
			}
			if (strlen(line) == 0)
			{
				continue;
			}
			if (strcmp(line, "quit") == 0)
			{
				return send_request(fd, LOOKIP_END, NULL);
			}
			res = send_request(fd, LOOKIP_LOOKUP, line);
			if (res != 0)
			{
				return res;
			}
			res = receive(fd, 1, 0);
			if (res != 0)
			{
				return res;
			}
		}
	}
}

/**
 * Print usage information
 */
static void usage(char *cmd)
{
	fprintf(stderr, "Usage:\n");
	fprintf(stderr, "  %s --help\n", cmd);
	fprintf(stderr, "  %s --dump\n", cmd);
	fprintf(stderr, "  %s --lookup <IP>\n", cmd);
	fprintf(stderr, "  %s --listen-up\n", cmd);
	fprintf(stderr, "  %s --listen-down\n", cmd);
	fprintf(stderr, "Any combination of options is allowed.\n");
}

int main(int argc, char *argv[])
{
	int fd, res = 0, end = 0;
	struct option long_opts[] = {
		{ "help", no_argument, NULL, 'h' },
		{ "dump", no_argument, NULL, 'd' },
		{ "lookup", required_argument, NULL, 'l' },
		{ "listen-up", no_argument, NULL, 'u' },
		{ "listen-down", no_argument, NULL, 'c' },
		{ 0,0,0,0 }
	};

	fd = make_connection();
	if (fd == -1)
	{
		return 1;
	}

	if (argc == 1)
	{
		res = interactive(fd);
		close(fd);
		return res;
	}

	while (res == 0)
	{
		switch (getopt_long(argc, argv, "", long_opts, NULL))
		{
			case EOF:
				end = 1;
				break;
			case 'h':
				usage(argv[0]);
				break;
			case 'd':
				res = send_request(fd, LOOKIP_DUMP, NULL);
				break;
			case 'l':
				res = send_request(fd, LOOKIP_LOOKUP, optarg);
				break;
			case 'u':
				res = send_request(fd, LOOKIP_REGISTER_UP, NULL);
				break;
			case 'c':
				res = send_request(fd, LOOKIP_REGISTER_DOWN, NULL);
				break;
			default:
				usage(argv[0]);
				res = 1;
				break;
		}
		if (end)
		{
			break;
		}
		if (res == 0)
		{	/* read all currently available results */
			res = receive(fd, 0, 1);
		}
	}
	if (res == 0)
	{
		/* send close message */
		send_request(fd, LOOKIP_END, NULL);
		/* read until socket gets closed */
		res = receive(fd, 1, 1);
	}
	close(fd);

	return res;
}