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 a0212d8
Show file tree
Hide file tree
Showing 26 changed files with 672 additions and 323 deletions.
33 changes: 22 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
CC ?= clang

# FrequenC Runtime Options
PORT = 8888# Port that FrequenC will listen on.
AUTHORIZATION = youshallnotpass# Authorization header for the server. Secure it with a strong password.
AUDIO_QUALITY = 1# Audio quality. 1 = Best, 2 = Good, 3 = Normal, 4 = Bad, 5 = Worst.

# Experiments
TCPLIMITS_EXPERIMENTAL_SAVE_MEMORY = 1 # Save memory by using a lower layers limits instead of the TCP limit itself.

# Security
# CSOCKET_SECURE = 1 # Secure the connection with SSL/TLS.
# CSOCKET_KEY = "key.pem" # SSL/TLS key.
# CSOCKET_CERT = "cert.pem" # SSL/TLS certificate.
PORT = 8888# Port that FrequenC will listen on.
AUTHORIZATION = youshallnotpass# Authorization header for the server. Secure it with a strong password.
ALLOW_UNSECURE_RANDOM = 0# Only use this for unsupported platforms.

# SSL/TLS

OPENSSL=1
WOLFSSL=2

Expand All @@ -29,8 +31,18 @@ GITHUB_BRANCH ?= unknown# The branch that the commit was made on.
SRC_DIR = lib external sources
OBJ_DIR = obj

CVERSION = -std=c99
CFLAGS ?= -Ofast -march=native -fno-signed-zeros -fno-trapping-math -funroll-loops
CFLAGS ?= -std=c99 -Ofast -march=native -fno-signed-zeros -fno-trapping-math -funroll-loops

ifeq ($(CC),gcc)
CFLAGS += -fanalyzer -Wno-analyzer-fd-leak -flto -Wpedantic -Wall -Wextra -Werror -Wformat -Wuninitialized -Wshadow -Wno-stringop-overread
else ifeq ($(CC),clang)
CFLAGS += -Wpedantic -Wall -Wextra -Werror -Wformat -Wuninitialized -Wshadow -flto=thin
else ifeq ($(CC),zig cc)
CFLAGS += -flto=thin -Wpedantic -Wall -Wextra -Werror -Wformat -Wuninitialized -Wshadow
else
CFLAGS += -Wpedantic -Wall -Wextra -Werror -Wformat -Wuninitialized -Wshadow -Wno-stringop-overread
endif

LDFLAGS ?= -Iinclude -Iexternal -Isources

ifeq ($(OS),Windows_NT)
Expand All @@ -39,23 +51,22 @@ else
LDFLAGS += -pthread
endif

OPTIONS = -DPORT=$(PORT) -DAUTHORIZATION=\"$(AUTHORIZATION)\" -DALLOW_UNSECURE_RANDOM=$(ALLOW_UNSECURE_RANDOM) $(if $(CSOCKET_SECURE),-DCSOCKET_SECURE -DCSOCKET_KEY=\"$(CSOCKET_KEY)\" -DCSOCKET_CERT=\"$(CSOCKET_CERT)\",) $(if $(TCPLIMITS_EXPERIMENTAL_SAVE_MEMORY),-DTCPLIMITS_EXPERIMENTAL_SAVE_MEMORY,) -DHARDCODED_SESSION_ID=$(HARDCODED_SESSION_ID) -DGITHUB_COMMIT_SHA=\"$(GITHUB_COMMIT_SHA)\" -DGITHUB_BRANCH=\"$(GITHUB_BRANCH)\" -DPCLL_SSL_LIBRARY=$(PCLL_SSL_LIBRARY)
CHECK_FLAGS = -Wpedantic -Wall -Wextra -Werror -Wformat -Wuninitialized -Wshadow -Wno-stringop-overread
OPTIONS = -DPORT=$(PORT) -DAUTHORIZATION=\"$(AUTHORIZATION)\" -DALLOW_UNSECURE_RANDOM=$(ALLOW_UNSECURE_RANDOM) $(if $(CSOCKET_SECURE),-DCSOCKET_SECURE -DCSOCKET_KEY=\"$(CSOCKET_KEY)\" -DCSOCKET_CERT=\"$(CSOCKET_CERT)\",) $(if $(TCPLIMITS_EXPERIMENTAL_SAVE_MEMORY),-DTCPLIMITS_EXPERIMENTAL_SAVE_MEMORY,) -DHARDCODED_SESSION_ID=$(HARDCODED_SESSION_ID) -DGITHUB_COMMIT_SHA=\"$(GITHUB_COMMIT_SHA)\" -DGITHUB_BRANCH=\"$(GITHUB_BRANCH)\" -DPCLL_SSL_LIBRARY=$(PCLL_SSL_LIBRARY) -DAUDIO_QUALITY=$(AUDIO_QUALITY)

SRCS = $(foreach dir,$(SRC_DIR),$(wildcard $(dir)/*.c))
OBJS = $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(SRCS)))

FrequenC: $(OBJS)
$(CC) $^ -o $@ $(CVERSION) $(CFLAGS) $(CHECK_FLAGS) $(OPTIONS) $(LDFLAGS) $(SSL_FLAGS)
$(CC) $^ -o $@ $(CFLAGS) $(OPTIONS) $(LDFLAGS) $(SSL_FLAGS)

$(OBJ_DIR)/%.o: lib/%.c | $(OBJ_DIR)
$(CC) -c $< -o $@ $(CVERSION) $(CFLAGS) $(CHECK_FLAGS) $(OPTIONS) $(LDFLAGS)
$(CC) -c $< -o $@ $(CFLAGS) $(OPTIONS) $(LDFLAGS)

$(OBJ_DIR)/%.o: external/%.c | $(OBJ_DIR)
$(CC) -c $< -o $@ $(CVERSION) $(CFLAGS) $(CHECK_FLAGS) $(OPTIONS) $(LDFLAGS)
$(CC) -c $< -o $@ $(CFLAGS) $(OPTIONS) $(LDFLAGS)

$(OBJ_DIR)/%.o: sources/%.c | $(OBJ_DIR)
$(CC) -c $< -o $@ $(CVERSION) $(CFLAGS) $(CHECK_FLAGS) $(OPTIONS) $(LDFLAGS)
$(CC) -c $< -o $@ $(CFLAGS) $(OPTIONS) $(LDFLAGS)

$(OBJ_DIR):
mkdir -p $@
Expand Down
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
Loading

0 comments on commit a0212d8

Please sign in to comment.