Fix for binding socket to a specific interface (fixes #116)
authorvpeter4 <peter.vicman@gmail.com>
Sun, 27 Apr 2014 10:32:15 +0000 (12:32 +0200)
committerAndy Green <andy.green@linaro.org>
Sun, 27 Apr 2014 23:32:14 +0000 (07:32 +0800)
 also allow binding to IP address with @iface info.iface = "eth0"; info.iface
 = "192.168.1.5";

lib/lws-plat-unix.c
lib/server.c

index b0c4a4c..28a7ed5 100644 (file)
@@ -334,6 +334,17 @@ interface_to_sa(struct libwebsocket_context *context,
 
        freeifaddrs(ifr);
        
+       if (rc == -1) {
+               /* check if bind to IP adddress */
+#ifdef LWS_USE_IPV6
+               if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
+                       rc = 0;
+               else
+#endif
+               if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
+                       rc = 0;
+       }
+
        return rc;
 }
 
index 8d55e96..37ce6a1 100644 (file)
@@ -78,7 +78,6 @@ int lws_context_init_server(struct lws_context_creation_info *info,
                bzero((char *) &serv_addr4, sizeof(serv_addr4));
                serv_addr4.sin_addr.s_addr = INADDR_ANY;
                serv_addr4.sin_family = AF_INET;
-               serv_addr4.sin_port = htons(info->port);
 
                if (info->iface) {
                        if (interface_to_sa(context, info->iface,
@@ -89,6 +88,8 @@ int lws_context_init_server(struct lws_context_creation_info *info,
                                return 1;
                        }
                }
+
+               serv_addr4.sin_port = htons(info->port);
        } /* ipv4 */
 
        n = bind(sockfd, v, n);