Function: network-lookup-address-info

network-lookup-address-info is a function defined in process.c.

Signature

(network-lookup-address-info NAME &optional FAMILY HINT)

Documentation

Look up Internet Protocol (IP) address info of NAME.

NAME must be an ASCII-only string. For looking up internationalized hostnames, use puny-encode-domain on the string first.

Optional argument FAMILY controls whether to look up IPv4 or IPv6 addresses. The default of nil means both, symbol ipv4 means IPv4 only, symbol ipv6 means IPv6 only. Optional argument HINTS allows specifying the hints passed to the underlying library call. The only supported value is numeric, which means treat NAME as a numeric IP address. This also suppresses DNS traffic. Return a list of addresses, or nil if none were found. Each address is a vector of integers, as per the description of ADDRESS in make-network-process. In case of error log the error message returned from the lookup.

View in manual

Probably introduced at or before Emacs version 27.1.

Source Code

// Defined in /usr/src/emacs/src/process.c
{
  Lisp_Object addresses = Qnil;
  Lisp_Object msg = Qnil;

  struct addrinfo *res, *lres;
  struct addrinfo hints;

  CHECK_STRING (name);

  memset (&hints, 0, sizeof hints);
  if (NILP (family))
    hints.ai_family = AF_UNSPEC;
  else if (EQ (family, Qipv4))
    hints.ai_family = AF_INET;
#ifdef AF_INET6
  else if (EQ (family, Qipv6))
    hints.ai_family = AF_INET6;
#endif
  else
    error ("Unsupported family");
  hints.ai_socktype = SOCK_DGRAM;

  if (EQ (hint, Qnumeric))
    hints.ai_flags = AI_NUMERICHOST;
  else if (!NILP (hint))
    error ("Unsupported hints value");

  msg = network_lookup_address_info_1 (name, NULL, &hints, &res);
  if (!EQ (msg, Qt))
    message ("%s", SSDATA(msg));
  else
    {
      for (lres = res; lres; lres = lres->ai_next)
        {
#ifndef AF_INET6
          if (lres->ai_family != AF_INET)
            continue;
#endif
          addresses = Fcons (conv_sockaddr_to_lisp (lres->ai_addr,
                                                    lres->ai_addrlen),
                             addresses);
        }
      addresses = Fnreverse (addresses);

      freeaddrinfo (res);
    }
  return addresses;
}