2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/igmp/igmp.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
|
|
|
#include "netutils/ipmsfilter.h"
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#include "igmp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Check if the destination address is a multicast address
|
|
|
|
*
|
|
|
|
* - IPv4: multicast addresses lie in the class D group -- The address range
|
|
|
|
* 224.0.0.0 to 239.255.255.255 (224.0.0.0/4)
|
|
|
|
*
|
|
|
|
* - IPv6 multicast addresses are have the high-order octet of the
|
|
|
|
* addresses=0xff (ff00::/8.)
|
|
|
|
*/
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
#if ((CONFIG_EXAMPLES_IGMP_GRPADDR & 0xffff0000) < 0xe0000000ul) || \
|
|
|
|
((CONFIG_EXAMPLES_IGMP_GRPADDR & 0xffff0000) > 0xeffffffful)
|
2011-03-20 19:18:19 +01:00
|
|
|
# error "Bad range for IGMP group address"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* igmp_main
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
struct in_addr addr;
|
2018-10-29 13:51:56 +01:00
|
|
|
struct in_addr mcast;
|
2012-10-04 19:36:07 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_IGMP_NOMAC)
|
2011-03-20 19:18:19 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Configuring Ethernet...\n");
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_IGMP_NOMAC
|
2011-03-20 19:18:19 +01:00
|
|
|
mac[0] = 0x00;
|
|
|
|
mac[1] = 0xe0;
|
2012-07-20 00:32:19 +02:00
|
|
|
mac[2] = 0xde;
|
|
|
|
mac[3] = 0xad;
|
|
|
|
mac[4] = 0xbe;
|
|
|
|
mac[5] = 0xef;
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr("eth0", mac);
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_IGMP_IPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr("eth0", &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_IGMP_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr("eth0", &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_IGMP_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask("eth0", &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2017-05-19 17:30:26 +02:00
|
|
|
/* New versions of netlib_set_ipvXaddr will not bring the network up,
|
2017-05-19 17:50:26 +02:00
|
|
|
* So ensure the network is really up at this point.
|
|
|
|
*/
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
netlib_ifup("eth0");
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Not much of a test for now */
|
2018-10-29 13:51:56 +01:00
|
|
|
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_IGMP_IPADDR);
|
|
|
|
mcast.s_addr = HTONL(CONFIG_EXAMPLES_IGMP_GRPADDR);
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Join the group */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Join group...\n");
|
2018-10-29 13:51:56 +01:00
|
|
|
ipmsfilter(&addr, &mcast, MCAST_INCLUDE);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Wait a while */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Wait for timeout...\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
sleep(5);
|
|
|
|
|
|
|
|
/* Leave the group */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Leave group...\n");
|
2018-10-29 13:51:56 +01:00
|
|
|
ipmsfilter(&addr, &mcast, MCAST_EXCLUDE);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Wait a while */
|
|
|
|
|
|
|
|
sleep(5);
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Exiting...\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|