Added new print_error function
Added checks for NULL values Added comments and quesitons as // TODO
This commit is contained in:
parent
055ecfd0ba
commit
ba0cf32cb0
@ -9,3 +9,10 @@ void print_debug(char * msg){
|
|||||||
getchar();
|
getchar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_error(char * msg){
|
||||||
|
printf("\x1b[41m[97m[ERROR] %s \x1b[0m\n", msg);
|
||||||
|
if (DEBUG_LEVEL > 1) {
|
||||||
|
getchar();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,4 +5,5 @@
|
|||||||
|
|
||||||
void print_debug(char * msg);
|
void print_debug(char * msg);
|
||||||
|
|
||||||
|
void print_error(char * msg);
|
||||||
#endif
|
#endif
|
||||||
|
25
src/hash.c
25
src/hash.c
@ -1,4 +1,5 @@
|
|||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
// Hash a single data
|
// Hash a single data
|
||||||
void hash_data(pub_data *data, unsigned char *buf) {
|
void hash_data(pub_data *data, unsigned char *buf) {
|
||||||
@ -34,7 +35,9 @@ void hash_network(list *data_list, unsigned char *buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Hash all of concat to obtain the network hash
|
// Hash all of concat to obtain the network hash
|
||||||
SHA256(concat, totlen, hash);
|
if (SHA256(concat, totlen, hash) == NULL) {
|
||||||
|
print_debug(">> Doing the hash failed : function return NULL, should return a pointer.");
|
||||||
|
};
|
||||||
|
|
||||||
// Put truncated hash into buf
|
// Put truncated hash into buf
|
||||||
hash_trunc(hash, buf);
|
hash_trunc(hash, buf);
|
||||||
@ -46,17 +49,27 @@ void hash_network(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 *hash32oct, unsigned char *buf) {
|
void hash_trunc(unsigned char *hash32oct, unsigned char *buf) {
|
||||||
// Copy the first 16 octets from hash32oct
|
// Copy the first 16 octets from hash32oct
|
||||||
memcpy(buf, hash32oct, 16);
|
if (memcpy(buf, hash32oct, 16) == NULL) {
|
||||||
|
print_debug(">> Truncating the hashs didn't work !");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(pub_data *data, unsigned char *buf) {
|
||||||
memcpy(buf, &(data->id), 8);
|
if (memcpy(buf, &(data->id), 8) == NULL) {
|
||||||
memcpy(buf+8, &(data->seqno), 2);
|
print_debug(">> Concat the data (id) didn't work !");
|
||||||
memcpy(buf+10, data->data, data->length);
|
};
|
||||||
|
if (memcpy(buf+8, &(data->seqno), 2) == NULL) {
|
||||||
|
print_debug(">> concat the data (seqno) didn't work !");
|
||||||
|
}
|
||||||
|
if (memcpy(buf+10, data->data, data->length) == NULL) {
|
||||||
|
print_debug(">> Contact the data (data) didn't work !");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
memcpy(hash1+size, hash2, 16);
|
if(memcpy(hash1+size, hash2, 16) == NULL){
|
||||||
|
print_debug(">> Concat the hash didn't work !");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
91
src/node.c
91
src/node.c
@ -204,11 +204,19 @@ pub_data *get_data(int64_t id) {
|
|||||||
pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
||||||
pub_data *new_data = (pub_data*) malloc(sizeof(pub_data));
|
pub_data *new_data = (pub_data*) malloc(sizeof(pub_data));
|
||||||
char *_data = (char*) malloc(len);
|
char *_data = (char*) malloc(len);
|
||||||
|
if (_data == NULL) {
|
||||||
|
print_error("Failed to allocate memory for copying the data !");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
new_data->length = len;
|
new_data->length = len;
|
||||||
new_data->id = id;
|
new_data->id = id;
|
||||||
new_data->seqno = seqno;
|
new_data->seqno = seqno;
|
||||||
new_data->data = _data;
|
new_data->data = _data;
|
||||||
memcpy(_data, data, len);
|
if(memcpy(_data, data, len) == NULL){
|
||||||
|
print_error("Failed to copy data !");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return new_data;
|
return new_data;
|
||||||
}
|
}
|
||||||
@ -221,6 +229,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
if(id == NODE_ID) {
|
if(id == NODE_ID) {
|
||||||
// We create our pub_data.
|
// We create our pub_data.
|
||||||
pub_data * message = malloc(sizeof(struct pub_data));
|
pub_data * message = malloc(sizeof(struct pub_data));
|
||||||
|
if (message == NULL) {
|
||||||
|
print_error("Failed to allocate memory for the message !");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
message->length = len;
|
message->length = len;
|
||||||
message->id = id;
|
message->id = id;
|
||||||
message->seqno = seqno;
|
message->seqno = seqno;
|
||||||
@ -229,7 +242,10 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
// If the data list has never been used, or is empty ( same thing )
|
// If the data list has never been used, or is empty ( same thing )
|
||||||
if (data_list == NULL) {
|
if (data_list == NULL) {
|
||||||
data_list = (list*) malloc(sizeof(struct list));
|
data_list = (list*) malloc(sizeof(struct list));
|
||||||
|
if (data_list == NULL) {
|
||||||
|
print_error("Failed to allocate memory to create the data_list !");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
list *tmp = data_list;
|
list *tmp = data_list;
|
||||||
// We create the next node of the linked list.
|
// We create the next node of the linked list.
|
||||||
tmp->data = (void *) message;
|
tmp->data = (void *) message;
|
||||||
@ -247,19 +263,33 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
|
|
||||||
// We create the next node of the linked list.
|
// We create the next node of the linked list.
|
||||||
list * new_node = malloc(sizeof(struct list));
|
list * new_node = malloc(sizeof(struct list));
|
||||||
|
if (new_node == NULL) {
|
||||||
|
print_error("Failed to allocate memory for the new node in data list.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
new_node->data = (void *) message;
|
new_node->data = (void *) message;
|
||||||
|
new_node->next = NULL;
|
||||||
|
|
||||||
// Adding the message to the list.
|
// Adding the message to the list.
|
||||||
tmp->next = (void *) new_node;
|
tmp->next = (void *) new_node;
|
||||||
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
// Copy data
|
// Copy data
|
||||||
pub_data *new_data = copy_data(len, id, seqno, data);
|
pub_data *new_data = copy_data(len, id, seqno, data);
|
||||||
|
if (new_data == NULL) {
|
||||||
|
print_error("Failed to copy data to new_data !");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if(data_list == NULL) {
|
if(data_list == NULL) {
|
||||||
// Update list
|
// Update list
|
||||||
data_list = (list*) malloc(sizeof(list));
|
data_list = (list*) malloc(sizeof(list));
|
||||||
|
if (data_list == NULL) {
|
||||||
|
print_error("Failed to allocate memory to create the data_list !");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
data_list->data = (void*) new_data;
|
data_list->data = (void*) new_data;
|
||||||
data_list->next = NULL;
|
data_list->next = NULL;
|
||||||
|
|
||||||
@ -280,7 +310,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
// If last hasn't been set then the new data becomes the head of the list
|
// If last hasn't been set then the new data becomes the head of the list
|
||||||
if(last == NULL) {
|
if(last == NULL) {
|
||||||
// Update list
|
// Update list
|
||||||
data_list = (list*) malloc(sizeof(list));
|
data_list = (list*) malloc(sizeof(struct list));
|
||||||
|
if (data_list == NULL) {
|
||||||
|
print_error("Failed to allocate memory to create the data_list !");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
data_list->data = (void*) new_data;
|
data_list->data = (void*) new_data;
|
||||||
data_list->next = tmp;
|
data_list->next = tmp;
|
||||||
|
|
||||||
@ -288,7 +322,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Else, we update the last node
|
// Else, we update the last node
|
||||||
new_node = (list*) malloc(sizeof(list));
|
new_node = (list*) malloc(sizeof(struct list));
|
||||||
|
if (new_node == NULL) {
|
||||||
|
print_error("Failed to allocate memory for the new node in data list.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
new_node->data = (void*) new_data;
|
new_node->data = (void*) new_data;
|
||||||
new_node->next = tmp;
|
new_node->next = tmp;
|
||||||
last->next = new_node;
|
last->next = new_node;
|
||||||
@ -323,6 +361,10 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
|
|
||||||
// Update list
|
// Update list
|
||||||
new_node = (list*) malloc(sizeof(list));
|
new_node = (list*) malloc(sizeof(list));
|
||||||
|
if (new_node == NULL) {
|
||||||
|
print_error("Failed to allocate memory for the new node in data list.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
new_node->data = (void*) new_data;
|
new_node->data = (void*) new_data;
|
||||||
new_node->next = NULL;
|
new_node->next = NULL;
|
||||||
last->next = new_node;
|
last->next = new_node;
|
||||||
@ -419,6 +461,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
sent = 1;
|
sent = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// In case we need to add a padding packet, and the packet is full,
|
||||||
|
// we just ignore that padding, and send out the packet.
|
||||||
if(pack->length >= 1020) {
|
if(pack->length >= 1020) {
|
||||||
errval = send_packet((char*) pack, pack->length, dest, socket_num);
|
errval = send_packet((char*) pack, pack->length, dest, socket_num);
|
||||||
*pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
*pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
||||||
@ -480,9 +524,10 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
|
|
||||||
print_debug(">> Finished adding the TLVs to the packet");
|
print_debug(">> Finished adding the TLVs to the packet");
|
||||||
|
|
||||||
// If the previous packet was went return 1 or -1 if there was an error sending it
|
// If the previous packet was sent return 1 or -1 if there was an error sending it
|
||||||
if(sent)
|
if(sent){
|
||||||
return errval? -1:1;
|
return errval? -1:1;
|
||||||
|
}
|
||||||
|
|
||||||
// Return 0 if the TLV was added to the packet
|
// Return 0 if the TLV was added to the packet
|
||||||
return 0;
|
return 0;
|
||||||
@ -774,14 +819,23 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
int pos = 4;
|
int pos = 4;
|
||||||
unsigned char tlv_len, hash[16];
|
unsigned char tlv_len, hash[16];
|
||||||
char warn[32];
|
char warn[32];
|
||||||
tlv new_tlv, cur_tlv;
|
|
||||||
|
// The TLV we are going to send back.
|
||||||
|
tlv new_tlv;
|
||||||
|
// The tlv we are currently looking at.
|
||||||
|
tlv cur_tlv;
|
||||||
|
|
||||||
new_tlv.pad1 = NULL;
|
new_tlv.pad1 = NULL;
|
||||||
cur_tlv.pad1 = NULL;
|
cur_tlv.pad1 = NULL;
|
||||||
list *tmp_list;
|
list *tmp_list;
|
||||||
pub_data *pdata;
|
pub_data *pdata;
|
||||||
|
|
||||||
|
// holds the random neighbour we send back in case of a neighbour_req.
|
||||||
struct neighbour_peer *random_neighbour;
|
struct neighbour_peer *random_neighbour;
|
||||||
|
// Holds the new neighbour send to us by a "neighbour tlv"
|
||||||
struct sockaddr_in6 new_neighbour;
|
struct sockaddr_in6 new_neighbour;
|
||||||
|
|
||||||
|
// We create the packet in which we are going to send back our responses.
|
||||||
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
||||||
memset(pack.body, 0, 1020);
|
memset(pack.body, 0, 1020);
|
||||||
|
|
||||||
@ -797,8 +851,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
*/
|
*/
|
||||||
int ifindex = 0;
|
int ifindex = 0;
|
||||||
|
|
||||||
ifindex = 0;
|
|
||||||
while(pos < total_packet_len) {
|
while(pos < total_packet_len) {
|
||||||
|
// Making sure the current TLV we are looking at is valid.
|
||||||
switch(validate_tlv(data, pos, total_packet_len)) {
|
switch(validate_tlv(data, pos, total_packet_len)) {
|
||||||
case 0:
|
case 0:
|
||||||
// We received a padding tlv so it is ignored
|
// We received a padding tlv so it is ignored
|
||||||
@ -818,7 +872,15 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
print_debug(">> Received neighbour request, sending out a neighbour address.");
|
print_debug(">> Received neighbour request, sending out a neighbour address.");
|
||||||
// Send a neighbour tlv
|
// Send a neighbour tlv
|
||||||
random_neighbour = get_random_neighbour();
|
random_neighbour = get_random_neighbour();
|
||||||
|
if (random_neighbour == NULL) {
|
||||||
|
print_debug(">> Failed to get a random neighbour, failing...");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : Seems to be a bug here, as this frees the new_tlv
|
||||||
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
||||||
|
// TODO : I suppose that since new_tlv is freed above, add_tlv ads an empty
|
||||||
|
// value to the packet ?
|
||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
@ -938,7 +1000,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
// so a node state tlv for this node id has to be sent,
|
// so a node state tlv for this node id has to be sent,
|
||||||
// if no pub_data exists for this id nothing is sent
|
// if no pub_data exists for this id nothing is sent
|
||||||
print_debug(">> Received node state request. Processing...");
|
print_debug(">> Received node state request. Processing...");
|
||||||
cur_tlv.node_state_req = (node_state_req*) (data + pos);
|
cur_tlv.node_state_req = (node_state_req*) (data[pos]);
|
||||||
pdata = get_data(ntohl(cur_tlv.node_state_req->node_id));
|
pdata = get_data(ntohl(cur_tlv.node_state_req->node_id));
|
||||||
|
|
||||||
if(pdata != NULL) {
|
if(pdata != NULL) {
|
||||||
@ -960,11 +1022,15 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
print_debug(">> Received message ! ");
|
print_debug(">> Received message ! ");
|
||||||
if (DEBUG_LEVEL > 0) {
|
if (DEBUG_LEVEL > 0) {
|
||||||
printf("\n\t %s \n", (char *) cur_tlv.node_state->data);
|
if (cur_tlv.node_state->data == NULL) {
|
||||||
|
print_error("The data in the current node is NULL !");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
printf("\x1b[31m[DEBUG]\x1b[0m >> “%s”\n", cur_tlv.node_state->data);
|
||||||
}
|
}
|
||||||
int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data);
|
int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
print_debug(">> Error while adding note state !");
|
print_debug(">> Error while adding node state !");
|
||||||
}
|
}
|
||||||
// The position is updated
|
// The position is updated
|
||||||
tlv_len = data[pos+1];
|
tlv_len = data[pos+1];
|
||||||
@ -1172,6 +1238,9 @@ int run_node(int sock_fd){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// TODO : Here, we can write all of the current messages we have in stack
|
||||||
|
// to a file, or to stdout.
|
||||||
|
// TODO : Same as above, but for the peers have know about.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/tlv.c
10
src/tlv.c
@ -1,5 +1,6 @@
|
|||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
|
|
||||||
|
// TODO this can be deleted ?
|
||||||
// creer un tlv
|
// creer un tlv
|
||||||
int build_tlv(tlv *tlv, cmd_token token) {
|
int build_tlv(tlv *tlv, cmd_token token) {
|
||||||
switch(token.type) {
|
switch(token.type) {
|
||||||
@ -25,6 +26,7 @@ int build_tlv(tlv *tlv, cmd_token token) {
|
|||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
// end deletion
|
||||||
|
|
||||||
int build_pad1(tlv *tlv) {
|
int build_pad1(tlv *tlv) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
@ -62,9 +64,7 @@ int build_padn(tlv *tlv, size_t len) {
|
|||||||
|
|
||||||
int build_neighbour_req(tlv *tlv) {
|
int build_neighbour_req(tlv *tlv) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
// if (tlv != NULL) {
|
free(tlv->pad1);
|
||||||
// free(tlv->pad1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req));
|
neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req));
|
||||||
|
|
||||||
@ -82,6 +82,10 @@ int build_neighbour_req(tlv *tlv) {
|
|||||||
|
|
||||||
int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) {
|
int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
|
// TODO : why do we free this part ? Doesn't it mean that, once the
|
||||||
|
// tlv has entered this function, it's not on the heap anymore,
|
||||||
|
// and thus, setting a value will not be accessible from other part of the
|
||||||
|
// code ?
|
||||||
free(tlv->pad1);
|
free(tlv->pad1);
|
||||||
|
|
||||||
neighbour *new = (neighbour*) malloc(sizeof(neighbour));
|
neighbour *new = (neighbour*) malloc(sizeof(neighbour));
|
||||||
|
Loading…
Reference in New Issue
Block a user