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