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.

104 lines
3.0 KiB

20 years ago
20 years ago
  1. /* $OpenBSD: ntp_msg.c,v 1.14 2004/12/14 06:27:13 dtucker 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. #include "ntp.h"
  25. int
  26. ntp_getmsg(char *p, ssize_t len, struct ntp_msg *msg)
  27. {
  28. if (len != NTP_MSGSIZE_NOAUTH && len != NTP_MSGSIZE) {
  29. log_warnx("malformed packet received");
  30. return (-1);
  31. }
  32. #define copyin(f,p) memcpy(&(f), (p), sizeof(f)); (p) += sizeof(f)
  33. copyin(msg->status, p);
  34. copyin(msg->stratum, p);
  35. copyin(msg->ppoll, p);
  36. copyin(msg->precision, p);
  37. copyin(msg->rootdelay.int_parts, p);
  38. copyin(msg->rootdelay.fractions, p);
  39. copyin(msg->dispersion.int_parts, p);
  40. copyin(msg->dispersion.fractions, p);
  41. copyin(msg->refid, p);
  42. copyin(msg->reftime.int_partl, p);
  43. copyin(msg->reftime.fractionl, p);
  44. copyin(msg->orgtime.int_partl, p);
  45. copyin(msg->orgtime.fractionl, p);
  46. copyin(msg->rectime.int_partl, p);
  47. copyin(msg->rectime.fractionl, p);
  48. copyin(msg->xmttime.int_partl, p);
  49. copyin(msg->xmttime.fractionl, p);
  50. return (0);
  51. }
  52. int
  53. ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len,
  54. int auth)
  55. {
  56. char buf[NTP_MSGSIZE];
  57. char *p = buf;
  58. socklen_t sa_len;
  59. #define copyout(p,f) memcpy((p), &(f), sizeof(f)); p += sizeof(f)
  60. copyout(p, msg->status);
  61. copyout(p, msg->stratum);
  62. copyout(p, msg->ppoll);
  63. copyout(p, msg->precision);
  64. copyout(p, msg->rootdelay.int_parts);
  65. copyout(p, msg->rootdelay.fractions);
  66. copyout(p, msg->dispersion.int_parts);
  67. copyout(p, msg->dispersion.fractions);
  68. copyout(p, msg->refid);
  69. copyout(p, msg->reftime.int_partl);
  70. copyout(p, msg->reftime.fractionl);
  71. copyout(p, msg->orgtime.int_partl);
  72. copyout(p, msg->orgtime.fractionl);
  73. copyout(p, msg->rectime.int_partl);
  74. copyout(p, msg->rectime.fractionl);
  75. copyout(p, msg->xmttime.int_partl);
  76. copyout(p, msg->xmttime.fractionl);
  77. if (sa != NULL)
  78. sa_len = SA_LEN(sa);
  79. else
  80. sa_len = 0;
  81. if (sendto(fd, &buf, len, 0, sa, sa_len) != len) {
  82. if (errno == ENOBUFS || errno == EHOSTUNREACH ||
  83. errno == ENETDOWN || errno == EHOSTDOWN) {
  84. /* logging is futile */
  85. return (-1);
  86. }
  87. log_warn("sendto");
  88. return (-1);
  89. }
  90. return (0);
  91. }