async_resolv.c revision 79cafdcc25948300e2b0c85955b67b0d0c4c73c7
/*
SSSD
Async resolver
Authors:
Martin Nagy <mnagy@redhat.com>
Jakub Hrozek <jhrozek@redhat.com>
Copyright (C) Red Hat, Inc 2009
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <ares.h>
#include <talloc.h>
#include <tevent.h>
#include <errno.h>
#include <netdb.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "resolv/async_resolv.h"
#include "util/dlinklist.h"
#ifndef HAVE_ARES_DATA
#define ares_free_data(dataptr) \
#define ares_malloc_data(data) \
#endif /* HAVE_ARES_DATA */
struct fd_watch {
int fd;
struct resolv_ctx *ctx;
};
struct resolv_ctx {
/* Contexts are linked so we can keep track of them and re-create
* the ares channels in all of them at once if we need to. */
struct resolv_ctx *prev;
struct resolv_ctx *next;
struct tevent_context *ev_ctx;
/* List of file descriptors that are watched by tevent. */
/* Time in milliseconds before canceling a DNS request */
int timeout;
/* The timeout watcher periodically calls ares_process_fd() to check
* if our pending requests didn't timeout. */
int pending_requests;
struct tevent_timer *timeout_watcher;
};
struct resolv_ctx *context_list;
static int
return_code(int ares_code)
{
switch (ares_code) {
case ARES_SUCCESS:
return EOK;
case ARES_ENOMEM:
return ENOMEM;
case ARES_EFILE:
default:
return EIO;
}
}
const char *
resolv_strerror(int ares_code)
{
return ares_strerror(ares_code);
}
static int
fd_watch_destructor(struct fd_watch *f)
{
f->fd = -1;
return 0;
}
static void
{
return;
}
if (flags & TEVENT_FD_READ) {
}
if (flags & TEVENT_FD_WRITE) {
}
}
static void
static void
{
}
/* Enforce a minimum of 1 second. */
} else {
}
ctx);
}
}
static void
{
/* NULLify the timeout_watcher so we don't
* free it in the _done() function if it
* gets called. Now that we're already in
* the handler, tevent will take care of
* freeing it when it returns.
*/
if (ctx->pending_requests > 0) {
}
}
static void
{
ctx->pending_requests++;
if (ctx->timeout_watcher) {
return;
}
}
static void
{
if (ctx->pending_requests <= 0) {
return;
}
ctx->pending_requests--;
if (ctx->pending_requests == 0) {
}
}
/*
* When ares is ready to read or write to a file descriptor, it will
* call this callback. If both read and write are 0, it means that ares
* will soon close the socket. We are mainly using this function to register
* new file descriptors with tevent.
*/
static void
{
int flags;
/* The socket is about to get closed. */
fd_event_close(ctx, s);
return;
}
/* Are we already watching this file descriptor? */
while (watch) {
return;
}
}
}
static void
{
/* The file descriptor is new, register it with tevent. */
return;
}
return;
}
}
static void
{
/* Remove the socket from list */
while (watch) {
return;
}
}
}
static int
{
return -1;
}
/* Set ctx->channel to NULL first, so that callbacks that get
* ARES_EDESTRUCTION won't retry. */
return 0;
}
static int
{
int ret;
struct ares_options options;
/* FIXME: the options would contain
* the nameservers to contact, the domains
* to search, timeout... => get from confdb
*/
if (ret != ARES_SUCCESS) {
resolv_strerror(ret)));
return return_code(ret);
}
if (old_channel != NULL) {
}
return EOK;
}
int
{
int ret;
struct resolv_ctx *ctx;
return ENOMEM;
goto done;
}
return EOK;
done:
return ret;
}
void
{
struct resolv_ctx *ctx;
}
}
struct hostent *
{
int len;
int i;
return NULL;
}
goto fail;
}
}
goto fail;
}
for (i = 0; i < len; i++) {
goto fail;
}
}
}
goto fail;
}
for (i = 0; i < len; i++) {
src->h_addr_list[i],
goto fail;
}
}
}
return ret;
fail:
return NULL;
}
/*******************************************************************
* Get host by name. *
*******************************************************************/
struct gethostbyname_state {
struct resolv_ctx *resolv_ctx;
/* Part of the query. */
const char *name;
int family;
/* These are returned by ares. The hostent struct will be freed
* when the user callback returns. */
int status;
int timeouts;
int retrying;
};
static void
struct tevent_req *
{
struct gethostbyname_state *state;
return NULL;
}
return NULL;
/* We need to have a wrapper around ares_gethostbyname(), because
* ares_gethostbyname() can in some cases call it's callback immediately.
* This would not let our caller to set a callback for req. */
return NULL;
}
return req;
}
static void
static void
{
return;
}
return;
}
} else {
}
if (status != ARES_SUCCESS) {
/* IPv4 failure. Try IPv6 */
req);
return;
}
/* Any other error indicates a server error,
* so don't bother trying again
*/
}
else {
}
}
static void
{
return;
}
return;
}
} else {
}
if (status != ARES_SUCCESS) {
}
else {
}
}
int
{
/* Fill in even in case of error as status contains the
* c-ares return code */
if (status) {
}
if (timeouts) {
}
if (hostent) {
}
return EOK;
}
static void
{
struct tevent_req);
struct gethostbyname_state);
if (!tevent_wakeup_recv(subreq)) {
return;
}
return;
}
}
/* SRV and TXT parsing is not used anywhere in the code yet, so we disable it
* for now
*/
#ifdef BUILD_TXT_SRV
/*
* A simple helper function that will take an array of struct ares_srv_reply that
* was allocated by malloc() in c-ares and copies it using talloc. The old one
* is freed and the talloc one is put into 'reply_list' instead.
*/
static int
{
/* Nothing to do, but not an error */
if (!old_list) {
return EOK;
}
/* Copy the linked list */
while (old_list) {
/* Special case for the first node */
if (!new_list) {
return ENOMEM;
}
} else {
return ENOMEM;
}
}
return ENOMEM;
}
}
/* Free the old one (uses malloc). */
/* And now put our own new_list in place. */
*reply_list = new_list;
return EOK;
}
/*******************************************************************
* Get SRV record *
*******************************************************************/
struct getsrv_state {
struct resolv_ctx *resolv_ctx;
/* the SRV query - for example _ldap._tcp.example.com */
const char *query;
/* parsed data returned by ares */
struct ares_srv_reply *reply_list;
int status;
int timeouts;
int retrying;
};
static void
struct tevent_req *
{
struct getsrv_state *state;
return NULL;
}
return NULL;
return NULL;
}
return req;
}
static void
{
int ret;
struct ares_srv_reply *reply_list;
return;
}
if (status != ARES_SUCCESS) {
goto fail;
}
if (status != ARES_SUCCESS) {
goto fail;
}
goto fail;
}
return;
fail:
}
int
{
if (status)
if (timeouts)
if (reply_list)
return EOK;
}
static void
{
struct tevent_req);
struct getsrv_state);
if (!tevent_wakeup_recv(subreq)) {
return;
}
return;
}
}
/*
* A simple helper function that will take an array of struct txt_reply that
* was allocated by malloc() in c-ares and copies it using talloc. The old one
* is freed and the talloc one is put into 'reply_list' instead.
*/
static int
{
/* Nothing to do, but not an error */
if (!old_list) {
return EOK;
}
/* Copy the linked list */
while (old_list) {
/* Special case for the first node */
if (!new_list) {
return ENOMEM;
}
} else {
return ENOMEM;
}
}
return ENOMEM;
}
}
/* And now put our own new_list in place. */
*reply_list = new_list;
return EOK;
}
/*******************************************************************
* Get TXT record *
*******************************************************************/
struct gettxt_state {
struct resolv_ctx *resolv_ctx;
/* the TXT query */
const char *query;
/* parsed data returned by ares */
struct ares_txt_reply *reply_list;
int status;
int timeouts;
int retrying;
};
static void
struct tevent_req *
{
struct gettxt_state *state;
return NULL;
}
return NULL;
return NULL;
}
return req;
}
static void
{
int ret;
struct ares_txt_reply *reply_list;
return;
}
if (status != ARES_SUCCESS) {
goto fail;
}
if (status != ARES_SUCCESS) {
goto fail;
}
goto fail;
}
return;
fail:
}
int
{
if (status)
if (timeouts)
if (reply_list)
return EOK;
}
static void
{
struct tevent_req);
struct gettxt_state);
if (!tevent_wakeup_recv(subreq)) {
return;
}
return;
}
}
#endif