This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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