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 * 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.
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.
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
25 #define BB_DECLARE_EXTERN
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
38 static const int RFC_868_BIAS = 2208988800UL;
43 time_t askremotedate(char *host)
46 struct sockaddr_in sin;
47 struct servent *tserv;
48 unsigned long int nett, localt;
51 if (!(h = gethostbyname(host))) { /* get the IP addr */
52 perror_msg("%s", host);
55 if ((tserv = getservbyname("time", "tcp")) == NULL) { /* find port # */
56 perror_msg("%s", "time");
59 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* get net connection */
60 perror_msg("%s", "socket");
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;
68 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { /* connect to time server */
69 perror_msg("%s", host);
73 if (read(fd, (void *)&nett, 4) != 4) { /* read time from server */
75 error_msg("%s did not send the complete time\n", host);
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
85 localt= ntohl(nett) - RFC_868_BIAS;
90 int rdate_main(int argc, char **argv)
95 /* Interpret command line args */
96 /* do special-case option parsing */
97 if (argv[1] && (strcmp(argv[1], "--help") == 0))
100 /* do normal option parsing */
101 while ((opt = getopt(argc, argv, "Hsp")) > 0) {
116 /* the default action is to set the date */
117 if (printdate==0 && setdate==0) setdate++;
119 if (optind == argc) {
123 if ((time= askremotedate(argv[optind])) == (time_t)-1) {
127 if (stime(&time) < 0)
128 perror_msg_and_die("Could not set time of day");
131 printf("%s", ctime(&time));