miniserver.c File Reference

This file 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 "util.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

void | SetHTTPGetCallback (MiniServerCallback callback)
| Set HTTP Get Callback. More...
void | SetSoapCallback (MiniServerCallback callback)
| Set SOAP Callback. More...
void | SetGenaCallback (MiniServerCallback callback)
| Set GENA Callback. More...
static void | RunMiniServer (MiniServerSockArray *miniSock)
| Run the miniserver. More...
static int | get_port (int sockfd)
| 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...
int | StartMiniServer (unsigned short *listen_port4, unsigned short *listen_port6)
| Initialize the sockets functionality for the Miniserver. More...
int | StopMiniServer ()
| Stop and Shutdown the MiniServer and free socket resources. More...

## Variables

unsigned short | miniStopSockPort
static MiniServerCallback | gGetCallback = NULL
static MiniServerCallback | gSoapCallback = NULL
static MiniServerCallback | gGenaCallback = NULL
static MiniServerState | gMServState = MSERV_IDLE

## Detailed Description

This file 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.

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

References get_port(), UPNP_E_INTERNAL_ERROR, UPNP_E_OUTOF_SOCKET, UPNP_E_SOCKET_BIND, UPNP_E_SUCCESS, and UpnpPrintf().

Referenced by StartMiniServer().

static int get_port | ( | int | sockfd | ) |

static

Returns port to which socket, sockfd, is bound.

Returns
  -1 on error; check errno, otherwise > 0 means port number.

Parameters

  [in] | sockfd | Socket descriptor.

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

References UpnpPrintf().

Referenced by StartMiniServer().

void SetGenaCallback | ( | MiniServerCallback | callback | ) |

Set GENA Callback.

Parameters

  [in] | callback | GENA Callback to be invoked.

Referenced by UpnpInitPreamble().

void SetHTTPGetCallback | ( | MiniServerCallback | callback | ) |

Set HTTP Get Callback.

Parameters

  [in] | callback | HTTP Callback to be invoked .

Referenced by UpnpEnableWebserver().

void SetSoapCallback | ( | MiniServerCallback | callback | ) |

Set SOAP Callback.

Parameters

  [in] | callback | SOAP Callback to be invoked .

Referenced by UpnpInitPreamble().

int StartMiniServer | ( | unsigned short * | listen_port4,
|  | unsigned short * | 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 get_miniserver_stopsock(), gMiniServerThreadPool, RunMiniServer(), 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 UpnpPrintf().

Referenced by UpnpFinish().