Blame ares_library_init.c

Packit 514978
Packit 514978
/* Copyright 1998 by the Massachusetts Institute of Technology.
Packit 514978
 * Copyright (C) 2004-2009 by Daniel Stenberg
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this
Packit 514978
 * software and its documentation for any purpose and without
Packit 514978
 * fee is hereby granted, provided that the above copyright
Packit 514978
 * notice appear in all copies and that both that copyright
Packit 514978
 * notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in
Packit 514978
 * advertising or publicity pertaining to distribution of the
Packit 514978
 * software without specific, written prior permission.
Packit 514978
 * M.I.T. makes no representations about the suitability of
Packit 514978
 * this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_library_init.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
/* library-private global and unique instance vars */
Packit 514978
Packit 514978
#ifdef USE_WINSOCK
Packit 514978
fpGetNetworkParams_t ares_fpGetNetworkParams = ZERO_NULL;
Packit 514978
fpSystemFunction036_t ares_fpSystemFunction036 = ZERO_NULL;
Packit 514978
fpGetAdaptersAddresses_t ares_fpGetAdaptersAddresses = ZERO_NULL;
Packit 514978
fpGetBestRoute2_t ares_fpGetBestRoute2 = ZERO_NULL;
Packit 514978
#endif
Packit 514978
Packit 514978
/* library-private global vars with source visibility restricted to this file */
Packit 514978
Packit 514978
static unsigned int ares_initialized;
Packit 514978
static int          ares_init_flags;
Packit 514978
Packit 514978
/* library-private global vars with visibility across the whole library */
Packit 514978
void *(*ares_malloc)(size_t size) = malloc;
Packit 514978
void *(*ares_realloc)(void *ptr, size_t size) = realloc;
Packit 514978
void (*ares_free)(void *ptr) = free;
Packit 514978
Packit 514978
#ifdef USE_WINSOCK
Packit 514978
static HMODULE hnd_iphlpapi;
Packit 514978
static HMODULE hnd_advapi32;
Packit 514978
#endif
Packit 514978
Packit 514978
Packit 514978
static int ares_win32_init(void)
Packit 514978
{
Packit 514978
#ifdef USE_WINSOCK
Packit 514978
Packit 514978
  hnd_iphlpapi = 0;
Packit 514978
  hnd_iphlpapi = LoadLibraryW(L"iphlpapi.dll");
Packit 514978
  if (!hnd_iphlpapi)
Packit 514978
    return ARES_ELOADIPHLPAPI;
Packit 514978
Packit 514978
  ares_fpGetNetworkParams = (fpGetNetworkParams_t)
Packit 514978
    GetProcAddress(hnd_iphlpapi, "GetNetworkParams");
Packit 514978
  if (!ares_fpGetNetworkParams)
Packit 514978
    {
Packit 514978
      FreeLibrary(hnd_iphlpapi);
Packit 514978
      return ARES_EADDRGETNETWORKPARAMS;
Packit 514978
    }
Packit 514978
Packit 514978
  ares_fpGetAdaptersAddresses = (fpGetAdaptersAddresses_t)
Packit 514978
    GetProcAddress(hnd_iphlpapi, "GetAdaptersAddresses");
Packit 514978
  if (!ares_fpGetAdaptersAddresses)
Packit 514978
    {
Packit 514978
      /* This can happen on clients before WinXP, I don't
Packit 514978
         think it should be an error, unless we don't want to
Packit 514978
         support Windows 2000 anymore */
Packit 514978
    }
Packit 514978
Packit 514978
  ares_fpGetBestRoute2 = (fpGetBestRoute2_t)
Packit 514978
    GetProcAddress(hnd_iphlpapi, "GetBestRoute2");
Packit 514978
  if (!ares_fpGetBestRoute2)
Packit 514978
    {
Packit 514978
      /* This can happen on clients before Vista, I don't
Packit 514978
         think it should be an error, unless we don't want to
Packit 514978
         support Windows XP anymore */
Packit 514978
    }
Packit 514978
Packit 514978
  /*
Packit 514978
   * When advapi32.dll is unavailable or advapi32.dll has no SystemFunction036,
Packit 514978
   * also known as RtlGenRandom, which is the case for Windows versions prior
Packit 514978
   * to WinXP then c-ares uses portable rand() function. Then don't error here.
Packit 514978
   */
Packit 514978
Packit 514978
  hnd_advapi32 = 0;
Packit 514978
  hnd_advapi32 = LoadLibraryW(L"advapi32.dll");
Packit 514978
  if (hnd_advapi32)
Packit 514978
    {
Packit 514978
      ares_fpSystemFunction036 = (fpSystemFunction036_t)
Packit 514978
        GetProcAddress(hnd_advapi32, "SystemFunction036");
Packit 514978
    }
Packit 514978
Packit 514978
#endif
Packit 514978
  return ARES_SUCCESS;
Packit 514978
}
Packit 514978
Packit 514978
Packit 514978
static void ares_win32_cleanup(void)
Packit 514978
{
Packit 514978
#ifdef USE_WINSOCK
Packit 514978
  if (hnd_advapi32)
Packit 514978
    FreeLibrary(hnd_advapi32);
Packit 514978
  if (hnd_iphlpapi)
Packit 514978
    FreeLibrary(hnd_iphlpapi);
Packit 514978
#endif
Packit 514978
}
Packit 514978
Packit 514978
Packit 514978
int ares_library_init(int flags)
Packit 514978
{
Packit 514978
  int res;
Packit 514978
Packit 514978
  if (ares_initialized)
Packit 514978
    {
Packit 514978
      ares_initialized++;
Packit 514978
      return ARES_SUCCESS;
Packit 514978
    }
Packit 514978
  ares_initialized++;
Packit 514978
Packit 514978
  if (flags & ARES_LIB_INIT_WIN32)
Packit 514978
    {
Packit 514978
      res = ares_win32_init();
Packit 514978
      if (res != ARES_SUCCESS)
Packit 514978
        return res;  /* LCOV_EXCL_LINE: can't test Win32 init failure */
Packit 514978
    }
Packit 514978
Packit 514978
  ares_init_flags = flags;
Packit 514978
Packit 514978
  return ARES_SUCCESS;
Packit 514978
}
Packit 514978
Packit 514978
int ares_library_init_mem(int flags,
Packit 514978
                          void *(*amalloc)(size_t size),
Packit 514978
                          void (*afree)(void *ptr),
Packit 514978
                          void *(*arealloc)(void *ptr, size_t size))
