90 lines
1.9 KiB
C++
90 lines
1.9 KiB
C++
/***************************************************************************
|
|
* 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;
|
|
}
|
|
|