1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-07-26 16:45:12 -04:00

[network] -bind-address-ipv6 <ipv6> to bind to IPv6 address

This commit is contained in:
Witold Filipczyk 2020-05-05 21:39:54 +02:00
parent 35e32f5acb
commit 5188b8ab84
2 changed files with 26 additions and 1 deletions

View File

@ -781,6 +781,10 @@ union option_info cmdline_options_info[] = {
"bind-address", 0, "",
N_("Use a specific local IP address")),
INIT_OPT_STRING("", N_("Use a specific local IPv6 address"),
"bind-address-ipv6", 0, "",
N_("Use a specific local IPv6 address")),
INIT_OPT_COMMAND("", NULL, "confdir", OPT_HIDDEN, redir_cmd, NULL),
INIT_OPT_STRING("", N_("Name of directory with configuration file"),

View File

@ -555,7 +555,9 @@ connect_socket(struct socket *csocket, struct connection_state state)
* XXX: Unify with @local_only handling? --pasky */
int silent_fail = 0;
unsigned char *bind_address = get_cmd_opt_str("bind-address");
unsigned char *bind_address_ipv6 = get_cmd_opt_str("bind-address-ipv6");
int to_bind = (bind_address && *bind_address);
int to_bind_ipv6 = (bind_address_ipv6 && *bind_address_ipv6);
csocket->ops->set_state(csocket, state);
@ -628,7 +630,7 @@ connect_socket(struct socket *csocket, struct connection_state state)
}
#ifdef HAVE_INET_PTON
if (to_bind) {
if (pf == PF_INET && to_bind) {
struct sockaddr_in sa;
int res;
@ -638,6 +640,23 @@ connect_socket(struct socket *csocket, struct connection_state state)
sa.sin_port = htons(0);
res = bind(sock, (struct sockaddr *)(void *)&sa, sizeof sa);
if (res < 0) {
if (errno && !saved_errno) saved_errno = errno;
close(sock);
continue;
}
}
#ifdef CONFIG_IPV6
if (pf == PF_INET6 && to_bind_ipv6) {
struct sockaddr_in sa;
int res;
memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET6;
inet_pton(AF_INET6, bind_address_ipv6, &(sa.sin_addr));
sa.sin_port = htons(0);
res = bind(sock, (struct sockaddr *)(void *)&sa, sizeof sa);
if (res < 0) {
if (errno && !saved_errno) saved_errno = errno;
close(sock);
@ -645,6 +664,8 @@ connect_socket(struct socket *csocket, struct connection_state state)
}
}
#endif
#endif
csocket->fd = sock;