Blame resolv/resolv_context.h

Packit 6c4009
/* Temporary, thread-local resolver state.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* struct resolv_context objects are allocated on the heap,
Packit 6c4009
   initialized by __resolv_context_get (and its variants), and
Packit 6c4009
   destroyed by __resolv_context_put.
Packit 6c4009
Packit 6c4009
   A nested call to __resolv_context_get (after another call to
Packit 6c4009
   __resolv_context_get without a matching __resolv_context_put call,
Packit 6c4009
   on the same thread) returns the original pointer, instead of
Packit 6c4009
   allocating a new context.  This prevents unexpected reloading of
Packit 6c4009
   the resolver configuration.  Care is taken to keep the context in
Packit 6c4009
   sync with the thread-local _res object.  (This does not happen with
Packit 6c4009
   __resolv_context_get_override, and __resolv_context_get_no_inet6 may
Packit 6c4009
   also interpose another context object if RES_USE_INET6 needs to be
Packit 6c4009
   disabled.)
Packit 6c4009
Packit 6c4009
   In contrast to struct __res_state, struct resolv_context is not
Packit 6c4009
   affected by ABI compatibility concerns.
Packit 6c4009
Packit 6c4009
   For the benefit of the res_n* functions, a struct __res_state
Packit 6c4009
   pointer is included in the context object, and a separate
Packit 6c4009
   initialization function is provided.  */
