Compare commits

..

No commits in common. "143cff86f5358c9fdb98d77dd19f023a23857c4b" and "750f9e36724b0ed4c2d2798e21d1f1052332e111" have entirely different histories.

7 changed files with 41 additions and 158 deletions

2
.gitignore vendored
View file

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

View file

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

2
cfg Normal file
View file

@ -0,0 +1,2 @@
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); updateSendPackets(nei, proto);
for(auto nei : symNei) for(auto nei : symNei)
updateSendPackets(nei); updateSendPackets(nei, proto);
if(potentialNei.size() > 0 if(potentialNei.size() > 0
&& symNei.size() < csts::SYM_COUNT_BEFORE_PEEK && symNei.size() < csts::SYM_COUNT_BEFORE_PEEK
@ -51,7 +51,6 @@ 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);
} }
} }
@ -66,28 +65,27 @@ 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[id] = time(NULL); lastRecv.insert({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[id] = time(NULL); lastRecv.insert({id, time(NULL)});
lastIHU[id] = time(NULL); lastIHU.insert({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;
} }
@ -95,55 +93,34 @@ 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), list<Neighbour>& fromList = listOfType(cType), toList = listOfType(nType);
*toList = listOfType(nType); neiType.insert({id, 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) {
printf("%ld %ld\n", fromList->size(), toList->size()); fromList.splice(it, toList);
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) {
fprintf(stderr, "[ERROR] Node %lX wasn't found (type change)\n", id); //TODO log error.
} }
printf("TYPE %lX %d\n", id, neiType[id]);
} }
void Neighbours::updateSendPackets(const Neighbour& nei) { void Neighbours::updateSendPackets(const Neighbour& nei, Protocol* proto) {
if(!sendIHU(nei.id)) if(time(NULL) - lastIHUSent[nei.id] >= csts::TIME_RESEND_IHU) {
sendEmpty(nei.id); lastIHUSent[nei.id] = time(NULL);
} lastPckSent[nei.id] = time(NULL);
proto->sendIHU(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; else if(time(NULL) - lastPckSent[nei.id] >= csts::TIME_RESEND_EMPTY) {
} lastPckSent[nei.id] = time(NULL);
proto->sendEmpty(nei.id);
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,12 +44,9 @@ 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); void updateSendPackets(const Neighbour& nei, Protocol* proto);
bool sendEmpty(u64 id);
bool sendIHU(u64 id);
private: private:
Protocol* proto; Protocol* proto;

View file

@ -27,7 +27,6 @@ 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;
@ -35,7 +34,6 @@ Protocol::Protocol(const SockAddr& listenAddr, u64 selfId) :
perror("Cannot set socket timeout"); perror("Cannot set socket timeout");
throw NwError(); throw NwError();
} }
*/
startPollNetwork(); startPollNetwork();
} }
@ -161,20 +159,14 @@ 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;
@ -188,34 +180,24 @@ void Protocol::pollNetwork() {
} }
u16 bodyLen; u16 bodyLen;
data >> bodyLen; data >> bodyLen;
if(data.size() < bodyLen + 8u) { if(data.size() < bodyLen + 64u) {
fprintf(stderr, "[WARNING] Body too short (%lu < %d)\n", fprintf(stderr, "[WARNING] Body too short\n");
data.size(), bodyLen+8u);
continue; continue;
} }
else if(data.size() != bodyLen + 8u) { else if(data.size() != bodyLen + 64u) {
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 %d.\n", fprintf(stderr, "ERROR: Unknown address family.");
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;

View file

@ -1,71 +0,0 @@
#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;
}