Lightweight C library for HTML5 websockets
Network related helper APIs


#define LWS_ITOSA_USABLE   0
#define LWS_ITOSA_NOT_EXIST   -1


LWS_VISIBLE const LWS_EXTERN char *LWS_WARN_UNUSED_RESULT lws_canonical_hostname (struct lws_context *context)
LWS_VISIBLE LWS_EXTERN void lws_get_peer_addresses (struct lws *wsi, lws_sockfd_type fd, char *name, int name_len, char *rip, int rip_len)
LWS_VISIBLE const LWS_EXTERN char * lws_get_peer_simple (struct lws *wsi, char *name, int namelen)
LWS_VISIBLE LWS_EXTERN int lws_interface_to_sa (int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)

Detailed Description

Network related helper APIs

These wrap miscellaneous useful network-related functions

Macro Definition Documentation



#include <include/libwebsockets/lws-network-helper.h>

-3 /* only returned by lws_socket_bind() on

Function Documentation

◆ lws_canonical_hostname()

LWS_VISIBLE const LWS_EXTERN char* LWS_WARN_UNUSED_RESULT lws_canonical_hostname ( struct lws_context *  context)

#include <include/libwebsockets/lws-network-helper.h>

lws_canonical_hostname() - returns this host's hostname

This is typically used by client code to fill in the host parameter when making a client connection. You can only call it after the context has been created.

contextWebsocket context

◆ lws_get_peer_addresses()

LWS_VISIBLE LWS_EXTERN void lws_get_peer_addresses ( struct lws *  wsi,
lws_sockfd_type  fd,
char *  name,
int  name_len,
char *  rip,
int  rip_len 

#include <include/libwebsockets/lws-network-helper.h>

lws_get_peer_addresses() - Get client address information

wsiLocal struct lws associated with
fdConnection socket descriptor
nameBuffer to take client address name
name_lenLength of client address name buffer
ripBuffer to take client address IP dotted quad
rip_lenLength of client address IP buffer
 This function fills in name and rip with the name and IP of
 the client connected with socket descriptor fd.  Names may be
 truncated if there is not enough room.  If either cannot be
 determined, they will be returned as valid zero-length strings.

◆ lws_get_peer_simple()

LWS_VISIBLE const LWS_EXTERN char* lws_get_peer_simple ( struct lws *  wsi,
char *  name,
int  namelen 

#include <include/libwebsockets/lws-network-helper.h>

lws_get_peer_simple() - Get client address information without RDNS

wsiLocal struct lws associated with
nameBuffer to take client address name
namelenLength of client address name buffer

This provides a type IP address in name from the peer that has connected to wsi

◆ lws_interface_to_sa()

LWS_VISIBLE LWS_EXTERN int lws_interface_to_sa ( int  ipv6,
const char *  ifname,
struct sockaddr_in *  addr,
size_t  addrlen 

#include <include/libwebsockets/lws-network-helper.h>

lws_interface_to_sa() - Convert interface name or IP to sockaddr struct

ipv6Allow IPV6 addresses
ifnameInterface name or IP
addrstruct sockaddr_in * to be written
addrlenLength of addr

This converts a textual network interface name to a sockaddr usable by other network functions.

If the network interface doesn't exist, it will return LWS_ITOSA_NOT_EXIST.

If the network interface is not usable, eg ethernet cable is removed, it may logically exist but not have any IP address. As such it will return LWS_ITOSA_NOT_USABLE.

If the network interface exists and is usable, it will return LWS_ITOSA_USABLE.