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>
|
|
|
|
|
#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-04-16 15:27:32 +02:00
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <stdint.h>
|
2020-04-16 18:52:19 +02:00
|
|
|
|
#include <net/if.h>
|
2020-04-29 19:53:19 +02:00
|
|
|
|
#include <errno.h>
|
2020-04-29 22:22:30 +02:00
|
|
|
|
#include <endian.h>
|
2020-04-29 19:53:19 +02:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <unistd.h>
|
2020-04-29 22:22:30 +02:00
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <locale.h>
|
2020-03-24 13:25:18 +01:00
|
|
|
|
|
|
|
|
|
/* 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;
|
2020-04-16 15:27:32 +02:00
|
|
|
|
int16_t port;
|
2020-03-24 13:25:18 +01:00
|
|
|
|
char is_temporary;
|
2020-04-24 21:45:09 +02:00
|
|
|
|
time_t last_seen;
|
2020-03-24 13:25:18 +01:00
|
|
|
|
} 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 {
|
2020-04-16 15:08:22 +02:00
|
|
|
|
unsigned char length;
|
2020-04-29 22:22:30 +02:00
|
|
|
|
uint64_t id;
|
2020-04-29 19:53:19 +02:00
|
|
|
|
uint16_t seqno;
|
2020-04-30 19:56:58 +02:00
|
|
|
|
unsigned 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 {
|
2020-04-16 15:08:22 +02:00
|
|
|
|
void *data;
|
|
|
|
|
void *next;
|
2020-03-31 18:28:12 +02:00
|
|
|
|
} list;
|
|
|
|
|
|
2020-04-16 15:08:22 +02:00
|
|
|
|
#include "tlv.h"
|
|
|
|
|
#include "hash.h"
|
|
|
|
|
#include "parser.h"
|
2020-04-29 20:58:56 +02:00
|
|
|
|
#include "debug.h"
|
2020-04-16 15:08:22 +02:00
|
|
|
|
|
|
|
|
|
// On which port do we listen to
|
|
|
|
|
#define LISTEN_PORT 1212
|
|
|
|
|
|
|
|
|
|
// The node ID
|
2020-04-27 15:50:23 +02:00
|
|
|
|
// #define NODE_ID 42675882021843277
|
2020-04-30 19:56:58 +02:00
|
|
|
|
#define NODE_ID 13809235719890846928
|
2020-04-16 15:08:22 +02:00
|
|
|
|
|
|
|
|
|
// The number of neighbours
|
|
|
|
|
// The neighbour table has 15 entries
|
|
|
|
|
#define NEIGHBOUR_MAX 15
|
2020-03-24 13:25:18 +01:00
|
|
|
|
|
2020-04-18 15:24:11 +02:00
|
|
|
|
// The adress of the main peer
|
2020-04-30 19:56:58 +02:00
|
|
|
|
#define ROOT_PEER_ADDR "::1"
|
2020-04-28 17:48:41 +02:00
|
|
|
|
|
2020-03-24 13:25:18 +01:00
|
|
|
|
|
2020-03-13 14:46:06 +01:00
|
|
|
|
// fonctions signatures
|
2020-04-24 19:07:50 +02:00
|
|
|
|
int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-24 19:07:50 +02:00
|
|
|
|
int check_header(char * received_data_buffer, int received_data_len, packet * packet_to_return);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-29 19:53:19 +02:00
|
|
|
|
int validate_tlv(char *data, int pos, uint16_t packet_len);
|
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-04-24 22:48:10 +02:00
|
|
|
|
int ask_for_peers(int socket_num);
|
|
|
|
|
|
2020-04-29 19:53:19 +02:00
|
|
|
|
int work_with_tlvs(char * data, uint16_t packet_len, struct sockaddr_in6 *sender, int socket_num);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-16 18:52:19 +02:00
|
|
|
|
int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-29 19:53:19 +02:00
|
|
|
|
int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num);
|
2020-04-16 18:52:19 +02:00
|
|
|
|
|
|
|
|
|
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-17 15:36:46 +02:00
|
|
|
|
// This function is in charge of saying how and what goes where.
|
2020-04-24 22:48:10 +02:00
|
|
|
|
int run_node(int sock_fd);
|
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 18:47:38 +02:00
|
|
|
|
* Returns -1 in case of error, 0 otherwise.
|
2020-04-01 17:51:25 +02:00
|
|
|
|
*/
|
2020-04-29 19:53:19 +02:00
|
|
|
|
int send_tlv(tlv *tlv_to_send, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num);
|
2020-04-01 17:51:25 +02:00
|
|
|
|
|
2020-04-01 18:47:38 +02:00
|
|
|
|
/* Takes a list of TLV and sends them over to everyone in the list of addresses.
|
|
|
|
|
* Returns -1 in case of error, 0 otherwise.
|
2020-04-01 17:54:01 +02:00
|
|
|
|
*/
|
2020-04-29 19:53:19 +02:00
|
|
|
|
int send_tlvs(struct list * tlv_list, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num);
|
2020-04-01 17:54:01 +02:00
|
|
|
|
|
2020-04-18 15:02:55 +02:00
|
|
|
|
/* Check our peer list. If we have less than 5 peers, send out a
|
|
|
|
|
TLV NEIGHBOUR_REQUEST to a random peer
|
|
|
|
|
*/
|
2020-04-24 22:48:10 +02:00
|
|
|
|
int t_ask_for_more_peers(int sock_fd);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-18 15:02:55 +02:00
|
|
|
|
/* We look at every peer, if he is marked as is_temporary, AND we didn't get a
|
|
|
|
|
packet from him in the last 70 sec, we remove him from the list.
|
|
|
|
|
*/
|
2020-04-24 22:48:10 +02:00
|
|
|
|
int t_update_neighbours();
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-18 15:02:55 +02:00
|
|
|
|
/* We send out a TLV Network Hash to every peer, and we expect getting a TLV
|
|
|
|
|
Network state from each of them.
|
|
|
|
|
*/
|
2020-04-24 22:48:10 +02:00
|
|
|
|
int t_get_network_state(int sock_fd);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-17 15:36:46 +02:00
|
|
|
|
// This function adds a message to the message table.
|
|
|
|
|
int add_message(char * message, int message_len);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
2020-04-17 15:36:46 +02:00
|
|
|
|
// This functions creates the structures needed for the rest of the project,
|
|
|
|
|
// creates the socket, and returns all of it.
|
2020-04-30 19:56:58 +02:00
|
|
|
|
int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port);
|
2020-03-13 14:46:06 +01:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-04-24 20:23:57 +02:00
|
|
|
|
// Search for this peer in the neighbour table
|
2020-04-24 21:45:09 +02:00
|
|
|
|
neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port);
|
2020-04-24 20:23:57 +02:00
|
|
|
|
|
|
|
|
|
// Return -1 if the peer could not be added, 0 if it could or if it was already in the table
|
2020-04-24 21:45:09 +02:00
|
|
|
|
int add_n_update_neighbour(struct in6_addr *ip, int16_t port);
|
2020-04-24 20:23:57 +02:00
|
|
|
|
|
2020-04-07 19:05:30 +02:00
|
|
|
|
// get data associated with id, if it doesn't exist return NULL
|
2020-04-29 22:22:30 +02:00
|
|
|
|
pub_data *get_data(uint64_t id);
|
2020-04-07 19:05:30 +02:00
|
|
|
|
|
|
|
|
|
// Take data as args and create a pub_data structure in the heap
|
2020-04-30 19:56:58 +02:00
|
|
|
|
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data);
|
2020-04-07 19:05:30 +02:00
|
|
|
|
|
|
|
|
|
// add new data to data list
|
2020-04-30 19:56:58 +02:00
|
|
|
|
int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found);
|
2020-04-07 19:05:30 +02:00
|
|
|
|
|
2020-03-24 12:24:56 +01:00
|
|
|
|
#endif
|