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