[Libwebsockets] How to obtain the IP address of the client

Andy Green andy at warmcat.com
Fri Nov 8 18:48:09 CET 2019



On 11/8/19 4:42 PM, Brice Hamon wrote:
> That's how I do it, in my protocol callback function.
> 
>   case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
>          {
>              char client_name[512];
>              char remote_client_ip[64];
>              int fd = 0;
>              if (wsi)
>                  fd = lws_get_socket_fd(wsi);
> 
>              lws_get_peer_addresses(wsi, fd,
>                                  client_name, sizeof(client_name),
>                                  remote_client_ip, 
> sizeof(remote_client_ip));
> 
>              websocket->log(LOG_INFO, "WS protocol: websocket request 
> from %s (%s)",
>                                  client_name, remote_client_ip);
>              break;
>          }

Thanks.  There's also an alternative way that doesn't do the reverse DNS 
lookup that might be preferable in some cases

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/http-server/minimal-http-server-dynamic/minimal-http-server-dynamic.c#n55

-Andy

> 
> 
> On Fri, Nov 8, 2019 at 11:26 AM Stefano Mora <stefano.mora at newdep.com 
> <mailto:stefano.mora at newdep.com>> wrote:
> 
>     Hi all,
>     is there a way to get the IP address of the connected client(s) ?
>     In which phase I can obtain it/them?
> 
>     I saw the lws_get_peer_addresses function but I don't have the fd
>     parameter..
>     Thanks
> 
> 
>     Stefano Mora / SW Engineer
>     stefano.mora at newdep.com <mailto:stefano.mora at newdep.com>
> 
> 
> 
>     _______________________________________________
>     Libwebsockets mailing list
>     Libwebsockets at ml.libwebsockets.org
>     <mailto:Libwebsockets at ml.libwebsockets.org>
>     https://libwebsockets.org/mailman/listinfo/libwebsockets
> 
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 


More information about the Libwebsockets mailing list