Committer:
mbed714
Date:
Tue Nov 09 19:32:44 2010 +0000
Revision:
3:0c324737064d
Parent:
0:98aadd37a5c5

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed714 0:98aadd37a5c5 1
mbed714 0:98aadd37a5c5 2 /*
mbed714 0:98aadd37a5c5 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
mbed714 0:98aadd37a5c5 4
mbed714 0:98aadd37a5c5 5 Permission is hereby granted, free of charge, to any person obtaining a copy
mbed714 0:98aadd37a5c5 6 of this software and associated documentation files (the "Software"), to deal
mbed714 0:98aadd37a5c5 7 in the Software without restriction, including without limitation the rights
mbed714 0:98aadd37a5c5 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbed714 0:98aadd37a5c5 9 copies of the Software, and to permit persons to whom the Software is
mbed714 0:98aadd37a5c5 10 furnished to do so, subject to the following conditions:
mbed714 0:98aadd37a5c5 11
mbed714 0:98aadd37a5c5 12 The above copyright notice and this permission notice shall be included in
mbed714 0:98aadd37a5c5 13 all copies or substantial portions of the Software.
mbed714 0:98aadd37a5c5 14
mbed714 0:98aadd37a5c5 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbed714 0:98aadd37a5c5 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbed714 0:98aadd37a5c5 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbed714 0:98aadd37a5c5 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbed714 0:98aadd37a5c5 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed714 0:98aadd37a5c5 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mbed714 0:98aadd37a5c5 21 THE SOFTWARE.
mbed714 0:98aadd37a5c5 22 */
mbed714 0:98aadd37a5c5 23
mbed714 0:98aadd37a5c5 24 #include "net.h"
mbed714 0:98aadd37a5c5 25
mbed714 0:98aadd37a5c5 26 #include "host.h"
mbed714 0:98aadd37a5c5 27 #include "ipaddr.h"
mbed714 0:98aadd37a5c5 28 #include "netservice.h"
mbed714 0:98aadd37a5c5 29 #include "if/net/netif.h"
mbed714 0:98aadd37a5c5 30 #include "if/net/nettcpsocket.h"
mbed714 0:98aadd37a5c5 31 #include "if/net/netudpsocket.h"
mbed714 0:98aadd37a5c5 32 #include "if/net/netdnsrequest.h"
mbed714 0:98aadd37a5c5 33
mbed714 0:98aadd37a5c5 34 //#define __DEBUG
mbed714 0:98aadd37a5c5 35 #include "dbg/dbg.h"
mbed714 0:98aadd37a5c5 36
mbed714 0:98aadd37a5c5 37 Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket()
mbed714 0:98aadd37a5c5 38 {
mbed714 0:98aadd37a5c5 39
mbed714 0:98aadd37a5c5 40 }
mbed714 0:98aadd37a5c5 41
mbed714 0:98aadd37a5c5 42 Net::~Net()
mbed714 0:98aadd37a5c5 43 {
mbed714 0:98aadd37a5c5 44
mbed714 0:98aadd37a5c5 45 }
mbed714 0:98aadd37a5c5 46
mbed714 0:98aadd37a5c5 47
mbed714 0:98aadd37a5c5 48 void Net::poll()
mbed714 0:98aadd37a5c5 49 {
mbed714 0:98aadd37a5c5 50 //DBG("\r\nNet : Services polling\r\n");
mbed714 0:98aadd37a5c5 51
mbed714 0:98aadd37a5c5 52 //Poll Services
mbed714 0:98aadd37a5c5 53 NetService::servicesPoll();
mbed714 0:98aadd37a5c5 54
mbed714 0:98aadd37a5c5 55 //DBG("\r\nNet : Interfaces polling\r\n");
mbed714 0:98aadd37a5c5 56
mbed714 0:98aadd37a5c5 57 //Poll Interfaces
mbed714 0:98aadd37a5c5 58 list<NetIf*>::iterator pIfIt;
mbed714 0:98aadd37a5c5 59
mbed714 0:98aadd37a5c5 60 for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ )
mbed714 0:98aadd37a5c5 61 {
mbed714 0:98aadd37a5c5 62 (*pIfIt)->poll();
mbed714 0:98aadd37a5c5 63 }
mbed714 0:98aadd37a5c5 64
mbed714 0:98aadd37a5c5 65 //DBG("\r\nNet : Sockets polling\r\n");
mbed714 0:98aadd37a5c5 66
mbed714 0:98aadd37a5c5 67 //Poll Tcp Sockets
mbed714 0:98aadd37a5c5 68 list<NetTcpSocket*>::iterator pNetTcpSocketIt;
mbed714 0:98aadd37a5c5 69
mbed714 0:98aadd37a5c5 70 for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); )
mbed714 0:98aadd37a5c5 71 {
mbed714 0:98aadd37a5c5 72 (*pNetTcpSocketIt)->poll();
mbed714 0:98aadd37a5c5 73
mbed714 0:98aadd37a5c5 74 if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) )
mbed714 0:98aadd37a5c5 75 {
mbed714 0:98aadd37a5c5 76 (*pNetTcpSocketIt)->m_removed = true;
mbed714 0:98aadd37a5c5 77 delete (*pNetTcpSocketIt);
mbed714 0:98aadd37a5c5 78 (*pNetTcpSocketIt) = NULL;
mbed714 0:98aadd37a5c5 79 pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt);
mbed714 0:98aadd37a5c5 80 }
mbed714 0:98aadd37a5c5 81 else
mbed714 0:98aadd37a5c5 82 {
mbed714 0:98aadd37a5c5 83 pNetTcpSocketIt++;
mbed714 0:98aadd37a5c5 84 }
mbed714 0:98aadd37a5c5 85 }
mbed714 0:98aadd37a5c5 86
mbed714 0:98aadd37a5c5 87 //Poll Udp Sockets
mbed714 0:98aadd37a5c5 88 list<NetUdpSocket*>::iterator pNetUdpSocketIt;
mbed714 0:98aadd37a5c5 89
mbed714 0:98aadd37a5c5 90 for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); )
mbed714 0:98aadd37a5c5 91 {
mbed714 0:98aadd37a5c5 92 (*pNetUdpSocketIt)->poll();
mbed714 0:98aadd37a5c5 93
mbed714 0:98aadd37a5c5 94 if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) )
mbed714 0:98aadd37a5c5 95 {
mbed714 0:98aadd37a5c5 96 (*pNetUdpSocketIt)->m_removed = true;
mbed714 0:98aadd37a5c5 97 delete (*pNetUdpSocketIt);
mbed714 0:98aadd37a5c5 98 (*pNetUdpSocketIt) = NULL;
mbed714 0:98aadd37a5c5 99 pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt);
mbed714 0:98aadd37a5c5 100 }
mbed714 0:98aadd37a5c5 101 else
mbed714 0:98aadd37a5c5 102 {
mbed714 0:98aadd37a5c5 103 pNetUdpSocketIt++;
mbed714 0:98aadd37a5c5 104 }
mbed714 0:98aadd37a5c5 105 }
mbed714 0:98aadd37a5c5 106
mbed714 0:98aadd37a5c5 107
mbed714 0:98aadd37a5c5 108 }
mbed714 0:98aadd37a5c5 109
mbed714 0:98aadd37a5c5 110 NetTcpSocket* Net::tcpSocket(NetIf& netif) {
mbed714 0:98aadd37a5c5 111 NetTcpSocket* pNetTcpSocket = netif.tcpSocket();
mbed714 0:98aadd37a5c5 112 pNetTcpSocket->m_refs++;
mbed714 0:98aadd37a5c5 113 return pNetTcpSocket;
mbed714 0:98aadd37a5c5 114 }
mbed714 0:98aadd37a5c5 115
mbed714 0:98aadd37a5c5 116 NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if
mbed714 0:98aadd37a5c5 117 if ( net().m_defaultIf == NULL )
mbed714 0:98aadd37a5c5 118 return NULL;
mbed714 0:98aadd37a5c5 119 NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket();
mbed714 0:98aadd37a5c5 120 pNetTcpSocket->m_refs++;
mbed714 0:98aadd37a5c5 121 return pNetTcpSocket;
mbed714 0:98aadd37a5c5 122 }
mbed714 0:98aadd37a5c5 123
mbed714 0:98aadd37a5c5 124 void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket)
mbed714 0:98aadd37a5c5 125 {
mbed714 0:98aadd37a5c5 126 pNetTcpSocket->m_refs--;
mbed714 0:98aadd37a5c5 127 if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs)
mbed714 0:98aadd37a5c5 128 pNetTcpSocket->close();
mbed714 0:98aadd37a5c5 129 }
mbed714 0:98aadd37a5c5 130
mbed714 0:98aadd37a5c5 131 NetUdpSocket* Net::udpSocket(NetIf& netif) {
mbed714 0:98aadd37a5c5 132 NetUdpSocket* pNetUdpSocket = netif.udpSocket();
mbed714 0:98aadd37a5c5 133 pNetUdpSocket->m_refs++;
mbed714 0:98aadd37a5c5 134 return pNetUdpSocket;
mbed714 0:98aadd37a5c5 135 }
mbed714 0:98aadd37a5c5 136
mbed714 0:98aadd37a5c5 137 NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if
mbed714 0:98aadd37a5c5 138 if ( net().m_defaultIf == NULL )
mbed714 0:98aadd37a5c5 139 return NULL;
mbed714 0:98aadd37a5c5 140 NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket();
mbed714 0:98aadd37a5c5 141 pNetUdpSocket->m_refs++;
mbed714 0:98aadd37a5c5 142 return pNetUdpSocket;
mbed714 0:98aadd37a5c5 143 }
mbed714 0:98aadd37a5c5 144
mbed714 0:98aadd37a5c5 145 void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket)
mbed714 0:98aadd37a5c5 146 {
mbed714 0:98aadd37a5c5 147 pNetUdpSocket->m_refs--;
mbed714 0:98aadd37a5c5 148 if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs)
mbed714 0:98aadd37a5c5 149 pNetUdpSocket->close();
mbed714 0:98aadd37a5c5 150 }
mbed714 0:98aadd37a5c5 151
mbed714 0:98aadd37a5c5 152 NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) {
mbed714 0:98aadd37a5c5 153 return netif.dnsRequest(hostname);
mbed714 0:98aadd37a5c5 154 }
mbed714 0:98aadd37a5c5 155
mbed714 0:98aadd37a5c5 156 NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if
mbed714 0:98aadd37a5c5 157 if ( net().m_defaultIf == NULL )
mbed714 0:98aadd37a5c5 158 return NULL;
mbed714 0:98aadd37a5c5 159 return net().m_defaultIf->dnsRequest(hostname);
mbed714 0:98aadd37a5c5 160 }
mbed714 0:98aadd37a5c5 161
mbed714 0:98aadd37a5c5 162 NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif)
mbed714 0:98aadd37a5c5 163 {
mbed714 0:98aadd37a5c5 164 return netif.dnsRequest(pHost);
mbed714 0:98aadd37a5c5 165 }
mbed714 0:98aadd37a5c5 166
mbed714 0:98aadd37a5c5 167 NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if
mbed714 0:98aadd37a5c5 168 {
mbed714 0:98aadd37a5c5 169 if ( net().m_defaultIf == NULL )
mbed714 0:98aadd37a5c5 170 return NULL;
mbed714 0:98aadd37a5c5 171 return net().m_defaultIf->dnsRequest(pHost);
mbed714 0:98aadd37a5c5 172 }
mbed714 0:98aadd37a5c5 173
mbed714 0:98aadd37a5c5 174 void Net::setDefaultIf(NetIf& netif) {
mbed714 0:98aadd37a5c5 175 net().m_defaultIf = &netif;
mbed714 0:98aadd37a5c5 176 }
mbed714 0:98aadd37a5c5 177
mbed714 0:98aadd37a5c5 178 void Net::setDefaultIf(NetIf* pIf)
mbed714 0:98aadd37a5c5 179 {
mbed714 0:98aadd37a5c5 180 net().m_defaultIf = pIf;
mbed714 0:98aadd37a5c5 181 }
mbed714 0:98aadd37a5c5 182
mbed714 0:98aadd37a5c5 183 NetIf* Net::getDefaultIf() {
mbed714 0:98aadd37a5c5 184 return net().m_defaultIf;
mbed714 0:98aadd37a5c5 185 }
mbed714 0:98aadd37a5c5 186
mbed714 0:98aadd37a5c5 187 void Net::registerIf(NetIf* pIf)
mbed714 0:98aadd37a5c5 188 {
mbed714 0:98aadd37a5c5 189 net().m_lpIf.push_back(pIf);
mbed714 0:98aadd37a5c5 190 }
mbed714 0:98aadd37a5c5 191
mbed714 0:98aadd37a5c5 192 void Net::unregisterIf(NetIf* pIf)
mbed714 0:98aadd37a5c5 193 {
mbed714 0:98aadd37a5c5 194 if( net().m_defaultIf == pIf )
mbed714 0:98aadd37a5c5 195 net().m_defaultIf = NULL;
mbed714 0:98aadd37a5c5 196 net().m_lpIf.remove(pIf);
mbed714 0:98aadd37a5c5 197 }
mbed714 0:98aadd37a5c5 198
mbed714 0:98aadd37a5c5 199 void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket)
mbed714 0:98aadd37a5c5 200 {
mbed714 0:98aadd37a5c5 201 net().m_lpNetTcpSocket.push_back(pNetTcpSocket);
mbed714 0:98aadd37a5c5 202 DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size());
mbed714 0:98aadd37a5c5 203 }
mbed714 0:98aadd37a5c5 204
mbed714 0:98aadd37a5c5 205 void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket)
mbed714 0:98aadd37a5c5 206 {
mbed714 0:98aadd37a5c5 207 DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1);
mbed714 0:98aadd37a5c5 208 if(!pNetTcpSocket->m_removed)
mbed714 0:98aadd37a5c5 209 net().m_lpNetTcpSocket.remove(pNetTcpSocket);
mbed714 0:98aadd37a5c5 210 }
mbed714 0:98aadd37a5c5 211
mbed714 0:98aadd37a5c5 212 void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket)
mbed714 0:98aadd37a5c5 213 {
mbed714 0:98aadd37a5c5 214 net().m_lpNetUdpSocket.push_back(pNetUdpSocket);
mbed714 0:98aadd37a5c5 215 DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size());
mbed714 0:98aadd37a5c5 216 }
mbed714 0:98aadd37a5c5 217
mbed714 0:98aadd37a5c5 218 void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket)
mbed714 0:98aadd37a5c5 219 {
mbed714 0:98aadd37a5c5 220 DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1);
mbed714 0:98aadd37a5c5 221 if(!pNetUdpSocket->m_removed)
mbed714 0:98aadd37a5c5 222 net().m_lpNetUdpSocket.remove(pNetUdpSocket);
mbed714 0:98aadd37a5c5 223 }
mbed714 0:98aadd37a5c5 224
mbed714 0:98aadd37a5c5 225 Net& Net::net()
mbed714 0:98aadd37a5c5 226 {
mbed714 0:98aadd37a5c5 227 static Net* pInst = new Net(); //Called only once
mbed714 0:98aadd37a5c5 228 return *pInst;
mbed714 0:98aadd37a5c5 229 }