Skip to content

Commit

Permalink
add: csocket-client library
Browse files Browse the repository at this point in the history
This commit adds "csocket-client" library for future Windows compability.
  • Loading branch information
ThePedroo committed May 6, 2024
1 parent f16a867 commit 8845e30
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 345 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ The standalone audio-sending node written in C.
## Dependencies

- [`OpenSSL`](https://www.openssl.org/)

> [!NOTE]
> The PerformanC Organization recommends the use of the latest version of OpenSSL.
- [`OpenSSL`](https://www.openssl.org/) or [`WolfSSL`](https://www.wolfssl.com/)

## Installation

> [!IMPORTANT]
> FrequenC as of now only supports Linux. Windows support is WIP and experimental.
### 1. Clone the repository

```shell
Expand Down
149 changes: 149 additions & 0 deletions external/csocket-client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#endif

#include "pcll.h"

#include "csocket-client.h"

void _csocket_client_close(struct csocket_client *client) {
#ifdef _WIN32
closesocket(client->socket);
WSACleanup();
#else
close(client->socket);
#endif

if (client->secure) {
pcll_shutdown(&client->connection);
pcll_free(&client->connection);
}
}

int csocket_client_init(struct csocket_client *client, bool secure, char *hostname, int port) {
#ifdef _WIN32
if (WSAStartup(MAKEWORD(2,2), &client->wsa) != 0) {
perror("[csocket-client]: Failed to initialize Winsock");

return -1;
}
#endif

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

return -1;
}

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

return -1;
}

struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = *((struct in_addr *)host->h_addr_list[0])
};

if (connect(client->socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("[csocket-client]: Failed to connect to server");

return -1;
}

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

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

return -1;
}

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

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

return -1;
}

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

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

return -1;
}

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));

return -1;
}
}

return CSOCKET_CLIENT_SUCCESS;
}

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

return CSOCKET_CLIENT_ERROR;
}
} else {
if (send(client->socket, data, size, 0) < 0) {
perror("[csocket-client]: Failed to send data");

return CSOCKET_CLIENT_ERROR;
}
}

return CSOCKET_CLIENT_SUCCESS;
}

int csocket_client_recv(struct csocket_client *client, char *buffer, size_t 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");

return CSOCKET_CLIENT_ERROR;
}

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

return CSOCKET_CLIENT_ERROR;
}

return recv_length;
}

return CSOCKET_CLIENT_SUCCESS;
}

void csocket_client_close(struct csocket_client *client) {
_csocket_client_close(client);
}
38 changes: 38 additions & 0 deletions external/csocket-client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef CSOCKET_CLIENT_H
#define CSOCKET_CLIENT_H

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#endif

#include "pcll.h"
#include "types.h"

#define CSOCKET_CLIENT_SUCCESS 0
#define CSOCKET_CLIENT_ERROR -1

struct csocket_client {
#ifdef _WIN32
SOCKET socket;
#else
int socket;
#endif
struct pcll_connection connection;
bool secure;
};

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

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

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

void csocket_client_close(struct csocket_client *client);

#endif /* CSOCKET_CLIENT_H */
Loading

0 comments on commit 8845e30

Please sign in to comment.