pianod2
multisource multiuser scriptable networked music player
Classes | Macros | Typedefs | Enumerations | Functions
fb_service.h File Reference

Football private declarations. More...

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "fb_public.h"
#include "fb_transport.h"
Include dependency graph for fb_service.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

union  fb_socketaddr_t
 Address of opposing end of a connection. More...
 
struct  fb_service_t
 Football service state structure. More...
 
struct  fb_message_t
 Message structure. More...
 
struct  fb_messagelist_t
 Q list structure. More...
 
struct  fb_ioqueue_t
 Queue structure. More...
 
struct  fb_inputbuffer_t
 Connection input structure. More...
 
struct  fb_http_request_t
 Structure which collects HTTP request information as it is read. More...
 
struct  fb_connection_t
 Connection state information. More...
 
struct  fb_iterator_t
 Iterator for connections on a football service. More...
 

Macros

#define FB_WHERE(level)   __FILE__, __LINE__, __func__, (level)
 Use FB_WHERE to send the log type; this macro includes the file, line and function when NDEBUG is not set. More...
 
#define fb_perror(errfunc)   fb_log(FB_WHERE (FB_LOG_ERROR), (errfunc ": %s"), strerror (errno))
 Log a message with a form akin to perror. More...
 
#define fb_mutex_init(mutex)   ((void)0)
 
#define fb_mutex_destroy(mutex)   ((void)0)
 
#define fb_mutex_lock(mutex)   ((void)0)
 
#define fb_mutex_unlock(mutex)   ((void)0)
 
#define fb_http_socket(id)
 
#define fb_ip6_socket(id)
 
#define fb_encrypted_socket(id)   ((id) == FB_SOCKET_HTTPS_IP4 || (id) == FB_SOCKET_HTTPS_IP6)
 
#define FAR_FUTURE   ((time_t) (((time_t) 0x7fffffff) << ((sizeof (time_t) - 4) * 8)))
 

Typedefs

typedef enum fb_log_types_t FB_LOG_TYPE
 
typedef enum fb_socket_type_t FB_SOCKETTYPE
 Magic numbers used to differentiate parameter types sent to fb_fprintf. More...
 
typedef enum fb_socket_state_t FB_SOCKETSTATE
 Connection states. More...
 
typedef enum fb_socketid_t FB_SOCKETID
 
typedef union fb_socketaddr_t FB_SOCKETADDR
 Address of opposing end of a connection. More...
 
typedef struct fb_message_t FB_MESSAGE
 Message structure. More...
 
typedef struct fb_messagelist_t FB_MESSAGELIST
 Q list structure. More...
 
typedef struct fb_ioqueue_t FB_IOQUEUE
 Queue structure. More...
 
typedef struct fb_inputbuffer_t FB_INPUTBUFFER
 Connection input structure. More...
 
typedef struct fb_http_request_t FB_HTTPREQUEST
 Structure which collects HTTP request information as it is read. More...
 

Enumerations

enum  fb_log_types_t {
  FB_LOG_ERROR = 0 , FB_LOG_WARNING = 0x01 , FB_LOG_PARSER = 0x04 , FB_LOG_IO_TRACE = 0x08 ,
  FB_LOG_CONN_STATUS = 0x10 , FB_LOG_CONN_ERROR = 0x20 , FB_LOG_TLS_STATUS = 0x100 , FB_LOG_TLS_ERROR = 0x200 ,
  FB_LOG_HTTP_STATUS = 0x1000 , FB_LOG_HTTP_ERROR = 0x2000 , FB_LOG_HTTP_TRAFFIC = 0x4000
}
 
enum  fb_socket_type_t { FB_SOCKTYPE_SERVICE = 0x3692 , FB_SOCKTYPE_CONNECTION = 0x5285 , FB_SOCKTYPE_USER = 0xa9f7 , FB_SOCKTYPE_EVENT = 0xbd53 }
 Magic numbers used to differentiate parameter types sent to fb_fprintf. More...
 
