From ecc168cbf4b81bd2111beb8d931691d163cf78d5 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 19:15:24 +0200 Subject: [PATCH] Debugging receiving packets, validate_tlv --- src/node.c | 75 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/src/node.c b/src/node.c index e5afe0f..d71c883 100644 --- a/src/node.c +++ b/src/node.c @@ -45,6 +45,8 @@ int ask_for_peers(int socket_num) { } } + ifindex = 0; + // Initialize sockaddr struct sockaddr_in6 dest; memset(&dest, 0, sizeof(struct sockaddr_in6)); @@ -125,39 +127,49 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_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) { + + char * buff_str_ip[1024]; + char * ip_str = inet_ntop(AF_INET6,ip, buff_str_ip, 1024); + + printf(">> Found peer %s:%i.\n", ip_str, ntohs(port)); + // We try to find a peer with this address and port. neighbour_peer *peer = get_neighbour(ip, port); time_t curtime; - // peer is not in the neighbour list so we try to add it - if(ip == NULL) { + if (peer == NULL) { + printf(">> We don't know this peer yet\n"); // check if there are less than 15 neighbours - if(len_list(neighbour_list) == 15) + if(len_list(neighbour_list) >= 15){ return -1; + } else { + printf(">> Adding them to the peer table.\n"); + // 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->is_temporary = 1; - // 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->is_temporary = 1; + // set last_seen time + time(&curtime); + peer->last_seen = curtime; - // set last_seen time + // set new peer as head of list + list *node = (list*) malloc(sizeof(list)); + node->data = (void*) peer; + node->next = neighbour_list; + neighbour_list = node; + return 0; + } + } else { + printf(">> Found peer %s:%i, updating the last seen time...\n", (char *) ip, port); + time_t curtime; + // if the peer was already in the list, update it time(&curtime); peer->last_seen = curtime; - // set new peer as head of list - list *node = (list*) malloc(sizeof(list)); - node->data = (void*) peer; - node->next = neighbour_list; - neighbour_list = node; - return 0; + } - - // if the peer was already in the list, update it - time(&curtime); - peer->last_seen = curtime; - - return 0; } // get data associated with id, if it doesn't exist return NULL @@ -421,7 +433,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); + ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); // Vectorized buffer struct iovec vec_buff = {.iov_len = length + 4, .iov_base = packet_buff}; @@ -587,21 +599,26 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list // We need to make sure the TLV announces a length that will no go onto // another tlv, as we might end up reading bullshit. int validate_tlv(char *data, int pos, int16_t packet_len){ + char type = data[pos]; // Nothing to do in this case - if(type == 0) + if(type == 0){ + printf(">> Found padding TLV type.\n"); return 0; + } // Check that we can read a length - if(pos + 1 >= packet_len) + if(pos + 1 >= packet_len){ return -1; + } unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length - if(pos + tlv_len >= packet_len) + if(pos + tlv_len >= packet_len){ return -1; + } // Returns the type of the tlv or -1 if something went wrong switch(type) { @@ -655,6 +672,8 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack return -1; } + // Convert to hardware order. + ((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length); printf("packet : %i, %i \n", packet_to_return->length + 4,received_data_len ); if (packet_to_return->length + 4 < received_data_len ) { perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); @@ -669,6 +688,7 @@ int add_message(char * message, int message_len){ } // 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 pos = 0; unsigned char tlv_len, hash[16]; char warn[32]; @@ -692,6 +712,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, } } + ifindex = 0; while(pos < packet_len) { switch(validate_tlv(data, pos, packet_len)) { case 0: @@ -736,6 +757,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + printf("fzfzfe\n"); break; case 4: @@ -841,6 +863,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, default: // A malformed packet was found so we stop looking for more packets and send a warning tlv strcpy(warn, "Packet is malformed."); + printf("Malformed packet\n"); build_warning(&new_tlv, warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -914,7 +937,7 @@ int run_node(int sock_fd){ // Init the ~20s delay for node update. srand(time(NULL)); - time_t delay = time(NULL) + 2; + time_t delay = time(NULL) + 20; /* Descriptor zero is stdin */ fds[0].fd = 0;