blob: 38a7689e912a042c616da94820bea300f4d892a3 [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 Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersen6ab22022000-08-21 23:04:00 +000025#define BB_DECLARE_EXTERN
26#include "messages.c"
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <netdb.h>
32#include <stdio.h>
33#include <getopt.h>
Eric Andersened3ef502001-01-27 08:24:39 +000034#include <stdlib.h>
35#include <unistd.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
40int setdate= 0;
41int printdate= 0;
42
43time_t askremotedate(char *host)
44{
45 struct hostent *h;
46 struct sockaddr_in sin;
47 struct servent *tserv;
48 unsigned long int nett, localt;
49 int fd;
50
51 if (!(h = gethostbyname(host))) { /* get the IP addr */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000052 perror_msg("%s", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000053 return(-1);
54 }
55 if ((tserv = getservbyname("time", "tcp")) == NULL) { /* find port # */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000056 perror_msg("%s", "time");
Eric Andersen6ab22022000-08-21 23:04:00 +000057 return(-1);
58 }
59 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* get net connection */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000060 perror_msg("%s", "socket");
Eric Andersen6ab22022000-08-21 23:04:00 +000061 return(-1);
62 }
63
64 memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr));
65 sin.sin_port= tserv->s_port;
66 sin.sin_family = AF_INET;
67
Eric Andersenba35b982000-09-21 04:05:38 +000068 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { /* connect to time server */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000069 perror_msg("%s", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000070 close(fd);
71 return(-1);
72 }
Eric Andersend1ee7e32000-10-26 07:57:27 +000073 if (read(fd, (void *)&nett, 4) != 4) { /* read time from server */
Eric Andersen6ab22022000-08-21 23:04:00 +000074 close(fd);
Matt Kraaidd19c692001-01-31 19:00:21 +000075 error_msg("%s did not send the complete time", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000076 }
77 close(fd);
78
79 /* convert from network byte order to local byte order.
80 * RFC 868 time is the number of seconds
81 * since 00:00 (midnight) 1 January 1900 GMT
82 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
83 * Subtract the RFC 868 time to get Linux epoch
84 */
85 localt= ntohl(nett) - RFC_868_BIAS;
86
87 return(localt);
88}
89
90int rdate_main(int argc, char **argv)
91{
92 time_t time;
93 int opt;
94
95 /* Interpret command line args */
96 /* do special-case option parsing */
97 if (argv[1] && (strcmp(argv[1], "--help") == 0))
Eric Andersen67991cf2001-02-14 21:23:06 +000098 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +000099
100 /* do normal option parsing */
101 while ((opt = getopt(argc, argv, "Hsp")) > 0) {
102 switch (opt) {
103 default:
104 case 'H':
Eric Andersen67991cf2001-02-14 21:23:06 +0000105 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +0000106 break;
107 case 's':
108 setdate++;
109 break;
110 case 'p':
111 printdate++;
112 break;
113 }
114 }
115
116 /* the default action is to set the date */
117 if (printdate==0 && setdate==0) setdate++;
118
119 if (optind == argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000120 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +0000121 }
122
123 if ((time= askremotedate(argv[optind])) == (time_t)-1) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000124 return EXIT_FAILURE;
Eric Andersen6ab22022000-08-21 23:04:00 +0000125 }
126 if (setdate) {
127 if (stime(&time) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000128 perror_msg_and_die("Could not set time of day");
Eric Andersen6ab22022000-08-21 23:04:00 +0000129 }
130 if (printdate) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000131 printf("%s", ctime(&time));
Eric Andersen6ab22022000-08-21 23:04:00 +0000132 }
133
Matt Kraai3e856ce2000-12-01 02:55:13 +0000134 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +0000135}