~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to arlib/sample.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
11
static  char    sccsid[] = "@(#)sample.c        1.1 12/21/92 (C)1992 Darren Reed. ASYNC DNS";
 
12
#endif
 
13
 
 
14
char    line[512];
 
15
 
 
16
int     lookup = 0, seq = 0;
 
17
long    expire = 0;
 
18
 
 
19
main()
 
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
 
 
88
printhostent(hp)
 
89
struct 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
 
 
104
int     waitonlookup(afd)
 
105
int     afd;
 
106
{
 
107
        struct  timeval delay;
 
108
        struct  hostent *hp;
 
109
        fd_set  rd;
 
110
        long    now;
 
111
        int     nfd, del;
 
112
 
 
113
waitloop:
 
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
}