-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconnection.c
96 lines (77 loc) · 1.81 KB
/
connection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <common_ssl.h>
#include <connection.h>
connection_t *
get_connection(connections_head_t *head)
{
connection_t *conn;
conn = head->free_connections;
if (NULL == conn) {
goto error;
}
head->free_connections = conn->next;
head->free_connections_n--;
bzero(conn, sizeof(connection_t));
return conn;
error:
return NULL;
}
void
free_connection(connections_head_t *head, connection_t *conn)
{
if (conn->ssl) {
SSL_shutdown(conn->ssl);
SSL_free(conn->ssl);
}
if (conn->fd) close(conn->fd);
conn->next = head->free_connections;
head->free_connections++;
}
void
destroy_connections(connections_head_t *head)
{
size_t i;
if (head) {
if (head->ctx) SSL_CTX_free(head->ctx);
if (head->epfd) close(head->epfd);
if (head->connections) {
for (i = 0; i < head->connections_n; ++i) {
free_connection(head, head->connections + i);
}
free(head->connections);
}
}
}
connections_head_t *
init_connections(size_t size)
{
connections_head_t *head;
connection_t *next;
size_t i;
head = malloc(sizeof(connections_head_t));
if (NULL == head) {
goto error;
}
bzero(head, sizeof(connections_head_t));
head->connections_n = size;
head->connections = calloc(size, sizeof(connection_t));
if (NULL == head->connections) {
goto error;
}
i = size;
next = NULL;
do {
i--;
head->connections[i].next = next;
next = &head->connections[i];
} while(i);
head->free_connections = head->connections;
head->free_connections_n = size;
return head;
error:
destroy_connections(head);
return NULL;
}