Debugging receiving packets, validate_tlv

This commit is contained in:
n07070 2020-04-27 19:15:24 +02:00
parent 82948ef639
commit ecc168cbf4

View File

@ -45,6 +45,8 @@ int ask_for_peers(int socket_num) {
} }
} }
ifindex = 0;
// Initialize sockaddr // Initialize sockaddr
struct sockaddr_in6 dest; struct sockaddr_in6 dest;
memset(&dest, 0, sizeof(struct sockaddr_in6)); 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 // 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, 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); neighbour_peer *peer = get_neighbour(ip, port);
time_t curtime; time_t curtime;
// peer is not in the neighbour list so we try to add it if (peer == NULL) {
if(ip == NULL) { printf(">> We don't know this peer yet\n");
// check if there are less than 15 neighbours // check if there are less than 15 neighbours
if(len_list(neighbour_list) == 15) if(len_list(neighbour_list) >= 15){
return -1; 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 // set last_seen time
peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); time(&curtime);
memcpy(&peer->ip, ip, sizeof(struct in6_addr)); peer->last_seen = curtime;
peer->port = LISTEN_PORT;
peer->is_temporary = 1;
// 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); time(&curtime);
peer->last_seen = 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; 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 // 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 // Send length bytes from packet
int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { 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 // Vectorized buffer
struct iovec vec_buff = {.iov_len = length + 4, .iov_base = packet_buff}; 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 // We need to make sure the TLV announces a length that will no go onto
// another tlv, as we might end up reading bullshit. // another tlv, as we might end up reading bullshit.
int validate_tlv(char *data, int pos, int16_t packet_len){ int validate_tlv(char *data, int pos, int16_t packet_len){
char type = data[pos]; char type = data[pos];
// Nothing to do in this case // Nothing to do in this case
if(type == 0) if(type == 0){
printf(">> Found padding TLV type.\n");
return 0; return 0;
}
// Check that we can read a length // Check that we can read a length
if(pos + 1 >= packet_len) if(pos + 1 >= packet_len){
return -1; return -1;
}
unsigned char tlv_len = data[pos+1]; unsigned char tlv_len = data[pos+1];
// Check that the tlv does not exceed the packet length // Check that the tlv does not exceed the packet length
if(pos + tlv_len >= packet_len) if(pos + tlv_len >= packet_len){
return -1; return -1;
}
// Returns the type of the tlv or -1 if something went wrong // Returns the type of the tlv or -1 if something went wrong
switch(type) { switch(type) {
@ -655,6 +672,8 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
return -1; 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 ); printf("packet : %i, %i \n", packet_to_return->length + 4,received_data_len );
if (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."); 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. // 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 packet_len, struct sockaddr_in6 *sender, int socket_num){
int pos = 0; int pos = 0;
unsigned char tlv_len, hash[16]; unsigned char tlv_len, hash[16];
char warn[32]; 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) { while(pos < packet_len) {
switch(validate_tlv(data, pos, packet_len)) { switch(validate_tlv(data, pos, packet_len)) {
case 0: case 0:
@ -736,6 +757,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender,
// The position is updated // The position is updated
tlv_len = data[pos+1]; tlv_len = data[pos+1];
pos += tlv_len + 2; pos += tlv_len + 2;
printf("fzfzfe\n");
break; break;
case 4: case 4:
@ -841,6 +863,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender,
default: default:
// A malformed packet was found so we stop looking for more packets and send a warning tlv // A malformed packet was found so we stop looking for more packets and send a warning tlv
strcpy(warn, "Packet is malformed."); strcpy(warn, "Packet is malformed.");
printf("Malformed packet\n");
build_warning(&new_tlv, warn, strlen(warn)); build_warning(&new_tlv, warn, strlen(warn));
add_tlv(&pack, &new_tlv, sender, socket_num); 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. // Init the ~20s delay for node update.
srand(time(NULL)); srand(time(NULL));
time_t delay = time(NULL) + 2; time_t delay = time(NULL) + 20;
/* Descriptor zero is stdin */ /* Descriptor zero is stdin */
fds[0].fd = 0; fds[0].fd = 0;