daemon.c File Reference

A minimal-HTTP server library. More...

#include "platform.h"
#include "internal.h"
#include "response.h"
#include "connection.h"
#include "memorypool.h"
#include "connection_https.h"
#include "gnutls_int.h"
#include "gnutls_global.h"
#include <poll.h>
Include dependency graph for daemon.c:

Go to the source code of this file.

Defines

#define MHD_MAX_CONNECTIONS_DEFAULT   FD_SETSIZE -4
#define MHD_POOL_SIZE_DEFAULT   (32 * 1024)
#define DEBUG_CLOSE   MHD_NO
#define DEBUG_CONNECT   MHD_NO
#define ATTRIBUTE_CONSTRUCTOR
#define ATTRIBUTE_DESTRUCTOR

Typedefs

typedef void(* VfprintfFunctionPointerType )(void *, const char *, va_list)

Functions

static void mhd_panic_std (void *cls, const char *file, unsigned int line, const char *reason)
static struct MHD_DaemonMHD_get_master (struct MHD_Daemon *daemon)
static void MHD_ip_count_lock (struct MHD_Daemon *daemon)
static void MHD_ip_count_unlock (struct MHD_Daemon *daemon)
static int MHD_ip_addr_compare (const void *a1, const void *a2)
static int MHD_ip_addr_to_key (struct sockaddr *addr, socklen_t addrlen, struct MHD_IPCount *key)
static int MHD_ip_limit_add (struct MHD_Daemon *daemon, struct sockaddr *addr, socklen_t addrlen)
static void MHD_ip_limit_del (struct MHD_Daemon *daemon, struct sockaddr *addr, socklen_t addrlen)
static int _set_priority (MHD_gtls_priority_st *st, const int *list)
static ssize_t recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i)
static ssize_t send_tls_adapter (struct MHD_Connection *connection, const void *other, size_t i)
static int MHD_init_daemon_certificate (struct MHD_Daemon *daemon)
static int MHD_TLS_init (struct MHD_Daemon *daemon)
int MHD_get_fdset (struct MHD_Daemon *daemon, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *except_fd_set, int *max_fd)
static void * MHD_handle_connection (void *data)
static ssize_t recv_param_adapter (struct MHD_Connection *connection, void *other, size_t i)
static ssize_t send_param_adapter (struct MHD_Connection *connection, const void *other, size_t i)
static int MHD_accept_connection (struct MHD_Daemon *daemon)
static void MHD_cleanup_connections (struct MHD_Daemon *daemon)
int MHD_get_timeout (struct MHD_Daemon *daemon, unsigned long long *timeout)
static int MHD_select (struct MHD_Daemon *daemon, int may_block)
static int MHD_poll (struct MHD_Daemon *daemon)
int MHD_run (struct MHD_Daemon *daemon)
static void * MHD_select_thread (void *cls)
struct MHD_DaemonMHD_start_daemon (unsigned int options, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls,...)
static int parse_options_va (struct MHD_Daemon *daemon, const struct sockaddr **servaddr, va_list ap)
static int parse_options (struct MHD_Daemon *daemon, const struct sockaddr **servaddr,...)
struct MHD_DaemonMHD_start_daemon_va (unsigned int options, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls, va_list ap)
static void MHD_close_connections (struct MHD_Daemon *daemon)
void MHD_stop_daemon (struct MHD_Daemon *daemon)
union MHD_DaemonInfoMHD_get_daemon_info (struct MHD_Daemon *daemon, enum MHD_DaemonInfoType infoType,...)
void MHD_set_panic_func (MHD_PanicCallback cb, void *cls)
const char * MHD_get_version (void)
static void sigalrmHandler (int sig)
void ATTRIBUTE_CONSTRUCTOR MHD_init ()
void ATTRIBUTE_DESTRUCTOR MHD_fini ()

Variables

MHD_PanicCallback mhd_panic
void * mhd_panic_cls
pthread_mutex_t MHD_gnutls_init_mutex
static struct sigaction sig
static struct sigaction old

Detailed Description

A minimal-HTTP server library.

Author:
Daniel Pittman
Christian Grothoff

Definition in file daemon.c.


Define Documentation

#define ATTRIBUTE_CONSTRUCTOR

Definition at line 1955 of file daemon.c.

#define ATTRIBUTE_DESTRUCTOR

Definition at line 1956 of file daemon.c.

#define DEBUG_CLOSE   MHD_NO

Print extra messages with reasons for closing sockets? (only adds non-error messages).

Definition at line 61 of file daemon.c.

#define DEBUG_CONNECT   MHD_NO

Print extra messages when establishing connections? (only adds non-error messages).

Definition at line 67 of file daemon.c.

#define MHD_MAX_CONNECTIONS_DEFAULT   FD_SETSIZE -4

Default connection limit.

Definition at line 47 of file daemon.c.

Referenced by MHD_start_daemon_va().

#define MHD_POOL_SIZE_DEFAULT   (32 * 1024)

Default memory allowed per connection.

Definition at line 55 of file daemon.c.

Referenced by MHD_start_daemon_va().


Typedef Documentation

typedef void(* VfprintfFunctionPointerType)(void *, const char *, va_list)

Definition at line 1163 of file daemon.c.


Function Documentation

static int _set_priority ( MHD_gtls_priority_st st,
const int *  list 
) [static]

Note: code duplication with code in MHD_gnutls_priority.c

Returns:
0

Definition at line 342 of file daemon.c.

References MAX_ALGOS, MHD_gtls_priority_st::num_algorithms, and MHD_gtls_priority_st::priority.

Referenced by parse_options_va().

Here is the caller graph for this function:

static int MHD_accept_connection ( struct MHD_Daemon daemon  )  [static]
static void MHD_cleanup_connections ( struct MHD_Daemon daemon  )  [static]

Free resources associated with all closed connections. (destroy responses, free buffers, etc.). A connection is known to be closed if the socket_fd is -1.

Definition at line 853 of file daemon.c.

References MHD_Connection::addr, MHD_Connection::addr_len, MHD_Daemon::connections, MHD_Connection::daemon, MHD_Daemon::max_connections, MHD__gnutls_deinit(), MHD_destroy_response(), MHD_ip_limit_del(), MHD_pool_destroy(), MHD_USE_THREAD_PER_CONNECTION, MHD_Connection::next, MHD_Daemon::options, MHD_Connection::pid, MHD_Connection::pool, MHD_Connection::response, MHD_Daemon::shutdown, MHD_Connection::socket_fd, STRERROR, and MHD_Connection::tls_session.

Referenced by MHD_close_connections(), MHD_run(), and MHD_select_thread().

Here is the call graph for this function:

Here is the caller graph for this function:

static void MHD_close_connections ( struct MHD_Daemon daemon  )  [static]

Close all connections for the daemon

Definition at line 1758 of file daemon.c.

References MHD_Daemon::connections, MHD_cleanup_connections(), MHD_connection_close(), MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN, and MHD_Connection::socket_fd.

Referenced by MHD_stop_daemon().

Here is the call graph for this function:

Here is the caller graph for this function:

void ATTRIBUTE_DESTRUCTOR MHD_fini (  ) 

Definition at line 1985 of file daemon.c.

References MHD__gnutls_global_deinit(), MHD_gnutls_init_mutex, mhd_panic, mhd_panic_cls, old, and sig.

Here is the call graph for this function:

union MHD_DaemonInfo* MHD_get_daemon_info ( struct MHD_Daemon daemon,
enum MHD_DaemonInfoType  infoType,
  ... 
) [write]

Obtain information about the given daemon (not fully implemented!).

Parameters:
daemon what daemon to get information about
infoType what information is desired?
... depends on infoType
Returns:
NULL if this information is not available (or if the infoType is unknown)

Definition at line 1895 of file daemon.c.

References MHD_DAEMON_INFO_LISTEN_FD, and MHD_Daemon::socket_fd.

int MHD_get_fdset ( struct MHD_Daemon daemon,
fd_set *  read_fd_set,
fd_set *  write_fd_set,
fd_set *  except_fd_set,
int *  max_fd 
)

Obtain the select sets for this daemon.

Returns:
MHD_YES on success, MHD_NO if this daemon was not started with the right options for this call.

Definition at line 447 of file daemon.c.

References MHD_Daemon::connections, MHD_connection_get_fdset(), MHD_NO, MHD_USE_POLL, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Connection::next, MHD_Daemon::options, MHD_Daemon::shutdown, and MHD_Daemon::socket_fd.

Referenced by MHD_select().

Here is the call graph for this function:

Here is the caller graph for this function:

static struct MHD_Daemon* MHD_get_master ( struct MHD_Daemon daemon  )  [static, read]

