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