rtime.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #if 0
  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. * 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. #include <stdio.h>
  45. #include <unistd.h>
  46. #include <rpc/rpc.h>
  47. #include <rpc/clnt.h>
  48. #include <sys/types.h>
  49. #include <sys/poll.h>
  50. #include <sys/socket.h>
  51. #include <sys/time.h>
  52. #include <rpc/auth_des.h>
  53. #include <errno.h>
  54. #include <netinet/in.h>
  55. #define NYEARS (u_long)(1970 - 1900)
  56. #define TOFFSET (u_long)(60*60*24*(365*NYEARS + (NYEARS/4)))
  57. static void do_close (int);
  58. static void
  59. do_close (int s)
  60. {
  61. int save;
  62. save = errno;
  63. close (s);
  64. __set_errno (save);
  65. }
  66. int
  67. rtime (struct sockaddr_in *addrp, struct rpc_timeval *timep,
  68. struct rpc_timeval *timeout)
  69. {
  70. int s;
  71. struct pollfd fd;
  72. int milliseconds;
  73. int res;
  74. /* RFC 868 says the time is transmitted as a 32-bit value. */
  75. uint32_t thetime;
  76. struct sockaddr_in from;
  77. socklen_t fromlen;
  78. int type;
  79. if (timeout == NULL)
  80. type = SOCK_STREAM;
  81. else
  82. type = SOCK_DGRAM;
  83. s = socket (AF_INET, type, 0);
  84. if (s < 0)
  85. return (-1);
  86. addrp->sin_family = AF_INET;
  87. addrp->sin_port = htons (IPPORT_TIMESERVER);
  88. if (type == SOCK_DGRAM)
  89. {
  90. res = sendto (s, (char *) &thetime, sizeof (thetime), 0,
  91. (struct sockaddr *) addrp, sizeof (*addrp));
  92. if (res < 0)
  93. {
  94. do_close (s);
  95. return -1;
  96. }
  97. milliseconds = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
  98. fd.fd = s;
  99. fd.events = POLLIN;
  100. do
  101. res = poll (&fd, 1, milliseconds);
  102. while (res < 0 && errno == EINTR);
  103. if (res <= 0)
  104. {
  105. if (res == 0)
  106. __set_errno (ETIMEDOUT);
  107. do_close (s);
  108. return (-1);
  109. }
  110. fromlen = sizeof (from);
  111. res = recvfrom (s, (char *) &thetime, sizeof (thetime), 0,
  112. (struct sockaddr *) &from, &fromlen);
  113. do_close (s);
  114. if (res < 0)
  115. return -1;
  116. }
  117. else
  118. {
  119. if (connect (s, (struct sockaddr *) addrp, sizeof (*addrp)) < 0)
  120. {
  121. do_close (s);
  122. return -1;
  123. }
  124. res = read (s, (char *) &thetime, sizeof (thetime));
  125. do_close (s);
  126. if (res < 0)
  127. return (-1);
  128. }
  129. if (res != sizeof (thetime))
  130. {
  131. __set_errno (EIO);
  132. return -1;
  133. }
  134. thetime = ntohl (thetime);
  135. timep->tv_sec = thetime - TOFFSET;
  136. timep->tv_usec = 0;
  137. return 0;
  138. }