Trace up to and return master daemon. If the supplied daemon is a master, then return the daemon itself.

Definition at line 114 of file daemon.c.

References MHD_Daemon::master.

Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().

Here is the caller graph for this function:

int MHD_get_timeout ( struct MHD_Daemon daemon,
unsigned long long *  timeout 
)

Obtain timeout value for select for this daemon (only needed if connection timeout is used). The returned value is how long select should at most block, not the timeout value set for connections.

Parameters:
timeout set to the timeout (in milliseconds)
Returns:
MHD_YES on success, MHD_NO if timeouts are not used (or no connections exist that would necessiate the use of a timeout right now).

Definition at line 917 of file daemon.c.

References MHD_Daemon::connection_timeout, MHD_Daemon::connections, MHD_Connection::last_activity, MHD_NO, MHD_YES, and MHD_Connection::next.

Referenced by MHD_select().

Here is the caller graph for this function:

const char* MHD_get_version ( void   ) 

Obtain the version of this library

Returns:
static version string, e.g. "0.4.1"

Definition at line 1934 of file daemon.c.

static void* MHD_handle_connection ( void *  data  )  [static]
void ATTRIBUTE_CONSTRUCTOR MHD_init (  ) 

Initialize the signal handler for SIGALRM and do other setup work.

Definition at line 1963 of file daemon.c.

References MHD__gnutls_global_init(), MHD_gnutls_init_mutex, mhd_panic, mhd_panic_cls, mhd_panic_std(), old, sig, and sigalrmHandler().

Here is the call graph for this function:

static int MHD_init_daemon_certificate ( struct MHD_Daemon daemon  )  [static]

Read and setup our certificate and key.

Returns:
0 on success

Definition at line 390 of file daemon.c.

References MHD_gnutls_datum_t::data, GNUTLS_X509_FMT_PEM, MHD_Daemon::https_mem_cert, MHD_Daemon::https_mem_key, MHD__gnutls_certificate_set_x509_key_mem(), MHD_gnutls_datum_t::size, and MHD_Daemon::x509_cred.

Referenced by MHD_TLS_init().

Here is the call graph for this function:

Here is the caller graph for this function:

static int MHD_ip_addr_compare ( const void *  a1,
const void *  a2 
) [static]

Tree comparison function for IP addresses (supplied to tsearch() family). We compare everything in the struct up through the beginning of the 'count' field.

Definition at line 173 of file daemon.c.

Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().

Here is the caller graph for this function:

static int MHD_ip_addr_to_key ( struct sockaddr *  addr,
socklen_t  addrlen,
struct MHD_IPCount *  key 
) [static]

Parse address and initialize 'key' using the address. Returns MHD_YES on success and MHD_NO otherwise (e.g., invalid address type).

Definition at line 183 of file daemon.c.

References MHD_NO, and MHD_YES.

Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().

Here is the caller graph for this function:

static void MHD_ip_count_lock ( struct MHD_Daemon daemon  )  [static]

Lock shared structure for IP connection counts

Definition at line 141 of file daemon.c.

References MHD_Daemon::per_ip_connection_mutex.

Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().

Here is the caller graph for this function:

static void MHD_ip_count_unlock ( struct MHD_Daemon daemon  )  [static]

Unlock shared structure for IP connection counts

Definition at line 156 of file daemon.c.

References MHD_Daemon::per_ip_connection_mutex.

Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().

Here is the caller graph for this function:

static int MHD_ip_limit_add ( struct MHD_Daemon daemon,
struct sockaddr *  addr,
socklen_t  addrlen 
) [static]

Check if IP address is over its limit.

Returns:
Return MHD_YES if IP below limit, MHD_NO if IP has surpassed limit. Also returns MHD_NO if fails to allocate memory.

Definition at line 219 of file daemon.c.

References MHD_get_master(), MHD_ip_addr_compare(), MHD_ip_addr_to_key(), MHD_ip_count_lock(), MHD_ip_count_unlock(), MHD_NO, MHD_YES, MHD_Daemon::per_ip_connection_count, MHD_Daemon::per_ip_connection_limit, and TSEARCH.

Referenced by MHD_accept_connection().

Here is the call graph for this function:

Here is the caller graph for this function:

static void MHD_ip_limit_del ( struct MHD_Daemon daemon,
struct sockaddr *  addr,
socklen_t  addrlen 
) [static]

Decrement connection count for IP address, removing from table count reaches 0

