⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ares_init.c

📁 rtlinux-3.2源码
💻 C
字号:
/* Copyright 1998 by the Massachusetts Institute of Technology. * * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without * fee is hereby granted, provided that the above copyright * notice appear in all copies and that both that copyright * notice and this permission notice appear in supporting * documentation, and that the name of M.I.T. not be used in * advertising or publicity pertaining to distribution of the * software without specific, written prior permission. * M.I.T. makes no representations about the suitability of * this software for any purpose.  It is provided "as is" * without express or implied warranty. * * CHANGELOG: this file has been modified by Sergio Perez Alca駃z <serpeal@upvnet.upv.es>  *            Departamento de Inform醫ica de Sistemas y Computadores           *            Universidad Polit閏nica de Valencia                              *            Valencia (Spain)     *            Date: April 2003                                           * */#include "ares_private.h"#define htons HTONS#define htonl HTONLextern int gettimeofday(struct timeval *tv, struct timezone *tz);static int init_by_options(ares_channel channel, struct ares_options *options,			   int optmask);static int init_by_defaults(ares_channel channel);int ares_init(ares_channel *channelptr){  return ares_init_options(channelptr, NULL, 0);}int ares_init_options(ares_channel *channelptr, struct ares_options *options,		      int optmask){  ares_channel channel;  int i, status;  struct server_state *server;  channel = malloc(sizeof(struct ares_channeldata));  if (!channel)    return ARES_ENOMEM;  /* Set everything to distinguished values so we know they haven't   * been set yet.   */  channel->flags = -1;  channel->timeout = -1;  channel->tries = -1;  channel->ndots = -1;  channel->udp_port = -1;  channel->tcp_port = -1;  channel->nservers = -1;  channel->ndomains = -1;  channel->nsort = -1;  channel->lookups = NULL;  /* Initialize configuration by each of the four sources, from highest   * precedence to lowest.   */  status = init_by_options(channel, options, optmask);  if (status == ARES_SUCCESS)    status = init_by_defaults(channel);  if (status != ARES_SUCCESS)    {      /* Something failed; clean up memory we may have allocated. */      if (channel->nservers != -1)	free(channel->servers);      if (channel->ndomains != -1)	{	  for (i = 0; i < channel->ndomains; i++)	    free(channel->domains[i]);	  free(channel->domains);	}      if (channel->nsort != -1)	free(channel->sortlist);      free(channel->lookups);      free(channel);      return status;    }  /* Trim to one server if ARES_FLAG_PRIMARY is set. */  if ((channel->flags & ARES_FLAG_PRIMARY) && channel->nservers > 1)    channel->nservers = 1;  /* Initialize server states. */  for (i = 0; i < channel->nservers; i++)    {      server = &channel->servers[i];      server->udp_socket = -1;      server->tcp_socket = -1;      server->tcp_lenbuf_pos = 0;      server->tcp_buffer = NULL;      server->qhead = NULL;      server->qtail = NULL;    }  /* Choose a somewhat random query ID.  The main point is to avoid   * collisions with stale queries.  An attacker trying to spoof a DNS   * answer also has to guess the query ID, but it's only a 16-bit   * field, so there's not much to be done about that.   *///  channel->next_id = (RetrieveClock() ^ os_current_process()) & 0xffff;  {    struct timeval time;    gettimeofday(&time,NULL);    channel->next_id = (time.tv_usec ^ time.tv_sec) & 0xffff;  }  channel->queries = NULL;  *channelptr = channel;  return ARES_SUCCESS;}static int init_by_options(ares_channel channel, struct ares_options *options,			   int optmask){  int i;  /* Easy stuff. */  if ((optmask & ARES_OPT_FLAGS) && channel->flags == -1)    channel->flags = options->flags;  if ((optmask & ARES_OPT_TIMEOUT) && channel->timeout == -1)    channel->timeout = options->timeout;  if ((optmask & ARES_OPT_TRIES) && channel->tries == -1)    channel->tries = options->tries;  if ((optmask & ARES_OPT_NDOTS) && channel->ndots == -1)    channel->ndots = options->ndots;  if ((optmask & ARES_OPT_UDP_PORT) && channel->udp_port == -1)    channel->udp_port = options->udp_port;  if ((optmask & ARES_OPT_TCP_PORT) && channel->tcp_port == -1)    channel->tcp_port = options->tcp_port;  /* Copy the servers, if given. */  if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)    {      channel->servers =	malloc(options->nservers * sizeof(struct server_state));      if (!channel->servers && options->nservers != 0)	return ARES_ENOMEM;      for (i = 0; i < options->nservers; i++)	channel->servers[i].addr = options->servers[i];      channel->nservers = options->nservers;    }  /* Copy the domains, if given.  Keep channel->ndomains consistent so   * we can clean up in case of error.   */  if ((optmask & ARES_OPT_DOMAINS) && channel->ndomains == -1)    {      channel->domains = malloc(options->ndomains * sizeof(char *));      if (!channel->domains && options->ndomains != 0)	return ARES_ENOMEM;      for (i = 0; i < options->ndomains; i++)	{	  channel->ndomains = i;	  channel->domains[i] = strdup(options->domains[i]);	  if (!channel->domains[i])	    return ARES_ENOMEM;	}      channel->ndomains = options->ndomains;    }  /* Set lookups, if given. */  if ((optmask & ARES_OPT_LOOKUPS) && !channel->lookups)    {      channel->lookups = strdup(options->lookups);      if (!channel->lookups)	return ARES_ENOMEM;    }  return ARES_SUCCESS;}static int init_by_defaults(ares_channel channel){  if (channel->flags == -1)    channel->flags = 0;  if (channel->timeout == -1)    channel->timeout = DEFAULT_TIMEOUT;  if (channel->tries == -1)    channel->tries = DEFAULT_TRIES;  if (channel->ndots == -1)    channel->ndots = 1;  if (channel->udp_port == -1)    channel->udp_port = htons(NAMESERVER_PORT);  if (channel->tcp_port == -1)    channel->tcp_port = htons(NAMESERVER_PORT);  if (channel->nservers == -1)    {      /* If nobody specified servers, try a local named. */      channel->servers = malloc(sizeof(struct server_state));      if (!channel->servers)	return ARES_ENOMEM;      channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);      channel->nservers = 1;    }  if (channel->ndomains == -1)    {      channel->domains = malloc(0);      channel->ndomains = 0;    }  if (channel->nsort == -1)    {      channel->sortlist = NULL;      channel->nsort = 0;    }  if (!channel->lookups)    {      channel->lookups = strdup("bf");      if (!channel->lookups)	return ARES_ENOMEM;    }  return ARES_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -