PageRenderTime 41ms CodeModel.GetById 10ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/ntp/arlib/sample.c

https://bitbucket.org/freebsd/freebsd-head/
C | 143 lines | 128 code | 15 blank | 0 comment | 21 complexity | 3dab0d421119b76d5afeafa72aa32d95 MD5 | raw file
  1#include <stdio.h>
  2#include <strings.h>
  3#include <errno.h>
  4#include <sys/types.h>
  5#include <sys/time.h>
  6#include <netinet/in.h>
  7#include <netdb.h>
  8#include "arlib.h"
  9
 10#ifndef	lint
 11static	char	sccsid[] = "@(#)sample.c	1.1 12/21/92 (C)1992 Darren Reed. ASYNC DNS";
 12#endif
 13
 14char	line[512];
 15
 16int	lookup = 0, seq = 0;
 17long	expire = 0;
 18
 19main()
 20{
 21	struct	in_addr adr;
 22	struct	timeval	tv2;
 23	fd_set	rd;
 24	long	now;
 25	char	*s;
 26	int	afd, nfd, pid = getpid(), del;
 27
 28	afd = ar_init(ARES_INITLIST|ARES_CALLINIT|ARES_INITSOCK);
 29
 30	(void)printf("afd = %d pid = %d\n",afd, pid);
 31
 32	while (1)
 33	{
 34		(void)printf("Host =>");
 35		(void)fflush(stdout);
 36		*line = '\0';
 37		FD_ZERO(&rd);
 38		FD_SET(0,&rd);
 39		FD_SET(afd,&rd);
 40		now = time(NULL);
 41		if (expire >= now)
 42		    {
 43			tv2.tv_usec = 0;
 44			tv2.tv_sec = expire - now;
 45			nfd = select(FD_SETSIZE, &rd, NULL, NULL, &tv2);
 46		    }
 47		else
 48			nfd = select(FD_SETSIZE, &rd, NULL, NULL, NULL);
 49
 50		if (FD_ISSET(0, &rd))
 51		{
 52			if (!fgets(line, sizeof(line) - 1, stdin))
 53				exit(0);
 54			if (s = index(line, '\n'))
 55				*s = '\0';
 56		}
 57
 58		if (isalpha(*line))
 59		{
 60			(void)printf("Asking about [%s] #%d.\n",line, ++seq);
 61			(void)ar_gethostbyname(line, (char *)&seq,
 62					       sizeof(seq));
 63			lookup++;
 64		}
 65		else if (isdigit(*line))
 66		{
 67			(void)printf("Asking about IP#[%s] #%d.\n",
 68				line, ++seq);
 69			adr.s_addr = inet_addr(line);
 70			(void)ar_gethostbyaddr(&adr, (char *)&seq,
 71					       sizeof(seq));
 72			lookup++;
 73		}
 74		if (lookup)
 75			(void)printf("Waiting for answer:\n");
 76		if (FD_ISSET(afd, &rd))
 77			(void)waitonlookup(afd);
 78		del = 0;
 79		expire = ar_timeout(time(NULL), &del, sizeof(del));
 80		if (del)
 81		{
 82			(void)fprintf(stderr,"#%d failed\n", del);
 83			lookup--;
 84		}
 85	}
 86}
 87
 88printhostent(hp)
 89struct hostent *hp;
 90{
 91	struct in_addr ip;
 92	int i;
 93
 94	(void)printf("hname = %s\n", hp->h_name);
 95	for (i = 0; hp->h_aliases[i]; i++)
 96		(void)printf("alias %d = %s\n", i+1, hp->h_aliases[i]);
 97	for (i = 0; hp->h_addr_list[i]; i++)
 98	{
 99		bcopy(hp->h_addr_list[i], (char *)&ip, sizeof(ip));
100		(void)printf("IP# %d = %s\n", i+1, inet_ntoa(ip));
101	}
102}
103
104int	waitonlookup(afd)
105int	afd;
106{
107	struct	timeval delay;
108	struct	hostent	*hp;
109	fd_set	rd;
110	long	now;
111	int	nfd, del;
112
113waitloop:
114	FD_ZERO(&rd);
115	now = time(NULL);
116	if (expire >= now)
117		delay.tv_sec = expire - now;
118	else
119		delay.tv_sec = 1;
120	delay.tv_usec = 0;
121	FD_SET(afd, &rd);
122	FD_SET(0, &rd);
123
124	nfd = select(FD_SETSIZE, &rd, 0, 0, &delay);
125	if (nfd == 0)
126		return 0;
127	else if (FD_ISSET(afd, &rd))
128	{
129		del = 0;
130		hp = ar_answer(&del, sizeof(del));
131
132		(void)printf("hp=%x seq=%d\n",hp,del);
133		if (hp)
134		    {
135			(void)printhostent(hp);
136			if (!--lookup)
137				return 1;
138		    }
139	}
140	if (FD_ISSET(0, &rd))
141		return 2;
142	return 0;
143}