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