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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/* gnome-vfs-resolve.c - Resolver API
Copyright (C) 2004 Christian Kellner <gicmo@gnome.org>
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#ifndef G_OS_WIN32
/* Keep <sys/types.h> above the network includes for FreeBSD. */
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <fcntl.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#ifdef HAVE_RES_NINIT
#include <arpa/nameser.h>
#include <resolv.h>
/* RELOAD_TIMEVAL specifies the minimum of seconds between resolver reloads */
#define RELOAD_TIMEVAL 2
#endif
#include <glib-object.h>
#include <libgnomevfs/gnome-vfs-resolve.h>
#define INIT_BUFSIZE 8192 /* Unix Network Programming Chapter 11 Page 304 :) */
#if !HAVE_GETADDRINFO && !HAVE_GETHOSTBYNAME_R_GLIBC && !HAVE_GETHOSTBYNAME_R_SOLARIS && !HAVE_GETHOSTBYNAME_R_HPUX
G_LOCK_DEFINE (dns_lock);
# ifndef G_OS_WIN32
# ifndef h_errno
extern int h_errno;
# endif /* h_errno */
# endif /* G_OS_WIN32 */
#endif
struct GnomeVFSResolveHandle_ {
#ifdef HAVE_GETADDRINFO
struct addrinfo *result;
struct addrinfo *current;
#else
GList *result;
GList *current;
#endif
};
#ifndef HAVE_GETADDRINFO
static GnomeVFSResult
resolvehandle_from_hostent (struct hostent *he, GnomeVFSResolveHandle **handle)
{
GnomeVFSAddress *addr;
GList *result;
char **iter;
void *aptr;
size_t addrlen;
struct sockaddr *sa;
struct sockaddr_in sin;
#ifdef ENABLE_IPV6
struct sockaddr_in6 sin6;
#endif
switch (he->h_addrtype) {
case AF_INET:
memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
aptr = &(sin.sin_addr);
sa = (struct sockaddr *) &sin;
addrlen = sizeof (struct sockaddr_in);
break;
#ifdef ENABLE_IPV6
case AF_INET6:
memset (&sin6, 0, sizeof (sin6));
sin6.sin6_family = AF_INET6;
aptr = &(sin6.sin6_addr);
sa = (struct sockaddr *) &sin6;
addrlen = sizeof (struct sockaddr_in6);
break;
#endif
default:
return GNOME_VFS_ERROR_INTERNAL;
}
result = NULL;
*handle = NULL;
for (iter = he->h_addr_list; *iter != NULL; iter++) {
g_memmove (aptr, *iter, he->h_length);
addr = gnome_vfs_address_new_from_sockaddr (sa, addrlen);
if (addr != NULL)
result = g_list_append (result, addr);
}
if (result == NULL)
return GNOME_VFS_ERROR_INTERNAL;
*handle = g_new0 (GnomeVFSResolveHandle, 1);
(*handle)->result = result;
(*handle)->current = result;
return GNOME_VFS_OK;
}
#endif
#ifdef HAVE_RES_NINIT
static gboolean
restart_resolve (void)
{
static GTimeVal last_reload = { 0, 0 };
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
GTimeVal now;
gboolean ret;
g_static_mutex_lock (&mutex);
g_get_current_time (&now);
if ((now.tv_sec - last_reload.tv_sec) > RELOAD_TIMEVAL) {
last_reload.tv_sec = now.tv_sec;
ret = (res_ninit (&_res) == 0);
} else {
ret = FALSE;
}
g_static_mutex_unlock (&mutex);
return ret;
}
#else
#define restart_resolve() FALSE
#endif
#ifdef HAVE_GETADDRINFO
static GnomeVFSResult
_gnome_vfs_result_from_gai_error (int error)
{
switch (error) {
case EAI_NONAME: return GNOME_VFS_ERROR_HOST_NOT_FOUND;
#ifdef EAI_ADDRFAMILY
case EAI_ADDRFAMILY: return GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS;
#endif
#ifdef EAI_NODATA
case EAI_NODATA: return GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS;
#endif
#ifdef EAI_SYSTEM
case EAI_SYSTEM: return gnome_vfs_result_from_errno ();
#endif
case EAI_FAIL:
case EAI_AGAIN: return GNOME_VFS_ERROR_NAMESERVER;
case EAI_MEMORY: return GNOME_VFS_ERROR_NO_MEMORY;
/* We should not get these errors there just here to have
complete list of getaddrinfo errors*/
case EAI_FAMILY:
case EAI_SOCKTYPE:
case EAI_SERVICE:
case EAI_BADFLAGS:
default:
return GNOME_VFS_ERROR_INTERNAL;
}
}
#endif
/**
* gnome_vfs_resolve:
* @hostname: hostname you want to resolve.
* @handle: pointer to a pointer to a #GnomeVFSResolveHandle.
*
* Tries to resolve @hostname. If the operation was successful you can
* get the resolved addresses in form of #GnomeVFSAddress by calling
* gnome_vfs_resolve_next_address().
*
* Return value: A #GnomeVFSResult indicating the success of the operation.
*
* Since: 2.8
*/
GnomeVFSResult
gnome_vfs_resolve (const char *hostname,
GnomeVFSResolveHandle **handle)
{
#ifdef HAVE_GETADDRINFO
struct addrinfo hints, *result;
int res;
gboolean retry = TRUE;
restart:
memset (&hints, 0, sizeof (hints));
hints.ai_socktype = SOCK_STREAM;
#ifdef ENABLE_IPV6
# ifdef HAVE_AI_ADDRCONFIG /* RFC3493 */
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
# else
hints.ai_family = AF_UNSPEC;
# endif
#else
hints.ai_family = AF_INET;
#endif /* ENABLE_IPV6 */
res = getaddrinfo (hostname, NULL, &hints, &result);
if (res != 0) {
if (retry && restart_resolve ()) {
retry = FALSE;
goto restart;
} else {
return _gnome_vfs_result_from_gai_error (res);
}
}
*handle = g_new0 (GnomeVFSResolveHandle, 1);
(*handle)->result = result;
(*handle)->current = result;
return GNOME_VFS_OK;
#else /* HAVE_GETADDRINFO */
struct hostent resbuf, *result = &resbuf;
GnomeVFSResult ret;
int res;
gboolean retry = TRUE;
#ifdef HAVE_GETHOSTBYNAME_R_GLIBC
size_t buflen;
char *buf;
int h_errnop;
restart:
buf = NULL;
buflen = INIT_BUFSIZE;
h_errnop = 0;
do {
buf = g_renew (char, buf, buflen);
res = gethostbyname_r (hostname,
&resbuf,
buf,
buflen,
&result,
&h_errnop);
buflen *= 2;
} while (res == ERANGE);
if (res != 0 || result == NULL || result->h_addr_list[0] == NULL) {
g_free (buf);
if (retry && restart_resolve ()) {
retry = FALSE;
goto restart;
} else {
return gnome_vfs_result_from_h_errno_val (h_errnop);
}
}
ret = resolvehandle_from_hostent (result, handle);
g_free (buf);
#elif HAVE_GETHOSTBYNAME_R_SOLARIS
size_t buflen;
char *buf;
int h_errnop;
restart:
buf = NULL;
buflen = INIT_BUFSIZE;
h_errnop = 0;
do {
buf = g_renew (char, buf, buflen);
result = gethostbyname_r (hostname,
&resbuf,
buf,
buflen,
&h_errnop);
buflen *= 2;
} while (h_errnop == ERANGE);
if (result == NULL) {
g_free (buf);
if (retry && restart_resolve ()) {
retry = FALSE;
goto restart;
} else {
return gnome_vfs_result_from_h_errno_val (h_errnop);
}
}
ret = resolvehandle_from_hostent (result, handle);
g_free (buf);
#elif HAVE_GETHOSTBYNAME_R_HPUX
struct hostent_data buf;
restart:
res = gethostbyname_r (hostname, result, &buf);
if (res != 0) {
if (retry && restart_resolve ()) {
retry = FALSE;
goto restart;
} else {
return gnome_vfs_result_from_h_errno_val (h_errnop);
}
}
ret = resolvehandle_from_hostent (result, handle);
#else /* !HAVE_GETHOSTBYNAME_R_GLIBC && !HAVE_GETHOSTBYNAME_R_SOLARIS && !HAVE_GETHOSTBYNAME_R_HPUX */
res = 0;/* only set to avoid unused variable error */
G_LOCK (dns_lock);
restart:
result = gethostbyname (hostname);
if (result == NULL) {
if (retry && restart_resolve ()) {
retry = FALSE;
goto restart;
} else {
#ifndef G_OS_WIN32
ret = gnome_vfs_result_from_h_errno ();
#else
switch (WSAGetLastError ()) {
case WSAHOST_NOT_FOUND:
ret = GNOME_VFS_ERROR_HOST_NOT_FOUND;
break;
case WSANO_DATA:
ret = GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS;
break;
case WSATRY_AGAIN:
case WSANO_RECOVERY:
ret = GNOME_VFS_ERROR_NAMESERVER;
default:
ret = GNOME_VFS_ERROR_GENERIC;
}
#endif
}
} else {
ret = resolvehandle_from_hostent (result, handle);
}
G_UNLOCK (dns_lock);
#endif /* !HAVE_GETHOSTBYNAME_R_GLIBC && !HAVE_GETHOSTBYNAME_R_SOLARIS && !HAVE_GETHOSTBYNAME_R_HPUX */
return ret;
#endif /* HAVE_GETADDRINFO */
}
/**
* gnome_vfs_resolve_reset_to_beginning:
* @handle: a #GnomeVFSResolveHandle.
*
* Reset @handle so that a following call to gnome_vfs_resolve_next_address()
* will return the first resolved address.
*
* Since: 2.8
*/
void
gnome_vfs_resolve_reset_to_beginning (GnomeVFSResolveHandle *handle)
{
g_return_if_fail (handle != NULL);
handle->current = handle->result;
}
/**
* gnome_vfs_resolve_next_address:
* @handle: a #GnomeVFSResolveHandle.
* @address: a pointer to a pointer to a #GnomeVFSAddress.
*
* Stores the next #GnomeVFSAddress available in @handle of the
* former lookup in @address.
*
* Return value: %TRUE if the next address was stored in @address or
* %FALSE if no other address is available.
*
* Since: 2.8
*/
gboolean
gnome_vfs_resolve_next_address (GnomeVFSResolveHandle *handle,
GnomeVFSAddress **address)
{
g_return_val_if_fail (address != NULL, FALSE);
g_return_val_if_fail (handle != NULL, FALSE);
*address = NULL;
#ifdef HAVE_GETADDRINFO
while (*address == NULL && handle->current != NULL) {
#ifdef _AIX
/* getaddrinfo() on AIX 4.3.2 and probably others don't set sa_family in
ai_addr so we have to copy it from ai_family */
handle->current->ai_addr->sa_family = handle->current->ai_family;
#endif
*address = gnome_vfs_address_new_from_sockaddr (handle->current->ai_addr,
handle->current->ai_addrlen);
handle->current = handle->current->ai_next;
}
#else
if (handle->current) {
*address = gnome_vfs_address_dup (handle->current->data);
handle->current = handle->current->next;
}
#endif
return *address != NULL;
}
/**
* gnome_vfs_resolve_free:
* @handle: a #GnomeVFSResolveHandle.
*
* Use this function to free a #GnomeVFSResolveHandle returned by
* gnome_vfs_resolve().
*
* Since: 2.8
*/
void
gnome_vfs_resolve_free (GnomeVFSResolveHandle *handle)
{
#ifdef HAVE_GETADDRINFO
if (handle->result != NULL)
freeaddrinfo (handle->result);
#else
for (handle->current = handle->result; handle->current != NULL;
handle->current = handle->current->next) {
gnome_vfs_address_free (handle->current->data);
}
g_list_free (handle->result);
#endif
g_free (handle);
}
|