[PATCH 05/12] Don't open vrrp_send_socket if address family is wrong

[email protected] (Quentin Armitage)
Newsgroups gmane.linux.keepalived.devel
Message-ID <[email protected]>
open_vrrp_send_socket was opening a socket, and then checking that
the address family was valid. Checking that the address family is
valid at the beginning of the function streamlines the code.

Signed-off-by: Quentin Armitage <[email protected]>
---
 keepalived/vrrp/vrrp.c |   36 +++++++++++++++++-------------------
 1 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/keepalived/vrrp/vrrp.c b/keepalived/vrrp/vrrp.c
index b9299fe..1be1e72 100644
--- a/keepalived/vrrp/vrrp.c
+++ b/keepalived/vrrp/vrrp.c
@@ -1433,6 +1433,12 @@ open_vrrp_send_socket(sa_family_t family, int proto, int idx, int unicast)
 	interface_t *ifp;
 	int fd = -1;
 
+	if (family != AF_INET && family != AF_INET6) {
+		log_message(LOG_INFO, "cant open raw socket. unknown family=%d"
+				    , family);
+		return -1;
+	}
+
 	/* Retreive interface_t */
 	ifp = if_get_by_ifindex(idx);
 
@@ -1448,31 +1454,23 @@ open_vrrp_send_socket(sa_family_t family, int proto, int idx, int unicast)
 		if_setsockopt_hdrincl(&fd);
 		if (unicast)
 			if_setsockopt_bindtodevice(&fd, ifp);
-		else {
-			if_setsockopt_mcast_if(family, &fd, ifp);
-			if_setsockopt_mcast_loop(family, &fd);
-		}
-		if_setsockopt_priority(&fd);
-		if (fd < 0)
-			return -1;
 	} else if (family == AF_INET6) {
 		/* Set v6 related */
 		if_setsockopt_ipv6_checksum(&fd);
-		if (!unicast) {
+		if (!unicast)
 			if_setsockopt_mcast_hops(family, &fd);
-			if_setsockopt_mcast_if(family, &fd, ifp);
-			if_setsockopt_mcast_loop(family, &fd);
-		}
-		if_setsockopt_priority(&fd);
-		if (fd < 0)
-			return -1;
-	} else {
-		log_message(LOG_INFO, "cant open raw socket. unknow family=%d"
-				    , family);
-		close(fd);
-		return -1;
 	}
 
+	if (!unicast) {
+		if_setsockopt_mcast_if(family, &fd, ifp);
+		if_setsockopt_mcast_loop(family, &fd);
+	}
+
+	if_setsockopt_priority(&fd);
+
+	if (fd < 0)
+		return -1;
+
 	return fd;
 }
 
-- 
1.7.7.6


------------------------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.