Fixed sending messages

Fixed return types
Fixed signatures
Fixed *some* warnings
This commit is contained in:
n07070 2020-04-14 19:32:15 +02:00
parent 52aa2c653f
commit f294be3ba2
7 changed files with 70 additions and 55 deletions

View File

@ -2,20 +2,22 @@
#define HASH_H #define HASH_H
#include <openssl/sha.h> #include <openssl/sha.h>
#include "node.h"
#include "tlv.h" #include "tlv.h"
#include "parser.h"
#include "node.h"
// Hash a single data // Hash a single data
void hash_data(pub_data *data, unsigned char *buf); void hash_data(struct pub_data *data, unsigned char *buf);
// Hash every data contained in data_list then return a network hash // Hash every data contained in data_list then return a network hash
void hash_network(list *data_list, unsigned char *buf); void hash_network(struct list *data_list, unsigned char *buf);
// Truncate 32 octet hash to 16 octets // Truncate 32 octet hash to 16 octets
void hash_trunc(unsigned char *hash256bit, unsigned char *buf); void hash_trunc(unsigned char *hash256bit, unsigned char *buf);
// Concat all fields of data and put them in buf // Concat all fields of data and put them in buf
void concat_data(pub_data *data, unsigned char *buf); void concat_data(struct pub_data *data, unsigned char *buf);
// Concat hash2 to hash1 (hash1 is modified) // Concat hash2 to hash1 (hash1 is modified)
void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size); void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size);

View File

