Removed Adapter queue, Sessions now have their adapters created and attached

This commit is contained in:
Tristan B. Velloza Kildaire 2022-01-01 12:29:40 +02:00
parent 6d3c331e4b
commit 383603d455
5 changed files with 57 additions and 96 deletions

124
alpha.c
View File

@ -8,35 +8,49 @@
* Prototyopes
*/
Adapter* createDevice(char*);
void addAdapter(Adapter*);
void listAdapters();
/**
* Global session state management
*/
Adapter* adapterQueue = NULL;
void setupAdapters(Session*);
int main()
{
printf("Starting alphavpn...\n");
/**
* TODO: Testing code
* 1. Create a device named `alpha0`
*/
Adapter* adapter1 = createDevice("alpha0");
listAdapters();
addAdapter(adapter1);
listAdapters();
Session* sessionHead = getSessionConfig();
if(sessionHead)
{
setupAdapters(sessionHead);
}
else
{
printf("Error when reading configuration file\n");
}
Adapter* adapter2 = createDevice("alpha1");
addAdapter(adapter2);
listAdapters();
}
test();
/**
* Setup the adapters for each session
*/
void setupAdapters(Session* session)
{
while(session)
{
// TODO: Implement me
Adapter* newAdapter = createDevice(session->requestedInterface);
/**
* If the creation of a new Adapter succeeds,
* i.e. the allocation of a new TUN or TAP adapter
* if successful, then attach this adapter to
* its related Session
*/
if(newAdapter)
{
session->adapter = *newAdapter;
}
/* Move onto next Session */
session = session->next;
}
}
/**
@ -58,79 +72,17 @@ Adapter* createDevice(char* name)
adapter->fd = 0;
/* TODO: Fetch final name */
adapter->interfaceName = "poes";
adapter->interfaceName = name;
/* NULL out `next` ptr */
adapter->next = NULL;
return adapter;
}
else
{
/* TODO: Remove this later */
printf("Allocation failure for device name '%s'!", name);
}
return adapter;
}
/**
* Add the given Adapter to the list of
* adapters
*/
void addAdapter(Adapter* adapter)
{
/* Only add the adapter if it isn't NULL */
if(adapter)
{
Adapter* current = adapterQueue;
/**
* If the wueue is empty then we
* make the head of the queue the
* adapter to be added
*/
if(adapterQueue == NULL)
{
adapterQueue = adapter;
}
/**
* Else, we iterate till the tail
* of the adapter queue and add it
* to the tail
*/
else
{
while(current->next)
{
current = current->next;
}
current->next = adapter;
}
}
}
/**
* Lists all the current adapaters
*/
void listAdapters()
{
Adapter* current = adapterQueue;
uint_least8_t adapterCnt = 0;
if(current)
{
do
{
printf("Adapter (%u): %s\n", adapterCnt, current->interfaceName);
adapterCnt++;
}
while((current = current->next));
}
else
{
/* TODO: Error */
printf("Error iterating adapter queue, it is empty\n");
return 0;
}
}

View File

@ -10,9 +10,8 @@
* Prototypes
*/
Session* parseConfig(json_t*);
void test();
void test()
Session* getSessionConfig()
{
u_int8_t* configPath = "config.json";
@ -51,17 +50,19 @@ void test()
if(rootJSON)
{
/* Parse the configuration */
parseConfig(rootJSON);
return parseConfig(rootJSON);
}
/* On JSON parse failure */
else
{
printf("Error parsing configuration file: %s\n", jsonError.text);
return 0;
}
}
else
{
printf("Error allocating config file memory buffer\n");
return 0;
}
}
/* On stat() failure */
@ -69,17 +70,15 @@ void test()
{
/* TODO: Handle this */
printf("Error stat()'ing file\n");
return 0;
}
}
else
{
/* TODO: Remove this */
printf("Opening config file failed\n");
return 0;
}
}

View File

@ -5,5 +5,4 @@
#include<jansson.h>
#include "types.h"
void test();
Session* parseConfig(json_t*);
Session* getSessionConfig();

View File

@ -14,6 +14,16 @@
},
"session2" : {
"interface": "alpha1"
"interface": "alpha0",
"privateKey": "sdhjfhdsybGFYTBYHGHvHVhnY&U7tu6567567+",
"listen": "[::]:1001",
"peer": {
"publicKey": "fjdufyd7ht78UGH=",
"endpoint": "google.com:1111"
},
"hooks": {
"up": "fuck",
"down": "poes"
}
}
}

View File

@ -52,6 +52,7 @@ struct Session
Peer peer;
/* TODO: Add Adapter here */
Adapter adapter;
/* Hooks */