diff options
| author | Robert Pluim | 2018-07-17 13:08:12 +0200 |
|---|---|---|
| committer | Robert Pluim | 2018-07-17 13:08:12 +0200 |
| commit | adff0d5f75d4b3a74816527edb9ebe997c2089f3 (patch) | |
| tree | 677c7644375cfcb825d2540b2e33970cb4ba46f9 /src/process.c | |
| parent | e89c06e8cea429620bc2cf4a98b9b741861b811a (diff) | |
| download | emacs-adff0d5f75d4b3a74816527edb9ebe997c2089f3.tar.gz emacs-adff0d5f75d4b3a74816527edb9ebe997c2089f3.zip | |
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.
Diffstat (limited to 'src/process.c')
| -rw-r--r-- | src/process.c | 84 |
1 files changed, 50 insertions, 34 deletions
diff --git a/src/process.c b/src/process.c index 2025398c22d..25f02afb297 100644 --- a/src/process.c +++ b/src/process.c | |||
| @@ -276,6 +276,10 @@ static int read_process_output (Lisp_Object, int); | |||
| 276 | static void create_pty (Lisp_Object); | 276 | static void create_pty (Lisp_Object); |
| 277 | static void exec_sentinel (Lisp_Object, Lisp_Object); | 277 | static void exec_sentinel (Lisp_Object, Lisp_Object); |
| 278 | 278 | ||
| 279 | static Lisp_Object | ||
| 280 | network_lookup_address_info_1 (Lisp_Object host, const char *service, | ||
| 281 | struct addrinfo *hints, struct addrinfo **res); | ||
| 282 | |||
| 279 | /* Number of bits set in connect_wait_mask. */ | 283 | /* Number of bits set in connect_wait_mask. */ |
| 280 | static int num_pending_connects; | 284 | static int num_pending_connects; |
| 281 | 285 | ||
| @@ -4064,7 +4068,7 @@ usage: (make-network-process &rest ARGS) */) | |||
| 4064 | if (!NILP (host)) | 4068 | if (!NILP (host)) |
| 4065 | { | 4069 | { |
| 4066 | struct addrinfo *res, *lres; | 4070 | struct addrinfo *res, *lres; |
| 4067 | int ret; | 4071 | Lisp_Object msg; |
| 4068 | 4072 | ||
| 4069 | maybe_quit (); | 4073 | maybe_quit (); |
| 4070 | 4074 | ||
| @@ -4073,20 +4077,11 @@ usage: (make-network-process &rest ARGS) */) | |||
| 4073 | hints.ai_family = family; | 4077 | hints.ai_family = family; |
| 4074 | hints.ai_socktype = socktype; | 4078 | hints.ai_socktype = socktype; |
| 4075 | 4079 | ||
| 4076 | ret = getaddrinfo (SSDATA (host), portstring, &hints, &res); | 4080 | msg = network_lookup_address_info_1 (host, portstring, &hints, &res); |
| 4077 | if (ret) | 4081 | if (!EQ(msg, Qt)) |
| 4078 | #ifdef HAVE_GAI_STRERROR | 4082 | { |
| 4079 | { | 4083 | error ("%s", SSDATA (msg)); |
| 4080 | synchronize_system_messages_locale (); | 4084 | } |
| 4081 | char const *str = gai_strerror (ret); | ||
| 4082 | if (! NILP (Vlocale_coding_system)) | ||
| 4083 | str = SSDATA (code_convert_string_norecord | ||
| 4084 | (build_string (str), Vlocale_coding_system, 0)); | ||
| 4085 | error ("%s/%s %s", SSDATA (host), portstring, str); | ||
| 4086 | } | ||
| 4087 | #else | ||
| 4088 | error ("%s/%s getaddrinfo error %d", SSDATA (host), portstring, ret); | ||
| 4089 | #endif | ||
| 4090 | 4085 | ||
| 4091 | for (lres = res; lres; lres = lres->ai_next) | 4086 | for (lres = res; lres; lres = lres->ai_next) |
| 4092 | addrinfos = Fcons (conv_addrinfo_to_lisp (lres), addrinfos); | 4087 | addrinfos = Fcons (conv_addrinfo_to_lisp (lres), addrinfos); |
| @@ -4535,6 +4530,37 @@ Data that is unavailable is returned as nil. */) | |||
| 4535 | #endif | 4530 | #endif |
| 4536 | } | 4531 | } |
| 4537 | 4532 | ||
| 4533 | static Lisp_Object | ||
| 4534 | network_lookup_address_info_1 (Lisp_Object host, const char *service, | ||
| 4535 | struct addrinfo *hints, struct addrinfo **res) | ||
| 4536 | { | ||
| 4537 | Lisp_Object msg = Qt; | ||
| 4538 | int ret; | ||
| 4539 | |||
| 4540 | if (SBYTES (host) != SCHARS (host)) | ||
| 4541 | error ("Non-ASCII hostname %s detected, please use puny-encode-domain", | ||
| 4542 | SSDATA (host)); | ||
| 4543 | ret = getaddrinfo (SSDATA (host), service, hints, res); | ||
| 4544 | if (ret) | ||
| 4545 | { | ||
| 4546 | if (service == NULL) | ||
| 4547 | service = "0"; | ||
| 4548 | #ifdef HAVE_GAI_STRERROR | ||
| 4549 | synchronize_system_messages_locale (); | ||
| 4550 | char const *str = gai_strerror (ret); | ||
| 4551 | if (! NILP (Vlocale_coding_system)) | ||
| 4552 | str = SSDATA (code_convert_string_norecord | ||
| 4553 | (build_string (str), Vlocale_coding_system, 0)); | ||
| 4554 | AUTO_STRING (format, "%s/%s %s"); | ||
| 4555 | msg = CALLN (Fformat, format, host, build_string (service), build_string (str)); | ||
| 4556 | #else | ||
| 4557 | AUTO_STRING (format, "%s/%s getaddrinfo error %d"); | ||
| 4558 | msg = CALLN (Fformat, format, host, build_string (service), make_number (ret)); | ||
| 4559 | #endif | ||
| 4560 | } | ||
| 4561 | return msg; | ||
| 4562 | } | ||
| 4563 | |||
| 4538 | DEFUN ("network-lookup-address-info", Fnetwork_lookup_address_info, | 4564 | DEFUN ("network-lookup-address-info", Fnetwork_lookup_address_info, |
| 4539 | Snetwork_lookup_address_info, 1, 2, 0, | 4565 | Snetwork_lookup_address_info, 1, 2, 0, |
| 4540 | doc: /* Look up ip address info of NAME. | 4566 | doc: /* Look up ip address info of NAME. |
| @@ -4545,42 +4571,32 @@ nil if none were found. Each address is a vector of integers. */) | |||
| 4545 | (Lisp_Object name, Lisp_Object family) | 4571 | (Lisp_Object name, Lisp_Object family) |
| 4546 | { | 4572 | { |
| 4547 | Lisp_Object addresses = Qnil; | 4573 | Lisp_Object addresses = Qnil; |
| 4548 | struct addrinfo *res, *lres; | 4574 | Lisp_Object msg = Qnil; |
| 4549 | int ret; | ||
| 4550 | 4575 | ||
| 4576 | struct addrinfo *res, *lres; | ||
| 4551 | struct addrinfo hints; | 4577 | struct addrinfo hints; |
| 4552 | 4578 | ||
| 4553 | if (STRING_MULTIBYTE (name)) | ||
| 4554 | error ("Non-ASCII hostname \"%s\" detected, please use puny-encode-string", | ||
| 4555 | SSDATA (name)); | ||
| 4556 | memset (&hints, 0, sizeof hints); | 4579 | memset (&hints, 0, sizeof hints); |
| 4557 | if (EQ (family, Qnil)) | 4580 | if (EQ (family, Qnil)) |
| 4558 | hints.ai_family = AF_UNSPEC; | 4581 | hints.ai_family = AF_UNSPEC; |
| 4559 | if (EQ (family, Qipv4)) | 4582 | else if (EQ (family, Qipv4)) |
| 4560 | hints.ai_family = AF_INET; | 4583 | hints.ai_family = AF_INET; |
| 4561 | if (EQ (family, Qipv6)) | 4584 | else if (EQ (family, Qipv6)) |
| 4562 | #ifdef AF_INET6 | 4585 | #ifdef AF_INET6 |
| 4563 | hints.ai_family = AF_INET6; | 4586 | hints.ai_family = AF_INET6; |
| 4564 | #else | 4587 | #else |
| 4565 | /* If we don't support IPv6, querying will never work anyway */ | 4588 | /* If we don't support IPv6, querying will never work anyway */ |
| 4566 | return addresses; | 4589 | return addresses; |
| 4567 | #endif | 4590 | #endif |
| 4591 | else | ||
| 4592 | error ("Unsupported lookup type"); | ||
| 4568 | hints.ai_socktype = SOCK_DGRAM; | 4593 | hints.ai_socktype = SOCK_DGRAM; |
| 4569 | 4594 | ||
| 4570 | ret = getaddrinfo (SSDATA (name), NULL, &hints, &res); | 4595 | msg = network_lookup_address_info_1 (name, NULL, &hints, &res); |
| 4571 | if (ret) | 4596 | if (!EQ(msg, Qt)) |
| 4572 | #ifdef HAVE_GAI_STRERROR | ||
| 4573 | { | 4597 | { |
| 4574 | synchronize_system_messages_locale (); | 4598 | message ("%s", SSDATA(msg)); |
| 4575 | char const *str = gai_strerror (ret); | ||
| 4576 | if (! NILP (Vlocale_coding_system)) | ||
| 4577 | str = SSDATA (code_convert_string_norecord | ||
| 4578 | (build_string (str), Vlocale_coding_system, 0)); | ||
| 4579 | message ("\"%s\" \"%s\"", SSDATA (name), str); | ||
| 4580 | } | 4599 | } |
| 4581 | #else | ||
| 4582 | message ("%s network-lookup-address-info error %d", SSDATA (name), ret); | ||
| 4583 | #endif | ||
| 4584 | else | 4600 | else |
| 4585 | { | 4601 | { |
| 4586 | for (lres = res; lres; lres = lres->ai_next) | 4602 | for (lres = res; lres; lres = lres->ai_next) |