First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

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