From cf89bb0ce50c28df8def88833044326aee10688a Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Fri, 22 Nov 2024 23:41:29 +0000 Subject: [PATCH] [Add] Started working on the plain network plugin. We'll also add SSL version. --- .gitignore | 4 ++++ base/Makefile | 0 base/config/api.h | 5 +++++ base/network.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ base/network.h | 11 ++++++++++ 5 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 base/Makefile create mode 100644 base/config/api.h create mode 100644 base/network.c create mode 100644 base/network.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c89ec1e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +*.log +tmp/ +*.swp diff --git a/base/Makefile b/base/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/base/config/api.h b/base/config/api.h new file mode 100644 index 0000000..fb2c56f --- /dev/null +++ b/base/config/api.h @@ -0,0 +1,5 @@ +#pragma once + +#define PLUGIN_TYPE_NETWOR 0x1 + +void configErrorPush(const char* err); diff --git a/base/network.c b/base/network.c new file mode 100644 index 0000000..40481b9 --- /dev/null +++ b/base/network.c @@ -0,0 +1,54 @@ +#include +#include "network.h" + +unsigned int pluginType() { + return PLUGIN_TYPE_NETWORK; +} + +void* pluginConnect(const char* host, int port) { + NETWORK_PLAIN* connection; + struct sockaddr_in address; + struct hostent* hp; + + if((connection = malloc(sizeof(NETWORK_PLAIN))) == NULL) { + configErrorPush("Unable to malloc"); + return NULL; + } + + if((hp = gethostbyname(host)) == NULL) { + configErrorPush("Unable to resolve hostname"); + free(connection); + return NULL; + } + + if((connection->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + configErrorPush("Unable to create a socket"); + free(connection); + return NULL; + } + + address.sin_family = AF_INET; + address.sin_port = htons(port); + address.sin_addr.s_addr = *(u_long*)hp->h_addr; + + if(connect(connection->socket, (struct sockaddr*) &address, sizeof(struct sockaddr)) == -1) { + configErrorPush("Unable to establish a connection"); + free(connection); + return NULL; + } + + /* TODO: Non-blocking */ + return connection; +} + +int pluginSocketGet(NETWORK_PLAIN* connection) { + return connection->socket; +} + +int pluginReadData(NETWORK_PLAIN* connection, char* buffer, int buffer_len) { + return recv(connection->socket, buffer, buffer_len, 0); +} + +int pluginSendData(NETWORK_PLAI* connection, char* buffer, int buffer_len) { + return send(connection->socket, buffer, buffer_len, 0); +} diff --git a/base/network.h b/base/network.h new file mode 100644 index 0000000..98736cb --- /dev/null +++ b/base/network.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include +#include +#include +#include + +typedef struct { + int socket; +} NETWORK_PLAIN;