| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#include <pthread.h> |
| 44 |
#include <stdlib.h> |
| 45 |
#include <string.h> |
| 46 |
#include <unistd.h> |
| 47 |
#include <errno.h> |
| 48 |
#include <sys/types.h> |
| 49 |
#include <sys/poll.h> |
| 50 |
#include <sys/socket.h> |
| 51 |
#include <sys/time.h> |
| 52 |
#include <netinet/in.h> |
| 53 |
#include <stdio.h> |
| 54 |
#include <netdb.h> |
| 55 |
#include <sys/select.h> |
| 56 |
|
| 57 |
extern int _rpc_dtablesize( void ); |
| 58 |
|
| 59 |
#define NYEARS (unsigned long)(1970 - 1900) |
| 60 |
#define TOFFSET (unsigned long)(60*60*24*(365*NYEARS + (NYEARS/4))) |
| 61 |
|
| 62 |
static void do_close( int ); |
| 63 |
|
| 64 |
int |
| 65 |
rtime(addrp, timep, timeout) |
| 66 |
struct sockaddr_in *addrp; |
| 67 |
struct timeval *timep; |
| 68 |
struct timeval *timeout; |
| 69 |
{ |
| 70 |
int s; |
| 71 |
struct pollfd fd; |
| 72 |
int milliseconds; |
| 73 |
int res; |
| 74 |
unsigned long thetime; |
| 75 |
struct sockaddr_in from; |
| 76 |
socklen_t fromlen; |
| 77 |
int type; |
| 78 |
struct servent *serv; |
| 79 |
|
| 80 |
if (timeout == NULL) { |
| 81 |
type = SOCK_STREAM; |
| 82 |
} else { |
| 83 |
type = SOCK_DGRAM; |
| 84 |
} |
| 85 |
s = socket(AF_INET, type, 0); |
| 86 |
if (s < 0) { |
| 87 |
return(-1); |
| 88 |
} |
| 89 |
addrp->sin_family = AF_INET; |
| 90 |
|
| 91 |
|
| 92 |
if ((serv = getservbyname("time", "tcp")) == NULL) { |
| 93 |
do_close(s); |
| 94 |
return(-1); |
| 95 |
} |
| 96 |
|
| 97 |
addrp->sin_port = serv->s_port; |
| 98 |
|
| 99 |
if (type == SOCK_DGRAM) { |
| 100 |
res = sendto(s, (char *)&thetime, sizeof(thetime), 0, |
| 101 |
(struct sockaddr *)addrp, sizeof(*addrp)); |
| 102 |
if (res < 0) { |
| 103 |
do_close(s); |
| 104 |
return(-1); |
| 105 |
} |
| 106 |
|
| 107 |
milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000); |
| 108 |
fd.fd = s; |
| 109 |
fd.events = POLLIN; |
| 110 |
do |
| 111 |
res = poll (&fd, 1, milliseconds); |
| 112 |
while (res < 0 && errno == EINTR); |
| 113 |
if (res <= 0) { |
| 114 |
if (res == 0) { |
| 115 |
errno = ETIMEDOUT; |
| 116 |
} |
| 117 |
do_close(s); |
| 118 |
return(-1); |
| 119 |
} |
| 120 |
fromlen = sizeof(from); |
| 121 |
res = recvfrom(s, (char *)&thetime, sizeof(thetime), 0, |
| 122 |
(struct sockaddr *)&from, &fromlen); |
| 123 |
do_close(s); |
| 124 |
if (res < 0) { |
| 125 |
return(-1); |
| 126 |
} |
| 127 |
} else { |
| 128 |
if (connect(s, (struct sockaddr *)addrp, sizeof(*addrp)) < 0) { |
| 129 |
do_close(s); |
| 130 |
return(-1); |
| 131 |
} |
| 132 |
res = read(s, (char *)&thetime, sizeof(thetime)); |
| 133 |
do_close(s); |
| 134 |
if (res < 0) { |
| 135 |
return(-1); |
| 136 |
} |
| 137 |
} |
| 138 |
if (res != sizeof(thetime)) { |
| 139 |
errno = EIO; |
| 140 |
return(-1); |
| 141 |
} |
| 142 |
thetime = ntohl(thetime); |
| 143 |
timep->tv_sec = thetime - TOFFSET; |
| 144 |
timep->tv_usec = 0; |
| 145 |
return(0); |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
do_close(s) |
| 150 |
int s; |
| 151 |
{ |
| 152 |
int save; |
| 153 |
|
| 154 |
save = errno; |
| 155 |
(void)close(s); |
| 156 |
errno = save; |
| 157 |
} |