OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / inet / rpc / rtime.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29 #if 0
30 static char sccsid[] = "@(#)rtime.c     2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI";
31 #endif
32
33 /*
34  * Copyright (c) 1988 by Sun Microsystems, Inc.
35  */
36 /*
37  * rtime - get time from remote machine
38  *
39  * gets time, obtaining value from host
40  * on the udp/time socket.  Since timeserver returns
41  * with time of day in seconds since Jan 1, 1900,  must
42  * subtract seconds before Jan 1, 1970 to get
43  * what unix uses.
44  */
45
46 #define __FORCE_GLIBC
47 #include <features.h>
48
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <rpc/rpc.h>
52 #include <rpc/clnt.h>
53 #include <sys/types.h>
54 #include <sys/poll.h>
55 #include <sys/socket.h>
56 #include <sys/time.h>
57 #include <rpc/auth_des.h>
58 #include <errno.h>
59 #include <netinet/in.h>
60
61 libc_hidden_proto(read)
62 libc_hidden_proto(socket)
63 libc_hidden_proto(close)
64 libc_hidden_proto(connect)
65 libc_hidden_proto(recvfrom)
66 libc_hidden_proto(sendto)
67 libc_hidden_proto(poll)
68
69 #define NYEARS  (u_long)(1970 - 1900)
70 #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
71
72 static void do_close (int);
73
74 static void
75 do_close (int s)
76 {
77   int save;
78
79   save = errno;
80   close (s);
81   __set_errno (save);
82 }
83
84 int
85 rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
86        struct rpc_timeval *timeout)
87 {
88   int s;
89   struct pollfd fd;
90   int milliseconds;
91   int res;
92   unsigned long thetime;
93   struct sockaddr_in from;
94   int fromlen;
95   int type;
96
97   if (timeout == NULL)
98     type = SOCK_STREAM;
99   else
100     type = SOCK_DGRAM;
101
102   s = socket (AF_INET, type, 0);
103   if (s < 0)
104     return (-1);
105
106   addrp->sin_family = AF_INET;
107   addrp->sin_port = htons (IPPORT_TIMESERVER);
108   if (type == SOCK_DGRAM)
109     {
110       res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
111                     (struct sockaddr *) addrp, sizeof (*addrp));
112       if (res < 0)
113         {
114           do_close (s);
115           return -1;
116         }
117       milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
118       fd.fd = s;
119       fd.events = POLLIN;
120       do
121         res = poll (&fd, 1, milliseconds);
122       while (res < 0 && errno == EINTR);
123       if (res <= 0)
124         {
125           if (res == 0)
126             __set_errno (ETIMEDOUT);
127           do_close (s);
128           return (-1);
129         }
130       fromlen = sizeof (from);
131       res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
132                       (struct sockaddr *) &from, &fromlen);
133       do_close (s);
134       if (res < 0)
135         return -1;
136     }
137   else
138     {
139       if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
140         {
141           do_close (s);
142           return -1;
143         }
144       res = read (s, (char *) &thetime, sizeof (thetime));
145       do_close (s);
146       if (res < 0)
147         return (-1);
148     }
149   if (res != sizeof (thetime))
150     {
151       __set_errno (EIO);
152       return -1;
153     }
154   thetime = ntohl (thetime);
155   timep->tv_sec = thetime - TOFFSET;
156   timep->tv_usec = 0;
157   return 0;
158 }