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.

71 lines
1.8 KiB

19 years ago
  1. /* $OpenBSD: ntp_msg.c,v 1.22 2016/09/03 11:52:06 reyk 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 USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.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)
  37. {
  38. socklen_t sa_len;
  39. ssize_t n;
  40. if (sa != NULL)
  41. sa_len = SA_LEN(sa);
  42. else
  43. sa_len = 0;
  44. n = sendto(fd, msg, sizeof(*msg), 0, sa, sa_len);
  45. if (n == -1) {
  46. if (errno == ENOBUFS || errno == EHOSTUNREACH ||
  47. errno == ENETDOWN || errno == EHOSTDOWN) {
  48. /* logging is futile */
  49. return (-1);
  50. }
  51. log_warn("sendto");
  52. return (-1);
  53. }
  54. if (n != sizeof(*msg)) {
  55. log_warnx("ntp_sendmsg: only %zd of %zu bytes sent", n,
  56. sizeof(*msg));
  57. return (-1);
  58. }
  59. return (0);
  60. }