Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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