Merge branch 'fix-get-peer' into 'master'
Fix get peer See merge request perdriau/dazibao!18
This commit is contained in:
commit
0487681fc7
120
src/node.c
120
src/node.c
@ -96,7 +96,7 @@ neighbour_peer *get_random_neighbour() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Search for this peer in the neighbour table
|
// 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.");
|
print_debug(">> Getting neighbour.");
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 1) {
|
if (DEBUG_LEVEL > 1) {
|
||||||
@ -114,7 +114,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
|
|||||||
peer = (neighbour_peer*) tmp->data;
|
peer = (neighbour_peer*) tmp->data;
|
||||||
|
|
||||||
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0
|
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;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
|
|||||||
// Return -1 if we have enough peers,
|
// Return -1 if we have enough peers,
|
||||||
// 1 if it was added
|
// 1 if it was added
|
||||||
// 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, 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.
|
// 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){
|
if(len_list(neighbour_list) >= 15){
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} 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
|
// if there are less, initialize the new peer to add to the list
|
||||||
peer = (neighbour_peer*) malloc(sizeof(neighbour_peer));
|
peer = (neighbour_peer*) malloc(sizeof(neighbour_peer));
|
||||||
memcpy(&peer->ip, ip, sizeof(struct in6_addr));
|
memcpy(&peer->ip, ip, sizeof(struct in6_addr));
|
||||||
peer->port = LISTEN_PORT;
|
peer->port = port;
|
||||||
peer->is_temporary = 1;
|
peer->is_temporary = 1;
|
||||||
|
|
||||||
// set last_seen time
|
// 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);
|
found->data = (char*) malloc(len);
|
||||||
memcpy(found->data, data, 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;
|
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->data = (void*) new_data;
|
||||||
data_list->next = tmp;
|
data_list->next = tmp;
|
||||||
|
|
||||||
printf(">> Added new message to data list.\n");
|
print_debug(">> Added new message to data list.");
|
||||||
|
|
||||||
return 1;
|
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;
|
new_node->next = tmp;
|
||||||
last->next = new_node;
|
last->next = new_node;
|
||||||
|
|
||||||
printf(">> Added new message to data list.\n");
|
print_debug(">> Added new message to data list.");
|
||||||
|
|
||||||
return 1;
|
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;
|
new_node->next = NULL;
|
||||||
last->next = new_node;
|
last->next = new_node;
|
||||||
|
|
||||||
printf(">> Added new message to data list.\n");
|
print_debug(">> Added new message to data list.");
|
||||||
|
|
||||||
return 1;
|
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);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
tmp_list = tmp_list->next;
|
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
|
// 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;
|
cur_message = data + pos + 28;
|
||||||
|
|
||||||
print_debug(">> Received message ! ");
|
print_debug(">> Received message ! ");
|
||||||
printf("Type : %d\n", data[pos] );
|
// printf("Type : %d\n", data[pos] );
|
||||||
printf("Length : %d\n", tlv_len );
|
// printf("Length : %d\n", tlv_len );
|
||||||
printf("Node ID : %lu\n", be64toh(*id) );
|
// printf("Node ID : %lu\n", be64toh(*id) );
|
||||||
printf("Seqno : %hu\n", be16toh(*seqno) );
|
// printf("Seqno : %hu\n", be16toh(*seqno) );
|
||||||
printf("Hash :");
|
// printf("Hash :");
|
||||||
for(int x = 0; x < 16; x++){
|
// for(int x = 0; x < 16; x++){
|
||||||
printf("%02x", cur_hash[x]);
|
// printf("%02x", cur_hash[x]);
|
||||||
fflush(0);
|
// fflush(0);
|
||||||
}
|
// }
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
for(int x = 0; x < tlv_len - 26; x++){
|
// for(int x = 0; x < tlv_len - 26; x++){
|
||||||
printf("%c", cur_message[x]);
|
// printf("%c", cur_message[x]);
|
||||||
fflush(0);
|
// fflush(0);
|
||||||
}
|
// }
|
||||||
printf("\n");
|
// 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) {
|
||||||
@ -1142,22 +1155,22 @@ 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: ");
|
// print_debug(">> Built message: ");
|
||||||
printf("Type : %d\n", data[pos] );
|
// printf("Type : %d\n", data[pos] );
|
||||||
printf("Length : %d\n", pdata_check.length + 26);
|
// printf("Length : %d\n", pdata_check.length + 26);
|
||||||
printf("Node ID : %lu\n", pdata_check.id);
|
// printf("Node ID : %lu\n", pdata_check.id);
|
||||||
printf("Seqno : %hu\n", pdata_check.seqno);
|
// printf("Seqno : %hu\n", pdata_check.seqno);
|
||||||
printf("Hash :");
|
// printf("Hash :");
|
||||||
for(int x = 0; x < 16; x++){
|
// for(int x = 0; x < 16; x++){
|
||||||
printf("%02x", hash2[x]);
|
// printf("%02x", hash2[x]);
|
||||||
fflush(0);
|
// fflush(0);
|
||||||
}
|
// }
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
for(int x = 0; x < tlv_len - 26; x++){
|
// for(int x = 0; x < tlv_len - 26; x++){
|
||||||
printf("%c", pdata_check.data[x]);
|
// printf("%c", pdata_check.data[x]);
|
||||||
fflush(0);
|
// fflush(0);
|
||||||
}
|
// }
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
|
|
||||||
if(memcmp(hash2, cur_hash, 16) != 0) {
|
if(memcmp(hash2, cur_hash, 16) != 0) {
|
||||||
print_debug(">> Malformed hash.");
|
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);
|
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
|
// Compare hashes
|
||||||
pdata = get_data(be64toh(*id));
|
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){
|
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);
|
// print_data(data_list);
|
||||||
|
|
||||||
return 0;
|
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;
|
struct in6_addr ip = sender->sin6_addr;
|
||||||
int16_t port = htobe16(sender->sin6_port);
|
int16_t port = htobe16(sender->sin6_port);
|
||||||
|
|
||||||
|
|
||||||
int rc = add_n_update_neighbour(&ip, port);
|
int rc = add_n_update_neighbour(&ip, port);
|
||||||
if( rc == -1) {
|
if( rc == -1) {
|
||||||
print_debug(">> We have enough peers, we won't add him..");
|
print_debug(">> We have enough peers, we won't add him..");
|
||||||
return -1;
|
return -3;
|
||||||
} else if (rc == 1){
|
} else if (rc == 1){
|
||||||
print_debug(">> Peer was added to the table.\a");
|
print_debug(">> Peer was added to the table.\a");
|
||||||
} else {
|
} else {
|
||||||
@ -1403,7 +1404,6 @@ int run_node(int sock_fd){
|
|||||||
// }
|
// }
|
||||||
// printf("\n");
|
// printf("\n");
|
||||||
|
|
||||||
|
|
||||||
/* Call poll() */
|
/* Call poll() */
|
||||||
ret = poll(fds, 2, 10);
|
ret = poll(fds, 2, 10);
|
||||||
|
|
||||||
@ -1472,7 +1472,9 @@ int run_node(int sock_fd){
|
|||||||
}
|
}
|
||||||
// Treat incoming packets.
|
// Treat incoming packets.
|
||||||
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
|
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.");
|
print_error("Error while treating the incoming packet.");
|
||||||
}
|
}
|
||||||
// Reset buffer
|
// Reset buffer
|
||||||
@ -1527,7 +1529,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){
|
|||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
root_peer->port = (int16_t) root_peer_port;
|
root_peer->port = root_peer_port;
|
||||||
root_peer->is_temporary = 0;
|
root_peer->is_temporary = 0;
|
||||||
root_peer->last_seen = root_peer_seen;
|
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'){
|
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.");
|
print_error("Conversion of port number failed, please choose a smaller number.");
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t root_peer_port = (uint16_t) val;
|
uint16_t root_peer_port = (uint16_t) val;
|
||||||
|
|
||||||
char * root_peer_ip = (char *) argv[1];
|
char * root_peer_ip = (char *) argv[1];
|
||||||
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
|
10
src/node.h
10
src/node.h
@ -27,7 +27,7 @@
|
|||||||
*/
|
*/
|
||||||
typedef struct neighbour_peer {
|
typedef struct neighbour_peer {
|
||||||
struct in6_addr ip;
|
struct in6_addr ip;
|
||||||
int16_t port;
|
uint16_t port;
|
||||||
char is_temporary;
|
char is_temporary;
|
||||||
time_t last_seen;
|
time_t last_seen;
|
||||||
} neighbour_peer;
|
} neighbour_peer;
|
||||||
@ -61,9 +61,9 @@ typedef struct list {
|
|||||||
#define LISTEN_PORT 1212
|
#define LISTEN_PORT 1212
|
||||||
|
|
||||||
// The node ID
|
// The node ID
|
||||||
|
#define NODE_ID 42009235719890846928
|
||||||
// #define NODE_ID 42675882021843277
|
// #define NODE_ID 42675882021843277
|
||||||
// #define NODE_ID 13809235719890846928
|
// #define NODE_ID 1312
|
||||||
#define NODE_ID 1312
|
|
||||||
|
|
||||||
// The number of neighbours
|
// The number of neighbours
|
||||||
// The neighbour table has 15 entries
|
// The neighbour table has 15 entries
|
||||||
@ -133,10 +133,10 @@ int len_list(list *l);
|
|||||||
neighbour_peer *get_random_neighbour();
|
neighbour_peer *get_random_neighbour();
|
||||||
|
|
||||||
// Search for this peer in the neighbour table
|
// 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
|
// 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
|
// get data associated with id, if it doesn't exist return NULL
|
||||||
pub_data *get_data(uint64_t id);
|
pub_data *get_data(uint64_t id);
|
||||||
|
Loading…
Reference in New Issue
Block a user