From bda49e5e9254c402acd8f307367c64e18d486fc5 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Mon, 27 Apr 2020 20:08:30 +0200 Subject: [PATCH] work with tlvs check packet length --- src/node.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/node.c b/src/node.c index d71c883..2f959b2 100644 --- a/src/node.c +++ b/src/node.c @@ -36,16 +36,16 @@ int ask_for_peers(int socket_num) { struct in6_addr ip = peer->ip; int16_t port = peer->port; - int ifindex = if_nametoindex("enp3s0"); + /*int ifindex = if_nametoindex("enp3s0"); if(ifindex == 0) { int ifindex = if_nametoindex("eth0"); if(ifindex == 0) { perror("if_nametoindex failed"); return -1; } - } + }*/ - ifindex = 0; + int ifindex = 0; // Initialize sockaddr struct sockaddr_in6 dest; @@ -687,9 +687,15 @@ int add_message(char * message, int message_len){ return 0; } // We then look at the differents TLVs in the packet. -int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num){ +int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ + int16_t packet_len = ntohs(((packet*) data)->length); - int pos = 0; + if(packet_len != total_packet_len - 4) { + fprintf(stderr, ">> Length indicated in packet differs from real length of packet received.\n"); + return -1; + } + + int pos = 4; unsigned char tlv_len, hash[16]; char warn[32]; tlv new_tlv, cur_tlv; @@ -703,6 +709,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); +/* int ifindex = if_nametoindex("enp3s0"); if(ifindex == 0) { int ifindex = if_nametoindex("eth0"); @@ -711,6 +718,8 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, return -1; } } +*/ + int ifindex = 0; ifindex = 0; while(pos < packet_len) {