/bin/date/netdate.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 183 lines · 131 code · 17 blank · 35 comment · 30 complexity · 987d73f539466bc6636b93c8ac390cb1 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #if 0
  30. #ifndef lint
  31. static char sccsid[] = "@(#)netdate.c 8.1 (Berkeley) 5/31/93";
  32. #endif /* not lint */
  33. #endif
  34. #include <sys/cdefs.h>
  35. __FBSDID("$FreeBSD$");
  36. #include <sys/param.h>
  37. #include <sys/time.h>
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #include <netdb.h>
  41. #define TSPTYPES
  42. #include <protocols/timed.h>
  43. #include <err.h>
  44. #include <errno.h>
  45. #include <string.h>
  46. #include <unistd.h>
  47. #include "extern.h"
  48. #define WAITACK 2 /* seconds */
  49. #define WAITDATEACK 5 /* seconds */
  50. extern int retval;
  51. /*
  52. * Set the date in the machines controlled by timedaemons by communicating the
  53. * new date to the local timedaemon. If the timedaemon is in the master state,
  54. * it performs the correction on all slaves. If it is in the slave state, it
  55. * notifies the master that a correction is needed.
  56. * Returns 0 on success. Returns > 0 on failure, setting retval to 2;
  57. */
  58. int
  59. netsettime(time_t tval)
  60. {
  61. struct timeval tout;
  62. struct servent *sp;
  63. struct tsp msg;
  64. struct sockaddr_in lsin, dest, from;
  65. fd_set ready;
  66. long waittime;
  67. int s, port, timed_ack, found, lerr;
  68. socklen_t length;
  69. char hostname[MAXHOSTNAMELEN];
  70. if ((sp = getservbyname("timed", "udp")) == NULL) {
  71. warnx("timed/udp: unknown service");
  72. return (retval = 2);
  73. }
  74. dest.sin_port = sp->s_port;
  75. dest.sin_family = AF_INET;
  76. dest.sin_addr.s_addr = htonl((u_long)INADDR_ANY);
  77. s = socket(AF_INET, SOCK_DGRAM, 0);
  78. if (s < 0) {
  79. if (errno != EPROTONOSUPPORT)
  80. warn("timed");
  81. return (retval = 2);
  82. }
  83. memset(&lsin, 0, sizeof(lsin));
  84. lsin.sin_family = AF_INET;
  85. for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
  86. lsin.sin_port = htons((u_short)port);
  87. if (bind(s, (struct sockaddr *)&lsin, sizeof(lsin)) >= 0)
  88. break;
  89. if (errno == EADDRINUSE)
  90. continue;
  91. if (errno != EADDRNOTAVAIL)
  92. warn("bind");
  93. goto bad;
  94. }
  95. if (port == IPPORT_RESERVED / 2) {
  96. warnx("all ports in use");
  97. goto bad;
  98. }
  99. memset(&msg, 0, sizeof(msg));
  100. msg.tsp_type = TSP_SETDATE;
  101. msg.tsp_vers = TSPVERSION;
  102. if (gethostname(hostname, sizeof(hostname))) {
  103. warn("gethostname");
  104. goto bad;
  105. }
  106. (void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
  107. msg.tsp_seq = htons((u_short)0);
  108. msg.tsp_time.tv_sec = htonl((u_long)tval);
  109. msg.tsp_time.tv_usec = htonl((u_long)0);
  110. length = sizeof(struct sockaddr_in);
  111. if (connect(s, (struct sockaddr *)&dest, length) < 0) {
  112. warn("connect");
  113. goto bad;
  114. }
  115. if (send(s, (char *)&msg, sizeof(struct tsp), 0) < 0) {
  116. if (errno != ECONNREFUSED)
  117. warn("send");
  118. goto bad;
  119. }
  120. timed_ack = -1;
  121. waittime = WAITACK;
  122. loop:
  123. tout.tv_sec = waittime;
  124. tout.tv_usec = 0;
  125. FD_ZERO(&ready);
  126. FD_SET(s, &ready);
  127. found = select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout);
  128. length = sizeof(lerr);
  129. if (!getsockopt(s,
  130. SOL_SOCKET, SO_ERROR, (char *)&lerr, &length) && lerr) {
  131. if (lerr != ECONNREFUSED)
  132. warnc(lerr, "send (delayed error)");
  133. goto bad;
  134. }
  135. if (found > 0 && FD_ISSET(s, &ready)) {
  136. length = sizeof(struct sockaddr_in);
  137. if (recvfrom(s, &msg, sizeof(struct tsp), 0,
  138. (struct sockaddr *)&from, &length) < 0) {
  139. if (errno != ECONNREFUSED)
  140. warn("recvfrom");
  141. goto bad;
  142. }
  143. msg.tsp_seq = ntohs(msg.tsp_seq);
  144. msg.tsp_time.tv_sec = ntohl(msg.tsp_time.tv_sec);
  145. msg.tsp_time.tv_usec = ntohl(msg.tsp_time.tv_usec);
  146. switch (msg.tsp_type) {
  147. case TSP_ACK:
  148. timed_ack = TSP_ACK;
  149. waittime = WAITDATEACK;
  150. goto loop;
  151. case TSP_DATEACK:
  152. (void)close(s);
  153. return (0);
  154. default:
  155. warnx("wrong ack received from timed: %s",
  156. tsptype[msg.tsp_type]);
  157. timed_ack = -1;
  158. break;
  159. }
  160. }
  161. if (timed_ack == -1)
  162. warnx("can't reach time daemon, time set locally");
  163. bad:
  164. (void)close(s);
  165. return (retval = 2);
  166. }