Merge from vendor branch GCC:
[dragonfly.git] / contrib / ntpd / ntp_msg.c
1 /*      $OpenBSD: src/usr.sbin/ntpd/ntp_msg.c,v 1.14 2004/12/14 06:27:13 dtucker Exp $ */
2
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
6  *
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.
10  *
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.
18  */
19
20 #include <sys/param.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 #include "ntpd.h"
27 #include "ntp.h"
28
29 int
30 ntp_getmsg(char *p, ssize_t len, struct ntp_msg *msg)
31 {
32         if (len != NTP_MSGSIZE_NOAUTH && len != NTP_MSGSIZE) {
33                 log_warnx("malformed packet received");
34                 return (-1);
35         }
36
37 #define copyin(f,p)     memcpy(&(f), (p), sizeof(f)); (p) += sizeof(f)
38
39         copyin(msg->status, p);
40         copyin(msg->stratum, p);
41         copyin(msg->ppoll, p);
42         copyin(msg->precision, p);
43         copyin(msg->rootdelay.int_parts, p);
44         copyin(msg->rootdelay.fractions, p);
45         copyin(msg->dispersion.int_parts, p);
46         copyin(msg->dispersion.fractions, p);
47         copyin(msg->refid, p);
48         copyin(msg->reftime.int_partl, p);
49         copyin(msg->reftime.fractionl, p);
50         copyin(msg->orgtime.int_partl, p);
51         copyin(msg->orgtime.fractionl, p);
52         copyin(msg->rectime.int_partl, p);
53         copyin(msg->rectime.fractionl, p);
54         copyin(msg->xmttime.int_partl, p);
55         copyin(msg->xmttime.fractionl, p);
56
57         return (0);
58 }
59
60 int
61 ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg, ssize_t len,
62     int auth)
63 {
64         char             buf[NTP_MSGSIZE];
65         char            *p = buf;
66         socklen_t       sa_len;
67
68 #define copyout(p,f)    memcpy((p), &(f), sizeof(f)); p += sizeof(f)
69
70         copyout(p, msg->status);
71         copyout(p, msg->stratum);
72         copyout(p, msg->ppoll);
73         copyout(p, msg->precision);
74         copyout(p, msg->rootdelay.int_parts);
75         copyout(p, msg->rootdelay.fractions);
76         copyout(p, msg->dispersion.int_parts);
77         copyout(p, msg->dispersion.fractions);
78         copyout(p, msg->refid);
79         copyout(p, msg->reftime.int_partl);
80         copyout(p, msg->reftime.fractionl);
81         copyout(p, msg->orgtime.int_partl);
82         copyout(p, msg->orgtime.fractionl);
83         copyout(p, msg->rectime.int_partl);
84         copyout(p, msg->rectime.fractionl);
85         copyout(p, msg->xmttime.int_partl);
86         copyout(p, msg->xmttime.fractionl);
87
88         if (sa != NULL)
89                 sa_len = SA_LEN(sa);
90         else
91                 sa_len = 0;
92
93         if (sendto(fd, &buf, len, 0, sa, sa_len) != len) {
94                 if (errno == ENOBUFS || errno == EHOSTUNREACH ||
95                     errno == ENETDOWN || errno == EHOSTDOWN) {
96                         /* logging is futile */
97                         return (-1);
98                 }
99                 log_warn("sendto");
100                 return (-1);
101         }
102
103         return (0);
104 }