[Libwebsockets] Patch for Windows to allow bind to specific interface.

Emerson Clarke emerson at openfin.co
Wed Nov 5 19:49:52 CET 2014


For further information on this issue, please see the following CWE
vulnerability notice:

http://cwe.mitre.org/data/definitions/605.html



On Wed, Nov 5, 2014 at 6:05 PM, Emerson Clarke <emerson at openfin.co> wrote:
> Hi,
>
> Ive been working with libwebsockets recently, and it seems like a well
> written and very useful library, so many thanks.
>
> I did encounter one issue when trying to bind a websockets server on Windows.
>
> The problem was that another application kept stealing the port i was
> using. This seemed to be because the other application used
> SO_REUSEADDR and in combination with libwebsockets binding to 0.0.0.0
> (INADDR_ANY) this was allowing the other application to steal its
> port.
>
> The solution was to allow libwebsockets to bind to a specific
> interface (i.e. 127.0.0.1), but there appeared to be no implementation
> for:
>
> LWS_VISIBLE int
> interface_to_sa(struct libwebsocket_context *context,
> const char *ifname, struct sockaddr_in *addr, size_t addrlen)
>
> So i wrote one quickly that attempts to resolve a numeric ip and then
> looks up the hostname otherwise.  If youd like to include it, that
> would be great. An MIT or equivalent licence is hereby granted...
>
> The code should work on OSX/Linux etc also.
>
> LWS_VISIBLE int
> interface_to_sa(struct libwebsocket_context *context,
> const char *ifname, struct sockaddr_in *addr, size_t addrlen)
> {
>
> long long address = inet_addr(ifname);
> if (address == INADDR_NONE)
> {
>
> struct hostent *entry = gethostbyname(ifname);
> if (entry)
> address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
> }
>
> if (address == INADDR_NONE)
> return -1;
>
> addr->sin_addr.s_addr = address;
>
> return 0;
> }
>
>
> Regards,
>
> Emerson



More information about the Libwebsockets mailing list