-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathahost.c
94 lines (76 loc) · 1.52 KB
/
ahost.c
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
/*
* ahost.c
*
* Copyright (c) 2002 Dug Song <[email protected]>
*
* $Id: ahost.c,v 1.2 2002/11/22 04:42:34 dugsong Exp $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dnet.h>
#include <event.h>
#include "ares.h"
#include "bag.h"
#include "parse.h"
static void
usage(void)
{
fprintf(stderr, "Usage: ahost [host/net ...]\n");
exit(1);
}
static void
print_dns(uint32_t ip, const char *hostname, void *arg)
{
if (hostname == NULL)
hostname = "ERROR";
else if (*hostname == '\0')
hostname = "unknown";
printf("%s (%s)\n", ip_ntoa(&ip), hostname);
}
static int
query_ip(uint32_t ip, void *arg)
{
return (ares_query(htonl(ip), print_dns, NULL));
}
int
main(int argc, char *argv[])
{
bag_t *bag;
char buf[BUFSIZ];
uint32_t start, end;
int c;
bag = bag_open();
while ((c = getopt(argc, argv, "h?")) != -1) {
switch (c) {
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
for (c = 0; c < argc; c++) {
if (parse_host_range(argv[c], &start, &end) < 0)
errx(1, "invalid host/net: %s", argv[c]);
bag_add_range(bag, ntohl(start), ntohl(end));
}
if (c == 0) {
while (fgets(buf, sizeof(buf), stdin) != NULL) {
strtok(buf, "\r\n");
if (parse_host_range(buf, &start, &end) < 0)
errx(1, "invalid host/net: %s", buf);
bag_add_range(bag, ntohl(start), ntohl(end));
}
}
event_init();
ares_open();
bag_loop(bag, query_ip, NULL);
event_dispatch();
bag = bag_close(bag);
exit(0);
}