1rtime(3) Library Functions Manual rtime(3)
2
3
4
6 rtime - get time from a remote machine
7
9 Standard C library (libc, -lc)
10
12 #include <rpc/auth_des.h>
13
14 int rtime(struct sockaddr_in *addrp, struct rpc_timeval *timep,
15 struct rpc_timeval *timeout);
16
18 This function uses the Time Server Protocol as described in RFC 868 to
19 obtain the time from a remote machine.
20
21 The Time Server Protocol gives the time in seconds since 00:00:00 UTC,
22 1 Jan 1900, and this function subtracts the appropriate constant in or‐
23 der to convert the result to seconds since the Epoch, 1970-01-01
24 00:00:00 +0000 (UTC).
25
26 When timeout is non-NULL, the udp/time socket (port 37) is used. Oth‐
27 erwise, the tcp/time socket (port 37) is used.
28
30 On success, 0 is returned, and the obtained 32-bit time value is stored
31 in timep->tv_sec. In case of error -1 is returned, and errno is set to
32 indicate the error.
33
35 All errors for underlying functions (sendto(2), poll(2), recvfrom(2),
36 connect(2), read(2)) can occur. Moreover:
37
38 EIO The number of returned bytes is not 4.
39
40 ETIMEDOUT
41 The waiting time as defined in timeout has expired.
42
44 For an explanation of the terms used in this section, see at‐
45 tributes(7).
46
47 ┌────────────────────────────────────────────┬───────────────┬─────────┐
48 │Interface │ Attribute │ Value │
49 ├────────────────────────────────────────────┼───────────────┼─────────┤
50 │rtime() │ Thread safety │ MT-Safe │
51 └────────────────────────────────────────────┴───────────────┴─────────┘
52
54 Only IPv4 is supported.
55
56 Some in.timed versions support only TCP. Try the example program with
57 use_tcp set to 1.
58
60 rtime() in glibc 2.2.5 and earlier does not work properly on 64-bit ma‐
61 chines.
62
64 This example requires that port 37 is up and open. You may check that
65 the time entry within /etc/inetd.conf is not commented out.
66
67 The program connects to a computer called "linux". Using "localhost"
68 does not work. The result is the localtime of the computer "linux".
69
70 #include <errno.h>
71 #include <netdb.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <time.h>
76
77 #include <rpc/auth_des.h>
78
79 static int use_tcp = 0;
80 static const char servername[] = "linux";
81
82 int
83 main(void)
84 {
85 int ret;
86 time_t t;
87 struct hostent *hent;
88 struct rpc_timeval time1 = {0, 0};
89 struct rpc_timeval timeout = {1, 0};
90 struct sockaddr_in name;
91
92 memset(&name, 0, sizeof(name));
93 sethostent(1);
94 hent = gethostbyname(servername);
95 memcpy(&name.sin_addr, hent->h_addr, hent->h_length);
96
97 ret = rtime(&name, &time1, use_tcp ? NULL : &timeout);
98 if (ret < 0)
99 perror("rtime error");
100 else {
101 t = time1.tv_sec;
102 printf("%s\n", ctime(&t));
103 }
104
105 exit(EXIT_SUCCESS);
106 }
107
109 ntpdate(1), inetd(8)
110
111
112
113Linux man-pages 6.04 2022-12-15 rtime(3)