Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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