diff options
author | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-12-07 13:19:19 +0000 |
---|---|---|
committer | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2016-12-07 13:19:19 +0000 |
commit | af93213e68cd2f975132807ec9ceb78ecf90a924 (patch) | |
tree | 40a7d0399462881308a1c45974f213d6223c8aaf /usr.sbin | |
parent | acf3cd5327680c25dad886997ee70e78a83fe03c (diff) |
Rename function discover_interfaces into get_interface and change its
prototype to be more useful.
ok reyk@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/dhcrelay/dhcpd.h | 5 | ||||
-rw-r--r-- | usr.sbin/dhcrelay/dhcrelay.c | 13 | ||||
-rw-r--r-- | usr.sbin/dhcrelay/dispatch.c | 35 |
3 files changed, 27 insertions, 26 deletions
diff --git a/usr.sbin/dhcrelay/dhcpd.h b/usr.sbin/dhcrelay/dhcpd.h index 59eee286d6e..c3a4648a6ff 100644 --- a/usr.sbin/dhcrelay/dhcpd.h +++ b/usr.sbin/dhcrelay/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.14 2016/02/07 00:49:28 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.15 2016/12/07 13:19:18 rzalamena Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -121,7 +121,8 @@ ssize_t receive_packet(struct interface_info *, unsigned char *, size_t, extern void (*bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *); -void discover_interfaces(struct interface_info *); +struct interface_info *get_interface(const char *, + void (*)(struct protocol *)); void dispatch(void); void got_one(struct protocol *); void add_protocol(char *, int, void (*)(struct protocol *), void *); diff --git a/usr.sbin/dhcrelay/dhcrelay.c b/usr.sbin/dhcrelay/dhcrelay.c index 497656bea8d..73eec72f51b 100644 --- a/usr.sbin/dhcrelay/dhcrelay.c +++ b/usr.sbin/dhcrelay/dhcrelay.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcrelay.c,v 1.43 2016/09/26 17:15:19 jca Exp $ */ +/* $OpenBSD: dhcrelay.c,v 1.44 2016/12/07 13:19:18 rzalamena Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@cvs.openbsd.org> @@ -113,17 +113,14 @@ main(int argc, char *argv[]) case 'i': if (interfaces != NULL) usage(); - if ((interfaces = calloc(1, - sizeof(struct interface_info))) == NULL) - error("calloc"); - strlcpy(interfaces->name, optarg, - sizeof(interfaces->name)); + + interfaces = get_interface(optarg, got_one); break; case 'o': /* add the relay agent information option */ oflag++; break; - + default: usage(); /* not reached */ @@ -177,8 +174,6 @@ main(int argc, char *argv[]) if (!sp) usage(); - discover_interfaces(interfaces); - rdomain = get_rdomain(interfaces->name); /* Enable the relay agent option by default for enc0 */ diff --git a/usr.sbin/dhcrelay/dispatch.c b/usr.sbin/dhcrelay/dispatch.c index e6d734d80e6..90197304d5d 100644 --- a/usr.sbin/dhcrelay/dispatch.c +++ b/usr.sbin/dhcrelay/dispatch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dispatch.c,v 1.11 2016/08/27 01:26:22 guenther Exp $ */ +/* $OpenBSD: dispatch.c,v 1.12 2016/12/07 13:19:18 rzalamena Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -74,18 +74,20 @@ void (*bootp_packet_handler)(struct interface_info *, static int interface_status(struct interface_info *ifinfo); -/* - * Use getifaddrs() to get a list of all the attached interfaces. For - * each interface that's of type INET and not the loopback interface, - * register that interface with the network I/O software, figure out - * what subnet it's on, and add it to the list of interfaces. - */ -void -discover_interfaces(struct interface_info *iface) +struct interface_info * +get_interface(const char *ifname, void (*handler)(struct protocol *)) { - struct sockaddr_in foo; - struct ifaddrs *ifap, *ifa; - struct ifreq *tif; + struct interface_info *iface; + struct ifaddrs *ifap, *ifa; + struct ifreq *tif; + struct sockaddr_in foo; + + 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"); if (getifaddrs(&ifap) != 0) error("getifaddrs failed"); @@ -96,7 +98,7 @@ discover_interfaces(struct interface_info *iface) (!(ifa->ifa_flags & IFF_UP))) continue; - if (strcmp(iface->name, ifa->ifa_name)) + if (strcmp(ifname, ifa->ifa_name)) continue; /* @@ -139,14 +141,17 @@ discover_interfaces(struct interface_info *iface) } } + freeifaddrs(ifap); + if (!iface->ifp) error("%s: not found", iface->name); /* Register the interface... */ if_register_receive(iface); if_register_send(iface); - add_protocol(iface->name, iface->rfdesc, got_one, iface); - freeifaddrs(ifap); + add_protocol(iface->name, iface->rfdesc, handler, iface); + + return (iface); } /* |