summaryrefslogtreecommitdiff
path: root/src/resolve.c
blob: 880b67867f1340ff192e192c71da4e610bfc8c70 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
// AUTHOR
// N. Nielsen
//
// VERSION
// 0.2
//
// LICENSE
// This software is in the public domain.
//
// The software is provided "as is", without warranty of any kind,
// express or implied, including but not limited to the warranties
// of merchantability, fitness for a particular purpose, and
// noninfringement. In no event shall the author(s) be liable for any
// claim, damages, or other liability, whether in an action of
// contract, tort, or otherwise, arising from, out of, or in connection
// with the software or the use or other dealings in the software.
//
// SUPPORT
// Send bug reports to: <nielsen@memberwebs.com>
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>

#include <netinet/in.h>
#include <netinet/if_ether.h>

#include <arpa/nameser.h>
#include <arpa/inet.h>

#include <resolv.h>

#if HAVE_CONFIG_H
#include <config.h>
#endif

/* The main processing functions */
static void processFile(FILE* fIn, FILE* fOut);
static void processReverse(const char* address, FILE* fOut);
static void processNone(const char* address, FILE* fOut);
static void processAddress(const char* address, FILE* fOut);
static void processHost(const char* address, FILE* fOut);
static void processMAC(const char* address, FILE* fOut);

static void atExit();
static void usage();

/* Are we in reverse resolve mode? */
int g_doReverse = 0;
int g_doVerbose = 0;

/* These are globals for efficiency */
u_char* g_packetBuf = NULL;
char* g_domainBuf = NULL;
u_char* g_routeBuf = NULL;
size_t g_routeLen = 0;