enum  fb_socket_state_t {
  FB_SOCKET_STATE_TLS_HANDSHAKE , FB_SOCKET_STATE_GREETING , FB_SOCKET_STATE_GATHERING_HEADER , FB_SOCKET_STATE_OPEN ,
  FB_SOCKET_STATE_FLUSHING , FB_SOCKET_STATE_CLOSING
}
 Connection states. More...
 
enum  fb_socketid_t {
  FB_SOCKET_LINE_IP4 , FB_SOCKET_LINE_IP6 , FB_SOCKET_HTTP_IP4 , FB_SOCKET_HTTP_IP6 ,
  FB_SOCKET_HTTPS_IP4 , FB_SOCKET_HTTPS_IP6 , FB_SOCKET_COUNT
}
 

Functions

void fb_free_freelists (void)
 
FB_MESSAGEfb_messagealloc (void)
 
void fb_messagefree (FB_MESSAGE *freethis)
 
FB_MESSAGELISTfb_qalloc ()
 
void fb_qfree (FB_MESSAGELIST *freethis)
 
bool fb_queue_add (FB_IOQUEUE *queue, FB_MESSAGE *message)
 
bool fb_queue_empty (FB_IOQUEUE *q)
 
void fb_queue_consume (FB_IOQUEUE *q, size_t consume)
 
void fb_queue_destroy (FB_IOQUEUE *q)
 
void fb_destroy_service (struct fb_service_t *service)
 
void fb_schedule_reap (FB_SERVICE *service)
 
bool fb_transfer_by_name (FB_CONNECTION *connection, const char *name)
 
void fb_queue_event (FB_EVENT *event)
 
void fb_dispose_event (FB_EVENT *event)
 
bool fb_register (int socket, FB_SOCKETTYPE type, void *thing)
 Add a socket to the registry. More...
 
void fb_unregister (int socket)
 Remove a socket from the registry. More...
 
FB_CONNECTIONfb_accept_connection (FB_SERVICE *service, FB_SOCKETID id)
 
void fb_destroy_connection (FB_CONNECTION *connection)
 
bool fb_set_input_buffer_size (FB_CONNECTION *connection, size_t size)
 
bool fb_recv_input (FB_CONNECTION *connection, ssize_t byte_count)
 
FB_EVENTfb_read_input (FB_EVENT *event, FB_CONNECTION *connection)
 
FB_EVENTfb_new_connect (FB_EVENT *event, FB_SERVICE *service)
 
FB_EVENTfb_send_output (FB_EVENT *event, FB_CONNECTION *connection)
 
FB_EVENTfb_read_websocket_input (FB_EVENT *event, FB_CONNECTION *connection)
 
bool fb_websocket_ping (FB_CONNECTION *connection)
 
bool fb_websocket_announce_close (FB_CONNECTION *connection)
 
bool fb_websocket_encode (FB_CONNECTION *connection)
 
void fb_destroy_httprequest (FB_HTTPREQUEST *request)
 
void fb_collect_http_request (FB_EVENT *event, FB_HTTPREQUEST *request)
 
void fb_collect_http_parameter (char *line, FB_HTTPREQUEST *request)
 
bool fb_http_command (const char *command)
 
FB_EVENTfb_execute_http_request (FB_EVENT *event, FB_CONNECTION *connection)
 
int fb_create_argv (const char *commandline, char ***result)
 
int fb_create_argv_from_query_string (const char *query_string, char ***result, char ***remainders)
 
void fb_destroy_argv (char **argv)
 
const char * fb_connection_info (FB_CONNECTION *connection)
 
void fb_set_buffering (int socket_fd, bool enable)
 
void fb_set_readable (int socket, bool enable)
 
void fb_set_writable (int socket, bool enable)
 

Detailed Description

Football private declarations.

Macro Definition Documentation

◆ FAR_FUTURE

