Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Wed Dec 15 18:01:30 2010 +0000
Revision:
7:4e2468d7d5cb
Parent:
0:ac1725ba162c

        

Who changed what in which revision?

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