Add basic config file
This commit is contained in:
parent
d4eccda003
commit
18741af602
5 changed files with 152 additions and 44 deletions
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@ CXX=g++
|
||||||
CXXFLAGS=-Wall -Wextra -Werror -pedantic -std=c++14 -O2
|
CXXFLAGS=-Wall -Wextra -Werror -pedantic -std=c++14 -O2
|
||||||
CXXLIBS=-lpthread
|
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
|
TARGET = jeanhubert
|
||||||
|
|
||||||
all: $(TARGET)
|
all: $(TARGET)
|
||||||
|
|
2
cfg
Normal file
2
cfg
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
id a0ae025e0df5f0a6
|
||||||
|
bootstrap 2460e2b01bc8d704 ::ffff:81.194.27.155 1212
|
90
configFile.cpp
Normal file
90
configFile.cpp
Normal 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
33
configFile.h
Normal 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;
|
||||||
|
};
|
||||||
|
|
69
main.cpp
69
main.cpp
|
@ -9,54 +9,51 @@
|
||||||
#include "nw_constants.h"
|
#include "nw_constants.h"
|
||||||
#include "neighbours.h"
|
#include "neighbours.h"
|
||||||
#include "packetParser.h"
|
#include "packetParser.h"
|
||||||
|
#include "configFile.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <unistd.h>
|
#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);
|
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;
|
SockAddr addr;
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sin6_family = AF_INET6;
|
addr.sin6_family = AF_INET6;
|
||||||
addr.sin6_port = htons(csts::DEFAULT_PORT);
|
addr.sin6_port = htons(csts::DEFAULT_PORT);
|
||||||
|
|
||||||
u64 myId=0;
|
printf("%lX\n", cfg.getSelfId());
|
||||||
for(int i=0; i < 8; i++) {
|
|
||||||
myId <<= 8;
|
|
||||||
myId += rand() % (1<<8);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%lu\n", myId);
|
Protocol proto(addr, cfg.getSelfId());
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
Neighbours neighboursManager(&proto);
|
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);
|
PacketParser pckParser(&neighboursManager, &proto);
|
||||||
|
|
||||||
// proto.sendEmpty(loc_addr);
|
|
||||||
// proto.sendEmpty(jch_addr);
|
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
neighboursManager.fullUpdate();
|
neighboursManager.fullUpdate();
|
||||||
|
|
||||||
|
@ -69,20 +66,6 @@ int main(int /*argc*/, char** /*argv*/) {
|
||||||
sleep(2);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue