I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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