diff --git a/lib/client-handshake.c b/lib/client-handshake.c index fc8ffb76..6bce8fe5 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -7,6 +7,7 @@ struct libwebsocket *libwebsocket_client_connect_2( struct pollfd pfd; struct hostent *server_hostent; struct sockaddr_in server_addr; + struct sockaddr_in client_addr; int n; int plen = 0; const char *ads; @@ -65,6 +66,25 @@ struct libwebsocket *libwebsocket_client_connect_2( libwebsocket_set_timeout(wsi, PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE, AWAITING_TIMEOUT); + + bzero((char *) &client_addr, sizeof(client_addr)); + client_addr.sin_family = AF_INET; + + if (context->iface != NULL) { + if (interface_to_sa(context->iface, &client_addr, + sizeof(client_addr)) < 0) { + lwsl_err("Unable to find interface %s\n", context->iface); + compatible_close(wsi->sock); + goto failed; + } + + if (bind(wsi->sock, (struct sockaddr *) &client_addr, + sizeof(client_addr)) < 0) { + lwsl_err("Error binding to interface %s", context->iface); + compatible_close(wsi->sock); + goto failed; + } + } } server_addr.sin_family = AF_INET; diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 266d5a21..84a10c83 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -1973,6 +1973,7 @@ libwebsocket_create_context(struct lws_context_creation_info *info) context->http_proxy_port = 0; context->http_proxy_address[0] = '\0'; context->options = info->options; + context->iface = info->iface; /* to reduce this allocation, */ context->max_fds = getdtablesize(); lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n", diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index 332416b1..87c85f4a 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -259,6 +259,7 @@ struct libwebsocket_context { int fds_count; int max_fds; int listen_port; + const char *iface; char http_proxy_address[128]; char canonical_hostname[128]; unsigned int http_proxy_port;