Add packetParser

This commit is contained in:
Théophile Bastian 2016-11-24 14:39:03 +01:00
parent 1527c1c6fe
commit ce0b019e91
4 changed files with 126 additions and 1 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
OBJS = Bytes.o main.o protocol.o neighbours.o packetParser.o
TARGET = jeanhubert
all: $(TARGET)

View file

@ -8,6 +8,7 @@
#include "protocol.h"
#include "nw_constants.h"
#include "neighbours.h"
#include "packetParser.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
@ -51,11 +52,20 @@ 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);
}

88
packetParser.cpp Normal file
View file

@ -0,0 +1,88 @@
/***************************************************************************
* 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++;
}

27
packetParser.h Normal file
View file

@ -0,0 +1,27 @@
/***************************************************************************
* 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;
};