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