Compare commits
No commits in common. "d4eccda003ea95de50da126b73ad0bda1c503505" and "1527c1c6fea76969aa9b63a338052664e9ee7ef4" have entirely different histories.
d4eccda003
...
1527c1c6fe
5 changed files with 1 additions and 129 deletions
2
Makefile
2
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
|
||||
TARGET = jeanhubert
|
||||
|
||||
all: $(TARGET)
|
||||
|
|
10
main.cpp
10
main.cpp
|
@ -8,7 +8,6 @@
|
|||
#include "protocol.h"
|
||||
#include "nw_constants.h"
|
||||
#include "neighbours.h"
|
||||
#include "packetParser.h"
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
@ -52,20 +51,11 @@ int main(int /*argc*/, char** /*argv*/) {
|
|||
Neighbours neighboursManager(&proto);
|
||||
neighboursManager.addPotentialNei(Neighbour(jch_id, jch_addr));
|
||||
|
||||
PacketParser pckParser(&neighboursManager, &proto);
|
||||
|
||||
// proto.sendEmpty(loc_addr);
|
||||
// proto.sendEmpty(jch_addr);
|
||||
|
||||
while(true) {
|
||||
neighboursManager.fullUpdate();
|
||||
|
||||
while(proto.readyRead()) {
|
||||
SockAddr fromAddr;
|
||||
Bytes pck = proto.getPacket(&fromAddr);
|
||||
pckParser.parse(pck);
|
||||
}
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,88 +0,0 @@
|
|||
/***************************************************************************
|
||||
* By Théophile Bastian, 2017
|
||||
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
|
||||
* License: WTFPL v2 <http://www.wtfpl.net/>
|
||||
**************************************************************************/
|
||||
|
||||
#include "packetParser.h"
|
||||
#include <cstring>
|
||||
|
||||
PacketParser::PacketParser(Neighbours* nei, Protocol* proto) :
|
||||
neighbours(nei), protocol(proto)
|
||||
{}
|
||||
|
||||
void PacketParser::parse(Bytes pck) {
|
||||
u64 peerId;
|
||||
pck >> peerId;
|
||||
neighbours->receivedFrom(peerId);
|
||||
|
||||
while(pck.size() > 0) {
|
||||
readTLV(pck, peerId);
|
||||
}
|
||||
}
|
||||
|
||||
void PacketParser::readTLV(Bytes& pck, u64 nei) {
|
||||
u8 type, len;
|
||||
pck >> type >> len;
|
||||
|
||||
switch(type) {
|
||||
case csts::TLV_PAD1:
|
||||
case csts::TLV_PADN:
|
||||
break;
|
||||
|
||||
case csts::TLV_IHU: {
|
||||
u64 ihuId;
|
||||
pck >> ihuId;
|
||||
if(ihuId != protocol->getSelfId())
|
||||
break;
|
||||
|
||||
neighbours->hadIHU(nei);
|
||||
break;
|
||||
}
|
||||
|
||||
case csts::TLV_NR:
|
||||
//TODO
|
||||
break;
|
||||
|
||||
case csts::TLV_NEIGH:
|
||||
receiveNeigh(pck, len);
|
||||
break;
|
||||
|
||||
case csts::TLV_DATA:
|
||||
receiveData(pck, len, nei);
|
||||
break;
|
||||
|
||||
case csts::TLV_IHAVE:
|
||||
//TODO
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PacketParser::receiveNeigh(Bytes& pck, u8 length) {
|
||||
while(length >= 8+16+2) { /* enough to read one peer */
|
||||
u64 id;
|
||||
u16 port;
|
||||
SockAddr addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin6_family = AF_INET6;
|
||||
|
||||
pck >> id;
|
||||
for(int byte=0; byte < 16; byte++)
|
||||
pck >> addr.sin6_addr.s6_addr[byte];
|
||||
pck >> port;
|
||||
addr.sin6_port = htons(port);
|
||||
|
||||
Neighbour nei(id, addr);
|
||||
neighbours->addPotentialNei(nei);
|
||||
}
|
||||
}
|
||||
|
||||
void PacketParser::receiveData(Bytes& pck, u8 length, u64 from) {
|
||||
u32 seqNo;
|
||||
u64 datId;
|
||||
pck >> seqNo >> datId;
|
||||
protocol->sendIHave(from, datId, seqNo);
|
||||
//TODO
|
||||
length++;
|
||||
}
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
/***************************************************************************
|
||||
* By Théophile Bastian, 2017
|
||||
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
|
||||
* License: WTFPL v2 <http://www.wtfpl.net/>
|
||||
**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "neighbours.h"
|
||||
#include "protocol.h"
|
||||
|
||||
class PacketParser {
|
||||
public:
|
||||
PacketParser(Neighbours* nei, Protocol* proto);
|
||||
|
||||
void parse(Bytes pck);
|
||||
|
||||
private: //meth
|
||||
void readTLV(Bytes& pck, u64 nei);
|
||||
void receiveNeigh(Bytes& pck, u8 length);
|
||||
void receiveData(Bytes& pck, u8 length, u64 from);
|
||||
|
||||
private:
|
||||
Neighbours* neighbours;
|
||||
Protocol* protocol;
|
||||
};
|
||||
|
|
@ -70,9 +70,6 @@ class Protocol {
|
|||
void sendIHave(u64 id, u64 datId, u32 seqno);
|
||||
/** Sends a IHave packet for `datId` */
|
||||
|
||||
u64 getSelfId() const { return selfId; }
|
||||
/** Returns the ID of this node. */
|
||||
|
||||
private: //meth
|
||||
void startPollNetwork();
|
||||
void pollNetwork();
|
||||
|
|
Loading…
Reference in a new issue