Skip to content

Commit

Permalink
fix: connection process
Browse files Browse the repository at this point in the history
This commit fixes the connection process done by FrequenC.
  • Loading branch information
ThePedroo committed May 15, 2024
1 parent b6993e9 commit 747ac90
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 30 deletions.
36 changes: 19 additions & 17 deletions external/csocket-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ int csocket_client_init(struct csocket_client *client, bool secure, char *hostna
if (WSAStartup(MAKEWORD(2,2), &client->wsa) != 0) {
perror("[csocket-client]: Failed to initialize Winsock");

return -1;
return CSOCKET_CLIENT_ERROR;
}

if ((client->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
perror("[csocket-client]: Failed to create socket");

return -1;
return CSOCKET_CLIENT_ERROR;
}
#else
if ((client->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
if ((client->socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("[csocket-client]: Failed to create socket");

return -1;
return CSOCKET_CLIENT_ERROR;
}
#endif

struct hostent *host = gethostbyname(hostname);
if (!host) {
perror("[https-client]: Failed to resolve hostname");
perror("[csocket-client]: Failed to resolve hostname");

return -1;
return CSOCKET_CLIENT_ERROR;
}

struct sockaddr_in addr = {
Expand All @@ -63,44 +63,46 @@ int csocket_client_init(struct csocket_client *client, bool secure, char *hostna
if (connect(client->socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("[csocket-client]: Failed to connect to server");

return -1;
return CSOCKET_CLIENT_ERROR;
}

if ((client->secure = secure)) {
pcll_init_ssl_library();

int ret = pcll_init_ssl(&client->connection);
if (ret != PCLL_SUCCESS) {
_csocket_client_close(client);

perror("[https-client]: Failed to initialize SSL");
perror("[csocket-client]: Failed to initialize SSL");

return -1;
return CSOCKET_CLIENT_ERROR;
}

ret = pcll_set_fd(&client->connection, (int)client->socket);
if (ret != PCLL_SUCCESS) {
_csocket_client_close(client);

perror("[https-client]: Failed to attach SSL to socket");
perror("[csocket-client]: Failed to attach SSL to socket");

return -1;
return CSOCKET_CLIENT_ERROR;
}

ret = pcll_set_safe_mode(&client->connection, hostname);
if (ret != PCLL_SUCCESS) {
_csocket_client_close(client);

perror("[https-client]: Failed to set safe mode");
perror("[csocket-client]: Failed to set safe mode");

return -1;
return CSOCKET_CLIENT_ERROR;
}

ret = pcll_connect(&client->connection);
if (ret != PCLL_SUCCESS) {
_csocket_client_close(client);

printf("[https-client]: Failed to perform SSL handshake: %d\n", pcll_get_error(&client->connection, ret));
printf("[csocket-client]: Failed to perform SSL handshake: %d\n", pcll_get_error(&client->connection, ret));

return -1;
return CSOCKET_CLIENT_ERROR;
}
}

Expand All @@ -111,7 +113,7 @@ int csocket_client_send(struct csocket_client *client, char *data, int size) {
if (client->secure) {
int ret = pcll_send(&client->connection, data, size);
if (ret == PCLL_ERROR) {
perror("[https-client]: Failed to send data");
perror("[csocket-client]: Failed to send data");

return CSOCKET_CLIENT_ERROR;
}
Expand All @@ -130,7 +132,7 @@ int csocket_client_recv(struct csocket_client *client, char *buffer, int size) {
if (client->secure) {
int recv_length = pcll_recv(&client->connection, buffer, size);
if (recv_length == PCLL_ERROR) {
perror("[https-client]: Failed to receive data");
perror("[csocket-client]: Failed to receive data");

return CSOCKET_CLIENT_ERROR;
}
Expand Down
6 changes: 5 additions & 1 deletion external/pcll.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ int pcll_init_ssl(struct pcll_connection *connection) {
return PCLL_ERROR;
}

int ret = SSL_CTX_set_min_proto_version(connection->ctx, TLS1_3_VERSION);
/*
This is set to TLS v1.2 as that's the maximum Discord accepts. ONLY put as v1.2 if
the targeted service doesn't accept it.
*/
int ret = SSL_CTX_set_min_proto_version(connection->ctx, TLS1_2_VERSION);
if (ret != SSL_SUCCESS) {
SSL_CTX_free(connection->ctx);

Expand Down
1 change: 1 addition & 0 deletions external/pdvoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ void *_pdvoice_connect(void *data) {
struct httpclient_request_params params = {
.host = connection->ws_connection_info->endpoint,
.port = 443,
.secure = true,
.path = path,
.method = "GET",
.headers = (struct httpparser_header[4 + 1]) {
Expand Down
11 changes: 11 additions & 0 deletions external/pdvoice.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef PDVOICE_H
#define PDVOICE_H

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif

#include "cthreads.h"

#include "httpclient.h"
Expand All @@ -15,6 +20,12 @@ struct pdvoice_udp_connection_info {
int ssrc;
char *ip;
int port;

#ifdef _WIN32
SOCKET socket;
#else
int socket;
#endif
};

struct _pdvoice_udp_thread_data {
Expand Down
2 changes: 1 addition & 1 deletion include/httpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct httpclient_response {
struct csocket_client connection;
};

int httpclient_request(struct httpclient_request_params *request, struct httpclient_response *httpResponse);
int httpclient_request(struct httpclient_request_params *request, struct httpclient_response *http_response);

void httpclient_shutdown(struct httpclient_response *response);

Expand Down
22 changes: 17 additions & 5 deletions lib/httpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,36 @@ void _httpclient_build_request(struct httpclient_request_params *request, struct
}

if (request->body != NULL) {
current_length += snprintf(request_string + current_length, (length + 1) - current_length, "Content-Length: %zu\r\n\r\n%.*s", request->body->length, (int)request->body->length, request->body->string);
tstr_append(request_string, "Content-Length: ", &current_length, 16);

char content_length_str[16];
int content_length_length = snprintf(content_length_str, 16, "%zu", request->body->length);
tstr_append(request_string, content_length_str, &current_length, content_length_length);

tstr_append(request_string, "\r\n\r\n", &current_length, 4);
tstr_append(request_string, request->body->string, &current_length, request->body->length);
} else {
current_length += snprintf(request_string + current_length, (length + 1) - current_length, "\r\n");
tstr_append(request_string, "\r\n", &current_length, 2);
}

response->string = request_string;
response->length = length;
response->length = length - 1;
}

int httpclient_request(struct httpclient_request_params *request, struct httpclient_response *http_response) {
pcll_init_ssl_library();

csocket_client_init(&http_response->connection, request->secure, request->host, request->port);
int ret = csocket_client_init(&http_response->connection, request->secure, request->host, request->port);
if (ret == CSOCKET_CLIENT_ERROR) {
perror("[https-client]: Failed to initialize connection");

return -1;
}

struct tstr_string http_request;
_httpclient_build_request(request, &http_request);

int ret = csocket_client_send(&http_response->connection, http_request.string, (int)http_request.length);
ret = csocket_client_send(&http_response->connection, http_request.string, (int)http_request.length);
if (ret == CSOCKET_CLIENT_ERROR) {
csocket_client_close(&http_response->connection);

Expand Down
11 changes: 5 additions & 6 deletions lib/httpparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,19 @@ int httpparser_parse_request(struct httpparser_request *http_request, const char

int requested_length = (request_length + headers_end.end - 4) - chunk_size.end - 2;

if (requested_length != content_length) return -1;

if (requested_length > http_request->chunk_length) requested_length = http_request->chunk_length;

http_request->body = frequenc_safe_malloc((http_request->chunk_length + 1) * sizeof(char));
frequenc_fast_copy(request + headers_end.end + 4, http_request->body, requested_length);
http_request->body_length = requested_length;
http_request->body = frequenc_safe_malloc(requested_length * sizeof(char));
frequenc_unsafe_fast_copy(body_and_length + chunk_size.end + 2, http_request->body, requested_length);

/* TODO: Implement chunk handling */
http_request->finished = http_request->body_length == (size_t)http_request->chunk_length;
} else {
if ((request_length - headers_end.end - 4) != content_length) return -1;

http_request->body = frequenc_safe_malloc((content_length + 1) * sizeof(char));
frequenc_fast_copy(request + headers_end.end + 4, http_request->body, content_length);
http_request->body = frequenc_safe_malloc(content_length * sizeof(char));
frequenc_unsafe_fast_copy(request + headers_end.end + 4, http_request->body, content_length);

http_request->body_length = content_length;
http_request->finished = true;
Expand Down
4 changes: 4 additions & 0 deletions lib/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ int frequenc_send_text_ws_client(struct httpclient_response *response, char *mes
buffer[0] = 1 | 128;

if (message_length >= 65536) {
buffer[1] = 127;

buffer[2] = buffer[3] = 0;
buffer[4] = (unsigned char)(message_length >> 56);
buffer[5] = (unsigned char)(message_length >> 48);
Expand All @@ -366,6 +368,8 @@ int frequenc_send_text_ws_client(struct httpclient_response *response, char *mes
buffer[10] = (unsigned char)(message_length >> 8);
buffer[11] = (unsigned char)(message_length & 0xFF);
} else if (message_length > 125) {
buffer[1] = 126;

buffer[2] = (unsigned char)(message_length >> 8);
buffer[3] = (unsigned char)(message_length & 0xFF);
} else {
Expand Down

0 comments on commit 747ac90

Please sign in to comment.