libwebsockets
Lightweight C library for HTML5 websockets
Miscellaneous APIs

Data Structures

struct  lws_dll
 
struct  lws_dll_lws
 

Functions

LWS_VISIBLE LWS_EXTERN void lws_dll_add_front (struct lws_dll *d, struct lws_dll *phead)
 
LWS_VISIBLE LWS_EXTERN void lws_dll_remove (struct lws_dll *d)
 
LWS_VISIBLE LWS_EXTERN int lws_buflist_append_segment (struct lws_buflist **head, const uint8_t *buf, size_t len)
 
LWS_VISIBLE LWS_EXTERN size_t lws_buflist_next_segment_len (struct lws_buflist **head, uint8_t **buf)
 
LWS_VISIBLE LWS_EXTERN int lws_buflist_use_segment (struct lws_buflist **head, size_t len)
 
LWS_VISIBLE LWS_EXTERN void lws_buflist_destroy_all_segments (struct lws_buflist **head)
 
void lws_buflist_describe (struct lws_buflist **head, void *id)
 
LWS_VISIBLE LWS_EXTERN int lws_snprintf (char *str, size_t size, const char *format,...) LWS_FORMAT(3)
 
LWS_VISIBLE LWS_EXTERN char * lws_strncpy (char *dest, const char *src, size_t size)
 
LWS_VISIBLE LWS_EXTERN int lws_get_random (struct lws_context *context, void *buf, int len)
 
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_daemonize (const char *_lock_path)
 
LWS_VISIBLE LWS_EXTERN const char *LWS_WARN_UNUSED_RESULT lws_get_library_version (void)
 
