Merge branch 'fix-get-peer' into 'master'

Fix get peer

See merge request perdriau/dazibao!18
This commit is contained in:
PERDRIAU nelson 2020-05-05 14:58:41 +02:00
commit 0487681fc7
2 changed files with 67 additions and 67 deletions

View File

@ -96,7 +96,7 @@ neighbour_peer *get_random_neighbour() {
}
// Search for this peer in the neighbour table
neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
neighbour_peer *get_neighbour(struct in6_addr *ip, uint16_t port) {
print_debug(">> Getting neighbour.");
if (DEBUG_LEVEL > 1) {
@ -114,7 +114,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
peer = (neighbour_peer*) tmp->data;
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0
&& memcmp(&peer->port, &port, sizeof(int16_t)) == 0) {
&& memcmp(&peer->port, &port, sizeof(uint16_t)) == 0) {
return peer;
}
@ -128,7 +128,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
// Return -1 if we have enough peers,
// 1 if it was added
// Return 0 if peer was updated as last_seen
int add_n_update_neighbour(struct in6_addr *ip, int16_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.
@ -141,11 +141,18 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
if(len_list(neighbour_list) >= 15){
return -1;
} else {
print_debug(">> Adding them to the peer table.\n");
if (DEBUG_LEVEL > 0) {
if (DEBUG_LEVEL > 1) {
char * buff_str_ip[1024];
char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024);
printf("\x1b[31m[DEBUG]\x1b[0m >> Adding peer %s @ %i to the peer table.\n", ip_str, port );
sleep(3);
}
}
// if there are less, initialize the new peer to add to the list
peer = (neighbour_peer*) malloc(sizeof(neighbour_peer));
memcpy(&peer->ip, ip, sizeof(struct in6_addr));
peer->port = LISTEN_PORT;
peer->port = port;
peer->is_temporary = 1;
// set last_seen time
@ -255,7 +262,9 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
found->data = (char*) malloc(len);
memcpy(found->data, data, len);
printf(">> Updated %li's published data.\n", id);
if (DEBUG_LEVEL > 0) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Updated %li's published data.\n", id);
}
return 1;
}
@ -281,7 +290,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
data_list->data = (void*) new_data;
data_list->next = tmp;
printf(">> Added new message to data list.\n");
print_debug(">> Added new message to data list.");
return 1;
}
@ -292,7 +301,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
new_node->next = tmp;
last->next = new_node;
printf(">> Added new message to data list.\n");
print_debug(">> Added new message to data list.");
return 1;
}
@ -308,7 +317,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
new_node->next = NULL;
last->next = new_node;
printf(">> Added new message to data list.\n");
print_debug(">> Added new message to data list.");
return 1;
}
@ -1029,7 +1038,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
add_tlv(&pack, &new_tlv, sender, socket_num);
tmp_list = tmp_list->next;
printf("%lu\n", *(uint64_t*) (pack.body + 2));
if (DEBUG_LEVEL > 1) {
// TODO wtf is this ?
printf(" >> \x1b[31m[DEBUG]\x1b[0m %lu\n", *(uint64_t*) (pack.body + 2));
}
}
// The position is updated
@ -1114,21 +1127,21 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
cur_message = data + pos + 28;
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");
// 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
pub_data pdata_check = (pub_data) {
@ -1142,22 +1155,22 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
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");
// 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) {
print_debug(">> Malformed hash.");
@ -1184,17 +1197,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
free(pdata_check.data);
/*
if (DEBUG_LEVEL > 0) {
if (cur_message == NULL) {
print_error("The data in the current node is NULL !");
return -1;
}
printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0\n", (const wchar_t*) cur_message);
sleep(1);
}
*/
// Compare hashes
pdata = get_data(be64toh(*id));
@ -1257,7 +1259,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
if(pack.length > 0){
send_packet((char*) &pack, pack.length, sender, socket_num);
}
print_data(data_list);
// print_data(data_list);
return 0;
}
@ -1276,11 +1278,10 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
struct in6_addr ip = sender->sin6_addr;
int16_t port = htobe16(sender->sin6_port);
int rc = add_n_update_neighbour(&ip, port);
if( rc == -1) {
print_debug(">> We have enough peers, we won't add him..");
return -1;
return -3;
} else if (rc == 1){
print_debug(">> Peer was added to the table.\a");
} else {
@ -1403,7 +1404,6 @@ int run_node(int sock_fd){
// }
// printf("\n");
/* Call poll() */
ret = poll(fds, 2, 10);
@ -1472,7 +1472,9 @@ int run_node(int sock_fd){
}
// Treat incoming packets.
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
if (work_tlv_status < 0) {
if (work_tlv_status == -3) {
print_debug(">> Received paquet from peer not in peer list, and we have enough peers, ignoring...");
} else if (work_tlv_status < 0) {
print_error("Error while treating the incoming packet.");
}
// Reset buffer
@ -1527,7 +1529,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){
return -3;
}
root_peer->port = (int16_t) root_peer_port;
root_peer->port = root_peer_port;
root_peer->is_temporary = 0;
root_peer->last_seen = root_peer_seen;
@ -1567,9 +1569,7 @@ int main(int argc, const char *argv[]) {
if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == argv[2] || *end != '\0'){
print_error("Conversion of port number failed, please choose a smaller number.");
}
uint16_t root_peer_port = (uint16_t) val;
char * root_peer_ip = (char *) argv[1];
setlocale(LC_ALL, "");

View File

@ -27,7 +27,7 @@
*/
typedef struct neighbour_peer {
struct in6_addr ip;
int16_t port;
uint16_t port;
char is_temporary;
time_t last_seen;
} neighbour_peer;
@ -61,9 +61,9 @@ typedef struct list {
#define LISTEN_PORT 1212
// The node ID
#define NODE_ID 42009235719890846928
// #define NODE_ID 42675882021843277
// #define NODE_ID 13809235719890846928
#define NODE_ID 1312
// #define NODE_ID 1312
// The number of neighbours
// The neighbour table has 15 entries
@ -133,10 +133,10 @@ 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);
neighbour_peer *get_neighbour(struct in6_addr *ip, uint16_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);
int add_n_update_neighbour(struct in6_addr *ip, uint16_t port);
// get data associated with id, if it doesn't exist return NULL
pub_data *get_data(uint64_t id);