From 0cfc04eac370ee33118e17a298d4739c94cacc73 Mon Sep 17 00:00:00 2001 From: Vladimir Oltean Date: Tue, 19 Apr 2022 12:28:03 +0300 Subject: [PATCH 1/6] mreceive: refactor multicast joining to separate function Signed-off-by: Vladimir Oltean --- mreceive.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) --- a/mreceive.c +++ b/mreceive.c @@ -61,12 +61,27 @@ Usage: mreceive [-g GROUP] [-p PORT] [-i -h Print the command usage.\n\n", VERSION); } +static void igmp_join(int s, in_addr_t multiaddr, in_addr_t interface) +{ + struct ip_mreq mreq; + int ret; + + mreq.imr_multiaddr.s_addr = multiaddr; + mreq.imr_interface.s_addr = interface; + + ret = setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, + (char *)&mreq, sizeof(mreq)); + if (ret == SOCKET_ERROR) { + printf("setsockopt() IP_ADD_MEMBERSHIP failed.\n"); + exit(1); + } +} + int main(int argc, char *argv[]) { struct sockaddr_in stLocal, stFrom; unsigned char achIn[BUFSIZE]; int s, i; - struct ip_mreq stMreq; int iTmp, iRet; int ipnum = 0; int ii; @@ -153,22 +168,10 @@ int main(int argc, char *argv[]) /* join the multicast group. */ if (!ipnum) { /* single interface */ - stMreq.imr_multiaddr.s_addr = inet_addr(TEST_ADDR); - stMreq.imr_interface.s_addr = INADDR_ANY; - iRet = setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)); - if (iRet == SOCKET_ERROR) { - printf("setsockopt() IP_ADD_MEMBERSHIP failed.\n"); - exit(1); - } + igmp_join(s, inet_addr(TEST_ADDR), INADDR_ANY); } else { for (i = 0; i < ipnum; i++) { - stMreq.imr_multiaddr.s_addr = inet_addr(TEST_ADDR); - stMreq.imr_interface.s_addr = IP[i]; - iRet = setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)); - if (iRet == SOCKET_ERROR) { - printf("setsockopt() IP_ADD_MEMBERSHIP failed.\n"); - exit(1); - } + igmp_join(s, inet_addr(TEST_ADDR), IP[i]); } }