Merge branch '21-cleanup-code' into 'master'
Resolve "cleanup code" Closes #21 See merge request perdriau/dazibao!21
This commit is contained in:
commit
bc180fa6af
@ -1,4 +0,0 @@
|
|||||||
# Notes et recherches sur le projet
|
|
||||||
|
|
||||||
Telecharger la librarie OpenSSl avec 'sudo apt-get install libssl-dev'
|
|
||||||
Utiliser 'gcc -o {exec} {fichier.c} -lssl -lcrypto' pour utiliser la librarie OpenSSL
|
|
@ -1,5 +0,0 @@
|
|||||||
# dazibao
|
|
||||||
|
|
||||||
Le but de ce projet est d’implémenter un dazibao (« journal à grandes lettres »), semblable à
|
|
||||||
un « mur » de réseau social, mais de façon complètement distribuée. Le protocole est basé sur un
|
|
||||||
algorithme non-fiable d’inondation.
|
|
@ -7,9 +7,9 @@
|
|||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
void welcome(){
|
void welcome(){
|
||||||
print_info("----------------------------------------");
|
print_info("----------------------------------------------------------------------");
|
||||||
print_info(" 大字报");
|
print_info(" 大 字 报");
|
||||||
print_info("----------------------------------------");
|
print_info("-----------------------------------------------------------------------");
|
||||||
print_info(">> To write a message, just write it to the terminal and press enter.");
|
print_info(">> To write a message, just write it to the terminal and press enter.");
|
||||||
print_info(">> To show all messages, just press enter without writing anything.");
|
print_info(">> To show all messages, just press enter without writing anything.");
|
||||||
}
|
}
|
||||||
@ -20,7 +20,7 @@ void print_info(char * msg) {
|
|||||||
|
|
||||||
void print_debug(char * msg){
|
void print_debug(char * msg){
|
||||||
if (DEBUG_LEVEL > 0) {
|
if (DEBUG_LEVEL > 0) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg);
|
printf("\x1b[31m[DEBUG]\x1b[0m %s\n", msg);
|
||||||
}
|
}
|
||||||
if (DEBUG_LEVEL > 9) {
|
if (DEBUG_LEVEL > 9) {
|
||||||
getchar();
|
getchar();
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
|
|
||||||
#define DEBUG_LEVEL 7
|
#define DEBUG_LEVEL 7
|
||||||
|
|
||||||
void welcome();
|
void welcome();
|
||||||
|
32
src/hash.c
32
src/hash.c
@ -3,13 +3,6 @@
|
|||||||
|
|
||||||
// Hash a single data
|
// Hash a single data
|
||||||
void hash_data(pub_data *data, unsigned char *buf) {
|
void hash_data(pub_data *data, unsigned char *buf) {
|
||||||
/*
|
|
||||||
data->length = 52 - 26;
|
|
||||||
data->id = 34538;
|
|
||||||
data->seqno = 4864;
|
|
||||||
data->data = "Luke, je suis ton \"pair\" !";
|
|
||||||
printf("Hash received: 9ffe841a99776f6d1295ac75b53a58d7\n");
|
|
||||||
*/
|
|
||||||
|
|
||||||
// All three fields are concatenated into a single buffer
|
// All three fields are concatenated into a single buffer
|
||||||
int totlen = data->length + 10;
|
int totlen = data->length + 10;
|
||||||
@ -27,13 +20,6 @@ void hash_data(pub_data *data, unsigned char *buf) {
|
|||||||
// Put truncated hash into buf
|
// Put truncated hash into buf
|
||||||
hash_trunc(hash, buf);
|
hash_trunc(hash, buf);
|
||||||
|
|
||||||
/*
|
|
||||||
printf("Hash built: ");
|
|
||||||
for(int i = 0; i < 16; i++) {
|
|
||||||
printf("%02x", buf[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hash every data contained in data_list then return a network hash
|
// Hash every data contained in data_list then return a network hash
|
||||||
@ -90,24 +76,6 @@ void concat_data(pub_data *data, unsigned char *buf) {
|
|||||||
if (memcpy(buf+10, data->data, data->length) == NULL) {
|
if (memcpy(buf+10, data->data, data->length) == NULL) {
|
||||||
print_debug(">> Contact the data (data) didn't work !");
|
print_debug(">> Contact the data (data) didn't work !");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
uint64_t *id = (uint64_t *) buf;
|
|
||||||
uint16_t *seqno2 = (uint16_t *) (buf + 8);
|
|
||||||
char *message = (char*) (buf + 10);
|
|
||||||
char fuck[100];
|
|
||||||
|
|
||||||
printf("id: %ld\nseqno: %d\nmessage: ", *id, *seqno2);
|
|
||||||
for(int i = 0; i < data->length; i++) {
|
|
||||||
printf("%c", message[i]);
|
|
||||||
}
|
|
||||||
printf("\nORIGINAL\n");
|
|
||||||
printf("id: %ld\nseqno: %d\nmessage: ", data->id, htobe16(data->seqno));
|
|
||||||
for(int i = 0; i < data->length; i++) {
|
|
||||||
printf("%c", data->data[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concat hash2 to hash1 (hash1 is modified)
|
// Concat hash2 to hash1 (hash1 is modified)
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
#include "parser.h"
|
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
// Hash a single data
|
// Hash a single data
|
||||||
|
@ -1 +0,0 @@
|
|||||||
sudo apt-get install libssl-dev
|
|
269
src/node.c
269
src/node.c
@ -29,15 +29,6 @@ int ask_for_peers(int socket_num) {
|
|||||||
struct in6_addr ip = peer->ip;
|
struct in6_addr ip = peer->ip;
|
||||||
int16_t port = peer->port;
|
int16_t port = peer->port;
|
||||||
|
|
||||||
/*int ifindex = if_nametoindex("enp3s0");
|
|
||||||
if(ifindex == 0) {
|
|
||||||
int ifindex = if_nametoindex("eth0");
|
|
||||||
if(ifindex == 0) {
|
|
||||||
perror("if_nametoindex failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
int ifindex = 0;
|
int ifindex = 0;
|
||||||
|
|
||||||
// Initialize sockaddr
|
// Initialize sockaddr
|
||||||
@ -130,7 +121,6 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, uint16_t port) {
|
|||||||
// Return 0 if peer was updated as last_seen
|
// Return 0 if peer was updated as last_seen
|
||||||
int add_n_update_neighbour(struct in6_addr *ip, uint16_t port) {
|
int add_n_update_neighbour(struct in6_addr *ip, uint16_t port) {
|
||||||
|
|
||||||
|
|
||||||
// We try to find a peer with this address and port.
|
// We try to find a peer with this address and port.
|
||||||
neighbour_peer *peer = get_neighbour(ip, port);
|
neighbour_peer *peer = get_neighbour(ip, port);
|
||||||
time_t curtime;
|
time_t curtime;
|
||||||
@ -218,15 +208,15 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new_data;
|
return new_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added
|
// A node state TLV was received and either no data associated to it's id is in
|
||||||
|
// our data list or the data was updated, return -1 if an error occurend,
|
||||||
|
// 0 if nothing had to be done and 1 if something was updated/added
|
||||||
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) {
|
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) {
|
||||||
// Check if it's our own id
|
// Check if it's our own id
|
||||||
if(id == NODE_ID) {
|
if(id == NODE_ID) {
|
||||||
// wtf
|
|
||||||
if(found == NULL) {
|
if(found == NULL) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Our own node is not in the data list, something went terribly wrong.\n");
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Our own node is not in the data list, something went terribly wrong.\n");
|
||||||
return -1;
|
return -1;
|
||||||
@ -244,7 +234,8 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's not our own id, update the data if it's already in our data list and seqno is bigger than our stored seqno
|
// If it's not our own id, update the data if it's already in our data
|
||||||
|
// list and seqno is bigger than our stored seqno
|
||||||
if(found != NULL) {
|
if(found != NULL) {
|
||||||
// Check if seqno is smaller or equals to our stored seqno
|
// Check if seqno is smaller or equals to our stored seqno
|
||||||
if( ((found->seqno - seqno) & 32768) == 0 ) {
|
if( ((found->seqno - seqno) & 32768) == 0 ) {
|
||||||
@ -257,13 +248,13 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
|
|||||||
found->id = id;
|
found->id = id;
|
||||||
found->seqno = seqno;
|
found->seqno = seqno;
|
||||||
|
|
||||||
// Updata message
|
// Update message
|
||||||
free(found->data);
|
free(found->data);
|
||||||
found->data = (char*) malloc(len);
|
found->data = (char*) malloc(len);
|
||||||
memcpy(found->data, data, len);
|
memcpy(found->data, data, len);
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 0) {
|
if (DEBUG_LEVEL > 0) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Updated %li's published data.\n", id);
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Updated %lu 's published data.\n", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -335,6 +326,7 @@ int update_neighbours() {
|
|||||||
// check every neighbour
|
// check every neighbour
|
||||||
while(tmp != NULL) {
|
while(tmp != NULL) {
|
||||||
peer = (neighbour_peer*) tmp->data;
|
peer = (neighbour_peer*) tmp->data;
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 1) {
|
if (DEBUG_LEVEL > 1) {
|
||||||
char * buff_str_ip[1024];
|
char * buff_str_ip[1024];
|
||||||
char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024);
|
char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024);
|
||||||
@ -342,7 +334,8 @@ int update_neighbours() {
|
|||||||
}
|
}
|
||||||
// Check if peer is temporary
|
// Check if peer is temporary
|
||||||
if(peer->is_temporary) {
|
if(peer->is_temporary) {
|
||||||
// If it's been 70 seconds or more since we last received a packet from this peer then remove it from the list
|
// If it's been 70 seconds or more since we last received a packet
|
||||||
|
// from this peer then remove it from the list
|
||||||
time(&curtime);
|
time(&curtime);
|
||||||
|
|
||||||
if(difftime(peer->last_seen, curtime) >= 70) {
|
if(difftime(peer->last_seen, curtime) >= 70) {
|
||||||
@ -394,7 +387,8 @@ int update_neighbours() {
|
|||||||
return deleted;
|
return deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add TLV to packet, if it does not fit then send the packet and reset the packet buff to be able to add more TLVs that will be sent afterwards
|
// Add TLV to packet, if it does not fit then send the packet and reset the
|
||||||
|
// packet buff to be able to add more TLVs that will be sent afterwards
|
||||||
int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
||||||
print_debug(">> Adding tlv to packet");
|
print_debug(">> Adding tlv to packet");
|
||||||
char type = tlv->pad1->type, sent = 0, errval = 0;
|
char type = tlv->pad1->type, sent = 0, errval = 0;
|
||||||
@ -536,17 +530,16 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
|||||||
packet_tlv_send_out.msg_name = dest;
|
packet_tlv_send_out.msg_name = dest;
|
||||||
packet_tlv_send_out.msg_namelen = sizeof(struct sockaddr_in6);
|
packet_tlv_send_out.msg_namelen = sizeof(struct sockaddr_in6);
|
||||||
packet_tlv_send_out.msg_iov = vec_buff;
|
packet_tlv_send_out.msg_iov = vec_buff;
|
||||||
packet_tlv_send_out.msg_iovlen = 1; // We have only one iovec buffer. But if we had 2, we would write 2.
|
packet_tlv_send_out.msg_iovlen = 1;
|
||||||
|
|
||||||
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
|
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
|
||||||
|
|
||||||
if (response_code < 0) {
|
if (response_code < 0) {
|
||||||
print_error("Unable to send out the packet to peer.");
|
print_error("Unable to send out the packet to peer.");
|
||||||
error_while_sending = 1;
|
error_while_sending = 1;
|
||||||
} else if (response_code < length) {
|
} else if (response_code < length) {
|
||||||
print_debug(">> Sent out only part of the packet.");
|
print_debug(">> Sent out only part of the packet.");
|
||||||
error_while_sending = 1;
|
error_while_sending = 1;
|
||||||
} else {
|
|
||||||
print_debug(">> Send out packet to peer.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error_while_sending == 1) {
|
if (error_while_sending == 1) {
|
||||||
@ -559,14 +552,16 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a single TLV to the specified addresses, return -1 if an error was encountered, 0 otherwise
|
// Send a single TLV to the specified addresses, return -1 if an error
|
||||||
|
// was encountered, 0 otherwise
|
||||||
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
||||||
|
|
||||||
char type = tlv->pad1->type;
|
char type = tlv->pad1->type;
|
||||||
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
||||||
memset(pack.body, 0, 1020);
|
memset(pack.body, 0, 1020);
|
||||||
|
|
||||||
// Copy data from tlv into body
|
// Copy data from tlv into body
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case 1:
|
case 1:
|
||||||
memcpy(pack.body + pack.length, (char*) &tlv->pad1->type, 1);
|
memcpy(pack.body + pack.length, (char*) &tlv->pad1->type, 1);
|
||||||
pack.length += 1;
|
pack.length += 1;
|
||||||
@ -647,71 +642,6 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
return send_packet((char*) &pack, pack.length, dest, socket_num);
|
return send_packet((char*) &pack, pack.length, dest, socket_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO This function can be deleted.
|
|
||||||
// int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){
|
|
||||||
// print_debug(">> Building packet to send a TLV.");
|
|
||||||
//
|
|
||||||
// // We first need to build the packet,
|
|
||||||
// char packet_buff[1024];
|
|
||||||
// struct packet pack;
|
|
||||||
// pack.magic = 95;
|
|
||||||
// pack.version = 1;
|
|
||||||
// if (tlv_size > 1020) {
|
|
||||||
// print_debug(">> Unable to send the tlv, it's size is above 1020 bytes.");
|
|
||||||
// return -1;
|
|
||||||
// } else {
|
|
||||||
// memcpy((void *) pack.body, tlv_to_send, tlv_size);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Move the content of the paquet struct to a buffer
|
|
||||||
// // That will be send out in a vectorized buffer.
|
|
||||||
// memcpy(&packet_buff,&pack,1024);
|
|
||||||
//
|
|
||||||
// if (DEBUG_LEVEL > 1) {
|
|
||||||
// print_debug(">> Packet has been built.");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Vectorized buffer
|
|
||||||
// struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff };
|
|
||||||
//
|
|
||||||
// int error_while_sending = 0;
|
|
||||||
//
|
|
||||||
// // For every dest
|
|
||||||
// for (size_t i = 0; i < dest_list_size; i++) {
|
|
||||||
// // Creating the struct to send out with sendmsg
|
|
||||||
// struct msghdr packet_tlv_send_out = {
|
|
||||||
// .msg_name = &dest_list[i],
|
|
||||||
// .msg_namelen = sizeof(dest_list[i]),
|
|
||||||
// .msg_iov = &vec_buff,
|
|
||||||
// .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0);
|
|
||||||
// if (response_code < 0) {
|
|
||||||
// if (DEBUG_LEVEL > 0) {
|
|
||||||
// printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i);
|
|
||||||
// }
|
|
||||||
// error_while_sending = 1;
|
|
||||||
// continue;
|
|
||||||
// } else if (response_code < sizeof(packet_tlv_send_out)) {
|
|
||||||
// print_debug(">> Sent out only part of the packet.");
|
|
||||||
// error_while_sending = 1;
|
|
||||||
// continue;
|
|
||||||
// } else {
|
|
||||||
// if (DEBUG_LEVEL > 0) {
|
|
||||||
// printf("\x1b[31m[DEBUG]\x1b[0m >> Sent out packet to peer %i", i);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if (error_while_sending == 1) {
|
|
||||||
// print_debug(">> Error occured while sending out a packet.");
|
|
||||||
// return -1;
|
|
||||||
// } else {
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// We need to make sure the TLV announces a length that will no go onto
|
// We need to make sure the TLV announces a length that will no go onto
|
||||||
// another tlv, as we might end up reading bullshit.
|
// another tlv, as we might end up reading bullshit.
|
||||||
int validate_tlv(char *data, int pos, uint16_t packet_len){
|
int validate_tlv(char *data, int pos, uint16_t packet_len){
|
||||||
@ -739,89 +669,50 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 1) {
|
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> TLV has type %i\n", type );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the type of the tlv or -1 if something went wrong
|
// Returns the type of the tlv or -1 if something went wrong
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case 1:
|
case 1:
|
||||||
|
print_debug(">> Received a TLV padding");
|
||||||
return 1;
|
return 1;
|
||||||
case 2:
|
case 2:
|
||||||
if(tlv_len != LEN_NEIGHBOUR_REQ) return -1;
|
if(tlv_len != LEN_NEIGHBOUR_REQ) return -1;
|
||||||
|
print_debug(">> Received a TLV Neighbour Request");
|
||||||
return 2;
|
return 2;
|
||||||
case 3:
|
case 3:
|
||||||
if(tlv_len != LEN_NEIGHBOUR) return -1;
|
if(tlv_len != LEN_NEIGHBOUR) return -1;
|
||||||
|
print_debug(">> Received a TLV Neighbour");
|
||||||
return 3;
|
return 3;
|
||||||
case 4:
|
case 4:
|
||||||
if(tlv_len != LEN_NETWORK_HASH) return -1;
|
if(tlv_len != LEN_NETWORK_HASH) return -1;
|
||||||
|
print_debug(">> Received a TLV Network hash");
|
||||||
return 4;
|
return 4;
|
||||||
case 5:
|
case 5:
|
||||||
if(tlv_len != LEN_NETWORK_STATE_REQ) return -1;
|
if(tlv_len != LEN_NETWORK_STATE_REQ) return -1;
|
||||||
|
print_debug(">> Received a TLV Network state request");
|
||||||
return 5;
|
return 5;
|
||||||
case 6:
|
case 6:
|
||||||
if(tlv_len != LEN_NODE_HASH) return -1;
|
if(tlv_len != LEN_NODE_HASH) return -1;
|
||||||
|
print_debug(">> Received a TLV Node hash");
|
||||||
return 6;
|
return 6;
|
||||||
case 7:
|
case 7:
|
||||||
if(tlv_len != LEN_NODE_STATE_REQ) return -1;
|
if(tlv_len != LEN_NODE_STATE_REQ) return -1;
|
||||||
|
print_debug(">> Received a TLV Node state request");
|
||||||
return 7;
|
return 7;
|
||||||
case 8:
|
case 8:
|
||||||
if(tlv_len < MIN_LEN_NODE_STATE || tlv_len > MAX_LEN_NODE_STATE) return -1;
|
if(tlv_len < MIN_LEN_NODE_STATE || tlv_len > MAX_LEN_NODE_STATE) return -1;
|
||||||
|
print_debug(">> Received a TLV Node state");
|
||||||
/*
|
|
||||||
// Check if it has the right hash
|
|
||||||
uint16_t *seqno = (uint16_t*) (data + pos + 10);
|
|
||||||
uint64_t *id = (uint64_t*) (data + pos + 2);
|
|
||||||
|
|
||||||
pub_data pdata = (pub_data) {
|
|
||||||
.length = tlv_len,
|
|
||||||
.id = *id,
|
|
||||||
.seqno = *seqno,
|
|
||||||
.data = (unsigned char*) malloc(tlv_len)
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned char *cur_hash = (unsigned char*) (data + pos + 12);
|
|
||||||
char *cur_message = data + pos + 28;
|
|
||||||
|
|
||||||
unsigned char hash[16];
|
|
||||||
|
|
||||||
memcpy(pdata.data, cur_message, tlv_len);
|
|
||||||
|
|
||||||
hash_data(&pdata, hash);
|
|
||||||
|
|
||||||
if(memcmp(hash, cur_hash, 16) != 0) {
|
|
||||||
print_debug(">> Malformed hash.");
|
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
|
|
||||||
|
|
||||||
for(int x = 0; x < 16; x++){
|
|
||||||
printf("%02x", hash[x]);
|
|
||||||
fflush(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
|
|
||||||
|
|
||||||
for(int x = 0; x < 16; x++){
|
|
||||||
printf("%02x", cur_hash[x]);
|
|
||||||
fflush(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return 8;
|
return 8;
|
||||||
case 9:
|
case 9:
|
||||||
|
print_error(">> Received a TLV Warning");
|
||||||
return 9;
|
return 9;
|
||||||
default:
|
default:
|
||||||
|
print_error("Could not tell which TLV we received");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For every packet recivied,
|
// For every packet recivied, we make sure it's conform
|
||||||
// then we make sure it's conform
|
|
||||||
// We then extract the data from it to make it easy to work with
|
// We then extract the data from it to make it easy to work with
|
||||||
int check_header(char * received_data_buffer, int received_data_len, struct packet * packet_to_return){
|
int check_header(char * received_data_buffer, int received_data_len, struct packet * packet_to_return){
|
||||||
|
|
||||||
@ -843,7 +734,8 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
|
|||||||
// Convert to hardware order.
|
// Convert to hardware order.
|
||||||
((packet*) packet_to_return)->length = be16toh(((packet*) packet_to_return)->length);
|
((packet*) packet_to_return)->length = be16toh(((packet*) packet_to_return)->length);
|
||||||
if (packet_to_return->length + 4 < received_data_len ) {
|
if (packet_to_return->length + 4 < received_data_len ) {
|
||||||
print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics.");
|
print_debug(">> The packet length is bigger than the UDP datagram, which \
|
||||||
|
is not possible with the current laws of physics.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -871,7 +763,8 @@ int add_message(char * message, int message_len){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_debug(">> Message could not be added because our own ID is not in the data_list, something went wrong.");
|
print_error(">> Message could not be added because our own ID is not in the data_list, \
|
||||||
|
something went wrong.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -880,7 +773,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
uint16_t packet_len = ((packet*) data)->length;
|
uint16_t packet_len = ((packet*) data)->length;
|
||||||
|
|
||||||
if(packet_len != total_packet_len - 4) {
|
if(packet_len != total_packet_len - 4) {
|
||||||
print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet.");
|
print_debug(">> Length indicated in packet differs from real length of packet received,\
|
||||||
|
disgarding packet.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int nbr_of_tlvs = 0;
|
int nbr_of_tlvs = 0;
|
||||||
@ -898,7 +792,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
|
|
||||||
// holds the random neighbour we send back in case of a neighbour_req.
|
// holds the random neighbour we send back in case of a neighbour_req.
|
||||||
struct neighbour_peer *random_neighbour;
|
struct neighbour_peer *random_neighbour;
|
||||||
// memset(random_neighbour, 0, sizeof(struct neighbour_peer));
|
|
||||||
|
|
||||||
// Holds the new neighbour send to us by a "neighbour tlv"
|
// Holds the new neighbour send to us by a "neighbour tlv"
|
||||||
struct sockaddr_in6 new_neighbour;
|
struct sockaddr_in6 new_neighbour;
|
||||||
@ -917,10 +810,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
|
|
||||||
while(pos < total_packet_len) {
|
while(pos < total_packet_len) {
|
||||||
// Making sure the current TLV we are looking at is valid.
|
// Making sure the current TLV we are looking at is valid.
|
||||||
// TODO : I think we should reset all the structs here.
|
|
||||||
// memset(random_neighbour, 0, sizeof(struct neighbour_peer));
|
|
||||||
// memset(pdata, 0, sizeof(struct pub_data));
|
|
||||||
// memset(tmp_list, 0, sizeof(struct list));
|
|
||||||
memset(hash, 0, 16);
|
memset(hash, 0, 16);
|
||||||
memset(warn, 0, 32);
|
memset(warn, 0, 32);
|
||||||
tlv_len = 0;
|
tlv_len = 0;
|
||||||
@ -951,10 +840,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : Seems to be a bug here, as this frees the new_tlv
|
|
||||||
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
||||||
// TODO : I suppose that since new_tlv is freed above, add_tlv ads an empty
|
|
||||||
// value to the packet ?
|
|
||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
@ -1039,8 +925,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
tmp_list = tmp_list->next;
|
tmp_list = tmp_list->next;
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 1) {
|
if (DEBUG_LEVEL > 1) {
|
||||||
// TODO wtf is this ?
|
printf(" >> \x1b[31m[DEBUG]\x1b[0m Sent back hash of the message we have for %lu\n", *(uint64_t*) (pack.body + 2));
|
||||||
printf(" >> \x1b[31m[DEBUG]\x1b[0m %lu\n", *(uint64_t*) (pack.body + 2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1054,7 +939,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
// We received a node hash tlv so
|
// We received a node hash tlv so
|
||||||
// if there is no entry for node_id in the data list or the hashes differ
|
// if there is no entry for node_id in the data list or the hashes differ
|
||||||
// we send a node state request,
|
// we send a node state request,
|
||||||
//if the hashes are identical nothing has to be done
|
// if the hashes are identical nothing has to be done
|
||||||
print_debug(">> Received node hash, updating message entry...");
|
print_debug(">> Received node hash, updating message entry...");
|
||||||
|
|
||||||
id = (uint64_t*) (data + pos + 2);
|
id = (uint64_t*) (data + pos + 2);
|
||||||
@ -1085,7 +970,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 0) {
|
if (DEBUG_LEVEL > 0) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Sending node state request for node %lu...\n", be64toh(*id));
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Sending node state request for node %lu \n", be64toh(*id));
|
||||||
}
|
}
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
@ -1117,8 +1002,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
// We received a node state tlv so
|
// We received a node state tlv so we add it to the data list
|
||||||
// we add it to the data list
|
|
||||||
// or update the data stored
|
// or update the data stored
|
||||||
print_debug(">> Received node state, updating...");
|
print_debug(">> Received node state, updating...");
|
||||||
|
|
||||||
@ -1129,21 +1013,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
cur_message = data + pos + 28;
|
cur_message = data + pos + 28;
|
||||||
|
|
||||||
print_debug(">> Received message ! ");
|
print_debug(">> Received message ! ");
|
||||||
// printf("Type : %d\n", data[pos] );
|
|
||||||
// printf("Length : %d\n", tlv_len );
|
|
||||||
// printf("Node ID : %lu\n", be64toh(*id) );
|
|
||||||
// printf("Seqno : %hu\n", be16toh(*seqno) );
|
|
||||||
// printf("Hash :");
|
|
||||||
// for(int x = 0; x < 16; x++){
|
|
||||||
// printf("%02x", cur_hash[x]);
|
|
||||||
// fflush(0);
|
|
||||||
// }
|
|
||||||
// printf("\n");
|
|
||||||
// for(int x = 0; x < tlv_len - 26; x++){
|
|
||||||
// printf("%c", cur_message[x]);
|
|
||||||
// fflush(0);
|
|
||||||
// }
|
|
||||||
// printf("\n");
|
|
||||||
|
|
||||||
// Check if it has the right hash
|
// Check if it has the right hash
|
||||||
pub_data pdata_check = (pub_data) {
|
pub_data pdata_check = (pub_data) {
|
||||||
@ -1157,23 +1026,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
|
|
||||||
hash_data(&pdata_check, hash2);
|
hash_data(&pdata_check, hash2);
|
||||||
|
|
||||||
// print_debug(">> Built message: ");
|
|
||||||
// printf("Type : %d\n", data[pos] );
|
|
||||||
// printf("Length : %d\n", pdata_check.length + 26);
|
|
||||||
// printf("Node ID : %lu\n", pdata_check.id);
|
|
||||||
// printf("Seqno : %hu\n", pdata_check.seqno);
|
|
||||||
// printf("Hash :");
|
|
||||||
// for(int x = 0; x < 16; x++){
|
|
||||||
// printf("%02x", hash2[x]);
|
|
||||||
// fflush(0);
|
|
||||||
// }
|
|
||||||
// printf("\n");
|
|
||||||
// for(int x = 0; x < tlv_len - 26; x++){
|
|
||||||
// printf("%c", pdata_check.data[x]);
|
|
||||||
// fflush(0);
|
|
||||||
// }
|
|
||||||
// printf("\n");
|
|
||||||
|
|
||||||
if(memcmp(hash2, cur_hash, 16) != 0) {
|
if(memcmp(hash2, cur_hash, 16) != 0) {
|
||||||
print_debug(">> Malformed hash.");
|
print_debug(">> Malformed hash.");
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : ");
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : ");
|
||||||
@ -1263,7 +1115,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
if(pack.length > 0){
|
if(pack.length > 0){
|
||||||
send_packet((char*) &pack, pack.length, sender, socket_num);
|
send_packet((char*) &pack, pack.length, sender, socket_num);
|
||||||
}
|
}
|
||||||
// print_data(data_list);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1307,11 +1158,12 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
|
|||||||
}
|
}
|
||||||
|
|
||||||
int t_ask_for_more_peers(int sock_fd){
|
int t_ask_for_more_peers(int sock_fd){
|
||||||
if (DEBUG_LEVEL > 1) {
|
if (ask_for_peers(sock_fd) < 0 ){
|
||||||
print_peers(neighbour_list);
|
print_error("Error while asking for more peers !");
|
||||||
sleep(3);
|
return -1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return ask_for_peers(sock_fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* For every peer we know about, we send out a TLV network hash
|
/* For every peer we know about, we send out a TLV network hash
|
||||||
@ -1362,7 +1214,15 @@ int t_get_network_state(int sock_fd){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int t_update_neighbours(){
|
int t_update_neighbours(){
|
||||||
return update_neighbours();
|
int nbr_deleted = update_neighbours();
|
||||||
|
if (nbr_deleted < 0) {
|
||||||
|
print_error("Error while deleting peers for the peer list.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (DEBUG_LEVEL > 0) {
|
||||||
|
printf("\x1b[31m[DEBUG]\x1b[0m >> We deleted %i peers for the peer list.\n", nbr_deleted);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int run_node(int sock_fd){
|
int run_node(int sock_fd){
|
||||||
@ -1376,7 +1236,7 @@ int run_node(int sock_fd){
|
|||||||
|
|
||||||
// Init the ~20s delay for node update.
|
// Init the ~20s delay for node update.
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
time_t delay = time(NULL) + 5;
|
time_t delay = time(NULL) + 1;
|
||||||
|
|
||||||
/* Descriptor zero is stdin */
|
/* Descriptor zero is stdin */
|
||||||
fds[0].fd = 0;
|
fds[0].fd = 0;
|
||||||
@ -1398,16 +1258,6 @@ int run_node(int sock_fd){
|
|||||||
delay = time(NULL) + 20 + (rand() % 10);
|
delay = time(NULL) + 20 + (rand() % 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This might be cool to add, but we need to find a way to write to stdin
|
|
||||||
// while it's running.
|
|
||||||
// if (time(NULL) < delay) {
|
|
||||||
// // Thanks to :
|
|
||||||
// // https://gist.github.com/amullins83/24b5ef48657c08c4005a8fab837b7499
|
|
||||||
// printf("\b\x1b[2K\r>> Next request in %li seconds..", delay - time(NULL));
|
|
||||||
// fflush(stdout);
|
|
||||||
// }
|
|
||||||
// printf("\n");
|
|
||||||
|
|
||||||
/* Call poll() */
|
/* Call poll() */
|
||||||
ret = poll(fds, 2, 10);
|
ret = poll(fds, 2, 10);
|
||||||
|
|
||||||
@ -1458,7 +1308,6 @@ int run_node(int sock_fd){
|
|||||||
// Read data from the socket ( incoming packet )
|
// Read data from the socket ( incoming packet )
|
||||||
if (fds[1].revents & (POLLIN | POLLPRI)) {
|
if (fds[1].revents & (POLLIN | POLLPRI)) {
|
||||||
|
|
||||||
|
|
||||||
// Vectorized buffer
|
// Vectorized buffer
|
||||||
struct iovec vec_buff_rec = { .iov_len = sizeof(output_buffer), .iov_base = output_buffer };
|
struct iovec vec_buff_rec = { .iov_len = sizeof(output_buffer), .iov_base = output_buffer };
|
||||||
|
|
||||||
@ -1469,8 +1318,8 @@ int run_node(int sock_fd){
|
|||||||
.msg_name = &sender,
|
.msg_name = &sender,
|
||||||
.msg_namelen = sizeof(sender),
|
.msg_namelen = sizeof(sender),
|
||||||
.msg_iov = &vec_buff_rec,
|
.msg_iov = &vec_buff_rec,
|
||||||
.msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
|
.msg_iovlen = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
bytes = recvmsg(sock_fd, &msg_from_peer, 0);
|
bytes = recvmsg(sock_fd, &msg_from_peer, 0);
|
||||||
if (bytes < 0) {
|
if (bytes < 0) {
|
||||||
@ -1495,9 +1344,7 @@ int run_node(int sock_fd){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO : Here, we can write all of the current messages we have in stack
|
// Todo : write HTML output of our message list ?
|
||||||
// to a file, or to stdout.
|
|
||||||
// TODO : Same as above, but for the peers have know about.
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1524,7 +1371,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){
|
|||||||
memset(&server_addr, 0, sizeof(server_addr));
|
memset(&server_addr, 0, sizeof(server_addr));
|
||||||
server_addr.sin6_family = AF_INET6;
|
server_addr.sin6_family = AF_INET6;
|
||||||
// server_addr.sin6_addr = INADDR_ANY;
|
// server_addr.sin6_addr = INADDR_ANY;
|
||||||
server_addr.sin6_port = LISTEN_PORT;
|
server_addr.sin6_port = be16toh(LISTEN_PORT);
|
||||||
if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
|
if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
|
||||||
print_error("Failed to bind socket");
|
print_error("Failed to bind socket");
|
||||||
perror("");
|
perror("");
|
||||||
|
@ -55,7 +55,6 @@ typedef struct list {
|
|||||||
|
|
||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "parser.h"
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
// On which port do we listen to
|
// On which port do we listen to
|
||||||
|
32
src/parser.c
32
src/parser.c
@ -1,32 +0,0 @@
|
|||||||
#include "parser.h"
|
|
||||||
|
|
||||||
// retourne le type de commande à exécuter
|
|
||||||
cmd_token parse_cmd() {
|
|
||||||
char buf[198], cmd[5], arg[193];
|
|
||||||
cmd_token token;
|
|
||||||
token.type = ERROR;
|
|
||||||
memset(token.arg, 0, 193);
|
|
||||||
|
|
||||||
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
|
|
||||||
if(sscanf(buf, "%s %[^\t\n]", cmd, arg) != 2)
|
|
||||||
return token;
|
|
||||||
|
|
||||||
if(strcmp("req", cmd) == 0) {
|
|
||||||
if(strcmp("neighbour", arg) == 0)
|
|
||||||
token.type = NEIGHBOUR_REQ;
|
|
||||||
else if(strcmp("network state", arg) == 0)
|
|
||||||
token.type = NETWORK_STATE_REQ;
|
|
||||||
else if(strcmp("node state", arg) == 0)
|
|
||||||
token.type = NODE_STATE_REQ;
|
|
||||||
} else if(strcmp("post", cmd) == 0) {
|
|
||||||
token.type = POST;
|
|
||||||
//arg[192] = 0;
|
|
||||||
strcpy(token.arg, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
return token;
|
|
||||||
}
|
|
19
src/parser.h
19
src/parser.h
@ -1,19 +0,0 @@
|
|||||||
#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;
|
|
||||||
|
|
||||||
typedef struct cmd_token {
|
|
||||||
cmd_type type;
|
|
||||||
char arg[193];
|
|
||||||
} cmd_token;
|
|
||||||
|
|
||||||
// retourne le type de commande à exécuter
|
|
||||||
cmd_token parse_cmd();
|
|
||||||
|
|
||||||
#endif
|
|
@ -110,10 +110,6 @@ typedef union tlv {
|
|||||||
|
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
#include "parser.h"
|
|
||||||
|
|
||||||
// build tlv from token
|
|
||||||
int build_tlv(tlv *tlv, struct cmd_token token);
|
|
||||||
|
|
||||||
// build specific tlv
|
// build specific tlv
|
||||||
int build_pad1(tlv *tlv);
|
int build_pad1(tlv *tlv);
|
||||||
|
BIN
src/énoncé.pdf
BIN
src/énoncé.pdf
Binary file not shown.
Loading…
Reference in New Issue
Block a user