int main(int argc, char** argv)
{
	int useFiles = 0;
	int ch;

	/* Register clean up function */
	atexit(atExit);

	/* Parse command line options here */
	while((ch = getopt(argc, argv, "rfv")) != -1)
	{
		switch(ch)
		{
		/* Treat arguments as files */
		case 'f':
			useFiles = 1;
			break;

		case 'v':
			g_doVerbose = 1;
			break;

		/* Do reverse resolve */
		case 'r':
			g_doReverse = 1;
			break;

		case '?':
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	if(argc)
	{
		while(argc > 0)
		{
			/* Process the argument as a file full of addresses */
			if(useFiles)
			{
				FILE* file = fopen(argv[0], "r");
				if(file == NULL)
					err(1, "can't open file: %s", argv[0]);

				processFile(file, stdout);

				fclose(file);
			}

			/* Process each argument as an address */
			else
			{
				processAddress(argv[0], stdout);
			}

			argc--;
			argv++;
		}
	}
	else
	{
		/* No args, just process stdin etc... */
		processFile(stdin, stdout);
	}

	exit(0);
}


/**
 * Eats comments and spaces
 */
static void eatSpace(FILE* file)
{
	char ch;

	do
	{
		ch = getc(file);

		if(ch == '#')
		{
			while(ch != '\n')
				ch = getc(file);
		}
	}
	while(isspace(ch));

	ungetc(ch, file);
}


/**
 * Read addresses from a file and process
 */
static void processFile(FILE* fIn, FILE* fOut)
{
	/* Allocate memory, cache for efficiency */
	if(g_domainBuf == NULL)
	{
		g_domainBuf = (char*)malloc(sizeof(char) * MAXDNAME);
		if(g_domainBuf == NULL)
			errx(1, "out of memory.");
	}


	/* And process rest of file */
	eatSpace(fIn);

	while(!feof(fIn) && !ferror(fIn))
	{
		char ch;
		int i = 0;

		while(1)
		{
			ch = fgetc(fIn);

			if(isspace(ch))
				break;

			if(i < MAXDNAME)
				g_domainBuf[i] = ch;

			i++;
		}

		g_domainBuf[i] = 0;

		processAddress(g_domainBuf, fOut);

		eatSpace(fIn);
	}

	/* TODO: Should this be a warning or quit? */
	if(ferror(fIn))
		err(1, "error reading file");
}


/**
 * Main address processing function
 */
static void processAddress(const char* address, FILE* fOut)
{
	if(g_doVerbose)
		fprintf(stderr, "resolve: resolving %s\n", address);

	/* If it's an IP then .... */
	if(strlen(address) == strspn(address, "0123456789."))
	{
		if(g_doReverse)
			processReverse(address, fOut);

		else
			processNone(address, fOut);
	}

	/* If it's a MAC address then .... */
	else if(strlen(address) == strspn(address, "0123456789abcdefABCDEF-:"))
	{
		if(g_doReverse)
			processNone(address, fOut);
		else
			processMAC(address, fOut);
	}

	/* If it's a net block then .... */
	if(strlen(address) == strspn(address, "0123456789./"))
	{
		if(g_doReverse)
			warnx("can't reverse resolve net block: %s", address);
		else
			processNone(address, fOut);
	}

	/* Otherwise it should be a domain name. try to resolve it. */
	else
	{
		if(g_doReverse)
			processNone(address, fOut);
		else
			processHost(address, fOut);
	}
}


/**
 * Used if address is aleady (hopefully) in correct form
 */
static void processNone(const char* address, FILE* fOut)
{
	fprintf(fOut, address);
	fputc('\n', fOut);
}


/**
 * Forward resolve a host name into an IP
 */
static void processHost(const char* address, FILE* fOut)
{
	int ret;
	ns_msg handle;
	int rrnum;
	ns_rr rr;

	/* Allocation cached for efficiency */
	if(g_packetBuf == NULL)
	{
		g_packetBuf = (u_char*)malloc(sizeof(u_char) * PACKETSZ);
		if(g_packetBuf == NULL)
			errx(1, "out of memory.");
	}

	/* Do a DNS lookup */
	ret = res_search(address, C_IN, ns_t_a, g_packetBuf, PACKETSZ);
	if(ret == -1)
		warnx("couldn't resolve: %s\n", address);
	else
	{
		/* Initialize a handle to this response. */
		if(ns_initparse(g_packetBuf, ret, &handle) < 0)
			warn("couldn't parse dns response: %s\n", strerror(errno));
		else
		{
			/* Parse the packet */
			for(rrnum = 0; rrnum < ns_msg_count(handle, ns_s_an); rrnum++)
			{
				if(!ns_parserr(&handle, ns_s_an, rrnum, &rr))
				{
					if(ns_rr_type(rr) == ns_t_a &&	/* It's a host adress */
					   ns_rr_rdlen(rr) == 0x04)		/* And has an IP */
					{
						struct in_addr addr;
						memcpy(&(addr.s_addr), ns_rr_rdata(rr), 0x04);
						fprintf(fOut, inet_ntoa(addr));
						fputc('\n', fOut);
					}
				}
			}
		}
	}
}


/**
 * Reverse resolve an IP into a host name
 */
static void processReverse(const char* address, FILE* fOut)
{
	int ret;
	ns_msg handle;
	int rrnum;
	ns_rr rr;
	u_int32_t ha;
	char name[NS_MAXDNAME];
	struct in_addr addr;

	/* Is it a valid IP address first of all */
	if(!inet_aton(address, &addr))
	{
		warnx("invalid ip address: %s\n", address);
		return;
	}

	/* Format query */
	ha = ntohl(addr.s_addr);

	sprintf(name, "%u.%u.%u.%u.IN-ADDR.ARPA.",
			(ha) & 0xff,
			(ha >> 8) & 0xff,
			(ha >> 16) & 0xff,
			(ha >> 24) & 0xff);

	/* Allocation cached for efficiency */
	if(g_packetBuf == NULL)
	{
		g_packetBuf = (u_char*)malloc(sizeof(u_char) * PACKETSZ);
		if(g_packetBuf == NULL)
			errx("out of memory.");
	}

	/* Do that lookup */
	ret = res_search(name, C_IN, ns_t_ptr, g_packetBuf, PACKETSZ);
	if(ret == -1)
		warnx("couldn't resolve: %s\n", name);
	else
	{
		/* Initialize a handle to this response. */
		if(ns_initparse(g_packetBuf, ret, &handle) < 0)
			warn("couldn't parse dns response");
		else
		{
			/* Parse the packet */
			for(rrnum = 0; rrnum < ns_msg_count(handle, ns_s_an); rrnum++)
			{
				if(!ns_parserr(&handle, ns_s_an, rrnum, &rr))
				{
					if(ns_rr_type(rr) == ns_t_ptr) /* It's a domain pointer */
					{
			            /* Expand the host's name */
						if (ns_name_uncompress(
							ns_msg_base(handle),/* Start of the packet   */
							ns_msg_end(handle), /* End of the packet     */
							ns_rr_rdata(rr),    /* Position in the packet*/
							name,				/* Result                */
							MAXDNAME)           /* Size of nsList buffer */
                                  < 0) /* Negative: error       */
						{
							warn("couldn't parse dns response");
						}
						else
						{
							fprintf(fOut, "%s\n", name);
						}
					}
				}
			}
		}
	}
}


/**
 * Parse components of a MAC address
 */
static int parseMAC(const char* address, u_char* mac)
{
	int i;

	for(i = 0; i < 6; i++)
	{
		char* end;
		int part;

		part = strtol(address, &end, 16);

		if(i != 5 && *end != ':' && *end != '-')
			return 0;

		if(part < 0 || part > 255)
			return 0;

		mac[i] = (u_char)part;

		address = end + 1;
	}

	return 1;
}


#define ROUNDUP(a) \
	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))

