[Add] Started working on the plain network plugin. We'll also add SSL
version.
This commit is contained in:
parent
7c85c2bc8b
commit
cf89bb0ce5
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.DS_Store
|
||||
*.log
|
||||
tmp/
|
||||
*.swp
|
0
base/Makefile
Normal file
0
base/Makefile
Normal file
5
base/config/api.h
Normal file
5
base/config/api.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#define PLUGIN_TYPE_NETWOR 0x1
|
||||
|
||||
void configErrorPush(const char* err);
|
54
base/network.c
Normal file
54
base/network.c
Normal 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
11
base/network.h
Normal 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;
|
Loading…
Reference in New Issue
Block a user