home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / rpc / rtime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-15  |  3.5 KB  |  142 lines

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