UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
anonymous_namespace{miniserver.cpp} Namespace Reference

Classes

struct  active_connection_t
 
struct  mserv_request_t
 miniserver received request message. More...
 

Enumerations

enum  MiniServerState { MSERV_IDLE , MSERV_RUNNING , MSERV_STOPPING }
 miniserver state More...
 

Variables

in_port_t miniStopSockPort
 Port of the stop socket.
 
MiniServerState gMServState {MSERV_IDLE}
 miniserver state
 
MiniServerCallback gSoapCallback {nullptr}
 SOAP callback.
 
MiniServerCallback gGenaCallback {nullptr}
 GENA callback.
 

Scope restricted to file

static LinkedList gActiveConnections
 
static pthread_mutex_t gActiveConnectionsMutex
 
static int gActiveConnectionsInitialized = 0
 
static int active_connection_cmp (void *first, void *second)
 Compare function for active connections, return true if equal (found)
 
static void add_active_connection (SOCKET sock)
 Add a socket to the active connections list.
 
static void remove_active_connection (SOCKET sock)
 Remove a socket from the active connections list.
 
void shutdown_all_active_connections (void)
 Shutdown all active socket connections.
 
int host_header_is_numeric (char *a_host_port, size_t a_host_port_len)
 Check if a network address is numeric.
 
int getNumericHostRedirection (SOCKET a_socket, char *a_host_port, size_t a_hp_size)
 Returns the ip address with port as text that is bound to a socket.
 
int dispatch_request (SOCKINFO *a_info, http_parser_t *a_hparser)
 Based on the type of message, appropriate callback is issued.
 
void free_handle_request_arg (void *args)
 Free memory assigned for handling request and unitialize socket functionality.
 
void handle_request (void *args)
 Receive the request and dispatch it for handling.
 
void schedule_request_job (SOCKET a_sock, UPnPsdk::SSockaddr &clientAddr)
 Initilize the thread pool to handle an incomming request, sets priority for the job and adds the job to the thread pool.
 
void fdset_if_valid (SOCKET a_sock, fd_set *a_set)
 Add a socket file descriptor to an 'fd_set' structure as needed for ::select().
 
int web_server_accept (SOCKET listen_sock, fd_set &set)
 Accept requested connection from a remote control point and run it in a new thread.
 
void ssdp_read (SOCKET *rsock, fd_set *set)
 Read data from the SSDP socket.
 
int receive_from_stopSock (SOCKET ssock, fd_set *set)
 Check if we have received a packet that shall stop the miniserver.
 
void RunMiniServer (MiniServerSockArray *miniSock)
 Run the miniserver.
 
void RunMiniServer_f (MiniServerSockArray *miniSock)
 
int get_port (SOCKET sockfd, uint16_t *port)
 Returns port to which socket, sockfd, is bound.
 
int get_miniserver_sockets (MiniServerSockArray *out, in_port_t listen_port4, in_port_t listen_port6, in_port_t listen_port6UlaGua)
 Create STREAM sockets, binds to local network interfaces and listens for incoming connections.
 
int get_miniserver_stopsock (MiniServerSockArray *out)
 Creates the miniserver STOP socket, usable to listen for stopping the miniserver.
 
void InitMiniServerSockArray (MiniServerSockArray *miniSocket)
 Initialize a miniserver Socket Array.
 

Class Documentation

◆ anonymous_namespace{miniserver.cpp}::active_connection_t

struct anonymous_namespace{miniserver.cpp}::active_connection_t

Structure to hold active connection info

Definition at line 110 of file miniserver.cpp.

Class Members
SOCKET socket
time_t connect_time

◆ anonymous_namespace{miniserver.cpp}::mserv_request_t

struct anonymous_namespace{miniserver.cpp}::mserv_request_t

miniserver received request message.

This defines the structure of a UPnP request that has income from a remote control point.

Definition at line 70 of file miniserver.cpp.

Class Members
SOCKET sock Connection socket file descriptor.
sockaddr_storage foreign_sockaddr Socket address of the remote control point.

