Add support to binding client to interface.
authorMattias Lundberg <lundberg.mattias@gmail.com>
Tue, 18 Feb 2014 09:06:57 +0000 (10:06 +0100)
committerAndy Green <andy.green@linaro.org>
Wed, 19 Feb 2014 00:38:08 +0000 (08:38 +0800)
lib/client-handshake.c
lib/libwebsockets.c
lib/private-libwebsockets.h

index fc8ffb7..6bce8fe 100644 (file)
@@ -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;
index 266d5a2..84a10c8 100644 (file)
@@ -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",
index 332416b..87c85f4 100644 (file)
@@ -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;