summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2017-02-13 19:15:40 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2017-02-13 19:15:40 +0000
commit5907726959c605536450f146cdd3051a7c07ce8b (patch)
tree4064c44a2e6be99f2354e0c0b5e529f9ed5678bb
parent811cf9b90880cad1317d38af0ee02bb8a610344f (diff)
Switch from old errwarn.c logging to shiny new log.[ch].
ok benno@
-rw-r--r--usr.sbin/dhcrelay/Makefile4
-rw-r--r--usr.sbin/dhcrelay/bpf.c27
-rw-r--r--usr.sbin/dhcrelay/dhcrelay.c105
-rw-r--r--usr.sbin/dhcrelay/dispatch.c23
-rw-r--r--usr.sbin/dhcrelay/log.c199
-rw-r--r--usr.sbin/dhcrelay/log.h46
-rw-r--r--usr.sbin/dhcrelay/packet.c13
7 files changed, 333 insertions, 84 deletions
diff --git a/usr.sbin/dhcrelay/Makefile b/usr.sbin/dhcrelay/Makefile
index 7b58a21ec23..13f09900ccf 100644
--- a/usr.sbin/dhcrelay/Makefile
+++ b/usr.sbin/dhcrelay/Makefile
@@ -1,8 +1,8 @@
-# $OpenBSD: Makefile,v 1.2 2004/04/12 21:30:30 henning Exp $
+# $OpenBSD: Makefile,v 1.3 2017/02/13 19:15:39 krw Exp $
.include <bsd.own.mk>
-SRCS= dhcrelay.c dispatch.c errwarn.c bpf.c packet.c
+SRCS= dhcrelay.c dispatch.c log.c bpf.c packet.c
PROG= dhcrelay
MAN= dhcrelay.8
diff --git a/usr.sbin/dhcrelay/bpf.c b/usr.sbin/dhcrelay/bpf.c
index 493b425a463..6ca2b447d2a 100644
--- a/usr.sbin/dhcrelay/bpf.c
+++ b/usr.sbin/dhcrelay/bpf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bpf.c,v 1.14 2016/12/12 15:41:05 rzalamena Exp $ */
+/* $OpenBSD: bpf.c,v 1.15 2017/02/13 19:15:39 krw Exp $ */
/* BPF socket interface code, originally contributed by Archie Cobbs. */
@@ -59,6 +59,7 @@
#include "dhcp.h"
#include "dhcpd.h"
+#include "log.h"
/*
* Called by get_interface_list for each interface that's discovered.
@@ -72,11 +73,11 @@ if_register_bpf(struct interface_info *info)
/* Open the BPF device */
if ((sock = open("/dev/bpf0", O_RDWR)) == -1)
- error("Can't open bpf device: %m");
+ fatalx("Can't open bpf device: %m");
/* Set the BPF device to point at this interface. */
if (ioctl(sock, BIOCSETIF, &info->ifr) == -1)
- error("Can't attach interface %s to bpf device: %m",
+ fatalx("Can't attach interface %s to bpf device: %m",
info->name);
return (sock);
@@ -268,11 +269,11 @@ if_register_receive(struct interface_info *info, int isserver)
/* Make sure the BPF version is in range... */
if (ioctl(info->rfdesc, BIOCVERSION, &v) == -1)
- error("Can't get BPF version: %m");
+ fatalx("Can't get BPF version: %m");
if (v.bv_major != BPF_MAJOR_VERSION ||
v.bv_minor < BPF_MINOR_VERSION)
- error("Kernel BPF version out of range - recompile dhcpd!");
+ fatalx("Kernel BPF version out of range - recompile dhcpd!");
/*
* Set immediate mode so that reads return as soon as a packet
@@ -280,19 +281,19 @@ if_register_receive(struct interface_info *info, int isserver)
* with packets.
*/
if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) == -1)
- error("Can't set immediate mode on bpf device: %m");
+ fatalx("Can't set immediate mode on bpf device: %m");
/* make sure kernel fills in the source ethernet address */
if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) == -1)
- error("Can't set header complete flag on bpf device: %m");
+ fatalx("Can't set header complete flag on bpf device: %m");
/* Get the required BPF buffer length from the kernel. */
if (ioctl(info->rfdesc, BIOCGBLEN, &sz) == -1)
- error("Can't get bpf buffer length: %m");
+ fatalx("Can't get bpf buffer length: %m");
info->rbuf_max = sz;
info->rbuf = malloc(info->rbuf_max);
if (!info->rbuf)
- error("Can't allocate %lu bytes for bpf input buffer.",
+ fatalx("Can't allocate %lu bytes for bpf input buffer.",
(unsigned long)info->rbuf_max);
info->rbuf_offset = 0;
info->rbuf_len = 0;
@@ -309,7 +310,7 @@ if_register_receive(struct interface_info *info, int isserver)
p.bf_insns = dhcp_bpf_filter;
}
if (ioctl(info->rfdesc, BIOCSETF, &p) == -1)
- error("Can't install packet filter program: %m");
+ fatalx("Can't install packet filter program: %m");
/* Set up the bpf write filter program structure. */
if (isserver) {
@@ -321,11 +322,11 @@ if_register_receive(struct interface_info *info, int isserver)
}
if (ioctl(info->rfdesc, BIOCSETWF, &p) == -1)
- error("Can't install write filter program: %m");
+ fatalx("Can't install write filter program: %m");
/* make sure these settings cannot be changed after dropping privs */
if (ioctl(info->rfdesc, BIOCLOCK) == -1)
- error("Failed to lock bpf descriptor: %m");
+ fatalx("Failed to lock bpf descriptor: %m");
}
ssize_t
@@ -357,7 +358,7 @@ send_packet(struct interface_info *interface,
result = writev(interface->wfdesc, iov, 2);
done:
if (result == -1)
- warning("send_packet: %m");
+ log_warnx("send_packet: %m");
return (result);
}
diff --git a/usr.sbin/dhcrelay/dhcrelay.c b/usr.sbin/dhcrelay/dhcrelay.c
index 1072d219c1c..5048ed828c0 100644
--- a/usr.sbin/dhcrelay/dhcrelay.c
+++ b/usr.sbin/dhcrelay/dhcrelay.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhcrelay.c,v 1.55 2016/12/16 18:38:39 rzalamena Exp $ */
+/* $OpenBSD: dhcrelay.c,v 1.56 2017/02/13 19:15:39 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@cvs.openbsd.org>
@@ -61,6 +61,7 @@
#include "dhcp.h"
#include "dhcpd.h"
+#include "log.h"
void usage(void);
int rdaemon(int);
@@ -107,7 +108,6 @@ int
main(int argc, char *argv[])
{
int ch, devnull = -1, daemonize, opt, rdomain;
- extern char *__progname;
struct server_list *sp = NULL;
struct passwd *pw;
struct sockaddr_in laddr;
@@ -115,9 +115,8 @@ main(int argc, char *argv[])
daemonize = 1;
- /* Initially, log errors to stderr as well as to syslogd. */
- openlog(__progname, LOG_NDELAY, DHCPD_LOG_FACILITY);
- setlogmask(LOG_UPTO(LOG_INFO));
+ log_init(1, LOG_DAEMON); /* log to stderr until daemonized */
+ log_setverbose(1);
while ((ch = getopt(argc, argv, "aC:di:oR:r")) != -1) {
switch (ch) {
@@ -133,7 +132,7 @@ main(int argc, char *argv[])
interfaces = get_interface(optarg, got_one, 0);
if (interfaces == NULL)
- error("interface '%s' not found", optarg);
+ fatalx("interface '%s' not found", optarg);
break;
case 'o':
/* add the relay agent information option */
@@ -159,7 +158,7 @@ main(int argc, char *argv[])
usage();
if (rai_remote != NULL && rai_circuit == NULL)
- error("you must specify a circuit-id with a remote-id");
+ fatalx("you must specify a circuit-id with a remote-id");
/* Validate that we have space for all suboptions. */
if (rai_circuit != NULL) {
@@ -168,7 +167,7 @@ main(int argc, char *argv[])
optslen += 2 + strlen(rai_remote);
if (optslen > DHCP_OPTION_MAXLEN)
- error("relay agent information is too long");
+ fatalx("relay agent information is too long");
}
while (argc > 0) {
@@ -176,14 +175,14 @@ main(int argc, char *argv[])
struct in_addr ia, *iap = NULL;
if ((sp = calloc(1, sizeof(*sp))) == NULL)
- error("calloc");
+ fatalx("calloc");
if ((sp->intf = get_interface(argv[0], got_one, 1)) != NULL) {
if (drm == DRM_LAYER3)
- error("don't mix interfaces with hosts");
+ fatalx("don't mix interfaces with hosts");
if (sp->intf->hw_address.htype == HTYPE_IPSEC_TUNNEL)
- error("can't use IPSec with layer 2");
+ fatalx("can't use IPSec with layer 2");
sp->next = servers;
servers = sp;
@@ -199,13 +198,13 @@ main(int argc, char *argv[])
else {
he = gethostbyname(argv[0]);
if (!he)
- warning("%s: host unknown", argv[0]);
+ log_warnx("%s: host unknown", argv[0]);
else
iap = ((struct in_addr *)he->h_addr_list[0]);
}
if (iap) {
if (drm == DRM_LAYER2)
- error("don't mix interfaces with hosts");
+ fatalx("don't mix interfaces with hosts");
drm = DRM_LAYER3;
sp->next = servers;
@@ -221,16 +220,16 @@ main(int argc, char *argv[])
if (daemonize) {
devnull = open(_PATH_DEVNULL, O_RDWR, 0);
if (devnull == -1)
- error("open(%s): %m", _PATH_DEVNULL);
+ fatalx("open(%s): %m", _PATH_DEVNULL);
}
if (interfaces == NULL)
- error("no interface given");
+ fatalx("no interface given");
/* We need an address for running layer 3 mode. */
if (drm == DRM_LAYER3 &&
(interfaces->hw_address.htype != HTYPE_IPSEC_TUNNEL &&
interfaces->primary_address.s_addr == 0))
- error("interface '%s' does not have an address",
+ fatalx("interface '%s' does not have an address",
interfaces->name);
/* Default DHCP/BOOTP ports. */
@@ -262,19 +261,19 @@ main(int argc, char *argv[])
sp->to.sin_len = sizeof sp->to;
sp->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sp->fd == -1)
- error("socket: %m");
+ fatalx("socket: %m");
opt = 1;
if (setsockopt(sp->fd, SOL_SOCKET, SO_REUSEPORT,
&opt, sizeof(opt)) == -1)
- error("setsockopt: %m");
+ fatalx("setsockopt: %m");
if (setsockopt(sp->fd, SOL_SOCKET, SO_RTABLE, &rdomain,
sizeof(rdomain)) == -1)
- error("setsockopt: %m");
+ fatalx("setsockopt: %m");
if (bind(sp->fd, (struct sockaddr *)&laddr, sizeof laddr) == -1)
- error("bind: %m");
+ fatalx("bind: %m");
if (connect(sp->fd, (struct sockaddr *)&sp->to,
sizeof sp->to) == -1)
- error("connect: %m");
+ fatalx("connect: %m");
add_protocol("server", sp->fd, got_response, sp);
}
@@ -283,17 +282,17 @@ main(int argc, char *argv[])
laddr.sin_addr.s_addr = INADDR_ANY;
server_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (server_fd == -1)
- error("socket: %m");
+ fatalx("socket: %m");
opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT,
&opt, sizeof(opt)) == -1)
- error("setsockopt: %m");
+ fatalx("setsockopt: %m");
if (setsockopt(server_fd, SOL_SOCKET, SO_RTABLE, &rdomain,
sizeof(rdomain)) == -1)
- error("setsockopt: %m");
+ fatalx("setsockopt: %m");
if (bind(server_fd, (struct sockaddr *)&laddr,
sizeof(laddr)) == -1)
- error("bind: %m");
+ fatalx("bind: %m");
}
tzset();
@@ -305,24 +304,26 @@ main(int argc, char *argv[])
bootp_packet_handler = l2relay;
if ((pw = getpwnam("_dhcp")) == NULL)
- error("user \"_dhcp\" not found");
+ fatalx("user \"_dhcp\" not found");
if (chroot(_PATH_VAREMPTY) == -1)
- error("chroot: %m");
+ fatalx("chroot: %m");
if (chdir("/") == -1)
- error("chdir(\"/\"): %m");
+ fatalx("chdir(\"/\"): %m");
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
- error("can't drop privileges: %m");
+ fatalx("can't drop privileges: %m");
if (daemonize) {
if (rdaemon(devnull) == -1)
- error("rdaemon: %m");
+ fatalx("rdaemon: %m");
log_perror = 0;
}
+ log_init(0, LOG_DAEMON); /* stop loggoing to stderr */
+ log_setverbose(0);
if (pledge("stdio route", NULL) == -1)
- error("pledge");
+ fatalx("pledge");
dispatch();
/* not reached */
@@ -338,7 +339,7 @@ relay(struct interface_info *ip, struct dhcp_packet *packet, int length,
struct sockaddr_in to;
if (packet->hlen > sizeof packet->chaddr) {
- note("Discarding packet with invalid hlen.");
+ log_info("Discarding packet with invalid hlen.");
return;
}
@@ -379,7 +380,7 @@ relay(struct interface_info *ip, struct dhcp_packet *packet, int length,
relay_agentinfo(pc, interfaces, packet->op);
if ((length = relay_agentinfo_remove(pc, packet,
length)) == -1) {
- note("ignoring BOOTREPLY with invalid "
+ log_info("ignoring BOOTREPLY with invalid "
"relay agent information");
return;
}
@@ -397,19 +398,19 @@ relay(struct interface_info *ip, struct dhcp_packet *packet, int length,
ss2sin(&pc->pc_src)->sin_addr = interfaces->primary_address;
if (send_packet(interfaces, packet, length, pc) != -1)
- debug("forwarded BOOTREPLY for %s to %s",
+ log_debug("forwarded BOOTREPLY for %s to %s",
print_hw_addr(packet->htype, packet->hlen,
packet->chaddr), inet_ntoa(to.sin_addr));
return;
}
if (ip == NULL) {
- note("ignoring non BOOTREPLY from server");
+ log_info("ignoring non BOOTREPLY from server");
return;
}
if (packet->hops > 16) {
- note("ignoring BOOTREQUEST with hop count of %d",
+ log_info("ignoring BOOTREQUEST with hop count of %d",
packet->hops);
return;
}
@@ -426,7 +427,7 @@ relay(struct interface_info *ip, struct dhcp_packet *packet, int length,
relay_agentinfo(pc, interfaces, packet->op);
if ((length = relay_agentinfo_append(pc, packet, length)) == -1) {
- note("ignoring BOOTREQUEST with invalid "
+ log_info("ignoring BOOTREQUEST with invalid "
"relay agent information");
return;
}
@@ -435,7 +436,7 @@ relay(struct interface_info *ip, struct dhcp_packet *packet, int length,
servers. */
for (sp = servers; sp; sp = sp->next) {
if (send(sp->fd, packet, length, 0) != -1) {
- debug("forwarded BOOTREQUEST for %s to %s",
+ log_debug("forwarded BOOTREQUEST for %s to %s",
print_hw_addr(packet->htype, packet->hlen,
packet->chaddr), inet_ntoa(sp->to.sin_addr));
}
@@ -533,7 +534,7 @@ got_response(struct protocol *l)
* Ignore ECONNREFUSED as too many dhcp servers send a bogus
* icmp unreach for every request.
*/
- warning("recv failed for %s: %m",
+ log_warnx("recv failed for %s: %m",
inet_ntoa(sp->to.sin_addr));
return;
}
@@ -544,7 +545,7 @@ got_response(struct protocol *l)
return;
if (result < BOOTP_MIN_LEN) {
- note("Discarding packet with invalid size.");
+ log_info("Discarding packet with invalid size.");
return;
}
@@ -636,7 +637,7 @@ relay_agentinfo_cmp(struct packet_ctx *pc, uint8_t *p, int plen)
default:
/* Unmatched type */
- note("unmatched relay info %d", *p);
+ log_info("unmatched relay info %d", *p);
return (0);
}
}
@@ -657,7 +658,7 @@ relay_agentinfo_append(struct packet_ctx *pc, struct dhcp_packet *dp,
startp = (uint8_t *)dp;
p = (uint8_t *)&dp->options;
if (memcmp(p, DHCP_OPTIONS_COOKIE, DHCP_OPTIONS_COOKIE_LEN)) {
- note("invalid dhcp options cookie");
+ log_info("invalid dhcp options cookie");
return (-1);
}
@@ -674,7 +675,7 @@ relay_agentinfo_append(struct packet_ctx *pc, struct dhcp_packet *dp,
optlen = p[1] + DHCP_OPTION_HDR_LEN;
if ((i + optlen) > opttotal) {
- note("truncated dhcp options");
+ log_info("truncated dhcp options");
return (-1);
}
@@ -710,7 +711,7 @@ relay_agentinfo_append(struct packet_ctx *pc, struct dhcp_packet *dp,
/* Check if we have enough space for the new options. */
if (neededlen > maxlen) {
- warning("no space for relay agent info");
+ log_warnx("no space for relay agent info");
return (newtotal);
}
@@ -777,7 +778,7 @@ relay_agentinfo_remove(struct packet_ctx *pc, struct dhcp_packet *dp,
startp = (uint8_t *)dp;
p = (uint8_t *)&dp->options;
if (memcmp(p, DHCP_OPTIONS_COOKIE, DHCP_OPTIONS_COOKIE_LEN)) {
- note("invalid dhcp options cookie");
+ log_info("invalid dhcp options cookie");
return (-1);
}
@@ -795,7 +796,7 @@ relay_agentinfo_remove(struct packet_ctx *pc, struct dhcp_packet *dp,
optlen = p[1] + DHCP_OPTION_HDR_LEN;
if ((i + optlen) > opttotal) {
- note("truncated dhcp options");
+ log_info("truncated dhcp options");
return (-1);
}
@@ -860,7 +861,7 @@ get_rdomain(char *name)
struct ifreq ifr;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- error("get_rdomain socket: %m");
+ fatalx("get_rdomain socket: %m");
bzero(&ifr, sizeof(ifr));
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
@@ -879,7 +880,7 @@ l2relay(struct interface_info *ip, struct dhcp_packet *dp, int length,
ssize_t dplen;
if (dp->hlen > sizeof(dp->chaddr)) {
- note("Discarding packet with invalid hlen.");
+ log_info("Discarding packet with invalid hlen.");
return;
}
@@ -899,14 +900,14 @@ l2relay(struct interface_info *ip, struct dhcp_packet *dp, int length,
if (sp->intf == ip)
continue;
- debug("forwarded BOOTREQUEST for %s to %s",
+ log_debug("forwarded BOOTREQUEST for %s to %s",
print_hw_addr(pc->pc_htype, pc->pc_hlen,
pc->pc_smac), sp->intf->name);
send_packet(sp->intf, dp, dplen, pc);
}
if (ip != interfaces) {
- debug("forwarded BOOTREQUEST for %s to %s",
+ log_debug("forwarded BOOTREQUEST for %s to %s",
print_hw_addr(pc->pc_htype, pc->pc_hlen,
pc->pc_smac), interfaces->name);
@@ -920,7 +921,7 @@ l2relay(struct interface_info *ip, struct dhcp_packet *dp, int length,
return;
if (ip != interfaces) {
- debug("forwarded BOOTREPLY for %s to %s",
+ log_debug("forwarded BOOTREPLY for %s to %s",
print_hw_addr(pc->pc_htype, pc->pc_hlen,
pc->pc_dmac), interfaces->name);
send_packet(interfaces, dp, dplen, pc);
@@ -928,7 +929,7 @@ l2relay(struct interface_info *ip, struct dhcp_packet *dp, int length,
break;
default:
- debug("invalid operation type '%d'", dp->op);
+ log_debug("invalid operation type '%d'", dp->op);
return;
}
}
diff --git a/usr.sbin/dhcrelay/dispatch.c b/usr.sbin/dhcrelay/dispatch.c
index 5274556e208..98b2226d0dd 100644
--- a/usr.sbin/dhcrelay/dispatch.c
+++ b/usr.sbin/dhcrelay/dispatch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dispatch.c,v 1.15 2016/12/12 15:41:05 rzalamena Exp $ */
+/* $OpenBSD: dispatch.c,v 1.16 2017/02/13 19:15:39 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -62,6 +62,7 @@
#include "dhcp.h"
#include "dhcpd.h"
+#include "log.h"
struct protocol *protocols;
struct timeout *timeouts;
@@ -83,14 +84,14 @@ get_interface(const char *ifname, void (*handler)(struct protocol *),
int found = 0;
if ((iface = calloc(1, sizeof(*iface))) == NULL)
- error("failed to allocate memory");
+ fatalx("failed to allocate memory");
if (strlcpy(iface->name, ifname, sizeof(iface->name)) >=
sizeof(iface->name))
- error("interface name '%s' too long", ifname);
+ fatalx("interface name '%s' too long", ifname);
if (getifaddrs(&ifap) != 0)
- error("getifaddrs failed");
+ fatalx("getifaddrs failed");
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if ((ifa->ifa_flags & IFF_LOOPBACK) ||
@@ -143,7 +144,7 @@ get_interface(const char *ifname, void (*handler)(struct protocol *),
if (strlcpy(iface->ifr.ifr_name, ifname,
sizeof(iface->ifr.ifr_name)) >= sizeof(iface->ifr.ifr_name))
- error("interface name '%s' too long", ifname);
+ fatalx("interface name '%s' too long", ifname);
/* Register the interface... */
if_register_receive(iface, isserver);
@@ -173,7 +174,7 @@ dispatch(void)
fds = calloc(nfds, sizeof(struct pollfd));
if (fds == NULL)
- error("Can't allocate poll structures.");
+ fatalx("Can't allocate poll structures.");
do {
/*
@@ -220,7 +221,7 @@ another:
}
if (i == 0)
- error("No live interfaces to poll on - exiting.");
+ fatalx("No live interfaces to poll on - exiting.");
/* Wait for a packet or a timeout... XXX */
count = poll(fds, nfds, to_msec);
@@ -232,7 +233,7 @@ another:
continue;
}
else
- error("poll: %m");
+ fatalx("poll: %m");
}
/* Get the current time... */
@@ -275,13 +276,13 @@ got_one(struct protocol *l)
memset(&pc, 0, sizeof(pc));
if ((result = receive_packet(ip, u.packbuf, sizeof(u), &pc)) == -1) {
- warning("receive_packet failed on %s: %s", ip->name,
+ log_warnx("receive_packet failed on %s: %s", ip->name,
strerror(errno));
ip->errors++;
if ((!interface_status(ip)) ||
(ip->noifmedia && ip->errors > 20)) {
/* our interface has gone away. */
- warning("Interface %s no longer appears valid.",
+ log_warnx("Interface %s no longer appears valid.",
ip->name);
ip->dead = 1;
interfaces_invalidated = 1;
@@ -367,7 +368,7 @@ add_protocol(char *name, int fd, void (*handler)(struct protocol *),
p = malloc(sizeof(*p));
if (!p)
- error("can't allocate protocol struct for %s", name);
+ fatalx("can't allocate protocol struct for %s", name);
p->fd = fd;
p->handler = handler;
diff --git a/usr.sbin/dhcrelay/log.c b/usr.sbin/dhcrelay/log.c
new file mode 100644
index 00000000000..4850447d522
--- /dev/null
+++ b/usr.sbin/dhcrelay/log.c
@@ -0,0 +1,199 @@
+/* $OpenBSD: log.c,v 1.1 2017/02/13 19:15:39 krw Exp $ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <syslog.h>
+#include <errno.h>
+#include <time.h>
+
+#include "log.h"
+
+static int debug;
+static int verbose;
+static const char *log_procname;
+
+void
+log_init(int n_debug, int facility)
+{
+ extern char *__progname;
+
+ debug = n_debug;
+ verbose = n_debug;
+ log_procinit(__progname);
+
+ if (!debug)
+ openlog(__progname, LOG_PID | LOG_NDELAY, facility);
+
+ tzset();
+}
+
+void
+log_procinit(const char *procname)
+{
+ if (procname != NULL)
+ log_procname = procname;
+}
+
+void
+log_setverbose(int v)
+{
+ verbose = v;
+}
+
+int
+log_getverbose(void)
+{
+ return (verbose);
+}
+
+void
+logit(int pri, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vlog(pri, fmt, ap);
+ va_end(ap);
+}
+
+void
+vlog(int pri, const char *fmt, va_list ap)
+{
+ char *nfmt;
+ int saved_errno = errno;
+
+ if (debug) {
+ /* best effort in out of mem situations */
+ if (asprintf(&nfmt, "%s\n", fmt) == -1) {
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ } else {
+ vfprintf(stderr, nfmt, ap);
+ free(nfmt);
+ }
+ fflush(stderr);
+ } else
+ vsyslog(pri, fmt, ap);
+
+ errno = saved_errno;
+}
+
+void
+log_warn(const char *emsg, ...)
+{
+ char *nfmt;
+ va_list ap;
+ int saved_errno = errno;
+
+ /* best effort to even work in out of memory situations */
+ if (emsg == NULL)
+ logit(LOG_CRIT, "%s", strerror(saved_errno));
+ else {
+ va_start(ap, emsg);
+
+ if (asprintf(&nfmt, "%s: %s", emsg,
+ strerror(saved_errno)) == -1) {
+ /* we tried it... */
+ vlog(LOG_CRIT, emsg, ap);
+ logit(LOG_CRIT, "%s", strerror(saved_errno));
+ } else {
+ vlog(LOG_CRIT, nfmt, ap);
+ free(nfmt);
+ }
+ va_end(ap);
+ }
+
+ errno = saved_errno;
+}
+
+void
+log_warnx(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vlog(LOG_CRIT, emsg, ap);
+ va_end(ap);
+}
+
+void
+log_info(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vlog(LOG_INFO, emsg, ap);
+ va_end(ap);
+}
+
+void
+log_debug(const char *emsg, ...)
+{
+ va_list ap;
+
+ if (verbose) {
+ va_start(ap, emsg);
+ vlog(LOG_DEBUG, emsg, ap);
+ va_end(ap);
+ }
+}
+
+static void
+vfatalc(int code, const char *emsg, va_list ap)
+{
+ static char s[BUFSIZ];
+ const char *sep;
+
+ if (emsg != NULL) {
+ (void)vsnprintf(s, sizeof(s), emsg, ap);
+ sep = ": ";
+ } else {
+ s[0] = '\0';
+ sep = "";
+ }
+ if (code)
+ logit(LOG_CRIT, "fatal in %s: %s%s%s",
+ log_procname, s, sep, strerror(code));
+ else
+ logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
+}
+
+void
+fatal(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vfatalc(errno, emsg, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void
+fatalx(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vfatalc(0, emsg, ap);
+ va_end(ap);
+ exit(1);
+}
diff --git a/usr.sbin/dhcrelay/log.h b/usr.sbin/dhcrelay/log.h
new file mode 100644
index 00000000000..51ebf6eee83
--- /dev/null
+++ b/usr.sbin/dhcrelay/log.h
@@ -0,0 +1,46 @@
+/* $OpenBSD: log.h,v 1.1 2017/02/13 19:15:39 krw Exp $ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef LOG_H
+#define LOG_H
+
+#include <stdarg.h>
+#include <sys/cdefs.h>
+
+void log_init(int, int);
+void log_procinit(const char *);
+void log_setverbose(int);
+int log_getverbose(void);
+void log_warn(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_warnx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_info(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_debug(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void logit(int, const char *, ...)
+ __attribute__((__format__ (printf, 2, 3)));
+void vlog(int, const char *, va_list)
+ __attribute__((__format__ (printf, 2, 0)));
+__dead void fatal(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+__dead void fatalx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+
+#endif /* LOG_H */
diff --git a/usr.sbin/dhcrelay/packet.c b/usr.sbin/dhcrelay/packet.c
index 547ce33c980..506eaef1f4b 100644
--- a/usr.sbin/dhcrelay/packet.c
+++ b/usr.sbin/dhcrelay/packet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.c,v 1.12 2016/12/08 19:18:15 rzalamena Exp $ */
+/* $OpenBSD: packet.c,v 1.13 2017/02/13 19:15:39 krw Exp $ */
/* Packet assembly code, originally contributed by Archie Cobbs. */
@@ -57,6 +57,7 @@
#include "dhcp.h"
#include "dhcpd.h"
+#include "log.h"
u_int32_t checksum(unsigned char *, unsigned, u_int32_t);
@@ -212,7 +213,7 @@ decode_udp_ip_header(struct interface_info *interface, unsigned char *buf,
ip_packets_bad_checksum++;
if (ip_packets_seen > 4 && ip_packets_bad_checksum != 0 &&
(ip_packets_seen / ip_packets_bad_checksum) < 2) {
- note("%u bad IP checksums seen in %u packets",
+ log_info("%u bad IP checksums seen in %u packets",
ip_packets_bad_checksum, ip_packets_seen);
ip_packets_seen = ip_packets_bad_checksum = 0;
}
@@ -231,7 +232,7 @@ decode_udp_ip_header(struct interface_info *interface, unsigned char *buf,
#ifdef DEBUG
if (ntohs(ip->ip_len) != buflen)
- debug("ip length %d disagrees with bytes received %d.",
+ log_debug("ip length %d disagrees with bytes received %d.",
ntohs(ip->ip_len), buflen);
#endif
@@ -263,7 +264,7 @@ decode_udp_ip_header(struct interface_info *interface, unsigned char *buf,
udp_packets_length_overflow != 0 &&
(udp_packets_length_checked /
udp_packets_length_overflow) < 2) {
- note("%u udp packets in %u too long - dropped",
+ log_info("%u udp packets in %u too long - dropped",
udp_packets_length_overflow,
udp_packets_length_checked);
udp_packets_length_overflow =
@@ -272,7 +273,7 @@ decode_udp_ip_header(struct interface_info *interface, unsigned char *buf,
return (-1);
}
if (len + data != buf + bufix + buflen)
- debug("accepting packet with data after udp payload.");
+ log_debug("accepting packet with data after udp payload.");
usum = udp->uh_sum;
udp->uh_sum = 0;
@@ -287,7 +288,7 @@ decode_udp_ip_header(struct interface_info *interface, unsigned char *buf,
udp_packets_bad_checksum++;
if (udp_packets_seen > 4 && udp_packets_bad_checksum != 0 &&
(udp_packets_seen / udp_packets_bad_checksum) < 2) {
- note("%u bad udp checksums in %u packets",
+ log_info("%u bad udp checksums in %u packets",
udp_packets_bad_checksum, udp_packets_seen);
udp_packets_seen = udp_packets_bad_checksum = 0;
}