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