Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
segundo 0:ac1725ba162c 1 #pragma diag_remark 1464
segundo 0:ac1725ba162c 2 /*
segundo 0:ac1725ba162c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
segundo 0:ac1725ba162c 4
segundo 0:ac1725ba162c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
segundo 0:ac1725ba162c 6 of this software and associated documentation files (the "Software"), to deal
segundo 0:ac1725ba162c 7 in the Software without restriction, including without limitation the rights
segundo 0:ac1725ba162c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
segundo 0:ac1725ba162c 9 copies of the Software, and to permit persons to whom the Software is
segundo 0:ac1725ba162c 10 furnished to do so, subject to the following conditions:
segundo 0:ac1725ba162c 11
segundo 0:ac1725ba162c 12 The above copyright notice and this permission notice shall be included in
segundo 0:ac1725ba162c 13 all copies or substantial portions of the Software.
segundo 0:ac1725ba162c 14
segundo 0:ac1725ba162c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
segundo 0:ac1725ba162c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
segundo 0:ac1725ba162c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
segundo 0:ac1725ba162c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
segundo 0:ac1725ba162c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
segundo 0:ac1725ba162c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
segundo 0:ac1725ba162c 21 THE SOFTWARE.
segundo 0:ac1725ba162c 22 */
segundo 0:ac1725ba162c 23
segundo 0:ac1725ba162c 24 #include "lwip/ip_addr.h"
segundo 0:ac1725ba162c 25 #include "lwipNetUdpSocket.h"
segundo 0:ac1725ba162c 26 #include "lwip/udp.h"
segundo 0:ac1725ba162c 27 #include "lwip/igmp.h"
segundo 0:ac1725ba162c 28
segundo 0:ac1725ba162c 29
segundo 0:ac1725ba162c 30 //#define __DEBUG
segundo 0:ac1725ba162c 31 #include "dbg/dbg.h"
segundo 0:ac1725ba162c 32
segundo 0:ac1725ba162c 33 #include "netCfg.h"
segundo 0:ac1725ba162c 34 #if NET_LWIP_STACK
segundo 0:ac1725ba162c 35
segundo 0:ac1725ba162c 36 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
segundo 0:ac1725ba162c 37 {
segundo 0:ac1725ba162c 38 DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb);
segundo 0:ac1725ba162c 39 if(!m_pPcb)
segundo 0:ac1725ba162c 40 m_pPcb = udp_new();
segundo 0:ac1725ba162c 41 if(m_pPcb)
segundo 0:ac1725ba162c 42 {
segundo 0:ac1725ba162c 43 //Setup callback
segundo 0:ac1725ba162c 44 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
segundo 0:ac1725ba162c 45 }
segundo 0:ac1725ba162c 46 }
segundo 0:ac1725ba162c 47
segundo 0:ac1725ba162c 48 LwipNetUdpSocket::~LwipNetUdpSocket()
segundo 0:ac1725ba162c 49 {
segundo 0:ac1725ba162c 50 close();
segundo 0:ac1725ba162c 51 }
segundo 0:ac1725ba162c 52
segundo 0:ac1725ba162c 53 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
segundo 0:ac1725ba162c 54 {
segundo 0:ac1725ba162c 55 err_t err;
segundo 0:ac1725ba162c 56
segundo 0:ac1725ba162c 57 if(!m_pPcb)
segundo 0:ac1725ba162c 58 return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
segundo 0:ac1725ba162c 59
segundo 0:ac1725ba162c 60 #if LWIP_IGMP //Multicast support enabled
segundo 0:ac1725ba162c 61 if(me.getIp().isMulticast())
segundo 0:ac1725ba162c 62 {
segundo 0:ac1725ba162c 63 DBG("This is a multicast addr, joining multicast group\n");
segundo 0:ac1725ba162c 64 m_multicastGroup = me.getIp();
segundo 0:ac1725ba162c 65 err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
segundo 0:ac1725ba162c 66 if(err)
segundo 0:ac1725ba162c 67 return NETUDPSOCKET_IF; //Could not find or create group
segundo 0:ac1725ba162c 68 }
segundo 0:ac1725ba162c 69 #endif
segundo 0:ac1725ba162c 70
segundo 0:ac1725ba162c 71 err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
segundo 0:ac1725ba162c 72 if(err)
segundo 0:ac1725ba162c 73 return NETUDPSOCKET_INUSE;
segundo 0:ac1725ba162c 74
segundo 0:ac1725ba162c 75 //Setup callback
segundo 0:ac1725ba162c 76 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
segundo 0:ac1725ba162c 77
segundo 0:ac1725ba162c 78 return NETUDPSOCKET_OK;
segundo 0:ac1725ba162c 79 }
segundo 0:ac1725ba162c 80
segundo 0:ac1725ba162c 81 #define MAX(a,b) ((a>b)?a:b)
segundo 0:ac1725ba162c 82 #define MIN(a,b) ((a<b)?a:b)
segundo 0:ac1725ba162c 83
segundo 0:ac1725ba162c 84 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost)
segundo 0:ac1725ba162c 85 {
segundo 0:ac1725ba162c 86 if( !m_pPcb ) //Pcb doesn't exist (anymore)
segundo 0:ac1725ba162c 87 return NETUDPSOCKET_MEM;
segundo 0:ac1725ba162c 88 pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
segundo 0:ac1725ba162c 89 if( !p )
segundo 0:ac1725ba162c 90 return NETUDPSOCKET_MEM;
segundo 0:ac1725ba162c 91 char* pBuf = (char*) buf;
segundo 0:ac1725ba162c 92 pbuf* q = p;
segundo 0:ac1725ba162c 93 do
segundo 0:ac1725ba162c 94 {
segundo 0:ac1725ba162c 95 memcpy (q->payload, (void*)pBuf, q->len);
segundo 0:ac1725ba162c 96 pBuf += q->len;
segundo 0:ac1725ba162c 97 q = q->next;
segundo 0:ac1725ba162c 98 } while(q != NULL);
segundo 0:ac1725ba162c 99
segundo 0:ac1725ba162c 100 err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() );
segundo 0:ac1725ba162c 101 pbuf_free( p );
segundo 0:ac1725ba162c 102 if(err)
segundo 0:ac1725ba162c 103 return NETUDPSOCKET_SETUP; //Connection problem
segundo 0:ac1725ba162c 104 DBG("%d bytes sent in UDP Socket.\n", len);
segundo 0:ac1725ba162c 105 return len;
segundo 0:ac1725ba162c 106 }
segundo 0:ac1725ba162c 107
segundo 0:ac1725ba162c 108 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost)
segundo 0:ac1725ba162c 109 {
segundo 0:ac1725ba162c 110 if( !m_pPcb ) //Pcb doesn't exist (anymore)
segundo 0:ac1725ba162c 111 return NETUDPSOCKET_MEM;
segundo 0:ac1725ba162c 112 int inLen = 0;
segundo 0:ac1725ba162c 113 int cpyLen = 0;
segundo 0:ac1725ba162c 114
segundo 0:ac1725ba162c 115 static int rmgLen = 0;
segundo 0:ac1725ba162c 116 //Contains the remaining len in this pbuf
segundo 0:ac1725ba162c 117
segundo 0:ac1725ba162c 118 if( m_lInPkt.empty() )
segundo 0:ac1725ba162c 119 return 0;
segundo 0:ac1725ba162c 120
segundo 0:ac1725ba162c 121 pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf;
segundo 0:ac1725ba162c 122
segundo 0:ac1725ba162c 123 if(pHost)
segundo 0:ac1725ba162c 124 *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port );
segundo 0:ac1725ba162c 125
segundo 0:ac1725ba162c 126 if( !pBuf )
segundo 0:ac1725ba162c 127 {
segundo 0:ac1725ba162c 128 rmgLen = 0;
segundo 0:ac1725ba162c 129 return 0;
segundo 0:ac1725ba162c 130 }
segundo 0:ac1725ba162c 131
segundo 0:ac1725ba162c 132 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
segundo 0:ac1725ba162c 133 {
segundo 0:ac1725ba162c 134 rmgLen = pBuf->len;
segundo 0:ac1725ba162c 135 }
segundo 0:ac1725ba162c 136
segundo 0:ac1725ba162c 137 while ( inLen < len )
segundo 0:ac1725ba162c 138 {
segundo 0:ac1725ba162c 139 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
segundo 0:ac1725ba162c 140 memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen);
segundo 0:ac1725ba162c 141 inLen += cpyLen;
segundo 0:ac1725ba162c 142 buf += cpyLen;
segundo 0:ac1725ba162c 143
segundo 0:ac1725ba162c 144 rmgLen = rmgLen - cpyLen; //Update rmgLen
segundo 0:ac1725ba162c 145
segundo 0:ac1725ba162c 146 if( rmgLen > 0 )
segundo 0:ac1725ba162c 147 {
segundo 0:ac1725ba162c 148 //We did not read this pbuf completely, so let's save it's pos & return
segundo 0:ac1725ba162c 149 break;
segundo 0:ac1725ba162c 150 }
segundo 0:ac1725ba162c 151
segundo 0:ac1725ba162c 152 if(pBuf->next)
segundo 0:ac1725ba162c 153 {
segundo 0:ac1725ba162c 154 pbuf* pNextPBuf = pBuf->next;
segundo 0:ac1725ba162c 155 pBuf->next = NULL; //So that it is not freed as well
segundo 0:ac1725ba162c 156 //We get the reference to pNextPBuf from m_pReadPbuf
segundo 0:ac1725ba162c 157 pbuf_free((pbuf*)pBuf);
segundo 0:ac1725ba162c 158 pBuf = pNextPBuf;
segundo 0:ac1725ba162c 159 rmgLen = pBuf->len;
segundo 0:ac1725ba162c 160 }
segundo 0:ac1725ba162c 161 else
segundo 0:ac1725ba162c 162 {
segundo 0:ac1725ba162c 163 pbuf_free((pbuf*)pBuf);
segundo 0:ac1725ba162c 164 pBuf = NULL;
segundo 0:ac1725ba162c 165 rmgLen = 0;
segundo 0:ac1725ba162c 166 m_lInPkt.pop_front();
segundo 0:ac1725ba162c 167 break; //No more data to read
segundo 0:ac1725ba162c 168 }
segundo 0:ac1725ba162c 169 }
segundo 0:ac1725ba162c 170
segundo 0:ac1725ba162c 171 return inLen;
segundo 0:ac1725ba162c 172 }
segundo 0:ac1725ba162c 173
segundo 0:ac1725ba162c 174 NetUdpSocketErr LwipNetUdpSocket::close()
segundo 0:ac1725ba162c 175 {
segundo 0:ac1725ba162c 176 DBG("LwipNetUdpSocket::close() : Closing...\n");
segundo 0:ac1725ba162c 177
segundo 0:ac1725ba162c 178 if(m_closed)
segundo 0:ac1725ba162c 179 return NETUDPSOCKET_OK; //Already being closed
segundo 0:ac1725ba162c 180 m_closed = true;
segundo 0:ac1725ba162c 181
segundo 0:ac1725ba162c 182 if( !m_pPcb ) //Pcb doesn't exist (anymore)
segundo 0:ac1725ba162c 183 return NETUDPSOCKET_MEM;
segundo 0:ac1725ba162c 184
segundo 0:ac1725ba162c 185 DBG("LwipNetUdpSocket::close() : Cleanup...\n");
segundo 0:ac1725ba162c 186
segundo 0:ac1725ba162c 187 //Cleanup incoming data
segundo 0:ac1725ba162c 188 cleanUp();
segundo 0:ac1725ba162c 189
segundo 0:ac1725ba162c 190
segundo 0:ac1725ba162c 191 DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n");
segundo 0:ac1725ba162c 192 udp_remove( (udp_pcb*) m_pPcb);
segundo 0:ac1725ba162c 193
segundo 0:ac1725ba162c 194 m_pPcb = NULL;
segundo 0:ac1725ba162c 195 return NETUDPSOCKET_OK;
segundo 0:ac1725ba162c 196 }
segundo 0:ac1725ba162c 197
segundo 0:ac1725ba162c 198 NetUdpSocketErr LwipNetUdpSocket::poll()
segundo 0:ac1725ba162c 199 {
segundo 0:ac1725ba162c 200 NetUdpSocket::flushEvents();
segundo 0:ac1725ba162c 201 return NETUDPSOCKET_OK;
segundo 0:ac1725ba162c 202 }
segundo 0:ac1725ba162c 203
segundo 0:ac1725ba162c 204 // Callbacks events
segundo 0:ac1725ba162c 205
segundo 0:ac1725ba162c 206 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
segundo 0:ac1725ba162c 207 {
segundo 0:ac1725ba162c 208 DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len);
segundo 0:ac1725ba162c 209 list<InPacket>::iterator it;
segundo 0:ac1725ba162c 210 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
segundo 0:ac1725ba162c 211 {
segundo 0:ac1725ba162c 212 if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) )
segundo 0:ac1725ba162c 213 {
segundo 0:ac1725ba162c 214 //Let's tail this packet to the previous one
segundo 0:ac1725ba162c 215 pbuf_cat((pbuf*)((*it).pBuf), p);
segundo 0:ac1725ba162c 216 //No need to queue an event in that case since the read buf has not been processed yet
segundo 0:ac1725ba162c 217 return;
segundo 0:ac1725ba162c 218 }
segundo 0:ac1725ba162c 219 }
segundo 0:ac1725ba162c 220
segundo 0:ac1725ba162c 221 //New host, add a packet to the queue
segundo 0:ac1725ba162c 222 InPacket pkt;
segundo 0:ac1725ba162c 223 pkt.pBuf = p;
segundo 0:ac1725ba162c 224 pkt.addr = *addr;
segundo 0:ac1725ba162c 225 pkt.port = port;
segundo 0:ac1725ba162c 226 m_lInPkt.push_back(pkt);
segundo 0:ac1725ba162c 227
segundo 0:ac1725ba162c 228 queueEvent(NETUDPSOCKET_READABLE);
segundo 0:ac1725ba162c 229 }
segundo 0:ac1725ba162c 230
segundo 0:ac1725ba162c 231 void LwipNetUdpSocket::cleanUp() //Flush input buffer
segundo 0:ac1725ba162c 232 {
segundo 0:ac1725ba162c 233 //Ensure that further error won't be followed to this inst (which can be destroyed)
segundo 0:ac1725ba162c 234 if( m_pPcb )
segundo 0:ac1725ba162c 235 {
segundo 0:ac1725ba162c 236 udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
segundo 0:ac1725ba162c 237 }
segundo 0:ac1725ba162c 238
segundo 0:ac1725ba162c 239 //Leaving multicast group(Ok because LwIP has a refscount for multicast group)
segundo 0:ac1725ba162c 240 #if LWIP_IGMP //Multicast support enabled
segundo 0:ac1725ba162c 241 if(m_multicastGroup.isMulticast())
segundo 0:ac1725ba162c 242 {
segundo 0:ac1725ba162c 243 igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
segundo 0:ac1725ba162c 244 m_multicastGroup = IpAddr();
segundo 0:ac1725ba162c 245 }
segundo 0:ac1725ba162c 246 #endif
segundo 0:ac1725ba162c 247
segundo 0:ac1725ba162c 248 list<InPacket>::iterator it;
segundo 0:ac1725ba162c 249 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
segundo 0:ac1725ba162c 250 {
segundo 0:ac1725ba162c 251 //Free buf
segundo 0:ac1725ba162c 252 pbuf_free((pbuf*)((*it).pBuf));
segundo 0:ac1725ba162c 253 }
segundo 0:ac1725ba162c 254 m_lInPkt.clear();
segundo 0:ac1725ba162c 255 }
segundo 0:ac1725ba162c 256
segundo 0:ac1725ba162c 257 // Static callback from LwIp
segundo 0:ac1725ba162c 258
segundo 0:ac1725ba162c 259 void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
segundo 0:ac1725ba162c 260 {
segundo 0:ac1725ba162c 261 LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg;
segundo 0:ac1725ba162c 262 return pMe->recvCb( pcb, p, addr, port );
segundo 0:ac1725ba162c 263 }
segundo 0:ac1725ba162c 264
segundo 0:ac1725ba162c 265 #endif