libwebsockets
Lightweight C library for HTML5 websockets
Network related helper APIs

Data Structures

struct  lws_dns_score
 
struct  lws_route
 

Macros

#define LWS_ITOSA_USABLE   0
 
#define LWS_ITOSA_NOT_EXIST   -1
 
#define LWS_ITOSA_NOT_USABLE   -2
 
#define LWS_ITOSA_BUSY
 

Typedefs

typedef uint16_t lws_route_uidx_t
 
typedef struct lws_dns_score lws_dns_score_t
 
typedef struct lws_route lws_route_t
 
typedef lws_route_t lws_dns_sort_t
 

Functions

LWS_VISIBLE LWS_EXTERN const 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 LWS_EXTERN const char * lws_get_peer_simple (struct lws *wsi, char *name, size_t namelen)
 
LWS_VISIBLE LWS_EXTERN const 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_sa46_on_net (const lws_sockaddr46 *sa46a, const lws_sockaddr46 *sa46_net, int net_len)
 
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

◆ LWS_ITOSA_BUSY

#define LWS_ITOSA_BUSY

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

Value:
-3 /* only returned by lws_socket_bind() on
EADDRINUSE */

Function Documentation

◆ lws_canonical_hostname()

LWS_VISIBLE LWS_EXTERN const 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.

Parameters
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

Parameters
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 LWS_EXTERN const 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

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

This provides a 123.123.123.123 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

Parameters
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

Parameters
sa46afirst
sa46bsecond

Returns 0 if the address family is INET or INET6 and the address is the same, or if the AF is the same but not INET or INET6, otherwise nonzero.

◆ lws_sa46_on_net()

LWS_VISIBLE LWS_EXTERN int lws_sa46_on_net ( const lws_sockaddr46 sa46a,
const lws_sockaddr46 sa46_net,
int  net_len 
)

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

lws_sa46_on_net() - checks if an sa46 is on the subnet represented by another

Parameters
sa46afirst
sa46_netnetwork
net_lenlength of network non-mask

Returns 0 if sa46a belongs on network sa46_net/net_len

If there is an ipv4 / v6 mismatch between the ip and the net, the ipv4 address is promoted to ::ffff:x.x.x.x before the comparison.

◆ 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

Parameters
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 "1.2.3.4" or "::1". Returns the number of chars written into buf, 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

Parameters
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 "1.2.3.4" or "::1". Returns the number of chars written into buf, else < 0. ipv6 only supported with LWS_IPV6=1 at cmake.