dazibao/src/node.h
2020-04-29 22:22:30 +02:00

150 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Define constants
#ifndef NODE_H
#define NODE_H
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <net/if.h>
#include <errno.h>
#include <endian.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <locale.h>
/* 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;
int16_t port;
char is_temporary;
time_t 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 pub_data {
unsigned char length;
uint64_t id;
uint16_t seqno;
char *data;
} pub_data;
// General list
typedef struct list {
void *data;
void *next;
} list;
#include "tlv.h"
#include "hash.h"
#include "parser.h"
#include "debug.h"
// On which port do we listen to
#define LISTEN_PORT 1212
// The node ID
// #define NODE_ID 42675882021843277
#define NODE_ID 42013376900101010
// The number of neighbours
// The neighbour table has 15 entries
#define NEIGHBOUR_MAX 15
// The adress of the main peer
#define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b"
// fonctions signatures
int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd);
int check_header(char * received_data_buffer, int received_data_len, packet * packet_to_return);
int validate_tlv(char *data, int pos, uint16_t packet_len);
int update_neighbours();
int ask_for_peers(int socket_num);
int work_with_tlvs(char * data, uint16_t packet_len, struct sockaddr_in6 *sender, int socket_num);
int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num);
int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num);
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num);
// This function is in charge of saying how and what goes where.
int run_node(int sock_fd);
/* Takes a TLV and sends it over to everyone in the list of addresses.
* Returns -1 in case of error, 0 otherwise.
*/
int send_tlv(tlv *tlv_to_send, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num);
/* Takes a list of TLV and sends them over to everyone in the list of addresses.
* Returns -1 in case of error, 0 otherwise.
*/
int send_tlvs(struct list * tlv_list, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num);
/* Check our peer list. If we have less than 5 peers, send out a
TLV NEIGHBOUR_REQUEST to a random peer
*/
int t_ask_for_more_peers(int sock_fd);
/* 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.
*/
int t_update_neighbours();
/* We send out a TLV Network Hash to every peer, and we expect getting a TLV
Network state from each of them.
*/
int t_get_network_state(int sock_fd);
// This function adds a message to the message table.
int add_message(char * message, int message_len);
// This functions creates the structures needed for the rest of the project,
// creates the socket, and returns all of it.
int bootstrap_node(int * sock_fd);
// Helper functions
int len_list(list *l);
neighbour_peer *get_random_neighbour();
// Search for this peer in the neighbour table
neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port);
// Return -1 if the peer could not be added, 0 if it could or if it was already in the table
int add_n_update_neighbour(struct in6_addr *ip, int16_t port);
// get data associated with id, if it doesn't exist return NULL
pub_data *get_data(uint64_t id);
// Take data as args and create a pub_data structure in the heap
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data);
// add new data to data list
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found);
#endif