LWS_VISIBLE LWS_EXTERN void * lws_wsi_user (struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN void lws_set_wsi_user (struct lws *wsi, void *user)
 
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_parse_uri (char *p, const char **prot, const char **ads, int *port, const char **path)
 
LWS_VISIBLE LWS_EXTERN const char * lws_cmdline_option (int argc, const char **argv, const char *val)
 
LWS_VISIBLE LWS_EXTERN unsigned long lws_now_secs (void)
 
LWS_VISIBLE LWS_EXTERN int lws_compare_time_t (struct lws_context *context, time_t t1, time_t t2)
 
LWS_VISIBLE LWS_EXTERN struct lws_context *LWS_WARN_UNUSED_RESULT lws_get_context (const struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_get_vhost_listen_port (struct lws_vhost *vhost)
 
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_get_count_threads (struct lws_context *context)
 
LWS_VISIBLE LWS_EXTERN struct lws *LWS_WARN_UNUSED_RESULT lws_get_parent (const struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN struct lws *LWS_WARN_UNUSED_RESULT lws_get_child (const struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN const struct lws_udp *LWS_WARN_UNUSED_RESULT lws_get_udp (const struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN void lws_set_parent_carries_io (struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN void * lws_get_opaque_parent_data (const struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN void lws_set_opaque_parent_data (struct lws *wsi, void *data)
 
LWS_VISIBLE LWS_EXTERN int lws_get_child_pending_on_writable (const struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN void lws_clear_child_pending_on_writable (struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN int lws_get_close_length (struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN unsigned char * lws_get_close_payload (struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN struct lws * lws_get_network_wsi (struct lws *wsi)
 
LWS_VISIBLE LWS_EXTERN void lws_set_allocator (void *(*realloc)(void *ptr, size_t size, const char *reason))
 

Detailed Description

Miscellaneous APIs

Various APIs outside of other categories

Function Documentation

◆ lws_buflist_append_segment()

LWS_VISIBLE LWS_EXTERN int lws_buflist_append_segment ( struct lws_buflist **  head,
const uint8_t *  buf,
size_t  len 
)

#include <lib/libwebsockets.h>

lws_buflist_append_segment(): add buffer to buflist at head

Parameters
headlist head
bufbuffer to stash
lenlength of buffer to stash

Returns -1 on OOM, 1 if this was the first segment on the list, and 0 if it was a subsequent segment.

◆ lws_buflist_destroy_all_segments()

LWS_VISIBLE LWS_EXTERN void lws_buflist_destroy_all_segments ( struct lws_buflist **  head)

#include <lib/libwebsockets.h>

lws_buflist_destroy_all_segments(): free all segments on the list

Parameters
headlist head

This frees everything on the list unconditionally. *head is always NULL after this.

◆ lws_buflist_next_segment_len()

LWS_VISIBLE LWS_EXTERN size_t lws_buflist_next_segment_len ( struct lws_buflist **  head,
uint8_t **  buf 
)

#include <lib/libwebsockets.h>

lws_buflist_next_segment_len(): number of bytes left in current segment

Parameters
headlist head
bufif non-NULL, *buf is written with the address of the start of the remaining data in the segment

Returns the number of bytes left in the current segment. 0 indicates that the buflist is empty (there are no segments on the buflist).

◆ lws_buflist_use_segment()

LWS_VISIBLE LWS_EXTERN int lws_buflist_use_segment ( struct lws_buflist **  head,
size_t  len 
)

#include <lib/libwebsockets.h>

lws_buflist_use_segment(): remove len bytes from the current segment

Parameters
headlist head
lennumber of bytes to mark as used

If len is less than the remaining length of the current segment, the position in the current segment is simply advanced and it returns.

If len uses up the remaining length of the current segment, then the segment is deleted and the list head moves to the next segment if any.

Returns the number of bytes left in the current segment. 0 indicates that the buflist is empty (there are no segments on the buflist).

◆ lws_cmdline_option()

LWS_VISIBLE LWS_EXTERN const char* lws_cmdline_option ( int  argc,
const char **  argv,
const char *  val 
)

#include <lib/libwebsockets.h>

lws_cmdline_option(): simple commandline parser

Parameters
argccount of argument strings
argvargument strings
valstring to find

Returns NULL if the string val is not found in the arguments.

If it is found, then it returns a pointer to the next character after val. So if val is "-d", then for the commandlines "myapp -d15" and "myapp -d 15", in both cases the return will point to the "15".

In the case there is no argument, like "myapp -d", the return will either point to the '\0' at the end of -d, or to the start of the next argument, ie, will be non-NULL.

◆ lws_compare_time_t()

LWS_VISIBLE LWS_EXTERN int lws_compare_time_t ( struct lws_context *  context,
time_t  t1,
time_t  t2 
)

#include <lib/libwebsockets.h>

lws_compare_time_t(): return relationship between two time_t

Parameters
contextstruct lws_context
t1time_t 1
t2time_t 2

returns <0 if t2 > t1; >0 if t1 > t2; or == 0 if t1 == t2.

This is aware of clock discontiguities that may have affected either t1 or t2 and adapts the comparison for them.

For the discontiguity detection to work, you must avoid any arithmetic on the times being compared. For example to have a timeout that triggers 15s from when it was set, store the time it was set and compare like if (lws_compare_time_t(context, now, set_time) > 15)

◆ lws_daemonize()

LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_daemonize ( const char *  _lock_path)

#include <lib/libwebsockets.h>

lws_daemonize(): make current process run in the background

Parameters
_lock_paththe filepath to write the lock file

Spawn lws as a background process, taking care of various things

◆ lws_get_child()

LWS_VISIBLE LWS_EXTERN struct lws* LWS_WARN_UNUSED_RESULT lws_get_child ( const struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_get_child() - get child wsi or NULL

Parameters
wsilws connection

Allows you to find a related wsi from the parent wsi.

◆ lws_get_context()

LWS_VISIBLE LWS_EXTERN struct lws_context* LWS_WARN_UNUSED_RESULT lws_get_context ( const struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_get_context - Allow getting lws_context from a Websocket connection instance

With this function, users can access context in the callback function. Otherwise users may have to declare context as a global variable.

Parameters
wsiWebsocket connection instance

◆ lws_get_count_threads()

LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_get_count_threads ( struct lws_context *  context)

#include <lib/libwebsockets.h>

lws_get_count_threads(): how many service threads the context uses

Parameters
contextthe lws context

By default this is always 1, if you asked for more than lws can handle it will clip the number of threads. So you can use this to find out how many threads are actually in use.

◆ lws_get_library_version()

LWS_VISIBLE LWS_EXTERN const char* LWS_WARN_UNUSED_RESULT lws_get_library_version ( void  )

#include <lib/libwebsockets.h>

lws_get_library_version(): return string describing the version of lws

On unix, also includes the git describe

◆ lws_get_network_wsi()

LWS_VISIBLE LWS_EXTERN struct lws* lws_get_network_wsi ( struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_get_network_wsi() - Returns wsi that has the tcp connection for this wsi

Parameters
wsiwsi you have

Returns wsi that has the tcp connection (which may be the incoming wsi)

HTTP/1 connections will always return the incoming wsi HTTP/2 connections may return a different wsi that has the tcp connection

◆ lws_get_parent()

LWS_VISIBLE LWS_EXTERN struct lws* LWS_WARN_UNUSED_RESULT lws_get_parent ( const struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_get_parent() - get parent wsi or NULL

Parameters
wsilws connection

Specialized wsi like cgi stdin/out/err are associated to a parent wsi, this allows you to get their parent.

◆ lws_get_random()

LWS_VISIBLE LWS_EXTERN int lws_get_random ( struct lws_context *  context,
void *  buf,
int  len 
)

#include <lib/libwebsockets.h>

lws_get_random(): fill a buffer with platform random data

Parameters
contextthe lws context
bufbuffer to fill
lenhow much to fill

This is intended to be called from the LWS_CALLBACK_RECEIVE callback if it's interested to see if the frame it's dealing with was sent in binary mode.

◆ lws_get_udp()

LWS_VISIBLE LWS_EXTERN const struct lws_udp* LWS_WARN_UNUSED_RESULT lws_get_udp ( const struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_get_udp() - get wsi's udp struct

Parameters
wsilws connection

Returns NULL or pointer to the wsi's UDP-specific information

◆ lws_get_vhost_listen_port()

LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_get_vhost_listen_port ( struct lws_vhost *  vhost)

#include <lib/libwebsockets.h>

lws_get_vhost_listen_port - Find out the port number a vhost is listening on

In the case you passed 0 for the port number at context creation time, you can discover the port number that was actually chosen for the vhost using this api.

Parameters
vhostVhost to get listen port from

◆ lws_now_secs()

LWS_VISIBLE LWS_EXTERN unsigned long lws_now_secs ( void  )

#include <lib/libwebsockets.h>

lws_now_secs(): return seconds since 1970-1-1

◆ lws_parse_uri()

LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_parse_uri ( char *  p,
const char **  prot,
const char **  ads,
int *  port,
const char **  path 
)

#include <lib/libwebsockets.h>

lws_parse_uri: cut up prot:/ads:port/path into pieces Notice it does so by dropping '\0' into input string and the leading / on the path is consequently lost

Parameters
pincoming uri string.. will get written to
protresult pointer for protocol part (https://)
adsresult pointer for address part
portresult pointer for port part
pathresult pointer for path part

◆ lws_set_allocator()

LWS_VISIBLE LWS_EXTERN void lws_set_allocator ( void *(*)(void *ptr, size_t size, const char *reason)  realloc)

#include <lib/libwebsockets.h>

lws_set_allocator() - custom allocator support

Parameters
reallocAllows you to replace the allocator (and deallocator) used by lws

◆ lws_set_parent_carries_io()

LWS_VISIBLE LWS_EXTERN void lws_set_parent_carries_io ( struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_parent_carries_io() - mark wsi as needing to send messages via parent

Parameters
wsichild lws connection

◆ lws_set_wsi_user()

LWS_VISIBLE LWS_EXTERN void lws_set_wsi_user ( struct lws *  wsi,
void *  user 
)

#include <lib/libwebsockets.h>

lws_wsi_set_user() - set the user data associated with the client connection

Parameters
wsilws connection
useruser data

By default lws allocates this and it's not legal to externally set it yourself. However client connections may have it set externally when the connection is created... if so, this api can be used to modify it at runtime additionally.

◆ lws_snprintf()

LWS_VISIBLE LWS_EXTERN int lws_snprintf ( char *  str,
size_t  size,
const char *  format,
  ... 
)

#include <lib/libwebsockets.h>

lws_snprintf(): snprintf that truncates the returned length too

Parameters
strdestination buffer
sizebytes left in destination buffer
formatformat string
...args for format

This lets you correctly truncate buffers by concatenating lengths, if you reach the limit the reported length doesn't exceed the limit.

◆ lws_strncpy()

LWS_VISIBLE LWS_EXTERN char* lws_strncpy ( char *  dest,
const char *  src,
size_t  size 
)

#include <lib/libwebsockets.h>

lws_strncpy(): strncpy that guarantees NUL on truncated copy

Parameters
destdestination buffer
srcsource buffer
sizebytes left in destination buffer

This lets you correctly truncate buffers by concatenating lengths, if you reach the limit the reported length doesn't exceed the limit.

◆ lws_wsi_user()

LWS_VISIBLE LWS_EXTERN void* lws_wsi_user ( struct lws *  wsi)

#include <lib/libwebsockets.h>

lws_wsi_user() - get the user data associated with the connection

Parameters
wsilws connection

Not normally needed since it's passed into the callback