@ -8,8 +8,11 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "tlv.h"
#include "node.h" #include "node.h"
#include "tlv.h"
#include "hash.h"
#include "parser.h"
/* ---- Fonctions utilitaires ---- */ /* ---- Fonctions utilitaires ---- */
@ -160,27 +163,27 @@ void add_data(unsigned char len, long id, short seqno, char *data) {
/* ---- Fin fonctions utilitaires ---- */ /* ---- Fin fonctions utilitaires ---- */
int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int sock_num){ int send_tlv(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num){
debug_print("Building packet to send a TLV."); // debug_print("Building packet to send a TLV.");
// We first need to build the packet, // We first need to build the packet,
char packet_buff[1024]; char packet_buff[1024];
struct packet pack; struct packet pack;
pack.magic = 95; pack.magic = 95;
pack.version = 1; pack.version = 1;
if(sizeof(tlv_to_send) > 1020){ if (tlv_size > 1020) {
debug_print("Unable to send TLV, the size is bigger than 1020 bits."); perror(">> Unable to send the tlv, it's size if above 1020 bytes.");
return -1; return -1;
} else { } else {
pack.length = sizeof(tlv_to_send); memcpy((void *) pack.body, tlv_to_send, tlv_size);
strcpy(pack.body, (char) tlv_to_send);
} }
// Casting the struct to a buffer. // Move the content of the paquet struct to a buffer
packet_buff = (char *) pack; // That will be send out in a vectorized buffer.
// packet_buff = (char *) pack;
memcpy(&packet_buff,&pack,1024);
debug_print("Packet has been built."); // debug_print("Packet has been built.");
// Vectorized buffer // Vectorized buffer
struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff };
@ -197,22 +200,22 @@ int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_li
.msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
}; };
response_code = sendmsg((int) sock_num, &packet_tlv_send_out, 0); int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0);
if (response_code < 0) { if (response_code < 0) {
debug_print("Unable to send out the packet to peer %i", i); // debug_print("Unable to send out the packet to peer %i", i);
error_while_sending = 1; error_while_sending = 1;
continue; continue;
} else if (response_code < sizeof(packet_tlv_send_out)) { } else if (response_code < sizeof(packet_tlv_send_out)) {
debug_print("Sent out only part of the packet."); // debug_print("Sent out only part of the packet.");
error_while_sending = 1; error_while_sending = 1;
continue; continue;
} else { } else {
debug_print("Send out packet to peer %i", i); // debug_print("Send out packet to peer %i", i);
} }
} }
if (error_while_sending == 1) { if (error_while_sending == 1) {
debug_print("Error occured while sending out a packet."); // debug_print("Error occured while sending out a packet.");
return -1; return -1;
} else { } else {
return 0; return 0;
@ -273,9 +276,9 @@ int validate_tlv(char *data, int pos, short packet_len){
// For every packet recivied, // For every packet recivied,
// then we make sure it's conform // then we make sure it's conform
// We then extract the data from it to make it easy to work with // We then extract the data from it to make it easy to work with
int check_header(char * req[], int buffer_size, struct packet * packet_to_return){ int check_header(char * received_datagram[], int buffer_len, struct packet * packet_to_return){
packet * packet_to_return = (packet*) req; packet_to_return = (packet*) received_datagram;
// We need to check a few things ; // We need to check a few things ;
// The first byte must be worth 95, // The first byte must be worth 95,
@ -290,7 +293,7 @@ int check_header(char * req[], int buffer_size, struct packet * packet_to_return
return -1; return -1;
} }
if (packet_to_return.length + 4 > buffer_size ) { if (packet_to_return->length + 4 > buffer_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.");
return -1; return -1;
} }
@ -306,13 +309,16 @@ int update_neighbours(){
}; };
// 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, short packet_len, struct sockaddr_in6 sender){ int work_with_tlvs(char * data[], short packet_len, struct sockaddr_in6 sender){
int pos = 0; int pos = 0;
unsigned char tlv_len, hash[16], warn[32]; unsigned char tlv_len, hash[16], warn[32];
tlv new_tlv, cur_tlv; tlv new_tlv, cur_tlv;
list *tmp_list; list *tmp_list;
pub_data *pdata; pub_data *pdata;
struct neighbour_peer * random_neighbour;
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:
@ -330,11 +336,11 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
// We received a neighbour request so a random neighbor tlv has to be sent // We received a neighbour request so a random neighbor tlv has to be sent
// Send a neighbour tlv // Send a neighbour tlv
neighbour_peer *random = get_random_neighbour(); random_neighbour = get_random_neighbour();
build_neighbour(&new_tlv, random->ip, random->port); build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 3); // add_tlv(packet, &new_tlv, 3);
// The position is updated // The position is updated
tlv_len = data[pos+1]; tlv_len = data[pos+1];
@ -349,7 +355,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
build_network_hash(&new_tlv, data_list); build_network_hash(&new_tlv, data_list);
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 4); // add_tlv(packet, &new_tlv, 4);
// The position is updated // The position is updated
tlv_len = data[pos+1]; tlv_len = data[pos+1];
@ -364,7 +370,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) {
build_network_state_req(&new_tlv); build_network_state_req(&new_tlv);
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 5); // add_tlv(packet, &new_tlv, 5);
} }
// The position is updated // The position is updated
@ -382,7 +388,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
pdata = (pub_data*) tmp_list->data; pdata = (pub_data*) tmp_list->data;
build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data);
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 4); // add_tlv(packet, &new_tlv, 4);
} }
// The position is updated // The position is updated
@ -413,7 +419,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
// If no pub_data was found or the hashes differ then we send a node state request // If no pub_data was found or the hashes differ then we send a node state request
build_node_state_req(&new_tlv, cur_tlv.node_hash->node_id); build_node_state_req(&new_tlv, cur_tlv.node_hash->node_id);
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 7); // add_tlv(packet, &new_tlv, 7);
// The position is updated // The position is updated
tlv_len = data[pos+1]; tlv_len = data[pos+1];
@ -428,7 +434,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
if(pdata != NULL) { if(pdata != NULL) {
build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length);
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 8); // add_tlv(packet, &new_tlv, 8);
} }
// The position is updated // The position is updated
@ -465,7 +471,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
strcpy(warn, "Packet is malformed."); strcpy(warn, "Packet is malformed.");
build_warning(&new_tlv, warn, strlen(warn)); build_warning(&new_tlv, warn, strlen(warn));
// NOT FINISHED - What packet is it added to? // NOT FINISHED - What packet is it added to?
add_tlv(packet, &new_tlv, 9); // add_tlv(packet, &new_tlv, 9);
return -1; return -1;
} }
@ -535,7 +541,7 @@ void listen_for_packets(){
// struct tlv_list received_tlvs; // struct tlv_list received_tlvs;
// if (validate_tlvs(formated_rec_datagram) < 0) // if (validate_tlvs(formated_rec_datagram) < 0)
int nbr_success_tlv = work_with_tlvs(formated_rec_datagram, &req, sender); int nbr_success_tlv = work_with_tlvs(&req, 1024, sender);
if (nbr_success_tlv < 0){ if (nbr_success_tlv < 0){
perror(">> Error while treating the TLVs of the packet."); perror(">> Error while treating the TLVs of the packet.");
printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv ); printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv );
@ -551,7 +557,7 @@ int main(int argc, const char *argv[]) {
while(cont){ while(cont){
// We create the neighbourhood table // We create the neighbourhood table
neighbour_peer neighbour_list[NEIGHBOUR_MAX]; // neighbour_peer neighbour_list[NEIGHBOUR_MAX];
// We create the message table // We create the message table
// We create our own message. // We create our own message.

View File

@ -13,6 +13,7 @@
#include "tlv.h" #include "tlv.h"
#include "hash.h" #include "hash.h"
#include "parser.h"
// On which port do we listen to // On which port do we listen to
#define LISTEN_PORT 1212 #define LISTEN_PORT 1212
@ -65,27 +66,27 @@ static list *neighbour_list;
// fonctions signatures // fonctions signatures
void listen_for_packets(); void listen_for_packets();
int check_header(char * received_datagram[], int len, struct packet pack); int check_header(char * received_datagram[], int buffer_len, struct packet * packet_to_return);
int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l); int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l);
int update_neighbours(); int update_neighbours();
int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender); int work_with_tlvs(char * data[], short packet_len, struct sockaddr_in6 sender);
void add_tlv(packet *packet, tlv *tlv, char type); void add_tlv(struct packet *packet, union tlv *tlv, char type);
int send_packet(); // int send_packet();
/* Takes a TLV and sends it over to everyone in the list of addresses. /* Takes a TLV and sends it over to everyone in the list of addresses.
* Returns -1 in case of error, 0 otherwise. * Returns -1 in case of error, 0 otherwise.
*/ */
int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); int send_tlv(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
/* Takes a list of TLV and sends them over to everyone in the list of addresses. /* Takes a list of TLV and sends them over to everyone in the list of addresses.
* Returns -1 in case of error, 0 otherwise. * Returns -1 in case of error, 0 otherwise.
*/ */
int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); int send_tlvs(struct list * tlv_list, int tlv_size, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num);
// threaded functions // threaded functions
void t_ask_for_more_peers(); void t_ask_for_more_peers();

