libwebsockets
Lightweight C library for HTML5 websockets
Socket adoption helpers

Data Structures

union  lws_sock_file_fd_type
 
struct  lws_udp
 

Macros

#define LWS_CAUDP_BIND   1
 

Enumerations

enum  lws_adoption_type {
  LWS_ADOPT_RAW_FILE_DESC = 0, LWS_ADOPT_HTTP = 1, LWS_ADOPT_SOCKET = 2, LWS_ADOPT_ALLOW_SSL = 4,
  LWS_ADOPT_FLAG_UDP = 16, LWS_ADOPT_FLAG_RAW_PROXY = 32, LWS_ADOPT_RAW_SOCKET_UDP = LWS_ADOPT_SOCKET | LWS_ADOPT_FLAG_UDP
}
 

Functions

LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_socket (struct lws_context *context, lws_sockfd_type accept_fd)
 
LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_socket_vhost (struct lws_vhost *vh, lws_sockfd_type accept_fd)
 
LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_descriptor_vhost (struct lws_vhost *vh, lws_adoption_type type, lws_sock_file_fd_type fd, const char *vh_prot_name, struct lws *parent)
 
LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_socket_readbuf (struct lws_context *context, lws_sockfd_type accept_fd, const char *readbuf, size_t len)
 
LWS_VISIBLE LWS_EXTERN struct lws * lws_adopt_socket_vhost_readbuf (struct lws_vhost *vhost, lws_sockfd_type accept_fd, const char *readbuf, size_t len)
 
LWS_VISIBLE LWS_EXTERN struct lws * lws_create_adopt_udp (struct lws_vhost *vhost, int port, int flags, const char *protocol_name, struct lws *parent_wsi)
 

Detailed Description

Socket adoption helpers

When integrating with an external app with its own event loop, these can be used to accept connections from someone else's listening socket.

When using lws own event loop, these are not needed.

Function Documentation

◆ lws_adopt_socket()

LWS_VISIBLE LWS_EXTERN struct lws* lws_adopt_socket ( struct lws_context *  context,
lws_sockfd_type  accept_fd 
)

#include <include/libwebsockets/lws-adopt.h>

lws_adopt_socket() - adopt foreign socket as if listen socket accepted it for the default vhost of context.

Parameters
contextlws context
accept_fdfd of already-accepted socket to adopt

Either returns new wsi bound to accept_fd, or closes accept_fd and returns NULL, having cleaned up any new wsi pieces.

LWS adopts the socket in http serving mode, it's ready to accept an upgrade to ws or just serve http.

◆ lws_adopt_socket_readbuf()

LWS_VISIBLE LWS_EXTERN struct lws* lws_adopt_socket_readbuf ( struct lws_context *  context,
lws_sockfd_type  accept_fd,
const char *  readbuf,
size_t  len 
)

#include <include/libwebsockets/lws-adopt.h>

lws_adopt_socket_readbuf() - adopt foreign socket and first rx as if listen socket accepted it for the default vhost of context.

Parameters
contextlws context
accept_fdfd of already-accepted socket to adopt
readbufNULL or pointer to data that must be drained before reading from accept_fd
lenThe length of the data held at
readbufEither returns new wsi bound to accept_fd, or closes accept_fd and returns NULL, having cleaned up any new wsi pieces.

LWS adopts the socket in http serving mode, it's ready to accept an upgrade to ws or just serve http.

If your external code did not already read from the socket, you can use lws_adopt_socket() instead.

This api is guaranteed to use the data at

Parameters
readbuffirst, before reading from the socket.

readbuf is limited to the size of the ah rx buf, currently 2048 bytes.

◆ lws_adopt_socket_vhost()

LWS_VISIBLE LWS_EXTERN struct lws* lws_adopt_socket_vhost ( struct lws_vhost *  vh,
lws_sockfd_type  accept_fd 
)

#include <include/libwebsockets/lws-adopt.h>

lws_adopt_socket_vhost() - adopt foreign socket as if listen socket accepted it for vhost

Parameters
vhlws vhost
accept_fdfd of already-accepted socket to adopt

Either returns new wsi bound to accept_fd, or closes accept_fd and returns NULL, having cleaned up any new wsi pieces.

LWS adopts the socket in http serving mode, it's ready to accept an upgrade to ws or just serve http.

◆ lws_adopt_socket_vhost_readbuf()

LWS_VISIBLE LWS_EXTERN struct lws* lws_adopt_socket_vhost_readbuf ( struct lws_vhost *  vhost,
lws_sockfd_type  accept_fd,
const char *  readbuf,
size_t  len 
)

#include <include/libwebsockets/lws-adopt.h>

lws_adopt_socket_vhost_readbuf() - adopt foreign socket and first rx as if listen socket accepted it for vhost.

Parameters
vhostlws vhost
accept_fdfd of already-accepted socket to adopt
readbufNULL or pointer to data that must be drained before reading from accept_fd
lenThe length of the data held at
readbufEither returns new wsi bound to accept_fd, or closes accept_fd and returns NULL, having cleaned up any new wsi pieces.

LWS adopts the socket in http serving mode, it's ready to accept an upgrade to ws or just serve http.

If your external code did not already read from the socket, you can use lws_adopt_socket() instead.

This api is guaranteed to use the data at

Parameters
readbuffirst, before reading from the socket.

readbuf is limited to the size of the ah rx buf, currently 2048 bytes.

◆ lws_create_adopt_udp()

LWS_VISIBLE LWS_EXTERN struct lws* lws_create_adopt_udp ( struct lws_vhost *  vhost,
int  port,
int  flags,
const char *  protocol_name,
struct lws *  parent_wsi 
)

#include <include/libwebsockets/lws-adopt.h>

lws_create_adopt_udp() - create, bind and adopt a UDP socket

Parameters
vhostlws vhost
portUDP port to bind to, -1 means unbound
flags0 or LWS_CAUDP_NO_BIND
protocol_nameName of protocol on vhost to bind wsi to
parent_wsiNULL or parent wsi new wsi will be a child of

Either returns new wsi bound to accept_fd, or closes accept_fd and returns NULL, having cleaned up any new wsi pieces.