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