Donatien Garnier / ZG2100NetIfSource
Revision:
1:3a7c15057192
Parent:
0:b802fc31f1db
--- a/if/lwip/lwipNetUdpSocket.cpp	Fri Jul 09 15:37:23 2010 +0000
+++ b/if/lwip/lwipNetUdpSocket.cpp	Fri Aug 06 10:23:41 2010 +0000
@@ -21,8 +21,11 @@
 THE SOFTWARE.
 */
 
+#include "lwip/ip_addr.h"
 #include "lwipNetUdpSocket.h"
 #include "lwip/udp.h"
+#include "lwip/igmp.h"
+
 
 //#define __DEBUG
 #include "dbg/dbg.h"
@@ -30,9 +33,9 @@
 #include "netCfg.h"
 #if NET_LWIP_STACK
 
-LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
+LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt(), m_multicastGroup() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
 {
-  DBG("\r\nNew LwipNetUdpSocket %p (pPCb=%p)\r\n", (void*)this, (void*) pPcb);
+  DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb);
   if(!m_pPcb)
     m_pPcb = udp_new();
   if(m_pPcb)
@@ -49,10 +52,23 @@
   
 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
 {
+  err_t err;
+  
   if(!m_pPcb)
     return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
+   
+  #if LWIP_IGMP //Multicast support enabled
+  if(me.getIp().isMulticast())
+  {
+    DBG("This is a multicast addr, joining multicast group\n");
+    m_multicastGroup = me.getIp();
+    err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
+    if(err)
+      return NETUDPSOCKET_IF; //Could not find or create group
+  }
+  #endif
 
-  err_t err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
+  err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
   if(err)
     return NETUDPSOCKET_INUSE;
 
@@ -85,7 +101,7 @@
   pbuf_free( p );
   if(err)
     return NETUDPSOCKET_SETUP; //Connection problem
-  DBG("\r\n%d bytes sent in UDP Socket.\r\n", len);
+  DBG("%d bytes sent in UDP Socket.\n", len);
   return len;
 }
 
@@ -157,7 +173,7 @@
 
 NetUdpSocketErr LwipNetUdpSocket::close()
 {
-  DBG("\r\nLwipNetUdpSocket::close() : Closing...\r\n");
+  DBG("LwipNetUdpSocket::close() : Closing...\n");
 
   if(m_closed)
     return NETUDPSOCKET_OK; //Already being closed
@@ -166,12 +182,13 @@
   if( !m_pPcb ) //Pcb doesn't exist (anymore)
     return NETUDPSOCKET_MEM;
     
-  DBG("\r\nLwipNetUdpSocket::close() : Cleanup...\r\n");
+  DBG("LwipNetUdpSocket::close() : Cleanup...\n");
     
   //Cleanup incoming data
   cleanUp();
- 
-  DBG("\r\nLwipNetUdpSocket::close() : removing m_pPcb...\r\n");
+  
+
+  DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n");
   udp_remove( (udp_pcb*) m_pPcb);
     
   m_pPcb = NULL;
@@ -188,7 +205,7 @@
 
 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
 {
-  DBG("\r\n Packet of length %d arrived in UDP Socket.\r\n", p->tot_len);
+  DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len);
   list<InPacket>::iterator it;
   for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
   {
@@ -219,6 +236,15 @@
     udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
   }
   
+  //Leaving multicast group(Ok because LwIP has a refscount for multicast group)
+  #if LWIP_IGMP //Multicast support enabled
+  if(m_multicastGroup.isMulticast())
+  {
+    igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
+    m_multicastGroup = IpAddr();
+  }
+  #endif
+  
   list<InPacket>::iterator it;
   for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
   {