1 /* $OpenBSD: src/usr.sbin/ntpd/ntp_msg.c,v 1.11 2004/10/22 21:24:20 henning Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/param.h>
30 ntp_getmsg(char *p, ssize_t len, struct ntp_msg *msg)
32 if (len != NTP_MSGSIZE_NOAUTH && len != NTP_MSGSIZE) {
33 log_warnx("malformed packet received");
37 memcpy(&msg->status, p, sizeof(msg->status));
38 p += sizeof(msg->status);
39 memcpy(&msg->stratum, p, sizeof(msg->stratum));
40 p += sizeof(msg->stratum);
41 memcpy(&msg->ppoll, p, sizeof(msg->ppoll));
42 p += sizeof(msg->ppoll);
43 memcpy(&msg->precision, p, sizeof(msg->precision));
44 p += sizeof(msg->precision);
45 memcpy(&msg->rootdelay.int_part, p, sizeof(msg->rootdelay.int_part));
46 p += sizeof(msg->rootdelay.int_part);
47 memcpy(&msg->rootdelay.fraction, p, sizeof(msg->rootdelay.fraction));
48 p += sizeof(msg->rootdelay.fraction);
49 memcpy(&msg->dispersion.int_part, p, sizeof(msg->dispersion.int_part));
50 p += sizeof(msg->dispersion.int_part);
51 memcpy(&msg->dispersion.fraction, p, sizeof(msg->dispersion.fraction));
52 p += sizeof(msg->dispersion.fraction);
53 memcpy(&msg->refid, p, sizeof(msg->refid));
54 p += sizeof(msg->refid);
55 memcpy(&msg->reftime.int_part, p, sizeof(msg->reftime.int_part));
56 p += sizeof(msg->reftime.int_part);
57 memcpy(&msg->reftime.fraction, p, sizeof(msg->reftime.fraction));
58 p += sizeof(msg->reftime.fraction);
59 memcpy(&msg->orgtime.int_part, p, sizeof(msg->orgtime.int_part));
60 p += sizeof(msg->orgtime.int_part);
61 memcpy(&msg->orgtime.fraction, p, sizeof(msg->orgtime.fraction));
62 p += sizeof(msg->orgtime.fraction);
63 memcpy(&msg->rectime.int_part, p, sizeof(msg->rectime.int_part));
64 p += sizeof(msg->rectime.int_part);
65 memcpy(&msg->rectime.fraction, p, sizeof(msg->rectime.fraction));
66 p += sizeof(msg->rectime.fraction);
67 memcpy(&msg->xmttime.int_part, p, sizeof(msg->xmttime.int_part));
68 p += sizeof(msg->xmttime.int_part);
69 memcpy(&msg->xmttime.fraction, p, sizeof(msg->xmttime.fraction));
70 p += sizeof(msg->xmttime.fraction);
76 ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len,
79 char buf[NTP_MSGSIZE];
84 memcpy(p, &msg->status, sizeof(msg->status));
85 p += sizeof(msg->status);
86 memcpy(p, &msg->stratum, sizeof(msg->stratum));
87 p += sizeof(msg->stratum);
88 memcpy(p, &msg->ppoll, sizeof(msg->ppoll));
89 p += sizeof(msg->ppoll);
90 memcpy(p, &msg->precision, sizeof(msg->precision));
91 p += sizeof(msg->precision);
92 memcpy(p, &msg->rootdelay.int_part, sizeof(msg->rootdelay.int_part));
93 p += sizeof(msg->rootdelay.int_part);
94 memcpy(p, &msg->rootdelay.fraction, sizeof(msg->rootdelay.fraction));
95 p += sizeof(msg->rootdelay.fraction);
96 memcpy(p, &msg->dispersion.int_part, sizeof(msg->dispersion.int_part));
97 p += sizeof(msg->dispersion.int_part);
98 memcpy(p, &msg->dispersion.fraction, sizeof(msg->dispersion.fraction));
99 p += sizeof(msg->dispersion.fraction);
100 memcpy(p, &msg->refid, sizeof(msg->refid));
101 p += sizeof(msg->refid);
102 memcpy(p, &msg->reftime.int_part, sizeof(msg->reftime.int_part));
103 p += sizeof(msg->reftime.int_part);
104 memcpy(p, &msg->reftime.fraction, sizeof(msg->reftime.fraction));
105 p += sizeof(msg->reftime.fraction);
106 memcpy(p, &msg->orgtime.int_part, sizeof(msg->orgtime.int_part));
107 p += sizeof(msg->orgtime.int_part);
108 memcpy(p, &msg->orgtime.fraction, sizeof(msg->orgtime.fraction));
109 p += sizeof(msg->orgtime.fraction);
110 memcpy(p, &msg->rectime.int_part, sizeof(msg->rectime.int_part));
111 p += sizeof(msg->rectime.int_part);
112 memcpy(p, &msg->rectime.fraction, sizeof(msg->rectime.fraction));
113 p += sizeof(msg->rectime.fraction);
114 memcpy(p, &msg->xmttime.int_part, sizeof(msg->xmttime.int_part));
115 p += sizeof(msg->xmttime.int_part);
116 memcpy(p, &msg->xmttime.fraction, sizeof(msg->xmttime.fraction));
117 p += sizeof(msg->xmttime.fraction);
124 if (sendto(fd, &buf, len, 0, sa, sa_len) != len) {
125 if (errno == ENOBUFS || errno == EHOSTUNREACH ||
126 errno == ENETDOWN || errno == EHOSTDOWN) {
127 /* logging is futile */