NetServices Stack source
Dependents: HelloWorld ServoInterfaceBoardExample1 4180_Lab4
lwipNetDnsRequest.cpp
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 #include "lwipNetDnsRequest.h" 00025 #include "lwip/err.h" //err_t, ERR_xxx 00026 #include "lwip/dns.h" 00027 00028 #include "netCfg.h" 00029 #if NET_LWIP_STACK 00030 00031 //#define __DEBUG 00032 #include "dbg/dbg.h" 00033 00034 LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false) 00035 { 00036 DBG("New LwipNetDnsRequest %p\n", this); 00037 } 00038 00039 LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false) 00040 { 00041 DBG("New LwipNetDnsRequest %p\n", this); 00042 } 00043 00044 LwipNetDnsRequest::~LwipNetDnsRequest() 00045 { 00046 DBG("LwipNetDnsRequest %p destroyed\n", this); 00047 } 00048 00049 /* 00050 Main useful function here (see lwip/dns.h): 00051 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr, 00052 dns_found_callback found, void *callback_arg); 00053 */ 00054 00055 //Execute request & return OK if found, NOTFOUND or ERROR on error, or PROCESSING if the request has not completed yet 00056 void LwipNetDnsRequest::poll() 00057 { 00058 err_t err; 00059 switch(m_state) 00060 { 00061 case LWIPNETDNS_START: //First req, let's call dns_gethostbyname 00062 ip_addr_t ipStruct; 00063 err = dns_gethostbyname(m_hostname, &ipStruct, LwipNetDnsRequest::sFoundCb, (void*) this ); 00064 if( err == ERR_OK ) 00065 { 00066 m_ip = IpAddr(&ipStruct); 00067 m_state = LWIPNETDNS_OK; 00068 DBG("DNS: Ip found in cache.\n"); 00069 } 00070 else if( err == ERR_INPROGRESS) 00071 { 00072 DBG("DNS: Processing.\n"); 00073 m_state = LWIPNETDNS_PROCESSING; 00074 } 00075 else //Likely ERR_VAL 00076 { 00077 DBG("DNS: Error on init.\n"); 00078 m_state = LWIPNETDNS_ERROR; 00079 } 00080 break; 00081 case LWIPNETDNS_PROCESSING: 00082 break; //Nothing to do, DNS is polled on interrupt 00083 case LWIPNETDNS_OK: 00084 if(!m_cbFired) 00085 { 00086 DBG("DNS: Ip found.\n"); 00087 m_cbFired = true; 00088 onReply(NETDNS_FOUND); //Raise callback 00089 } 00090 break; 00091 case LWIPNETDNS_NOTFOUND: 00092 if(!m_cbFired) 00093 { 00094 DBG("DNS: could not be resolved.\n"); 00095 m_cbFired = true; 00096 onReply(NETDNS_NOTFOUND); //Raise callback 00097 } 00098 break; 00099 case LWIPNETDNS_ERROR: 00100 default: 00101 if(!m_cbFired) 00102 { 00103 DBG("DNS: Error.\n"); 00104 m_cbFired = true; 00105 onReply(NETDNS_ERROR); //Raise callback 00106 } 00107 break; 00108 } 00109 if(m_closing && (m_state!=LWIPNETDNS_PROCESSING)) //Check wether the closure has been reqd 00110 { 00111 DBG("LwipNetDnsRequest: Closing in poll()\n"); 00112 NetDnsRequest::close(); 00113 } 00114 } 00115 00116 void LwipNetDnsRequest::close() 00117 { 00118 DBG("LwipNetDnsRequest: Close req\n"); 00119 if(m_state!=LWIPNETDNS_PROCESSING) 00120 { 00121 DBG("LwipNetDnsRequest: Closing in close()\n"); 00122 NetDnsRequest::close(); 00123 } 00124 else //Cannot close rightaway, waiting for callback from underlying layer 00125 { 00126 m_closing = true; 00127 } 00128 } 00129 00130 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr) 00131 { 00132 if( ipaddr == NULL ) 00133 { 00134 DBG("LwipNetDnsRequest: Callback: Name not found\n"); 00135 m_state = LWIPNETDNS_NOTFOUND; 00136 return; 00137 } 00138 DBG("LwipNetDnsRequest: Callback: Resolved\n"); 00139 m_ip = IpAddr(ipaddr); 00140 m_state = LWIPNETDNS_OK; 00141 } 00142 00143 00144 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg) 00145 { 00146 DBG("LwipNetDnsRequest: Static callback\n"); 00147 LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg; 00148 return pMe->foundCb( name, ipaddr ); 00149 } 00150 00151 #endif 00152
Generated on Tue Jul 12 2022 11:52:57 by 1.7.2