View File

@ -18,7 +18,12 @@ int build_tlv(tlv *tlv, cmd_token token) {
case ERROR: case ERROR:
printf("Wrong format, use 'req {neighbour | network state | node state}' or 'post {message}'"); printf("Wrong format, use 'req {neighbour | network state | node state}' or 'post {message}'");
break; break;
default:
perror("Unrecognized tlv type.");
return -1;
} }
return -1;
} }
int build_pad1(tlv *tlv) { int build_pad1(tlv *tlv) {

View File

@ -4,9 +4,10 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdlib.h> #include <stdlib.h>
#include "parser.h"
#include "hash.h"
#include "node.h" #include "node.h"
#include "hash.h"
#include "parser.h"
#define LEN_NEIGHBOUR_REQ 0 #define LEN_NEIGHBOUR_REQ 0
#define LEN_NEIGHBOUR 18 #define LEN_NEIGHBOUR 18
@ -111,14 +112,14 @@ typedef union tlv {
} tlv; } tlv;
// build tlv from token // build tlv from token
int build_tlv(tlv *tlv, cmd_token token); int build_tlv(tlv *tlv, struct cmd_token token);
// build specific tlv // build specific tlv
int build_pad1(tlv *tlv); int build_pad1(tlv *tlv);
int build_padn(tlv *tlv, size_t len); int build_padn(tlv *tlv, size_t len);
int build_neighbour_req(tlv *tlv); int build_neighbour_req(union tlv *tlv);
int build_neighbour(tlv *tlv, struct in6_addr ip, short port); int build_neighbour(tlv *tlv, struct in6_addr ip, short port);
int build_network_hash(tlv *tlv, list *data_list); int build_network_hash(tlv *tlv, struct list *data_list);
int build_network_state_req(tlv *tlv); int build_network_state_req(tlv *tlv);
int build_node_hash(tlv *tlv, long node_id, short seqno, char *data); int build_node_hash(tlv *tlv, long node_id, short seqno, char *data);
int build_node_state_req(tlv *tlv, long node_id); int build_node_state_req(tlv *tlv, long node_id);