#define FAR_FUTURE   ((time_t) (((time_t) 0x7fffffff) << ((sizeof (time_t) - 4) * 8)))

◆ fb_encrypted_socket

#define fb_encrypted_socket (   id)    ((id) == FB_SOCKET_HTTPS_IP4 || (id) == FB_SOCKET_HTTPS_IP6)

◆ fb_http_socket

#define fb_http_socket (   id)
Value:
((id) == FB_SOCKET_HTTP_IP4 || (id) == FB_SOCKET_HTTP_IP6 || \
@ FB_SOCKET_HTTP_IP6
Definition: fb_service.h:107
@ FB_SOCKET_HTTPS_IP4
Definition: fb_service.h:108
@ FB_SOCKET_HTTPS_IP6
Definition: fb_service.h:109
@ FB_SOCKET_HTTP_IP4
Definition: fb_service.h:106

◆ fb_ip6_socket

#define fb_ip6_socket (   id)
Value:
((id) == FB_SOCKET_LINE_IP6 || (id) == FB_SOCKET_HTTP_IP6 || \
@ FB_SOCKET_LINE_IP6
Definition: fb_service.h:105

◆ fb_mutex_destroy

#define fb_mutex_destroy (   mutex)    ((void)0)

◆ fb_mutex_init

#define fb_mutex_init (   mutex)    ((void)0)

◆ fb_mutex_lock

#define fb_mutex_lock (   mutex)    ((void)0)

◆ fb_mutex_unlock

#define fb_mutex_unlock (   mutex)    ((void)0)

◆ fb_perror

#define fb_perror (   errfunc)    fb_log(FB_WHERE (FB_LOG_ERROR), (errfunc ": %s"), strerror (errno))

Log a message with a form akin to perror.

◆ FB_WHERE

#define FB_WHERE (   level)    __FILE__, __LINE__, __func__, (level)

Use FB_WHERE to send the log type; this macro includes the file, line and function when NDEBUG is not set.

Typedef Documentation

◆ FB_HTTPREQUEST

Structure which collects HTTP request information as it is read.

◆ FB_INPUTBUFFER

Connection input structure.

◆ FB_IOQUEUE

typedef struct fb_ioqueue_t FB_IOQUEUE

Queue structure.

These hold output for a specific connection.

◆ FB_LOG_TYPE

◆ FB_MESSAGE

typedef struct fb_message_t FB_MESSAGE

Message structure.

May appear in multiple connections' queues.

◆ FB_MESSAGELIST

Q list structure.

Per-connection list of its messages.

◆ FB_SOCKETADDR

Address of opposing end of a connection.

◆ FB_SOCKETID

typedef enum fb_socketid_t FB_SOCKETID

◆ FB_SOCKETSTATE

Connection states.

◆ FB_SOCKETTYPE

Magic numbers used to differentiate parameter types sent to fb_fprintf.

Enumeration Type Documentation

◆ fb_log_types_t

Enumerator
FB_LOG_ERROR 
FB_LOG_WARNING 
FB_LOG_PARSER 
FB_LOG_IO_TRACE 
FB_LOG_CONN_STATUS 
FB_LOG_CONN_ERROR 
FB_LOG_TLS_STATUS 
FB_LOG_TLS_ERROR 
FB_LOG_HTTP_STATUS 
FB_LOG_HTTP_ERROR 
FB_LOG_HTTP_TRAFFIC 

◆ fb_socket_state_t

Connection states.

Enumerator
FB_SOCKET_STATE_TLS_HANDSHAKE 

Connection has not yet completed TLS handshake.

FB_SOCKET_STATE_GREETING 

HTTP/TLS connection is awaiting a request.

FB_SOCKET_STATE_GATHERING_HEADER 

HTTP/HTTPS request in process, still reading headering.

FB_SOCKET_STATE_OPEN 

Line-oriented or WebSocket session in process.

FB_SOCKET_STATE_FLUSHING 

Line-oriented or WebSocket session is closing, flushing output.

FB_SOCKET_STATE_CLOSING 

Line-oriented or WebSocket connection has finished flushing.

◆ fb_socket_type_t

Magic numbers used to differentiate parameter types sent to fb_fprintf.

Enumerator
FB_SOCKTYPE_SERVICE 

Random numbers.

FB_SOCKTYPE_CONNECTION 
FB_SOCKTYPE_USER 
FB_SOCKTYPE_EVENT 

◆ fb_socketid_t

Enumerator
FB_SOCKET_LINE_IP4 
FB_SOCKET_LINE_IP6 
FB_SOCKET_HTTP_IP4 
FB_SOCKET_HTTP_IP6 
FB_SOCKET_HTTPS_IP4 
FB_SOCKET_HTTPS_IP6 
FB_SOCKET_COUNT 

Function Documentation

◆ fb_accept_connection()

FB_CONNECTION* fb_accept_connection ( FB_SERVICE service,
FB_SOCKETID  id 
)

Accept a connection.

Parameters
serviceThe service on which a connection is arriving.
idIdentifies the arrival socket (line, HTTP, TLS, IP4 vs IP6)
Returns
a pointer to a new connection, or NULL on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_collect_http_parameter()

void fb_collect_http_parameter ( char *  line,
FB_HTTPREQUEST request 
)

Collect HTTP method parameters that are relevant.

Parameters
linethe HTTP header line received.
requestthe request structure into which details will be stored.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_collect_http_request()

void fb_collect_http_request ( FB_EVENT event,
FB_HTTPREQUEST request 
)

Collect the relevant bits of 'get', 'head' or other HTTP method.

Parameters
eventan event, complete with argv structure.
requestHTTP request structure into which details will be placed.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_connection_info()

const char* fb_connection_info ( FB_CONNECTION connection)

Return a a connection's opposing end as a string.

Parameters
connectionthe connection to get the string of.
Returns
a string with the info, or a stand-in value.
Here is the caller graph for this function:

◆ fb_create_argv()

int fb_create_argv ( const char *  commandline,
char ***  result 
)

Create an argv-style array. A command line with nothing on it results in 0 and a pointer to an array with one null.

Parameters
commandlineThe command to split up. This original string remains unaltered.
resultThe place to put the argv array.
Returns
The number of populated terms in the argv array. On error, returns a negative number and a null pointer.
Here is the caller graph for this function:

◆ fb_create_argv_from_query_string()

int fb_create_argv_from_query_string ( const char *  query_string,
char ***  result_names,
char ***  result_values 
)

Create arrays of parameter names and values from an HTTP query parameter string. An empty query string results in 0 and pointers arrays with one null.

Parameters
query_stringThe command to split up. This original string remains unaltered.
result_namesThe place to put the parameter name array reference.
result_valuesThe place to put the parameter value array reference.
Returns
The number of populated terms in the parameter array. On error, returns a negative number and a null pointer.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_destroy_argv()

void fb_destroy_argv ( char **  argv)

Free up resources used by an argv array.

Parameters
argvThe array to release.
Here is the caller graph for this function:

◆ fb_destroy_connection()

void fb_destroy_connection ( FB_CONNECTION connection)

Close and destroy a connection.

Returns
Nothing.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_destroy_httprequest()

void fb_destroy_httprequest ( FB_HTTPREQUEST request)

Reset a request structure. Frees dynamically allocated elements and clears structure.

Parameters
requestThe request structure to reset.
Here is the caller graph for this function:

◆ fb_destroy_service()

void fb_destroy_service ( FB_SERVICE service)

Destroy a service's resources. Abruptly closes any remaining connections, frees them, closes listener sockets and frees service.

Parameters
servicethe service to terminate.
Returns
Nothing.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_dispose_event()

void fb_dispose_event ( FB_EVENT event)

Destroy an event and free its resources. Events are all statically allocated, so we never free them in the current implementation. However, dispose frees the dynamically allocated elements and clears out the structure.

Parameters
eventthe event to free
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_execute_http_request()

FB_EVENT* fb_execute_http_request ( FB_EVENT event,
FB_CONNECTION connection 
)

Interpret the collected HTTP request. Return an HTTP error if the request is invalid. If it is a WebSocket request, initiate a WebSocket session. Otherwise serve the request and reset the connection.

Parameters
eventa partially filled event applicable to this connection.
connectionthe connection being served.
Returns
An FB_EVENT_CONNECT event if a WebSocket session is initiated, NULL otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_free_freelists()

void fb_free_freelists ( void  )

Free the free message lists

Here is the caller graph for this function:

◆ fb_http_command()

bool fb_http_command ( const char *  command)

Determine if a request is an HTTP one.

Parameters
commandthe HTTP request (GET, HEAD, etc.)
Returns
true if command is an HTTP request, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_messagealloc()

FB_MESSAGE* fb_messagealloc ( void  )

Allocate a message block structure. Get it off the free list if its available, otherwise malloc a new one.

Returns
the message block, or NULL on failure.
Here is the caller graph for this function:

◆ fb_messagefree()

void fb_messagefree ( FB_MESSAGE freethis)

"Free" a message block by putting it on the free list.

Here is the caller graph for this function:

◆ fb_new_connect()

FB_EVENT* fb_new_connect ( FB_EVENT event,
FB_SERVICE service 
)

Accept a new connection and return an event announcing it if applicable.

Parameters
eventa partially filled event applicable to the new connection.
servicethe service on which the connection is arriving.
Returns
FB_EVENT_CONNECTED for successful line-oriented connections, NULL otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_qalloc()

FB_MESSAGELIST* fb_qalloc ( )
Here is the caller graph for this function:

◆ fb_qfree()

void fb_qfree ( FB_MESSAGELIST freethis)

"Free" a single message by putting it on the free list.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_queue_add()

bool fb_queue_add ( FB_IOQUEUE queue,
FB_MESSAGE message 
)

Add a message to a queue. Does not adjust message block use counts.

Parameters
queuethe queue to add to.
messagethe message to add to the queue.
Returns
true on success, false on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_queue_consume()

void fb_queue_consume ( FB_IOQUEUE q,
size_t  consume 
)

Consume bytes at the front of the queue. Bytes consumed must not exceed the remaining bytes in the front message block.

Parameters
qthe queue
consumeThe number of bytes to consume.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_queue_destroy()

void fb_queue_destroy ( FB_IOQUEUE q)

Trash everything in a queue.

Parameters
qthe queue to free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_queue_empty()

bool fb_queue_empty ( FB_IOQUEUE q)

Check if a queue empty is empty.

Returns
true if the queue is empty.
Here is the caller graph for this function:

◆ fb_queue_event()

void fb_queue_event ( FB_EVENT event)

Queue an event. Only used for fallback mode on HTTP port, when we need to deliver a connection arrival and then the command, so size is limited to 1.

Parameters
eventthe event to queue
Here is the caller graph for this function:

◆ fb_read_input()

FB_EVENT* fb_read_input ( FB_EVENT ev,
FB_CONNECTION connection 
)

Read input based on protocol and state. If applicable, returns an event for the input.

  • FB_EVENT_INPUT is returned when there is input
  • FB_EVENT_CONNECT is returned on greeting or WebSocket establishment
    Parameters
    eva partially-filled event applicable to this input
    connectionthe connection from which input is read
    Returns
    an event (if applicable), or NULL.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_read_websocket_input()

FB_EVENT* fb_read_websocket_input ( FB_EVENT event,
FB_CONNECTION connection 
)

Read data from WebSocket connection. Reads a WebSocket packet and decodes it, and returns an event for the payload.

Parameters
eventa partially filled event applicable to the connection.
connectionthe connection to read from.
Returns
an FB_EVENT_INPUT for the message received, or NULL if the packet is incomplete.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_recv_input()

bool fb_recv_input ( FB_CONNECTION connection,
ssize_t  byte_count 
)

Read some bytes directly from a socket or via TLS if encrypted. Handle connection closing and errors.

Parameters
connectionthe connection to read input from.
byte_countthe number of bytes to read
Returns
true if the byte count is met, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_register()

bool fb_register ( int  socket_fd,
FB_SOCKETTYPE  type,
void *  thing 
)

Add a socket to the registry.

Parameters
socket_fdThe file descriptor of the socket being added.
typeType of socket (service, connection, user...)
thingPointer of type indicated by 'type'.
Returns
true on success, false on failure
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_schedule_reap()

void fb_schedule_reap ( FB_SERVICE service)

Schedule a service for reaping when it has no connections left.

Here is the caller graph for this function:

◆ fb_send_output()

FB_EVENT* fb_send_output ( FB_EVENT event,
FB_CONNECTION connection 
)

Write output to a connection from the queue. Events are generated for connection closure only; there are none for writing.

If the socket is in a closing state, when the buffer is empty, close the connection.

Parameters
eventA partially complete event record, or NULL. If NULL, events are delayed (along with moving state toward closure) until a later time, when an event can be returned for those state changes.
connectionThe connection whose queued data is to be sent.
Returns
FB_EVENT_CLOSE or NULL
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_set_buffering()

void fb_set_buffering ( int  socket_fd,
bool  enable 
)

Enable/disable writing on a socket. This is used to remember TLS data buffering.

Parameters
socket_fdthe file descriptor to apply flags to
enabletrue if TLS is buffering, false if it is not.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_set_input_buffer_size()

bool fb_set_input_buffer_size ( FB_CONNECTION connection,
size_t  size 
)

Expand the input buffer to a specified size.

Parameters
connectionThe connection whose buffer size is being set.
sizethe desired size.
Returns
true on success, false on failure.
Here is the caller graph for this function:

◆ fb_set_readable()

void fb_set_readable ( int  socket_fd,
bool  enable 
)

Enable/disable reading on a socket.

Parameters
socket_fdthe file descriptor to apply flags to
enabletrue to enable reads from the socket, false to disable
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_set_writable()

void fb_set_writable ( int  socket_fd,
bool  enable 
)

Enable/disable writing on a socket. This is used within Football, enabled when fresh data is queued for a socket and disabled when the buffer is empty.

Parameters
socket_fdthe file descriptor to apply flags to
enabletrue to enable write-polling on the socket
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_transfer_by_name()

bool fb_transfer_by_name ( FB_CONNECTION connection,
const char *  name 
)

Transfer a connection to a related service.

Parameters
connectionThe connection to transfer.
nameThe name of the service to reassign it to.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_unregister()

void fb_unregister ( int  socket_fd)

Remove a socket from the registry.

Always succeeds, unless there's a bug.

Parameters
socket_fdthe file descriptor to unregister
Here is the caller graph for this function:

◆ fb_websocket_announce_close()

bool fb_websocket_announce_close ( FB_CONNECTION connection)

Send a WebSocket close packet. This should be called as last thing prior to closing the connection.

Parameters
connectionThe connection to write the close to.
Returns
true on success, false on failure.
Here is the caller graph for this function:

◆ fb_websocket_encode()

bool fb_websocket_encode ( FB_CONNECTION connection)

Build WebSocket packets from output. Checks if there's a complete packet (line) in the assembly queue. When there is, assemble a WebSocket packet and put it in the output queue.

Parameters
connectionThe connection to check.
Returns
true on success, false on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_websocket_ping()

bool fb_websocket_ping ( FB_CONNECTION connection)

Send a WebSocket ping packet. Some browsers have timeouts, so we need to send pings as keep-alives.

Parameters
connectionThe connection to check.
Returns
true on success, false on failure.
Here is the call graph for this function:
Here is the caller graph for this function: