User | Revision | Line number | New contents of line |
robertcook |
0:32a0996dff0f
|
1
|
|
robertcook |
0:32a0996dff0f
|
2
|
/*
|
robertcook |
0:32a0996dff0f
|
3
|
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
|
robertcook |
0:32a0996dff0f
|
4
|
|
robertcook |
0:32a0996dff0f
|
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
robertcook |
0:32a0996dff0f
|
6
|
of this software and associated documentation files (the "Software"), to deal
|
robertcook |
0:32a0996dff0f
|
7
|
in the Software without restriction, including without limitation the rights
|
robertcook |
0:32a0996dff0f
|
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
robertcook |
0:32a0996dff0f
|
9
|
copies of the Software, and to permit persons to whom the Software is
|
robertcook |
0:32a0996dff0f
|
10
|
furnished to do so, subject to the following conditions:
|
robertcook |
0:32a0996dff0f
|
11
|
|
robertcook |
0:32a0996dff0f
|
12
|
The above copyright notice and this permission notice shall be included in
|
robertcook |
0:32a0996dff0f
|
13
|
all copies or substantial portions of the Software.
|
robertcook |
0:32a0996dff0f
|
14
|
|
robertcook |
0:32a0996dff0f
|
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
robertcook |
0:32a0996dff0f
|
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
robertcook |
0:32a0996dff0f
|
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
robertcook |
0:32a0996dff0f
|
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
robertcook |
0:32a0996dff0f
|
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
robertcook |
0:32a0996dff0f
|
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
robertcook |
0:32a0996dff0f
|
21
|
THE SOFTWARE.
|
robertcook |
0:32a0996dff0f
|
22
|
*/
|
robertcook |
0:32a0996dff0f
|
23
|
|
robertcook |
0:32a0996dff0f
|
24
|
/** \file
|
robertcook |
0:32a0996dff0f
|
25
|
DNS Request header file
|
robertcook |
0:32a0996dff0f
|
26
|
*/
|
robertcook |
0:32a0996dff0f
|
27
|
|
robertcook |
0:32a0996dff0f
|
28
|
#ifndef DNSREQUEST_H
|
robertcook |
0:32a0996dff0f
|
29
|
#define DNSREQUEST_H
|
robertcook |
0:32a0996dff0f
|
30
|
|
robertcook |
0:32a0996dff0f
|
31
|
#include "core/net.h"
|
robertcook |
0:32a0996dff0f
|
32
|
#include "core/ipaddr.h"
|
robertcook |
0:32a0996dff0f
|
33
|
#include "core/host.h"
|
robertcook |
0:32a0996dff0f
|
34
|
//Essentially it is a safe interface to NetDnsRequest
|
robertcook |
0:32a0996dff0f
|
35
|
|
robertcook |
0:32a0996dff0f
|
36
|
///DNS Request error codes
|
robertcook |
0:32a0996dff0f
|
37
|
enum DNSRequestErr
|
robertcook |
0:32a0996dff0f
|
38
|
{
|
robertcook |
0:32a0996dff0f
|
39
|
__DNS_MIN = -0xFFFF,
|
robertcook |
0:32a0996dff0f
|
40
|
DNS_SETUP, ///<DNSRequest not properly configured
|
robertcook |
0:32a0996dff0f
|
41
|
DNS_IF, ///<Interface has problems, does not exist or is not initialized
|
robertcook |
0:32a0996dff0f
|
42
|
DNS_MEM, ///<Not enough mem
|
robertcook |
0:32a0996dff0f
|
43
|
DNS_INUSE, ///<Interface / Port is in use
|
robertcook |
0:32a0996dff0f
|
44
|
DNS_PROCESSING, ///<Request has not completed
|
robertcook |
0:32a0996dff0f
|
45
|
//...
|
robertcook |
0:32a0996dff0f
|
46
|
DNS_OK = 0 ///<Success
|
robertcook |
0:32a0996dff0f
|
47
|
};
|
robertcook |
0:32a0996dff0f
|
48
|
|
robertcook |
0:32a0996dff0f
|
49
|
///DNS Request Result Events
|
robertcook |
0:32a0996dff0f
|
50
|
enum DNSReply
|
robertcook |
0:32a0996dff0f
|
51
|
{
|
robertcook |
0:32a0996dff0f
|
52
|
DNS_PRTCL,
|
robertcook |
0:32a0996dff0f
|
53
|
DNS_NOTFOUND, ///Hostname is unknown
|
robertcook |
0:32a0996dff0f
|
54
|
DNS_ERROR, ///Problem with DNS Service
|
robertcook |
0:32a0996dff0f
|
55
|
//...
|
robertcook |
0:32a0996dff0f
|
56
|
DNS_FOUND,
|
robertcook |
0:32a0996dff0f
|
57
|
};
|
robertcook |
0:32a0996dff0f
|
58
|
|
robertcook |
0:32a0996dff0f
|
59
|
class NetDnsRequest;
|
robertcook |
0:32a0996dff0f
|
60
|
enum NetDnsReply;
|
robertcook |
0:32a0996dff0f
|
61
|
|
robertcook |
0:32a0996dff0f
|
62
|
///This is a simple DNS Request class
|
robertcook |
0:32a0996dff0f
|
63
|
/**
|
robertcook |
0:32a0996dff0f
|
64
|
This class exposes an API to deal with DNS Requests
|
robertcook |
0:32a0996dff0f
|
65
|
*/
|
robertcook |
0:32a0996dff0f
|
66
|
class DNSRequest
|
robertcook |
0:32a0996dff0f
|
67
|
{
|
robertcook |
0:32a0996dff0f
|
68
|
public:
|
robertcook |
0:32a0996dff0f
|
69
|
///Creates a new request
|
robertcook |
0:32a0996dff0f
|
70
|
DNSRequest();
|
robertcook |
0:32a0996dff0f
|
71
|
|
robertcook |
0:32a0996dff0f
|
72
|
///Terminates and closes request
|
robertcook |
0:32a0996dff0f
|
73
|
~DNSRequest();
|
robertcook |
0:32a0996dff0f
|
74
|
|
robertcook |
0:32a0996dff0f
|
75
|
///Resolves an hostname
|
robertcook |
0:32a0996dff0f
|
76
|
/**
|
robertcook |
0:32a0996dff0f
|
77
|
@param hostname : hostname to resolve
|
robertcook |
0:32a0996dff0f
|
78
|
*/
|
robertcook |
0:32a0996dff0f
|
79
|
DNSRequestErr resolve(const char* hostname);
|
robertcook |
0:32a0996dff0f
|
80
|
|
robertcook |
0:32a0996dff0f
|
81
|
///Resolves an hostname
|
robertcook |
0:32a0996dff0f
|
82
|
/**
|
robertcook |
0:32a0996dff0f
|
83
|
@param host : hostname to resolve, the result will be stored in the IpAddr field of this object
|
robertcook |
0:32a0996dff0f
|
84
|
*/
|
robertcook |
0:32a0996dff0f
|
85
|
DNSRequestErr resolve(Host* pHost);
|
robertcook |
0:32a0996dff0f
|
86
|
|
robertcook |
0:32a0996dff0f
|
87
|
///Setups callback
|
robertcook |
0:32a0996dff0f
|
88
|
/**
|
robertcook |
0:32a0996dff0f
|
89
|
The callback function will be called on result.
|
robertcook |
0:32a0996dff0f
|
90
|
@param pMethod : callback function
|
robertcook |
0:32a0996dff0f
|
91
|
*/
|
robertcook |
0:32a0996dff0f
|
92
|
void setOnReply( void (*pMethod)(DNSReply) );
|
robertcook |
0:32a0996dff0f
|
93
|
|
robertcook |
0:32a0996dff0f
|
94
|
class CDummy;
|
robertcook |
0:32a0996dff0f
|
95
|
///Setups callback
|
robertcook |
0:32a0996dff0f
|
96
|
/**
|
robertcook |
0:32a0996dff0f
|
97
|
The callback function will be called on result.
|
robertcook |
0:32a0996dff0f
|
98
|
@param pItem : instance of class on which to execute the callback method
|
robertcook |
0:32a0996dff0f
|
99
|
@param pMethod : callback method
|
robertcook |
0:32a0996dff0f
|
100
|
*/
|
robertcook |
0:32a0996dff0f
|
101
|
template<class T>
|
robertcook |
0:32a0996dff0f
|
102
|
void setOnReply( T* pItem, void (T::*pMethod)(DNSReply) )
|
robertcook |
0:32a0996dff0f
|
103
|
{
|
robertcook |
0:32a0996dff0f
|
104
|
m_pCbItem = (CDummy*) pItem;
|
robertcook |
0:32a0996dff0f
|
105
|
m_pCbMeth = (void (CDummy::*)(DNSReply)) pMethod;
|
robertcook |
0:32a0996dff0f
|
106
|
}
|
robertcook |
0:32a0996dff0f
|
107
|
|
robertcook |
0:32a0996dff0f
|
108
|
///Gets IP address once it has been resolved
|
robertcook |
0:32a0996dff0f
|
109
|
/**
|
robertcook |
0:32a0996dff0f
|
110
|
@param pIp : pointer to an IpAddr instance in which to store the resolved IP address
|
robertcook |
0:32a0996dff0f
|
111
|
*/
|
robertcook |
0:32a0996dff0f
|
112
|
DNSRequestErr getResult(IpAddr* pIp);
|
robertcook |
0:32a0996dff0f
|
113
|
|
robertcook |
0:32a0996dff0f
|
114
|
///Closes DNS Request before completion
|
robertcook |
0:32a0996dff0f
|
115
|
DNSRequestErr close();
|
robertcook |
0:32a0996dff0f
|
116
|
|
robertcook |
0:32a0996dff0f
|
117
|
protected:
|
robertcook |
0:32a0996dff0f
|
118
|
void onNetDnsReply(NetDnsReply r);
|
robertcook |
0:32a0996dff0f
|
119
|
DNSRequestErr checkInst();
|
robertcook |
0:32a0996dff0f
|
120
|
|
robertcook |
0:32a0996dff0f
|
121
|
private:
|
robertcook |
0:32a0996dff0f
|
122
|
NetDnsRequest* m_pNetDnsRequest;
|
robertcook |
0:32a0996dff0f
|
123
|
|
robertcook |
0:32a0996dff0f
|
124
|
CDummy* m_pCbItem;
|
robertcook |
0:32a0996dff0f
|
125
|
void (CDummy::*m_pCbMeth)(DNSReply);
|
robertcook |
0:32a0996dff0f
|
126
|
|
robertcook |
0:32a0996dff0f
|
127
|
void (*m_pCb)(DNSReply);
|
robertcook |
0:32a0996dff0f
|
128
|
|
robertcook |
0:32a0996dff0f
|
129
|
};
|
robertcook |
0:32a0996dff0f
|
130
|
|
robertcook |
0:32a0996dff0f
|
131
|
#endif
|