1 /* rdate 1.0 - Set time&date from remote host Author: Kees J. Bot
13 #include <sys/ioctl.h>
15 #include <net/netlib.h>
16 #include <net/gen/in.h>
17 #include <net/gen/netdb.h>
18 #include <net/gen/tcp.h>
19 #include <net/gen/tcp_io.h>
21 void report(const char *label
)
23 fprintf(stderr
, "rdate: %s: %s\n", label
, strerror(errno
));
26 void fatal(const char *label
)
32 int main(int argc
, char **argv
)
37 struct servent
*servent
;
38 struct hostent
*hostent
;
40 nwio_tcpconf_t tcpconf
;
46 fprintf(stderr
, "Usage: rdate host ...\n");
50 /* Look up the port number of the TCP service "time". */
51 if ((servent
= getservbyname("time", "tcp")) == nil
) {
52 fprintf(stderr
, "rdate: \"time\": unknown service\n");
55 time_port
= servent
->s_port
;
57 if ((tcp_device
= getenv("TCP_DEVICE")) == nil
) tcp_device
= TCP_DEVICE
;
59 if ((fd
= open(tcp_device
, O_RDWR
)) < 0) fatal(tcp_device
);
61 /* Try each host on the command line. */
62 for (i
= 1; i
< argc
; i
++) {
63 if ((hostent
= gethostbyname(argv
[i
])) == nil
) {
64 fprintf(stderr
, "rdate: %s: unknown host\n", argv
[i
]);
68 /* Configure a TCP channel and connect to the remote host. */
70 tcpconf
.nwtc_flags
= NWTC_LP_SEL
| NWTC_SET_RA
| NWTC_SET_RP
;
71 memcpy(&tcpconf
.nwtc_remaddr
, hostent
->h_addr
, 4);
72 tcpconf
.nwtc_remport
= time_port
;
73 if (ioctl(fd
, NWIOSTCPCONF
, &tcpconf
) == -1) fatal(tcp_device
);
76 if (ioctl(fd
, NWIOTCPCONN
, &tcpcl
) < 0) {
81 /* Read four bytes to obtain the time. */
82 switch (read(fd
, &net_time
, sizeof(net_time
))) {
87 fprintf(stderr
, "rdate: %s: short read\n", argv
[i
]);
89 case sizeof(net_time
):
94 if (i
== argc
) exit(1);
96 /* Internet time is in seconds since 1900, UNIX time is in seconds
99 unix_time
= ntohl(net_time
) - 2208988800;
101 /* Try to set the time and tell us about it. */
102 if (stime(&unix_time
) < 0) {
105 printf("time set to ");
107 printf("%s: %s", argv
[i
], ctime(&unix_time
));