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
18 #include <arpa/inet.h>
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) {
43 if (signal(SIGALRM
,SIG_DFL
) == SIG_ERR
)
44 fatal(1,"unable to reset signal handler",NULL
);
48 void preferred_family(int fam
) {
51 pref_family
= AF_INET
;
55 pref_family
= AF_INET6
;
59 fatal(0,"unable to set the preferred family", NULL
);
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
74 struct addrinfo hints
;
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
);
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
);
94 fatal(0, "getaddrinfo(hostname, ntp) failed with %s",
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(NULL, ntp) failed with %s",
114 memcpy(anywhere
, res
->ai_addr
, res
->ai_addrlen
);
118 hints
.ai_family
= AF_INET6
;
119 hints
.ai_flags
= AI_PASSIVE
;
120 rval
= getaddrinfo(NULL
, "ntp", &hints
, &res
);
122 fatal(0, "getaddrinfo(NULL, ntp, INET6, AI_PASSIVE) failed with %s",
124 memcpy(anywhere
, res
->ai_addr
, res
->ai_addrlen
);
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
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
;
156 if (setjmp(jump_buffer
))
157 fatal(0,"unable to set up access to NTP server %s",hostname
);
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])) {
167 host
= gethostbyname(hostname
);
169 if ((ipaddr
= inet_addr(hostname
)) == (unsigned long)-1)
170 fatal(0,"invalid IP number %s",hostname
);
171 network_to_address(address
,ipaddr
);
173 host
= gethostbyaddr((void *)address
,sizeof(struct in_addr
),
177 /* Now clear the timer and check the result. */
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
);
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
;
200 fprintf(stderr
,"Using port %d for NTP\n",port_to_integer(*port
));
205 "%s: assuming port %d for NTP - check /etc/services\n",
206 argv0
,port_to_integer(*port
));