summaryrefslogtreecommitdiffstats
path: root/arlib/sample.c
blob: 83db6a842a8f79a632b3d83878d73a1806310e86 (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
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include "arlib.h"

#ifndef	lint
static	char	sccsid[] = "@(#)sample.c	1.1 12/21/92 (C)1992 Darren Reed. ASYNC DNS";
#endif

char	line[512];

int	lookup = 0, seq = 0;
long	expire = 0;

main()
{
	struct	in_addr adr;
	struct	timeval	tv2;
	fd_set	rd;
	long	now;
	char	*s;
	int	afd, nfd, pid = getpid(), del;

	afd = ar_init(ARES_INITLIST|ARES_CALLINIT|ARES_INITSOCK);

	(void)printf("afd = %d pid = %d\n",afd, pid);

	while (1)
	{
		(void)printf("Host =>");
		(void)fflush(stdout);
		*line = '\0';
		FD_ZERO(&rd);
		FD_SET(0,&rd);
		FD_SET(afd,&rd);
		now = time(NULL);
		if (expire >= now)
		    {
			tv2.tv_usec = 0;
			tv2.tv_sec = expire - now;
			nfd = select(FD_SETSIZE, &rd, NULL, NULL, &tv2);
		    }
		else
			nfd = select(FD_SETSIZE, &rd, NULL, NULL, NULL);

		if (FD_ISSET(0, &rd))
		{
			if (!fgets(line, sizeof(line) - 1, stdin))
				exit(0);
			if (s = index(line, '\n'))
				*s = '\0';
		}

		if (isalpha(*line))
		{
			(void)printf("Asking about [%s] #%d.\n",line, ++seq);
			(void)ar_gethostbyname(line, (char *)&seq,
					       sizeof(seq));
			lookup++;
		}
		else if (isdigit(*line))
		{
			(void)printf("Asking about IP#[%s] #%d.\n",
				line, ++seq);
			adr.s_addr = inet_addr(line);
			(void)ar_gethostbyaddr(&adr, (char *)&seq,
					       sizeof(seq));
			lookup++;
		}
		if (lookup)
			(void)printf("Waiting for answer:\n");
		if (FD_ISSET(afd, &rd))
			(void)waitonlookup(afd);
		del = 0;
		expire = ar_timeout(time(NULL), &del, sizeof(del));
		if (del)
		{
			(void)fprintf(stderr,"#%d failed\n", del);
			lookup--;
		}
	}
}

printhostent(hp)
struct hostent *hp;
{
	struct in_addr ip;
	int i;

	(void)printf("hname = %s\n", hp->h_name);
	for (i = 0; hp->h_aliases[i]; i++)
		(void)printf("alias %d = %s\n", i+1, hp->h_aliases[i]);
	for (i = 0; hp->h_addr_list[i]; i++)
	{
		bcopy(hp->h_addr_list[i], (char *)&ip, sizeof(ip));
		(void)printf("IP# %d = %s\n", i+1, inet_ntoa(ip));
	}
}

int	waitonlookup(afd)
int	afd;
{
	struct	timeval delay;
	struct	hostent	*hp;
	fd_set	rd;
	long	now;
	int	nfd, del;

waitloop:
	FD_ZERO(&rd);
	now = time(NULL);
	if (expire >= now)
		delay.tv_sec = expire - now;
	else
		delay.tv_sec = 1;
	delay.tv_usec = 0;
	FD_SET(afd, &rd);
	FD_SET(0, &rd);

	nfd = select(FD_SETSIZE, &rd, 0, 0, &delay);
	if (nfd == 0)
		return 0;
	else if (FD_ISSET(afd, &rd))
	{
		del = 0;
		hp = ar_answer(&del, sizeof(del));

		(void)printf("hp=%x seq=%d\n",hp,del);
		if (hp)
		    {
			(void)printhostent(hp);
			if (!--lookup)
				return 1;
		    }
	}
	if (FD_ISSET(0, &rd))
		return 2;
	return 0;
}
OpenPOWER on IntegriCloud