[Add] Started working on the plain network plugin. We'll also add SSL

version.
This commit is contained in:
Ritchie Cunningham 2024-11-22 23:41:29 +00:00
parent 7c85c2bc8b
commit cf89bb0ce5
5 changed files with 74 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.DS_Store
*.log
tmp/
*.swp

0
base/Makefile Normal file
View File

5
base/config/api.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#define PLUGIN_TYPE_NETWOR 0x1
void configErrorPush(const char* err);

54
base/network.c Normal file
View File

@ -0,0 +1,54 @@
#include <config/api.h>
#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);
}

11
base/network.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
typedef struct {
int socket;
} NETWORK_PLAIN;