1 | // |
---|
2 | // DNS.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/DNS.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: NetCore |
---|
8 | // Module: DNS |
---|
9 | // |
---|
10 | // Definition of the DNS class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_DNS_INCLUDED |
---|
40 | #define Net_DNS_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/SocketDefs.h" |
---|
45 | #include "Poco/Net/IPAddress.h" |
---|
46 | #include "Poco/Net/HostEntry.h" |
---|
47 | #include "Poco/Mutex.h" |
---|
48 | #include <map> |
---|
49 | |
---|
50 | |
---|
51 | namespace Poco { |
---|
52 | namespace Net { |
---|
53 | |
---|
54 | |
---|
55 | class Net_API DNS |
---|
56 | /// This class provides an interface to the |
---|
57 | /// domain name service. |
---|
58 | /// |
---|
59 | /// An internal DNS cache is used to speed up name lookups. |
---|
60 | { |
---|
61 | public: |
---|
62 | static const HostEntry& hostByName(const std::string& hostname); |
---|
63 | /// Returns a HostEntry object containing the DNS information |
---|
64 | /// for the host with the given name. |
---|
65 | /// |
---|
66 | /// Throws a HostNotFoundException if a host with the given |
---|
67 | /// name cannot be found. |
---|
68 | /// |
---|
69 | /// Throws a NoAddressFoundException if no address can be |
---|
70 | /// found for the hostname. |
---|
71 | /// |
---|
72 | /// Throws a DNSException in case of a general DNS error. |
---|
73 | /// |
---|
74 | /// Throws an IOException in case of any other error. |
---|
75 | |
---|
76 | static const HostEntry& hostByAddress(const IPAddress& address); |
---|
77 | /// Returns a HostEntry object containing the DNS information |
---|
78 | /// for the host with the given IP address. |
---|
79 | /// |
---|
80 | /// Throws a HostNotFoundException if a host with the given |
---|
81 | /// name cannot be found. |
---|
82 | /// |
---|
83 | /// Throws a DNSException in case of a general DNS error. |
---|
84 | /// |
---|
85 | /// Throws an IOException in case of any other error. |
---|
86 | |
---|
87 | static const HostEntry& resolve(const std::string& address); |
---|
88 | /// Returns a HostEntry object containing the DNS information |
---|
89 | /// for the host with the given IP address or host name. |
---|
90 | /// |
---|
91 | /// Throws a HostNotFoundException if a host with the given |
---|
92 | /// name cannot be found. |
---|
93 | /// |
---|
94 | /// Throws a NoAddressFoundException if no address can be |
---|
95 | /// found for the hostname. |
---|
96 | /// |
---|
97 | /// Throws a DNSException in case of a general DNS error. |
---|
98 | /// |
---|
99 | /// Throws an IOException in case of any other error. |
---|
100 | |
---|
101 | static IPAddress resolveOne(const std::string& address); |
---|
102 | /// Convenience method that calls resolve(address) and returns |
---|
103 | /// the first address from the HostInfo. |
---|
104 | |
---|
105 | static const HostEntry& thisHost(); |
---|
106 | /// Returns a HostEntry object containing the DNS information |
---|
107 | /// for this host. |
---|
108 | /// |
---|
109 | /// Throws a HostNotFoundException if DNS information |
---|
110 | /// for this host cannot be found. |
---|
111 | /// |
---|
112 | /// Throws a NoAddressFoundException if no address can be |
---|
113 | /// found for this host. |
---|
114 | /// |
---|
115 | /// Throws a DNSException in case of a general DNS error. |
---|
116 | /// |
---|
117 | /// Throws an IOException in case of any other error. |
---|
118 | |
---|
119 | static void flushCache(); |
---|
120 | /// Flushes the internal DNS cache. |
---|
121 | |
---|
122 | static std::string hostName(); |
---|
123 | /// Returns the host name of this host. |
---|
124 | |
---|
125 | protected: |
---|
126 | static int lastError(); |
---|
127 | /// Returns the code of the last error. |
---|
128 | |
---|
129 | static void error(int code, const std::string& arg); |
---|
130 | /// Throws an exception according to the error code. |
---|
131 | |
---|
132 | private: |
---|
133 | typedef std::map<std::string, HostEntry> DNSCache; |
---|
134 | |
---|
135 | static DNSCache _cache; |
---|
136 | static Poco::FastMutex _mutex; |
---|
137 | }; |
---|
138 | |
---|
139 | |
---|
140 | } } // namespace Poco::Net |
---|
141 | |
---|
142 | |
---|
143 | #endif // Net_DNS_INCLUDED |
---|