NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hexley 0:281d6ff68967 1
hexley 0:281d6ff68967 2 /*
hexley 0:281d6ff68967 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
hexley 0:281d6ff68967 4
hexley 0:281d6ff68967 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hexley 0:281d6ff68967 6 of this software and associated documentation files (the "Software"), to deal
hexley 0:281d6ff68967 7 in the Software without restriction, including without limitation the rights
hexley 0:281d6ff68967 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hexley 0:281d6ff68967 9 copies of the Software, and to permit persons to whom the Software is
hexley 0:281d6ff68967 10 furnished to do so, subject to the following conditions:
hexley 0:281d6ff68967 11
hexley 0:281d6ff68967 12 The above copyright notice and this permission notice shall be included in
hexley 0:281d6ff68967 13 all copies or substantial portions of the Software.
hexley 0:281d6ff68967 14
hexley 0:281d6ff68967 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hexley 0:281d6ff68967 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hexley 0:281d6ff68967 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hexley 0:281d6ff68967 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hexley 0:281d6ff68967 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hexley 0:281d6ff68967 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hexley 0:281d6ff68967 21 THE SOFTWARE.
hexley 0:281d6ff68967 22 */
hexley 0:281d6ff68967 23
hexley 0:281d6ff68967 24 #include "lwipNetDnsRequest.h"
hexley 0:281d6ff68967 25 #include "lwip/err.h" //err_t, ERR_xxx
hexley 0:281d6ff68967 26 #include "lwip/dns.h"
hexley 0:281d6ff68967 27
hexley 0:281d6ff68967 28 #include "netCfg.h"
hexley 0:281d6ff68967 29 #if NET_LWIP_STACK
hexley 0:281d6ff68967 30
hexley 0:281d6ff68967 31 //#define __DEBUG
hexley 0:281d6ff68967 32 #include "dbg/dbg.h"
hexley 0:281d6ff68967 33
hexley 0:281d6ff68967 34 LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
hexley 0:281d6ff68967 35 {
hexley 0:281d6ff68967 36 DBG("New LwipNetDnsRequest %p\n", this);
hexley 0:281d6ff68967 37 }
hexley 0:281d6ff68967 38
hexley 0:281d6ff68967 39 LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
hexley 0:281d6ff68967 40 {
hexley 0:281d6ff68967 41 DBG("New LwipNetDnsRequest %p\n", this);
hexley 0:281d6ff68967 42 }
hexley 0:281d6ff68967 43
hexley 0:281d6ff68967 44 LwipNetDnsRequest::~LwipNetDnsRequest()
hexley 0:281d6ff68967 45 {
hexley 0:281d6ff68967 46 DBG("LwipNetDnsRequest %p destroyed\n", this);
hexley 0:281d6ff68967 47 }
hexley 0:281d6ff68967 48
hexley 0:281d6ff68967 49 /*
hexley 0:281d6ff68967 50 Main useful function here (see lwip/dns.h):
hexley 0:281d6ff68967 51 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr,
hexley 0:281d6ff68967 52 dns_found_callback found, void *callback_arg);
hexley 0:281d6ff68967 53 */
hexley 0:281d6ff68967 54
hexley 0:281d6ff68967 55 //Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet
hexley 0:281d6ff68967 56 void LwipNetDnsRequest::poll()
hexley 0:281d6ff68967 57 {
hexley 0:281d6ff68967 58 err_t err;
hexley 0:281d6ff68967 59 switch(m_state)
hexley 0:281d6ff68967 60 {
hexley 0:281d6ff68967 61 case LWIPNETDNS_START: //First req, let's call dns_gethostbyname
hexley 0:281d6ff68967 62 ip_addr_t ipStruct;
hexley 0:281d6ff68967 63 err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this );
hexley 0:281d6ff68967 64 if( err == ERR_OK )
hexley 0:281d6ff68967 65 {
hexley 0:281d6ff68967 66 m_ip = IpAddr(&ipStruct);
hexley 0:281d6ff68967 67 m_state = LWIPNETDNS_OK;
hexley 0:281d6ff68967 68 DBG("DNS: Ip found in cache.\n");
hexley 0:281d6ff68967 69 }
hexley 0:281d6ff68967 70 else if( err == ERR_INPROGRESS)
hexley 0:281d6ff68967 71 {
hexley 0:281d6ff68967 72 DBG("DNS: Processing.\n");
hexley 0:281d6ff68967 73 m_state = LWIPNETDNS_PROCESSING;
hexley 0:281d6ff68967 74 }
hexley 0:281d6ff68967 75 else //Likely ERR_VAL
hexley 0:281d6ff68967 76 {
hexley 0:281d6ff68967 77 DBG("DNS: Error on init.\n");
hexley 0:281d6ff68967 78 m_state = LWIPNETDNS_ERROR;
hexley 0:281d6ff68967 79 }
hexley 0:281d6ff68967 80 break;
hexley 0:281d6ff68967 81 case LWIPNETDNS_PROCESSING:
hexley 0:281d6ff68967 82 break; //Nothing to do, DNS is polled on interrupt
hexley 0:281d6ff68967 83 case LWIPNETDNS_OK:
hexley 0:281d6ff68967 84 if(!m_cbFired)
hexley 0:281d6ff68967 85 {
hexley 0:281d6ff68967 86 DBG("DNS: Ip found.\n");
hexley 0:281d6ff68967 87 m_cbFired = true;
hexley 0:281d6ff68967 88 onReply(NETDNS_FOUND); //Raise callback
hexley 0:281d6ff68967 89 }
hexley 0:281d6ff68967 90 break;
hexley 0:281d6ff68967 91 case LWIPNETDNS_NOTFOUND:
hexley 0:281d6ff68967 92 if(!m_cbFired)
hexley 0:281d6ff68967 93 {
hexley 0:281d6ff68967 94 DBG("DNS: could not be resolved.\n");
hexley 0:281d6ff68967 95 m_cbFired = true;
hexley 0:281d6ff68967 96 onReply(NETDNS_NOTFOUND); //Raise callback
hexley 0:281d6ff68967 97 }
hexley 0:281d6ff68967 98 break;
hexley 0:281d6ff68967 99 case LWIPNETDNS_ERROR:
hexley 0:281d6ff68967 100 default:
hexley 0:281d6ff68967 101 if(!m_cbFired)
hexley 0:281d6ff68967 102 {
hexley 0:281d6ff68967 103 DBG("DNS: Error.\n");
hexley 0:281d6ff68967 104 m_cbFired = true;
hexley 0:281d6ff68967 105 onReply(NETDNS_ERROR); //Raise callback
hexley 0:281d6ff68967 106 }
hexley 0:281d6ff68967 107 break;
hexley 0:281d6ff68967 108 }
hexley 0:281d6ff68967 109 if(m_closing && (m_state!=LWIPNETDNS_PROCESSING)) //Check wether the closure has been reqd
hexley 0:281d6ff68967 110 {
hexley 0:281d6ff68967 111 DBG("LwipNetDnsRequest: Closing in poll()\n");
hexley 0:281d6ff68967 112 NetDnsRequest::close();
hexley 0:281d6ff68967 113 }
hexley 0:281d6ff68967 114 }
hexley 0:281d6ff68967 115
hexley 0:281d6ff68967 116 void LwipNetDnsRequest::close()
hexley 0:281d6ff68967 117 {
hexley 0:281d6ff68967 118 DBG("LwipNetDnsRequest: Close req\n");
hexley 0:281d6ff68967 119 if(m_state!=LWIPNETDNS_PROCESSING)
hexley 0:281d6ff68967 120 {
hexley 0:281d6ff68967 121 DBG("LwipNetDnsRequest: Closing in close()\n");
hexley 0:281d6ff68967 122 NetDnsRequest::close();
hexley 0:281d6ff68967 123 }
hexley 0:281d6ff68967 124 else //Cannot close rightaway, waiting for callback from underlying layer
hexley 0:281d6ff68967 125 {
hexley 0:281d6ff68967 126 m_closing = true;
hexley 0:281d6ff68967 127 }
hexley 0:281d6ff68967 128 }
hexley 0:281d6ff68967 129
hexley 0:281d6ff68967 130 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr)
hexley 0:281d6ff68967 131 {
hexley 0:281d6ff68967 132 if( ipaddr == NULL )
hexley 0:281d6ff68967 133 {
hexley 0:281d6ff68967 134 DBG("LwipNetDnsRequest: Callback: Name not found\n");
hexley 0:281d6ff68967 135 m_state = LWIPNETDNS_NOTFOUND;
hexley 0:281d6ff68967 136 return;
hexley 0:281d6ff68967 137 }
hexley 0:281d6ff68967 138 DBG("LwipNetDnsRequest: Callback: Resolved\n");
hexley 0:281d6ff68967 139 m_ip = IpAddr(ipaddr);
hexley 0:281d6ff68967 140 m_state = LWIPNETDNS_OK;
hexley 0:281d6ff68967 141 }
hexley 0:281d6ff68967 142
hexley 0:281d6ff68967 143
hexley 0:281d6ff68967 144 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg)
hexley 0:281d6ff68967 145 {
hexley 0:281d6ff68967 146 DBG("LwipNetDnsRequest: Static callback\n");
hexley 0:281d6ff68967 147 LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg;
hexley 0:281d6ff68967 148 return pMe->foundCb( name, ipaddr );
hexley 0:281d6ff68967 149 }
hexley 0:281d6ff68967 150
hexley 0:281d6ff68967 151 #endif
hexley 0:281d6ff68967 152