From 063277c5ecd82551b2bf1409d1583edc7c0fcaad Mon Sep 17 00:00:00 2001 From: Robert Pluim Date: Thu, 7 Nov 2019 16:16:39 +0100 Subject: [PATCH] Ensure building and running on non-IPv6 capable hosts works * src/process.c (Fmake_network_process) [AF_INET6]: Only build ::1 localhost when IPv6 is supported. (Fnetwork_lookup_address_info) [AF_INET6]: Move check for Qipv6 inside ifdef, since its definition depends on AF_INET6. Don't return IPv6 addresses when they're not supported. * test/src/process-tests.el (lookup-family-specification, lookup-google): Only do IPv6 lookup if IPv6 is supported. --- src/process.c | 24 ++++++++++++++++-------- test/src/process-tests.el | 11 ++++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/process.c b/src/process.c index 1f959e39607..8aa4811f7ed 100644 --- a/src/process.c +++ b/src/process.c @@ -4015,9 +4015,11 @@ usage: (make-network-process &rest ARGS) */) if (family != AF_LOCAL) #endif { +#ifdef AF_INET6 if (family == AF_INET6) host = build_string ("::1"); else +#endif host = build_string ("127.0.0.1"); } } @@ -4027,9 +4029,11 @@ usage: (make-network-process &rest ARGS) */) { /* Depending on setup, "localhost" may map to different IPv4 and/or IPv6 addresses, so it's better to be explicit (Bug#6781). */ +#ifdef AF_INET6 if (family == AF_INET6) host = build_string ("::1"); else +#endif host = build_string ("127.0.0.1"); } CHECK_STRING (host); @@ -4622,7 +4626,8 @@ DEFUN ("network-lookup-address-info", Fnetwork_lookup_address_info, Optional parameter 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. Returns a list of addresses, or -nil if none were found. Each address is a vector of integers. */) +nil if none were found. Each address is a vector of integers, as per +the description of ADDRESS in `make-network-process'. */) (Lisp_Object name, Lisp_Object family) { Lisp_Object addresses = Qnil; @@ -4636,12 +4641,9 @@ nil if none were found. Each address is a vector of integers. */) hints.ai_family = AF_UNSPEC; else if (EQ (family, Qipv4)) hints.ai_family = AF_INET; - else if (EQ (family, Qipv6)) #ifdef AF_INET6 + else if (EQ (family, Qipv6)) 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"); @@ -4653,9 +4655,15 @@ nil if none were found. Each address is a vector of integers. */) else { for (lres = res; lres; lres = lres->ai_next) - addresses = Fcons (conv_sockaddr_to_lisp (lres->ai_addr, - lres->ai_addrlen), - addresses); + { +#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); diff --git a/test/src/process-tests.el b/test/src/process-tests.el index 158c036aaa7..f065d393d2a 100644 --- a/test/src/process-tests.el +++ b/test/src/process-tests.el @@ -337,7 +337,8 @@ See Bug#30460." (skip-unless (not (getenv "EMACS_HYDRA_CI"))) (should-error (network-lookup-address-info "google.com" 'both)) (should (network-lookup-address-info "google.com" 'ipv4)) - (should (network-lookup-address-info "google.com" 'ipv6))) + (when (featurep 'make-network-process '(:family ipv6)) + (should (network-lookup-address-info "google.com" 'ipv6)))) (ert-deftest lookup-unicode-domains () "Unicode domains should fail" @@ -354,11 +355,11 @@ See Bug#30460." "Check that we can look up google IP addresses" (skip-unless (not (getenv "EMACS_HYDRA_CI"))) (let ((addresses-both (network-lookup-address-info "google.com")) - (addresses-v4 (network-lookup-address-info "google.com" 'ipv4)) - (addresses-v6 (network-lookup-address-info "google.com" 'ipv6))) + (addresses-v4 (network-lookup-address-info "google.com" 'ipv4))) (should addresses-both) - (should addresses-v4) - (should addresses-v6))) + (should addresses-v4)) + (when (featurep 'make-network-process '(:family ipv6)) + (should (network-lookup-address-info "google.com" 'ipv6)))) (ert-deftest non-existent-lookup-failure () (skip-unless (not (getenv "EMACS_HYDRA_CI"))) -- 2.39.5