Fork of NetServicesMin with some warnings removed

Dependencies:   lwip-sys lwip

Fork of NetServicesMin by Hendrik Lipka

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
Child:
2:9cc2c6e42ffd
initial version

Who changed what in which revision?

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