From 728cd663005142392693bdefdef489e4991949de Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 27 Jul 2016 20:13:16 -0400 Subject: [PATCH] pimd: Create pim_socket_bind Allow a socket to be bound to a specified interface. Signed-off-by: Donald Sharp --- pimd/pim_sock.c | 34 +++++++++++++++++++++++----------- pimd/pim_sock.h | 1 + 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pimd/pim_sock.c b/pimd/pim_sock.c index b100c37b46..70b18b14a4 100644 --- a/pimd/pim_sock.c +++ b/pimd/pim_sock.c @@ -68,6 +68,28 @@ int pim_socket_raw(int protocol) return fd; } +/* + * Given a socket and a interface, + * Bind that socket to that interface + */ +int pim_socket_bind (int fd, struct interface *ifp) +{ + int ret; + + if (pimd_privs.change (ZPRIVS_RAISE)) + zlog_err ("%s: could not raise privs, %s", + __PRETTY_FUNCTION__, safe_strerror (errno)); + + ret = setsockopt (fd, SOL_SOCKET, + SO_BINDTODEVICE, ifp->name, strlen (ifp->name)); + + if (pimd_privs.change (ZPRIVS_LOWER)) + zlog_err ("%s: could not lower privs, %s", + __PRETTY_FUNCTION__, safe_strerror (errno)); + + return ret; +} + int pim_socket_mcast(int protocol, struct in_addr ifaddr, int ifindex, u_char loop) { int fd; @@ -87,17 +109,7 @@ int pim_socket_mcast(int protocol, struct in_addr ifaddr, int ifindex, u_char lo ifp = if_lookup_by_index_vrf (ifindex, VRF_DEFAULT); - if (pimd_privs.change (ZPRIVS_RAISE)) - zlog_err ("%s: could not raise privs, %s", - __PRETTY_FUNCTION__, safe_strerror (errno)); - - ret = setsockopt (fd, SOL_SOCKET, - SO_BINDTODEVICE, ifp->name, strlen (ifp->name)); - - if (pimd_privs.change (ZPRIVS_LOWER)) - zlog_err ("%s: could not lower privs, %s", - __PRETTY_FUNCTION__, safe_strerror (errno)); - + ret = pim_socket_bind (fd, ifp); if (ret) { zlog_warn("Could not set fd: %d for interface: %s to device", diff --git a/pimd/pim_sock.h b/pimd/pim_sock.h index 556049d9b0..51ad88e24e 100644 --- a/pimd/pim_sock.h +++ b/pimd/pim_sock.h @@ -37,6 +37,7 @@ #define PIM_SOCK_ERR_NAME (-10) /* Socket name (getsockname) */ #define PIM_SOCK_ERR_BIND (-11) /* Can't bind to interface */ +int pim_socket_bind (int fd, struct interface *ifp); int pim_socket_raw(int protocol); int pim_socket_mcast(int protocol, struct in_addr ifaddr, int ifindex, u_char loop); int pim_socket_join(int fd, struct in_addr group, -- 2.39.5