-
Notifications
You must be signed in to change notification settings - Fork 32
/
port.c
132 lines (118 loc) · 3.73 KB
/
port.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (C) 20016 Christos Tsantilas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*/
#include "common.h"
#include "c-icap.h"
#include "array.h"
#include "port.h"
#ifdef USE_OPENSSL
#include "net_io_ssl.h"
#endif
#include "debug.h"
static int mystrcmp(const char *s1, const char *s2)
{
if (s1 && !s2)
return 1;
if (!s1 && s2)
return -1;
if (!s1 && !s2)
return 0;
return strcmp(s1, s2);
}
static int ci_port_compare_config(ci_port_t *src, ci_port_t *dst)
{
if (dst->port != src->port)
return 0;
if (mystrcmp(dst->address, src->address))
return 0;
/*fd, configured, protocol_family and secs_to_linger are filled by c-icap while port configured*/
if (dst->tls_enabled != src->tls_enabled)
return 0;
return 1;
}
static void ci_port_move_configured(ci_port_t *dst, ci_port_t *src)
{
dst->configured = src->configured;
dst->accept_socket = src->accept_socket;
dst->stat_connections = src->stat_connections;
dst->proto = src->proto;
src->configured = 0;
src->accept_socket = CI_SOCKET_INVALID;
src->stat_connections = -1;
dst->tls_enabled = src->tls_enabled;
#ifdef USE_OPENSSL
dst->tls_accept_details = src->tls_accept_details;
src->tls_accept_details = NULL;
if (src->tls_enabled)
ci_port_reconfigure_tls(dst);
#endif
}
void ci_port_handle_reconfigure(ci_vector_t *new_ports, ci_vector_t *old_ports)
{
int i, k;
ci_port_t *find_port, *check_port, *check_old_port;
for (i = 0; (check_port = (ci_port_t *)ci_vector_get(new_ports, i)) != NULL; ++i) {
for (k = 0, find_port = NULL; (find_port == NULL) && ((check_old_port = (ci_port_t *)ci_vector_get(old_ports, k)) != NULL); ++k ) {
if (ci_port_compare_config(check_port, check_old_port)) {
find_port = check_port;
ci_port_move_configured(check_port, check_old_port);
}
}
if (find_port)
ci_debug_printf(1, "Port %d is already configured\n", find_port->port);
}
}
void ci_port_close(ci_port_t *port)
{
if (!ci_socket_valid(port->accept_socket))
return;
#ifdef USE_OPENSSL
if (port->tls_accept_details)
icap_close_server_tls(port);
else
#endif
close(port->accept_socket);
port->proto = 0;
port->accept_socket = CI_SOCKET_INVALID;
port->configured = 0;
}
void ci_port_list_release(ci_vector_t *ports)
{
int i;
ci_port_t *p;
for (i = 0; (p = (ci_port_t *)ci_vector_get(ports, i)) != NULL; ++i) {
ci_port_close(p);
if (p->address)
free(p->address);
#ifdef USE_OPENSSL
if (p->tls_server_cert)
free(p->tls_server_cert);
if (p->tls_server_key)
free(p->tls_server_key);
if (p->tls_client_ca_certs)
free(p->tls_client_ca_certs);
if (p->tls_cafile)
free(p->tls_cafile);
if (p->tls_capath)
free(p->tls_capath);
if (p->tls_ciphers)
free(p->tls_ciphers);
#endif
}
ci_vector_destroy(ports);
}