Enumeration Type Documentation

◆ MiniServerState

enum anonymous_namespace{miniserver.cpp}::MiniServerState

miniserver state

Enumerator
MSERV_IDLE 

miniserver is idle.

MSERV_RUNNING 

miniserver is running.

MSERV_STOPPING 

miniserver is running to stop.

Definition at line 78 of file miniserver.cpp.

Function Documentation

◆ active_connection_cmp()

static int anonymous_namespace{miniserver.cpp}::active_connection_cmp ( void *  first,
void *  second 
)
static

Compare function for active connections, return true if equal (found)

Definition at line 118 of file miniserver.cpp.

+ Here is the caller graph for this function:

◆ add_active_connection()

static void anonymous_namespace{miniserver.cpp}::add_active_connection ( SOCKET  sock)
static

Add a socket to the active connections list.

Definition at line 130 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove_active_connection()

static void anonymous_namespace{miniserver.cpp}::remove_active_connection ( SOCKET  sock)
static

Remove a socket from the active connections list.

Definition at line 158 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ shutdown_all_active_connections()

void anonymous_namespace{miniserver.cpp}::shutdown_all_active_connections ( void  )

Shutdown all active socket connections.

Definition at line 183 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ host_header_is_numeric()

int anonymous_namespace{miniserver.cpp}::host_header_is_numeric ( char *  a_host_port,
size_t  a_host_port_len 
)

Check if a network address is numeric.

An empty netaddress or an unspecified one ("[::]", "0.0.0.0") is not valid.

Parameters
a_host_portnetwork address
a_host_port_lenlength of a_host_port excl. terminating '\0'.

Definition at line 224 of file miniserver.cpp.

+ Here is the caller graph for this function:

◆ getNumericHostRedirection()

int anonymous_namespace{miniserver.cpp}::getNumericHostRedirection ( SOCKET  a_socket,
char *  a_host_port,
size_t  a_hp_size 
)

Returns the ip address with port as text that is bound to a socket.

Example: may return "[2001:db8::ab]:50044" or "192.168.1.2:54321".

Returns
On success: true
On error: false, The result buffer remains unmodified.
Parameters
[in]a_socketSocket file descriptor.
[in,out]a_host_portPointer to buffer that will be filled.
[in]a_hp_sizesize of the buffer.

Definition at line 251 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dispatch_request()

int anonymous_namespace{miniserver.cpp}::dispatch_request ( SOCKINFO a_info,
http_parser_t a_hparser 
)

Based on the type of message, appropriate callback is issued.

Returns
On success: 0
On error: HTTP_INTERNAL_SERVER_ERROR if Callback is NULL.
Parameters
[in]a_infoSocket Information object.
[in]a_hparserHTTP parser object.

Definition at line 278 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_handle_request_arg()

void anonymous_namespace{miniserver.cpp}::free_handle_request_arg ( void *  args)

Free memory assigned for handling request and unitialize socket functionality.

Parameters
[in]argsRequest Message to be freed.

Definition at line 382 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_request()

void anonymous_namespace{miniserver.cpp}::handle_request ( void *  args)

Receive the request and dispatch it for handling.

Parameters
[in]argsPointer to Received Request Message to be handled. It's expected to be mserv_request_t*

Definition at line 398 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ schedule_request_job()

void anonymous_namespace{miniserver.cpp}::schedule_request_job ( SOCKET  a_sock,
UPnPsdk::SSockaddr clientAddr 
)
inline

Initilize the thread pool to handle an incomming request, sets priority for the job and adds the job to the thread pool.

Parameters
[in]a_sockSocket Descriptor on which connection is accepted.
[in]clientAddrCtrlpnt address object.

Definition at line 468 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fdset_if_valid()

void anonymous_namespace{miniserver.cpp}::fdset_if_valid ( SOCKET  a_sock,
fd_set *  a_set 
)

Add a socket file descriptor to an 'fd_set' structure as needed for ::select().

