Blame resolv/resolv_context.h

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