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 projet.pdf
jeanhubert jeanhubert
cfg*
*.bin

View file

@ -54,10 +54,12 @@ Bytes& Bytes::operator<<(u8 v) {
} }
Bytes& Bytes::operator<<(u16 v) { Bytes& Bytes::operator<<(u16 v) {
insertData<u16>(htons(v)); insertData<u16>(htons(v));
// insertData<u16>(htons(v));
return *this; return *this;
} }
Bytes& Bytes::operator<<(u32 v) { Bytes& Bytes::operator<<(u32 v) {
insertData<u32>(htonl(v)); insertData<u32>(htonl(v));
// insertData<u32>(htonl(v));
return *this; return *this;
} }
Bytes& Bytes::operator<<(u64 v) { Bytes& Bytes::operator<<(u64 v) {
@ -84,12 +86,12 @@ Bytes& Bytes::operator>>(u8& v) {
} }
Bytes& Bytes::operator>>(u16& v) { Bytes& Bytes::operator>>(u16& v) {
extractData<u16>(v); extractData<u16>(v);
htons(v); // v = htons(v);
return *this; return *this;
} }
Bytes& Bytes::operator>>(u32& v) { Bytes& Bytes::operator>>(u32& v) {
extractData<u32>(v); extractData<u32>(v);
htonl(v); // v = htonl(v);
return *this; return *this;
} }
Bytes& Bytes::operator>>(u64& v) { 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(); fullCheck();
for(auto nei : unidirNei) for(auto nei : unidirNei)
updateSendPackets(nei, proto); updateSendPackets(nei);
for(auto nei : symNei) for(auto nei : symNei)
updateSendPackets(nei, proto); updateSendPackets(nei);
if(potentialNei.size() > 0 if(potentialNei.size() > 0
&& symNei.size() < csts::SYM_COUNT_BEFORE_PEEK && symNei.size() < csts::SYM_COUNT_BEFORE_PEEK
@ -51,6 +51,7 @@ void Neighbours::fullUpdate() {
auto it = potentialNei.begin(); auto it = potentialNei.begin();
for(int at=0; at < nPeerId; ++at, ++it); for(int at=0; at < nPeerId; ++at, ++it);
proto->sendEmpty(it->id); proto->sendEmpty(it->id);
lastPckSent[it->id] = time(NULL);
lastPeerPeek = time(NULL); lastPeerPeek = time(NULL);
} }
} }
@ -65,27 +66,28 @@ void Neighbours::addPotentialNei(const Neighbour& nei) {
void Neighbours::receivedFrom(u64 id) { void Neighbours::receivedFrom(u64 id) {
NeiType typ = neiType[id]; NeiType typ = neiType[id];
lastRecv.insert({id, time(NULL)}); lastRecv[id] = time(NULL);
if(typ == NEI_POTENTIAL) if(typ == NEI_POTENTIAL)
changeNeiType(id, NEI_UNIDIR); changeNeiType(id, NEI_UNIDIR);
} }
void Neighbours::hadIHU(u64 id) { void Neighbours::hadIHU(u64 id) {
NeiType typ = neiType[id]; NeiType typ = neiType[id];
lastRecv.insert({id, time(NULL)}); lastRecv[id] = time(NULL);
lastIHU.insert({id, time(NULL)}); lastIHU[id] = time(NULL);
if(typ == NEI_POTENTIAL || typ == NEI_UNIDIR) if(typ == NEI_POTENTIAL || typ == NEI_UNIDIR)
changeNeiType(id, NEI_SYM); changeNeiType(id, NEI_SYM);
} }
list<Neighbour>& Neighbours::listOfType(NeiType typ) { list<Neighbour>* Neighbours::listOfType(NeiType typ) {
switch(typ) { switch(typ) {
case NEI_POTENTIAL: case NEI_POTENTIAL:
return potentialNei; return &potentialNei;
case NEI_UNIDIR: case NEI_UNIDIR:
return unidirNei; return &unidirNei;
case NEI_SYM: case NEI_SYM:
return symNei; return &symNei;
default: default:
break; break;
} }
@ -93,34 +95,55 @@ list<Neighbour>& Neighbours::listOfType(NeiType typ) {
} }
void Neighbours::changeNeiType(u64 id, NeiType nType) { void Neighbours::changeNeiType(u64 id, NeiType nType) {
printf("TYPE %lX %d\n", id, neiType[id]);
NeiType cType = neiType[id]; NeiType cType = neiType[id];
if(cType == nType) if(cType == nType)
return; return;
list<Neighbour>& fromList = listOfType(cType), toList = listOfType(nType); list<Neighbour> *fromList = listOfType(cType),
neiType.insert({id, nType}); *toList = listOfType(nType);
neiType[id] = nType;
bool wasSpliced=false; 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) { 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; wasSpliced=true;
break; break;
} }
} }
if(!wasSpliced) { 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) { void Neighbours::updateSendPackets(const Neighbour& nei) {
if(time(NULL) - lastIHUSent[nei.id] >= csts::TIME_RESEND_IHU) { if(!sendIHU(nei.id))
lastIHUSent[nei.id] = time(NULL); sendEmpty(nei.id);
lastPckSent[nei.id] = time(NULL); }
proto->sendIHU(nei.id);
} bool Neighbours::sendEmpty(u64 id) {
else if(time(NULL) - lastPckSent[nei.id] >= csts::TIME_RESEND_EMPTY) { if(time(NULL) - lastPckSent[id] >= csts::TIME_RESEND_EMPTY) {
lastPckSent[nei.id] = time(NULL); printf("[DBG] sending empty packet to %lX.\n", id);
proto->sendEmpty(nei.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 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 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: private:
Protocol* proto; Protocol* proto;

View file

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