modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
0:355018f44c9f
minor change

Who changed what in which revision?

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