Back to index

glibc  2.9
rtime.c
Go to the documentation of this file.
00001 #if defined(LIBC_SCCS) && !defined(lint)
00002 static char sccsid[] = "@(#)rtime.c       2.2 88/08/10 4.0 RPCSRC; from 1.8 88/02/08 SMI";
00003 #endif
00004 /*
00005  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00006  * unrestricted use provided that this legend is included on all tape
00007  * media and as a part of the software program in whole or part.  Users
00008  * may copy or modify Sun RPC without charge, but are not authorized
00009  * to license or distribute it to anyone else except as part of a product or
00010  * program developed by the user.
00011  *
00012  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00013  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00014  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00015  *
00016  * Sun RPC is provided with no support and without any obligation on the
00017  * part of Sun Microsystems, Inc. to assist in its use, correction,
00018  * modification or enhancement.
00019  *
00020  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00021  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00022  * OR ANY PART THEREOF.
00023  *
00024  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00025  * or profits or other special, indirect and consequential damages, even if
00026  * Sun has been advised of the possibility of such damages.
00027  *
00028  * Sun Microsystems, Inc.
00029  * 2550 Garcia Avenue
00030  * Mountain View, California  94043
00031  */
00032 
00033 /*
00034  * Copyright (c) 1988 by Sun Microsystems, Inc.
00035  */
00036 /*
00037  * rtime - get time from remote machine
00038  *
00039  * gets time, obtaining value from host
00040  * on the udp/time socket.  Since timeserver returns
00041  * with time of day in seconds since Jan 1, 1900,  must
00042  * subtract seconds before Jan 1, 1970 to get
00043  * what unix uses.
00044  */
00045 #include <stdio.h>
00046 #include <unistd.h>
00047 #include <rpc/rpc.h>
00048 #include <rpc/clnt.h>
00049 #include <sys/types.h>
00050 #include <sys/poll.h>
00051 #include <sys/socket.h>
00052 #include <sys/time.h>
00053 #include <rpc/auth_des.h>
00054 #include <errno.h>
00055 #include <netinet/in.h>
00056 
00057 #define NYEARS       (u_long)(1970 - 1900)
00058 #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
00059 
00060 static void do_close (int);
00061 
00062 static void
00063 do_close (int s)
00064 {
00065   int save;
00066 
00067   save = errno;
00068   __close (s);
00069   __set_errno (save);
00070 }
00071 
00072 int
00073 rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
00074        struct rpc_timeval *timeout)
00075 {
00076   int s;
00077   struct pollfd fd;
00078   int milliseconds;
00079   int res;
00080   /* RFC 868 says the time is transmitted as a 32-bit value.  */
00081   uint32_t thetime;
00082   struct sockaddr_in from;
00083   socklen_t fromlen;
00084   int type;
00085 
00086   if (timeout == NULL)
00087     type = SOCK_STREAM;
00088   else
00089     type = SOCK_DGRAM;
00090 
00091   s = __socket (AF_INET, type, 0);
00092   if (s < 0)
00093     return (-1);
00094 
00095   addrp->sin_family = AF_INET;
00096   addrp->sin_port = htons (IPPORT_TIMESERVER);
00097   if (type == SOCK_DGRAM)
00098     {
00099       res = __sendto (s, (char *) &thetime, sizeof (thetime), 0,
00100                     (struct sockaddr *) addrp, sizeof (*addrp));
00101       if (res < 0)
00102        {
00103          do_close (s);
00104          return -1;
00105        }
00106       milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
00107       fd.fd = s;
00108       fd.events = POLLIN;
00109       do
00110        res = __poll (&fd, 1, milliseconds);
00111       while (res < 0 && errno == EINTR);
00112       if (res <= 0)
00113        {
00114          if (res == 0)
00115            __set_errno (ETIMEDOUT);
00116          do_close (s);
00117          return (-1);
00118        }
00119       fromlen = sizeof (from);
00120       res = __recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
00121                      (struct sockaddr *) &from, &fromlen);
00122       do_close (s);
00123       if (res < 0)
00124        return -1;
00125     }
00126   else
00127     {
00128       if (__connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
00129        {
00130          do_close (s);
00131          return -1;
00132        }
00133       res = __read (s, (char *) &thetime, sizeof (thetime));
00134       do_close (s);
00135       if (res < 0)
00136        return (-1);
00137     }
00138   if (res != sizeof (thetime))
00139     {
00140       __set_errno (EIO);
00141       return -1;
00142     }
00143   thetime = ntohl (thetime);
00144   timep->tv_sec = thetime - TOFFSET;
00145   timep->tv_usec = 0;
00146   return 0;
00147 }
00148 libc_hidden_def (rtime)