ant-http/lib/ws.c

564 lines
14 KiB
C
Raw Permalink Normal View History

2020-08-25 16:40:24 +02:00
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
2019-12-22 00:11:26 +01:00
#ifdef USE_OPENSSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
2020-08-25 16:40:24 +02:00
#include <sys/time.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include "utils.h"
#include "handle.h"
#include "ws.h"
2024-03-12 21:59:50 +01:00
#define CONN_TIME_OUT_S 3
#define BITV(v, i) ((v & (1 << i)) >> i)
#define MAX_BUFF 1024
#define PREFERRED_WS_CIPHERS "HIGH:!aNULL:!kRSA:!SRP:!PSK:!CAMELLIA:!RC4:!MD5:!DSS"
#define CLIENT_RQ "GET /%s HTTP/1.1\r\nHost: %s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"
#define SERVER_WS_KEY "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
2020-12-22 16:02:36 +01:00
static void ws_gen_mask_key(ws_msg_header_t *header)
2017-06-05 19:09:59 +02:00
{
int r = rand();
2020-12-22 16:02:36 +01:00
header->mask_key[0] = (r >> 24) & 0xFF;
header->mask_key[1] = (r >> 16) & 0xFF;
header->mask_key[2] = (r >> 8) & 0xFF;
header->mask_key[3] = r & 0xFF;
2017-06-05 19:09:59 +02:00
}
/**
2024-03-12 21:59:50 +01:00
* Read a frame header
* based on this header, we'll decide
* the appropriate handle for frame data
*/
2020-12-22 16:02:36 +01:00
ws_msg_header_t *ws_read_header(void *client)
{
2020-12-22 16:02:36 +01:00
2018-03-02 19:04:00 +01:00
uint8_t byte = 0;
uint8_t bytes[8];
2020-12-22 16:02:36 +01:00
ws_msg_header_t *header = (ws_msg_header_t *)malloc(sizeof(*header));
// get first byte
if (antd_recv(client, &byte, sizeof(byte)) <= 0)
2020-12-22 16:02:36 +01:00
goto fail;
if (BITV(byte, 6) || BITV(byte, 5) || BITV(byte, 4))
goto fail; // all RSV bit must be 0
2024-03-12 21:59:50 +01:00
// printf("FIN: %d, RSV1: %d, RSV2: %d, RSV3:%d, opcode:%d\n", BITV(byte,7), BITV(byte,6), BITV(byte,5), BITV(byte,4),(byte & 0x0F) );
// find and opcode
2020-12-22 16:02:36 +01:00
header->fin = BITV(byte, 7);
header->opcode = (byte & 0x0F);
2020-12-22 16:02:36 +01:00
// get next byte
if (antd_recv(client, &byte, sizeof(byte)) <= 0)
2020-12-22 16:02:36 +01:00
goto fail;
2024-03-12 21:59:50 +01:00
// printf("MASK: %d paylen:%d\n", BITV(byte,7), (byte & 0x7F));
// check mask bit, should be 1
2020-12-22 16:02:36 +01:00
header->mask = BITV(byte, 7);
2017-06-05 19:09:59 +02:00
/*if(!BITV(byte,7))
{
// close the connection with protocol error
ws_close(client, 1002);
goto fail;
2017-06-05 19:09:59 +02:00
}*/
// get the data length of the frame
int len = (byte & 0x7F);
2020-12-22 16:02:36 +01:00
if (len <= 125)
{
header->plen = len;
2020-12-22 16:02:36 +01:00
}
else if (len == 126)
{
if (antd_recv(client, bytes, 2 * sizeof(uint8_t)) <= 0)
2020-12-22 16:02:36 +01:00
goto fail;
header->plen = (bytes[0] << 8) + bytes[1];
}
else
{
2024-03-12 21:59:50 +01:00
// read only last 4 byte
if (antd_recv(client, bytes, 8 * sizeof(uint8_t)) <= 0)
2020-12-22 16:02:36 +01:00
goto fail;
header->plen = (bytes[4] << 24) + (bytes[5] << 16) + (bytes[6] << 8) + bytes[7];
}
2024-03-12 21:59:50 +01:00
// printf("len: %d\n", header->plen);
// last step is to get the maskey
2020-12-22 16:02:36 +01:00
if (header->mask)
if (antd_recv(client, header->mask_key, 4 * sizeof(uint8_t)) <= 0)
2020-12-22 16:02:36 +01:00
goto fail;
2024-03-12 21:59:50 +01:00
// printf("key 0: %d key 1: %d key2:%d, key3: %d\n",header->mask_key[0],header->mask_key[1],header->mask_key[2], header->mask_key[3] );
2020-12-22 16:02:36 +01:00
// check wheather it is a ping or a close message
// process it and return NULL
2024-03-12 21:59:50 +01:00
// otherwise return the header
// return the header
2020-12-22 16:02:36 +01:00
switch (header->opcode)
{
case WS_CLOSE: // client requests to close the connection
// send back a close message
2020-12-22 16:02:36 +01:00
UNUSED(ws_send_close(client, 1000, header->mask ? 0 : 1));
2024-03-12 21:59:50 +01:00
// goto fail;
break;
2020-12-22 16:02:36 +01:00
case WS_PING: // client send a ping
// send back a pong message
2020-12-22 16:02:36 +01:00
UNUSED(ws_pong(client, header, header->mask ? 0 : 1));
break;
default:
break;
}
return header;
2020-12-22 16:02:36 +01:00
fail:
free(header);
return NULL;
}
/**
2024-03-12 21:59:50 +01:00
* Read data from client
* and unmask data using the key
*/
2020-12-22 16:02:36 +01:00
int ws_read_data(void *client, ws_msg_header_t *header, int len, uint8_t *data)
{
// if len == -1 ==> read all remaining data to 'data';
2020-12-22 16:02:36 +01:00
if (header->plen == 0)
return 0;
int dlen = (len == -1 || len > (int)header->plen) ? (int)header->plen : len;
if ((dlen = antd_recv(client, data, dlen)) <= 0)
2020-12-22 16:02:36 +01:00
return -1;
header->plen = header->plen - dlen;
// unmask received data
2020-12-22 16:02:36 +01:00
if (header->mask)
for (int i = 0; i < dlen; ++i)
data[i] = data[i] ^ header->mask_key[i % 4];
2017-06-05 19:09:59 +02:00
data[dlen] = '\0';
return dlen;
}
2020-12-22 16:02:36 +01:00
int _send_header(void *client, ws_msg_header_t header)
{
uint8_t byte = 0;
uint8_t bytes[8];
2020-12-22 16:02:36 +01:00
for (int i = 0; i < 8; i++)
bytes[i] = 0;
2024-03-12 21:59:50 +01:00
// first byte |FIN|000|opcode|
byte = (header.fin << 7) + header.opcode;
2024-03-12 21:59:50 +01:00
// printf("BYTE: %d\n", byte);
2020-12-22 16:02:36 +01:00
if (antd_send(client, &byte, 1) != 1)
return -1;
// second byte, payload length
2017-06-05 19:09:59 +02:00
// mask may be 0 or 1
2024-03-12 21:59:50 +01:00
// if(header.mask == 1)
2017-06-05 19:09:59 +02:00
// printf("Data is masked\n");
2020-12-22 16:02:36 +01:00
if (header.plen <= 125)
{
2020-12-22 16:02:36 +01:00
byte = (header.mask << 7) + header.plen;
if (antd_send(client, &byte, 1) != 1)
return -1;
}
2020-12-22 16:02:36 +01:00
else if (header.plen < 65536) // 16 bits
{
2017-06-05 19:09:59 +02:00
byte = (header.mask << 7) + 126;
bytes[0] = (header.plen) >> 8;
bytes[1] = (header.plen) & 0x00FF;
2020-12-22 16:02:36 +01:00
if (antd_send(client, &byte, 1) != 1)
return -1;
if (antd_send(client, bytes, 2) != 2)
return -1;
}
else // > 16 bits
{
2020-12-22 16:02:36 +01:00
byte = (header.mask << 7) + 127;
bytes[4] = (header.plen) >> 24;
2020-12-22 16:02:36 +01:00
bytes[5] = ((header.plen) >> 16) & 0x00FF;
bytes[6] = ((header.plen) >> 8) & 0x00FF;
bytes[7] = (header.plen) & 0x00FF;
2020-12-22 16:02:36 +01:00
if (antd_send(client, &byte, 1) != 1)
return -1;
if (antd_send(client, bytes, 8) != 8)
return -1;
}
2017-06-05 19:09:59 +02:00
// send mask key
2020-12-22 16:02:36 +01:00
if (header.mask)
2017-06-05 19:09:59 +02:00
{
2020-12-22 16:02:36 +01:00
if (antd_send(client, header.mask_key, 4) != 4)
return -1;
2017-06-05 19:09:59 +02:00
}
2020-12-22 16:02:36 +01:00
return 0;
}
/**
2024-03-12 21:59:50 +01:00
* Send a frame to client
*/
2020-12-22 16:02:36 +01:00
int ws_send_frame(void *client, uint8_t *data, ws_msg_header_t header)
{
2020-12-22 16:02:36 +01:00
uint8_t *masked;
2017-06-05 19:09:59 +02:00
masked = data;
2020-12-22 16:02:36 +01:00
int ret;
if (header.mask)
2017-06-05 19:09:59 +02:00
{
ws_gen_mask_key(&header);
2020-12-22 16:02:36 +01:00
masked = (uint8_t *)malloc(header.plen);
for (int i = 0; i < (int)header.plen; ++i)
masked[i] = data[i] ^ header.mask_key[i % 4];
2017-06-05 19:09:59 +02:00
}
2020-12-22 16:02:36 +01:00
if (_send_header(client, header) != 0)
return -1;
if (header.opcode == WS_TEXT)
ret = antd_send(client, (char *)masked, header.plen);
else
2020-12-22 16:02:36 +01:00
ret = antd_send(client, (uint8_t *)masked, header.plen);
if (masked && header.mask)
2017-06-05 19:09:59 +02:00
free(masked);
2020-12-22 16:02:36 +01:00
if (ret != (int)header.plen)
{
return -1;
}
return 0;
}
/**
2024-03-12 21:59:50 +01:00
* send a text data frame to client
*/
2020-12-22 16:02:36 +01:00
int ws_send_text(void *client, const char *data, int mask)
{
ws_msg_header_t header;
header.fin = 1;
header.opcode = WS_TEXT;
2017-06-05 19:09:59 +02:00
header.mask = mask;
header.plen = strlen(data);
//_send_header(client,header);
2024-03-12 21:59:50 +01:00
// send(client, data, header.plen,0);
2020-12-22 16:02:36 +01:00
return ws_send_frame(client, (uint8_t *)data, header);
}
/**
2024-03-12 21:59:50 +01:00
* send a single binary data fram to client
* not tested yet, but should work
*/
2020-12-22 16:02:36 +01:00
int ws_send_binary(void *client, uint8_t *data, int l, int mask)
{
ws_msg_header_t header;
header.fin = 1;
header.opcode = WS_BIN;
header.plen = l;
2017-06-05 19:09:59 +02:00
header.mask = mask;
2020-12-22 16:02:36 +01:00
return ws_send_frame(client, data, header);
//_send_header(client,header);
2024-03-12 21:59:50 +01:00
// send(client, data, header.plen,0);
}
/*
2024-03-12 21:59:50 +01:00
* send a file as binary data
*/
2020-12-22 16:02:36 +01:00
int ws_send_file(void *client, const char *file, int mask)
{
uint8_t buff[1024];
FILE *ptr;
2020-12-22 16:02:36 +01:00
ptr = fopen(file, "rb");
if (!ptr)
{
2020-12-22 16:02:36 +01:00
return ws_send_close(client, 1011, mask);
}
ws_msg_header_t header;
size_t size;
int first_frame = 1;
2020-12-22 16:02:36 +01:00
int ret = 0;
2024-03-12 21:59:50 +01:00
// ws_send_frame(client,buff,header);
2017-06-05 19:09:59 +02:00
header.mask = mask;
2020-12-22 16:02:36 +01:00
while (!feof(ptr))
{
2020-12-22 16:02:36 +01:00
size = fread(buff, 1, 1024, ptr);
if (feof(ptr))
2018-10-09 10:35:37 +02:00
header.fin = 1;
else
header.fin = 0;
// clear opcode
2020-12-22 16:02:36 +01:00
if (first_frame)
{
2018-10-09 10:35:37 +02:00
header.opcode = WS_BIN;
first_frame = 0;
}
else
header.opcode = 0;
header.plen = size;
2024-03-12 21:59:50 +01:00
// printf("FIN: %d OC:%d\n", header.fin, header.opcode);
2020-12-22 16:02:36 +01:00
ret += ws_send_frame(client, buff, header);
}
fclose(ptr);
2020-12-22 16:02:36 +01:00
if (ret != 0)
{
return -1;
}
return 0;
}
/**
2024-03-12 21:59:50 +01:00
* Not tested yet
* but should work
*/
2020-12-22 16:02:36 +01:00
int ws_pong(void *client, ws_msg_header_t *oheader, int mask)
{
ws_msg_header_t pheader;
2020-12-22 16:02:36 +01:00
int ret;
pheader.fin = 1;
pheader.opcode = WS_PONG;
2019-12-22 00:11:26 +01:00
pheader.plen = oheader->plen;
pheader.mask = mask;
2020-12-22 16:02:36 +01:00
uint8_t *data = (uint8_t *)malloc(oheader->plen);
if (!data)
return -1;
if (ws_read_data(client, oheader, pheader.plen, data) == -1)
2019-12-21 17:38:20 +01:00
{
2019-12-22 00:11:26 +01:00
ERROR("Cannot read ping data %d", pheader.plen);
2019-12-21 17:38:20 +01:00
free(data);
2020-12-22 16:02:36 +01:00
return -1;
2019-12-21 17:38:20 +01:00
}
2020-12-22 16:02:36 +01:00
ret = ws_send_frame(client, data, pheader);
2019-12-21 17:38:20 +01:00
free(data);
2017-06-05 19:09:59 +02:00
//_send_header(client, pheader);
2024-03-12 21:59:50 +01:00
// send(client, data, len, 0);
2020-12-22 16:02:36 +01:00
return ret;
}
2020-12-22 16:02:36 +01:00
int ws_ping(void *client, const char *echo, int mask)
2019-12-22 00:11:26 +01:00
{
ws_msg_header_t pheader;
pheader.fin = 1;
pheader.opcode = WS_PING;
pheader.plen = strlen(echo);
pheader.mask = mask;
2020-12-22 16:02:36 +01:00
return ws_send_frame(client, (uint8_t *)echo, pheader);
2019-12-22 00:11:26 +01:00
}
/*
2024-03-12 21:59:50 +01:00
* Not tested yet, but should work
*/
2020-12-22 16:02:36 +01:00
int ws_send_close(void *client, unsigned int status, int mask)
{
2024-03-12 21:59:50 +01:00
// printf("CLOSED\n");
ws_msg_header_t header;
header.fin = 1;
header.opcode = WS_CLOSE;
header.plen = 2;
2020-12-22 16:02:36 +01:00
header.mask = mask;
uint8_t bytes[2];
bytes[0] = status >> 8;
bytes[1] = status & 0xFF;
2017-06-05 19:09:59 +02:00
/*if(mask)
{
// XOR itself
header.mask_key[0] = bytes[0];
header.mask_key[1] = bytes[1];
bytes[0] = bytes[1] ^ bytes[1];
}*/
2020-12-22 16:02:36 +01:00
return ws_send_frame(client, bytes, header);
2017-06-05 19:09:59 +02:00
//_send_header(client, header);
2024-03-12 21:59:50 +01:00
// send(client,bytes,2,0);
2017-06-05 19:09:59 +02:00
}
2017-07-29 23:54:43 +02:00
2020-12-22 16:02:36 +01:00
void ws_client_close(ws_client_t *wsclient)
2019-12-22 00:11:26 +01:00
{
antd_close(wsclient->antdsock);
#ifdef USE_OPENSSL
2020-12-22 16:02:36 +01:00
if (wsclient->ssl_ctx)
2019-12-22 00:11:26 +01:00
{
2020-08-19 12:26:17 +02:00
SSL_CTX_free(wsclient->ssl_ctx);
2024-03-09 16:52:28 +01:00
// DEPRECATED: FIPS_mode_set(0);
2019-12-22 00:11:26 +01:00
// DEPRECATED: CONF_modules_unload(1);
EVP_cleanup();
EVP_PBE_cleanup();
// DEPRECATED:ENGINE_cleanup();
CRYPTO_cleanup_all_ex_data();
// DEPRECATED: ERR_remove_state(0);
ERR_free_strings();
}
#endif
}
2024-03-12 21:59:50 +01:00
// this is for the client side, not use for now
int ws_client_connect(ws_client_t *wsclient, ws_port_config_t pcnf)
2017-06-05 19:09:59 +02:00
{
2024-03-12 21:59:50 +01:00
char *ip = ip_from_hostname(wsclient->host);
2021-01-22 01:12:26 +01:00
if (ip == NULL)
2019-12-22 00:11:26 +01:00
return -1;
2024-03-12 21:59:50 +01:00
int sock = antd_request_socket(ip, pcnf.port);
2020-12-22 16:02:36 +01:00
if (sock <= 0)
2019-12-22 00:11:26 +01:00
{
ERROR("Cannot request socket");
return -1;
}
2021-01-22 01:12:26 +01:00
// time out setting
struct timeval timeout;
timeout.tv_sec = CONN_TIME_OUT_S;
2024-03-12 21:59:50 +01:00
timeout.tv_usec = 0; // 3 s
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
2021-01-22 01:12:26 +01:00
ERROR("setsockopt failed:%s", strerror(errno));
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
ERROR("setsockopt failed:%s", strerror(errno));
2024-03-12 21:59:50 +01:00
2019-12-22 00:11:26 +01:00
// will be free
wsclient->antdsock->sock = sock;
wsclient->antdsock->z_status = 0;
2019-12-22 00:11:26 +01:00
wsclient->antdsock->last_io = time(NULL);
2020-01-08 19:17:51 +01:00
wsclient->antdsock->zstream = NULL;
2019-12-22 00:11:26 +01:00
#ifdef USE_OPENSSL
2020-12-22 16:02:36 +01:00
if (pcnf.usessl)
2019-12-22 00:11:26 +01:00
{
SSL_library_init();
SSL_load_error_strings();
ERR_load_crypto_strings();
2020-12-22 16:02:36 +01:00
OpenSSL_add_ssl_algorithms();
2019-12-22 00:11:26 +01:00
const SSL_METHOD *method;
unsigned long ssl_err = 0;
method = SSLv23_client_method();
ssl_err = ERR_get_error();
2020-12-22 16:02:36 +01:00
if (!method)
2019-12-22 00:11:26 +01:00
{
ERROR("SSLv23_method: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
wsclient->ssl_ctx = SSL_CTX_new(method);
ssl_err = ERR_get_error();
2020-12-22 16:02:36 +01:00
if (!wsclient->ssl_ctx)
{
2019-12-22 00:11:26 +01:00
ERROR("SSL_CTX_new: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
// configure the context
#if defined(SSL_CTX_set_ecdh_auto)
2020-12-22 16:02:36 +01:00
SSL_CTX_set_ecdh_auto(wsclient->ssl_ctx, 1);
2019-12-22 00:11:26 +01:00
#else
2020-12-22 16:02:36 +01:00
SSL_CTX_set_tmp_ecdh(wsclient->ssl_ctx, EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
2019-12-22 00:11:26 +01:00
#endif
2020-12-22 16:02:36 +01:00
SSL_CTX_set_options(wsclient->ssl_ctx, SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_SSLv2 | SSL_OP_NO_TICKET);
2019-12-22 00:11:26 +01:00
// set the cipher suit
2020-12-22 16:02:36 +01:00
const char *suit = wsclient->ciphersuit ? wsclient->ciphersuit : PREFERRED_WS_CIPHERS;
2024-03-12 21:59:50 +01:00
// const char* suit = "AES128-SHA";
2019-12-22 00:11:26 +01:00
if (SSL_CTX_set_cipher_list(wsclient->ssl_ctx, suit) != 1)
{
ssl_err = ERR_get_error();
// TODO Close the context
ERROR("SSL_CTX_set_cipher_list: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
2020-12-22 16:02:36 +01:00
if (wsclient->sslcert && wsclient->sslkey)
2019-12-22 00:11:26 +01:00
{
2020-12-22 16:02:36 +01:00
if (SSL_CTX_use_certificate_file(wsclient->ssl_ctx, wsclient->sslcert, SSL_FILETYPE_PEM) <= 0)
{
2019-12-22 00:11:26 +01:00
ssl_err = ERR_get_error();
ERROR("SSL_CTX_use_certificate_file: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
2020-12-22 16:02:36 +01:00
if (wsclient->sslpasswd)
SSL_CTX_set_default_passwd_cb_userdata(wsclient->ssl_ctx, (void *)wsclient->sslpasswd);
if (SSL_CTX_use_PrivateKey_file(wsclient->ssl_ctx, wsclient->sslkey, SSL_FILETYPE_PEM) <= 0)
{
2019-12-22 00:11:26 +01:00
ssl_err = ERR_get_error();
ERROR("SSL_CTX_use_PrivateKey_file: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
2020-12-22 16:02:36 +01:00
if (SSL_CTX_check_private_key(wsclient->ssl_ctx) == 0)
{
2019-12-22 00:11:26 +01:00
ssl_err = ERR_get_error();
ERROR("SSL_CTX_check_private_key: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
}
//
2020-12-22 16:02:36 +01:00
// validate
if (wsclient->verify_location)
2019-12-22 00:11:26 +01:00
{
SSL_CTX_set_verify(wsclient->ssl_ctx, SSL_VERIFY_PEER, NULL);
2020-12-22 16:02:36 +01:00
SSL_CTX_set_verify_depth(wsclient->ssl_ctx, 5);
if (!SSL_CTX_load_verify_locations(wsclient->ssl_ctx, wsclient->verify_location, NULL))
2019-12-22 00:11:26 +01:00
{
ssl_err = ERR_get_error();
// TODO Close the context
ERROR("SSL_CTX_load_verify_locations: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
}
else
{
2020-12-22 16:02:36 +01:00
SSL_CTX_set_verify(wsclient->ssl_ctx, SSL_VERIFY_NONE, NULL);
2019-12-22 00:11:26 +01:00
}
2020-12-22 16:02:36 +01:00
wsclient->antdsock->ssl = (void *)SSL_new(wsclient->ssl_ctx);
if (!wsclient->antdsock->ssl)
2019-12-22 00:11:26 +01:00
{
ssl_err = ERR_get_error();
ERROR("SSL_new: %s", ERR_error_string(ssl_err, NULL));
return -1;
}
2020-12-22 16:02:36 +01:00
SSL_set_fd((SSL *)wsclient->antdsock->ssl, wsclient->antdsock->sock);
2019-12-22 00:11:26 +01:00
int stat, ret;
ERR_clear_error();
2020-12-22 16:02:36 +01:00
while ((ret = SSL_connect(wsclient->antdsock->ssl)) <= 0)
2019-12-22 00:11:26 +01:00
{
stat = SSL_get_error(wsclient->antdsock->ssl, ret);
switch (stat)
{
2020-12-22 16:02:36 +01:00
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_NONE:
continue;
default:
ERR_print_errors_fp(stderr);
ERROR("Error performing SSL handshake %d", stat);
return -1;
2019-12-22 00:11:26 +01:00
}
}
}
#endif
return 0;
}
2020-12-22 16:02:36 +01:00
int ws_open_handshake(ws_client_t *client)
2019-12-22 00:11:26 +01:00
{
char buf[MAX_BUFF];
2020-12-22 16:02:36 +01:00
// now send ws request handshake
sprintf(buf, CLIENT_RQ, client->resource, client->host);
2024-03-12 21:59:50 +01:00
// printf("Send %s\n", buf);
2020-12-22 16:02:36 +01:00
int size = antd_send(client->antdsock, buf, strlen(buf));
if (size != (int)strlen(buf))
{
ERROR("Cannot send request \n");
return -1;
}
// now verify if server accept the socket
size = read_buf(client->antdsock, buf, MAX_BUFF);
char *token;
int done = 0;
while (size > 0 && strcmp("\r\n", buf))
2017-06-05 19:09:59 +02:00
{
2020-12-22 16:02:36 +01:00
char *line = buf;
token = strsep(&line, ":");
trim(token, ' ');
if (token != NULL && strcasecmp(token, "Sec-WebSocket-Accept") == 0)
2017-06-05 19:09:59 +02:00
{
2020-12-22 16:02:36 +01:00
token = strsep(&line, ":");
trim(token, ' ');
trim(token, '\n');
trim(token, '\r');
if (strcasecmp(token, SERVER_WS_KEY) == 0)
{
2024-03-12 21:59:50 +01:00
// LOG("Handshake sucessfull\n");
2020-12-22 16:02:36 +01:00
done = 1;
}
else
{
ERROR("WS handshake, Wrong key %s vs %s", token, SERVER_WS_KEY);
return -1;
}
}
2024-03-12 21:59:50 +01:00
// if(line) free(line);
2020-12-22 16:02:36 +01:00
size = read_buf(client->antdsock, buf, MAX_BUFF);
2017-06-05 19:09:59 +02:00
}
2020-12-22 16:02:36 +01:00
if (done)
2019-12-22 00:11:26 +01:00
return 0;
2020-12-22 16:02:36 +01:00
return -1;
2019-12-22 00:11:26 +01:00
}