NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Fri Jun 18 10:38:57 2010 +0000
Revision:
3:95e0bc00a1bb
Parent:
2:a4f97773c90f

        

Who changed what in which revision?

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