Commit adff0d5f authored by Robert Pluim's avatar Robert Pluim

Refactor getaddrinfo usage

* src/process.c:
(network_lookup_address_info_1): New function, does most of the
work to call getaddrinfo.  Now checks hostname for pure-ASCII.
(Fmake_network_process): Use it.
(Fnetwork_lookup_address_info): Likewise.  Error check family
argument.
parent e89c06e8
......@@ -276,6 +276,10 @@ static int read_process_output (Lisp_Object, int);
static void create_pty (Lisp_Object);
static void exec_sentinel (Lisp_Object, Lisp_Object);
static Lisp_Object
network_lookup_address_info_1 (Lisp_Object host, const char *service,
struct addrinfo *hints, struct addrinfo **res);
/* Number of bits set in connect_wait_mask. */
static int num_pending_connects;
......@@ -4064,7 +4068,7 @@ usage: (make-network-process &rest ARGS) */)
if (!NILP (host))
{
struct addrinfo *res, *lres;
int ret;
Lisp_Object msg;
maybe_quit ();
......@@ -4073,20 +4077,11 @@ usage: (make-network-process &rest ARGS) */)
hints.ai_family = family;
hints.ai_socktype = socktype;
ret = getaddrinfo (SSDATA (host), portstring, &hints, &res);
if (ret)
#ifdef HAVE_GAI_STRERROR
{
synchronize_system_messages_locale ();
char const *str = gai_strerror (ret);
if (! NILP (Vlocale_coding_system))
str = SSDATA (code_convert_string_norecord
(build_string (str), Vlocale_coding_system, 0));
error ("%s/%s %s", SSDATA (host), portstring, str);
}
#else
error ("%s/%s getaddrinfo error %d", SSDATA (host), portstring, ret);
#endif
msg = network_lookup_address_info_1 (host, portstring, &hints, &res);
if (!EQ(msg, Qt))
{
error ("%s", SSDATA (msg));
}
for (lres = res; lres; lres = lres->ai_next)
addrinfos = Fcons (conv_addrinfo_to_lisp (lres), addrinfos);
......@@ -4535,6 +4530,37 @@ Data that is unavailable is returned as nil. */)
#endif
}
static Lisp_Object
network_lookup_address_info_1 (Lisp_Object host, const char *service,
struct addrinfo *hints, struct addrinfo **res)
{
Lisp_Object msg = Qt;
int ret;
if (SBYTES (host) != SCHARS (host))
error ("Non-ASCII hostname %s detected, please use puny-encode-domain",
SSDATA (host));
ret = getaddrinfo (SSDATA (host), service, hints, res);
if (ret)
{
if (service == NULL)
service = "0";
#ifdef HAVE_GAI_STRERROR
synchronize_system_messages_locale ();
char const *str = gai_strerror (ret);
if (! NILP (Vlocale_coding_system))
str = SSDATA (code_convert_string_norecord
(build_string (str), Vlocale_coding_system, 0));
AUTO_STRING (format, "%s/%s %s");
msg = CALLN (Fformat, format, host, build_string (service), build_string (str));
#else
AUTO_STRING (format, "%s/%s getaddrinfo error %d");
msg = CALLN (Fformat, format, host, build_string (service), make_number (ret));
#endif
}
return msg;
}
DEFUN ("network-lookup-address-info", Fnetwork_lookup_address_info,
Snetwork_lookup_address_info, 1, 2, 0,
doc: /* Look up ip address info of NAME.
......@@ -4545,42 +4571,32 @@ nil if none were found. Each address is a vector of integers. */)
(Lisp_Object name, Lisp_Object family)
{
Lisp_Object addresses = Qnil;
struct addrinfo *res, *lres;
int ret;
Lisp_Object msg = Qnil;
struct addrinfo *res, *lres;
struct addrinfo hints;
if (STRING_MULTIBYTE (name))
error ("Non-ASCII hostname \"%s\" detected, please use puny-encode-string",
SSDATA (name));
memset (&hints, 0, sizeof hints);
if (EQ (family, Qnil))
hints.ai_family = AF_UNSPEC;
if (EQ (family, Qipv4))
else if (EQ (family, Qipv4))
hints.ai_family = AF_INET;
if (EQ (family, Qipv6))
else if (EQ (family, Qipv6))
#ifdef AF_INET6
hints.ai_family = AF_INET6;
#else
/* If we don't support IPv6, querying will never work anyway */
return addresses;
#endif
else
error ("Unsupported lookup type");
hints.ai_socktype = SOCK_DGRAM;
ret = getaddrinfo (SSDATA (name), NULL, &hints, &res);
if (ret)
#ifdef HAVE_GAI_STRERROR
msg = network_lookup_address_info_1 (name, NULL, &hints, &res);
if (!EQ(msg, Qt))
{
synchronize_system_messages_locale ();
char const *str = gai_strerror (ret);
if (! NILP (Vlocale_coding_system))
str = SSDATA (code_convert_string_norecord
(build_string (str), Vlocale_coding_system, 0));
message ("\"%s\" \"%s\"", SSDATA (name), str);
message ("%s", SSDATA(msg));
}
#else
message ("%s network-lookup-address-info error %d", SSDATA (name), ret);
#endif
else
{
for (lres = res; lres; lres = lres->ai_next)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment