From e83148a3813bcae5dc6f5a4e34cef4b67fa641cb Mon Sep 17 00:00:00 2001 From: vpeter4 Date: Sun, 27 Apr 2014 12:32:15 +0200 Subject: [PATCH] Fix for binding socket to a specific interface (fixes #116) also allow binding to IP address with @iface info.iface = "eth0"; info.iface = "192.168.1.5"; --- lib/lws-plat-unix.c | 11 +++++++++++ lib/server.c | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/lws-plat-unix.c b/lib/lws-plat-unix.c index 279a3be4a..26ff172b5 100644 --- a/lib/lws-plat-unix.c +++ b/lib/lws-plat-unix.c @@ -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; } diff --git a/lib/server.c b/lib/server.c index bb9a1ae1f..56e49ff00 100644 --- a/lib/server.c +++ b/lib/server.c @@ -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);