/src/resiprocate/rutil/DnsUtil.hxx
Line | Count | Source (jump to first uncovered line) |
1 | | #if !defined(RESIP_DNSUTIL_HXX) |
2 | | #define RESIP_DNSUTIL_HXX |
3 | | |
4 | | #include <list> |
5 | | #include "rutil/Socket.hxx" |
6 | | #include "rutil/BaseException.hxx" |
7 | | #include "rutil/Data.hxx" |
8 | | |
9 | | #ifndef WIN32 |
10 | | #include <netinet/in.h> |
11 | | #else |
12 | | #include <Ws2tcpip.h> |
13 | | #endif |
14 | | |
15 | | struct in_addr; |
16 | | struct in6_addr; |
17 | | namespace resip |
18 | | { |
19 | | |
20 | | class Tuple; |
21 | | |
22 | | /** |
23 | | @brief Provides a collection of utility functions for |
24 | | manipulating DNS names and IP addresses and discovering |
25 | | details about the local interfaces |
26 | | @ingroup network |
27 | | */ |
28 | | class DnsUtil |
29 | | { |
30 | | public: |
31 | | class Exception final : public BaseException |
32 | | { |
33 | | public: |
34 | | Exception(const Data& msg, |
35 | | const Data& file, |
36 | | const int line) |
37 | 0 | : BaseException(msg, file, line) {} |
38 | | protected: |
39 | 0 | const char* name() const noexcept override { return "DnsUtil::Exception"; } |
40 | | }; |
41 | | |
42 | | /** @returns the fully qualified local host name as the host |
43 | | * currently knows it ala gethostname(3) |
44 | | */ |
45 | | |
46 | | static const Data& getLocalHostName(); |
47 | | |
48 | | /** @returns the suffix after the first "." in whatever getLocalHostName returns */ |
49 | | static Data getLocalDomainName(); |
50 | | |
51 | | /** Gets the IP address of "the" local interface. Note that this will not work |
52 | | * on systems with more than one ethernet interface. |
53 | | * @deprecated Very few real systems have only one interface (loopback plus |
54 | | * physical, vpns, wireless and wired, etc.) Use getInterfaces |
55 | | * instead. |
56 | | */ |
57 | | |
58 | | static Data getLocalIpAddress(const Data& defaultInterface=Data::Empty) ; |
59 | | |
60 | | // returns pair of interface name, ip address |
61 | | /** @returns a list of interface name, IP addresses pairs of all available |
62 | | * interfaces (note that some of these may be v4 and others v6. |
63 | | */ |
64 | | static std::list<std::pair<Data,Data> > getInterfaces(const Data& matchingInterface=Data::Empty); |
65 | | |
66 | | // wrappers for the not so ubiquitous inet_pton, inet_ntop (e.g. WIN32) |
67 | | |
68 | | /// Converts from the network format to presentation |
69 | | /// format. That is, it converts from struct in_addr to |
70 | | /// character representation of the IPv4 address. |
71 | | static Data inet_ntop(const struct in_addr& addr); |
72 | | |
73 | | /// Converts from the network format to presentation |
74 | | /// format. Converts from struct sockaddr to character |
75 | | /// representation of the IPv4 address. |
76 | | static Data inet_ntop(const struct sockaddr& addr); |
77 | | |
78 | | /// Convert from the presentation format of the IPv4 address to |
79 | | /// struct in_addr. |
80 | | static int inet_pton(const Data& printableIp, struct in_addr& dst); |
81 | | |
82 | | static bool isIpAddress(const Data& ipAddress); |
83 | | static bool isIpV4Address(const Data& ipAddress); |
84 | | /** |
85 | | @brief based on RFC 1884 |
86 | | @example |
87 | | @code |
88 | | Data addr("5f1b:df00:ce3e:e200:20:800:2b37:6426"); |
89 | | cerr << "!! "<< addr << endl; |
90 | | resip_assert(DnsUtil::isIpV6Address(addr)); |
91 | | @endcode |
92 | | */ |
93 | | static bool isIpV6Address(const Data& ipAddress); |
94 | | |
95 | | #ifdef IPPROTO_IPV6 |
96 | | /// Converts from the network format to presentation |
97 | | /// format. That is, it converts from struct in_addr6 to |
98 | | /// character representation of the IPv6 address. |
99 | | static Data inet_ntop(const struct in6_addr& addr); |
100 | | |
101 | | /// Convert from the presentation format of the IPv6 address to |
102 | | /// struct in_addr6. |
103 | | static int inet_pton(const Data& printableIp, struct in6_addr& dst); |
104 | | #endif |
105 | | |
106 | | //pass-throughs when supported, actual implemenation in the WIN32 case |
107 | | |
108 | | /// Converts from the network format to presentation |
109 | | /// format. |
110 | | /// |
111 | | /// @param af the address family (AF_INET, AF_INET6) |
112 | | /// |
113 | | /// @param src the address of the networking representation to |
114 | | /// be converted (often of type struct in_addr *) |
115 | | /// |
116 | | /// @param dst the address where presentation format will be stored |
117 | | /// |
118 | | /// @size the size of the dst |
119 | | static const char * inet_ntop(int af, const void* src, char* dst, size_t size); |
120 | | |
121 | | #ifdef __APPLE__ |
122 | | static const Data UInt8ToStr[256]; |
123 | | #endif // __APPLE__ |
124 | | |
125 | | /// Converts from the presentation format to network |
126 | | /// format. |
127 | | /// |
128 | | /// @param af the address family (AF_INET, AF_INET6) |
129 | | /// |
130 | | /// @param src the address of the presentation representation to |
131 | | /// be converted |
132 | | /// |
133 | | /// @param dst the address where presentation format will be |
134 | | /// stored (struct in_addr *, or struct in_addr6 *) |
135 | | static int inet_pton(int af, const char * src, void * dst); |
136 | | |
137 | | |
138 | | // so string (case) comparison will work |
139 | | // or something |
140 | | /** Converts the printable form of an IPv6 address into |
141 | | * consistent format so that string comparisons will do |
142 | | * what you expect. |
143 | | * For instance, XXXX:0:0:0:YYYY:192.168.2.233 will be |
144 | | * converted to XXXX::::YYYY:192.168.2.233. |
145 | | * Currently, this is realized by (inet_ntop(inet_pton(input)). |
146 | | */ |
147 | | static Data canonicalizeIpV6Address(const Data& ipV6Address); |
148 | | |
149 | | /// Used to synchronously query A records - only for test code usage |
150 | | static std::list<Data> lookupARecords(const Data& host); |
151 | | |
152 | | private: |
153 | | /// Used to synchronously query the local hostname |
154 | | static Data lookupLocalHostName(); |
155 | | }; |
156 | | |
157 | | } |
158 | | |
159 | | |
160 | | #endif |
161 | | /* ==================================================================== |
162 | | * The Vovida Software License, Version 1.0 |
163 | | * |
164 | | * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. |
165 | | * |
166 | | * Redistribution and use in source and binary forms, with or without |
167 | | * modification, are permitted provided that the following conditions |
168 | | * are met: |
169 | | * |
170 | | * 1. Redistributions of source code must retain the above copyright |
171 | | * notice, this list of conditions and the following disclaimer. |
172 | | * |
173 | | * 2. Redistributions in binary form must reproduce the above copyright |
174 | | * notice, this list of conditions and the following disclaimer in |
175 | | * the documentation and/or other materials provided with the |
176 | | * distribution. |
177 | | * |
178 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
179 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
180 | | * not be used to endorse or promote products derived from this |
181 | | * software without prior written permission. For written |
182 | | * permission, please contact vocal@vovida.org. |
183 | | * |
184 | | * 4. Products derived from this software may not be called "VOCAL", nor |
185 | | * may "VOCAL" appear in their name, without prior written |
186 | | * permission of Vovida Networks, Inc. |
187 | | * |
188 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
189 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
190 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
191 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
192 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
193 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
194 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
195 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
196 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
197 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
198 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
199 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
200 | | * DAMAGE. |
201 | | * |
202 | | * ==================================================================== |
203 | | * |
204 | | * This software consists of voluntary contributions made by Vovida |
205 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
206 | | * Inc. For more information on Vovida Networks, Inc., please see |
207 | | * <http://www.vovida.org/>. |
208 | | * |
209 | | */ |