[Libwebsockets] How to set the "user" callback parameter for adopted sockets?

Andy Green andy at warmcat.com
Wed Nov 23 01:50:44 CET 2016


On Tue, 2016-11-22 at 19:02 -0500, Alan Conway wrote:
> I'm making progress! I can crash my router from a web brower :)
> 
> I can't figure out how to set the "user" parameter for lws callbacks
> for a socket adopted with lws_adopt_socket(). I can access the

That's not what *user_space is for.

It's for protocol-specific local data, per-connection.  When the
protocol changes, which it may often do with a http/1.1 keepalive
connection visiting urls that map on to different plugins, the user
data allocation is destroyed by lws and a new one sized appropriately
for the new protocol allocated.

> allocated space *after* the adopt call with lws_wsi_user(), but
> lws_adopt_socket() itself is firing my callbacks so that's too late.
> 
> The docs mention setting up your session data on the
> LWS_CALLBACK_ESTABLISHED event, but at that point I have nothing I
> can
> use to find data from my application.

LWS doesn't have this concept of external shadowed data per-connection
built-in.

You have two ways to do it

 - add a new opaque void * in struct lws and set it at adopt-time,
provide an accessor to get it from the wsi.  This is a bit of a burden
for everyone who doesn't care about this then.

 - Set the context user pointer at context creation time.  You can get
this from a wsi in the callback.  You can then use this context-wide
pointer to dereference the wsi pointer to your private connection
pointer on your side, entirely outside of lws.

-Andy

> Thanks,
> Alan.
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://libwebsockets.org/mailman/listinfo/libwebsockets



More information about the Libwebsockets mailing list