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
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 "net.h"
mr_q 0:d8f2f7d5f31b 25
mr_q 0:d8f2f7d5f31b 26 #include "host.h"
mr_q 0:d8f2f7d5f31b 27 #include "ipaddr.h"
mr_q 0:d8f2f7d5f31b 28 #include "netservice.h"
mr_q 0:d8f2f7d5f31b 29 #include "if/net/netif.h"
mr_q 0:d8f2f7d5f31b 30 #include "if/net/nettcpsocket.h"
mr_q 0:d8f2f7d5f31b 31 #include "if/net/netudpsocket.h"
mr_q 0:d8f2f7d5f31b 32 #include "if/net/netdnsrequest.h"
mr_q 0:d8f2f7d5f31b 33
mr_q 0:d8f2f7d5f31b 34 //#define __DEBUG
mr_q 0:d8f2f7d5f31b 35 #include "dbg/dbg.h"
mr_q 0:d8f2f7d5f31b 36
mr_q 0:d8f2f7d5f31b 37 Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket()
mr_q 0:d8f2f7d5f31b 38 {
mr_q 0:d8f2f7d5f31b 39
mr_q 0:d8f2f7d5f31b 40 }
mr_q 0:d8f2f7d5f31b 41
mr_q 0:d8f2f7d5f31b 42 Net::~Net()
mr_q 0:d8f2f7d5f31b 43 {
mr_q 0:d8f2f7d5f31b 44
mr_q 0:d8f2f7d5f31b 45 }
mr_q 0:d8f2f7d5f31b 46
mr_q 0:d8f2f7d5f31b 47
mr_q 0:d8f2f7d5f31b 48 void Net::poll()
mr_q 0:d8f2f7d5f31b 49 {
mr_q 0:d8f2f7d5f31b 50 //DBG("\r\nNet : Services polling\r\n");
mr_q 0:d8f2f7d5f31b 51
mr_q 0:d8f2f7d5f31b 52 //Poll Services
mr_q 0:d8f2f7d5f31b 53 NetService::servicesPoll();
mr_q 0:d8f2f7d5f31b 54
mr_q 0:d8f2f7d5f31b 55 //DBG("\r\nNet : Interfaces polling\r\n");
mr_q 0:d8f2f7d5f31b 56
mr_q 0:d8f2f7d5f31b 57 //Poll Interfaces
mr_q 0:d8f2f7d5f31b 58 list<NetIf*>::iterator pIfIt;
mr_q 0:d8f2f7d5f31b 59
mr_q 0:d8f2f7d5f31b 60 for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ )
mr_q 0:d8f2f7d5f31b 61 {
mr_q 0:d8f2f7d5f31b 62 (*pIfIt)->poll();
mr_q 0:d8f2f7d5f31b 63 }
mr_q 0:d8f2f7d5f31b 64
mr_q 0:d8f2f7d5f31b 65 //DBG("\r\nNet : Sockets polling\r\n");
mr_q 0:d8f2f7d5f31b 66
mr_q 0:d8f2f7d5f31b 67 //Poll Tcp Sockets
mr_q 0:d8f2f7d5f31b 68 list<NetTcpSocket*>::iterator pNetTcpSocketIt;
mr_q 0:d8f2f7d5f31b 69
mr_q 0:d8f2f7d5f31b 70 for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); )
mr_q 0:d8f2f7d5f31b 71 {
mr_q 0:d8f2f7d5f31b 72 (*pNetTcpSocketIt)->poll();
mr_q 0:d8f2f7d5f31b 73
mr_q 0:d8f2f7d5f31b 74 if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) )
mr_q 0:d8f2f7d5f31b 75 {
mr_q 0:d8f2f7d5f31b 76 (*pNetTcpSocketIt)->m_removed = true;
mr_q 0:d8f2f7d5f31b 77 delete (*pNetTcpSocketIt);
mr_q 0:d8f2f7d5f31b 78 (*pNetTcpSocketIt) = NULL;
mr_q 0:d8f2f7d5f31b 79 pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt);
mr_q 0:d8f2f7d5f31b 80 }
mr_q 0:d8f2f7d5f31b 81 else
mr_q 0:d8f2f7d5f31b 82 {
mr_q 0:d8f2f7d5f31b 83 pNetTcpSocketIt++;
mr_q 0:d8f2f7d5f31b 84 }
mr_q 0:d8f2f7d5f31b 85 }
mr_q 0:d8f2f7d5f31b 86
mr_q 0:d8f2f7d5f31b 87 //Poll Udp Sockets
mr_q 0:d8f2f7d5f31b 88 list<NetUdpSocket*>::iterator pNetUdpSocketIt;
mr_q 0:d8f2f7d5f31b 89
mr_q 0:d8f2f7d5f31b 90 for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); )
mr_q 0:d8f2f7d5f31b 91 {
mr_q 0:d8f2f7d5f31b 92 (*pNetUdpSocketIt)->poll();
mr_q 0:d8f2f7d5f31b 93
mr_q 0:d8f2f7d5f31b 94 if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) )
mr_q 0:d8f2f7d5f31b 95 {
mr_q 0:d8f2f7d5f31b 96 (*pNetUdpSocketIt)->m_removed = true;
mr_q 0:d8f2f7d5f31b 97 delete (*pNetUdpSocketIt);
mr_q 0:d8f2f7d5f31b 98 (*pNetUdpSocketIt) = NULL;
mr_q 0:d8f2f7d5f31b 99 pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt);
mr_q 0:d8f2f7d5f31b 100 }
mr_q 0:d8f2f7d5f31b 101 else
mr_q 0:d8f2f7d5f31b 102 {
mr_q 0:d8f2f7d5f31b 103 pNetUdpSocketIt++;
mr_q 0:d8f2f7d5f31b 104 }
mr_q 0:d8f2f7d5f31b 105 }
mr_q 0:d8f2f7d5f31b 106
mr_q 0:d8f2f7d5f31b 107
mr_q 0:d8f2f7d5f31b 108 }
mr_q 0:d8f2f7d5f31b 109
mr_q 0:d8f2f7d5f31b 110 NetTcpSocket* Net::tcpSocket(NetIf& netif) {
mr_q 0:d8f2f7d5f31b 111 NetTcpSocket* pNetTcpSocket = netif.tcpSocket();
mr_q 0:d8f2f7d5f31b 112 pNetTcpSocket->m_refs++;
mr_q 0:d8f2f7d5f31b 113 return pNetTcpSocket;
mr_q 0:d8f2f7d5f31b 114 }
mr_q 0:d8f2f7d5f31b 115
mr_q 0:d8f2f7d5f31b 116 NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if
mr_q 0:d8f2f7d5f31b 117 if ( net().m_defaultIf == NULL )
mr_q 0:d8f2f7d5f31b 118 return NULL;
mr_q 0:d8f2f7d5f31b 119 NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket();
mr_q 0:d8f2f7d5f31b 120 pNetTcpSocket->m_refs++;
mr_q 0:d8f2f7d5f31b 121 return pNetTcpSocket;
mr_q 0:d8f2f7d5f31b 122 }
mr_q 0:d8f2f7d5f31b 123
mr_q 0:d8f2f7d5f31b 124 void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket)
mr_q 0:d8f2f7d5f31b 125 {
mr_q 0:d8f2f7d5f31b 126 pNetTcpSocket->m_refs--;
mr_q 0:d8f2f7d5f31b 127 if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs)
mr_q 0:d8f2f7d5f31b 128 pNetTcpSocket->close();
mr_q 0:d8f2f7d5f31b 129 }
mr_q 0:d8f2f7d5f31b 130
mr_q 0:d8f2f7d5f31b 131 NetUdpSocket* Net::udpSocket(NetIf& netif) {
mr_q 0:d8f2f7d5f31b 132 NetUdpSocket* pNetUdpSocket = netif.udpSocket();
mr_q 0:d8f2f7d5f31b 133 pNetUdpSocket->m_refs++;
mr_q 0:d8f2f7d5f31b 134 return pNetUdpSocket;
mr_q 0:d8f2f7d5f31b 135 }
mr_q 0:d8f2f7d5f31b 136
mr_q 0:d8f2f7d5f31b 137 NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if
mr_q 0:d8f2f7d5f31b 138 if ( net().m_defaultIf == NULL )
mr_q 0:d8f2f7d5f31b 139 return NULL;
mr_q 0:d8f2f7d5f31b 140 NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket();
mr_q 0:d8f2f7d5f31b 141 pNetUdpSocket->m_refs++;
mr_q 0:d8f2f7d5f31b 142 return pNetUdpSocket;
mr_q 0:d8f2f7d5f31b 143 }
mr_q 0:d8f2f7d5f31b 144
mr_q 0:d8f2f7d5f31b 145 void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket)
mr_q 0:d8f2f7d5f31b 146 {
mr_q 0:d8f2f7d5f31b 147 pNetUdpSocket->m_refs--;
mr_q 0:d8f2f7d5f31b 148 if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs)
mr_q 0:d8f2f7d5f31b 149 pNetUdpSocket->close();
mr_q 0:d8f2f7d5f31b 150 }
mr_q 0:d8f2f7d5f31b 151
mr_q 0:d8f2f7d5f31b 152 NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) {
mr_q 0:d8f2f7d5f31b 153 return netif.dnsRequest(hostname);
mr_q 0:d8f2f7d5f31b 154 }
mr_q 0:d8f2f7d5f31b 155
mr_q 0:d8f2f7d5f31b 156 NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if
mr_q 0:d8f2f7d5f31b 157 if ( net().m_defaultIf == NULL )
mr_q 0:d8f2f7d5f31b 158 return NULL;
mr_q 0:d8f2f7d5f31b 159 return net().m_defaultIf->dnsRequest(hostname);
mr_q 0:d8f2f7d5f31b 160 }
mr_q 0:d8f2f7d5f31b 161
mr_q 0:d8f2f7d5f31b 162 NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif)
mr_q 0:d8f2f7d5f31b 163 {
mr_q 0:d8f2f7d5f31b 164 return netif.dnsRequest(pHost);
mr_q 0:d8f2f7d5f31b 165 }
mr_q 0:d8f2f7d5f31b 166
mr_q 0:d8f2f7d5f31b 167 NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if
mr_q 0:d8f2f7d5f31b 168 {
mr_q 0:d8f2f7d5f31b 169 if ( net().m_defaultIf == NULL )
mr_q 0:d8f2f7d5f31b 170 return NULL;
mr_q 0:d8f2f7d5f31b 171 return net().m_defaultIf->dnsRequest(pHost);
mr_q 0:d8f2f7d5f31b 172 }
mr_q 0:d8f2f7d5f31b 173
mr_q 0:d8f2f7d5f31b 174 void Net::setDefaultIf(NetIf& netif) {
mr_q 0:d8f2f7d5f31b 175 net().m_defaultIf = &netif;
mr_q 0:d8f2f7d5f31b 176 }
mr_q 0:d8f2f7d5f31b 177
mr_q 0:d8f2f7d5f31b 178 void Net::setDefaultIf(NetIf* pIf)
mr_q 0:d8f2f7d5f31b 179 {
mr_q 0:d8f2f7d5f31b 180 net().m_defaultIf = pIf;
mr_q 0:d8f2f7d5f31b 181 }
mr_q 0:d8f2f7d5f31b 182
mr_q 0:d8f2f7d5f31b 183 NetIf* Net::getDefaultIf() {
mr_q 0:d8f2f7d5f31b 184 return net().m_defaultIf;
mr_q 0:d8f2f7d5f31b 185 }
mr_q 0:d8f2f7d5f31b 186
mr_q 0:d8f2f7d5f31b 187 void Net::registerIf(NetIf* pIf)
mr_q 0:d8f2f7d5f31b 188 {
mr_q 0:d8f2f7d5f31b 189 net().m_lpIf.push_back(pIf);
mr_q 0:d8f2f7d5f31b 190 }
mr_q 0:d8f2f7d5f31b 191
mr_q 0:d8f2f7d5f31b 192 void Net::unregisterIf(NetIf* pIf)
mr_q 0:d8f2f7d5f31b 193 {
mr_q 0:d8f2f7d5f31b 194 if( net().m_defaultIf == pIf )
mr_q 0:d8f2f7d5f31b 195 net().m_defaultIf = NULL;
mr_q 0:d8f2f7d5f31b 196 net().m_lpIf.remove(pIf);
mr_q 0:d8f2f7d5f31b 197 }
mr_q 0:d8f2f7d5f31b 198
mr_q 0:d8f2f7d5f31b 199 void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket)
mr_q 0:d8f2f7d5f31b 200 {
mr_q 0:d8f2f7d5f31b 201 net().m_lpNetTcpSocket.push_back(pNetTcpSocket);
mr_q 0:d8f2f7d5f31b 202 DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size());
mr_q 0:d8f2f7d5f31b 203 }
mr_q 0:d8f2f7d5f31b 204
mr_q 0:d8f2f7d5f31b 205 void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket)
mr_q 0:d8f2f7d5f31b 206 {
mr_q 0:d8f2f7d5f31b 207 DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1);
mr_q 0:d8f2f7d5f31b 208 if(!pNetTcpSocket->m_removed)
mr_q 0:d8f2f7d5f31b 209 net().m_lpNetTcpSocket.remove(pNetTcpSocket);
mr_q 0:d8f2f7d5f31b 210 }
mr_q 0:d8f2f7d5f31b 211
mr_q 0:d8f2f7d5f31b 212 void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket)
mr_q 0:d8f2f7d5f31b 213 {
mr_q 0:d8f2f7d5f31b 214 net().m_lpNetUdpSocket.push_back(pNetUdpSocket);
mr_q 0:d8f2f7d5f31b 215 DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size());
mr_q 0:d8f2f7d5f31b 216 }
mr_q 0:d8f2f7d5f31b 217
mr_q 0:d8f2f7d5f31b 218 void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket)
mr_q 0:d8f2f7d5f31b 219 {
mr_q 0:d8f2f7d5f31b 220 DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1);
mr_q 0:d8f2f7d5f31b 221 if(!pNetUdpSocket->m_removed)
mr_q 0:d8f2f7d5f31b 222 net().m_lpNetUdpSocket.remove(pNetUdpSocket);
mr_q 0:d8f2f7d5f31b 223 }
mr_q 0:d8f2f7d5f31b 224
mr_q 0:d8f2f7d5f31b 225 Net& Net::net()
mr_q 0:d8f2f7d5f31b 226 {
mr_q 0:d8f2f7d5f31b 227 static Net* pInst = new Net(); //Called only once
mr_q 0:d8f2f7d5f31b 228 return *pInst;
mr_q 0:d8f2f7d5f31b 229 }