diff options
author | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-12-08 09:29:51 +0000 |
---|---|---|
committer | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-12-08 09:29:51 +0000 |
commit | 0f630a62f42440d49c043968238e7ccb983e48f3 (patch) | |
tree | ac77a3cb0d5db664df2d6fd6d0b44341f4e3dc0e /usr.sbin/dhcrelay | |
parent | 241cb4ea56e40bd3593e166d7c04e5949e7d36a7 (diff) |
Simplify get_interfaces function, make it return NULL if we didn't find
the interface and abort dhcrelay with a more friendly message when we
don't have an address configured in the interface.
ok jca@
Diffstat (limited to 'usr.sbin/dhcrelay')
-rw-r--r-- | usr.sbin/dhcrelay/bpf.c | 4 | ||||
-rw-r--r-- | usr.sbin/dhcrelay/dhcpd.h | 4 | ||||
-rw-r--r-- | usr.sbin/dhcrelay/dhcrelay.c | 7 | ||||
-rw-r--r-- | usr.sbin/dhcrelay/dispatch.c | 43 |
4 files changed, 31 insertions, 27 deletions
diff --git a/usr.sbin/dhcrelay/bpf.c b/usr.sbin/dhcrelay/bpf.c index 9a498c98f23..d211b6f78cf 100644 --- a/usr.sbin/dhcrelay/bpf.c +++ b/usr.sbin/dhcrelay/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.11 2016/05/28 07:00:18 natano Exp $ */ +/* $OpenBSD: bpf.c,v 1.12 2016/12/08 09:29:50 rzalamena Exp $ */ /* BPF socket interface code, originally contributed by Archie Cobbs. */ @@ -75,7 +75,7 @@ if_register_bpf(struct interface_info *info) error("Can't open bpf device: %m"); /* Set the BPF device to point at this interface. */ - if (ioctl(sock, BIOCSETIF, info->ifp) == -1) + if (ioctl(sock, BIOCSETIF, &info->ifr) == -1) error("Can't attach interface %s to bpf device: %m", info->name); diff --git a/usr.sbin/dhcrelay/dhcpd.h b/usr.sbin/dhcrelay/dhcpd.h index c3a4648a6ff..6c922a40988 100644 --- a/usr.sbin/dhcrelay/dhcpd.h +++ b/usr.sbin/dhcrelay/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.15 2016/12/07 13:19:18 rzalamena Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.16 2016/12/08 09:29:50 rzalamena Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -76,7 +76,7 @@ struct interface_info { size_t rbuf_max; size_t rbuf_offset; size_t rbuf_len; - struct ifreq *ifp; + struct ifreq ifr; int noifmedia; int errors; int dead; diff --git a/usr.sbin/dhcrelay/dhcrelay.c b/usr.sbin/dhcrelay/dhcrelay.c index d27d3e4a551..dfb31846947 100644 --- a/usr.sbin/dhcrelay/dhcrelay.c +++ b/usr.sbin/dhcrelay/dhcrelay.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcrelay.c,v 1.47 2016/12/07 20:03:22 patrick Exp $ */ +/* $OpenBSD: dhcrelay.c,v 1.48 2016/12/08 09:29:50 rzalamena Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@cvs.openbsd.org> @@ -115,6 +115,8 @@ main(int argc, char *argv[]) usage(); interfaces = get_interface(optarg, got_one); + if (interfaces == NULL) + error("interface '%s' not found", optarg); break; case 'o': /* add the relay agent information option */ @@ -165,6 +167,9 @@ main(int argc, char *argv[]) if (interfaces == NULL) error("no interface given"); + if (interfaces->primary_address.s_addr == 0) + error("interface '%s' does not have an address", + interfaces->name); /* Default DHCP/BOOTP ports. */ server_port = htons(SERVER_PORT); diff --git a/usr.sbin/dhcrelay/dispatch.c b/usr.sbin/dhcrelay/dispatch.c index 90197304d5d..649a7e29200 100644 --- a/usr.sbin/dhcrelay/dispatch.c +++ b/usr.sbin/dhcrelay/dispatch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dispatch.c,v 1.12 2016/12/07 13:19:18 rzalamena Exp $ */ +/* $OpenBSD: dispatch.c,v 1.13 2016/12/08 09:29:50 rzalamena Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -79,15 +79,15 @@ get_interface(const char *ifname, void (*handler)(struct protocol *)) { struct interface_info *iface; struct ifaddrs *ifap, *ifa; - struct ifreq *tif; - struct sockaddr_in foo; + struct sockaddr_in *sin; + int found = 0; if ((iface = calloc(1, sizeof(*iface))) == NULL) error("failed to allocate memory"); if (strlcpy(iface->name, ifname, sizeof(iface->name)) >= sizeof(iface->name)) - error("interface name too long"); + error("interface name '%s' too long", ifname); if (getifaddrs(&ifap) != 0) error("getifaddrs failed"); @@ -101,6 +101,8 @@ get_interface(const char *ifname, void (*handler)(struct protocol *)) if (strcmp(ifname, ifa->ifa_name)) continue; + found = 1; + /* * If we have the capability, extract link information * and record it in a linked list. @@ -120,31 +122,28 @@ get_interface(const char *ifname, void (*handler)(struct protocol *)) memcpy(iface->hw_address.haddr, LLADDR(foo), foo->sdl_alen); } else if (ifa->ifa_addr->sa_family == AF_INET) { - struct iaddr addr; + /* We already have the primary address. */ + if (iface->primary_address.s_addr != 0) + continue; - memcpy(&foo, ifa->ifa_addr, sizeof(foo)); - if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK)) + sin = (struct sockaddr_in *)ifa->ifa_addr; + if (sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) continue; - if (!iface->ifp) { - int len = IFNAMSIZ + ifa->ifa_addr->sa_len; - - if ((tif = malloc(len)) == NULL) - error("no space to remember ifp"); - strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ); - memcpy(&tif->ifr_addr, ifa->ifa_addr, - ifa->ifa_addr->sa_len); - iface->ifp = tif; - iface->primary_address = foo.sin_addr; - } - addr.len = 4; - memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len); + + iface->primary_address = sin->sin_addr; } } freeifaddrs(ifap); - if (!iface->ifp) - error("%s: not found", iface->name); + if (!found) { + free(iface); + return (NULL); + } + + if (strlcpy(iface->ifr.ifr_name, ifname, + sizeof(iface->ifr.ifr_name)) >= sizeof(iface->ifr.ifr_name)) + error("interface name '%s' too long", ifname); /* Register the interface... */ if_register_receive(iface); |