From 18741af602f09a00fe0c8c67ce4ca10d793e800b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Thu, 24 Nov 2016 15:26:42 +0100 Subject: [PATCH] Add basic config file --- Makefile | 2 +- cfg | 2 ++ configFile.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ configFile.h | 33 ++++++++++++++++++ main.cpp | 69 +++++++++++++++----------------------- 5 files changed, 152 insertions(+), 44 deletions(-) create mode 100644 cfg create mode 100644 configFile.cpp create mode 100644 configFile.h diff --git a/Makefile b/Makefile index 7dd165b..952266b 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/cfg b/cfg new file mode 100644 index 0000000..266a542 --- /dev/null +++ b/cfg @@ -0,0 +1,2 @@ +id a0ae025e0df5f0a6 +bootstrap 2460e2b01bc8d704 ::ffff:81.194.27.155 1212 diff --git a/configFile.cpp b/configFile.cpp new file mode 100644 index 0000000..fc98087 --- /dev/null +++ b/configFile.cpp @@ -0,0 +1,90 @@ +/*************************************************************************** + * By Théophile Bastian, 2017 + * M1 Network course project at ENS Cachan, Juliusz Chroboczek. + * License: WTFPL v2 + **************************************************************************/ + +#include "configFile.h" +#include +#include +#include +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; +} + diff --git a/configFile.h b/configFile.h new file mode 100644 index 0000000..de09ef3 --- /dev/null +++ b/configFile.h @@ -0,0 +1,33 @@ +/*************************************************************************** + * By Théophile Bastian, 2017 + * M1 Network course project at ENS Cachan, Juliusz Chroboczek. + * License: WTFPL v2 + **************************************************************************/ + +#pragma once + +#include +#include +#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& getBootstrapNodes() const { + return bootstrapNodes; + }; + /** Bootstrap nodes. No setter: wouldn't be useful. */ + + private: + u64 selfId; + std::vector bootstrapNodes; +}; + diff --git a/main.cpp b/main.cpp index e1ff925..4c81bd9 100644 --- a/main.cpp +++ b/main.cpp @@ -9,54 +9,51 @@ #include "nw_constants.h" #include "neighbours.h" #include "packetParser.h" +#include "configFile.h" #include #include #include #include #include -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; }