Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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