rtime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * Copyright (c) 1988 by Sun Microsystems, Inc.
  34. */
  35. /*
  36. * rtime - get time from remote machine
  37. *
  38. * gets time, obtaining value from host
  39. * on the udp/time socket. Since timeserver returns
  40. * with time of day in seconds since Jan 1, 1900, must
  41. * subtract seconds before Jan 1, 1970 to get
  42. * what unix uses.
  43. */
  44. #define connect __connect
  45. #define recvfrom __recvfrom
  46. #define sendto __sendto
  47. #define poll __poll
  48. #define __FORCE_GLIBC
  49. #include <features.h>
  50. #include <stdio.h>
  51. #include <unistd.h>
  52. #include <rpc/rpc.h>
  53. #include <rpc/clnt.h>
  54. #include <sys/types.h>
  55. #include <sys/poll.h>
  56. #include <sys/socket.h>
  57. #include <sys/time.h>
  58. #include <rpc/auth_des.h>
  59. #include <errno.h>
  60. #include <netinet/in.h>
  61. #define NYEARS (u_long)(1970 - 1900)
  62. #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
  63. static void do_close (int);
  64. static void
  65. do_close (int s)
  66. {
  67. int save;
  68. save = errno;
  69. __close (s);
  70. __set_errno (save);
  71. }
  72. int
  73. rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
  74. struct rpc_timeval *timeout)
  75. {
  76. int s;
  77. struct pollfd fd;
  78. int milliseconds;
  79. int res;
  80. unsigned long thetime;
  81. struct sockaddr_in from;
  82. int fromlen;
  83. int type;
  84. if (timeout == NULL)
  85. type = SOCK_STREAM;
  86. else
  87. type = SOCK_DGRAM;
  88. s = __socket (AF_INET, type, 0);
  89. if (s < 0)
  90. return (-1);
  91. addrp->sin_family = AF_INET;
  92. addrp->sin_port = htons (IPPORT_TIMESERVER);
  93. if (type == SOCK_DGRAM)
  94. {
  95. res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
  96. (struct sockaddr *) addrp, sizeof (*addrp));
  97. if (res < 0)
  98. {
  99. do_close (s);
  100. return -1;
  101. }
  102. milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
  103. fd.fd = s;
  104. fd.events = POLLIN;
  105. do
  106. res = poll (&fd, 1, milliseconds);
  107. while (res < 0 && errno == EINTR);
  108. if (res <= 0)
  109. {
  110. if (res == 0)
  111. __set_errno (ETIMEDOUT);
  112. do_close (s);
  113. return (-1);
  114. }
  115. fromlen = sizeof (from);
  116. res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
  117. (struct sockaddr *) &from, &fromlen);
  118. do_close (s);
  119. if (res < 0)
  120. return -1;
  121. }
  122. else
  123. {
  124. if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
  125. {
  126. do_close (s);
  127. return -1;
  128. }
  129. res = __read (s, (char *) &thetime, sizeof (thetime));
  130. do_close (s);
  131. if (res < 0)
  132. return (-1);
  133. }
  134. if (res != sizeof (thetime))
  135. {
  136. __set_errno (EIO);
  137. return -1;
  138. }
  139. thetime = ntohl (thetime);
  140. timep->tv_sec = thetime - TOFFSET;
  141. timep->tv_usec = 0;
  142. return 0;
  143. }