HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1 #pragma diag_remark 177
mr_q 0:d8f2f7d5f31b 2 /**
mr_q 0:d8f2f7d5f31b 3 * @file
mr_q 0:d8f2f7d5f31b 4 * IGMP - Internet Group Management Protocol
mr_q 0:d8f2f7d5f31b 5 *
mr_q 0:d8f2f7d5f31b 6 */
mr_q 0:d8f2f7d5f31b 7
mr_q 0:d8f2f7d5f31b 8 /*
mr_q 0:d8f2f7d5f31b 9 * Copyright (c) 2002 CITEL Technologies Ltd.
mr_q 0:d8f2f7d5f31b 10 * All rights reserved.
mr_q 0:d8f2f7d5f31b 11 *
mr_q 0:d8f2f7d5f31b 12 * Redistribution and use in source and binary forms, with or without
mr_q 0:d8f2f7d5f31b 13 * modification, are permitted provided that the following conditions
mr_q 0:d8f2f7d5f31b 14 * are met:
mr_q 0:d8f2f7d5f31b 15 * 1. Redistributions of source code must retain the above copyright
mr_q 0:d8f2f7d5f31b 16 * notice, this list of conditions and the following disclaimer.
mr_q 0:d8f2f7d5f31b 17 * 2. Redistributions in binary form must reproduce the above copyright
mr_q 0:d8f2f7d5f31b 18 * notice, this list of conditions and the following disclaimer in the
mr_q 0:d8f2f7d5f31b 19 * documentation and/or other materials provided with the distribution.
mr_q 0:d8f2f7d5f31b 20 * 3. Neither the name of CITEL Technologies Ltd nor the names of its contributors
mr_q 0:d8f2f7d5f31b 21 * may be used to endorse or promote products derived from this software
mr_q 0:d8f2f7d5f31b 22 * without specific prior written permission.
mr_q 0:d8f2f7d5f31b 23 *
mr_q 0:d8f2f7d5f31b 24 * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
mr_q 0:d8f2f7d5f31b 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mr_q 0:d8f2f7d5f31b 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mr_q 0:d8f2f7d5f31b 27 * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
mr_q 0:d8f2f7d5f31b 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mr_q 0:d8f2f7d5f31b 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
mr_q 0:d8f2f7d5f31b 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
mr_q 0:d8f2f7d5f31b 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
mr_q 0:d8f2f7d5f31b 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
mr_q 0:d8f2f7d5f31b 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
mr_q 0:d8f2f7d5f31b 34 * SUCH DAMAGE.
mr_q 0:d8f2f7d5f31b 35 *
mr_q 0:d8f2f7d5f31b 36 * This file is a contribution to the lwIP TCP/IP stack.
mr_q 0:d8f2f7d5f31b 37 * The Swedish Institute of Computer Science and Adam Dunkels
mr_q 0:d8f2f7d5f31b 38 * are specifically granted permission to redistribute this
mr_q 0:d8f2f7d5f31b 39 * source code.
mr_q 0:d8f2f7d5f31b 40 */
mr_q 0:d8f2f7d5f31b 41
mr_q 0:d8f2f7d5f31b 42 /*-------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 43 Note 1)
mr_q 0:d8f2f7d5f31b 44 Although the rfc requires V1 AND V2 capability
mr_q 0:d8f2f7d5f31b 45 we will only support v2 since now V1 is very old (August 1989)
mr_q 0:d8f2f7d5f31b 46 V1 can be added if required
mr_q 0:d8f2f7d5f31b 47
mr_q 0:d8f2f7d5f31b 48 a debug print and statistic have been implemented to
mr_q 0:d8f2f7d5f31b 49 show this up.
mr_q 0:d8f2f7d5f31b 50 -------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 51 -------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 52 Note 2)
mr_q 0:d8f2f7d5f31b 53 A query for a specific group address (as opposed to ALLHOSTS)
mr_q 0:d8f2f7d5f31b 54 has now been implemented as I am unsure if it is required
mr_q 0:d8f2f7d5f31b 55
mr_q 0:d8f2f7d5f31b 56 a debug print and statistic have been implemented to
mr_q 0:d8f2f7d5f31b 57 show this up.
mr_q 0:d8f2f7d5f31b 58 -------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 59 -------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 60 Note 3)
mr_q 0:d8f2f7d5f31b 61 The router alert rfc 2113 is implemented in outgoing packets
mr_q 0:d8f2f7d5f31b 62 but not checked rigorously incoming
mr_q 0:d8f2f7d5f31b 63 -------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 64 Steve Reynolds
mr_q 0:d8f2f7d5f31b 65 ------------------------------------------------------------*/
mr_q 0:d8f2f7d5f31b 66
mr_q 0:d8f2f7d5f31b 67 /*-----------------------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 68 * RFC 988 - Host extensions for IP multicasting - V0
mr_q 0:d8f2f7d5f31b 69 * RFC 1054 - Host extensions for IP multicasting -
mr_q 0:d8f2f7d5f31b 70 * RFC 1112 - Host extensions for IP multicasting - V1
mr_q 0:d8f2f7d5f31b 71 * RFC 2236 - Internet Group Management Protocol, Version 2 - V2 <- this code is based on this RFC (it's the "de facto" standard)
mr_q 0:d8f2f7d5f31b 72 * RFC 3376 - Internet Group Management Protocol, Version 3 - V3
mr_q 0:d8f2f7d5f31b 73 * RFC 4604 - Using Internet Group Management Protocol Version 3... - V3+
mr_q 0:d8f2f7d5f31b 74 * RFC 2113 - IP Router Alert Option -
mr_q 0:d8f2f7d5f31b 75 *----------------------------------------------------------------------------*/
mr_q 0:d8f2f7d5f31b 76
mr_q 0:d8f2f7d5f31b 77 /*-----------------------------------------------------------------------------
mr_q 0:d8f2f7d5f31b 78 * Includes
mr_q 0:d8f2f7d5f31b 79 *----------------------------------------------------------------------------*/
mr_q 0:d8f2f7d5f31b 80
mr_q 0:d8f2f7d5f31b 81 #include "lwip/opt.h"
mr_q 0:d8f2f7d5f31b 82
mr_q 0:d8f2f7d5f31b 83 #if LWIP_IGMP /* don't build if not configured for use in lwipopts.h */
mr_q 0:d8f2f7d5f31b 84
mr_q 0:d8f2f7d5f31b 85 #include "lwip/igmp.h"
mr_q 0:d8f2f7d5f31b 86 #include "lwip/debug.h"
mr_q 0:d8f2f7d5f31b 87 #include "lwip/def.h"
mr_q 0:d8f2f7d5f31b 88 #include "lwip/mem.h"
mr_q 0:d8f2f7d5f31b 89 #include "lwip/ip.h"
mr_q 0:d8f2f7d5f31b 90 #include "lwip/inet_chksum.h"
mr_q 0:d8f2f7d5f31b 91 #include "lwip/netif.h"
mr_q 0:d8f2f7d5f31b 92 #include "lwip/icmp.h"
mr_q 0:d8f2f7d5f31b 93 #include "lwip/udp.h"
mr_q 0:d8f2f7d5f31b 94 #include "lwip/tcp.h"
mr_q 0:d8f2f7d5f31b 95 #include "lwip/stats.h"
mr_q 0:d8f2f7d5f31b 96
mr_q 0:d8f2f7d5f31b 97 #include "string.h"
mr_q 0:d8f2f7d5f31b 98
mr_q 0:d8f2f7d5f31b 99 /*
mr_q 0:d8f2f7d5f31b 100 * IGMP constants
mr_q 0:d8f2f7d5f31b 101 */
mr_q 0:d8f2f7d5f31b 102 #define IGMP_TTL 1
mr_q 0:d8f2f7d5f31b 103 #define IGMP_MINLEN 8
mr_q 0:d8f2f7d5f31b 104 #define ROUTER_ALERT 0x9404
mr_q 0:d8f2f7d5f31b 105 #define ROUTER_ALERTLEN 4
mr_q 0:d8f2f7d5f31b 106
mr_q 0:d8f2f7d5f31b 107 /*
mr_q 0:d8f2f7d5f31b 108 * IGMP message types, including version number.
mr_q 0:d8f2f7d5f31b 109 */
mr_q 0:d8f2f7d5f31b 110 #define IGMP_MEMB_QUERY 0x11 /* Membership query */
mr_q 0:d8f2f7d5f31b 111 #define IGMP_V1_MEMB_REPORT 0x12 /* Ver. 1 membership report */
mr_q 0:d8f2f7d5f31b 112 #define IGMP_V2_MEMB_REPORT 0x16 /* Ver. 2 membership report */
mr_q 0:d8f2f7d5f31b 113 #define IGMP_LEAVE_GROUP 0x17 /* Leave-group message */
mr_q 0:d8f2f7d5f31b 114
mr_q 0:d8f2f7d5f31b 115 /* Group membership states */
mr_q 0:d8f2f7d5f31b 116 #define IGMP_GROUP_NON_MEMBER 0
mr_q 0:d8f2f7d5f31b 117 #define IGMP_GROUP_DELAYING_MEMBER 1
mr_q 0:d8f2f7d5f31b 118 #define IGMP_GROUP_IDLE_MEMBER 2
mr_q 0:d8f2f7d5f31b 119
mr_q 0:d8f2f7d5f31b 120 /**
mr_q 0:d8f2f7d5f31b 121 * IGMP packet format.
mr_q 0:d8f2f7d5f31b 122 */
mr_q 0:d8f2f7d5f31b 123 #ifdef PACK_STRUCT_USE_INCLUDES
mr_q 0:d8f2f7d5f31b 124 # include "arch/bpstruct.h"
mr_q 0:d8f2f7d5f31b 125 #endif
mr_q 0:d8f2f7d5f31b 126 PACK_STRUCT_BEGIN
mr_q 0:d8f2f7d5f31b 127 struct igmp_msg {
mr_q 0:d8f2f7d5f31b 128 PACK_STRUCT_FIELD(u8_t igmp_msgtype);
mr_q 0:d8f2f7d5f31b 129 PACK_STRUCT_FIELD(u8_t igmp_maxresp);
mr_q 0:d8f2f7d5f31b 130 PACK_STRUCT_FIELD(u16_t igmp_checksum);
mr_q 0:d8f2f7d5f31b 131 PACK_STRUCT_FIELD(ip_addr_p_t igmp_group_address);
mr_q 0:d8f2f7d5f31b 132 } PACK_STRUCT_STRUCT;
mr_q 0:d8f2f7d5f31b 133 PACK_STRUCT_END
mr_q 0:d8f2f7d5f31b 134 #ifdef PACK_STRUCT_USE_INCLUDES
mr_q 0:d8f2f7d5f31b 135 # include "arch/epstruct.h"
mr_q 0:d8f2f7d5f31b 136 #endif
mr_q 0:d8f2f7d5f31b 137
mr_q 0:d8f2f7d5f31b 138
mr_q 0:d8f2f7d5f31b 139 static struct igmp_group *igmp_lookup_group(struct netif *ifp, ip_addr_t *addr);
mr_q 0:d8f2f7d5f31b 140 static err_t igmp_remove_group(struct igmp_group *group);
mr_q 0:d8f2f7d5f31b 141 static void igmp_timeout( struct igmp_group *group);
mr_q 0:d8f2f7d5f31b 142 static void igmp_start_timer(struct igmp_group *group, u8_t max_time);
mr_q 0:d8f2f7d5f31b 143 static void igmp_stop_timer(struct igmp_group *group);
mr_q 0:d8f2f7d5f31b 144 static void igmp_delaying_member(struct igmp_group *group, u8_t maxresp);
mr_q 0:d8f2f7d5f31b 145 static err_t igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, struct netif *netif);
mr_q 0:d8f2f7d5f31b 146 static void igmp_send(struct igmp_group *group, u8_t type);
mr_q 0:d8f2f7d5f31b 147
mr_q 0:d8f2f7d5f31b 148
mr_q 0:d8f2f7d5f31b 149 static struct igmp_group* igmp_group_list;
mr_q 0:d8f2f7d5f31b 150 static ip_addr_t allsystems;
mr_q 0:d8f2f7d5f31b 151 static ip_addr_t allrouters;
mr_q 0:d8f2f7d5f31b 152
mr_q 0:d8f2f7d5f31b 153
mr_q 0:d8f2f7d5f31b 154 /**
mr_q 0:d8f2f7d5f31b 155 * Initialize the IGMP module
mr_q 0:d8f2f7d5f31b 156 */
mr_q 0:d8f2f7d5f31b 157 void
mr_q 0:d8f2f7d5f31b 158 igmp_init(void)
mr_q 0:d8f2f7d5f31b 159 {
mr_q 0:d8f2f7d5f31b 160 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_init: initializing\n"));
mr_q 0:d8f2f7d5f31b 161
mr_q 0:d8f2f7d5f31b 162 IP4_ADDR(&allsystems, 224, 0, 0, 1);
mr_q 0:d8f2f7d5f31b 163 IP4_ADDR(&allrouters, 224, 0, 0, 2);
mr_q 0:d8f2f7d5f31b 164 }
mr_q 0:d8f2f7d5f31b 165
mr_q 0:d8f2f7d5f31b 166 #ifdef LWIP_DEBUG
mr_q 0:d8f2f7d5f31b 167 /**
mr_q 0:d8f2f7d5f31b 168 * Dump global IGMP groups list
mr_q 0:d8f2f7d5f31b 169 */
mr_q 0:d8f2f7d5f31b 170 void
mr_q 0:d8f2f7d5f31b 171 igmp_dump_group_list()
mr_q 0:d8f2f7d5f31b 172 {
mr_q 0:d8f2f7d5f31b 173 struct igmp_group *group = igmp_group_list;
mr_q 0:d8f2f7d5f31b 174
mr_q 0:d8f2f7d5f31b 175 while (group != NULL) {
mr_q 0:d8f2f7d5f31b 176 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_dump_group_list: [%"U32_F"] ", (u32_t)(group->group_state)));
mr_q 0:d8f2f7d5f31b 177 ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
mr_q 0:d8f2f7d5f31b 178 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
mr_q 0:d8f2f7d5f31b 179 group = group->next;
mr_q 0:d8f2f7d5f31b 180 }
mr_q 0:d8f2f7d5f31b 181 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
mr_q 0:d8f2f7d5f31b 182 }
mr_q 0:d8f2f7d5f31b 183 #else
mr_q 0:d8f2f7d5f31b 184 #define igmp_dump_group_list()
mr_q 0:d8f2f7d5f31b 185 #endif /* LWIP_DEBUG */
mr_q 0:d8f2f7d5f31b 186
mr_q 0:d8f2f7d5f31b 187 /**
mr_q 0:d8f2f7d5f31b 188 * Start IGMP processing on interface
mr_q 0:d8f2f7d5f31b 189 *
mr_q 0:d8f2f7d5f31b 190 * @param netif network interface on which start IGMP processing
mr_q 0:d8f2f7d5f31b 191 */
mr_q 0:d8f2f7d5f31b 192 err_t
mr_q 0:d8f2f7d5f31b 193 igmp_start(struct netif *netif)
mr_q 0:d8f2f7d5f31b 194 {
mr_q 0:d8f2f7d5f31b 195 struct igmp_group* group;
mr_q 0:d8f2f7d5f31b 196
mr_q 0:d8f2f7d5f31b 197 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: starting IGMP processing on if %p\n", netif));
mr_q 0:d8f2f7d5f31b 198
mr_q 0:d8f2f7d5f31b 199 group = igmp_lookup_group(netif, &allsystems);
mr_q 0:d8f2f7d5f31b 200
mr_q 0:d8f2f7d5f31b 201 if (group != NULL) {
mr_q 0:d8f2f7d5f31b 202 group->group_state = IGMP_GROUP_IDLE_MEMBER;
mr_q 0:d8f2f7d5f31b 203 group->use++;
mr_q 0:d8f2f7d5f31b 204
mr_q 0:d8f2f7d5f31b 205 /* Allow the igmp messages at the MAC level */
mr_q 0:d8f2f7d5f31b 206 if (netif->igmp_mac_filter != NULL) {
mr_q 0:d8f2f7d5f31b 207 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: igmp_mac_filter(ADD "));
mr_q 0:d8f2f7d5f31b 208 ip_addr_debug_print(IGMP_DEBUG, &allsystems);
mr_q 0:d8f2f7d5f31b 209 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
mr_q 0:d8f2f7d5f31b 210 netif->igmp_mac_filter(netif, &allsystems, IGMP_ADD_MAC_FILTER);
mr_q 0:d8f2f7d5f31b 211 }
mr_q 0:d8f2f7d5f31b 212
mr_q 0:d8f2f7d5f31b 213 return ERR_OK;
mr_q 0:d8f2f7d5f31b 214 }
mr_q 0:d8f2f7d5f31b 215
mr_q 0:d8f2f7d5f31b 216 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 217 }
mr_q 0:d8f2f7d5f31b 218
mr_q 0:d8f2f7d5f31b 219 /**
mr_q 0:d8f2f7d5f31b 220 * Stop IGMP processing on interface
mr_q 0:d8f2f7d5f31b 221 *
mr_q 0:d8f2f7d5f31b 222 * @param netif network interface on which stop IGMP processing
mr_q 0:d8f2f7d5f31b 223 */
mr_q 0:d8f2f7d5f31b 224 err_t
mr_q 0:d8f2f7d5f31b 225 igmp_stop(struct netif *netif)
mr_q 0:d8f2f7d5f31b 226 {
mr_q 0:d8f2f7d5f31b 227 struct igmp_group *group = igmp_group_list;
mr_q 0:d8f2f7d5f31b 228 struct igmp_group *prev = NULL;
mr_q 0:d8f2f7d5f31b 229 struct igmp_group *next;
mr_q 0:d8f2f7d5f31b 230
mr_q 0:d8f2f7d5f31b 231 /* look for groups joined on this interface further down the list */
mr_q 0:d8f2f7d5f31b 232 while (group != NULL) {
mr_q 0:d8f2f7d5f31b 233 next = group->next;
mr_q 0:d8f2f7d5f31b 234 /* is it a group joined on this interface? */
mr_q 0:d8f2f7d5f31b 235 if (group->netif == netif) {
mr_q 0:d8f2f7d5f31b 236 /* is it the first group of the list? */
mr_q 0:d8f2f7d5f31b 237 if (group == igmp_group_list) {
mr_q 0:d8f2f7d5f31b 238 igmp_group_list = next;
mr_q 0:d8f2f7d5f31b 239 }
mr_q 0:d8f2f7d5f31b 240 /* is there a "previous" group defined? */
mr_q 0:d8f2f7d5f31b 241 if (prev != NULL) {
mr_q 0:d8f2f7d5f31b 242 prev->next = next;
mr_q 0:d8f2f7d5f31b 243 }
mr_q 0:d8f2f7d5f31b 244 /* disable the group at the MAC level */
mr_q 0:d8f2f7d5f31b 245 if (netif->igmp_mac_filter != NULL) {
mr_q 0:d8f2f7d5f31b 246 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_stop: igmp_mac_filter(DEL "));
mr_q 0:d8f2f7d5f31b 247 ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
mr_q 0:d8f2f7d5f31b 248 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
mr_q 0:d8f2f7d5f31b 249 netif->igmp_mac_filter(netif, &(group->group_address), IGMP_DEL_MAC_FILTER);
mr_q 0:d8f2f7d5f31b 250 }
mr_q 0:d8f2f7d5f31b 251 /* free group */
mr_q 0:d8f2f7d5f31b 252 memp_free(MEMP_IGMP_GROUP, group);
mr_q 0:d8f2f7d5f31b 253 } else {
mr_q 0:d8f2f7d5f31b 254 /* change the "previous" */
mr_q 0:d8f2f7d5f31b 255 prev = group;
mr_q 0:d8f2f7d5f31b 256 }
mr_q 0:d8f2f7d5f31b 257 /* move to "next" */
mr_q 0:d8f2f7d5f31b 258 group = next;
mr_q 0:d8f2f7d5f31b 259 }
mr_q 0:d8f2f7d5f31b 260 return ERR_OK;
mr_q 0:d8f2f7d5f31b 261 }
mr_q 0:d8f2f7d5f31b 262
mr_q 0:d8f2f7d5f31b 263 /**
mr_q 0:d8f2f7d5f31b 264 * Report IGMP memberships for this interface
mr_q 0:d8f2f7d5f31b 265 *
mr_q 0:d8f2f7d5f31b 266 * @param netif network interface on which report IGMP memberships
mr_q 0:d8f2f7d5f31b 267 */
mr_q 0:d8f2f7d5f31b 268 void
mr_q 0:d8f2f7d5f31b 269 igmp_report_groups(struct netif *netif)
mr_q 0:d8f2f7d5f31b 270 {
mr_q 0:d8f2f7d5f31b 271 struct igmp_group *group = igmp_group_list;
mr_q 0:d8f2f7d5f31b 272
mr_q 0:d8f2f7d5f31b 273 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_report_groups: sending IGMP reports on if %p\n", netif));
mr_q 0:d8f2f7d5f31b 274
mr_q 0:d8f2f7d5f31b 275 while (group != NULL) {
mr_q 0:d8f2f7d5f31b 276 if (group->netif == netif) {
mr_q 0:d8f2f7d5f31b 277 igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
mr_q 0:d8f2f7d5f31b 278 }
mr_q 0:d8f2f7d5f31b 279 group = group->next;
mr_q 0:d8f2f7d5f31b 280 }
mr_q 0:d8f2f7d5f31b 281 }
mr_q 0:d8f2f7d5f31b 282
mr_q 0:d8f2f7d5f31b 283 /**
mr_q 0:d8f2f7d5f31b 284 * Search for a group in the global igmp_group_list
mr_q 0:d8f2f7d5f31b 285 *
mr_q 0:d8f2f7d5f31b 286 * @param ifp the network interface for which to look
mr_q 0:d8f2f7d5f31b 287 * @param addr the group ip address to search for
mr_q 0:d8f2f7d5f31b 288 * @return a struct igmp_group* if the group has been found,
mr_q 0:d8f2f7d5f31b 289 * NULL if the group wasn't found.
mr_q 0:d8f2f7d5f31b 290 */
mr_q 0:d8f2f7d5f31b 291 struct igmp_group *
mr_q 0:d8f2f7d5f31b 292 igmp_lookfor_group(struct netif *ifp, ip_addr_t *addr)
mr_q 0:d8f2f7d5f31b 293 {
mr_q 0:d8f2f7d5f31b 294 struct igmp_group *group = igmp_group_list;
mr_q 0:d8f2f7d5f31b 295
mr_q 0:d8f2f7d5f31b 296 while (group != NULL) {
mr_q 0:d8f2f7d5f31b 297 if ((group->netif == ifp) && (ip_addr_cmp(&(group->group_address), addr))) {
mr_q 0:d8f2f7d5f31b 298 return group;
mr_q 0:d8f2f7d5f31b 299 }
mr_q 0:d8f2f7d5f31b 300 group = group->next;
mr_q 0:d8f2f7d5f31b 301 }
mr_q 0:d8f2f7d5f31b 302
mr_q 0:d8f2f7d5f31b 303 /* to be clearer, we return NULL here instead of
mr_q 0:d8f2f7d5f31b 304 * 'group' (which is also NULL at this point).
mr_q 0:d8f2f7d5f31b 305 */
mr_q 0:d8f2f7d5f31b 306 return NULL;
mr_q 0:d8f2f7d5f31b 307 }
mr_q 0:d8f2f7d5f31b 308
mr_q 0:d8f2f7d5f31b 309 /**
mr_q 0:d8f2f7d5f31b 310 * Search for a specific igmp group and create a new one if not found-
mr_q 0:d8f2f7d5f31b 311 *
mr_q 0:d8f2f7d5f31b 312 * @param ifp the network interface for which to look
mr_q 0:d8f2f7d5f31b 313 * @param addr the group ip address to search
mr_q 0:d8f2f7d5f31b 314 * @return a struct igmp_group*,
mr_q 0:d8f2f7d5f31b 315 * NULL on memory error.
mr_q 0:d8f2f7d5f31b 316 */
mr_q 0:d8f2f7d5f31b 317 struct igmp_group *
mr_q 0:d8f2f7d5f31b 318 igmp_lookup_group(struct netif *ifp, ip_addr_t *addr)
mr_q 0:d8f2f7d5f31b 319 {
mr_q 0:d8f2f7d5f31b 320 struct igmp_group *group = igmp_group_list;
mr_q 0:d8f2f7d5f31b 321
mr_q 0:d8f2f7d5f31b 322 /* Search if the group already exists */
mr_q 0:d8f2f7d5f31b 323 group = igmp_lookfor_group(ifp, addr);
mr_q 0:d8f2f7d5f31b 324 if (group != NULL) {
mr_q 0:d8f2f7d5f31b 325 /* Group already exists. */
mr_q 0:d8f2f7d5f31b 326 return group;
mr_q 0:d8f2f7d5f31b 327 }
mr_q 0:d8f2f7d5f31b 328
mr_q 0:d8f2f7d5f31b 329 /* Group doesn't exist yet, create a new one */
mr_q 0:d8f2f7d5f31b 330 group = (struct igmp_group *)memp_malloc(MEMP_IGMP_GROUP);
mr_q 0:d8f2f7d5f31b 331 if (group != NULL) {
mr_q 0:d8f2f7d5f31b 332 group->netif = ifp;
mr_q 0:d8f2f7d5f31b 333 ip_addr_set(&(group->group_address), addr);
mr_q 0:d8f2f7d5f31b 334 group->timer = 0; /* Not running */
mr_q 0:d8f2f7d5f31b 335 group->group_state = IGMP_GROUP_NON_MEMBER;
mr_q 0:d8f2f7d5f31b 336 group->last_reporter_flag = 0;
mr_q 0:d8f2f7d5f31b 337 group->use = 0;
mr_q 0:d8f2f7d5f31b 338 group->next = igmp_group_list;
mr_q 0:d8f2f7d5f31b 339
mr_q 0:d8f2f7d5f31b 340 igmp_group_list = group;
mr_q 0:d8f2f7d5f31b 341 }
mr_q 0:d8f2f7d5f31b 342
mr_q 0:d8f2f7d5f31b 343 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_lookup_group: %sallocated a new group with address ", (group?"":"impossible to ")));
mr_q 0:d8f2f7d5f31b 344 ip_addr_debug_print(IGMP_DEBUG, addr);
mr_q 0:d8f2f7d5f31b 345 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", ifp));
mr_q 0:d8f2f7d5f31b 346
mr_q 0:d8f2f7d5f31b 347 return group;
mr_q 0:d8f2f7d5f31b 348 }
mr_q 0:d8f2f7d5f31b 349
mr_q 0:d8f2f7d5f31b 350 /**
mr_q 0:d8f2f7d5f31b 351 * Remove a group in the global igmp_group_list
mr_q 0:d8f2f7d5f31b 352 *
mr_q 0:d8f2f7d5f31b 353 * @param group the group to remove from the global igmp_group_list
mr_q 0:d8f2f7d5f31b 354 * @return ERR_OK if group was removed from the list, an err_t otherwise
mr_q 0:d8f2f7d5f31b 355 */
mr_q 0:d8f2f7d5f31b 356 static err_t
mr_q 0:d8f2f7d5f31b 357 igmp_remove_group(struct igmp_group *group)
mr_q 0:d8f2f7d5f31b 358 {
mr_q 0:d8f2f7d5f31b 359 err_t err = ERR_OK;
mr_q 0:d8f2f7d5f31b 360
mr_q 0:d8f2f7d5f31b 361 /* Is it the first group? */
mr_q 0:d8f2f7d5f31b 362 if (igmp_group_list == group) {
mr_q 0:d8f2f7d5f31b 363 igmp_group_list = group->next;
mr_q 0:d8f2f7d5f31b 364 } else {
mr_q 0:d8f2f7d5f31b 365 /* look for group further down the list */
mr_q 0:d8f2f7d5f31b 366 struct igmp_group *tmpGroup;
mr_q 0:d8f2f7d5f31b 367 for (tmpGroup = igmp_group_list; tmpGroup != NULL; tmpGroup = tmpGroup->next) {
mr_q 0:d8f2f7d5f31b 368 if (tmpGroup->next == group) {
mr_q 0:d8f2f7d5f31b 369 tmpGroup->next = group->next;
mr_q 0:d8f2f7d5f31b 370 break;
mr_q 0:d8f2f7d5f31b 371 }
mr_q 0:d8f2f7d5f31b 372 }
mr_q 0:d8f2f7d5f31b 373 /* Group not found in the global igmp_group_list */
mr_q 0:d8f2f7d5f31b 374 if (tmpGroup == NULL)
mr_q 0:d8f2f7d5f31b 375 err = ERR_ARG;
mr_q 0:d8f2f7d5f31b 376 }
mr_q 0:d8f2f7d5f31b 377 /* free group */
mr_q 0:d8f2f7d5f31b 378 memp_free(MEMP_IGMP_GROUP, group);
mr_q 0:d8f2f7d5f31b 379
mr_q 0:d8f2f7d5f31b 380 return err;
mr_q 0:d8f2f7d5f31b 381 }
mr_q 0:d8f2f7d5f31b 382
mr_q 0:d8f2f7d5f31b 383 /**
mr_q 0:d8f2f7d5f31b 384 * Called from ip_input() if a new IGMP packet is received.
mr_q 0:d8f2f7d5f31b 385 *
mr_q 0:d8f2f7d5f31b 386 * @param p received igmp packet, p->payload pointing to the ip header
mr_q 0:d8f2f7d5f31b 387 * @param inp network interface on which the packet was received
mr_q 0:d8f2f7d5f31b 388 * @param dest destination ip address of the igmp packet
mr_q 0:d8f2f7d5f31b 389 */
mr_q 0:d8f2f7d5f31b 390 void
mr_q 0:d8f2f7d5f31b 391 igmp_input(struct pbuf *p, struct netif *inp, ip_addr_t *dest)
mr_q 0:d8f2f7d5f31b 392 {
mr_q 0:d8f2f7d5f31b 393 struct ip_hdr * iphdr;
mr_q 0:d8f2f7d5f31b 394 struct igmp_msg* igmp;
mr_q 0:d8f2f7d5f31b 395 struct igmp_group* group;
mr_q 0:d8f2f7d5f31b 396 struct igmp_group* groupref;
mr_q 0:d8f2f7d5f31b 397
mr_q 0:d8f2f7d5f31b 398 IGMP_STATS_INC(igmp.recv);
mr_q 0:d8f2f7d5f31b 399
mr_q 0:d8f2f7d5f31b 400 /* Note that the length CAN be greater than 8 but only 8 are used - All are included in the checksum */
mr_q 0:d8f2f7d5f31b 401 iphdr = (struct ip_hdr *)p->payload;
mr_q 0:d8f2f7d5f31b 402 if (pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4)) || (p->len < IGMP_MINLEN)) {
mr_q 0:d8f2f7d5f31b 403 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 404 IGMP_STATS_INC(igmp.lenerr);
mr_q 0:d8f2f7d5f31b 405 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: length error\n"));
mr_q 0:d8f2f7d5f31b 406 return;
mr_q 0:d8f2f7d5f31b 407 }
mr_q 0:d8f2f7d5f31b 408
mr_q 0:d8f2f7d5f31b 409 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: message from "));
mr_q 0:d8f2f7d5f31b 410 ip_addr_debug_print(IGMP_DEBUG, &(iphdr->src));
mr_q 0:d8f2f7d5f31b 411 LWIP_DEBUGF(IGMP_DEBUG, (" to address "));
mr_q 0:d8f2f7d5f31b 412 ip_addr_debug_print(IGMP_DEBUG, &(iphdr->dest));
mr_q 0:d8f2f7d5f31b 413 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", inp));
mr_q 0:d8f2f7d5f31b 414
mr_q 0:d8f2f7d5f31b 415 /* Now calculate and check the checksum */
mr_q 0:d8f2f7d5f31b 416 igmp = (struct igmp_msg *)p->payload;
mr_q 0:d8f2f7d5f31b 417 if (inet_chksum(igmp, p->len)) {
mr_q 0:d8f2f7d5f31b 418 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 419 IGMP_STATS_INC(igmp.chkerr);
mr_q 0:d8f2f7d5f31b 420 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: checksum error\n"));
mr_q 0:d8f2f7d5f31b 421 return;
mr_q 0:d8f2f7d5f31b 422 }
mr_q 0:d8f2f7d5f31b 423
mr_q 0:d8f2f7d5f31b 424 /* Packet is ok so find an existing group */
mr_q 0:d8f2f7d5f31b 425 group = igmp_lookfor_group(inp, dest); /* use the destination IP address of incoming packet */
mr_q 0:d8f2f7d5f31b 426
mr_q 0:d8f2f7d5f31b 427 /* If group can be found or create... */
mr_q 0:d8f2f7d5f31b 428 if (!group) {
mr_q 0:d8f2f7d5f31b 429 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 430 IGMP_STATS_INC(igmp.drop);
mr_q 0:d8f2f7d5f31b 431 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP frame not for us\n"));
mr_q 0:d8f2f7d5f31b 432 return;
mr_q 0:d8f2f7d5f31b 433 }
mr_q 0:d8f2f7d5f31b 434
mr_q 0:d8f2f7d5f31b 435 /* NOW ACT ON THE INCOMING MESSAGE TYPE... */
mr_q 0:d8f2f7d5f31b 436 switch (igmp->igmp_msgtype) {
mr_q 0:d8f2f7d5f31b 437 case IGMP_MEMB_QUERY: {
mr_q 0:d8f2f7d5f31b 438 /* IGMP_MEMB_QUERY to the "all systems" address ? */
mr_q 0:d8f2f7d5f31b 439 if ((ip_addr_cmp(dest, &allsystems)) && ip_addr_isany(&igmp->igmp_group_address)) {
mr_q 0:d8f2f7d5f31b 440 /* THIS IS THE GENERAL QUERY */
mr_q 0:d8f2f7d5f31b 441 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: General IGMP_MEMB_QUERY on \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
mr_q 0:d8f2f7d5f31b 442
mr_q 0:d8f2f7d5f31b 443 if (igmp->igmp_maxresp == 0) {
mr_q 0:d8f2f7d5f31b 444 IGMP_STATS_INC(igmp.rx_v1);
mr_q 0:d8f2f7d5f31b 445 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n"));
mr_q 0:d8f2f7d5f31b 446 igmp->igmp_maxresp = IGMP_V1_DELAYING_MEMBER_TMR;
mr_q 0:d8f2f7d5f31b 447 } else {
mr_q 0:d8f2f7d5f31b 448 IGMP_STATS_INC(igmp.rx_general);
mr_q 0:d8f2f7d5f31b 449 }
mr_q 0:d8f2f7d5f31b 450
mr_q 0:d8f2f7d5f31b 451 groupref = igmp_group_list;
mr_q 0:d8f2f7d5f31b 452 while (groupref) {
mr_q 0:d8f2f7d5f31b 453 /* Do not send messages on the all systems group address! */
mr_q 0:d8f2f7d5f31b 454 if ((groupref->netif == inp) && (!(ip_addr_cmp(&(groupref->group_address), &allsystems)))) {
mr_q 0:d8f2f7d5f31b 455 igmp_delaying_member(groupref, igmp->igmp_maxresp);
mr_q 0:d8f2f7d5f31b 456 }
mr_q 0:d8f2f7d5f31b 457 groupref = groupref->next;
mr_q 0:d8f2f7d5f31b 458 }
mr_q 0:d8f2f7d5f31b 459 } else {
mr_q 0:d8f2f7d5f31b 460 /* IGMP_MEMB_QUERY to a specific group ? */
mr_q 0:d8f2f7d5f31b 461 if (!ip_addr_isany(&igmp->igmp_group_address)) {
mr_q 0:d8f2f7d5f31b 462 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_MEMB_QUERY to a specific group "));
mr_q 0:d8f2f7d5f31b 463 ip_addr_debug_print(IGMP_DEBUG, &igmp->igmp_group_address);
mr_q 0:d8f2f7d5f31b 464 if (ip_addr_cmp(dest, &allsystems)) {
mr_q 0:d8f2f7d5f31b 465 ip_addr_t groupaddr;
mr_q 0:d8f2f7d5f31b 466 LWIP_DEBUGF(IGMP_DEBUG, (" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
mr_q 0:d8f2f7d5f31b 467 /* we first need to re-look for the group since we used dest last time */
mr_q 0:d8f2f7d5f31b 468 ip_addr_copy(groupaddr, igmp->igmp_group_address);
mr_q 0:d8f2f7d5f31b 469 group = igmp_lookfor_group(inp, &groupaddr);
mr_q 0:d8f2f7d5f31b 470 } else {
mr_q 0:d8f2f7d5f31b 471 LWIP_DEBUGF(IGMP_DEBUG, (" with the group address as destination [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
mr_q 0:d8f2f7d5f31b 472 }
mr_q 0:d8f2f7d5f31b 473
mr_q 0:d8f2f7d5f31b 474 if (group != NULL) {
mr_q 0:d8f2f7d5f31b 475 IGMP_STATS_INC(igmp.rx_group);
mr_q 0:d8f2f7d5f31b 476 igmp_delaying_member(group, igmp->igmp_maxresp);
mr_q 0:d8f2f7d5f31b 477 } else {
mr_q 0:d8f2f7d5f31b 478 IGMP_STATS_INC(igmp.drop);
mr_q 0:d8f2f7d5f31b 479 }
mr_q 0:d8f2f7d5f31b 480 } else {
mr_q 0:d8f2f7d5f31b 481 IGMP_STATS_INC(igmp.proterr);
mr_q 0:d8f2f7d5f31b 482 }
mr_q 0:d8f2f7d5f31b 483 }
mr_q 0:d8f2f7d5f31b 484 break;
mr_q 0:d8f2f7d5f31b 485 }
mr_q 0:d8f2f7d5f31b 486 case IGMP_V2_MEMB_REPORT: {
mr_q 0:d8f2f7d5f31b 487 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_V2_MEMB_REPORT\n"));
mr_q 0:d8f2f7d5f31b 488 IGMP_STATS_INC(igmp.rx_report);
mr_q 0:d8f2f7d5f31b 489 if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
mr_q 0:d8f2f7d5f31b 490 /* This is on a specific group we have already looked up */
mr_q 0:d8f2f7d5f31b 491 group->timer = 0; /* stopped */
mr_q 0:d8f2f7d5f31b 492 group->group_state = IGMP_GROUP_IDLE_MEMBER;
mr_q 0:d8f2f7d5f31b 493 group->last_reporter_flag = 0;
mr_q 0:d8f2f7d5f31b 494 }
mr_q 0:d8f2f7d5f31b 495 break;
mr_q 0:d8f2f7d5f31b 496 }
mr_q 0:d8f2f7d5f31b 497 default: {
mr_q 0:d8f2f7d5f31b 498 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: unexpected msg %d in state %d on group %p on if %p\n",
mr_q 0:d8f2f7d5f31b 499 igmp->igmp_msgtype, group->group_state, &group, group->netif));
mr_q 0:d8f2f7d5f31b 500 IGMP_STATS_INC(igmp.proterr);
mr_q 0:d8f2f7d5f31b 501 break;
mr_q 0:d8f2f7d5f31b 502 }
mr_q 0:d8f2f7d5f31b 503 }
mr_q 0:d8f2f7d5f31b 504
mr_q 0:d8f2f7d5f31b 505 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 506 return;
mr_q 0:d8f2f7d5f31b 507 }
mr_q 0:d8f2f7d5f31b 508
mr_q 0:d8f2f7d5f31b 509 /**
mr_q 0:d8f2f7d5f31b 510 * Join a group on one network interface.
mr_q 0:d8f2f7d5f31b 511 *
mr_q 0:d8f2f7d5f31b 512 * @param ifaddr ip address of the network interface which should join a new group
mr_q 0:d8f2f7d5f31b 513 * @param groupaddr the ip address of the group which to join
mr_q 0:d8f2f7d5f31b 514 * @return ERR_OK if group was joined on the netif(s), an err_t otherwise
mr_q 0:d8f2f7d5f31b 515 */
mr_q 0:d8f2f7d5f31b 516 err_t
mr_q 0:d8f2f7d5f31b 517 igmp_joingroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr)
mr_q 0:d8f2f7d5f31b 518 {
mr_q 0:d8f2f7d5f31b 519 err_t err = ERR_VAL; /* no matching interface */
mr_q 0:d8f2f7d5f31b 520 struct igmp_group *group;
mr_q 0:d8f2f7d5f31b 521 struct netif *netif;
mr_q 0:d8f2f7d5f31b 522
mr_q 0:d8f2f7d5f31b 523 /* make sure it is multicast address */
mr_q 0:d8f2f7d5f31b 524 LWIP_ERROR("igmp_joingroup: attempt to join non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
mr_q 0:d8f2f7d5f31b 525 LWIP_ERROR("igmp_joingroup: attempt to join allsystems address", (!ip_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
mr_q 0:d8f2f7d5f31b 526
mr_q 0:d8f2f7d5f31b 527 /* loop through netif's */
mr_q 0:d8f2f7d5f31b 528 netif = netif_list;
mr_q 0:d8f2f7d5f31b 529 while (netif != NULL) {
mr_q 0:d8f2f7d5f31b 530 /* Should we join this interface ? */
mr_q 0:d8f2f7d5f31b 531 if ((netif->flags & NETIF_FLAG_IGMP) && ((ip_addr_isany(ifaddr) || ip_addr_cmp(&(netif->ip_addr), ifaddr)))) {
mr_q 0:d8f2f7d5f31b 532 /* find group or create a new one if not found */
mr_q 0:d8f2f7d5f31b 533 group = igmp_lookup_group(netif, groupaddr);
mr_q 0:d8f2f7d5f31b 534
mr_q 0:d8f2f7d5f31b 535 if (group != NULL) {
mr_q 0:d8f2f7d5f31b 536 /* This should create a new group, check the state to make sure */
mr_q 0:d8f2f7d5f31b 537 if (group->group_state != IGMP_GROUP_NON_MEMBER) {
mr_q 0:d8f2f7d5f31b 538 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: join to group not in state IGMP_GROUP_NON_MEMBER\n"));
mr_q 0:d8f2f7d5f31b 539 } else {
mr_q 0:d8f2f7d5f31b 540 /* OK - it was new group */
mr_q 0:d8f2f7d5f31b 541 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: join to new group: "));
mr_q 0:d8f2f7d5f31b 542 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
mr_q 0:d8f2f7d5f31b 543 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
mr_q 0:d8f2f7d5f31b 544
mr_q 0:d8f2f7d5f31b 545 /* If first use of the group, allow the group at the MAC level */
mr_q 0:d8f2f7d5f31b 546 if ((group->use==0) && (netif->igmp_mac_filter != NULL)) {
mr_q 0:d8f2f7d5f31b 547 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: igmp_mac_filter(ADD "));
mr_q 0:d8f2f7d5f31b 548 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
mr_q 0:d8f2f7d5f31b 549 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
mr_q 0:d8f2f7d5f31b 550 netif->igmp_mac_filter(netif, groupaddr, IGMP_ADD_MAC_FILTER);
mr_q 0:d8f2f7d5f31b 551 }
mr_q 0:d8f2f7d5f31b 552
mr_q 0:d8f2f7d5f31b 553 IGMP_STATS_INC(igmp.tx_join);
mr_q 0:d8f2f7d5f31b 554 igmp_send(group, IGMP_V2_MEMB_REPORT);
mr_q 0:d8f2f7d5f31b 555
mr_q 0:d8f2f7d5f31b 556 igmp_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
mr_q 0:d8f2f7d5f31b 557
mr_q 0:d8f2f7d5f31b 558 /* Need to work out where this timer comes from */
mr_q 0:d8f2f7d5f31b 559 group->group_state = IGMP_GROUP_DELAYING_MEMBER;
mr_q 0:d8f2f7d5f31b 560 }
mr_q 0:d8f2f7d5f31b 561 /* Increment group use */
mr_q 0:d8f2f7d5f31b 562 group->use++;
mr_q 0:d8f2f7d5f31b 563 /* Join on this interface */
mr_q 0:d8f2f7d5f31b 564 err = ERR_OK;
mr_q 0:d8f2f7d5f31b 565 } else {
mr_q 0:d8f2f7d5f31b 566 /* Return an error even if some network interfaces are joined */
mr_q 0:d8f2f7d5f31b 567 /** @todo undo any other netif already joined */
mr_q 0:d8f2f7d5f31b 568 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: Not enought memory to join to group\n"));
mr_q 0:d8f2f7d5f31b 569 return ERR_MEM;
mr_q 0:d8f2f7d5f31b 570 }
mr_q 0:d8f2f7d5f31b 571 }
mr_q 0:d8f2f7d5f31b 572 /* proceed to next network interface */
mr_q 0:d8f2f7d5f31b 573 netif = netif->next;
mr_q 0:d8f2f7d5f31b 574 }
mr_q 0:d8f2f7d5f31b 575
mr_q 0:d8f2f7d5f31b 576 return err;
mr_q 0:d8f2f7d5f31b 577 }
mr_q 0:d8f2f7d5f31b 578
mr_q 0:d8f2f7d5f31b 579 /**
mr_q 0:d8f2f7d5f31b 580 * Leave a group on one network interface.
mr_q 0:d8f2f7d5f31b 581 *
mr_q 0:d8f2f7d5f31b 582 * @param ifaddr ip address of the network interface which should leave a group
mr_q 0:d8f2f7d5f31b 583 * @param groupaddr the ip address of the group which to leave
mr_q 0:d8f2f7d5f31b 584 * @return ERR_OK if group was left on the netif(s), an err_t otherwise
mr_q 0:d8f2f7d5f31b 585 */
mr_q 0:d8f2f7d5f31b 586 err_t
mr_q 0:d8f2f7d5f31b 587 igmp_leavegroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr)
mr_q 0:d8f2f7d5f31b 588 {
mr_q 0:d8f2f7d5f31b 589 err_t err = ERR_VAL; /* no matching interface */
mr_q 0:d8f2f7d5f31b 590 struct igmp_group *group;
mr_q 0:d8f2f7d5f31b 591 struct netif *netif;
mr_q 0:d8f2f7d5f31b 592
mr_q 0:d8f2f7d5f31b 593 /* make sure it is multicast address */
mr_q 0:d8f2f7d5f31b 594 LWIP_ERROR("igmp_leavegroup: attempt to leave non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
mr_q 0:d8f2f7d5f31b 595 LWIP_ERROR("igmp_leavegroup: attempt to leave allsystems address", (!ip_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
mr_q 0:d8f2f7d5f31b 596
mr_q 0:d8f2f7d5f31b 597 /* loop through netif's */
mr_q 0:d8f2f7d5f31b 598 netif = netif_list;
mr_q 0:d8f2f7d5f31b 599 while (netif != NULL) {
mr_q 0:d8f2f7d5f31b 600 /* Should we leave this interface ? */
mr_q 0:d8f2f7d5f31b 601 if ((netif->flags & NETIF_FLAG_IGMP) && ((ip_addr_isany(ifaddr) || ip_addr_cmp(&(netif->ip_addr), ifaddr)))) {
mr_q 0:d8f2f7d5f31b 602 /* find group */
mr_q 0:d8f2f7d5f31b 603 group = igmp_lookfor_group(netif, groupaddr);
mr_q 0:d8f2f7d5f31b 604
mr_q 0:d8f2f7d5f31b 605 if (group != NULL) {
mr_q 0:d8f2f7d5f31b 606 /* Only send a leave if the flag is set according to the state diagram */
mr_q 0:d8f2f7d5f31b 607 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: Leaving group: "));
mr_q 0:d8f2f7d5f31b 608 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
mr_q 0:d8f2f7d5f31b 609 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
mr_q 0:d8f2f7d5f31b 610
mr_q 0:d8f2f7d5f31b 611 /* If there is no other use of the group */
mr_q 0:d8f2f7d5f31b 612 if (group->use <= 1) {
mr_q 0:d8f2f7d5f31b 613 /* If we are the last reporter for this group */
mr_q 0:d8f2f7d5f31b 614 if (group->last_reporter_flag) {
mr_q 0:d8f2f7d5f31b 615 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: sending leaving group\n"));
mr_q 0:d8f2f7d5f31b 616 IGMP_STATS_INC(igmp.tx_leave);
mr_q 0:d8f2f7d5f31b 617 igmp_send(group, IGMP_LEAVE_GROUP);
mr_q 0:d8f2f7d5f31b 618 }
mr_q 0:d8f2f7d5f31b 619
mr_q 0:d8f2f7d5f31b 620 /* Disable the group at the MAC level */
mr_q 0:d8f2f7d5f31b 621 if (netif->igmp_mac_filter != NULL) {
mr_q 0:d8f2f7d5f31b 622 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: igmp_mac_filter(DEL "));
mr_q 0:d8f2f7d5f31b 623 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
mr_q 0:d8f2f7d5f31b 624 LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
mr_q 0:d8f2f7d5f31b 625 netif->igmp_mac_filter(netif, groupaddr, IGMP_DEL_MAC_FILTER);
mr_q 0:d8f2f7d5f31b 626 }
mr_q 0:d8f2f7d5f31b 627
mr_q 0:d8f2f7d5f31b 628 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: remove group: "));
mr_q 0:d8f2f7d5f31b 629 ip_addr_debug_print(IGMP_DEBUG, groupaddr);
mr_q 0:d8f2f7d5f31b 630 LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
mr_q 0:d8f2f7d5f31b 631
mr_q 0:d8f2f7d5f31b 632 /* Free the group */
mr_q 0:d8f2f7d5f31b 633 igmp_remove_group(group);
mr_q 0:d8f2f7d5f31b 634 } else {
mr_q 0:d8f2f7d5f31b 635 /* Decrement group use */
mr_q 0:d8f2f7d5f31b 636 group->use--;
mr_q 0:d8f2f7d5f31b 637 }
mr_q 0:d8f2f7d5f31b 638 /* Leave on this interface */
mr_q 0:d8f2f7d5f31b 639 err = ERR_OK;
mr_q 0:d8f2f7d5f31b 640 } else {
mr_q 0:d8f2f7d5f31b 641 /* It's not a fatal error on "leavegroup" */
mr_q 0:d8f2f7d5f31b 642 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: not member of group\n"));
mr_q 0:d8f2f7d5f31b 643 }
mr_q 0:d8f2f7d5f31b 644 }
mr_q 0:d8f2f7d5f31b 645 /* proceed to next network interface */
mr_q 0:d8f2f7d5f31b 646 netif = netif->next;
mr_q 0:d8f2f7d5f31b 647 }
mr_q 0:d8f2f7d5f31b 648
mr_q 0:d8f2f7d5f31b 649 return err;
mr_q 0:d8f2f7d5f31b 650 }
mr_q 0:d8f2f7d5f31b 651
mr_q 0:d8f2f7d5f31b 652 /**
mr_q 0:d8f2f7d5f31b 653 * The igmp timer function (both for NO_SYS=1 and =0)
mr_q 0:d8f2f7d5f31b 654 * Should be called every IGMP_TMR_INTERVAL milliseconds (100 ms is default).
mr_q 0:d8f2f7d5f31b 655 */
mr_q 0:d8f2f7d5f31b 656 void
mr_q 0:d8f2f7d5f31b 657 igmp_tmr(void)
mr_q 0:d8f2f7d5f31b 658 {
mr_q 0:d8f2f7d5f31b 659 struct igmp_group *group = igmp_group_list;
mr_q 0:d8f2f7d5f31b 660
mr_q 0:d8f2f7d5f31b 661 while (group != NULL) {
mr_q 0:d8f2f7d5f31b 662 if (group->timer > 0) {
mr_q 0:d8f2f7d5f31b 663 group->timer--;
mr_q 0:d8f2f7d5f31b 664 if (group->timer == 0) {
mr_q 0:d8f2f7d5f31b 665 igmp_timeout(group);
mr_q 0:d8f2f7d5f31b 666 }
mr_q 0:d8f2f7d5f31b 667 }
mr_q 0:d8f2f7d5f31b 668 group = group->next;
mr_q 0:d8f2f7d5f31b 669 }
mr_q 0:d8f2f7d5f31b 670 }
mr_q 0:d8f2f7d5f31b 671
mr_q 0:d8f2f7d5f31b 672 /**
mr_q 0:d8f2f7d5f31b 673 * Called if a timeout for one group is reached.
mr_q 0:d8f2f7d5f31b 674 * Sends a report for this group.
mr_q 0:d8f2f7d5f31b 675 *
mr_q 0:d8f2f7d5f31b 676 * @param group an igmp_group for which a timeout is reached
mr_q 0:d8f2f7d5f31b 677 */
mr_q 0:d8f2f7d5f31b 678 static void
mr_q 0:d8f2f7d5f31b 679 igmp_timeout(struct igmp_group *group)
mr_q 0:d8f2f7d5f31b 680 {
mr_q 0:d8f2f7d5f31b 681 /* If the state is IGMP_GROUP_DELAYING_MEMBER then we send a report for this group */
mr_q 0:d8f2f7d5f31b 682 if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
mr_q 0:d8f2f7d5f31b 683 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_timeout: report membership for group with address "));
mr_q 0:d8f2f7d5f31b 684 ip_addr_debug_print(IGMP_DEBUG, &(group->group_address));
mr_q 0:d8f2f7d5f31b 685 LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
mr_q 0:d8f2f7d5f31b 686
mr_q 0:d8f2f7d5f31b 687 IGMP_STATS_INC(igmp.tx_report);
mr_q 0:d8f2f7d5f31b 688 igmp_send(group, IGMP_V2_MEMB_REPORT);
mr_q 0:d8f2f7d5f31b 689 }
mr_q 0:d8f2f7d5f31b 690 }
mr_q 0:d8f2f7d5f31b 691
mr_q 0:d8f2f7d5f31b 692 /**
mr_q 0:d8f2f7d5f31b 693 * Start a timer for an igmp group
mr_q 0:d8f2f7d5f31b 694 *
mr_q 0:d8f2f7d5f31b 695 * @param group the igmp_group for which to start a timer
mr_q 0:d8f2f7d5f31b 696 * @param max_time the time in multiples of IGMP_TMR_INTERVAL (decrease with
mr_q 0:d8f2f7d5f31b 697 * every call to igmp_tmr())
mr_q 0:d8f2f7d5f31b 698 */
mr_q 0:d8f2f7d5f31b 699 static void
mr_q 0:d8f2f7d5f31b 700 igmp_start_timer(struct igmp_group *group, u8_t max_time)
mr_q 0:d8f2f7d5f31b 701 {
mr_q 0:d8f2f7d5f31b 702 /* ensure the input value is > 0 */
mr_q 0:d8f2f7d5f31b 703 if (max_time == 0) {
mr_q 0:d8f2f7d5f31b 704 max_time = 1;
mr_q 0:d8f2f7d5f31b 705 }
mr_q 0:d8f2f7d5f31b 706 /* ensure the random value is > 0 */
mr_q 0:d8f2f7d5f31b 707 group->timer = (LWIP_RAND() % (max_time - 1)) + 1;
mr_q 0:d8f2f7d5f31b 708 }
mr_q 0:d8f2f7d5f31b 709
mr_q 0:d8f2f7d5f31b 710 /**
mr_q 0:d8f2f7d5f31b 711 * Stop a timer for an igmp_group
mr_q 0:d8f2f7d5f31b 712 *
mr_q 0:d8f2f7d5f31b 713 * @param group the igmp_group for which to stop the timer
mr_q 0:d8f2f7d5f31b 714 */
mr_q 0:d8f2f7d5f31b 715 static void
mr_q 0:d8f2f7d5f31b 716 igmp_stop_timer(struct igmp_group *group)
mr_q 0:d8f2f7d5f31b 717 {
mr_q 0:d8f2f7d5f31b 718 group->timer = 0;
mr_q 0:d8f2f7d5f31b 719 }
mr_q 0:d8f2f7d5f31b 720
mr_q 0:d8f2f7d5f31b 721 /**
mr_q 0:d8f2f7d5f31b 722 * Delaying membership report for a group if necessary
mr_q 0:d8f2f7d5f31b 723 *
mr_q 0:d8f2f7d5f31b 724 * @param group the igmp_group for which "delaying" membership report
mr_q 0:d8f2f7d5f31b 725 * @param maxresp query delay
mr_q 0:d8f2f7d5f31b 726 */
mr_q 0:d8f2f7d5f31b 727 static void
mr_q 0:d8f2f7d5f31b 728 igmp_delaying_member(struct igmp_group *group, u8_t maxresp)
mr_q 0:d8f2f7d5f31b 729 {
mr_q 0:d8f2f7d5f31b 730 if ((group->group_state == IGMP_GROUP_IDLE_MEMBER) ||
mr_q 0:d8f2f7d5f31b 731 ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
mr_q 0:d8f2f7d5f31b 732 ((group->timer == 0) || (maxresp < group->timer)))) {
mr_q 0:d8f2f7d5f31b 733 igmp_start_timer(group, maxresp);
mr_q 0:d8f2f7d5f31b 734 group->group_state = IGMP_GROUP_DELAYING_MEMBER;
mr_q 0:d8f2f7d5f31b 735 }
mr_q 0:d8f2f7d5f31b 736 }
mr_q 0:d8f2f7d5f31b 737
mr_q 0:d8f2f7d5f31b 738
mr_q 0:d8f2f7d5f31b 739 /**
mr_q 0:d8f2f7d5f31b 740 * Sends an IP packet on a network interface. This function constructs the IP header
mr_q 0:d8f2f7d5f31b 741 * and calculates the IP header checksum. If the source IP address is NULL,
mr_q 0:d8f2f7d5f31b 742 * the IP address of the outgoing network interface is filled in as source address.
mr_q 0:d8f2f7d5f31b 743 *
mr_q 0:d8f2f7d5f31b 744 * @param p the packet to send (p->payload points to the data, e.g. next
mr_q 0:d8f2f7d5f31b 745 protocol header; if dest == IP_HDRINCL, p already includes an IP
mr_q 0:d8f2f7d5f31b 746 header and p->payload points to that IP header)
mr_q 0:d8f2f7d5f31b 747 * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
mr_q 0:d8f2f7d5f31b 748 * IP address of the netif used to send is used as source address)
mr_q 0:d8f2f7d5f31b 749 * @param dest the destination IP address to send the packet to
mr_q 0:d8f2f7d5f31b 750 * @param ttl the TTL value to be set in the IP header
mr_q 0:d8f2f7d5f31b 751 * @param proto the PROTOCOL to be set in the IP header
mr_q 0:d8f2f7d5f31b 752 * @param netif the netif on which to send this packet
mr_q 0:d8f2f7d5f31b 753 * @return ERR_OK if the packet was sent OK
mr_q 0:d8f2f7d5f31b 754 * ERR_BUF if p doesn't have enough space for IP/LINK headers
mr_q 0:d8f2f7d5f31b 755 * returns errors returned by netif->output
mr_q 0:d8f2f7d5f31b 756 */
mr_q 0:d8f2f7d5f31b 757 static err_t
mr_q 0:d8f2f7d5f31b 758 igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, struct netif *netif)
mr_q 0:d8f2f7d5f31b 759 {
mr_q 0:d8f2f7d5f31b 760 /* This is the "router alert" option */
mr_q 0:d8f2f7d5f31b 761 u16_t ra[2];
mr_q 0:d8f2f7d5f31b 762 ra[0] = PP_HTONS(ROUTER_ALERT);
mr_q 0:d8f2f7d5f31b 763 ra[1] = 0x0000; /* Router shall examine packet */
mr_q 0:d8f2f7d5f31b 764 IGMP_STATS_INC(igmp.xmit);
mr_q 0:d8f2f7d5f31b 765 return ip_output_if_opt(p, src, dest, IGMP_TTL, 0, IP_PROTO_IGMP, netif, ra, ROUTER_ALERTLEN);
mr_q 0:d8f2f7d5f31b 766 }
mr_q 0:d8f2f7d5f31b 767
mr_q 0:d8f2f7d5f31b 768 /**
mr_q 0:d8f2f7d5f31b 769 * Send an igmp packet to a specific group.
mr_q 0:d8f2f7d5f31b 770 *
mr_q 0:d8f2f7d5f31b 771 * @param group the group to which to send the packet
mr_q 0:d8f2f7d5f31b 772 * @param type the type of igmp packet to send
mr_q 0:d8f2f7d5f31b 773 */
mr_q 0:d8f2f7d5f31b 774 static void
mr_q 0:d8f2f7d5f31b 775 igmp_send(struct igmp_group *group, u8_t type)
mr_q 0:d8f2f7d5f31b 776 {
mr_q 0:d8f2f7d5f31b 777 struct pbuf* p = NULL;
mr_q 0:d8f2f7d5f31b 778 struct igmp_msg* igmp = NULL;
mr_q 0:d8f2f7d5f31b 779 ip_addr_t src = *IP_ADDR_ANY;
mr_q 0:d8f2f7d5f31b 780 ip_addr_t* dest = NULL;
mr_q 0:d8f2f7d5f31b 781
mr_q 0:d8f2f7d5f31b 782 /* IP header + "router alert" option + IGMP header */
mr_q 0:d8f2f7d5f31b 783 p = pbuf_alloc(PBUF_TRANSPORT, IGMP_MINLEN, PBUF_RAM);
mr_q 0:d8f2f7d5f31b 784
mr_q 0:d8f2f7d5f31b 785 if (p) {
mr_q 0:d8f2f7d5f31b 786 igmp = (struct igmp_msg *)p->payload;
mr_q 0:d8f2f7d5f31b 787 LWIP_ASSERT("igmp_send: check that first pbuf can hold struct igmp_msg",
mr_q 0:d8f2f7d5f31b 788 (p->len >= sizeof(struct igmp_msg)));
mr_q 0:d8f2f7d5f31b 789 ip_addr_copy(src, group->netif->ip_addr);
mr_q 0:d8f2f7d5f31b 790
mr_q 0:d8f2f7d5f31b 791 if (type == IGMP_V2_MEMB_REPORT) {
mr_q 0:d8f2f7d5f31b 792 dest = &(group->group_address);
mr_q 0:d8f2f7d5f31b 793 ip_addr_copy(igmp->igmp_group_address, group->group_address);
mr_q 0:d8f2f7d5f31b 794 group->last_reporter_flag = 1; /* Remember we were the last to report */
mr_q 0:d8f2f7d5f31b 795 } else {
mr_q 0:d8f2f7d5f31b 796 if (type == IGMP_LEAVE_GROUP) {
mr_q 0:d8f2f7d5f31b 797 dest = &allrouters;
mr_q 0:d8f2f7d5f31b 798 ip_addr_copy(igmp->igmp_group_address, group->group_address);
mr_q 0:d8f2f7d5f31b 799 }
mr_q 0:d8f2f7d5f31b 800 }
mr_q 0:d8f2f7d5f31b 801
mr_q 0:d8f2f7d5f31b 802 if ((type == IGMP_V2_MEMB_REPORT) || (type == IGMP_LEAVE_GROUP)) {
mr_q 0:d8f2f7d5f31b 803 igmp->igmp_msgtype = type;
mr_q 0:d8f2f7d5f31b 804 igmp->igmp_maxresp = 0;
mr_q 0:d8f2f7d5f31b 805 igmp->igmp_checksum = 0;
mr_q 0:d8f2f7d5f31b 806 igmp->igmp_checksum = inet_chksum(igmp, IGMP_MINLEN);
mr_q 0:d8f2f7d5f31b 807
mr_q 0:d8f2f7d5f31b 808 igmp_ip_output_if(p, &src, dest, group->netif);
mr_q 0:d8f2f7d5f31b 809 }
mr_q 0:d8f2f7d5f31b 810
mr_q 0:d8f2f7d5f31b 811 pbuf_free(p);
mr_q 0:d8f2f7d5f31b 812 } else {
mr_q 0:d8f2f7d5f31b 813 LWIP_DEBUGF(IGMP_DEBUG, ("igmp_send: not enough memory for igmp_send\n"));
mr_q 0:d8f2f7d5f31b 814 IGMP_STATS_INC(igmp.memerr);
mr_q 0:d8f2f7d5f31b 815 }
mr_q 0:d8f2f7d5f31b 816 }
mr_q 0:d8f2f7d5f31b 817
mr_q 0:d8f2f7d5f31b 818 #endif /* LWIP_IGMP */