Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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