1 /* Copyright (C) 1996 N.M. Maclaren
2 Copyright (C) 1996 The University of Cambridge
4 This includes all of the code needed to handle Internet addressing. It is way
5 outside current POSIX, unfortunately. It should be easy to convert to a system
6 that uses another mechanism. The signal handling is not necessary for its
7 function, but is an attempt to avoid the program hanging when the name server
16 #include <arpa/inet.h>
23 /* Used to force dns resolving to ipv4 or ipv6 addresses. */
24 static int pref_family;
26 /* There needs to be some disgusting grobble for handling timeouts, which is
27 identical to the grobble in socket.c. */
29 static jmp_buf jump_buffer;
31 static void jump_handler (int sig) {
32 longjmp(jump_buffer,1);
35 static void clear_alarm (void) {
41 if (signal(SIGALRM,SIG_DFL) == SIG_ERR)
42 fatal(1,"unable to reset signal handler",NULL);
46 void preferred_family(int fam) {
49 pref_family = AF_INET;
53 pref_family = AF_INET6;
57 fatal(0,"unable to set the preferred family", NULL);
64 void find_address (struct sockaddr_storage *address,
65 struct sockaddr_storage *anywhere, struct sockaddr_storage *everywhere,
66 int *port, char *hostname, int timespan) {
68 /* Locate the specified NTP server and return its Internet address and port
72 struct addrinfo hints;
74 struct sockaddr_in *sin;
75 struct sockaddr_in6 *sin6;
78 memset(address, 0, sizeof(struct sockaddr_storage));
79 memset(anywhere, 0, sizeof(struct sockaddr_storage));
80 memset(everywhere, 0, sizeof(struct sockaddr_storage));
82 if (setjmp(jump_buffer))
83 fatal(0,"unable to set up access to NTP server %s",hostname);
85 if (signal(SIGALRM,jump_handler) == SIG_ERR)
86 fatal(1,"unable to set up signal handler",NULL);
87 alarm((unsigned int)timespan);
89 /* Look up the Internet name or IP number. */
90 memset(&hints, 0, sizeof(hints));
91 hints.ai_socktype = SOCK_DGRAM;
92 hints.ai_family = pref_family;
93 rval = getaddrinfo(hostname, "ntp", &hints, &res);
95 fatal(0, "getaddrinfo failed with %s", gai_strerror(rval));
97 /* Now clear the timer and check the result. */
100 /* There can be more than one address in the list, but for now only
102 memcpy(address, res->ai_addr, res->ai_addrlen);
103 family = res->ai_family;
108 hints.ai_family = AF_INET;
109 hints.ai_flags = AI_PASSIVE;
110 rval = getaddrinfo(NULL, "ntp", &hints, &res);
112 fatal(0, "getaddrinfo failed with %s", gai_strerror(rval));
113 memcpy(anywhere, res->ai_addr, res->ai_addrlen);
115 rval = getaddrinfo("255.255.255.255", "ntp", &hints, &res);
117 fatal(0, "getaddrinfo failed with %s", gai_strerror(rval));
118 memcpy(everywhere, res->ai_addr, res->ai_addrlen);
122 hints.ai_family = AF_INET6;
123 hints.ai_flags = AI_PASSIVE;
124 rval = getaddrinfo(NULL, "ntp", &hints, &res);
126 fatal(0, "getaddrinfo failed with %s", gai_strerror(rval));
127 memcpy(anywhere, res->ai_addr, res->ai_addrlen);
129 /* IPv6 do not have broadcast, give it loopback. */
131 rval = getaddrinfo(NULL, "ntp", &hints, &res);
133 fatal(0, "getaddrinfo failed with %s", gai_strerror(rval));
134 memcpy(everywhere, res->ai_addr, res->ai_addrlen);
142 void find_address (struct in_addr *address, struct in_addr *anywhere,
143 struct in_addr *everywhere, int *port, char *hostname, int timespan) {
145 /* Locate the specified NTP server and return its Internet address and port
148 unsigned long ipaddr;
149 struct in_addr nowhere[1];
150 struct hostent *host;
151 struct servent *service;
153 /* Set up the reserved Internet addresses, attempting not to assume that
154 addresses are 32 bits. */
156 local_to_address(nowhere,INADDR_LOOPBACK);
157 local_to_address(anywhere,INADDR_ANY);
158 local_to_address(everywhere,INADDR_BROADCAST);
160 /* Check the address, if any. This assumes that the DNS is reliable, or is at
161 least checked by someone else. But it doesn't assume that it is accessible, so
162 it needs to set up a timeout. */
164 if (hostname == NULL)
165 *address = *anywhere;
167 if (setjmp(jump_buffer))
168 fatal(0,"unable to set up access to NTP server %s",hostname);
170 if (signal(SIGALRM,jump_handler) == SIG_ERR)
171 fatal(1,"unable to set up signal handler",NULL);
172 alarm((unsigned int)timespan);
174 /* Look up the Internet name or IP number. */
176 if (! isdigit(hostname[0])) {
178 host = gethostbyname(hostname);
180 if ((ipaddr = inet_addr(hostname)) == (unsigned long)-1)
181 fatal(0,"invalid IP number %s",hostname);
182 network_to_address(address,ipaddr);
184 host = gethostbyaddr((void *)address,sizeof(struct in_addr),
188 /* Now clear the timer and check the result. */
191 if (host == NULL) fatal(1,"unable to locate IP address/number",NULL);
192 if (host->h_length != sizeof(struct in_addr))
193 fatal(0,"the address does not seem to be an Internet one",NULL);
194 *address = *((struct in_addr **)host->h_addr_list)[0];
195 if (memcmp(address,nowhere,sizeof(struct in_addr)) == 0 ||
196 memcmp(address,anywhere,sizeof(struct in_addr)) == 0 ||
197 memcmp(address,everywhere,sizeof(struct in_addr)) == 0)
198 fatal(0,"reserved IP numbers cannot be used",NULL);
201 "%s: using NTP server %s (%s)\n",
202 argv0,host->h_name,inet_ntoa(*address));
205 /* Find out the port number (usually from /etc/services), and leave it in
206 network format. This is assumed not to be obtained from a network service!
207 Note that a port number is not assumed to be 16 bits. */
209 if ((service = getservbyname("ntp","udp")) != NULL) {
210 *port = service->s_port;
212 fprintf(stderr,"Using port %d for NTP\n",port_to_integer(*port));
217 "%s: assuming port %d for NTP - check /etc/services\n",
218 argv0,port_to_integer(*port));