[prev in list] [next in list] [prev in thread] [next in thread] 

List:       keepalived-devel
Subject:    [Keepalived-devel] [PATCH 05/12] Don't open vrrp_send_socket if address family is wrong
From:       quentin () armitage ! org ! uk (Quentin Armitage)
Date:       2015-11-01 21:46:11
Message-ID: 20151101214611.15829C357A () samson ! armitage ! org ! uk
[Download RAW message or body]

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 <quentin@armitage.org.uk>
---
 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


------------------------------------------------------------------------------
_______________________________________________
Keepalived-devel mailing list
Keepalived-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/keepalived-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic