1RTIME(3) Linux Programmer's Manual RTIME(3)
2
3
4
6 rtime - get time from a remote machine
7
9 #include <rpc/des_crypt.h>
10
11 int rtime(struct sockaddr_in *addrp, struct rpc_timeval *timep,
12 struct rpc_timeval *timeout);
13
15 This function uses the Time Server Protocol as described in RFC 868 to
16 obtain the time from a remote machine.
17
18 The Time Server Protocol gives the time in seconds since 00:00:00 UTC,
19 1 Jan 1900, and this function subtracts the appropriate constant in
20 order to convert the result to seconds since the Epoch, 1970-01-01
21 00:00:00 +0000 (UTC).
22
23 When timeout is non-NULL, the udp/time socket (port 37) is used. Oth‐
24 erwise, the tcp/time socket (port 37) is used.
25
27 On success, 0 is returned, and the obtained 32-bit time value is stored
28 in timep->tv_sec. In case of error -1 is returned, and errno is set
29 appropriately.
30
32 All errors for underlying functions (sendto(2), poll(2), recvfrom(2),
33 connect(2), read(2)) can occur. Moreover:
34
35 EIO The number of returned bytes is not 4.
36
37 ETIMEDOUT
38 The waiting time as defined in timeout has expired.
39
41 Only IPv4 is supported.
42
43 Some in.timed versions only support TCP. Try the example program with
44 use_tcp set to 1.
45
46 Libc5 uses the prototype
47 int rtime(struct sockaddr_in *, struct timeval *, struct timeval *);
48 and requires <sys/time.h> instead of <rpc/auth_des.h>.
49
51 rtime() in glibc 2.2.5 and earlier does not work properly on 64-bit
52 machines.
53
55 This example requires that port 37 is up and open. You may check that
56 the time entry within /etc/inetd.conf is not commented out.
57 The program connects to a computer called "linux". Using "localhost"
58 does not work. The result is the localtime of the computer "linux".
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <errno.h>
63 #include <string.h>
64 #include <time.h>
65 #include <rpc/auth_des.h>
66 #include <netdb.h>
67
68 int use_tcp = 0;
69 char *servername = "linux";
70
71 int
72 main(void)
73 {
74 struct sockaddr_in name;
75 struct rpc_timeval time1 = {0,0};
76 struct rpc_timeval timeout = {1,0};
77 struct hostent *hent;
78 int ret;
79
80 memset((char *) &name, 0, sizeof(name));
81 sethostent(1);
82 hent = gethostbyname(servername);
83 memcpy((char *) &name.sin_addr, hent->h_addr, hent->h_length);
84
85 ret = rtime(&name, &time1, use_tcp ? NULL : &timeout);
86 if (ret < 0)
87 perror("rtime error");
88 else
89 printf("%s\n", ctime((time_t *) &time1.tv_sec));
90
91 exit(EXIT_SUCCESS);
92 }
93
95 ntpdate(1), inetd(8)
96
98 This page is part of release 3.25 of the Linux man-pages project. A
99 description of the project, and information about reporting bugs, can
100 be found at http://www.kernel.org/doc/man-pages/.
101
102
103
104GNU 2010-02-25 RTIME(3)