c-template
socket.h File Reference

TCP socket servers, clients, and tooling for working with sockets. More...

#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>
#include "../../utils/logger.h"
Include dependency graph for socket.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  socket_client
 

Typedefs

typedef struct addrinfo addr_info
 
typedef struct sockaddr sock_addr
 
typedef struct sockaddr_storage sock_addr_storage
 
typedef struct socket_client socket_client
 

Enumerations

enum  SOCKET_OPTS { REUSEADDR, NOBLOCK, BLOCK }
 used to configure new sockets More...
 

Functions

socket_clientnew_socket_client (thread_logger *thl, addr_info hints, char *addr, char *port)
 returns a new socket client connected to addr:port More...
 
int get_new_socket (thread_logger *thl, addr_info *bind_address, SOCKET_OPTS sock_opts[], int num_opts)
 gets an available socket attached to bind_address More...
 
void setup_signal_handling ()
 prepares library for usage More...
 
bool set_socket_blocking_status (int fd, bool blocking)
 used to enable/disable blocking sockets More...
 
void signal_handler_fn (int signal_number)
 callback function used to handle OS signals shouldn't be called directly and instead used as the func in signal(SIGTERM, handler_fn) More...
 
char * get_name_info (sock_addr *client_address)
 returns the address the client is connecting from More...
 
addr_info default_hints ()
 generates an addr_info struct with defaults defaults is IPv4, TCP, and AI_PASSIVE flags More...
 

Variables

pthread_mutex_t _signal_mutex
 used to lock writes for _do_exit More...
 
bool _do_exit
 used to indicate when threads need to cleanup More...
 

Detailed Description

TCP socket servers, clients, and tooling for working with sockets.

Author
Bonedaddy

uses wait_group.h to provide lightweight synchronization between pthreads

Warning
before use you must call setup_signal_handling() so that all threads get properly cleaned up on exit
Note
you will want to adjust async_handle_conn_func to suit your needs as right now it is just an echo client it is likely you will need to have #define _POSIX_C_SOURCE 201112L see the following for more information

Definition in file socket.h.

Typedef Documentation

◆ addr_info

Definition at line 60 of file socket.h.

◆ sock_addr

Definition at line 66 of file socket.h.

◆ sock_addr_storage

Definition at line 72 of file socket.h.

◆ socket_client

Enumeration Type Documentation

◆ SOCKET_OPTS

used to configure new sockets

Enumerator
REUSEADDR 

sets socket with SO_REUSEADDR

NOBLOCK 

sets socket to non-blocking mode

BLOCK 

sets socket to blocking mode

Definition at line 77 of file socket.h.

Function Documentation

◆ default_hints()

addr_info default_hints ( )

generates an addr_info struct with defaults defaults is IPv4, TCP, and AI_PASSIVE flags

◆ get_name_info()

char* get_name_info ( sock_addr client_address)

returns the address the client is connecting from

◆ get_new_socket()

int get_new_socket ( thread_logger thl,
addr_info bind_address,
SOCKET_OPTS  sock_opts[],
int  num_opts 
)

gets an available socket attached to bind_address

Returns
Success: file descriptor socket number greater than 0
Failure: -1 initializers a socket attached to bind_address with sock_opts, and binds the address

◆ new_socket_client()

socket_client* new_socket_client ( thread_logger thl,
addr_info  hints,
char *  addr,
char *  port 
)

returns a new socket client connected to addr:port

◆ set_socket_blocking_status()

bool set_socket_blocking_status ( int  fd,
bool  blocking 
)

used to enable/disable blocking sockets

Returns
Failure: false
Success: true
Note
see https://stackoverflow.com/questions/1543466/how-do-i-change-a-tcp-socket-to-be-non-blocking/1549344#1549344

◆ setup_signal_handling()

void setup_signal_handling ( )

prepares library for usage

Warning
must be called before using the library sets up internal mutex, and system signal handling for terminating the server listes to SIGINT, SIGTERM, and SIGQUIT which will terminate the server

◆ signal_handler_fn()

void signal_handler_fn ( int  signal_number)

callback function used to handle OS signals shouldn't be called directly and instead used as the func in signal(SIGTERM, handler_fn)

Variable Documentation

◆ _do_exit

bool _do_exit

used to indicate when threads need to cleanup

Warning
should not be called libraries using socket.c polled at the beginning of every async_listen_func loop and async_handle_conn_func when set to true causes active pthreads to exit after successfully cleaning up

Definition at line 52 of file socket.h.

◆ _signal_mutex

pthread_mutex_t _signal_mutex

used to lock writes for _do_exit

Warning
should not be called libraries using socket.c

Definition at line 46 of file socket.h.