OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / msntp / internet.c
1 /*  Copyright (C) 1996 N.M. Maclaren
2     Copyright (C) 1996 The University of Cambridge
3
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
8 is inaccessible. */
9
10
11
12 #include "header.h"
13 #include "internet.h"
14
15 #include <netdb.h>
16 #include <arpa/inet.h>
17
18 #define INTERNET
19 #include "kludges.h"
20 #undef INTERNET
21
22
23
24 /* There needs to be some disgusting grobble for handling timeouts, which is
25 identical to the grobble in socket.c. */
26
27 static jmp_buf jump_buffer;
28
29 static void jump_handler (int sig) {
30     longjmp(jump_buffer,1);
31 }
32
33 static void clear_alarm (void) {
34     int k;
35
36     k = errno;
37     alarm(0);
38     errno = 0;
39     if (signal(SIGALRM,SIG_DFL) == SIG_ERR)
40         fatal(1,"unable to reset signal handler",NULL);
41     errno = k;
42 }
43
44
45
46 void find_address (struct in_addr *address, struct in_addr *anywhere,
47     struct in_addr *everywhere, int *port, char *hostname, int timespan) {
48
49 /* Locate the specified NTP server and return its Internet address and port 
50 number. */
51
52     struct in_addr nowhere[1];
53     struct hostent *host;
54     struct servent *service;
55
56 /* Set up the reserved Internet addresses, attempting not to assume that
57 addresses are 32 bits. */
58
59     local_to_address(nowhere,INADDR_LOOPBACK);
60     local_to_address(anywhere,INADDR_ANY);
61     local_to_address(everywhere,INADDR_BROADCAST);
62
63 /* Check the address, if any.  This assumes that the DNS is reliable, or is at
64 least checked by someone else.  But it doesn't assume that it is accessible, so
65 it needs to set up a timeout. */
66
67     if (hostname == NULL)
68         *address = *anywhere;
69     else {
70         if (setjmp(jump_buffer))
71             fatal(0,"unable to set up access to NTP server %s",hostname);
72         errno = 0;
73         if (signal(SIGALRM,jump_handler) == SIG_ERR)
74             fatal(1,"unable to set up signal handler",NULL);
75         alarm((unsigned int)timespan);
76
77 /* Look up the Internet name or IP number. */
78
79         host = gethostbyname(hostname);
80
81 /* Now clear the timer and check the result. */
82
83         clear_alarm();
84         if (host == NULL) fatal(1,"unable to locate IP address/number: %s", hostname);
85         if (host->h_length != sizeof(struct in_addr))
86             fatal(0,"the address does not seem to be an Internet one: %s", hostname);
87         *address = *((struct in_addr **)host->h_addr_list)[0];
88         if (memcmp(address,nowhere,sizeof(struct in_addr)) == 0 ||
89                 memcmp(address,anywhere,sizeof(struct in_addr)) == 0 ||
90                 memcmp(address,everywhere,sizeof(struct in_addr)) == 0)
91             fatal(0,"reserved IP numbers cannot be used",NULL);
92         if (verbose)
93             fprintf(stderr,
94                 "%s: using NTP server %s (%s)\n",
95                 argv0,host->h_name,inet_ntoa(*address));
96     }
97
98 /* Find out the port number (usually from /etc/services), and leave it in 
99 network format.  This is assumed not to be obtained from a network service!
100 Note that a port number is not assumed to be 16 bits. */
101
102     if ((service = getservbyname("ntp","udp")) != NULL) {
103         *port = service->s_port;
104         if (verbose > 2)
105             fprintf(stderr,"Using port %d for NTP\n",port_to_integer(*port));
106     } else {
107         *port = NTP_PORT;
108         if (verbose)
109             fprintf(stderr,
110                 "%s: assuming port %d for NTP - check /etc/services\n",
111                 argv0,port_to_integer(*port));
112     }
113 }