[Libwebsockets] Next lws release coming up

Andy Green andy at warmcat.com
Fri Apr 27 03:07:50 CEST 2018



On 04/27/2018 08:39 AM, Per Bothner wrote:
> On 04/26/2018 05:13 PM, Andy Green wrote:
>>
>>
>> On 04/27/2018 07:47 AM, Per Bothner wrote:
>>
>> Thanks.
>>
>>> info->port = vhost->listen_port;
>>
>> It's not the "right" solution, since info should be const (I am going 
>> through making it const atm).
> 
> It's a tradeoff betwen backwards compatibility vs cleaner API.

Well in the case of the api taking a const pointer, it won't make any 
compile-time compatibility problems.  Whatever it was giving it before 
is still going to be OK.

However it makes sure there are no unintended side-effects coming any 
more, which looks like it was what was happening here (due to my 
slipping on the notional const-ness of info...)

>> Can you print what is in info->port before and after that line?  I am 
>> having trouble imagining what that is doing to fix the problem.
> 
> At the entry to lws_context_init_server, info->port is 0.
> The old code did:
> 
>      n = lws_socket_bind(vhost, sockfd, info->port, info->iface);
>      if (n < 0)
>          goto bail;
>      info->port = n;
>      ...
>      vhost->listen_port = info->port;
> 
> The new code does:
> 
>      is = lws_socket_bind(vhost, sockfd, vhost->listen_port, vhost->iface);
>      ...
>      vhost->listen_port = is;
>      ....
> 
> This means when lws_context_init_server returns, info->port is still 0,
> while with the old code it is the same as vhost->listen_port.

The docs say

	int port;
	/**< VHOST: Port to listen on. Use CONTEXT_PORT_NO_LISTEN to suppress
	 * listening for a client. Use CONTEXT_PORT_NO_LISTEN_SERVER if you are
	 * writing a server but you are using \ref sock-adopt instead of the
	 * built-in listener */

0 is actually an invalid port number for tcp.  I didn't imagine anyone 
was going to bind on port 0 which means "pick a port for me"... but 
actually that's completely legit.

> When I grep the DomTerm sources, I find these suspicious lines:
> 
> protocol.c:    display_session(opts, pclient, NULL, info.port);
> protocol.c:    display_session(opts, pclient, NULL, info.port);
> server.c:                port_specified = info.port;
> server.c:                if (info.port < 0) {
> server.c:    info.port = 0;
> server.c:    make_html_file(info.port);
> server.c:                info.port, info.port);
> 
> and in server.h:
> 
> extern struct lws_context_creation_info info; // FIXME rename
> 
> I'm guessing I should change info.port to vhost->listen_port
> most places after the call to lws_create_context.

Right... I pushed a patch adding a suitable helper api

https://github.com/warmcat/libwebsockets/commit/f58e50224ea2089728c455ce791a1d89ba8a9552

diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 9c9d2714..d93e6051 100644
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -2508,6 +2508,12 @@ lws_socket_bind(struct lws_vhost *vhost, 
lws_sockfd_type sockfd, int port,
  	return port;
  }

+LWS_VISIBLE LWS_EXTERN int
+lws_get_vhost_listen_port(struct lws_vhost *vhost)
+{
+	return vhost->listen_port;
+}
+
  #if defined(LWS_WITH_IPV6)
  LWS_EXTERN unsigned long
  lws_get_addr_scope(const char *ipaddr)
diff --git a/lib/libwebsockets.h b/lib/libwebsockets.h
index 35c9692c..a565cd8d 100644
--- a/lib/libwebsockets.h
+++ b/lib/libwebsockets.h
@@ -2686,7 +2686,11 @@ struct lws_context_creation_info {
  	/**< VHOST: Port to listen on. Use CONTEXT_PORT_NO_LISTEN to suppress
  	 * listening for a client. Use CONTEXT_PORT_NO_LISTEN_SERVER if you are
  	 * writing a server but you are using \ref sock-adopt instead of the
-	 * built-in listener */
+	 * built-in listener.
+	 *
+	 * You can also set port to 0, in which case the kernel will pick
+	 * a random port that is not already in use.  You can find out what
+	 * port the vhost is listening on using lws_get_vhost_listen_port() */
  	const char *iface;
  	/**< VHOST: NULL to bind the listen socket to all interfaces, or the
  	 * interface name, eg, "eth2"
@@ -5918,6 +5922,18 @@ lws_compare_time_t(struct lws_context *context, 
time_t t1, time_t t2);
  LWS_VISIBLE LWS_EXTERN struct lws_context * LWS_WARN_UNUSED_RESULT
  lws_get_context(const struct lws *wsi);

+/**
+ * lws_get_vhost_listen_port - Find out the port number a vhost is 
listening on
+ *
+ * In the case you passed 0 for the port number at context creation 
time, you
+ * can discover the port number that was actually chosen for the vhost 
using
+ * this api.
+ *
+ * \param vhost:	Vhost to get listen port from
+ */
+LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
+lws_get_vhost_listen_port(struct lws_vhost *vhost);
+
  /**
   * lws_get_count_threads(): how many service threads the context uses
   *

Sorry for the confusion.

-Andy



More information about the Libwebsockets mailing list