changeset 2148:404a5ea19f84 draft

Cleanup IPv4 fd opening and closing.
author Roy Marples <roy@marples.name>
date Tue, 12 Nov 2013 15:00:22 +0000
parents b45c8654e594
children d83d83c2fad9
files arp.c bpf.c dhcp.c lpf.c
diffstat 4 files changed, 11 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/arp.c	Sun Nov 10 11:17:40 2013 +0000
+++ b/arp.c	Tue Nov 12 15:00:22 2013 +0000
@@ -220,7 +220,11 @@
 	if (state->new == NULL)
 		return;
 	if (state->arp_fd == -1) {
-		ipv4_opensocket(ifp, ETHERTYPE_ARP);
+		state->arp_fd = ipv4_opensocket(ifp, ETHERTYPE_ARP);
+		if (state->arp_fd == -1) {
+			syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
+			return;
+		}
 		eloop_event_add(state->arp_fd, arp_packet, ifp);
 	}
 	if (++state->claims < ANNOUNCE_NUM)
@@ -267,8 +271,11 @@
 	int arping = 0;
 
 	if (state->arp_fd == -1) {
-		if (ipv4_opensocket(ifp, ETHERTYPE_ARP) == -1)
+		state->arp_fd = ipv4_opensocket(ifp, ETHERTYPE_ARP);
+		if (state->arp_fd == -1) {
+			syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
 			return;
+		}
 		eloop_event_add(state->arp_fd, arp_packet, ifp);
 	}
 
--- a/bpf.c	Sun Nov 10 11:17:40 2013 +0000
+++ b/bpf.c	Tue Nov 12 15:00:22 2013 +0000
@@ -53,7 +53,6 @@
 {
 	struct dhcp_state *state;
 	int fd = -1;
-	int *fdp = NULL;
 	struct ifreq ifr;
 	int buf_len = 0;
 	struct bpf_version pv;
@@ -117,21 +116,14 @@
 	if (protocol == ETHERTYPE_ARP) {
 		pf.bf_insns = UNCONST(arp_bpf_filter);
 		pf.bf_len = arp_bpf_filter_len;
-		fdp = &state->arp_fd;
 	} else {
 		pf.bf_insns = UNCONST(dhcp_bpf_filter);
 		pf.bf_len = dhcp_bpf_filter_len;
-		fdp = &state->raw_fd;
 	}
 	if (ioctl(fd, BIOCSETF, &pf) == -1)
 		goto eexit;
 	if (set_cloexec(fd) == -1)
 		goto eexit;
-	if (fdp) {
-		if (*fdp != -1)
-			close(*fdp);
-		*fdp = fd;
-	}
 	return fd;
 
 eexit:
--- a/dhcp.c	Sun Nov 10 11:17:40 2013 +0000
+++ b/dhcp.c	Tue Nov 12 15:00:22 2013 +0000
@@ -2423,12 +2423,12 @@
 static int
 dhcp_open(struct interface *ifp)
 {
-	int r = 0;
 	struct dhcp_state *state;
 
 	state = D_STATE(ifp);
 	if (state->raw_fd == -1) {
-		if ((r = ipv4_opensocket(ifp, ETHERTYPE_IP)) == -1) {
+		state->raw_fd = ipv4_opensocket(ifp, ETHERTYPE_IP);
+		if (state->raw_fd == -1) {
 			syslog(LOG_ERR, "%s: %s: %m", __func__, ifp->name);
 			return -1;
 		}
--- a/lpf.c	Sun Nov 10 11:17:40 2013 +0000
+++ b/lpf.c	Tue Nov 12 15:00:22 2013 +0000
@@ -76,8 +76,6 @@
 		struct sockaddr_storage ss;
 	} su;
 	struct sock_fprog pf;
-	int *fd;
-	struct dhcp_state *state;
 #ifdef PACKET_AUXDATA
 	int n;
 #endif
@@ -113,14 +111,6 @@
 		goto eexit;
 	if (bind(s, &su.sa, sizeof(su)) == -1)
 		goto eexit;
-	state = D_STATE(ifp);
-	if (protocol == ETHERTYPE_ARP)
-		fd = &state->arp_fd;
-	else
-		fd = &state->raw_fd;
-	if (*fd != -1)
-		close(*fd);
-	*fd = s;
 	return s;
 
 eexit: