From 78eda5802aa73c0019bb31da3014c3bf689c33ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pali=20Roh=C3=A1r?= Date: Sat, 5 Sep 2020 13:52:50 +0200 Subject: [PATCH] Remove duplicate code in mcgroup.c file Existing functionality is provided by k_join and k_leave functions. --- src/Makefile.am | 1 - src/ifvc.c | 6 ++-- src/igmpproxy.h | 12 ++----- src/kern.c | 29 +++++++++------ src/mcgroup.c | 94 ------------------------------------------------- src/rttable.c | 11 +++--- 6 files changed, 28 insertions(+), 125 deletions(-) delete mode 100644 src/mcgroup.c diff --git a/src/Makefile.am b/src/Makefile.am index 12cc0e66..fdb5e06b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,6 @@ igmpproxy_SOURCES = \ igmpproxy.h \ kern.c \ lib.c \ - mcgroup.c \ mroute-api.c \ os-dragonfly.h \ os-freebsd.h \ diff --git a/src/ifvc.c b/src/ifvc.c index 51c77286..41fb630b 100644 --- a/src/ifvc.c +++ b/src/ifvc.c @@ -162,7 +162,7 @@ void rebuildIfVc () { my_log(LOG_NOTICE, 0, "%s [Hidden -> Downstream]", Dp->Name); Dp->state = IF_STATE_DOWNSTREAM; addVIF(Dp); - joinMcGroup(MRouterFD, Dp, allrouters_group); + k_join(Dp, allrouters_group); } // addVIF when found new IF @@ -170,7 +170,7 @@ void rebuildIfVc () { my_log(LOG_NOTICE, 0, "%s [New]", Dp->Name); Dp->state = config->defaultInterfaceState; addVIF(Dp); - joinMcGroup(MRouterFD, Dp, allrouters_group); + k_join(Dp, allrouters_group); IfDescEp++; } @@ -187,7 +187,7 @@ void rebuildIfVc () { if (IF_STATE_LOST == Dp->state) { my_log(LOG_NOTICE, 0, "%s [Downstream -> Hidden]", Dp->Name); Dp->state = IF_STATE_HIDDEN; - leaveMcGroup( MRouterFD, Dp, allrouters_group ); + k_leave(Dp, allrouters_group); delVIF(Dp); } } diff --git a/src/igmpproxy.h b/src/igmpproxy.h index 9fa36ff6..768516ac 100644 --- a/src/igmpproxy.h +++ b/src/igmpproxy.h @@ -242,16 +242,8 @@ void k_hdr_include(int hdrincl); void k_set_ttl(int t); void k_set_loop(int l); void k_set_if(uint32_t ifa); -/* -void k_join(uint32_t grp, uint32_t ifa); -void k_leave(uint32_t grp, uint32_t ifa); -*/ - -/* mcgroup.c - */ -int joinMcGroup( int UdpSock, struct IfDesc *IfDp, uint32_t mcastaddr ); -int leaveMcGroup( int UdpSock, struct IfDesc *IfDp, uint32_t mcastaddr ); - +void k_join(struct IfDesc *ifd, uint32_t grp); +void k_leave(struct IfDesc *ifd, uint32_t grp); /* rttable.c */ diff --git a/src/kern.c b/src/kern.c index 490ffd6d..e99ea350 100644 --- a/src/kern.c +++ b/src/kern.c @@ -111,29 +111,38 @@ void k_set_if(uint32_t ifa) { inetFmt(ifa, s1)); } -/* -void k_join(uint32_t grp, uint32_t ifa) { +void k_join(struct IfDesc *ifd, uint32_t grp) { struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = grp; - mreq.imr_interface.s_addr = ifa; + mreq.imr_interface.s_addr = ifd->InAdr.s_addr; + + my_log(LOG_NOTICE, 0, "Joining group %s on interface %s", inetFmt(grp, s1), ifd->Name); if (setsockopt(MRouterFD, IPPROTO_IP, IP_ADD_MEMBERSHIP, - (char *)&mreq, sizeof(mreq)) < 0) + (char *)&mreq, sizeof(mreq)) < 0) { + int mcastGroupExceeded = (errno == ENOBUFS); my_log(LOG_WARNING, errno, "can't join group %s on interface %s", - inetFmt(grp, s1), inetFmt(ifa, s2)); + inetFmt(grp, s1), ifd->Name); + if (mcastGroupExceeded) { + my_log(LOG_WARNING, 0, "Maximum number of multicast groups were exceeded"); +#ifdef __linux__ + my_log(LOG_WARNING, 0, "Check settings of '/sbin/sysctl net.ipv4.igmp_max_memberships'"); +#endif + } + } } - -void k_leave(uint32_t grp, uint32_t ifa) { +void k_leave(struct IfDesc *ifd, uint32_t grp) { struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = grp; - mreq.imr_interface.s_addr = ifa; + mreq.imr_interface.s_addr = ifd->InAdr.s_addr; + + my_log(LOG_NOTICE, 0, "Leaving group %s on interface %s", inetFmt(grp, s1), ifd->Name); if (setsockopt(MRouterFD, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) my_log(LOG_WARNING, errno, "can't leave group %s on interface %s", - inetFmt(grp, s1), inetFmt(ifa, s2)); + inetFmt(grp, s1), ifd->Name); } -*/ diff --git a/src/mcgroup.c b/src/mcgroup.c deleted file mode 100644 index a2355627..00000000 --- a/src/mcgroup.c +++ /dev/null @@ -1,94 +0,0 @@ -/* -** igmpproxy - IGMP proxy based multicast router -** Copyright (C) 2005 Johnny Egeland -** -** This program is free software; you can redistribute it and/or modify -** it under the terms of the GNU General Public License as published by -** the Free Software Foundation; either version 2 of the License, or -** (at your option) any later version. -** -** This program is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License -** along with this program; if not, write to the Free Software -** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -** -**---------------------------------------------------------------------------- -** -** This software is derived work from the following software. The original -** source code has been modified from it's original state by the author -** of igmpproxy. -** -** smcroute 0.92 - Copyright (C) 2001 Carsten Schill -** - Licensed under the GNU General Public License, either version 2 or -** any later version. -** -** mrouted 3.9-beta3 - Copyright (C) 2002 by The Board of Trustees of -** Leland Stanford Junior University. -** - Licensed under the 3-clause BSD license, see Stanford.txt file. -** -*/ -/** -* mcgroup contains functions for joining and leaving multicast groups. -* -*/ - -#include "igmpproxy.h" - - -/** -* Common function for joining or leaving a MCast group. -*/ -static int joinleave( int Cmd, int UdpSock, struct IfDesc *IfDp, uint32_t mcastaddr ) { - struct ip_mreq CtlReq; - const char *CmdSt = Cmd == 'j' ? "join" : "leave"; - - memset(&CtlReq, 0, sizeof(CtlReq)); - CtlReq.imr_multiaddr.s_addr = mcastaddr; - CtlReq.imr_interface.s_addr = IfDp->InAdr.s_addr; - - { - my_log( LOG_NOTICE, 0, "%sMcGroup: %s on %s", CmdSt, - inetFmt( mcastaddr, s1 ), IfDp ? IfDp->Name : "" ); - } - - if( setsockopt( UdpSock, IPPROTO_IP, - Cmd == 'j' ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, - (void *)&CtlReq, sizeof( CtlReq ) ) ) - { - int mcastGroupExceeded = (Cmd == 'j' && errno == ENOBUFS); - my_log( LOG_WARNING, errno, "MRT_%s_MEMBERSHIP failed", Cmd == 'j' ? "ADD" : "DROP" ); - if (mcastGroupExceeded) { - my_log(LOG_WARNING, 0, "Maximum number of multicast groups were exceeded"); -#ifdef __linux__ - my_log(LOG_WARNING, 0, "Check settings of '/sbin/sysctl net.ipv4.igmp_max_memberships'"); -#endif - } - return 1; - } - - return 0; -} - -/** -* Joins the MC group with the address 'McAdr' on the interface 'IfName'. -* The join is bound to the UDP socket 'UdpSock', so if this socket is -* closed the membership is dropped. -* -* @return 0 if the function succeeds, 1 if parameters are wrong or the join fails -*/ -int joinMcGroup( int UdpSock, struct IfDesc *IfDp, uint32_t mcastaddr ) { - return joinleave( 'j', UdpSock, IfDp, mcastaddr ); -} - -/** -* Leaves the MC group with the address 'McAdr' on the interface 'IfName'. -* -* @return 0 if the function succeeds, 1 if parameters are wrong or the join fails -*/ -int leaveMcGroup( int UdpSock, struct IfDesc *IfDp, uint32_t mcastaddr ) { - return joinleave( 'l', UdpSock, IfDp, mcastaddr ); -} diff --git a/src/rttable.c b/src/rttable.c index f3668e91..2593ecaf 100644 --- a/src/rttable.c +++ b/src/rttable.c @@ -130,12 +130,11 @@ void initRouteTable(void) { my_log(LOG_DEBUG, 0, "Joining all-routers group %s on vif %s", inetFmt(allrouters_group,s1),inetFmt(Dp->InAdr.s_addr,s2)); - //k_join(allrouters_group, Dp->InAdr.s_addr); - joinMcGroup( MRouterFD, Dp, allrouters_group ); + k_join(Dp, allrouters_group); my_log(LOG_DEBUG, 0, "Joining all igmpv3 multicast routers group %s on vif %s", inetFmt(alligmp3_group,s1),inetFmt(Dp->InAdr.s_addr,s2)); - joinMcGroup( MRouterFD, Dp, alligmp3_group ); + k_join(Dp, alligmp3_group); } } } @@ -183,8 +182,7 @@ static void sendJoinLeaveUpstream(struct RouteTable* route, int join) { inetFmt(route->group, s1), inetFmt(upstrIf->InAdr.s_addr, s2)); - //k_join(route->group, upstrIf->InAdr.s_addr); - joinMcGroup( MRouterFD, upstrIf, route->group ); + k_join(upstrIf, route->group); route->upstrState = ROUTESTATE_JOINED; } else { @@ -198,8 +196,7 @@ static void sendJoinLeaveUpstream(struct RouteTable* route, int join) { inetFmt(route->group, s1), inetFmt(upstrIf->InAdr.s_addr, s2)); - //k_leave(route->group, upstrIf->InAdr.s_addr); - leaveMcGroup( MRouterFD, upstrIf, route->group ); + k_leave(upstrIf, route->group); route->upstrState = ROUTESTATE_NOTJOINED; }