103 lines
2.4 KiB
C
103 lines
2.4 KiB
C
// Define constants
|
||
|
||
#ifndef NODE_H
|
||
#define NODE_H
|
||
|
||
#include <stdlib.h>
|
||
#include <time.h>
|
||
#include <stdio.h>
|
||
#include <sys/types.h>
|
||
#include <sys/socket.h>
|
||
#include <netinet/in.h>
|
||
#include <string.h>
|
||
|
||
#include "tlv.h"
|
||
#include "hash.h"
|
||
|
||
// On which port do we listen to
|
||
#define LISTEN_PORT 1212
|
||
|
||
// The node ID
|
||
#define NODE_ID 42675882021843277
|
||
|
||
// 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
|
||
*/
|
||
|
||
typedef struct pub_data {
|
||
unsigned char length;
|
||
long id;
|
||
short seqno;
|
||
char *data;
|
||
} pub_data;
|
||
|
||
// General list
|
||
typedef struct list {
|
||
void *data;
|
||
void *next;
|
||
} list;
|
||
|
||
// Static variables
|
||
static list *data_list;
|
||
static list *neighbour_list;
|
||
|
||
// TODO
|
||
|
||
// fonctions signatures
|
||
void listen_for_packets();
|
||
|
||
int check_header(char * received_datagram[], int len, struct packet pack);
|
||
|
||
int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l);
|
||
|
||
int update_neighbours();
|
||
|
||
int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender);
|
||
|
||
void add_tlv(packet *packet, tlv *tlv, char type);
|
||
|
||
int send_packet();
|
||
|
||
/* 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(struct tlv tlv_to_send, int tlv_type, 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, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
|
||
|
||
// threaded functions
|
||
void t_ask_for_more_peers();
|
||
|
||
void t_update_neighbours();
|
||
|
||
void t_get_network_state();
|
||
|
||
// Helper functions
|
||
int len_list(list *l);
|
||
|
||
neighbour_peer *get_random_neighbour();
|
||
|
||
#endif
|