Lightweight C library for HTML5 websockets
Network related helper APIs

Data Structures

union  lws_sockaddr46


#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, size_t namelen)
LWS_VISIBLE const LWS_EXTERN char * lws_get_peer_simple_fd (lws_sockfd_type fd, char *name, size_t namelen)
LWS_VISIBLE LWS_EXTERN int lws_interface_to_sa (int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
LWS_VISIBLE LWS_EXTERN int lws_sa46_compare_ads (const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46b)
LWS_VISIBLE LWS_EXTERN int lws_parse_numeric_address (const char *ads, uint8_t *result, size_t max_len)
LWS_VISIBLE LWS_EXTERN int lws_sa46_parse_numeric_address (const char *ads, lws_sockaddr46 *sa46)
LWS_VISIBLE LWS_EXTERN int lws_write_numeric_address (const uint8_t *ads, int size, char *buf, size_t len)
LWS_VISIBLE LWS_EXTERN int lws_sa46_write_numeric_address (lws_sockaddr46 *sa46, char *buf, size_t len)

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,
size_t  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.

◆ lws_sa46_compare_ads()

LWS_VISIBLE LWS_EXTERN int lws_sa46_compare_ads ( const lws_sockaddr46 sa46a,
const lws_sockaddr46 sa46b 

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

lws_sa46_compare_ads() - checks if two sa46 have the same address


Returns 0 if the address family and address are the same, otherwise nonzero.

◆ lws_sa46_write_numeric_address()

LWS_VISIBLE LWS_EXTERN int lws_sa46_write_numeric_address ( lws_sockaddr46 sa46,
char *  buf,
size_t  len 

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

lws_sa46_write_numeric_address() - convert sa46 ads to textual numeric ads

sa46the sa46 whose address to show
bufresult buffer to take text format
lenmax size of text buffer

Converts the ipv4 or ipv6 address in an lws_sockaddr46 to a textual representation of the numeric address, like "" or "::1". Return 0 if OK else < 0. ipv6 only supported with LWS_IPV6=1 at cmake.

◆ lws_write_numeric_address()

LWS_VISIBLE LWS_EXTERN int lws_write_numeric_address ( const uint8_t *  ads,
int  size,
char *  buf,
size_t  len 

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

lws_write_numeric_address() - convert network byte order ads to text

adsnetwork byte order address array
sizenumber of bytes valid in ads
bufresult buffer to take text format
lenmax size of text buffer

Converts an array of network-ordered byte address elements to a textual representation of the numeric address, like "" or "::1". Return 0 if OK else < 0. ipv6 only supported with LWS_IPV6=1 at cmake.