blob: 04a76129a3f62d2e199ad042c8d412bc4313deb0 [file] [log] [blame]
Eric Andersen6ab22022000-08-21 23:04:00 +00001/* vi: set sw=4 ts=4: */
2/*
3 * The Rdate command will ask a time server for the RFC 868 time
4 * and optionally set the system time.
5 *
6 * by Sterling Huxley <sterling@europa.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22*/
23
Eric Andersen6ab22022000-08-21 23:04:00 +000024#include <sys/time.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netdb.h>
29#include <stdio.h>
30#include <getopt.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000031#include <string.h>
32#include <time.h>
Eric Andersened3ef502001-01-27 08:24:39 +000033#include <stdlib.h>
34#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
Eric Andersen6ab22022000-08-21 23:04:00 +000036
37
Mark Whitley59ab0252001-01-23 22:30:04 +000038static const int RFC_868_BIAS = 2208988800UL;
Eric Andersen6ab22022000-08-21 23:04:00 +000039
Eric Andersen40eaa9f2001-03-19 19:41:54 +000040static time_t askremotedate(const char *host)
Eric Andersen6ab22022000-08-21 23:04:00 +000041{
42 struct hostent *h;
Eric Andersen1ca20a72001-03-21 07:34:27 +000043 struct sockaddr_in s_in;
Eric Andersen6ab22022000-08-21 23:04:00 +000044 struct servent *tserv;
45 unsigned long int nett, localt;
46 int fd;
47
Matt Kraaic55b8d42001-05-16 15:40:51 +000048 h = xgethostbyname(host); /* get the IP addr */
Matt Kraaic9fc6332001-10-01 17:52:14 +000049 memcpy(&s_in.sin_addr, h->h_addr, sizeof(s_in.sin_addr));
Eric Andersen40eaa9f2001-03-19 19:41:54 +000050
Matt Kraaic9fc6332001-10-01 17:52:14 +000051 s_in.sin_port = htons(37); /* find port # */
52 if ((tserv = getservbyname("time", "tcp")) != NULL)
53 s_in.sin_port = tserv->s_port;
54
55 s_in.sin_family = AF_INET;
Eric Andersen40eaa9f2001-03-19 19:41:54 +000056
57 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* get net connection */
Matt Kraai13cb8422001-07-30 14:43:20 +000058 perror_msg_and_die("socket");
Eric Andersen6ab22022000-08-21 23:04:00 +000059
Eric Andersen1ca20a72001-03-21 07:34:27 +000060 if (connect(fd, (struct sockaddr *)&s_in, sizeof(s_in)) < 0) /* connect to time server */
Eric Andersen40eaa9f2001-03-19 19:41:54 +000061 perror_msg_and_die("%s", host);
62
63 if (read(fd, (void *)&nett, 4) != 4) /* read time from server */
64 error_msg_and_die("%s did not send the complete time", host);
65
Eric Andersen6ab22022000-08-21 23:04:00 +000066 close(fd);
67
68 /* convert from network byte order to local byte order.
69 * RFC 868 time is the number of seconds
70 * since 00:00 (midnight) 1 January 1900 GMT
71 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
72 * Subtract the RFC 868 time to get Linux epoch
73 */
74 localt= ntohl(nett) - RFC_868_BIAS;
75
76 return(localt);
77}
78
79int rdate_main(int argc, char **argv)
80{
Eric Andersen1ca20a72001-03-21 07:34:27 +000081 time_t remote_time;
Eric Andersen6ab22022000-08-21 23:04:00 +000082 int opt;
Matt Kraai13cb8422001-07-30 14:43:20 +000083 int setdate = 1;
84 int printdate = 1;
Eric Andersen6ab22022000-08-21 23:04:00 +000085
86 /* Interpret command line args */
Matt Kraai13cb8422001-07-30 14:43:20 +000087 while ((opt = getopt(argc, argv, "sp")) > 0) {
Eric Andersen6ab22022000-08-21 23:04:00 +000088 switch (opt) {
Eric Andersen6ab22022000-08-21 23:04:00 +000089 case 's':
Matt Kraai13cb8422001-07-30 14:43:20 +000090 printdate = 0;
91 setdate = 1;
Eric Andersen6ab22022000-08-21 23:04:00 +000092 break;
93 case 'p':
Matt Kraai13cb8422001-07-30 14:43:20 +000094 printdate = 1;
95 setdate = 0;
Eric Andersen6ab22022000-08-21 23:04:00 +000096 break;
Matt Kraai13cb8422001-07-30 14:43:20 +000097 default:
98 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +000099 }
100 }
101
Eric Andersen40eaa9f2001-03-19 19:41:54 +0000102 if (optind == argc)
Eric Andersen67991cf2001-02-14 21:23:06 +0000103 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +0000104
Eric Andersen1ca20a72001-03-21 07:34:27 +0000105 remote_time = askremotedate(argv[optind]);
Eric Andersen40eaa9f2001-03-19 19:41:54 +0000106
Eric Andersen6ab22022000-08-21 23:04:00 +0000107 if (setdate) {
Eric Andersen1ca20a72001-03-21 07:34:27 +0000108 if (stime(&remote_time) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000109 perror_msg_and_die("Could not set time of day");
Eric Andersen6ab22022000-08-21 23:04:00 +0000110 }
Eric Andersen40eaa9f2001-03-19 19:41:54 +0000111
112 if (printdate)
Eric Andersen1ca20a72001-03-21 07:34:27 +0000113 printf("%s", ctime(&remote_time));
Eric Andersen6ab22022000-08-21 23:04:00 +0000114
Matt Kraai3e856ce2000-12-01 02:55:13 +0000115 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +0000116}