From 6c2a91e0a50fbe01c171c26d62bb4f5e78c52f0c Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Tue, 7 Aug 2018 15:54:35 +0200 Subject: [PATCH 1/4] prefix igmp functions --- src/igmp.c | 8 ++++---- src/igmpproxy.c | 4 ++-- src/igmpproxy.h | 6 +++--- src/request.c | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/igmp.c b/src/igmp.c index 38914377..779cf436 100644 --- a/src/igmp.c +++ b/src/igmp.c @@ -32,7 +32,7 @@ ** */ /** -* igmp.h - Recieves IGMP requests, and handle them +* igmp.c - Receives IGMP requests, and handle them * appropriately... */ @@ -50,7 +50,7 @@ extern int MRouterFD; * Open and initialize the igmp socket, and fill in the non-changing * IP header fields in the output packet buffer. */ -void initIgmp(void) { +void igmp_init(void) { struct ip *ip; recv_buf = malloc(RECV_BUF_SIZE); @@ -103,7 +103,7 @@ static const char *igmpPacketKind(unsigned int type, unsigned int code) { * Process a newly received IGMP packet that is sitting in the input * packet buffer. */ -void acceptIgmp(int recvlen) { +void igmp_accept(int recvlen) { register uint32_t src, dst, group; struct ip *ip; struct igmp *igmp; @@ -304,7 +304,7 @@ static void buildIgmp(uint32_t src, uint32_t dst, int type, int code, uint32_t g * Then send the message from the interface with IP address 'src' to * destination 'dst'. */ -void sendIgmp(uint32_t src, uint32_t dst, int type, int code, uint32_t group, int datalen) { +void igmp_send(uint32_t src, uint32_t dst, int type, int code, uint32_t group, int datalen) { struct sockaddr_in sdst; int setloop = 0, setigmpsource = 0; diff --git a/src/igmpproxy.c b/src/igmpproxy.c index 9d1172b3..c8ef0495 100644 --- a/src/igmpproxy.c +++ b/src/igmpproxy.c @@ -232,7 +232,7 @@ int igmpProxyInit(void) { } // Initialize IGMP - initIgmp(); + igmp_init(); // Initialize Routing table initRouteTable(); // Initialize timer @@ -326,7 +326,7 @@ void igmpProxyRun(void) { continue; } - acceptIgmp(recvlen); + igmp_accept(recvlen); } } diff --git a/src/igmpproxy.h b/src/igmpproxy.h index ad1063be..d6ba635b 100644 --- a/src/igmpproxy.h +++ b/src/igmpproxy.h @@ -223,9 +223,9 @@ struct Config *getCommonConfig(void); extern uint32_t allhosts_group; extern uint32_t allrouters_group; extern uint32_t alligmp3_group; -void initIgmp(void); -void acceptIgmp(int); -void sendIgmp (uint32_t, uint32_t, int, int, uint32_t,int); +void igmp_init(void); +void igmp_accept(int); +void igmp_send(uint32_t, uint32_t, int, int, uint32_t, int); /* lib.c */ diff --git a/src/request.c b/src/request.c index 13d92ff7..ae34f5f8 100644 --- a/src/request.c +++ b/src/request.c @@ -188,7 +188,7 @@ void sendGroupSpecificMemberQuery(void *argument) { if (interfaceInRoute(gvDesc->group ,Dp->index)) { // Send a group specific membership query... - sendIgmp(Dp->InAdr.s_addr, gvDesc->group, + igmp_send(Dp->InAdr.s_addr, gvDesc->group, IGMP_MEMBERSHIP_QUERY, conf->lastMemberQueryInterval * IGMP_TIMER_SCALE, gvDesc->group, 0); @@ -219,7 +219,7 @@ void sendGeneralMembershipQuery(void) { if ( Dp->InAdr.s_addr && ! (Dp->Flags & IFF_LOOPBACK) ) { if(Dp->state == IF_STATE_DOWNSTREAM) { // Send the membership query... - sendIgmp(Dp->InAdr.s_addr, allhosts_group, + igmp_send(Dp->InAdr.s_addr, allhosts_group, IGMP_MEMBERSHIP_QUERY, conf->queryResponseInterval * IGMP_TIMER_SCALE, 0, 0); From 51969eeda0fa2c4f6f422e959abd8475547602be Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Tue, 7 Aug 2018 16:06:27 +0200 Subject: [PATCH 2/4] move recvfrom part to igmp.c --- src/igmp.c | 20 +++++++++++++++++++- src/igmpproxy.c | 11 ++--------- src/igmpproxy.h | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/igmp.c b/src/igmp.c index 779cf436..55e94a7e 100644 --- a/src/igmp.c +++ b/src/igmp.c @@ -103,7 +103,7 @@ static const char *igmpPacketKind(unsigned int type, unsigned int code) { * Process a newly received IGMP packet that is sitting in the input * packet buffer. */ -void igmp_accept(int recvlen) { +static void acceptIgmp(int recvlen) { register uint32_t src, dst, group; struct ip *ip; struct igmp *igmp; @@ -262,6 +262,24 @@ void igmp_accept(int recvlen) { } } +/* Receive IGMP packet + * + * @return 0 if the function succeeds, 1 if recvfrom fails + */ +int igmp_receive(void) +{ + socklen_t dummy = 0; + int recvlen = recvfrom(MRouterFD, recv_buf, RECV_BUF_SIZE, 0, NULL, &dummy); + if (recvlen < 0) { + if (errno != EINTR) my_log(LOG_ERR, errno, "recvfrom"); + return 1; + } + + acceptIgmp(recvlen); + + return 0; +} + /* * Construct an IGMP message in the output packet buffer. The caller may diff --git a/src/igmpproxy.c b/src/igmpproxy.c index c8ef0495..d4d71a03 100644 --- a/src/igmpproxy.c +++ b/src/igmpproxy.c @@ -259,10 +259,8 @@ void igmpProxyRun(void) { // Get the config. struct Config *config = getCommonConfig(); // Set some needed values. - register int recvlen; int MaxFD, Rt, secs; fd_set ReadFDS; - socklen_t dummy = 0; struct timespec curtime, lasttime, difftime, tv; // The timeout is a pointer in order to set it to NULL if nessecary. struct timespec *timeout = &tv; @@ -318,15 +316,10 @@ void igmpProxyRun(void) { // Read IGMP request, and handle it... if( FD_ISSET( MRouterFD, &ReadFDS ) ) { - - recvlen = recvfrom(MRouterFD, recv_buf, RECV_BUF_SIZE, - 0, NULL, &dummy); - if (recvlen < 0) { - if (errno != EINTR) my_log(LOG_ERR, errno, "recvfrom"); + if (igmp_receive()) + { continue; } - - igmp_accept(recvlen); } } diff --git a/src/igmpproxy.h b/src/igmpproxy.h index d6ba635b..3e211e63 100644 --- a/src/igmpproxy.h +++ b/src/igmpproxy.h @@ -224,7 +224,7 @@ extern uint32_t allhosts_group; extern uint32_t allrouters_group; extern uint32_t alligmp3_group; void igmp_init(void); -void igmp_accept(int); +int igmp_receive(void); void igmp_send(uint32_t, uint32_t, int, int, uint32_t, int); /* lib.c From 4b32be772134d5119eb1461dd058c79955bd0df6 Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Tue, 7 Aug 2018 16:10:50 +0200 Subject: [PATCH 3/4] fix formatting --- src/igmp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/igmp.c b/src/igmp.c index 55e94a7e..2e69068b 100644 --- a/src/igmp.c +++ b/src/igmp.c @@ -266,8 +266,7 @@ static void acceptIgmp(int recvlen) { * * @return 0 if the function succeeds, 1 if recvfrom fails */ -int igmp_receive(void) -{ +int igmp_receive(void) { socklen_t dummy = 0; int recvlen = recvfrom(MRouterFD, recv_buf, RECV_BUF_SIZE, 0, NULL, &dummy); if (recvlen < 0) { From 73ac854db5ebea920ef3b4bd2d45128f6f9ee471 Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Tue, 7 Aug 2018 16:13:33 +0200 Subject: [PATCH 4/4] fix formatting --- src/igmpproxy.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/igmpproxy.c b/src/igmpproxy.c index d4d71a03..9a638e75 100644 --- a/src/igmpproxy.c +++ b/src/igmpproxy.c @@ -316,8 +316,7 @@ void igmpProxyRun(void) { // Read IGMP request, and handle it... if( FD_ISSET( MRouterFD, &ReadFDS ) ) { - if (igmp_receive()) - { + if (igmp_receive()) { continue; } }