1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[network] -bind-address <ipv4> cmdline option to bind to given IP address

This commit is contained in:
Witold Filipczyk 2020-05-05 21:18:29 +02:00
parent 08d200f42c
commit 35e32f5acb
2 changed files with 26 additions and 0 deletions

View File

@ -777,6 +777,10 @@ union option_info cmdline_options_info[] = {
"windows. The ID maps to information that will be used when "
"creating the new instance. You don't want to use it.")),
INIT_OPT_STRING("", N_("Use a specific local IP address"),
"bind-address", 0, "",
N_("Use a specific local IP address")),
INIT_OPT_COMMAND("", NULL, "confdir", OPT_HIDDEN, redir_cmd, NULL),
INIT_OPT_STRING("", N_("Name of directory with configuration file"),

View File

@ -554,6 +554,8 @@ connect_socket(struct socket *csocket, struct connection_state state)
* about such a connection attempt.
* XXX: Unify with @local_only handling? --pasky */
int silent_fail = 0;
unsigned char *bind_address = get_cmd_opt_str("bind-address");
int to_bind = (bind_address && *bind_address);
csocket->ops->set_state(csocket, state);
@ -624,6 +626,26 @@ connect_socket(struct socket *csocket, struct connection_state state)
close(sock);
continue;
}
#ifdef HAVE_INET_PTON
if (to_bind) {
struct sockaddr_in sa;
int res;
memset(&sa, 0, sizeof sa);
sa.sin_family = AF_INET;
inet_pton(AF_INET, bind_address, &(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);
continue;
}
}
#endif
csocket->fd = sock;
#ifdef CONFIG_IPV6