2020-03-13 14:46:06 +01:00
|
|
|
|
// Define constants
|
|
|
|
|
|
2020-03-24 12:24:56 +01:00
|
|
|
|
#ifndef NODE_H
|
|
|
|
|
#define NODE_H
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-03-31 18:28:12 +02:00
|
|
|
|
#include <stdlib.h>
|
2020-03-31 19:16:23 +02:00
|
|
|
|
#include <time.h>
|
2020-03-31 18:28:12 +02:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/types.h>
|
2020-03-24 19:39:16 +01:00
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
2020-03-31 18:28:12 +02:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2020-03-24 18:19:35 +01:00
|
|
|
|
#include "tlv.h"
|
2020-03-31 18:28:12 +02:00
|
|
|
|
#include "hash.h"
|
2020-03-24 18:19:35 +01:00
|
|
|
|
|
|
|
|
|
// On which port do we listen to
|
|
|
|
|
#define LISTEN_PORT 1212
|
|
|
|
|
|
2020-03-24 13:25:18 +01:00
|
|
|
|
// The node ID
|
2020-04-01 17:16:46 +02:00
|
|
|
|
#define NODE_ID 42675882021843277
|
2020-03-24 13:25:18 +01:00
|
|
|
|
|
|
|
|
|
// 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 d’un
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-31 15:57:31 +02:00
|
|
|
|
typedef struct pub_data {
|
|
|
|
|
unsigned char length;
|
|
|
|
|
long id;
|
2020-03-24 13:25:18 +01:00
|
|
|
|
short seqno;
|
|
|
|
|
char *data;
|
2020-03-31 15:57:31 +02:00
|
|
|
|
} pub_data;
|
|
|
|
|
|
2020-03-31 18:28:12 +02:00
|
|
|
|
// General list
|
|
|
|
|
typedef struct list {
|
|
|
|
|
void *data;
|
2020-03-31 22:10:15 +02:00
|
|
|
|
void *next;
|
2020-03-31 18:28:12 +02:00
|
|
|
|
} list;
|
|
|
|
|
|
2020-03-31 19:16:23 +02:00
|
|
|
|
// Static variables
|
2020-03-31 22:10:15 +02:00
|
|
|
|
static list *data_list;
|
2020-03-31 15:57:31 +02:00
|
|
|
|
static list *neighbour_list;
|
2020-03-24 13:25:18 +01:00
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
2020-03-13 14:46:06 +01:00
|
|
|
|
// fonctions signatures
|
|
|
|
|
void listen_for_packets();
|
|
|
|
|
|
2020-03-24 19:39:16 +01:00
|
|
|
|
int check_header(char * received_datagram[], int len, struct packet pack);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-03-24 19:39:16 +01:00
|
|
|
|
int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-03-24 18:19:35 +01:00
|
|
|
|
int update_neighbours();
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-03-24 19:39:16 +01:00
|
|
|
|
int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-03-31 15:57:31 +02:00
|
|
|
|
void add_tlv(packet *packet, tlv *tlv, char type);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-01 17:51:54 +02:00
|
|
|
|
int send_packet();
|
2020-03-31 22:10:15 +02:00
|
|
|
|
|
2020-04-01 17:56:17 +02:00
|
|
|
|
/* Takes a TLV and sends it over to everyone in the list of addresses.
|
2020-04-01 17:51:25 +02:00
|
|
|
|
* des paires.
|
|
|
|
|
* Retourne -1 en cas d'échec, 0 en cas de succès.
|
|
|
|
|
*/
|
2020-04-01 18:43:53 +02:00
|
|
|
|
int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
|
2020-04-01 17:51:25 +02:00
|
|
|
|
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-01 17:54:01 +02:00
|
|
|
|
/* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste
|
|
|
|
|
* des paires. Chaque pair recevera la même liste de TLV.
|
|
|
|
|
* Retourne -1 en cas d'échec, 0 en cas de succès.
|
|
|
|
|
*/
|
2020-04-01 18:43:53 +02:00
|
|
|
|
int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
|
2020-04-01 17:54:01 +02:00
|
|
|
|
|
|
|
|
|
// threaded functions
|
2020-03-13 14:46:06 +01:00
|
|
|
|
void t_ask_for_more_peers();
|
|
|
|
|
|
|
|
|
|
void t_update_neighbours();
|
|
|
|
|
|
|
|
|
|
void t_get_network_state();
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
2020-03-31 19:16:23 +02:00
|
|
|
|
int len_list(list *l);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-03-31 19:16:23 +02:00
|
|
|
|
neighbour_peer *get_random_neighbour();
|
2020-03-24 12:24:56 +01:00
|
|
|
|
|
|
|
|
|
#endif
|