a_set may already contain file descriptors. The given a_sock is added to the set. It is ensured that ::select() is not fed with invalid socket file descriptors, in particular with the EBADF error. That could mean: closed socket, or an other network error was detected before adding to the set. It checks that we do not use closed or unbind sockets. It also has a guard that we do not exceed the maximum number FD_SETSIZE (1024) of selectable file descriptors, as noted in "man select".

Returns

  • Nothing. Not good, but needed for compatibility.
    You can check if a_sock was added to a_set. There are messages to stderr if verbose logging is enabled.
Parameters
[in]a_socksocket file descriptor.
[out]a_setPointer to an 'fd_set' structure as needed for ::select(). The structure is modified as documented for ::select().

Definition at line 531 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ web_server_accept()

int anonymous_namespace{miniserver.cpp}::web_server_accept ( SOCKET  listen_sock,
fd_set &  set 
)

Accept requested connection from a remote control point and run it in a new thread.

Returns
On success: UPNP_E_SUCCESS
On error:
  • UPNP_E_NO_WEB_SERVER
  • UPNP_E_SOCKET_ERROR
  • UPNP_E_SOCKET_ACCEPT
Parameters
[in]listen_sockFile descriptor of socket that is listening on incomming requests.
[out]setReference to a file descriptor set as needed for ::select().

Definition at line 580 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ssdp_read()

void anonymous_namespace{miniserver.cpp}::ssdp_read ( SOCKET *  rsock,
fd_set *  set 
)

Read data from the SSDP socket.

Parameters
[in]rsockPointer to a Socket file descriptor.
[in]setPointer to a file descriptor set as needed for ::select().

Definition at line 636 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ receive_from_stopSock()

int anonymous_namespace{miniserver.cpp}::receive_from_stopSock ( SOCKET  ssock,
fd_set *  set 
)

Check if we have received a packet that shall stop the miniserver.

