solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1
6#ifndef SOCKET_H
7#define SOCKET_H
8
9#ifndef _POSIX_C_SOURCE
10#define _POSIX_C_SOURCE 200112L
11#endif
12
13// for SO_REUSEPORT
14#ifndef _GNU_SOURCE
15#define _GNU_SOURCE
16#endif
17
18#include <stdbool.h>
19#include <stddef.h>
20#include <stdio.h>
21
22#if defined(__cplusplus)
23extern "C" {
24#endif
25
26#ifdef _WIN32
27#include <winsock2.h> // Must be first on Windows
28// Add commen to disable formatting messing up order
29#include <mswsock.h>
30#include <windows.h> // Include after winsock2.h
31#include <ws2tcpip.h>
32#else
33#include <arpa/inet.h>
34#include <fcntl.h>
35#include <netinet/in.h>
36#include <sys/socket.h>
37#include <sys/types.h>
38#include <unistd.h>
39#endif
40
41#include "platform.h"
42
43// Socket abstraction that works on both Windows and Unix
44// On windows, you need to call initialize_winsock() before using any socket
45// functions and cleanup_winsock() after you are done using the socket
46// functions.
47typedef struct {
48#ifdef _WIN32
49 SOCKET handle;
50#else
51 int handle;
52#endif
53} Socket;
54
55// Initialize winsock on Windows. Does nothing on Unix.
56void socket_initialize(void);
57
58// Cleanup winsock on Windows. Does nothing on Unix.
59void socket_cleanup(void);
60
61// Create a socket, returns NULL on error.
62Socket* socket_create(int domain, int type, int protocol);
63
64// Close a socket and free the memory.
65int socket_close(Socket* sock);
66
67// Bind a socket to an address.
68// Returns 0 on success, -1 on error.
69int socket_bind(Socket* sock, const struct sockaddr* addr, socklen_t addrlen);
70
71// Listen for incoming connections. The backlog parameter is the maximum length
72// of the queue of pending connections. If the queue is full, the client will
73// receive an ECONNREFUSED error. Returns 0 on success, -1 on error.
74int socket_listen(Socket* sock, int backlog);
75
76// Accept an incoming connection
77Socket* socket_accept(Socket* sock, struct sockaddr* addr, socklen_t* addrlen);
78
79// Connect to a remote socket
80int socket_connect(Socket* sock, const struct sockaddr* addr, socklen_t addrlen);
81
82// Read from a socket
83ssize_t socket_recv(Socket* sock, void* buffer, size_t size, int flags);
84
85// Write to a socket
86ssize_t socket_send(Socket* sock, const void* buffer, size_t size, int flags);
87
88// Get the socket file descriptor
89int socket_fd(Socket* sock);
90
91// Get the socket error. Returns the last error code.
92// On Windows, it returns the code from WSAGetLastError().
93// On Unix, it returns the value of errno.
94int socket_error(void);
95
96// Get the socket error message for the given error code
97// as returned by socket_error().
98void socket_strerror(int err, char* buffer, size_t size);
99
100// Get the socket option
101int socket_get_option(Socket* sock, int level, int optname, void* optval, socklen_t* optlen);
102
103// Set the socket option (SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))
104int socket_reuse_port(Socket* sock, int enable);
105
106// Set the socket option
107int socket_set_option(Socket* sock, int level, int optname, const void* optval, socklen_t optlen);
108
109// Get the socket address
110int socket_get_address(Socket* sock, struct sockaddr* addr, socklen_t* addrlen);
111
112// Get the socket peer address
113int socket_get_peer_address(Socket* sock, struct sockaddr* addr, socklen_t* addrlen);
114
115// Get the socket family
116int socket_family(Socket* sock);
117
118// Get the socket type
119int socket_type(Socket* sock);
120
121#if defined(__cplusplus)
122}
123#endif
124
125#endif // SOCKET_H
Cross-platform compatibility definitions.