libUPnP 1.14.31
miniserver.c File Reference

Implements the functionality and utility functions used by the Miniserver module. More...

#include "config.h"
#include "miniserver.h"
#include "ThreadPool.h"
#include "httpreadwrite.h"
#include "ithread.h"
#include "ssdplib.h"
#include "statcodes.h"
#include "unixutil.h"
#include "upnpapi.h"
#include "upnputil.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
Include dependency graph for miniserver.c:

Data Structures

struct  mserv_request_t
struct  active_connection_t

Macros

#define APPLICATION_LISTENING_PORT   49152

Enumerations

enum  MiniServerState { MSERV_IDLE , MSERV_RUNNING , MSERV_STOPPING }

Functions

static UPNP_INLINE void fdset_if_valid (SOCKET sock, fd_set *set)
static void web_server_accept (SOCKET listen_sock, fd_set *set)
static void ssdp_read (SOCKET *read_sock, fd_set *set)
static int receive_from_stopSock (SOCKET ssock, fd_set *set)
static void RunMiniServer (MiniServerSockArray *mini_sock)
 Run the miniserver.
static int get_port (SOCKET sockfd, uint16_t *port)
 Returns port to which socket, sockfd, is bound.
static int get_miniserver_stopsock (MiniServerSockArray *out)
 Creates the miniserver STOP socket. This socket is created and listened on to know when it is time to stop the Miniserver.
static UPNP_INLINE void InitMiniServerSockArray (MiniServerSockArray *miniSocket)
int StartMiniServer (uint16_t *listen_port4, uint16_t *listen_port6, uint16_t *listen_port6UlaGua)
 Initialize the sockets functionality for the Miniserver.
int StopMiniServer (void)
 Stop and Shutdown the MiniServer and free socket resources.

Variables

static uint16_t miniStopSockPort
static MiniServerState gMServState = MSERV_IDLE
static LinkedList gActiveConnections
static ithread_mutex_t gActiveConnectionsMutex
static int gActiveConnectionsInitialized = 0

Detailed Description

Implements the functionality and utility functions used by the Miniserver module.

The miniserver is a central point for processing all network requests. It is made of:

  • The SSDP sockets for discovery.
  • The HTTP listeners for description / control / eventing.

Macro Definition Documentation

◆ APPLICATION_LISTENING_PORT

#define APPLICATION_LISTENING_PORT   49152

Enumeration Type Documentation

◆ MiniServerState

Enumerator
MSERV_IDLE 
MSERV_RUNNING 
MSERV_STOPPING 

Function Documentation

◆ get_miniserver_stopsock()

int get_miniserver_stopsock ( MiniServerSockArray * out)
static

Creates the miniserver STOP socket. This socket is created and listened on to know when it is time to stop the Miniserver.

Returns
  • 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.
  • UPNP_E_SUCCESS: Success.
Parameters
[in]outMiniserver Socket Array.

References get_port(), INVALID_SOCKET, MServerSockArray::miniServerStopSock, miniStopSockPort, sock_close(), SOCKET_ERROR, UPNP_E_INTERNAL_ERROR, UPNP_E_OUTOF_SOCKET, UPNP_E_SOCKET_BIND, UPNP_E_SUCCESS, and UpnpPrintf().

Referenced by StartMiniServer().

◆ get_port()

int get_port ( SOCKET sockfd,
uint16_t * port )
static

Returns port to which socket, sockfd, is bound.

Returns
-1 on error; check errno. 0 if successful.
Parameters
[in]sockfdSocket descriptor.
[out]portThe port value if successful, otherwise, untouched.

References UpnpPrintf().

Referenced by get_miniserver_stopsock().

◆ RunMiniServer()

void RunMiniServer ( MiniServerSockArray * mini_sock)
static

Run the miniserver.

The MiniServer accepts a new request and schedules a thread to handle the new request. Checks for socket state and invokes appropriate read and shutdown actions for the Miniserver and SSDP sockets.

Parameters
[in]mini_sockSocket Array.

References gMServState, MServerSockArray::miniServerSock4, MServerSockArray::miniServerSock6, MServerSockArray::miniServerSock6UlaGua, MServerSockArray::miniServerStopSock, MSERV_IDLE, MSERV_RUNNING, shutdown_all_active_connections(), sock_close(), SOCKET_ERROR, MServerSockArray::ssdpReqSock4, MServerSockArray::ssdpReqSock6, MServerSockArray::ssdpSock4, MServerSockArray::ssdpSock6, MServerSockArray::ssdpSock6UlaGua, and UpnpPrintf().

Referenced by StartMiniServer().

◆ StartMiniServer()

int StartMiniServer ( uint16_t * listen_port4,
uint16_t * listen_port6,
uint16_t * listen_port6UlaGua )

Initialize the sockets functionality for the Miniserver.

Initialize a thread pool job to run the MiniServer and the job to the thread pool.

If listen port is 0, port is dynamically picked.

Use timer mechanism to start the MiniServer, failure to meet the allowed delay aborts the attempt to launch the MiniServer.

Returns
  • On success: UPNP_E_SUCCESS.
  • On error: UPNP_E_XXX.
Parameters
[in,out]listen_port4Port on which the server listens for incoming IPv4 connections.
[in,out]listen_port6Port on which the server listens for incoming IPv6 LLA connections.
[in,out]listen_port6UlaGuaPort on which the server listens for incoming IPv6 ULA or GUA connections.

References get_miniserver_stopsock(), get_ssdp_sockets(), gMiniServerThreadPool, gMServState, MServerSockArray::miniServerSock4, MServerSockArray::miniServerSock6, MServerSockArray::miniServerSock6UlaGua, MServerSockArray::miniServerStopSock, MSERV_IDLE, MSERV_RUNNING, RunMiniServer(), sock_close(), MServerSockArray::ssdpReqSock4, MServerSockArray::ssdpReqSock6, MServerSockArray::ssdpSock4, MServerSockArray::ssdpSock6, MServerSockArray::ssdpSock6UlaGua, ThreadPoolAddPersistent(), TPJobInit(), TPJobSetFreeFunction(), TPJobSetPriority(), UPNP_E_INTERNAL_ERROR, UPNP_E_OUTOF_MEMORY, and UPNP_E_SUCCESS.

Referenced by UpnpInitStartServers().

◆ StopMiniServer()

int StopMiniServer ( void )

Stop and Shutdown the MiniServer and free socket resources.

Returns
Always returns 0.

References gMServState, INVALID_SOCKET, miniStopSockPort, MSERV_IDLE, MSERV_RUNNING, MSERV_STOPPING, sock_close(), and UpnpPrintf().

Referenced by UpnpFinish().

Variable Documentation

◆ gActiveConnections

LinkedList gActiveConnections
static

Global list to track active socket connections

◆ gMServState

MiniServerState gMServState = MSERV_IDLE
static

module vars

Referenced by RunMiniServer(), StartMiniServer(), and StopMiniServer().

◆ miniStopSockPort

uint16_t miniStopSockPort
static