mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-04-16 08:49:01 +00:00
FS-6426 --resolve
This commit is contained in:
parent
4e86b03126
commit
7ea4acaece
@ -467,7 +467,7 @@ int tport_ws_init_secondary(tport_t *self, int socket, int accepted,
|
|||||||
|
|
||||||
memset(&wstp->ws, 0, sizeof(wstp->ws));
|
memset(&wstp->ws, 0, sizeof(wstp->ws));
|
||||||
|
|
||||||
if (ws_init(&wstp->ws, socket, wstp->ws_secure ? wspri->ssl_ctx : NULL, 0) < 0) {
|
if (ws_init(&wstp->ws, socket, wstp->ws_secure ? wspri->ssl_ctx : NULL, 0, 0) < 0) {
|
||||||
ws_destroy(&wstp->ws);
|
ws_destroy(&wstp->ws);
|
||||||
wstp->ws_initialized = -1;
|
wstp->ws_initialized = -1;
|
||||||
return *return_reason = "WS_INIT", -1;
|
return *return_reason = "WS_INIT", -1;
|
||||||
|
@ -5,6 +5,12 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#define ms_sleep(x) usleep( x * 1000);
|
||||||
|
#else
|
||||||
|
#define ms_sleep(x) Sleep( x );
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SHA1_HASH_SIZE 20
|
#define SHA1_HASH_SIZE 20
|
||||||
struct ws_globals_s ws_globals;
|
struct ws_globals_s ws_globals;
|
||||||
|
|
||||||
@ -418,11 +424,93 @@ static int restore_socket(ws_socket_t sock)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static int establish_logical_layer(wsh_t *wsh)
|
||||||
|
{
|
||||||
|
|
||||||
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock)
|
if (!wsh->sanity) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wsh->logical_established) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wsh->secure && !wsh->secure_established) {
|
||||||
|
int code;
|
||||||
|
|
||||||
|
if (!wsh->ssl) {
|
||||||
|
wsh->ssl = SSL_new(wsh->ssl_ctx);
|
||||||
|
assert(wsh->ssl);
|
||||||
|
|
||||||
|
SSL_set_fd(wsh->ssl, wsh->sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
code = SSL_accept(wsh->ssl);
|
||||||
|
|
||||||
|
if (code == 1) {
|
||||||
|
wsh->secure_established = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code < 0) {
|
||||||
|
if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wsh->block) {
|
||||||
|
ms_sleep(10);
|
||||||
|
} else {
|
||||||
|
ms_sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
wsh->sanity--;
|
||||||
|
|
||||||
|
if (!wsh->block) {
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (wsh->sanity > 0);
|
||||||
|
|
||||||
|
if (!wsh->sanity) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!wsh->down && !wsh->handshake) {
|
||||||
|
int r = ws_handshake(wsh);
|
||||||
|
|
||||||
|
if (r < 0) {
|
||||||
|
wsh->down = 1;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wsh->handshake && !wsh->block) {
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
wsh->logical_established = 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block)
|
||||||
{
|
{
|
||||||
memset(wsh, 0, sizeof(*wsh));
|
memset(wsh, 0, sizeof(*wsh));
|
||||||
|
|
||||||
wsh->sock = sock;
|
wsh->sock = sock;
|
||||||
|
wsh->block = block;
|
||||||
|
wsh->sanity = 5000;
|
||||||
|
wsh->ssl_ctx = ssl_ctx;
|
||||||
|
|
||||||
if (!ssl_ctx) {
|
if (!ssl_ctx) {
|
||||||
ssl_ctx = ws_globals.ssl_ctx;
|
ssl_ctx = ws_globals.ssl_ctx;
|
||||||
@ -437,52 +525,8 @@ int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock)
|
|||||||
|
|
||||||
setup_socket(sock);
|
setup_socket(sock);
|
||||||
|
|
||||||
if (wsh->secure) {
|
if (establish_logical_layer(wsh) == -1) {
|
||||||
int code;
|
return -1;
|
||||||
int sanity = 500;
|
|
||||||
|
|
||||||
wsh->ssl = SSL_new(ssl_ctx);
|
|
||||||
assert(wsh->ssl);
|
|
||||||
|
|
||||||
SSL_set_fd(wsh->ssl, wsh->sock);
|
|
||||||
|
|
||||||
do {
|
|
||||||
code = SSL_accept(wsh->ssl);
|
|
||||||
|
|
||||||
if (code == 1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (code == 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (code < 0) {
|
|
||||||
if (code == -1 && SSL_get_error(wsh->ssl, code) != SSL_ERROR_WANT_READ) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifndef _MSC_VER
|
|
||||||
usleep(10000);
|
|
||||||
#else
|
|
||||||
Sleep(10);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} while (--sanity > 0);
|
|
||||||
|
|
||||||
if (!sanity) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!wsh->down && !wsh->handshake) {
|
|
||||||
int r = ws_handshake(wsh);
|
|
||||||
|
|
||||||
if (r < 0) {
|
|
||||||
wsh->down = 1;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wsh->down) {
|
if (wsh->down) {
|
||||||
@ -555,12 +599,19 @@ ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data)
|
|||||||
|
|
||||||
ssize_t need = 2;
|
ssize_t need = 2;
|
||||||
char *maskp;
|
char *maskp;
|
||||||
|
int ll = 0;
|
||||||
|
|
||||||
again:
|
again:
|
||||||
need = 2;
|
need = 2;
|
||||||
maskp = NULL;
|
maskp = NULL;
|
||||||
*data = NULL;
|
*data = NULL;
|
||||||
|
|
||||||
|
ll = establish_logical_layer(wsh);
|
||||||
|
|
||||||
|
if (ll < 0) {
|
||||||
|
return ll;
|
||||||
|
}
|
||||||
|
|
||||||
if (wsh->down) {
|
if (wsh->down) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,11 @@ typedef struct wsh_s {
|
|||||||
uint8_t down;
|
uint8_t down;
|
||||||
int secure;
|
int secure;
|
||||||
uint8_t close_sock;
|
uint8_t close_sock;
|
||||||
|
SSL_CTX *ssl_ctx;
|
||||||
|
int block;
|
||||||
|
int sanity;
|
||||||
|
int secure_established;
|
||||||
|
int logical_established;
|
||||||
} wsh_t;
|
} wsh_t;
|
||||||
|
|
||||||
ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
|
ssize_t ws_send_buf(wsh_t *wsh, ws_opcode_t oc);
|
||||||
@ -93,7 +98,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes);
|
|||||||
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
|
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes);
|
||||||
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
|
ssize_t ws_read_frame(wsh_t *wsh, ws_opcode_t *oc, uint8_t **data);
|
||||||
ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
|
ssize_t ws_write_frame(wsh_t *wsh, ws_opcode_t oc, void *data, size_t bytes);
|
||||||
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock);
|
int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int block);
|
||||||
ssize_t ws_close(wsh_t *wsh, int16_t reason);
|
ssize_t ws_close(wsh_t *wsh, int16_t reason);
|
||||||
void ws_destroy(wsh_t *wsh);
|
void ws_destroy(wsh_t *wsh);
|
||||||
void init_ssl(void);
|
void init_ssl(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user