Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / sntp / internet.c
blobee41f216033c322180179e105218da20d7514221
1 /* $NetBSD: internet.c,v 1.3 2003/12/04 17:17:36 drochner Exp $ */
3 /* Copyright (C) 1996 N.M. Maclaren
4 Copyright (C) 1996 The University of Cambridge
6 This includes all of the code needed to handle Internet addressing. It is way
7 outside current POSIX, unfortunately. It should be easy to convert to a system
8 that uses another mechanism. The signal handling is not necessary for its
9 function, but is an attempt to avoid the program hanging when the name server
10 is inaccessible. */
14 #include "header.h"
15 #include "internet.h"
17 #include <netdb.h>
18 #include <arpa/inet.h>
20 #define INTERNET
21 #include "kludges.h"
22 #undef INTERNET
25 /* Used to force dns resolving to ipv4 or ipv6 addresses. */
26 static int pref_family;
28 /* There needs to be some disgusting grobble for handling timeouts, which is
29 identical to the grobble in socket.c. */
31 static jmp_buf jump_buffer;
33 static void jump_handler (int sig) {
34 longjmp(jump_buffer,1);
37 static void clear_alarm (void) {
38 int k;
40 k = errno;
41 alarm(0);
42 errno = 0;
43 if (signal(SIGALRM,SIG_DFL) == SIG_ERR)
44 fatal(1,"unable to reset signal handler",NULL);
45 errno = k;
48 void preferred_family(int fam) {
49 switch(fam) {
50 case PREF_FAM_INET:
51 pref_family = AF_INET;
52 break;
53 #ifdef HAVE_IPV6
54 case PREF_FAM_INET6:
55 pref_family = AF_INET6;
56 break;
57 #endif
58 default:
59 fatal(0,"unable to set the preferred family", NULL);
60 break;
64 #ifdef HAVE_IPV6
66 void find_address (struct sockaddr_storage *address,
67 struct sockaddr_storage *anywhere,
68 int *port, char *hostname, int timespan) {
70 /* Locate the specified NTP server and return its Internet address and port
71 number. */
73 int family, rval;
74 struct addrinfo hints;
75 struct addrinfo *res;
77 res = NULL;
78 memset(address, 0, sizeof(struct sockaddr_storage));
79 memset(anywhere, 0, sizeof(struct sockaddr_storage));
81 if (setjmp(jump_buffer))
82 fatal(0,"unable to set up access to NTP server %s",hostname);
83 errno = 0;
84 if (signal(SIGALRM,jump_handler) == SIG_ERR)
85 fatal(1,"unable to set up signal handler",NULL);
86 alarm((unsigned int)timespan);
88 /* Look up the Internet name or IP number. */
89 memset(&hints, 0, sizeof(hints));
90 hints.ai_socktype = SOCK_DGRAM;
91 hints.ai_family = pref_family;
92 rval = getaddrinfo(hostname, "ntp", &hints, &res);
93 if (rval != 0)
94 fatal(0, "getaddrinfo(hostname, ntp) failed with %s",
95 gai_strerror(rval));
97 /* Now clear the timer and check the result. */
99 clear_alarm();
100 /* There can be more than one address in the list, but for now only
101 use the first. */
102 memcpy(address, res->ai_addr, res->ai_addrlen);
103 family = res->ai_family;
104 freeaddrinfo(res);
106 switch(family) {
107 case AF_INET:
108 hints.ai_family = AF_INET;
109 hints.ai_flags = AI_PASSIVE;
110 rval = getaddrinfo(NULL, "ntp", &hints, &res);
111 if (rval != 0)
112 fatal(0, "getaddrinfo(NULL, ntp) failed with %s",
113 gai_strerror(rval));
114 memcpy(anywhere, res->ai_addr, res->ai_addrlen);
115 freeaddrinfo(res);
116 break;
117 case AF_INET6:
118 hints.ai_family = AF_INET6;
119 hints.ai_flags = AI_PASSIVE;
120 rval = getaddrinfo(NULL, "ntp", &hints, &res);
121 if (rval != 0)
122 fatal(0, "getaddrinfo(NULL, ntp, INET6, AI_PASSIVE) failed with %s",
123 gai_strerror(rval));
124 memcpy(anywhere, res->ai_addr, res->ai_addrlen);
125 freeaddrinfo(res);
126 break;
130 #else
132 void find_address (struct in_addr *address, struct in_addr *anywhere,
133 int *port, char *hostname, int timespan) {
135 /* Locate the specified NTP server and return its Internet address and port
136 number. */
138 unsigned long ipaddr;
139 struct in_addr nowhere[1];
140 struct hostent *host;
141 struct servent *service;
143 /* Set up the reserved Internet addresses, attempting not to assume that
144 addresses are 32 bits. */
146 local_to_address(nowhere,INADDR_LOOPBACK);
147 local_to_address(anywhere,INADDR_ANY);
149 /* Check the address, if any. This assumes that the DNS is reliable, or is at
150 least checked by someone else. But it doesn't assume that it is accessible, so
151 it needs to set up a timeout. */
153 if (hostname == NULL)
154 *address = *anywhere;
155 else {
156 if (setjmp(jump_buffer))
157 fatal(0,"unable to set up access to NTP server %s",hostname);
158 errno = 0;
159 if (signal(SIGALRM,jump_handler) == SIG_ERR)
160 fatal(1,"unable to set up signal handler",NULL);
161 alarm((unsigned int)timespan);
163 /* Look up the Internet name or IP number. */
165 if (! isdigit(hostname[0])) {
166 errno = 0;
167 host = gethostbyname(hostname);
168 } else {
169 if ((ipaddr = inet_addr(hostname)) == (unsigned long)-1)
170 fatal(0,"invalid IP number %s",hostname);
171 network_to_address(address,ipaddr);
172 errno = 0;
173 host = gethostbyaddr((void *)address,sizeof(struct in_addr),
174 AF_INET);
177 /* Now clear the timer and check the result. */
179 clear_alarm();
180 if (host == NULL) fatal(1,"unable to locate IP address/number",NULL);
181 if (host->h_length != sizeof(struct in_addr))
182 fatal(0,"the address does not seem to be an Internet one",NULL);
183 *address = *((struct in_addr **)host->h_addr_list)[0];
184 if (memcmp(address,nowhere,sizeof(struct in_addr)) == 0
185 || memcmp(address,anywhere,sizeof(struct in_addr)) == 0)
186 fatal(0,"reserved IP numbers cannot be used",NULL);
187 if (verbose)
188 fprintf(stderr,
189 "%s: using NTP server %s (%s)\n",
190 argv0,host->h_name,inet_ntoa(*address));
193 /* Find out the port number (usually from /etc/services), and leave it in
194 network format. This is assumed not to be obtained from a network service!
195 Note that a port number is not assumed to be 16 bits. */
197 if ((service = getservbyname("ntp","udp")) != NULL) {
198 *port = service->s_port;
199 if (verbose > 2)
200 fprintf(stderr,"Using port %d for NTP\n",port_to_integer(*port));
201 } else {
202 *port = NTP_PORT;
203 if (verbose)
204 fprintf(stderr,
205 "%s: assuming port %d for NTP - check /etc/services\n",
206 argv0,port_to_integer(*port));
209 #endif