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"
Go to the source code of this file.
|
socket_client * | new_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...
|
|
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.
◆ addr_info
◆ sock_addr
◆ sock_addr_storage
◆ socket_client
◆ 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.
◆ 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()
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()
returns a new socket client connected to addr:port
◆ set_socket_blocking_status()
bool set_socket_blocking_status |
( |
int |
fd, |
|
|
bool |
blocking |
|
) |
| |
◆ 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)
◆ _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.