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