1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*******************************************************************************
* shroudBNC - an object-oriented framework for IRC *
* Copyright (C) 2005-2014 Gunnar Beutner *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*******************************************************************************/
#include "StdAfx.h"
ares_channel CDnsQuery::m_DnsChannel; /**< ares channel object */
/**
* GenericDnsQueryCallback
*
* Used as a thunk between c-ares' C-style callbacks and shroudBNC's
* object-oriented dns class.
*
* @param CookieRaw a pointer to a DnsEventCookie
* @param Status the status of the dns query
* @param Timeouts the number of timeouts that occured while querying the dns servers
* @param HostEntity the response for the dns query (can be NULL)
*/
void GenericDnsQueryCallback(void *CookieRaw, int Status, int Timeouts, hostent *HostEntity) {
DnsEventCookie *Cookie = (DnsEventCookie *)CookieRaw;
if (Cookie->Query == NULL) {
return;
}
Cookie->Query->AsyncDnsEvent(Status, HostEntity);
Cookie->Query->m_PendingQueries--;
Cookie->RefCount--;
if (Cookie->RefCount <= 0) {
delete Cookie;
}
}
/**
* CDnsQuery
*
* Constructs a new DNS query object.
*
* @param EventInterface an object to which DNS callbacks are routed
* @param EventFunction a function which is called when DNS queries are completed
* @param Timeout specifies the timeout value for dns queries
*
* \see IMPL_DNSEVENTPROXY
* \see USE_DNSEVENTPROXY
*/
CDnsQuery::CDnsQuery(void *EventInterface, DnsEventFunction EventFunction, int Timeout) {
m_Timeout = Timeout;
m_EventCookie = new DnsEventCookie;
m_EventCookie->RefCount = 1;
m_EventCookie->Query = this;
m_EventObject = EventInterface;
m_EventFunction = EventFunction;
m_PendingQueries = 0;
if (m_DnsChannel == NULL) {
ares_options Options;
Options.timeout = m_Timeout;
ares_init_options(&m_DnsChannel, &Options, ARES_OPT_TIMEOUT);
}
}
/**
* ~CDnsQuery
*
* Destroys a CDnsQuery object.
*/
CDnsQuery::~CDnsQuery(void) {
m_EventCookie->RefCount--;
m_EventCookie->Query = NULL;
if (m_EventCookie->RefCount <= 0) {
delete m_EventCookie;
}
}
/**
* GetHostByName
*
* Asynchronously performs the task of the "gethostbyname" function
* and calls the callback function once the operation has completed.
*
* @param Host the hostname
* @param Family the address family (AF_INET or AF_INET6)
*/
void CDnsQuery::GetHostByName(const char *Host, int Family) {
struct addrinfo hints = {}, *result;
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = Family;
if (getaddrinfo(Host, NULL, &hints, &result) == 0) {
struct hostent hent = {};
char *Address;
char *AddressList[2];
hent.h_addrtype = result->ai_family;
hent.h_length = INADDR_LEN(result->ai_family);
if (result->ai_family == AF_INET) {
Address = (char *)&(((sockaddr_in *)result->ai_addr)->sin_addr);
} else if (result->ai_family == AF_INET6) {
Address = (char *)&(((sockaddr_in6 *)result->ai_addr)->sin6_addr);
} else {
m_EventCookie->RefCount++;
GenericDnsQueryCallback(m_EventCookie, ARES_EBADNAME, 0, NULL);
return;
}
AddressList[0] = Address;
AddressList[1] = NULL;
hent.h_addr_list = AddressList;
m_EventCookie->RefCount++;
GenericDnsQueryCallback(m_EventCookie, ARES_SUCCESS, 0, &hent);
freeaddrinfo(result);
return;
}
m_PendingQueries++;
m_EventCookie->RefCount++;
ares_gethostbyname(m_DnsChannel, Host, Family, GenericDnsQueryCallback, m_EventCookie);
}
/**
* GetHostByAddr
*
* Asynchronously performs the task of the "gethostbyaddr" function
* and calls the callback function once the operation has completed.
*
* @param Address the address for which the hostname should be looked up
*/
void CDnsQuery::GetHostByAddr(sockaddr *Address) {
void *IpAddr;
#ifdef HAVE_IPV6
if (Address->sa_family == AF_INET) {
#endif /* HAVE_IPV6 */
IpAddr = &(((sockaddr_in *)Address)->sin_addr);
#ifdef HAVE_IPV6
} else {
IpAddr = &(((sockaddr_in6 *)Address)->sin6_addr);
}
#endif /* HAVE_IPV6 */
m_PendingQueries++;
m_EventCookie->RefCount++;
ares_gethostbyaddr(m_DnsChannel, IpAddr, INADDR_LEN(Address->sa_family),
Address->sa_family, GenericDnsQueryCallback, m_EventCookie);
}
/**
* AsyncDnsEvent
*
* Used by GenericDnsQueryCallback to notify the object of the status
* of a dns query.
*
* @param Status the status
* @param Response the response for the dns query
*/
void CDnsQuery::AsyncDnsEvent(int Status, hostent *Response) {
if (m_EventFunction != NULL) {
(*m_EventFunction)(m_EventObject, Response);
}
}
/**
* RegisterSockets
*
* Registers all DNS sockets and returns a cookie that can be used to
* unregister the sockets later on - or NULL if no sockets were registered.
*
* @return DnsSocketCookie a cookie
*/
DnsSocketCookie *CDnsQuery::RegisterSockets() {
SOCKET Sockets[ARES_GETSOCK_MAXNUM];
int Bitmask, Count = 0;
DnsSocketCookie *Cookie;
if (m_DnsChannel == NULL) {
return NULL;
}
Bitmask = ares_getsock(m_DnsChannel, Sockets, sizeof(Sockets) / sizeof(*Sockets));
Cookie = new DnsSocketCookie;
if (AllocFailed(Cookie)) {
g_Bouncer->Fatal();
}
for (int i = 0; i < (int)(sizeof(Sockets) / sizeof(*Sockets)); i++) {
if (!ARES_GETSOCK_READABLE(Bitmask, i) && !ARES_GETSOCK_WRITABLE(Bitmask, i)) {
continue;
}
Count++;
// ctor takes care of registering the socket
#ifdef _MSC_VER
# pragma warning( push )
# pragma warning( disable : 4800 )
#endif /* _MSC_VER */
Cookie->Sockets[Count - 1] = new CDnsSocket(Sockets[i], ARES_GETSOCK_WRITABLE(Bitmask, i));
#ifdef _MSC_VER
# pragma warning( pop )
#endif /* _MSC_VER */
}
if (Count == 0) {
delete Cookie;
return NULL;
}
Cookie->Count = Count;
return Cookie;
}
/**
* UnregisterSockets
*
* Unregisters a set of DNS sockets.
*
* @param Cookie a cookie returned by RegisterSockets
*/
void CDnsQuery::UnregisterSockets(DnsSocketCookie *Cookie) {
if (Cookie == NULL) {
return;
}
for (int i = 0; i < Cookie->Count; i++) {
// dtor takes care of un-registering the socket
Cookie->Sockets[i]->Destroy();
delete Cookie->Sockets[i];
}
delete Cookie;
}
/**
* ProcessTimeouts
*
* Processes timeouts for the DNS sockets.
*/
void CDnsQuery::ProcessTimeouts(void) {
if (m_DnsChannel != NULL) {
ares_process_fd(m_DnsChannel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
}
}
/**
* GetDnsChannel
*
* Returns the c-ares channel object.
*
* @return ares_channel the channel object
*/
ares_channel CDnsQuery::GetDnsChannel(void) {
return m_DnsChannel;
}
|