A simple socket interface for c applications that supports both, client and server applications.
The interface provides all common socket functionalities such as bind, listen, accept and more.
The server can operate in blocking-single-connection mode (Simple Server) or in non-blocking-multi-connection mode (Multi Server).
The server can operate in IPv4 and IPv6 modes, which, depending on the OS, might overlap (*nix IPv6 Servers will accept IPv4 request, Windows Servers won't).
-
- initialize a Server :
csocket_initServerSocket(domain, type, protocol, addrc, port, csocket_t, specialAddr) - bind the Server :
csocket_bindServer(csocket_t) - set up the listen queue :
csocket_listen(csocket_t, maxQueue) - initialize a server handler :
csocket_setUpMultiServer(csocket_t, maxClient, onActivity, csocket_multiHandler_t) - run Server in a loop :
csocket_multiServer(csocket_multiHandler_t)
- initialize a Server :
-
- initialize a Server :
csocket_initServerSocket(domain, type, protocol, addrc, port, csocket_t, specialAddr) - bind the Server :
csocket_bindServer(csocket_t) - set up the listen queue :
csocket_listen(csocket_t, maxQueue) - accept incoming client :
csocket_accept(csocket_t, csocket_activity_t)
- initialize a Server :
-
Windows:
-
Download and install(adding the path variable) the most recent WinLibs UCRT runtime and MinGW64(UCRT)
-
Compile the code statically using gcc:
gcc.exe -o <filename>.exe <filename>.c -l:libcsocket.lib -lws2_32 -L<libpath> -I<libpath>
-
-
*nix:
-
Verify the Dependencies
-
Compile the code statically using gcc:
gcc -o <filename>.o <filename>.c -l:libcsocket.a -lws2_32 -L<libpath> -I<libpath>
-
-
Windows:
winsock2.h Ws2tcpip.h stdio.h io.h time.h
-
*nix:
arpa/inet.h sys/socket.h time.h fcntl.h string.h stdlib.h stdio.h unistd.h errno.h
// empty structure initializer
#define CSOCKET_EMPTY {0}// type that holds the csocket
typedef struct csocket {
// sys/socket fields
int domain;
int type;
int protocol;
// server/client
struct csocket_mode mode;
// keepalive (global settings)
struct csocket_keepalive *ka;
// error string
char last_err[32];
} csocket_t;// placeholder with set size to hold server or client information
struct csocket_mode {
// common
int fd;
struct sockaddr *addr;
socklen_t addr_len;
int data[2];
int sc;
};// structure that holds server information -- can be stored in a mode structure
struct csocket_server {
// server socket
int server_fd;
// bind
struct sockaddr *addr;
socklen_t addr_len;
// listen
int backlog;
// accept
int client_fd;
int sc;
};// structure that holds client information -- can be stored in a mode structure
struct csocket_client {
// client socket
int client_fd;
// server addr
struct sockaddr *addr;
socklen_t addr_len;
int zero[1];
int sc;
};// type to hold address information
typedef struct csocket_addr {
int domain;
struct sockaddr *addr;
socklen_t addr_len;
} csocket_addr_t;// structure that holds client information
struct csocket_clients {
// incoming socket
int fd;
int domain;
struct sockaddr *addr;
socklen_t addr_len;
time_t connection_time;
// keepalive
struct csocket_keepalive *ka;
// manual shutdown
char shutdown;
};// default keepalive timeout
#define CSKA_TIMEOUT 120
// default keepalive message
#define CSKA_DEFAULTMSG "CSKA%UNIX%-%HOST%-%USER%\0"
#define CSKA_DEFAULTMSGLEN 25// type to hold keepalive information
typedef struct csocket_keepalive {
int enabled;
// keepalive timeout in seconds
int timeout;
// keepalive buffer = RCVBUF
char *buffer;
socklen_t buffer_len;
socklen_t buffer_usage;
// keepalive message and length
char *msg;
size_t msg_len;
/**
* keepalive message types:
* 1 - custom message stored in msg with length msg_len
* other values: CSKA_DEFAULTMSG
*
* variables for custom messages:
* - %UNIX% -> current time in unix format
* - %HOST% -> hostname of the current machine
* - %USER% -> username of the current machine
**/
int msg_type;
// resolved paramters
char *params;
socklen_t params_len;
socklen_t params_usage;
// last keepalive timestamp
time_t last_sig;
// handler function
void (*onActivity)(struct csocket_keepalive *);
// file descriptor and address information
int fd;
csocket_addr_t address;
// socket connection timestamp
time_t connection_time;
} csocket_keepalive_t;// type that holds multiHandler information and all the managed clients
typedef struct csocket_multiHandler {
// socket
csocket_t *src_socket;
// handler function
void (*onActivity)(struct csocket_multiHandler *, csocket_activity_t);
// client stash
int maxClients;
struct csocket_clients *client_sockets;
} csocket_multiHandler_t;// Activity Types
#define CSACT_TYPE_CONN 1 // Connected
#define CSACT_TYPE_DISCONN 2 // Disconnected
#define CSACT_TYPE_READ 4 // Read Available
#define CSACT_TYPE_WRITE 8 // Write Available
#define CSACT_TYPE_EXT 16 // Extra Available
#define CSACT_TYPE_DECLINED 32 // Connection Declined -> usually due to maxClients// type to hold an activity
typedef struct csocket_activity {
// incoming socket/client handle
struct csocket_clients client_socket;
// action type
int type;
// timestamps
time_t time;
time_t update_time;
} csocket_activity_t;-
csocket_setAddress(struct sockaddr **out_addr, socklen_t *addr_len, int domain, void *addrc, int port, int specialAddr)description params return -
description params return -
csocket_initServerSocket(int domain, int type, int protocol, void *addrc, int port, csocket_t *src_socket, int specialAddr)description params return -
csocket_initClientSocket(int domain, int type, int protocol, void *addrc, int port, csocket_t *src_socket, int specialAddr)description params return
-
description params return -
description params return -
description params return -
description params return -
description params return -
description params return
-
description params return -
description params return -
description params return -
description params return -
description params return -
description params return -
description params return -
description params return -
description params return -
description params return -
description params return
-
csocket_keepalive_create(int timeout, char *msg, size_t msg_len, csocket_keepalive_t *ka, csocket_t *src_socket)description params return -
csocket_keepalive_modify(int timeout, char *msg, size_t msg_len, csocket_keepalive_t *ka, csocket_t *src_socket)description params return -
description params return -
description params return -
description params return -
description params return -
description params return -
description Resolves the replacement string of a specified variable and writes the relative positioning to the referenced variable offset.params pointer to a char buffer that holds the variable char *src, size of the buffersize_t size, relative positioningsize_t *offsetreturn char *- On success, returns pointer to replacement string on heap, otherwise NULL. -
description Resolves a Query queryfrom the keepalive variables and places it in thedstbuffer of sizedst_len.params pointer to destination buffer char *dst, size of the buffersize_t *dst_len, pointer to string querychar *query, pointer to a keepalive typecsocket_keepalive_t *kareturn int- On success return 0, otherwise -1. -
description Updates keepalive info for connectedsockets.params pointer to a keepalive type csocket_keepalive_t *ka, socket fdint fdreturn int- On success return 0, otherwise -1. -
description Updates keepalive info for connectionlesssockets.params pointer to a keepalive type csocket_keepalive_t *ka, socket fdint fd, pointer to an address typecsocket_addr_t *dst_addrreturn int- On success return 0, otherwise -1. -
description Writes the keepalive type to the specified FILE. params FILE FILE *fp, pointer to a keepalive typecsocket_keepalive_t *kareturn void
-
description Bind a name to a csocket. params pointer to a csocket csocket_t *src_socketreturn int- On success return 0, otherwise return -1 and set the last error in last_err. -
description Listen for connections on csocket with a provided Queue size. params pointer to a csocket csocket_t *src_socket, Queue sizeint maxQueuereturn int- On success return 0, otherwise return -1 and set the last error in last_err. -
description Block code execution until a client connects and set the corresponding activity flags. params pointer to a csocket csocket_t *src_socket, pointer to an activity typecsocket_activity_t *activityreturn int- On success, return 0, otherwise return -1 and set the last error in last_err. -
csocket_setUpMultiServer(csocket_t *src_socket, int maxClient, void (*onActivity)(csocket_multiHandler_t *, csocket_activity_t), csocket_multiHandler_t *handler)description Creates a multiHandler handlerthat can be used in csocket_multiServer().params pointer to a csocket csocket_t *src_socket, Size of client bufferint maxClient, pointer to an activity handlervoid (*onActivity)(csocket_multiHandler_t *, csocket_activity_t), pointer to a multiHandler typecsocket_multiHAndler_t *handlerreturn int- On success, return 0, otherwise return -1 and set the last error in last_err. -
description Runs the multiServer. params pointer to a mutliHandler type csocket_multiHandler_t *handlerreturn int- On success, return 0, otherwise return -1 and set the last error in last_err. -
description Marks Client clientfor shutdown. Ifclientis NULL, marks all connected clients for shutdown.params pointer to a multiHandler type csocket_multiHandler_t *handler, pointer to a clients structstruct csocket_clients *clientreturn int- On success, return n > 0 number of marked clients, otherwise return <= 0.
-
description Connects a csocket. If timeout is not NULL, return after max. timeout. params pointer to a csocket csocket_t *src_socket, pointer to a timeval structurestruct timeval *timeoutreturn int- On success, return 0, otherwise return -1 and set the last error in last_err.
-
description Automatically calls the arpa/inet.h function ntop with appropriate address casting by providing the domain. params address family int domain, pointer to a network address structureconst void *addr, pointer to a output bufferchar *dst, size of the provided buffersocklen_t lenreturn const char *- On success, return pointer to dst, otherwise NULL.alias CSOCKET_NTOP(domain, addr, str, strlen)
-
description Frees by csocket allocated heap memory. params pointer to a csocket csocket_t *src_socketreturn void -
description Frees by activity type allocated heap memory. params pointer to an activity type csocket_activity_t *activityreturn void -
description Frees by multiHander type allocated heap memory. params pointer to a multiHandler type csocket_multiHandler_t *handlerreturn void -
description Frees by clients structure allocated heap memory. params pointer to a clients structure struct csocket_clients *clientreturn void -
description Frees by keepalive allocated heap memory. params pointer to a keepalive type csocket_keepalive_t *kareturn void
-
description Closes the connection and frees the socket/csocket. params pointer to a csocket csocket *src_socketreturn void