Compare commits

...

2 Commits

11 changed files with 148 additions and 2 deletions

8
Makefile Normal file
View File

@ -0,0 +1,8 @@
default:
mkdir -p bin
mkdir -p bin/base
cd base && make
clean:
rm -Rf bin/
cd base && make clean

View File

@ -0,0 +1,7 @@
default:
cd network_plain && make
cd network_ssl && make
clean:
cd network_plain && make clean
cd network_ssl && make clean

0
base/config/Makefile Normal file
View File

View File

@ -1,5 +1,20 @@
#pragma once
#define PLUGIN_TYPE_NETWOR 0x1
#define PLUGIN_TYPE_NETWORK 0x1
void configErrorPush(const char* err);
/* This is the common API for plugins. */
unsigned int pluginType();
const char* pluginName();
#if 0
#ifndef __NETWORK_H__
/* This one's for network plugins. */
void* pluginConnect(const char* host, int port);
int pluginSocketGet(void* connection);
int pluginReadData(void* connection, char* buffer, int buffer_len);
int pluginSendData(void* connection, const char* buffer, int buffer_len);
void* pluginSocketDone(void* connection);
#endif
#endif

View File

@ -0,0 +1,8 @@
SRC = network.c
OUTPUT = network_plain.so
CFLAGS += -fPIC -shared -Wl,-soname,network_plain.so -O3 -Wall -I../
default:
$(CC) $(CFLAGS) $(SRC) -o ../../bin/base/$(OUTPUT) $(LDFLAGS)
clean:

View File

@ -5,6 +5,10 @@ unsigned int pluginType() {
return PLUGIN_TYPE_NETWORK;
}
const char* pluginName() {
return "plain";
}
void* pluginConnect(const char* host, int port) {
NETWORK_PLAIN* connection;
struct sockaddr_in address;
@ -49,6 +53,12 @@ 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) {
int pluginSendData(NETWORK_PLAIN* connection, char* buffer, int buffer_len) {
return send(connection->socket, buffer, buffer_len, 0);
}
void* pluginSocketDone(NETWORK_PLAIN* connection) {
close(connection->socket);
free(connection);
return NULL;
}

View File

@ -0,0 +1,9 @@
SRC = network.c
OUTPUT = ../../bin/base/network_ssl.so
CFLAGS += -fPIC -shared -Wl,-soname,network_ssl.so -O3 -Wall -I../
LDFlags += -lssl
default:
$(CC) $(SRC) -o $(OUTPUT) $(CFLAGS) $(LDFLAGS)
clean:

View File

@ -0,0 +1,73 @@
#include <config/api.h>
#include "network.h"
unsigned int pluginType() {
return PLUGIN_TYPE_NETWORK;
}
const char* pluginName() {
return "ssl";
}
void* pluginConnect(const char* host, int port) {
NETWORK_SSL* connection;
if((connection = malloc(sizeof(BIO))) == NULL) {
configErrorPush("Unable to malloc");
return NULL;
}
connection->ctx == SSL_CTX_new(SSLv23_client_method());
if((connection->bio = BIO_new_ssl_connect(connection->ctx)) == NULL) {
configErrorPush("Unable to allocate a BIO");
free(connection);
return NULL;
}
BIO_get_SSL(connection->bio, &connection->ssl);
SSL_set_mode(connection->ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(connection->bio, host);
BIO_set_conn_int_port(connection->bio, &port);
if(BIO_do_connect(connection->bio) <= 0) {
configErrorPush("Unable to connect");
free(connection);
return NULL;
}
return connection;
}
int pluginSocketGet(NETWORK_SSL* connection) {
return BIO_get_fd(connection->bio, NULL);
}
int pluginReadData(NETWORK_SSL* connection, char* buffer, int buffer_len) {
int ret;
if((ret == BIO_read(connection->bio, buffer, buffer_len)) == 0)
return ECONNRESET;
else if(ret == -1)
return (!BIO_should_retry(connection->bio)) ? ECONNRESET : EWOULDBLOCK;
return ret;
}
int pluginSendData(NETWORK_SSL* connection, const char* buffer, int buffer_len) {
int ret;
if((ret = BIO_write(connection->bio, buffer, buffer_len)) <= 0) {
if(!BIO_should_retry(connection->bio))
return ECONNRESET;
return EWOULDBLOCK;
}
return ret;
}
void* pluginSocketDone(NETWORK_SSL* connection) {
BIO_free_all(connection->bio);
SSL_CTX_free(connection->ctx);
free(connection);
return NULL;
}

View File

@ -0,0 +1,16 @@
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <config/api.h>
typedef struct {
BIO* bio;
SSL_CTX* ctx;
SSL* ssl;
} NETWORK_SSL;

BIN
bin/base/network_plain.so Executable file

Binary file not shown.