HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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