Skip to content

Commit

Permalink
improve: code generally; add: retrieve stream URL support
Browse files Browse the repository at this point in the history
This commit improves the code generally, fixing type mismatch and numerous other issues, and also adds support for base retrieve stream URL support.
  • Loading branch information
ThePedroo committed Jun 2, 2024
1 parent 56e2dc7 commit cc48a34
Show file tree
Hide file tree
Showing 25 changed files with 650 additions and 312 deletions.
8 changes: 6 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ This endpoint allows clients to update information about the player, allowing Fr
- `endpoint` - String: The Discord voice server endpoint.
- `session_id` - String: The Discord voice server session ID.
- `token` - String: The Discord voice server token.
- `track` - String: The encoded track of the current playing track.

##### Example

Expand All @@ -514,7 +515,8 @@ This endpoint allows clients to update information about the player, allowing Fr
"endpoint": "brazil1001.discord.media",
"session_id": "Xe32",
"token": "Xe16"
}
},
"track": "XXXXXXXXXX..."
}
```

Expand All @@ -528,6 +530,7 @@ This endpoint allows clients to update information about the player, allowing Fr
- `endpoint` - String: The Discord voice server endpoint.
- `session_id` - String: The Discord voice server session ID.
- `token` - String: The Discord voice server token.
- `track` - String: The encoded track of the current playing track.

> [!WARNING]
> The `endpoint`, `session_id` and `token` are Discord voice server specific, and should not be shared with anyone. However they're only temporarily confidential.
Expand All @@ -540,7 +543,8 @@ This endpoint allows clients to update information about the player, allowing Fr
"endpoint": "brazil1001.discord.media",
"session_id": "Xe32",
"token": "Xe16"
}
},
"track: "XXXXXXXXXX..."
}
```

Expand Down
7 changes: 4 additions & 3 deletions external/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Remove the need for malloc.
- Added "len" parameter to b64_decode.
- Removed NULL termination from b64_encode.
- Made b64_isvalidchar, b64invs and b64chars static.
Modified by: @ThePedroo
*/
Expand All @@ -17,7 +18,7 @@

#include "base64.h"

const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

size_t b64_encoded_size(size_t inlen) {
size_t ret;
Expand Down Expand Up @@ -62,7 +63,7 @@ char *b64_encode(const unsigned char *in, char *out, size_t len) {
return out;
}

int b64invs[] = { 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,
static int b64invs[] = { 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, -1, -1, -1, 26, 27, 28,
Expand All @@ -87,7 +88,7 @@ size_t b64_decoded_size(const char *in, size_t len) {
return ret;
}

int b64_isvalidchar(char c) {
static int b64_isvalidchar(char c) {
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
Expand Down
1 change: 1 addition & 0 deletions external/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Remove the need for malloc.
- Added "len" parameter to b64_decode.
- Removed NULL termination from b64_encode.
- Made b64_isvalidchar, b64invs and b64chars static.
Modified by: @ThePedroo
*/
Expand Down
14 changes: 7 additions & 7 deletions external/csocket-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "csocket-client.h"

void _csocket_client_close(struct csocket_client *client) {
static void _csocket_client_close(struct csocket_client *client) {
#ifdef _WIN32
closesocket(client->socket);
WSACleanup();
Expand Down Expand Up @@ -100,9 +100,9 @@ int csocket_client_init(struct csocket_client *client, bool secure, char *hostna
return CSOCKET_CLIENT_SUCCESS;
}

int csocket_client_send(struct csocket_client *client, char *data, int size) {
int csocket_client_send(struct csocket_client *client, char *data, size_t size) {
if (client->secure) {
int ret = pcll_send(&client->connection, data, size);
int ret = pcll_send(&client->connection, data, (int)size);
if (ret == PCLL_ERROR) {
perror("[csocket-client]: Failed to send data");

Expand All @@ -119,18 +119,18 @@ int csocket_client_send(struct csocket_client *client, char *data, int size) {
return CSOCKET_CLIENT_SUCCESS;
}

int csocket_client_recv(struct csocket_client *client, char *buffer, int size) {
long csocket_client_recv(struct csocket_client *client, char *buffer, size_t size) {
if (client->secure) {
int recv_length = pcll_recv(&client->connection, buffer, size);
int recv_length = pcll_recv(&client->connection, buffer, (int)size);
if (recv_length == PCLL_ERROR) {
perror("[csocket-client]: Failed to receive data");

return CSOCKET_CLIENT_ERROR;
}

return recv_length;
return (long)recv_length;
} else {
int recv_length = recv(client->socket, buffer, size, 0);
long recv_length = recv(client->socket, buffer, size, 0);
if (recv_length < 0) {
perror("[csocket-client]: Failed to receive data");

Expand Down
4 changes: 2 additions & 2 deletions external/csocket-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct csocket_client {

int csocket_client_init(struct csocket_client *client, bool secure, char *hostname, unsigned short port);

int csocket_client_send(struct csocket_client *client, char *data, int size);
int csocket_client_send(struct csocket_client *client, char *data, size_t size);

int csocket_client_recv(struct csocket_client *client, char *buffer, int size);
long csocket_client_recv(struct csocket_client *client, char *buffer, size_t size);

void csocket_client_close(struct csocket_client *client);

Expand Down
6 changes: 3 additions & 3 deletions external/csocket-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int csocket_server_accept(struct csocket_server server, struct csocket_server_cl
return 0;
}

int csocket_server_send(struct csocket_server_client *client, char *data, int length) {
int csocket_server_send(struct csocket_server_client *client, char *data, size_t length) {
#ifdef CSOCKET_SECURE
if (pcll_send(client->connection, data, length) < 0) {
perror("[csocket-server]: Failed to send data");
Expand Down Expand Up @@ -144,7 +144,7 @@ int csocket_close_client(struct csocket_server_client *client) {
return 0;
}

int csocket_server_recv(struct csocket_server_client *client, char *buffer, int length) {
long csocket_server_recv(struct csocket_server_client *client, char *buffer, size_t length) {
#ifdef CSOCKET_SECURE
int bytes = pcll_recv(client->connection, buffer, length);
if (bytes <= 0) {
Expand All @@ -155,7 +155,7 @@ int csocket_server_recv(struct csocket_server_client *client, char *buffer, int

return bytes;
#else
int bytes = recv(client->socket, buffer, length, 0);
long bytes = recv(client->socket, buffer, length, 0);
if (bytes < 0) {
perror("[csocket-server]: Failed to receive data");

Expand Down
4 changes: 2 additions & 2 deletions external/csocket-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ int csocket_server_init(struct csocket_server *server);

int csocket_server_accept(struct csocket_server server, struct csocket_server_client *client);

int csocket_server_send(struct csocket_server_client *client, char *data, int length);
int csocket_server_send(struct csocket_server_client *client, char *data, size_t length);

int csocket_close_client(struct csocket_server_client *client);

int csocket_server_recv(struct csocket_server_client *client, char *buffer, int length);
long csocket_server_recv(struct csocket_server_client *client, char *buffer, size_t length);

int csocket_server_close(struct csocket_server *server);

Expand Down
40 changes: 19 additions & 21 deletions external/pcll.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ int pcll_init_only_ssl(struct pcll_connection *connection) {

ret = SSL_set_fd(connection->ssl, fd);
if (ret != SSL_SUCCESS) {
SSL_free(connection->ssl);
SSL_CTX_free(connection->ctx);
SSL_free(connection->ssl);
SSL_CTX_free(connection->ctx);

return ret;
return ret;
}

return PCLL_SUCCESS;
Expand All @@ -248,10 +248,10 @@ int pcll_init_only_ssl(struct pcll_connection *connection) {

ret = wolfSSL_set_fd(connection->ssl, fd);
if (ret != WOLFSSL_SUCCESS) {
wolfSSL_free(connection->ssl);
wolfSSL_CTX_free(connection->ctx);
wolfSSL_free(connection->ssl);
wolfSSL_CTX_free(connection->ctx);

return ret;
return ret;
}

return PCCL_SUCCESS;
Expand Down Expand Up @@ -324,20 +324,18 @@ int pcll_connect(struct pcll_connection *connection) {
context ? NULL : &connection->context,
&outdesc,
&flags,
NULL);
NULL
);

context = &connection->context;

if (inbuffers[1].BufferType == SECBUFFER_EXTRA) {
/* TODO: use ANSI C function */
MoveMemory(connection->incoming, connection->incoming + (connection->received - inbuffers[1].cbBuffer), inbuffers[1].cbBuffer);
memmove(connection->incoming, connection->incoming + (connection->received - inbuffers[1].cbBuffer), inbuffers[1].cbBuffer);

connection->received = inbuffers[1].cbBuffer;
}

else {
connection->received = 0;
}
else connection->received = 0;

if (sec == SEC_E_OK) break;

Expand All @@ -348,11 +346,11 @@ int pcll_connect(struct pcll_connection *connection) {
int size = outbuffers[0].cbBuffer;

while (size != 0) {
int d = send(connection->socket, buffer, size, 0);
if (d <= 0) break;
int ret = send(connection->socket, buffer, size, 0);
if (ret <= 0) break;

size -= d;
buffer += d;
size -= ret;
buffer += ret;
}

FreeContextBuffer(outbuffers[0].pvBuffer);
Expand All @@ -364,12 +362,12 @@ int pcll_connect(struct pcll_connection *connection) {

if (connection->received == sizeof(connection->incoming)) goto fail;

int r = recv(connection->socket, connection->incoming + connection->received, sizeof(connection->incoming) - connection->received, 0);
if (r == 0) return PCLL_SUCCESS;
int ret = recv(connection->socket, connection->incoming + connection->received, sizeof(connection->incoming) - connection->received, 0);
if (ret == 0) return PCLL_SUCCESS;

else if (r < 0) goto fail;
else if (ret < 0) goto fail;

connection->received += r;
connection->received += ret;
}

QueryContextAttributes(context, SECPKG_ATTR_STREAM_SIZES, &connection->sizes);
Expand Down Expand Up @@ -430,7 +428,7 @@ int pcll_get_error(struct pcll_connection *connection, int error) {
#endif
}

int pcll_send(struct pcll_connection* connection, char* data, int length) {
int pcll_send(struct pcll_connection* connection, char *data, int length) {
#if PCLL_SSL_LIBRARY == PCLL_OPENSSL
int ret = SSL_write(connection->ssl, data, length);
if (ret != length) {
Expand Down
22 changes: 11 additions & 11 deletions external/pdvoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void pdvoice_update_server(struct pdvoice *connection, char *endpoint, char *tok
connection->ws_connection_info->token = token;
}

void _pdvoice_on_connect(struct httpclient_response *client, void *user_data) {
static void _pdvoice_on_connect(struct httpclient_response *client, void *user_data) {
struct pdvoice *connection = user_data;

struct pjsonb jsonb;
Expand All @@ -59,12 +59,12 @@ void _pdvoice_on_connect(struct httpclient_response *client, void *user_data) {

pjsonb_end(&jsonb);

frequenc_send_text_ws_client(client, jsonb.string, jsonb.position);
frequenc_send_text_ws_client(client, jsonb.string, (size_t)jsonb.position);

pjsonb_free(&jsonb);
}

void _pdvoice_on_close(struct httpclient_response *client, struct frequenc_ws_frame *message, void *user_data) {
static void _pdvoice_on_close(struct httpclient_response *client, struct frequenc_ws_frame *message, void *user_data) {
(void)client;

struct pdvoice *connection = user_data;
Expand All @@ -74,7 +74,7 @@ void _pdvoice_on_close(struct httpclient_response *client, struct frequenc_ws_fr
pdvoice_free(connection);
}

void *_pdvoice_udp(void *data) {
static void *_pdvoice_udp(void *data) {
struct _pdvoice_udp_thread_data *thread_data = data;

#ifdef _WIN32
Expand Down Expand Up @@ -166,9 +166,9 @@ void *_pdvoice_udp(void *data) {
cthreads_mutex_unlock(thread_data->connection->mutex);

#ifdef _WIN32
int sent = sendto(udp_socket, (const char *)discovery_buffer, sizeof(discovery_buffer), 0, (struct sockaddr *)&destination, sizeof(destination));
long sent = sendto(udp_socket, (const char *)discovery_buffer, sizeof(discovery_buffer), 0, (struct sockaddr *)&destination, sizeof(destination));
#else
int sent = sendto(udp_socket, discovery_buffer, sizeof(discovery_buffer), 0, (struct sockaddr *)&destination, sizeof(destination));
long sent = sendto(udp_socket, discovery_buffer, sizeof(discovery_buffer), 0, (struct sockaddr *)&destination, sizeof(destination));
#endif
if (sent == -1) {
perror("[pdvoice]: Failed to send discovery packet to UDP socket");
Expand All @@ -180,7 +180,7 @@ void *_pdvoice_udp(void *data) {
struct sockaddr_in udp_client;
socklen_t udp_client_length = sizeof(udp_client);

int received = recvfrom(udp_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&udp_client, &udp_client_length);
long received = recvfrom(udp_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&udp_client, &udp_client_length);
if (received == -1) {
perror("[pdvoice]: Failed to receive from UDP socket");

Expand Down Expand Up @@ -218,7 +218,7 @@ void *_pdvoice_udp(void *data) {

cthreads_mutex_lock(thread_data->connection->mutex);

frequenc_send_text_ws_client(thread_data->client, buffer, buffer_length);
frequenc_send_text_ws_client(thread_data->client, buffer, (size_t)buffer_length);

cthreads_mutex_unlock(thread_data->connection->mutex);

Expand All @@ -236,7 +236,7 @@ void *_pdvoice_udp(void *data) {
return NULL;
}

void *_pdvoice_hb_interval(void *data) {
static void *_pdvoice_hb_interval(void *data) {
struct _pdvoice_hb_thread_data *thread_data = data;

char *buffer = "{\"op\":3,\"d\":null}";
Expand All @@ -254,7 +254,7 @@ void *_pdvoice_hb_interval(void *data) {
return NULL;
}

void _pdvoice_on_message(struct httpclient_response *client, struct frequenc_ws_frame *message, void *user_data) {
static void _pdvoice_on_message(struct httpclient_response *client, struct frequenc_ws_frame *message, void *user_data) {
struct pdvoice *connection = user_data;

printf("[pdvoice]: Received message from Discord voice gateway: %.*s\n", (int)message->payload_length, message->buffer);
Expand Down Expand Up @@ -375,7 +375,7 @@ void _pdvoice_on_message(struct httpclient_response *client, struct frequenc_ws_
}
}

void *_pdvoice_connect(void *data) {
static void *_pdvoice_connect(void *data) {
struct pdvoice *connection = data;

char path[4 + 1 + 1];
Expand Down
Loading

0 comments on commit cc48a34

Please sign in to comment.