Skip to content

Commit dac9734

Browse files
proto_tls/wss: complete fix in commit b6b7520
1 parent 0f4e738 commit dac9734

File tree

6 files changed

+256
-120
lines changed

6 files changed

+256
-120
lines changed

modules/proto_tls/proto_tls.c

+14-33
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,20 @@ static mi_response_t *tls_trace_mi_1(const mi_params_t *params,
139139

140140
trace_dest t_dst;
141141

142-
#ifndef NO_SSL_GLOBAL_LOCK
143-
gen_lock_t *ssl_global_lock;
144-
#else
145-
#define ssl_global_lock NULL
146-
#endif
147-
148142
static int w_tls_blocking_write(struct tcp_connection *c, int fd, const char *buf,
149143
size_t len)
150144
{
151145
int ret;
152146

153147
lock_get(&c->write_lock);
154148
ret = tls_blocking_write(c, fd, buf, len,
155-
tls_handshake_tout, tls_send_tout, t_dst, ssl_global_lock);
149+
tls_handshake_tout, tls_send_tout, t_dst, &tls_mgm_api);
156150
lock_release(&c->write_lock);
157151
return ret;
158152
}
159153

160154
static int tls_write_on_socket(struct tcp_connection* c, int fd,
161-
char *buf, int len, gen_lock_t *ssl_global_lock)
155+
char *buf, int len)
162156
{
163157
int n;
164158

@@ -174,7 +168,7 @@ static int tls_write_on_socket(struct tcp_connection* c, int fd,
174168
goto release;
175169
}
176170

177-
n = tls_write(c, fd, buf, len, NULL, ssl_global_lock);
171+
n = tls_write(c, fd, buf, len, NULL, &tls_mgm_api);
178172
if (n >= 0 && len - n) {
179173
/* if could not write entire buffer, delay it */
180174
n = tcp_async_add_chunk(c, buf + n, len - n, 0);
@@ -184,7 +178,7 @@ static int tls_write_on_socket(struct tcp_connection* c, int fd,
184178
}
185179
} else {
186180
n = tls_blocking_write(c, fd, buf, len,
187-
tls_handshake_tout, tls_send_tout, t_dst, ssl_global_lock);
181+
tls_handshake_tout, tls_send_tout, t_dst, &tls_mgm_api);
188182
}
189183
release:
190184
lock_release(&c->write_lock);
@@ -334,14 +328,6 @@ static int mod_init(void)
334328
sroutes->request, RT_NO);
335329
}
336330

337-
#ifndef NO_SSL_GLOBAL_LOCK
338-
ssl_global_lock = lock_alloc();
339-
if (!ssl_global_lock || !lock_init(ssl_global_lock)) {
340-
LM_ERR("could not initialize openssl lock!\n");
341-
return -1;
342-
}
343-
#endif
344-
345331
return 0;
346332
}
347333

@@ -351,11 +337,6 @@ static int mod_init(void)
351337
*/
352338
static void mod_destroy(void)
353339
{
354-
#ifndef NO_SSL_GLOBAL_LOCK
355-
lock_destroy(ssl_global_lock);
356-
lock_dealloc(ssl_global_lock);
357-
#endif
358-
359340
/* library destroy */
360341
ERR_free_strings();
361342
/*SSL_free_comp_methods(); - this function is not on std. openssl*/
@@ -459,7 +440,7 @@ static void proto_tls_conn_clean(struct tcp_connection* c)
459440
c->proto_data = NULL;
460441
}
461442

462-
tls_conn_clean(c, ssl_global_lock, &tls_mgm_api);
443+
tls_conn_clean(c, &tls_mgm_api);
463444
}
464445

465446

