Fork of NetServicesMin with some warnings removed

Dependencies:   lwip-sys lwip

Fork of NetServicesMin by Hendrik Lipka

Committer:
uci1
Date:
Sat Nov 24 20:20:45 2012 +0000
Revision:
4:c8b2f969c8dd
Parent:
0:8b387bed54c2
Remove debugging printouts

Who changed what in which revision?

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