libcake 0.0.1
Librairie en C
socket.h
Aller à la documentation de ce fichier.
1
7#ifndef __CAKE_SOCKET_H__
8#define __CAKE_SOCKET_H__
9
11#include "def.h"
12#include "strutf8.h"
14
15#ifdef CAKE_WINDOWS
16
18#include <ws2tcpip.h>
20
21typedef int cake_socklen;
22
23typedef SOCKET cake_socket;
24
25#define CAKE_SOCKET_BAD_SOCKET INVALID_SOCKET
26#define CAKE_SOCKET_ERROR SOCKET_ERROR
27
28
29#else
31#include <unistd.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <netdb.h>
37#include <errno.h>
39
40typedef socklen_t cake_socklen;
41
42#define cake_init_winsock()
43#define cake_clean_winsock()
44
45typedef int cake_socket;
46
47#define CAKE_SOCKET_BAD_SOCKET -1
48#define CAKE_SOCKET_ERROR -1
49
50#define cake_socket_get_last_error_code() errno
51
52#define cake_close_socket(sock) close(sock)
53#endif
54
56#include <openssl/ssl.h>
58
59#define CAKE_IP_V4 AF_INET
60#define CAKE_IP_V6 AF_INET6
61
62/* ===== SOCKET ===== */
63
67typedef struct cake_clientsocket {
68 cake_socket socket;
69 struct addrinfo *address;
70 cake_byte errorFrom;
71 ulong errorCode;
73
77typedef struct cake_tlsclient {
79 SSL_CTX *ctx;
80 SSL *ssl;
82
87 cake_socket sock;
88 struct sockaddr_in addr;
89 cake_byte errorFrom;
90 ulong errorCode;
92
96typedef struct cake_serversocket {
97 cake_socket socket;
98 cake_byte errorFrom;
101
102/* ===== STREAM ===== */
103
104typedef struct cake_socketbuffer {
105 cake_byte *ptr;
106 ulonglong size;
108
110 Cake_SocketBuffer buffer;
111 Cake_ClientSocket client;
113
114typedef struct cake_tlsclientstream {
115 Cake_SocketBuffer buffer;
116 Cake_TLSClient tls;
118
120 Cake_SocketBuffer buffer;
123
124#define CAKE_CLIENT_SOCKET_CONNECT_OK 0
125
126#define CAKE_SOCKET_CLOSE 0
127#define CAKE_SOCKET_READ_ERROR SOCKET_ERROR
128
129#define CAKE_SOCKET_ERROR_FROM_NO_ERROR 0
130#define CAKE_SOCKET_ERROR_FROM_GETADDRINFO 1
131#define CAKE_SOCKET_ERROR_FROM_SOCKET 2
132#define CAKE_SOCKET_ERROR_FROM_CONNECT 3
133#define CAKE_SOCKET_ERROR_FROM_BIND 4
134#define CAKE_SOCKET_ERROR_FROM_LISTEN 5
135#define CAKE_SOCKET_ERROR_FROM_ACCEPT 6
136#define CAKE_SOCKET_ERROR_FROM_RECV 7
137#define CAKE_SOCKET_ERROR_FROM_SEND 8
138#define CAKE_SOCKET_ERROR_FROM_SSL_SET_FD 9
139#define CAKE_SOCKET_ERROR_FROM_SSL_CONNECT 10
140#define CAKE_SOCKET_ERROR_FROM_SSL_WRITE_EX 11
141#define CAKE_SOCKET_ERROR_FROM_SSL_READ_EX 12
142
143#define CAKE_SOCKET_READ_BUFFER_SIZE 2048
144
145#ifdef __cplusplus
146extern "C" {
147#endif
148
149#ifdef CAKE_WINDOWS
150inline int cake_init_winsock() {
151 WSADATA win;
152 return WSAStartup(MAKEWORD(2, 2), &win);
153}
154
155inline int cake_clean_winsock() {
156 return WSACleanup();
157}
158
159inline int cake_socket_get_last_error_code() {
160 return WSAGetLastError();
161}
162
163inline int cake_socket_read(cake_socket sock, char *buffer, int size) {
164 return recv(sock, buffer, size, 0);
165}
166
167inline int cake_socket_send(cake_socket sock, char *buffer, int size) {
168 return send(sock, buffer, size, 0);
169}
170
171inline int cake_close_socket(cake_socket sock) {
172 return closesocket(sock);
173}
174#else
175#define cake_init_winsock
176#define cake_clean_winsock
177#endif
178
179inline void cake_init_ssl() {
180 SSL_library_init();
181 ERR_load_crypto_strings();
182 SSL_load_error_strings();
183}
184
185/* ===== CLIENT SOCKET ===== */
186
187cake_bool __cake_client_socket_recv(void *_s, char *_buffer, ulonglong len, ulonglong *_bytesReceived);
188
197cake_bool cake_create_client_socket(Cake_ClientSocket *sock, const char *hostname, const char *port, cake_byte ipMode);
198
205cake_bool cake_client_socket_send(Cake_ClientSocket *sock, const char *data, ulonglong size);
206
212
213/* ===== TLS CLIENT ===== */
214
215cake_bool __cake_tls_client_recv(void *_s, char *_buffer, ulonglong len, ulonglong *_bytesReceived);
216
217cake_bool cake_create_tls_client(Cake_TLSClient *tls, const char *hostname, const char *port, cake_byte ipMode);
218cake_bool cake_tls_client_connect(Cake_TLSClient *tls);
219cake_bool cake_tls_client_send(Cake_TLSClient *tls, const char *data, ulonglong size);
220char *cake_tls_client_recv_dyn(Cake_TLSClient *tls, ulonglong size);
221void cake_free_tls_client(Cake_TLSClient *tls);
222
223/* ===== ACCEPTED CLIENT SOCKET ===== */
224
225cake_bool __cake_accepted_client_socket_recv(void *_s, char *_buffer, ulonglong len, ulonglong *_bytesReceived);
226
227cake_bool cake_accepted_client_socket_send(Cake_AcceptedClientSocket *sock, const char *data, ulonglong size);
228inline void cake_free_accepted_client_socket(Cake_AcceptedClientSocket *client) {
229 cake_close_socket(client->sock);
230}
231
232/* ===== SERVER SOCKET ===== */
233
234cake_bool cake_create_server_socket(Cake_ServerSocket *sock, const char *port, cake_byte ipMode, int backlog);
235
236inline void cake_free_server_socket(Cake_ServerSocket *sock) {
237 cake_close_socket(sock->socket);
238}
239
240cake_bool cake_server_socket_accept(Cake_ServerSocket *sock, Cake_AcceptedClientSocket *dest);
241
242
243/* ===== SOCKET STREAM FUNCTIONS ===== */
244
245Cake_String_UTF8 *__cake_socket_stream_read_line(Cake_SocketBuffer *_stream, cake_bool (*recvFunc)(void *, char *, ulonglong, ulonglong *), void *_s);
246
247cake_bool __cake_tls_client_stream_read_raw(Cake_TLSClientStream *stream, ulonglong len, char *buff);
248
249inline Cake_String_UTF8 *cake_client_socket_stream_read_line(Cake_ClientSocketStream *stream) {
250 return __cake_socket_stream_read_line(&stream->buffer, __cake_client_socket_recv, &stream->client);
251}
252void cake_free_client_socket_stream(Cake_ClientSocketStream *stream);
253
254Cake_String_UTF8 *cake_tls_client_stream_read_line(Cake_TLSClientStream *stream);
255char *cake_tls_client_stream_read_raw(Cake_TLSClientStream *stream, ulonglong len);
256Cake_String_UTF8 *cake_tls_client_stream_read_str(Cake_TLSClientStream *stream, ulonglong len);
257void cake_free_tls_client_stream(Cake_TLSClientStream *stream);
258
259inline Cake_String_UTF8 *cake_accepted_client_socket_stream_read_line(Cake_AcceptedClientSocketStream *stream) {
260 return __cake_socket_stream_read_line(&stream->buffer, __cake_accepted_client_socket_recv, &stream->client);
261}
262void cake_free_accepted_client_socket_stream(Cake_AcceptedClientSocketStream *stream);
263
264#ifdef __cplusplus
265}
266#endif
267
268#endif
Fichier contenant les types utilisés fréquemment par la librairie.
char cake_bool
Type sur 8 bits, utilisé principalement comme valeur de retour des fonctions pour indiquer si une err...
Definition: def.h:126
struct cake_tlsclient Cake_TLSClient
Socket de connexion client utilisant le protocole TLS, sécurisant les communications.
cake_bool cake_client_socket_connect(Cake_ClientSocket *sock)
Permet de connecter un socket client au serveur cible.
Definition: socket.c:162
void cake_free_client_socket(Cake_ClientSocket *sock)
Ferme la connexion d'un socket client.
Definition: socket.c:186
struct cake_serversocket Cake_ServerSocket
Socket de serveur.
struct cake_acceptedclientsocket Cake_AcceptedClientSocket
Socket client accepté par un socket serveur.
struct cake_clientsocket Cake_ClientSocket
Socket de connexion client.
cake_bool cake_create_client_socket(Cake_ClientSocket *sock, const char *hostname, const char *port, cake_byte ipMode)
Crée un socket client sur l'adresse fournie en paramètre.
Definition: socket.c:138
Socket client accepté par un socket serveur.
Definition: socket.h:86
cake_byte errorFrom
Permet de savoir d'où vient l'erreur.
Definition: socket.h:89
ulong errorCode
Code de l'erreur.
Definition: socket.h:90
Definition: socket.h:119
Socket de connexion client.
Definition: socket.h:67
cake_byte errorFrom
Permet de savoir d'où vient l'erreur.
Definition: socket.h:70
ulong errorCode
Code de l'erreur.
Definition: socket.h:71
Definition: socket.h:109
Socket de serveur.
Definition: socket.h:96
cake_byte errorFrom
Permet de savoir d'où vient l'erreur.
Definition: socket.h:98
int errorCode
Code de l'erreur.
Definition: socket.h:99
Definition: socket.h:104
Chaînes de caractères encodées en UTF-8.
Definition: strutf8.h:19
Socket de connexion client utilisant le protocole TLS, sécurisant les communications.
Definition: socket.h:77
Cake_ClientSocket clientSocket
Socket client interne.
Definition: socket.h:78
Definition: socket.h:114
Fichier contenant le prototypes de tout ce qui touche aux chaînes de caractères UTF-8.