Пример #1
0
Файл: host.c Проект: Red54/axtu
struct address_list *
lookup_host (const char *host, int flags)
{
  struct address_list *al;
  int silent = flags & LH_SILENT;
  int use_cache;
  int numeric_address = 0;
  double timeout = opt.dns_timeout;

#ifndef ENABLE_IPV6
  /* If we're not using getaddrinfo, first check if HOST specifies a
     numeric IPv4 address.  Some implementations of gethostbyname
     (e.g. the Ultrix one and possibly Winsock) don't accept
     dotted-decimal IPv4 addresses.  */
  {
    uint32_t addr_ipv4 = (uint32_t)inet_addr (host);
    if (addr_ipv4 != (uint32_t) -1)
      {
	/* No need to cache host->addr relation, just return the
	   address.  */
	char *vec[2];
	vec[0] = (char *)&addr_ipv4;
	vec[1] = NULL;
	return address_list_from_ipv4_addresses (vec);
      }
  }
#else  /* ENABLE_IPV6 */
  /* If we're using getaddrinfo, at least check whether the address is
     already numeric, in which case there is no need to print the
     "Resolving..." output.  (This comes at no additional cost since
     the is_valid_ipv*_address are already required for
     url_parse.)  */
  {
    const char *end = host + strlen (host);
    if (is_valid_ipv4_address (host, end) || is_valid_ipv6_address (host, end))
      numeric_address = 1;
  }
#endif

  /* Cache is normally on, but can be turned off with --no-dns-cache.
     Don't cache passive lookups under IPv6.  */
  use_cache = opt.dns_cache;
#ifdef ENABLE_IPV6
  if ((flags & LH_BIND) || numeric_address)
    use_cache = 0;
#endif

  /* Try to find the host in the cache so we don't need to talk to the
     resolver.  If LH_REFRESH is requested, remove HOST from the cache
     instead.  */
  if (use_cache)
    {
      if (!(flags & LH_REFRESH))
	{
	  al = cache_query (host);
	  if (al)
	    return al;
	}
      else
	cache_remove (host);
    }

  /* No luck with the cache; resolve HOST. */

  if (!silent && !numeric_address)
    logprintf (LOG_VERBOSE, _("Resolving %s... "), escnonprint (host));

#ifdef ENABLE_IPV6
  {
    int err;
    struct addrinfo hints, *res;

    xzero (hints);
    hints.ai_socktype = SOCK_STREAM;
    if (opt.ipv4_only)
      hints.ai_family = AF_INET;
    else if (opt.ipv6_only)
      hints.ai_family = AF_INET6;
    else
      /* We tried using AI_ADDRCONFIG, but removed it because: it
	 misinterprets IPv6 loopbacks, it is broken on AIX 5.1, and
	 it's unneeded since we sort the addresses anyway.  */
	hints.ai_family = AF_UNSPEC;

    if (flags & LH_BIND)
      hints.ai_flags |= AI_PASSIVE;

#ifdef AI_NUMERICHOST
    if (numeric_address)
      {
	/* Where available, the AI_NUMERICHOST hint can prevent costly
	   access to DNS servers.  */
	hints.ai_flags |= AI_NUMERICHOST;
	timeout = 0;		/* no timeout needed when "resolving"
				   numeric hosts -- avoid setting up
				   signal handlers and such. */
      }
#endif

    err = getaddrinfo_with_timeout (host, NULL, &hints, &res, timeout);
    if (err != 0 || res == NULL)
      {
	if (!silent)
	  logprintf (LOG_VERBOSE, _("failed: %s.\n"),
		     err != EAI_SYSTEM ? gai_strerror (err) : strerror (errno));
	return NULL;
      }
    al = address_list_from_addrinfo (res);
    freeaddrinfo (res);
    if (!al)
      {
	logprintf (LOG_VERBOSE,
		   _("failed: No IPv4/IPv6 addresses for host.\n"));
	return NULL;
      }

    /* Reorder addresses so that IPv4 ones (or IPv6 ones, as per
       --prefer-family) come first.  Sorting is stable so the order of
       the addresses with the same family is undisturbed.  */
    if (al->count > 1 && opt.prefer_family != prefer_none)
      stable_sort (al->addresses, al->count, sizeof (ip_address),
		   opt.prefer_family == prefer_ipv4
		   ? cmp_prefer_ipv4 : cmp_prefer_ipv6);
  }
#else  /* not ENABLE_IPV6 */
  {
    struct hostent *hptr = gethostbyname_with_timeout (host, timeout);
    if (!hptr)
      {
	if (!silent)
	  {
	    if (errno != ETIMEDOUT)
	      logprintf (LOG_VERBOSE, _("failed: %s.\n"),
			 host_errstr (h_errno));
	    else
	      logputs (LOG_VERBOSE, _("failed: timed out.\n"));
	  }
	return NULL;
      }
    /* Do older systems have h_addr_list?  */
    al = address_list_from_ipv4_addresses (hptr->h_addr_list);
  }
#endif /* not ENABLE_IPV6 */

  /* Print the addresses determined by DNS lookup, but no more than
     three.  */
  if (!silent && !numeric_address)
    {
      int i;
      int printmax = al->count <= 3 ? al->count : 3;
      for (i = 0; i < printmax; i++)
	{
	  logprintf (LOG_VERBOSE, "%s",
		     pretty_print_address (al->addresses + i));
	  if (i < printmax - 1)
	    logputs (LOG_VERBOSE, ", ");
	}
      if (printmax != al->count)
	logputs (LOG_VERBOSE, ", ...");
      logputs (LOG_VERBOSE, "\n");
    }

  /* Cache the lookup information. */
  if (use_cache)
    cache_store (host, al);

  return al;
}
Пример #2
0
mm_cache_t *cache_query_icon ( char *id ) {
  return ( cache_query ( id, g_icon_cache ) );
}
Пример #3
0
mm_cache_t *cache_query_preview ( char *id ) {
  return ( cache_query ( id, g_preview_cache ) );
}
Пример #4
0
u_int32_t
FF_query(void *c, dsrecord *pattern, dsrecord **list)
{
	agent_private *ap;
	u_int32_t cat;
	dsattribute *a;
	dsdata *k, *k4, *k6;
	dsrecord *lastrec;
	char *fname;
	FILE *fp;
	char *line;
	dsrecord *item = NULL;
	dsrecord *host = NULL;
	char *fpath;
	long ts;
	int match, single_item, stamp;
	ff_cache_t *cache;
	struct stat sb;

	if (c == NULL) return 1;
	if (pattern == NULL) return 1;
	if (list == NULL) return 1;

	*list = NULL;
	lastrec = NULL;
	single_item = 0;
	stamp = 0;

	ap = (agent_private *)c;

	k = cstring_to_dsdata(CATEGORY_KEY);
	a = dsrecord_attribute(pattern, k, SELECT_META_ATTRIBUTE);
	dsdata_release(k);

	if (a == NULL) return 1;
	if (a->count == 0) return 1;

	cat = atoi(dsdata_to_cstring(a->value[0]));
	dsattribute_release(a);

	fname = categoryFilename[cat];
	if (fname == NULL) return 1;

	k = cstring_to_dsdata(STAMP_KEY);
	a = dsrecord_attribute(pattern, k, SELECT_META_ATTRIBUTE);
	dsdata_release(k);
	if (a != NULL)
	{
		dsrecord_remove_attribute(pattern, a, SELECT_META_ATTRIBUTE);
		stamp = 1;
	}
	dsattribute_release(a);

	k = cstring_to_dsdata(SINGLE_KEY);
	a = dsrecord_attribute(pattern, k, SELECT_META_ATTRIBUTE);
	dsdata_release(k);
	if (a != NULL)
	{
		dsattribute_release(a);
		single_item = 1;
	}

	asprintf(&fpath, "%s/%s", ap->dir, fname);

	ts = 0;
	cache = NULL;
	if (ap->flags == 0) cache = cache_for_category(cat);
	if (cache != NULL)
	{
		ts = cache->modtime;
	}
	else
	{
		memset(&sb, 0, sizeof(struct stat));
		if (stat(fpath, &sb) < 0) ts = 0;
		else ts = sb.st_mtime;
	}

	if (stamp == 1)
	{
		item = dsrecord_new();
		add_validation(item, fpath, ts);
		*list = item;
		free(fpath);
		return 0;
	}

	if (cache != NULL)
	{
		free(fpath);
		return cache_query(cache, cat, single_item, pattern, list);
	}

	fp = fopen(fpath, "r");
	if (fp == NULL)
	{
		free(fpath);
		return 1;
	}

	/* bootptab entries start after a "%%" line */
	if (cat == LUCategoryBootp)
	{
		while (NULL != (line = getLineFromFile(fp)))
		{
			if (!strncmp(line, "%%", 2)) break;
			freeString(line);
			line = NULL;
		}
		if (line == NULL)
		{
			fclose(fp);
			free(fpath);
			return 0;
		}

		freeString(line);
		line = NULL;
	}
		
	while (NULL != (line = getLineFromFile(fp)))
	{
		if (line[0] == '#') 
		{
			freeString(line);
			line = NULL;
			continue;
		}

		item = parse(line, cat);

		freeString(line);
		line = NULL;

		if (item == NULL) continue;

		match = dsrecord_match_select(item, pattern, SELECT_ATTRIBUTE);
		if (match == 1)
		{
			add_validation(item, fpath, ts);

			if (*list == NULL) *list = dsrecord_retain(item);
			else lastrec->next = dsrecord_retain(item);

			lastrec = item;

			if (cat == LUCategoryHost)
			{
			}
			else if (single_item == 1)
			{
				dsrecord_release(item);
				break;
			}
		}

		dsrecord_release(item);
	}

	free(fpath);
	fclose(fp);

	if ((cat == LUCategoryHost) && (single_item == 1))
	{
		if ((*list) == NULL) return 0;
		if ((*list)->next == NULL) return 0;

		k = cstring_to_dsdata("name");
		k4 = cstring_to_dsdata("ip_address");
		k6 = cstring_to_dsdata("ipv6_address");
		host = *list;

		for (item = host->next; item != NULL; item = item->next)
		{
			a = dsrecord_attribute(item, k, SELECT_ATTRIBUTE);
			dsrecord_merge_attribute(host, a, SELECT_ATTRIBUTE);
			dsattribute_release(a);

			a = dsrecord_attribute(item, k4, SELECT_ATTRIBUTE);
			dsrecord_merge_attribute(host, a, SELECT_ATTRIBUTE);
			dsattribute_release(a);

			a = dsrecord_attribute(item, k6, SELECT_ATTRIBUTE);
			dsrecord_merge_attribute(host, a, SELECT_ATTRIBUTE);
			dsattribute_release(a);
		}

		dsdata_release(k);
		dsdata_release(k4);
		dsdata_release(k6);

		dsrecord_release(host->next);
		host->next = NULL;
	}

	return 0;
}