1 /* vi: set sw=4 ts=4: */
3 * The Rdate command will ask a time server for the RFC 868 time
4 * and optionally set the system time.
6 * by Sterling Huxley <sterling@europa.com>
8 * Licensed under GPL v2 or later, see file License for details.
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
25 static const int RFC_868_BIAS = 2208988800UL;
27 static void socket_timeout(int sig)
29 bb_error_msg_and_die("timeout connecting to time server");
32 static time_t askremotedate(const char *host)
35 struct sockaddr_in s_in;
38 bb_lookup_host(&s_in, host);
39 s_in.sin_port = bb_lookup_port("time", "tcp", 37);
41 /* Add a timeout for dead or inaccessible servers */
43 signal(SIGALRM, socket_timeout);
45 fd = xconnect_tcp_v4(&s_in);
47 if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */
48 bb_error_msg_and_die("%s did not send the complete time", host);
51 /* convert from network byte order to local byte order.
52 * RFC 868 time is the number of seconds
53 * since 00:00 (midnight) 1 January 1900 GMT
54 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
55 * Subtract the RFC 868 time to get Linux epoch
58 return(ntohl(nett) - RFC_868_BIAS);
61 int rdate_main(int argc, char **argv)
66 opt_complementary = "-1";
67 flags = getopt32(argc, argv, "sp");
69 remote_time = askremotedate(argv[optind]);
71 if ((flags & 2) == 0) {
75 if (current_time == remote_time)
76 bb_error_msg("current time matches remote time");
78 if (stime(&remote_time) < 0)
79 bb_perror_msg_and_die("cannot set time of day");
83 printf("%s", ctime(&remote_time));