Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

20 years ago
  1. /* $OpenBSD: ntp_msg.c,v 1.18 2007/10/19 15:53:57 otto Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  15. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  16. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/param.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include "ntpd.h"
  24. int
  25. ntp_getmsg(struct sockaddr *sa, char *p, ssize_t len, struct ntp_msg *msg)
  26. {
  27. if (len != NTP_MSGSIZE_NOAUTH && len != NTP_MSGSIZE) {
  28. log_debug("malformed packet received from %s",
  29. log_sockaddr(sa));
  30. return (-1);
  31. }
  32. memcpy(msg, p, sizeof(*msg));
  33. return (0);
  34. }
  35. int
  36. ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len,
  37. int auth)
  38. {
  39. socklen_t sa_len;
  40. ssize_t n;
  41. if (sa != NULL)
  42. sa_len = SA_LEN(sa);
  43. else
  44. sa_len = 0;
  45. n = sendto(fd, msg, sizeof(*msg), 0, sa, sa_len);
  46. if (n == -1) {
  47. if (errno == ENOBUFS || errno == EHOSTUNREACH ||
  48. errno == ENETDOWN || errno == EHOSTDOWN) {
  49. /* logging is futile */
  50. return (-1);
  51. }
  52. log_warn("sendto");
  53. return (-1);
  54. }
  55. if (n != sizeof(*msg)) {
  56. log_warnx("ntp_sendmsg: only %ld of %ld bytes sent", n,
  57. sizeof(*msg));
  58. return (-1);
  59. }
  60. return (0);
  61. }