Changed debug print,

Added memcmp in get_neibourgh
Found bug with port number conversion
This commit is contained in:
n07070 2020-05-04 12:59:08 +02:00
parent e757389e32
commit e1364bc58e
2 changed files with 23 additions and 18 deletions

View File

@ -55,7 +55,7 @@ void print_data(list * l){
print_debug(">> Printing messages we know of so far :"); print_debug(">> Printing messages we know of so far :");
int nbr_msg = 0; int nbr_msg = 0;
setlocale(LC_ALL, "en_US.utf8"); setlocale(LC_ALL, "en_US.utf8");
print_debug(">> Peer ID | Seqno | Length | Message "); print_debug(">> Peer ID | Seqno | Length | Message ");
while(l != NULL){ while(l != NULL){
pub_data * message = l->data; pub_data * message = l->data;
printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data);

View File

@ -113,7 +113,8 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
// check for same ip and same port // check for same ip and same port
peer = (neighbour_peer*) tmp->data; peer = (neighbour_peer*) tmp->data;
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) { if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0
&& memcmp(&peer->port, &port, sizeof(int16_t)) == 0) {
return peer; return peer;
} }
@ -151,10 +152,12 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
time(&curtime); time(&curtime);
peer->last_seen = curtime; peer->last_seen = curtime;
list * tmp = neighbour_list;
// set new peer as head of list // set new peer as head of list
list *node = (list*) malloc(sizeof(list)); list *node = (list*) malloc(sizeof(list));
node->data = (void*) peer; node->data = (void*) peer;
node->next = neighbour_list; node->next = tmp;
neighbour_list = node; neighbour_list = node;
return 1; return 1;
} }
@ -397,7 +400,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
pack->magic = 95; pack->magic = 95;
pack->version = 1; pack->version = 1;
pack->length = 0; pack->length = 0;
memset(pack->body, 0, 1020); memset(pack->body, 0, 1020);
sent = 1; sent = 1;
@ -407,7 +410,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
// we just ignore that padding, and send out the packet. // we just ignore that padding, and send out the packet.
if(pack->length >= 1020) { if(pack->length >= 1020) {
errval = send_packet((char*) pack, pack->length, dest, socket_num); errval = send_packet((char*) pack, pack->length, dest, socket_num);
pack->magic = 95; pack->magic = 95;
pack->version = 1; pack->version = 1;
pack->length = 0; pack->length = 0;
@ -540,7 +543,9 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
if (error_while_sending == 1) { if (error_while_sending == 1) {
return -1; return -1;
} else { } else {
printf(">> Packet successfully sent to peer. %d bytes sent.\n", response_code); if (DEBUG_LEVEL > 0) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Packet successfully sent to peer. %d bytes sent.\n", response_code);
}
return 0; return 0;
} }
} }
@ -760,9 +765,9 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){
uint64_t *id = (uint64_t*) (data + pos + 2); uint64_t *id = (uint64_t*) (data + pos + 2);
pub_data pdata = (pub_data) { pub_data pdata = (pub_data) {
.length = tlv_len, .length = tlv_len,
.id = *id, .id = *id,
.seqno = *seqno, .seqno = *seqno,
.data = (unsigned char*) malloc(tlv_len) .data = (unsigned char*) malloc(tlv_len)
}; };
@ -772,7 +777,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){
unsigned char hash[16]; unsigned char hash[16];
memcpy(pdata.data, cur_message, tlv_len); memcpy(pdata.data, cur_message, tlv_len);
hash_data(&pdata, hash); hash_data(&pdata, hash);
if(memcmp(hash, cur_hash, 16) != 0) { if(memcmp(hash, cur_hash, 16) != 0) {
@ -783,7 +788,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){
printf("%02x", hash[x]); printf("%02x", hash[x]);
fflush(0); fflush(0);
} }
printf("\n"); printf("\n");
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
@ -1127,14 +1132,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
// 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) {
.length = tlv_len - 26, .length = tlv_len - 26,
.id = be64toh(*id), .id = be64toh(*id),
.seqno = be16toh(*seqno), .seqno = be16toh(*seqno),
.data = (char*) malloc(tlv_len) .data = (char*) malloc(tlv_len)
}; };
memcpy(pdata_check.data, cur_message, tlv_len - 26); memcpy(pdata_check.data, cur_message, tlv_len - 26);
hash_data(&pdata_check, hash2); hash_data(&pdata_check, hash2);
print_debug(">> Built message: "); print_debug(">> Built message: ");
@ -1162,7 +1167,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
printf("%02x", hash2[x]); printf("%02x", hash2[x]);
fflush(0); fflush(0);
} }
printf("\n"); printf("\n");
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
@ -1468,7 +1473,7 @@ 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 < 0) {
print_debug(">> Error while treating the incoming packet."); print_error("Error while treating the incoming packet.");
} }
// Reset buffer // Reset buffer
memset(output_buffer, 0, sizeof(output_buffer)); memset(output_buffer, 0, sizeof(output_buffer));
@ -1522,7 +1527,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){
return -3; return -3;
} }
root_peer->port = root_peer_port; root_peer->port = (int16_t) 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;