Packit 6c4009
Packit 6c4009
#ifndef _RESOLV_CONTEXT_H
Packit 6c4009
#define _RESOLV_CONTEXT_H
Packit 6c4009
Packit 6c4009
#include <bits/types/res_state.h>
Packit 6c4009
#include <resolv/resolv_conf.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
Packit 6c4009
/* Temporary resolver state.  */
Packit 6c4009
struct resolv_context
Packit 6c4009
{
Packit 6c4009
  struct __res_state *resp;     /* Backing resolver state.   */
Packit 6c4009
Packit 6c4009
  /* Extended resolver state.  This is set to NULL if the
Packit 6c4009
     __resolv_context_get functions are unable to locate an associated
Packit 6c4009
     extended state.  In this case, the configuration data in *resp
Packit 6c4009
     has to be used; otherwise, the data from *conf should be
Packit 6c4009
     preferred (because it is a superset).  */
Packit 6c4009
  struct resolv_conf *conf;
Packit 6c4009
Packit 6c4009
  /* The following fields are for internal use within the
Packit 6c4009
     resolv_context module.  */
Packit 6c4009
  size_t __refcount;            /* Count of reusages by the get functions.  */
Packit 6c4009
  bool __from_res;              /* True if created from _res.  */
Packit 6c4009
Packit 6c4009
  /* If RES_USE_INET6 was disabled at this level, this field points to
Packit 6c4009
     the previous context.  */
Packit 6c4009
  struct resolv_context *__next;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Return the current temporary resolver context, or NULL if there was
Packit 6c4009
   an error (indicated by errno).  A call to this function must be
Packit 6c4009
   paired with a call to __resolv_context_put.  */
Packit 6c4009
struct resolv_context *__resolv_context_get (void)
Packit 6c4009
  __attribute__ ((warn_unused_result));
Packit 6c4009
libc_hidden_proto (__resolv_context_get)
Packit 6c4009
Packit 6c4009
/* Deallocate the temporary resolver context.  Converse of
Packit 6c4009
   __resolv_context_get.  Restore the RES_USE_INET6 flag if necessary.
Packit 6c4009
   Do nothing if CTX is NULL.  */
Packit 6c4009
void __resolv_context_put (struct resolv_context *ctx);
Packit 6c4009
libc_hidden_proto (__resolv_context_put)
Packit 6c4009
Packit 6c4009
/* Like __resolv_context_get, but the _res structure can be partially
Packit 6c4009
   initialzed and those changes will not be overwritten.  */
Packit 6c4009
struct resolv_context *__resolv_context_get_preinit (void)
Packit 6c4009
  __attribute__ ((warn_unused_result));
Packit 6c4009
libc_hidden_proto (__resolv_context_get_preinit)
Packit 6c4009
Packit 6c4009
/* Wrap a struct __res_state object in a struct resolv_context object.
Packit 6c4009
   A call to this function must be paired with a call to
Packit 6c4009
   __resolv_context_put.  */
Packit 6c4009
struct resolv_context *__resolv_context_get_override (struct __res_state *)
Packit 6c4009
  __attribute__ ((nonnull (1), warn_unused_result));
Packit 6c4009
libc_hidden_proto (__resolv_context_get_override)
Packit 6c4009
Packit 6c4009
/* Return the search path entry at INDEX, or NULL if there are fewer
Packit 6c4009
   than INDEX entries.  */
Packit 6c4009
static __attribute__ ((nonnull (1), unused)) const char *
Packit 6c4009
__resolv_context_search_list (const struct resolv_context *ctx, size_t index)
Packit 6c4009
{
Packit 6c4009
  if (ctx->conf != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (index < ctx->conf->search_list_size)
Packit 6c4009
        return ctx->conf->search_list[index];
Packit 6c4009
      else
Packit 6c4009
        return NULL;
Packit 6c4009
    }
Packit 6c4009
  /* Fallback.  ctx->resp->dnsrch is a NULL-terminated array.  */
Packit 6c4009
  for (size_t i = 0; ctx->resp->dnsrch[i] != NULL && i < MAXDNSRCH; ++i)
Packit 6c4009
    if (i == index)
Packit 6c4009
      return ctx->resp->dnsrch[i];
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the number of name servers.  */
Packit 6c4009
static __attribute__ ((nonnull (1), unused)) size_t
Packit 6c4009
__resolv_context_nameserver_count (const struct resolv_context *ctx)
Packit 6c4009
{
Packit 6c4009
  if (ctx->conf != NULL)
Packit 6c4009
    return ctx->conf->nameserver_list_size;
Packit 6c4009
  else
Packit 6c4009
    return ctx->resp->nscount;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return a pointer to the socket address of the name server INDEX, or
Packit 6c4009
   NULL if the index is out of bounds.  */
Packit 6c4009
static __attribute__ ((nonnull (1), unused)) const struct sockaddr *
Packit 6c4009
__resolv_context_nameserver (const struct resolv_context *ctx, size_t index)
Packit 6c4009
{
Packit 6c4009
  if (ctx->conf != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (index < ctx->conf->nameserver_list_size)
Packit 6c4009
        return ctx->conf->nameserver_list[index];
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    if (index < ctx->resp->nscount)
Packit 6c4009
      {
Packit 6c4009
        if (ctx->resp->nsaddr_list[index].sin_family != 0)
Packit 6c4009
          return (const struct sockaddr *) &ctx->resp->nsaddr_list[index];
Packit 6c4009
        else
Packit 6c4009
          return (const struct sockaddr *) &ctx->resp->_u._ext.nsaddrs[index];
Packit 6c4009
      }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the number of sort list entries.  */
Packit 6c4009
static __attribute__ ((nonnull (1), unused)) size_t
Packit 6c4009
__resolv_context_sort_count (const struct resolv_context *ctx)
Packit 6c4009
{
Packit 6c4009
  if (ctx->conf != NULL)
Packit 6c4009
    return ctx->conf->sort_list_size;
Packit 6c4009
  else
Packit 6c4009
    return ctx->resp->nsort;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Return the sort list entry at INDEX.  */
Packit 6c4009
static __attribute__ ((nonnull (1), unused)) struct resolv_sortlist_entry
Packit 6c4009
__resolv_context_sort_entry (const struct resolv_context *ctx, size_t index)
Packit 6c4009
{
Packit 6c4009
  if (ctx->conf != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (index < ctx->conf->sort_list_size)
Packit 6c4009
        return ctx->conf->sort_list[index];
Packit 6c4009
      /* Fall through.  */
Packit 6c4009
    }
Packit 6c4009
  else if (index < ctx->resp->nsort)
Packit 6c4009
    return (struct resolv_sortlist_entry)
Packit 6c4009
      {
Packit 6c4009
        .addr = ctx->resp->sort_list[index].addr,
Packit 6c4009
        .mask = ctx->resp->sort_list[index].mask,
Packit 6c4009
      };
Packit 6c4009
Packit 6c4009
  return (struct resolv_sortlist_entry) { .mask = 0, };
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Called during thread shutdown to free the associated resolver
Packit 6c4009
   context (mostly in response to cancellation, otherwise the
Packit 6c4009
   __resolv_context_get/__resolv_context_put pairing will already have
Packit 6c4009
   deallocated the context object).  */
Packit 6c4009
void __resolv_context_freeres (void) attribute_hidden;
Packit 6c4009
Packit 6c4009
#endif /* _RESOLV_CONTEXT_H */