The received datagram must exactly match the shutdown_str from 127.0.0.1. This is a security issue to avoid that the UPnPlib can be terminated from a remote ip address. Receiving 0 bytes on a datagram (there's a datagram here) indicates that a zero-length datagram was successful sent. This will not stop the miniserver.

Returns
  • 1 - when the miniserver shall be stopped,
  • 0 - otherwise.
Parameters
[in]ssockSocket file descriptor.
[in]setPointer to a file descriptor set as needed for ::select().

Definition at line 672 of file miniserver.cpp.

+ Here is the caller graph for this function:

◆ RunMiniServer()

void anonymous_namespace{miniserver.cpp}::RunMiniServer ( MiniServerSockArray miniSock)

Run the miniserver.

The MiniServer accepts a new request and schedules a thread to handle the new request. It checks for socket state and invokes appropriate read and shutdown actions for the Miniserver and SSDP sockets. This function itself runs in its own thread.

Attention
The miniSock parameter must be allocated on the heap before calling the function because it is freed by it.
Parameters
[in]miniSockPointer to an Array containing valid sockets associated with different tasks like listen on a local interface for requests from control points or handle ssdp communication to a remote UPnP node.

Definition at line 735 of file miniserver.cpp.

+ Here is the call graph for this function:

◆ RunMiniServer_f()

void anonymous_namespace{miniserver.cpp}::RunMiniServer_f ( MiniServerSockArray miniSock)

Global list to track active socket connections

Definition at line 907 of file miniserver.cpp.

+ Here is the caller graph for this function:

◆ get_port()

int anonymous_namespace{miniserver.cpp}::get_port ( SOCKET  sockfd,
uint16_t *  port 
)

Returns port to which socket, sockfd, is bound.

Returns
On success: 0
On error: -1 with unmodified system error (errno or WSAGetLastError()).
Parameters
[in]sockfdSocket descriptor.
[out]portThe port value if successful, otherwise, untouched.

Definition at line 919 of file miniserver.cpp.

+ Here is the caller graph for this function:

◆ get_miniserver_sockets()

int anonymous_namespace{miniserver.cpp}::get_miniserver_sockets ( MiniServerSockArray out,
in_port_t  listen_port4,
in_port_t  listen_port6,
in_port_t  listen_port6UlaGua 
)

Create STREAM sockets, binds to local network interfaces and listens for incoming connections.

This is part of starting the miniserver and only available with the webserver compiled in.

Returns
On success: UPNP_E_SUCCESS
On error:
  • UPNP_E_OUTOF_SOCKET - Failed to create a socket.
  • UPNP_E_SOCKET_BIND - Bind() failed.
  • UPNP_E_LISTEN - Listen() failed.
  • UPNP_E_INTERNAL_ERROR - Port returned by the socket layer is < 0.
Parameters
[out]outPointer to a Socket Array that following members will be filled:
  • .miniServerSock6
  • .miniServerPort6
  • .miniServerSock6UlaGua
  • .miniServerPort6UlaGua
  • .miniServerSock4
  • .miniServerPort4
[in]listen_port4port on which the UPnP Device shall listen for incoming IPv4 connections. If 0 then an available port number from the operating system is returned in out.
[in]listen_port6port on which the UPnP Device shall listen for incoming IPv6 LLA connections. If 0 then an available port number from the operating system is returned in out.
[in]listen_port6UlaGuaport on which the UPnP Device shall listen for incoming IPv6 GUA connections. If 0 then an available port number from the operating system is returned in ***out**.

Definition at line 963 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_miniserver_stopsock()

int anonymous_namespace{miniserver.cpp}::get_miniserver_stopsock ( MiniServerSockArray out)

Creates the miniserver STOP socket, usable to listen for stopping the miniserver.

This datagram socket is created and bound to "localhost". It does not listen now but will later be used to listen on to know when it is time to stop the Miniserver.

Returns
On success: UPNP_E_SUCCESS
On error:
  • UPNP_E_OUTOF_SOCKET: Failed to create a socket.
  • UPNP_E_SOCKET_BIND: Bind() failed.
  • UPNP_E_INTERNAL_ERROR: Port returned by the socket layer is < 0.
Parameters
[out]outFills the socket file descriptor and the port it is bound to into the structure.

Definition at line 1076 of file miniserver.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ InitMiniServerSockArray()

void anonymous_namespace{miniserver.cpp}::InitMiniServerSockArray ( MiniServerSockArray miniSocket)

Initialize a miniserver Socket Array.

Parameters
miniSocketPointer to a miniserver Socket Array.

Definition at line 1124 of file miniserver.cpp.

+ Here is the caller graph for this function:

Variable Documentation

◆ miniStopSockPort

in_port_t anonymous_namespace{miniserver.cpp}::miniStopSockPort

Port of the stop socket.

With starting the miniserver there is also this port registered. Its socket is listing for a "ShutDown" message from a local network address (localhost). This is used to stop the miniserver from another thread.

Definition at line 89 of file miniserver.cpp.

◆ gMServState

MiniServerState anonymous_namespace{miniserver.cpp}::gMServState {MSERV_IDLE}

miniserver state

Definition at line 92 of file miniserver.cpp.

◆ gSoapCallback

MiniServerCallback anonymous_namespace{miniserver.cpp}::gSoapCallback {nullptr}

SOAP callback.

Definition at line 97 of file miniserver.cpp.

◆ gGenaCallback

MiniServerCallback anonymous_namespace{miniserver.cpp}::gGenaCallback {nullptr}

GENA callback.

Definition at line 99 of file miniserver.cpp.

◆ gActiveConnections

LinkedList anonymous_namespace{miniserver.cpp}::gActiveConnections
static

Global list to track active socket connections

Definition at line 105 of file miniserver.cpp.

◆ gActiveConnectionsMutex

pthread_mutex_t anonymous_namespace{miniserver.cpp}::gActiveConnectionsMutex
static

Global list to track active socket connections

Definition at line 106 of file miniserver.cpp.

◆ gActiveConnectionsInitialized

int anonymous_namespace{miniserver.cpp}::gActiveConnectionsInitialized = 0
static

Global list to track active socket connections

Definition at line 107 of file miniserver.cpp.