Definition at line 279 of file daemon.c.

References MHD_get_master(), MHD_ip_addr_compare(), MHD_ip_addr_to_key(), MHD_ip_count_lock(), MHD_ip_count_unlock(), MHD_NO, MHD_Daemon::per_ip_connection_count, MHD_Daemon::per_ip_connection_limit, TDELETE, and TFIND.

Referenced by MHD_accept_connection(), and MHD_cleanup_connections().

Here is the call graph for this function:

Here is the caller graph for this function:

static void mhd_panic_std ( void *  cls,
const char *  file,
unsigned int  line,
const char *  reason 
) [static]

Default implementation of the panic function

Definition at line 90 of file daemon.c.

Referenced by MHD_init().

Here is the caller graph for this function:

static int MHD_poll ( struct MHD_Daemon daemon  )  [static]

Poll for new connection. Used only with THREAD_PER_CONNECTION

Definition at line 1062 of file daemon.c.

References MHD_accept_connection(), MHD_NO, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Daemon::options, MHD_Daemon::shutdown, MHD_Daemon::socket_fd, and STRERROR.

Referenced by MHD_select_thread().

Here is the call graph for this function:

Here is the caller graph for this function:

int MHD_run ( struct MHD_Daemon daemon  ) 

Run webserver operations (without blocking unless in client callbacks). This method should be called by clients in combination with MHD_get_fdset if the client-controlled select method is used.

Returns:
MHD_YES on success, MHD_NO if this daemon was not started with the right options for this call.

Definition at line 1105 of file daemon.c.

References MHD_cleanup_connections(), MHD_NO, MHD_select(), MHD_USE_SELECT_INTERNALLY, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Daemon::options, and MHD_Daemon::shutdown.

Here is the call graph for this function:

static int MHD_select ( struct MHD_Daemon daemon,
int  may_block 
) [static]

Main select call.

Parameters:
may_block YES if blocking, NO if non-blocking
Returns:
MHD_NO on serious errors, MHD_YES on success

Definition at line 953 of file daemon.c.

References MHD_Daemon::connections, MHD_Connection::idle_handler, MHD_Daemon::max_connections, MHD_accept_connection(), MHD_get_fdset(), MHD_get_timeout(), MHD_NO, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Connection::next, MHD_Daemon::options, MHD_Connection::read_handler, SELECT, MHD_Daemon::shutdown, MHD_Connection::socket_fd, MHD_Daemon::socket_fd, STRERROR, and MHD_Connection::write_handler.

Referenced by MHD_run(), and MHD_select_thread().

Here is the call graph for this function:

Here is the caller graph for this function:

static void* MHD_select_thread ( void *  cls  )  [static]

Thread that runs the select loop until the daemon is explicitly shut down.

Definition at line 1121 of file daemon.c.

References MHD_cleanup_connections(), MHD_NO, MHD_poll(), MHD_select(), MHD_USE_POLL, MHD_YES, MHD_Daemon::options, and MHD_Daemon::shutdown.

Referenced by MHD_start_daemon_va().

Here is the call graph for this function:

Here is the caller graph for this function:

void MHD_set_panic_func ( MHD_PanicCallback  cb,
void *  cls 
)

Sets the global error handler to a different implementation. "cb" will only be called in the case of typically fatal, serious internal consistency issues. These issues should only arise in the case of serious memory corruption or similar problems with the architecture. While "cb" is allowed to return and MHD will then try to continue, this is never safe.

The default implementation that is used if no panic function is set simply calls "abort". Alternative implementations might call "exit" or other similar functions.

Parameters:
cb new error handler
cls passed to error handler

Definition at line 1922 of file daemon.c.

References mhd_panic, and mhd_panic_cls.

struct MHD_Daemon* MHD_start_daemon ( unsigned int  options,
unsigned short  port,
MHD_AcceptPolicyCallback  apc,
void *  apc_cls,
MHD_AccessHandlerCallback  dh,
void *  dh_cls,
  ... 
) [read]

Start a webserver on the given port.

Parameters:
port port to bind to
apc callback to call to check which clients will be allowed to connect
apc_cls extra argument to apc
dh default handler for all URIs
dh_cls extra argument to dh
Returns:
NULL on error, handle to daemon on success

Definition at line 1147 of file daemon.c.

References MHD_start_daemon_va().

Here is the call graph for this function:

