//Packet handling functions //One to handle each types of packets we receive //return 0 if handled correctly, 1 if packet is bad. int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) { uint64_t ping_id; if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { return 1; } if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. { return 1; } int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); if(len != sizeof(ping_id)) { return 1; } pingres(source, packet + 1, ping_id); pingreq(source, packet + 1);//TODO: make this smarter? return 0; }
int handle_pingreq(char * packet, uint32_t length, IP_Port source)//tested { if(length != 5 + CLIENT_ID_SIZE) { return 1; } uint32_t ping_id; memcpy(&ping_id, packet + 1, 4); IP_Port bad_ip = {{{0}}, 0}; if(is_pinging(bad_ip, ping_id))//check if packet is from ourself. { return 1; } pingres(source, ping_id); pingreq(source); return 0; }