Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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