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

## 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 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. More...
static int | get_port (SOCKET sockfd, uint16_t *port)
| Returns port to which socket, sockfd, is bound. More...
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. More...
static UPNP_INLINE void | InitMiniServerSockArray (MiniServerSockArray *miniSocket)
int | StartMiniServer (uint16_t *listen_port4, uint16_t *listen_port6)
| Initialize the sockets functionality for the Miniserver. More...
int | StopMiniServer ()
| Stop and Shutdown the MiniServer and free socket resources. More...

## 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:

- 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

enum MiniServerState

Enumerator
MSERV_IDLE |
MSERV_RUNNING |
MSERV_STOPPING |

## Function Documentation

## get_miniserver_stopsock()

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] | out | Miniserver Socket Array.

## get_port()

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] | sockfd | Socket descriptor.
  [out] | port | The port value if successful, otherwise, untouched.

References UpnpPrintf().

## RunMiniServer()

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] | miniSock | Socket Array.

## 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_port4 | Port on which the server listens for incoming IPv4 connections.
  [in,out] | listen_port6 | Port on which the server listens for incoming IPv6 connections.

References gMServState, MSERV_IDLE, UPNP_E_INTERNAL_ERROR, and UPNP_E_OUTOF_MEMORY.

## StopMiniServer()

int StopMiniServer | ( |  | ) |

Stop and Shutdown the MiniServer and free socket resources.

Returns
  Always returns 0.

## Variable Documentation

## gMServState

MiniServerState gMServState = MSERV_IDLE

static

module vars

Referenced by StartMiniServer().

## miniStopSockPort

uint16_t miniStopSockPort