Added work_with_tlvs
This commit is contained in:
parent
882b75433a
commit
8adf0d2bec
205
src/node.c
205
src/node.c
@ -6,20 +6,124 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
|
// We then look at the differents TLVs in the packet.
|
||||||
|
int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender){
|
||||||
|
|
||||||
|
// For every TLV,
|
||||||
|
// We make sure the TLV is legal.
|
||||||
|
int number_of_good_tlvs = 0;
|
||||||
|
int padding_to_next_tlv = 0;
|
||||||
|
int end_analysis = 0;
|
||||||
|
while (!end_analysis) {
|
||||||
|
|
||||||
|
// Switch
|
||||||
|
switch (data_from_packet[4 + padding_to_next_tlv]) {
|
||||||
|
case 0:
|
||||||
|
// Padding tlv, we can ignore, pass to next tlv.
|
||||||
|
padding_to_next_tlv++;
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// PadN TLV, we can ignore, pass to next tlv.
|
||||||
|
// Add the length to get to next TLV
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// Neighbour Request TLV
|
||||||
|
padding_to_next_tlv += 2;
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// Neighbour TLV
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
// Network hash TLV
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
|
||||||
|
// TLV Network Hash
|
||||||
|
// We calculate a network hash,
|
||||||
|
|
||||||
|
// We compare both,
|
||||||
|
|
||||||
|
// If they differ, we send a TLV Network State Request
|
||||||
|
// back to the sender.$
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
// Network State Request
|
||||||
|
padding_to_next_tlv += 2;
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
|
||||||
|
// TLV Network State Request
|
||||||
|
// We check our neighbourhood, and for each peer, we send back
|
||||||
|
// to the sender a TLV Node Hash
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
// Node Hash
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
// TLV Node hash
|
||||||
|
// We get a hash _h_ for the node _l_
|
||||||
|
// If we don't have an entry for _l_, or if we have the same one as the
|
||||||
|
// on we just receivied, we send out a TLV Node State Request back.
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
// Node State Request
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
// Node State
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
// TLV Node State
|
||||||
|
// We get a hash _h_, sequence number _s_, data _d_ for node _l_.
|
||||||
|
// We compute a network hash,
|
||||||
|
// We compare the hash, if they differ, then with l',s',d' our data and
|
||||||
|
// h' the corresponding hash,
|
||||||
|
// if _l_ is our own node id, then
|
||||||
|
// if s >> s' then we update our sequence number to s ⊕ 1 mod 2^16
|
||||||
|
// If it's another's node id, then
|
||||||
|
// If there is no entry for the sender,
|
||||||
|
// we store the entry in our data table.
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
// Warning
|
||||||
|
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
||||||
|
number_of_good_tlvs++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return -number_of_good_tlvs;
|
||||||
|
// We have a faulty TLV, thus we ignore and return.
|
||||||
|
}
|
||||||
|
// If we arrived at the end of the packet
|
||||||
|
// or if the length is not possible
|
||||||
|
if (received_packet.length <= padding_to_next_tlv + 4) {
|
||||||
|
end_analysis = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// We need to make sure the TLV announces a length that will no go onto
|
// We need to make sure the TLV announces a length that will no go onto
|
||||||
// another tlv, as we might end up reading bullshit.
|
// another tlv, as we might end up reading bullshit.
|
||||||
tlv_list validate_tlvs(packet packet_to_validate){
|
int validate_tlvs(struct packet packet_to_validate){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For every packet recivied,
|
// For every packet recivied,
|
||||||
// then we make sure it's conform
|
// then we make sure it's conform
|
||||||
// We then extract the data from it to make it easy to work with
|
// We then extract the data from it to make it easy to work with
|
||||||
int check_header(char * req[], int buffer_size, packet * packet_to_return){
|
int check_header(char * req[], int buffer_size, struct packet * packet_to_return){
|
||||||
|
|
||||||
packet * packet_to_return = (packet*) req;
|
packet * packet_to_return = (packet*) req;
|
||||||
|
|
||||||
@ -44,6 +148,13 @@ int check_header(char * req[], int buffer_size, packet * packet_to_return){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the sender is not in the neighbourhood, and we have 15 neighbours, we
|
||||||
|
// ignore the packet. Otherwise, we add him to the neighbourhood, marked as
|
||||||
|
// temporary.
|
||||||
|
int update_neighbours(){
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
// We listen forever for new paquets;
|
// We listen forever for new paquets;
|
||||||
void listen_for_packets(){
|
void listen_for_packets(){
|
||||||
|
|
||||||
@ -52,7 +163,7 @@ void listen_for_packets(){
|
|||||||
|
|
||||||
if(s < 0) {
|
if(s < 0) {
|
||||||
perror(">> Error, cannot create socket.");
|
perror(">> Error, cannot create socket.");
|
||||||
perror(">> Exiting...")
|
perror(">> Exiting...");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,36 +175,35 @@ void listen_for_packets(){
|
|||||||
int rc = bind(s, (struct sockaddr*)&server, sizeof(server));
|
int rc = bind(s, (struct sockaddr*)&server, sizeof(server));
|
||||||
if(rc < 0) {
|
if(rc < 0) {
|
||||||
perror(">> Error, cannot bind socket to choosen port.");
|
perror(">> Error, cannot bind socket to choosen port.");
|
||||||
perror(">> Exiting...")
|
perror(">> Exiting...");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A paquet has at most a length of 1024 bytes
|
// A paquet has at most a length of 1024 bytes
|
||||||
char req[1024];
|
char req[1024];
|
||||||
struct sockaddr_in6 client;
|
struct sockaddr_in6 sender;
|
||||||
struct iovec io = { .iov_len = 1024, .iov_base = req };
|
struct iovec io = { .iov_len = 1024, .iov_base = req };
|
||||||
struct msghdr msg_to_receive = {
|
struct msghdr msg_to_receive = {
|
||||||
.msg_name = &client,
|
.msg_name = &sender,
|
||||||
.msg_namelen = sizeof(client),
|
.msg_namelen = sizeof(sender),
|
||||||
.msg_iov = &io,
|
.msg_iov = &io,
|
||||||
.msg_iovlen = 1
|
.msg_iovlen = 1
|
||||||
};
|
};
|
||||||
while(1){
|
while(1){
|
||||||
memset(req, '\0', 1024);
|
memset(req, '\0', 1024);
|
||||||
|
|
||||||
struct sockaddr_in6 client;
|
|
||||||
unsigned int client_len = sizeof(client);
|
|
||||||
|
|
||||||
rc = recvmsg(s, &msg_to_receive, 0);
|
rc = recvmsg(s, &msg_to_receive, 0);
|
||||||
if(rc < 0) {
|
if(rc < 0) {
|
||||||
perror(">> Error while receiving a new datagram.");
|
perror(">> Error while receiving a new datagram.");
|
||||||
perror(">> Ignoring, continuing...")
|
perror(">> Ignoring, continuing...");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(">> New paquet received :\n");
|
printf(">> New paquet received :\n");
|
||||||
printf("%s\n", req);
|
printf("%s\n", req);
|
||||||
|
|
||||||
|
// TODO : Here, we need to fork.
|
||||||
|
|
||||||
// We verify the received packet is well formated,
|
// We verify the received packet is well formated,
|
||||||
// and we return it in the struct designed to work with it.
|
// and we return it in the struct designed to work with it.
|
||||||
struct packet * formated_rec_datagram;
|
struct packet * formated_rec_datagram;
|
||||||
@ -102,74 +212,25 @@ void listen_for_packets(){
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct tlv_list received_tlvs;
|
// TODO : Add the neighbour check here.
|
||||||
if (validate_tlvs(formated_rec_datagram) < 0) {
|
|
||||||
|
|
||||||
/* code */
|
// struct tlv_list received_tlvs;
|
||||||
|
// if (validate_tlvs(formated_rec_datagram) < 0)
|
||||||
|
|
||||||
|
int nbr_success_tlv = work_with_tlvs(formated_rec_datagram, &req, sender);
|
||||||
|
if (nbr_success_tlv < 0){
|
||||||
|
perror(">> Error while treating the TLVs of the packet.");
|
||||||
|
printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv );
|
||||||
|
} else {
|
||||||
|
printf(">> Done working with the TLVs of the packet, listenin for new packets.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// If the sender is not in the neighbourhood, and we have 15 neighbours, we
|
|
||||||
// ignore the packet. Otherwise, we add him to the neighbourhood, marked as
|
|
||||||
// temporary.
|
|
||||||
int update_neighbours(){
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// We then look at the differents TLVs in the packet.
|
|
||||||
void work_with_tlvs(){
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void work_with_tlvs(struct tlvs_list receivied_tlvs){
|
|
||||||
|
|
||||||
// For every TLV,
|
|
||||||
// We make sure the TLV is legal.
|
|
||||||
if(!validate_tlvs(tlv)){
|
|
||||||
perror(">> Invalid TLV receivied, it will be ignored.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Switch
|
|
||||||
|
|
||||||
// TLV Network Hash
|
|
||||||
// We calculate a network hash,
|
|
||||||
|
|
||||||
// We compare both,
|
|
||||||
|
|
||||||
// If they differ, we send a TLV Network State Request
|
|
||||||
// back to the sender.
|
|
||||||
|
|
||||||
// TLV Network State Request
|
|
||||||
// We check our neighbourhood, and for each peer, we send back
|
|
||||||
// to the sender a TLV Node Hash
|
|
||||||
|
|
||||||
// TLV Node hash
|
|
||||||
// We get a hash _h_ for the node _l_
|
|
||||||
// If we don't have an entry for _l_, or if we have the same one as the
|
|
||||||
// on we just receivied, we send out a TLV Node State Request back.
|
|
||||||
|
|
||||||
// TLV Node State
|
|
||||||
// We get a hash _h_, sequence number _s_, data _d_ for node _l_.
|
|
||||||
// We compute a network hash,
|
|
||||||
// We compare the hash, if they differ, then with l',s',d' our data and
|
|
||||||
// h' the corresponding hash,
|
|
||||||
// if _l_ is our own node id, then
|
|
||||||
// if s >> s' then we update our sequence number to s ⊕ 1 mod 2^16
|
|
||||||
// If it's another's node id, then
|
|
||||||
// If there is no entry for the sender,
|
|
||||||
// we store the entry in our data table.
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char const *argv[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
int continue = 1;
|
int continue = 1;
|
||||||
|
|
||||||
while(continue){
|
while(continue != 0){
|
||||||
|
|
||||||
// We create the neighbourhood table
|
// We create the neighbourhood table
|
||||||
neighbour_peer neighbour_list[NEIGHBOUR_MAX];
|
neighbour_peer neighbour_list[NEIGHBOUR_MAX];
|
||||||
@ -182,7 +243,7 @@ int main(int argc, char const *argv[]) {
|
|||||||
|
|
||||||
// This is in it's own fork.
|
// This is in it's own fork.
|
||||||
time_t delay = time(NULL) + 20;
|
time_t delay = time(NULL) + 20;
|
||||||
while(! (delay < time(NULL)){
|
while(! (delay < time(NULL))) {
|
||||||
// Theses functions are there for general book-keeping,and run in there own
|
// Theses functions are there for general book-keeping,and run in there own
|
||||||
// thread, being run every 20 seconds.
|
// thread, being run every 20 seconds.
|
||||||
// Every 20 sec, if we have less than 5 neighbours, we ask for more peers
|
// Every 20 sec, if we have less than 5 neighbours, we ask for more peers
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#ifndef NODE_H
|
#ifndef NODE_H
|
||||||
#define NODE_H
|
#define NODE_H
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
|
|
||||||
// On which port do we listen to
|
// On which port do we listen to
|
||||||
@ -45,13 +47,13 @@ typedef struct message {
|
|||||||
// fonctions signatures
|
// fonctions signatures
|
||||||
void listen_for_packets();
|
void listen_for_packets();
|
||||||
|
|
||||||
int check_header(char * received_datagram[], int len, packet pack);
|
int check_header(char * received_datagram[], int len, struct packet pack);
|
||||||
|
|
||||||
int validate_tlvs(packet * pack, tlv_list * tlv_l);
|
int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l);
|
||||||
|
|
||||||
int update_neighbours();
|
int update_neighbours();
|
||||||
|
|
||||||
void work_with_tlvs();
|
int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender);
|
||||||
|
|
||||||
// threaded functions
|
// threaded functions
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user