/**
 * Reverse resolve MAC address if in the ARP table
 */
static void processMAC(const char* address, FILE* fOut)
{
	u_char* next;
	struct rt_msghdr* rtm;
	struct sockaddr_inarp* sin;
	struct sockaddr_dl* sdl;
	u_char mac[6];

	/* Load the kernel routing table */
	if(g_routeBuf == NULL)
	{
		int mib[6];

		mib[0] = CTL_NET;
		mib[1] = PF_ROUTE;
		mib[2] = 0;
		mib[3] = AF_INET;
		mib[4] = NET_RT_FLAGS;
		mib[5] = RTF_LLINFO;

		if(sysctl(mib, 6, NULL, &g_routeLen, NULL, 0) < 0)
			errx(1, "can't load routing table");

		g_routeBuf = malloc(g_routeLen);

		if(g_routeBuf == NULL)
			errx(1, "out of memory");

		if(sysctl(mib, 6, g_routeBuf, &g_routeLen, NULL, 0) < 0)
			errx(1, "can't load routing table");
	}

	/* Get MAC bytes */
	if(!parseMAC(address, mac))
	{
		warn("invalid MAC address: %s", address);
		return;
	}

	/* Look for it in the routing table */
	for(next = g_routeBuf; next < (g_routeBuf + g_routeLen); next += rtm->rtm_msglen)
	{
		rtm = (struct rt_msghdr*)next;
		sin = (struct sockaddr_inarp*)(rtm + 1);
		(char*)sdl = (char*)sin + ROUNDUP(sin->sin_len);

		if(!memcmp(LLADDR(sdl), mac, sizeof(mac)))
		{
			fprintf(fOut, inet_ntoa(sin->sin_addr));
			fputc('\n', fOut);
			return;
		}
	}

	/* oops */
	warnx("unlisted MAC address: %s", address);
}



static void usage()
{
	fprintf(stderr, "usage: resolve [-rv] -f file ...\n");
	fprintf(stderr, "       resolve [-rv] address ...\n");
	exit(2);
}


/*
 * Clean up function.
 */
static void atExit()
{
	if(g_packetBuf != NULL)
		free(g_packetBuf);
	if(g_domainBuf != NULL)
		free(g_domainBuf);
}