@@ -557,7 +538,7 @@ static int proto_tls_send(struct socket_info* send_sock,
557538
* connect status */
558539
tls_update_fd(c, fd);
559540
n = tls_async_connect(c, fd, tls_async_handshake_connect_timeout, t_dst,
560-
ssl_global_lock);
541+
&tls_mgm_api);
561542
lock_release(&c->write_lock);
562543
if (n<0) {
563544
LM_ERR("failed async TLS connect\n");
@@ -601,7 +582,7 @@ static int proto_tls_send(struct socket_info* send_sock,
601582
send_it:
602583
LM_DBG("sending via fd %d...\n",fd);
603584

604-
rlen = tls_write_on_socket(c, fd, buf, len, ssl_global_lock);
585+
rlen = tls_write_on_socket(c, fd, buf, len);
605586
tcp_conn_set_lifetime( c, tcp_con_lifetime);
606587

607588
LM_DBG("after write: c=%p n=%d fd=%d\n",c, rlen, fd);
@@ -655,7 +636,7 @@ static int tls_read_req(struct tcp_connection* con, int* bytes_read)
655636
}
656637

657638
/* do this trick in order to trace whether if it's an error or not */
658-
ret=tls_fix_read_conn(con, tls_handshake_tout, t_dst, ssl_global_lock);
639+
ret=tls_fix_read_conn(con, tls_handshake_tout, t_dst, &tls_mgm_api);
659640
if (ret < 0) {
660641
LM_ERR("failed to do pre-tls handshake!\n");
661642
return -1;
@@ -697,7 +678,7 @@ static int tls_read_req(struct tcp_connection* con, int* bytes_read)
697678
if (req->parsed<req->pos){
698679
bytes=0;
699680
}else{
700-
bytes=tls_read(con,req, ssl_global_lock);
681+
bytes=tls_read(con,req, &tls_mgm_api);
701682
if (bytes<0) {
702683
LM_ERR("failed to read \n");
703684
goto error;
@@ -761,7 +742,7 @@ static int tls_async_write(struct tcp_connection* con, int fd)
761742
SSL *ssl = (SSL *)con->extra_data;
762743

763744
err = tls_fix_read_conn_unlocked(con, fd, tls_handshake_tout, t_dst,
764-
ssl_global_lock);
745+
&tls_mgm_api);
765746
if (err < 0) {
766747
LM_ERR("failed to do pre-tls handshake!\n");
767748
return -1;
@@ -776,7 +757,7 @@ static int tls_async_write(struct tcp_connection* con, int fd)
776757
chunk->len, chunk, con, chunk->ticks, get_ticks());
777758

778759
#ifndef NO_SSL_GLOBAL_LOCK
779-
lock_get(ssl_global_lock);
760+
tls_mgm_api.global_lock_get();
780761
#endif
781762

782763
n=SSL_write(con->extra_data, chunk->buf, chunk->len);
@@ -785,15 +766,15 @@ static int tls_async_write(struct tcp_connection* con, int fd)
785766
switch (err) {
786767
case SSL_ERROR_ZERO_RETURN:
787768
#ifndef NO_SSL_GLOBAL_LOCK
788-
lock_release(ssl_global_lock);
769+
tls_mgm_api.global_lock_release();
789770
#endif
790771

791772
LM_DBG("connection closed cleanly\n");
792773
return -1;
793774
case SSL_ERROR_WANT_READ:
794775
case SSL_ERROR_WANT_WRITE:
795776
#ifndef NO_SSL_GLOBAL_LOCK
796-
lock_release(ssl_global_lock);
777+
tls_mgm_api.global_lock_release();
797778
#endif
798779

799780
LM_DBG("Can't finish to write chunk %p on conn %p\n",
@@ -806,7 +787,7 @@ static int tls_async_write(struct tcp_connection* con, int fd)
806787
tls_print_errstack();
807788

808789
#ifndef NO_SSL_GLOBAL_LOCK
809-
lock_release(ssl_global_lock);
790+
tls_mgm_api.global_lock_release();
810791
#endif
811792

812793
/* report the conn as broken */

modules/proto_wss/proto_wss.c

+6-29
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,11 @@ static str wss_resource = str_init("/");
7373
static int wss_raw_writev(struct tcp_connection *c, int fd,
7474
const struct iovec *iov, int iovcnt, int tout);
7575

76-
#ifndef NO_SSL_GLOBAL_LOCK
77-
gen_lock_t *ssl_global_lock;
78-
#else
79-
#define ssl_global_lock NULL
80-
#endif
81-
8276
#define _ws_common_module "wss"
8377
#define _ws_common_tcp_current_req tcp_current_req
8478
#define _ws_common_current_req wss_current_req
8579
#define _ws_common_max_msg_chunks wss_max_msg_chunks
86-
#define _ws_common_read(c, r) tls_read((c), (r), ssl_global_lock)
80+
#define _ws_common_read(c, r) tls_read((c), (r), &tls_mgm_api)
8781
#define _ws_common_writev wss_raw_writev
8882
#define _ws_common_read_tout wss_hs_read_tout
8983
/*
@@ -111,7 +105,6 @@ static int trace_filter_route_id = -1;
111105
/**/
112106

113107
static int mod_init(void);
114-
static void mod_destroy(void);
115108
static int proto_wss_init(struct proto_info *pi);
116109
static int proto_wss_init_listener(struct socket_info *si);
117110
static int proto_wss_send(struct socket_info* send_sock,
@@ -187,7 +180,7 @@ struct module_exports exports = {
187180
0, /* module pre-initialization function */
188181
mod_init, /* module initialization function */
189182
0, /* response function */
190-
mod_destroy,/* destroy function */
183+
0, /* destroy function */
191184
0, /* per-child init function */
192185
0 /* reload confirm function */
193186
};
@@ -262,25 +255,9 @@ static int mod_init(void)
262255
sroutes->request, RT_NO);
263256
}
264257

265-
#ifndef NO_SSL_GLOBAL_LOCK
266-
ssl_global_lock = lock_alloc();
267-
if (!ssl_global_lock || !lock_init(ssl_global_lock)) {
268-
LM_ERR("could not initialize openssl lock!\n");
269-
return -1;
270-
}
271-
#endif
272-
273258
return 0;
274259
}
275260

276-
static void mod_destroy(void)
277-
{
278-
#ifndef NO_SSL_GLOBAL_LOCK
279-
lock_destroy(ssl_global_lock);
280-
lock_dealloc(ssl_global_lock);
281-
#endif
282-
}
283-
284261
static int wss_conn_init(struct tcp_connection* c)
285262
{
286263
struct ws_data *d;
@@ -343,7 +320,7 @@ static void ws_conn_clean(struct tcp_connection* c)
343320

344321
}
345322

346-
tls_conn_clean(c, ssl_global_lock, &tls_mgm_api);
323+
tls_conn_clean(c, &tls_mgm_api);
347324
}
348325

349326

@@ -511,7 +488,7 @@ static int wss_read_req(struct tcp_connection* con, int* bytes_read)
511488
struct ws_data* d;
512489

513490
/* we need to fix the SSL connection before doing anything */
514-
if (tls_fix_read_conn(con, 0, t_dst, ssl_global_lock) < 0) {
491+
if (tls_fix_read_conn(con, 0, t_dst, &tls_mgm_api) < 0) {
515492
LM_ERR("cannot fix read connection\n");
516493
if ( (d=con->proto_data) && d->dest && d->tprot ) {
517494
if ( d->message ) {
@@ -581,7 +558,7 @@ static int wss_raw_writev(struct tcp_connection *c, int fd,
581558
lock_get(&c->write_lock);
582559
for (i = 0; i < iovcnt; i++) {
583560
n = tls_blocking_write(c, fd, iov[i].iov_base, iov[i].iov_len,
584-
wss_hs_tls_tout, wss_send_tout, t_dst, ssl_global_lock);
561+
wss_hs_tls_tout, wss_send_tout, t_dst, &tls_mgm_api);
585562
if (n < 0) {
586563
ret = -1;
587564
goto end;
@@ -604,7 +581,7 @@ static int wss_raw_writev(struct tcp_connection *c, int fd,
604581
}
605582
lock_get(&c->write_lock);
606583
n = tls_blocking_write(c, fd, buf, n,
607-
wss_hs_tls_tout, wss_send_tout, t_dst, ssl_global_lock);
584+
wss_hs_tls_tout, wss_send_tout, t_dst, &tls_mgm_api);
608585
#endif /* TLS_DONT_WRITE_FRAGMENTS */
609586

610587
end:

modules/tls_mgm/api.h

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ typedef struct tls_domain * (*tls_find_server_domain_f) (struct ip_addr *, unsig
4343
typedef struct tls_domain * (*tls_find_client_domain_f) (struct ip_addr *, unsigned short);
4444
typedef struct tls_domain * (*tls_find_client_domain_name_f) (str *);
4545
typedef void (*tls_release_domain_f) (struct tls_domain *);
46+
#ifndef NO_SSL_GLOBAL_LOCK
47+
typedef void (*tls_global_lock_get_f) (void);
48+
typedef void (*tls_global_lock_release_f) (void);
49+
#endif
4650

4751
/* utility functions for operations directly on a SSL_CTX */
4852
typedef void (*tls_ctx_set_cert_store_f) (void *ctx, void *src_ctx);
@@ -57,6 +61,10 @@ struct tls_mgm_binds {
5761
tls_ctx_set_cert_store_f ctx_set_cert_store;
5862
tls_ctx_set_cert_chain_f ctx_set_cert_chain;
5963
tls_ctx_set_pkey_file_f ctx_set_pkey_file;
64+
#ifndef NO_SSL_GLOBAL_LOCK
65+
tls_global_lock_get_f global_lock_get;
66+
tls_global_lock_release_f global_lock_release;
67+
#endif
6068
};
6169

6270

0 commit comments

Comments
 (0)