This commit is contained in:
gonzalef
2020-03-24 17:42:25 +01:00
13 changed files with 188 additions and 366 deletions

94
src/node.c Normal file
View File

@@ -0,0 +1,94 @@
// This is the main file of the Dazibao project. It represents the node, and
// handles all of the main logic, including the network connexions.
// Will return a packet when we receive one that's valid.
packet listen_for_packets(){
}
// We need to make sure the TLV announces a length that will no go onto
// another tlv, as we might end up reading bullshit.
int validate_tlvs(union tlv tlv_to_validate){
}
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 continue = 1;
while(continue){
// We create the neighbourhood table
neighbour_peer neighbour_list[NEIGHBOUR_MAX];
// We create the message table
// We create our own message.
// Listen for incoming packets
listen_for_packets();
// For every packet recivied, we fork,
// then we make sure it's conform
// We then extract the data from it to make it easy to work with
check_header();
// 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.
update_neighbours();
// We then look at the differents TLVs in the packet.
work_with_tlvs();
time_t delay = time(NULL) + 20;
while(! (delay < time(NULL)){
// Theses functions are there for general book-keeping,and run in there own
// thread, being run every 20 seconds.
// Every 20 sec, if we have less than 5 neighbours, we ask for more peers
// by sending out a TLV neighbour Request at a random peer.
t_ask_for_more_peers();
// Every 20 sec, we also check for a peer that didn't emit a new message for
// the past 70 sec, if he's temporary, we delete him from the neighbourhood.
t_update_neighbours();
// We send out a TLV Network hash to get an ideal of the network state.
t_get_network_state();
}
}
return 0;
}

64
src/node.h Normal file
View File

@@ -0,0 +1,64 @@
// Define constants
#ifndef NODE_H
#define NODE_H
// The node ID
#define NODE_ID 203242402519736214145149136169422092269247115186189140178187251487819615911212154252117172522111472481308026129190139512419121015210238252292031613214452118122204415160254
// The number of neighbours
// The neighbour table has 15 entries
#define NEIGHBOUR_MAX 15
/* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)),
* et dont chaque entrée contient un booléen indiquant si le pair est permanent
* (configuré au lancement) ou transitoire, et la date de dernière réception dun
* paquet de la part de ce pair ;
*/
typedef struct neighbour_peer {
struct in6_addr ip;
short port;
char is_temporary;
struct timeval last_seen;
} neighbour_peer;
// The strucuture to hold the messages
/* It's a list of triplets, (Li,Si,Di)
* Li : The Node ID of the publisher 64 bits
* Si : the sequence number 16 bits
* Di : the data of the message 192 bytes
*/
typedef struct message {
long node_id_publisher;
short seqno;
char *data;
} message;
// TODO
// fonctions signatures
void listen_for_packets();
void check_header();
void update_neighbours();
void work_with_tlvs();
int validate_tlvs();
// threaded functions
void t_ask_for_more_peers();
void t_update_neighbours();
void t_get_network_state();
// Helper functions
char * hash();
short * get_seq_no(short s, int n);
#endif

View File

@@ -10,12 +10,13 @@ cmd_token parse_cmd() {
if(fgets(buf, 198, stdin) == NULL)
return token;
// cmd sera le premier mot rencontré et arg la suite de mots après celui ci, si les deux variables ne sont pas remplies alors il y a une erreur
// cmd sera le premier mot rencontré et arg la suite de mots après celui ci,
// si les deux variables ne sont pas remplies alors il y a une erreur
if(sscanf(buf, "%s %[^\t\n]", cmd, arg) != 2)
return token;
if(strcmp("req", cmd) == 0) {
if(strcmp("neighbour", arg) == 0)
if(strcmp("neighbour", arg) == 0)
token.type = NEIGHBOUR_REQ;
else if(strcmp("network state", arg) == 0)
token.type = NETWORK_STATE_REQ;

View File

@@ -1,6 +1,9 @@
#include <stdio.h>
#include <string.h>
#ifndef PARSER_H
#define PARSER_H
typedef enum cmd_type {
NEIGHBOUR_REQ, NETWORK_STATE_REQ, NODE_STATE_REQ, POST, ERROR
} cmd_type;
@@ -12,3 +15,5 @@ typedef struct cmd_token {
// retourne le type de commande à exécuter
cmd_token parse_cmd();
#endif

BIN
src/test

Binary file not shown.

View File

@@ -1,15 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char a[1];
a[0] = 0;
if(fork() == 0) {
while(1)
printf("%d\n", a[0]);
} else {
a[1] = 1;
}
}

View File

@@ -3,7 +3,10 @@
#include <stdlib.h>
#include "parser.h"
// 8 octets min (struct pointer 4 octets), 1024 octets max
#ifndef TLV_H
#define TLV_H
// 8 octets min (struct pointer 4 octets), 1024 octets max
typedef struct packet {
unsigned char magic; // 95 (si autre, ignorer)
unsigned char version; // 1 (si autre, ignorer)
@@ -80,7 +83,7 @@ typedef struct node_state {
typedef struct warning {
unsigned char type;
unsigned char length;
char *message;
char message[256];
} warning;
typedef union tlv {
@@ -110,4 +113,6 @@ int build_network_state_req(tlv *tlv);
int build_node_hash(tlv *tlv, long node_id, short seqno, char *node_hash);
int build_node_state_req(tlv *tlv, long node_id);
int build_node_state(tlv *tlv, long node_id, short seqno, char *node_hash, char *data);
int build_warning(tlv *tlv, char *message);
int build_warning(tlv *tlv, char *message);
#endif