Add basic config file

This commit is contained in:
Théophile Bastian 2016-11-24 15:26:42 +01:00
parent d4eccda003
commit 18741af602
5 changed files with 152 additions and 44 deletions

View file

@ -2,7 +2,7 @@ CXX=g++
CXXFLAGS=-Wall -Wextra -Werror -pedantic -std=c++14 -O2
CXXLIBS=-lpthread
OBJS = Bytes.o main.o protocol.o neighbours.o packetParser.o
OBJS = Bytes.o main.o protocol.o neighbours.o packetParser.o configFile.o
TARGET = jeanhubert
all: $(TARGET)

2
cfg Normal file
View file

@ -0,0 +1,2 @@
id a0ae025e0df5f0a6
bootstrap 2460e2b01bc8d704 ::ffff:81.194.27.155 1212

90
configFile.cpp Normal file
View file

@ -0,0 +1,90 @@
/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2 <http://www.wtfpl.net/>
**************************************************************************/
#include "configFile.h"
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
ConfigFile::ConfigFile() {
selfId=0;
for(int i=0; i < 8; i++) {
selfId <<= 8;
selfId += rand() % 0xFF;
}
}
bool ConfigFile::read(const char* path) {
ifstream handle(path);
if(!handle.is_open())
return false;
while(handle.good()) {
string attr;
handle >> attr;
if(attr == "id") {
handle >> hex >> selfId >> dec;
}
else if(attr == "bootstrap") {
u16 port;
u64 id;
SockAddr addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
string addrStr;
handle >> hex >> id >> dec;
handle >> addrStr;
handle >> port;
addr.sin6_port = htons(port);
if(inet_pton(AF_INET6, addrStr.c_str(), &(addr.sin6_addr)) != 1) {
//TODO proper log
fprintf(stderr, "Could not convert '%s' to IPv6 address\n",
addrStr.c_str());
continue;
}
bootstrapNodes.push_back(Neighbour(id, addr));
}
else if(attr.empty())
continue;
else {
//TODO proper log
fprintf(stderr, "Unknown configuration item: '%s'\n",
attr.c_str());
continue;
}
}
handle.close();
return true;
}
bool ConfigFile::write(const char* path) {
ofstream handle(path, ofstream::out | ofstream::trunc);
if(!handle.is_open())
return false;
handle << "id " << hex << selfId << dec << '\n';
for(Neighbour nei : bootstrapNodes) {
char addr[54];
if(inet_ntop(AF_INET6, &nei.addr.sin6_addr, addr, 54) == NULL) {
perror("Could not convert IPv6 back to string");
continue;
}
handle << "bootstrap " << hex << nei.id << dec << ' '
<< addr << ' ' << ntohs(nei.addr.sin6_port) << '\n';
}
handle.close();
return true;
}

33
configFile.h Normal file
View file

@ -0,0 +1,33 @@
/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2 <http://www.wtfpl.net/>
**************************************************************************/
#pragma once
#include <vector>
#include <string>
#include "data.h"
class ConfigFile {
public:
ConfigFile();
bool read(const char* path);
bool write(const char* path);
u64 getSelfId() const { return selfId; }
void setSelfId(u64 id) { selfId = id; }
/** Self id */
const std::vector<Neighbour>& getBootstrapNodes() const {
return bootstrapNodes;
};
/** Bootstrap nodes. No setter: wouldn't be useful. */
private:
u64 selfId;
std::vector<Neighbour> bootstrapNodes;
};

View file

@ -9,54 +9,51 @@
#include "nw_constants.h"
#include "neighbours.h"
#include "packetParser.h"
#include "configFile.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
int main(int /*argc*/, char** /*argv*/) {
int main(int argc, char** argv) {
bool hasConfig = false;
char* configFilePath = nullptr;
if(argc > 1) {
hasConfig = true;
configFilePath = argv[1];
}
srand(time(NULL)+42);
ConfigFile cfg;
if(hasConfig) {
if(!cfg.read(configFilePath) || !cfg.write(configFilePath)) {
fprintf(stderr, "Could not read/write on '%s'.\n", configFilePath);
exit(1);
}
}
SockAddr addr;
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(csts::DEFAULT_PORT);
u64 myId=0;
for(int i=0; i < 8; i++) {
myId <<= 8;
myId += rand() % (1<<8);
}
printf("%lX\n", cfg.getSelfId());
printf("%lu\n", myId);
SockAddr jch_addr;
memset(&jch_addr, 0, sizeof(jch_addr));
jch_addr.sin6_family = AF_INET6;
jch_addr.sin6_port = htons(1212);
int rc = inet_pton(AF_INET6, "::FFFF:81.194.27.155", &jch_addr.sin6_addr);
if(rc != 1) {
if(rc == 0)
fprintf(stderr, "Address uses an invalid format.\n");
else
perror("Cannot convert JCh address");
exit(1);
}
u64 jch_id = 0x43e3a5e0;
jch_id <<= 32;
jch_id += 0x10095a0f;
Protocol proto(addr, myId);
Protocol proto(addr, cfg.getSelfId());
Neighbours neighboursManager(&proto);
neighboursManager.addPotentialNei(Neighbour(jch_id, jch_addr));
for(const Neighbour& nei : cfg.getBootstrapNodes()) {
char addr[54];
inet_ntop(AF_INET6, &nei.addr.sin6_addr, addr, 54);
printf("Neigh: %lX [%s]:%hu\n", nei.id, addr,
ntohs(nei.addr.sin6_port));
neighboursManager.addPotentialNei(nei);
}
PacketParser pckParser(&neighboursManager, &proto);
// proto.sendEmpty(loc_addr);
// proto.sendEmpty(jch_addr);
while(true) {
neighboursManager.fullUpdate();
@ -69,20 +66,6 @@ int main(int /*argc*/, char** /*argv*/) {
sleep(2);
}
/*
SockAddr loc_addr;
memset(&loc_addr, 0, sizeof(loc_addr));
loc_addr.sin6_family = AF_INET6;
loc_addr.sin6_port = htons(1212);
rc = inet_pton(AF_INET6, "::FFFF:127.0.0.1", &loc_addr.sin6_addr);
if(rc != 1) {
fprintf(stderr, "Error.");
exit(1);
}
while(true);
*/
return 0;
}