GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ntpd/ntp_msg.c Lines: 0 19 0.0 %
Date: 2017-11-07 Branches: 0 16 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ntp_msg.c,v 1.22 2016/09/03 11:52:06 reyk 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 USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/types.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <unistd.h>
24
#include <errno.h>
25
26
#include "ntpd.h"
27
28
int
29
ntp_getmsg(struct sockaddr *sa, char *p, ssize_t len, struct ntp_msg *msg)
30
{
31
	if (len != NTP_MSGSIZE_NOAUTH && len != NTP_MSGSIZE) {
32
		log_debug("malformed packet received from %s",
33
		    log_sockaddr(sa));
34
		return (-1);
35
	}
36
37
	memcpy(msg, p, sizeof(*msg));
38
39
	return (0);
40
}
41
42
int
43
ntp_sendmsg(int fd, struct sockaddr *sa, struct ntp_msg *msg)
44
{
45
	socklen_t	sa_len;
46
	ssize_t		n;
47
48
	if (sa != NULL)
49
		sa_len = SA_LEN(sa);
50
	else
51
		sa_len = 0;
52
53
	n = sendto(fd, msg, sizeof(*msg), 0, sa, sa_len);
54
	if (n == -1) {
55
		if (errno == ENOBUFS || errno == EHOSTUNREACH ||
56
		    errno == ENETDOWN || errno == EHOSTDOWN) {
57
			/* logging is futile */
58
			return (-1);
59
		}
60
		log_warn("sendto");
61
		return (-1);
62
	}
63
64
	if (n != sizeof(*msg)) {
65
		log_warnx("ntp_sendmsg: only %zd of %zu bytes sent", n,
66
		    sizeof(*msg));
67
		return (-1);
68
	}
69
70
	return (0);
71
}