struct MHD_Daemon* MHD_start_daemon_va ( unsigned int  options,
unsigned short  port,
MHD_AcceptPolicyCallback  apc,
void *  apc_cls,
MHD_AccessHandlerCallback  dh,
void *  dh_cls,
va_list  ap 
) [read]
void MHD_stop_daemon ( struct MHD_Daemon daemon  ) 
static int MHD_TLS_init ( struct MHD_Daemon daemon  )  [static]

Initialize security aspects of the HTTPS daemon

Returns:
0 on success

Definition at line 419 of file daemon.c.

References MHD_Daemon::cred_type, GNUTLS_E_MEMORY_ERROR, MHD__gnutls_certificate_allocate_credentials(), MHD_GNUTLS_CRD_CERTIFICATE, MHD_init_daemon_certificate(), and MHD_Daemon::x509_cred.

Referenced by MHD_start_daemon_va().

Here is the call graph for this function:

Here is the caller graph for this function:

static int parse_options ( struct MHD_Daemon daemon,
const struct sockaddr **  servaddr,
  ... 
) [static]

Parse a list of options given as varargs.

Parameters:
daemon the daemon to initialize
... the options
Returns:
MHD_YES on success, MHD_NO on error

Definition at line 1187 of file daemon.c.

References parse_options_va().

Referenced by parse_options_va().

Here is the call graph for this function:

Here is the caller graph for this function:

static int parse_options_va ( struct MHD_Daemon daemon,
const struct sockaddr **  servaddr,
va_list  ap 
) [static]
static ssize_t recv_param_adapter ( struct MHD_Connection connection,
void *  other,
size_t  i 
) [static]

Callback for receiving data from the socket.

Parameters:
conn the MHD connection structure
other where to write received data to
i maximum size of other (in bytes)
Returns:
number of bytes actually received

Definition at line 612 of file daemon.c.

References MHD_Connection::daemon, MHD_USE_SSL, MHD_Daemon::options, RECV, and MHD_Connection::socket_fd.

Referenced by MHD_accept_connection().

Here is the caller graph for this function:

static ssize_t recv_tls_adapter ( struct MHD_Connection connection,
void *  other,
size_t  i 
) [static]

Callback for receiving data from the socket.

Parameters:
conn the MHD connection structure
other where to write received data to
i maximum size of other (in bytes)
Returns:
number of bytes actually received

Definition at line 363 of file daemon.c.

References MHD__gnutls_record_recv(), and MHD_Connection::tls_session.

Referenced by MHD_accept_connection().

Here is the call graph for this function:

Here is the caller graph for this function:

static ssize_t send_param_adapter ( struct MHD_Connection connection,
const void *  other,
size_t  i 
) [static]

Callback for writing data to the socket.

Parameters:
conn the MHD connection structure
other data to write
i number of bytes to write
Returns:
actual number of bytes written

Definition at line 631 of file daemon.c.

References MHD_Connection::daemon, MHD_USE_SSL, MHD_Daemon::options, SEND, and MHD_Connection::socket_fd.

Referenced by MHD_accept_connection().

Here is the caller graph for this function:

static ssize_t send_tls_adapter ( struct MHD_Connection connection,
const void *  other,
size_t  i 
) [static]

Callback for writing data to the socket.

Parameters:
conn the MHD connection structure
other data to write
i number of bytes to write
Returns:
actual number of bytes written

Definition at line 377 of file daemon.c.

References MHD__gnutls_record_send(), and MHD_Connection::tls_session.

Referenced by MHD_accept_connection().

Here is the call graph for this function:

Here is the caller graph for this function:

static void sigalrmHandler ( int  sig  )  [static]

Definition at line 1946 of file daemon.c.

Referenced by MHD_init().

Here is the caller graph for this function:


Variable Documentation

pthread_mutex_t MHD_gnutls_init_mutex

Definition at line 334 of file daemon.c.

Referenced by MHD_fini(), MHD_init(), MHD_start_daemon_va(), and MHD_stop_daemon().

struct sigaction old [static]

Definition at line 1943 of file daemon.c.

Referenced by MHD_fini(), MHD_gtls_bin2hex(), MHD_init(), and parse_cookie_header().

struct sigaction sig [static]

Definition at line 1941 of file daemon.c.

Referenced by MHD_fini(), MHD_gtls_proc_cert_client_cert_vrfy(), and MHD_init().

Generated by  doxygen 1.6.2-20100208