Packit 514978
{
Packit 514978
  if (amalloc)
Packit 514978
    ares_malloc = amalloc;
Packit 514978
  if (arealloc)
Packit 514978
    ares_realloc = arealloc;
Packit 514978
  if (afree)
Packit 514978
    ares_free = afree;
Packit 514978
  return ares_library_init(flags);
Packit 514978
}
Packit 514978
Packit 514978
Packit 514978
void ares_library_cleanup(void)
Packit 514978
{
Packit 514978
  if (!ares_initialized)
Packit 514978
    return;
Packit 514978
  ares_initialized--;
Packit 514978
  if (ares_initialized)
Packit 514978
    return;
Packit 514978
Packit 514978
  if (ares_init_flags & ARES_LIB_INIT_WIN32)
Packit 514978
    ares_win32_cleanup();
Packit 514978
Packit 514978
  ares_init_flags = ARES_LIB_INIT_NONE;
Packit 514978
  ares_malloc = malloc;
Packit 514978
  ares_realloc = realloc;
Packit 514978
  ares_free = free;
Packit 514978
}
Packit 514978
Packit 514978
Packit 514978
int ares_library_initialized(void)
Packit 514978
{
Packit 514978
#ifdef USE_WINSOCK
Packit 514978
  if (!ares_initialized)
Packit 514978
    return ARES_ENOTINITIALIZED;
Packit 514978
#endif
Packit 514978
  return ARES_SUCCESS;
Packit 514978
}