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

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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