Compare commits

...

5 commits

Author SHA1 Message Date
Théophile Bastian 143cff86f5 Add fake client sending IHUs 2016-11-25 15:48:08 +01:00
Théophile Bastian 6730d8ba85 Send IHU, IHave, ... 2016-11-25 15:47:35 +01:00
Théophile Bastian 6c0e1f4d26 Ignoring conf files 2016-11-25 15:47:08 +01:00
Théophile Bastian 761d657f9b Removed network endianness conversion
Turns out the reference implementation does not use it…
2016-11-25 15:45:39 +01:00
Théophile Bastian 6b69cee089 Fix constant 2016-11-24 16:18:16 +01:00
7 changed files with 160 additions and 43 deletions

2
.gitignore vendored
View file

@ -30,3 +30,5 @@
projet.pdf
jeanhubert
cfg*
*.bin

View file

@ -54,10 +54,12 @@ Bytes& Bytes::operator<<(u8 v) {
}
Bytes& Bytes::operator<<(u16 v) {
insertData<u16>(htons(v));
// insertData<u16>(htons(v));
return *this;
}
Bytes& Bytes::operator<<(u32 v) {
insertData<u32>(htonl(v));
// insertData<u32>(htonl(v));
return *this;
}
Bytes& Bytes::operator<<(u64 v) {
@ -84,12 +86,12 @@ Bytes& Bytes::operator>>(u8& v) {
}
Bytes& Bytes::operator>>(u16& v) {
extractData<u16>(v);
htons(v);
// v = htons(v);
return *this;
}
Bytes& Bytes::operator>>(u32& v) {
extractData<u32>(v);
htonl(v);
// v = htonl(v);
return *this;
}
Bytes& Bytes::operator>>(u64& v) {

2
cfg
View file

@ -1,2 +0,0 @@
id a0ae025e0df5f0a6
bootstrap 2460e2b01bc8d704 ::ffff:81.194.27.155 1212

View file

@ -40,9 +40,9 @@ void Neighbours::fullUpdate() {
fullCheck();
for(auto nei : unidirNei)
updateSendPackets(nei, proto);
updateSendPackets(nei);
for(auto nei : symNei)
updateSendPackets(nei, proto);
updateSendPackets(nei);
if(potentialNei.size() > 0
&& symNei.size() < csts::SYM_COUNT_BEFORE_PEEK
@ -51,6 +51,7 @@ void Neighbours::fullUpdate() {
auto it = potentialNei.begin();
for(int at=0; at < nPeerId; ++at, ++it);
proto->sendEmpty(it->id);
lastPckSent[it->id] = time(NULL);
lastPeerPeek = time(NULL);
}
}
@ -65,27 +66,28 @@ void Neighbours::addPotentialNei(const Neighbour& nei) {
void Neighbours::receivedFrom(u64 id) {
NeiType typ = neiType[id];
lastRecv.insert({id, time(NULL)});
lastRecv[id] = time(NULL);
if(typ == NEI_POTENTIAL)
changeNeiType(id, NEI_UNIDIR);
}
void Neighbours::hadIHU(u64 id) {
NeiType typ = neiType[id];
lastRecv.insert({id, time(NULL)});
lastIHU.insert({id, time(NULL)});
lastRecv[id] = time(NULL);
lastIHU[id] = time(NULL);
if(typ == NEI_POTENTIAL || typ == NEI_UNIDIR)
changeNeiType(id, NEI_SYM);
}
list<Neighbour>& Neighbours::listOfType(NeiType typ) {
list<Neighbour>* Neighbours::listOfType(NeiType typ) {
switch(typ) {
case NEI_POTENTIAL:
return potentialNei;
return &potentialNei;
case NEI_UNIDIR:
return unidirNei;
return &unidirNei;
case NEI_SYM:
return symNei;
return &symNei;
default:
break;
}
@ -93,34 +95,55 @@ list<Neighbour>& Neighbours::listOfType(NeiType typ) {
}
void Neighbours::changeNeiType(u64 id, NeiType nType) {
printf("TYPE %lX %d\n", id, neiType[id]);
NeiType cType = neiType[id];
if(cType == nType)
return;
list<Neighbour>& fromList = listOfType(cType), toList = listOfType(nType);
neiType.insert({id, nType});
list<Neighbour> *fromList = listOfType(cType),
*toList = listOfType(nType);
neiType[id] = nType;
bool wasSpliced=false;
for(auto it=fromList.begin(); it != fromList.end(); ++it) {
for(auto it=fromList->begin(); it != fromList->end(); ++it) {
if(it->id == id) {
fromList.splice(it, toList);
printf("%ld %ld\n", fromList->size(), toList->size());
toList->push_back(*it); // splice() doesn't work?!
fromList->erase(it);
printf("%ld %ld %d\n", fromList->size(), toList->size(),
toList == &unidirNei);
wasSpliced=true;
break;
}
}
if(!wasSpliced) {
//TODO log error.
fprintf(stderr, "[ERROR] Node %lX wasn't found (type change)\n", id);
}
printf("TYPE %lX %d\n", id, neiType[id]);
}
void Neighbours::updateSendPackets(const Neighbour& nei, Protocol* proto) {
if(time(NULL) - lastIHUSent[nei.id] >= csts::TIME_RESEND_IHU) {
lastIHUSent[nei.id] = time(NULL);
lastPckSent[nei.id] = time(NULL);
proto->sendIHU(nei.id);
}
else if(time(NULL) - lastPckSent[nei.id] >= csts::TIME_RESEND_EMPTY) {
lastPckSent[nei.id] = time(NULL);
proto->sendEmpty(nei.id);
}
void Neighbours::updateSendPackets(const Neighbour& nei) {
if(!sendIHU(nei.id))
sendEmpty(nei.id);
}
bool Neighbours::sendEmpty(u64 id) {
if(time(NULL) - lastPckSent[id] >= csts::TIME_RESEND_EMPTY) {
printf("[DBG] sending empty packet to %lX.\n", id);
lastPckSent[id] = time(NULL);
proto->sendEmpty(id);
return true;
}
return false;
}
bool Neighbours::sendIHU(u64 id) {
if(time(NULL) - lastIHUSent[id] >= csts::TIME_RESEND_IHU) {
printf("[DBG] sending IHU packet to %lX.\n", id);
lastIHUSent[id] = time(NULL);
lastPckSent[id] = time(NULL);
proto->sendIHU(id);
return true;
}
return false;
}

View file

@ -44,9 +44,12 @@ class Neighbours {
NEI_UNDEF, NEI_POTENTIAL, NEI_UNIDIR, NEI_SYM
};
std::list<Neighbour>& listOfType(NeiType typ);
std::list<Neighbour>* listOfType(NeiType typ);
void changeNeiType(u64 id, NeiType nType);
void updateSendPackets(const Neighbour& nei, Protocol* proto);
void updateSendPackets(const Neighbour& nei);
bool sendEmpty(u64 id);
bool sendIHU(u64 id);
private:
Protocol* proto;

View file

@ -27,6 +27,7 @@ Protocol::Protocol(const SockAddr& listenAddr, u64 selfId) :
}
// Set socket reception timeout
/*
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000;
@ -34,6 +35,7 @@ Protocol::Protocol(const SockAddr& listenAddr, u64 selfId) :
perror("Cannot set socket timeout");
throw NwError();
}
*/
startPollNetwork();
}
@ -159,14 +161,20 @@ void Protocol::startPollNetwork() {
void Protocol::pollNetwork() {
u8 buffer[MAX_MTU];
SockAddr fromAddr6;
struct sockaddr* fromAddr = (struct sockaddr*)&fromAddr6;
socklen_t fromAddrLen;
while(!terminating) {
struct sockaddr fromAddr;
memset(&fromAddr, 0, sizeof(fromAddr));
socklen_t fromAddrLen=sizeof(fromAddr);
ssize_t readDat = recvfrom(sock, buffer, MAX_MTU, 0,
fromAddr, &fromAddrLen);
if(readDat <= 0)
&fromAddr, &fromAddrLen);
if(readDat < 0) {
perror("[WARNING] Bad packet");
continue;
}
if(readDat <= 0) {
fprintf(stderr, "[WARNING] Empty packet.\n");
continue;
}
Bytes data(buffer, readDat);
u8 magic, version;
data >> magic >> version;
@ -180,24 +188,34 @@ void Protocol::pollNetwork() {
}
u16 bodyLen;
data >> bodyLen;
if(data.size() < bodyLen + 64u) {
fprintf(stderr, "[WARNING] Body too short\n");
if(data.size() < bodyLen + 8u) {
fprintf(stderr, "[WARNING] Body too short (%lu < %d)\n",
data.size(), bodyLen+8u);
continue;
}
else if(data.size() != bodyLen + 64u) {
else if(data.size() != bodyLen + 8u) {
fprintf(stderr, "[WARNING] Body too long\n");
}
SockAddr convFromAddr;
if(fromAddr->sa_family == AF_INET)
convFromAddr = addrOfV4(*(struct sockaddr_in*)fromAddr);
else if(fromAddr->sa_family == AF_INET6)
convFromAddr = *(SockAddr*)fromAddr;
if(fromAddr.sa_family == AF_INET)
convFromAddr = addrOfV4(*((struct sockaddr_in*)&fromAddr));
else if(fromAddr.sa_family == AF_INET6)
convFromAddr = *(SockAddr*)&fromAddr;
else {
fprintf(stderr, "ERROR: Unknown address family.");
fprintf(stderr, "[ERROR] Unknown address family %d.\n",
fromAddr.sa_family);
fprintf(stderr, "\t%hu\n", ((SockAddr*)&fromAddr)->sin6_port);
while(data.size() > 0) {
u8 c;
data >> c;
fprintf(stderr, "%02X|", c);
}
fprintf(stderr, "\n");
continue;
}
puts("Received packet");
AvailPacket pck;
pck.from = convFromAddr;
pck.data = data;

71
utils/ihu_replier.c Normal file
View file

@ -0,0 +1,71 @@
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdint.h>
const int BUFFER_SIZE = 4096;
int main(void) {
int sock = socket(PF_INET6, SOCK_DGRAM, 0);
if(sock < 0) {
perror("Could not create socket");
exit(1);
}
int reuseVal = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseVal, sizeof(reuseVal))
< 0) {
perror("Could not set reuseaddr");
exit(1);
}
struct sockaddr_in6 sockIn;
memset(&sockIn, 0, sizeof(sockIn));
sockIn.sin6_family = AF_INET6;
sockIn.sin6_port = htons(1212);
if(bind(sock, (struct sockaddr*)&sockIn, sizeof(sockIn))) {
perror("Could not bind socket to port 8080");
exit(1);
}
char* buffer = (char*)malloc(sizeof(char) * BUFFER_SIZE);
while(1) {
struct sockaddr fromAddr;
socklen_t fromAddr_len;
ssize_t readBytes = recvfrom(sock, buffer, BUFFER_SIZE,
0, &fromAddr, &fromAddr_len);
if(readBytes < 0) {
perror("WARNING: Could not read from socket");
continue;
}
uint8_t outPck[24];
memset(outPck, 0, 24);
outPck[0] = 57;
outPck[1] = 0;
*(uint16_t*)(outPck+2) = htonl(10);
outPck[12] = 2;
outPck[13] = 8;
memcpy(outPck+14, buffer+4, 8);
printf("%d\n", fromAddr.sa_family);
char sAddr[54];
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)(&fromAddr);
inet_ntop(AF_INET6, &addr6->sin6_addr, sAddr, 54);
printf("Sending [%s]:%d\n", sAddr, addr6->sin6_port);
sendto(sock, outPck, 22, 0, &fromAddr, fromAddr_len);
}
if(close(sock) < 0) {
perror("Could not destroy socket");
exit(1);
}
return 0;
}