libUPnP  1.6.17
Data Structures | Defines | Enumerations | Functions | Variables
miniserver.c File Reference

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

#include "config.h"
#include "miniserver.h"
#include "httpreadwrite.h"
#include "ithread.h"
#include "ssdplib.h"
#include "statcodes.h"
#include "ThreadPool.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

Defines

#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 lsock, fd_set *set)
static void ssdp_read (SOCKET rsock, fd_set *set)
static int receive_from_stopSock (SOCKET ssock, fd_set *set)
static void RunMiniServer (MiniServerSockArray *miniSock)
 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)
 Initialize the sockets functionality for the Miniserver.
int StopMiniServer ()
 Stop and Shutdown the MiniServer and free socket resources.

Variables

uint16_t miniStopSockPort
static MiniServerState gMServState = MSERV_IDLE

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:


Define Documentation

#define APPLICATION_LISTENING_PORT   49152

Enumeration Type Documentation

Enumerator:
MSERV_IDLE 
MSERV_RUNNING 
MSERV_STOPPING 

Function Documentation

static 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().

static 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 successfull.
Parameters:
[in]sockfdSocket descriptor.
[out]portThe port value if successful, otherwise, untouched.

References UpnpPrintf().

Referenced by get_miniserver_stopsock().

static void RunMiniServer ( MiniServerSockArray miniSock) [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]miniSockSocket Array.

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

Referenced by StartMiniServer().

int StartMiniServer ( uint16_t *  listen_port4,
uint16_t *  listen_port6 
)

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 connections.

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

Referenced by UpnpInitStartServers().

int StopMiniServer ( )

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

module vars

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

uint16_t miniStopSockPort