hash fix (not finished yet)
This commit is contained in:
parent
733c700492
commit
c56a151762
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
||||
TARGET ?= dazibao
|
||||
SRC_DIRS ?= ./src/*
|
||||
CC := gcc
|
||||
CFLAGS= -O2 -Wall
|
||||
CFLAGS= -O2 -Wall -g
|
||||
SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.s)
|
||||
OBJS := $(addsuffix .o,$(basename $(SRCS)))
|
||||
DEPS := $(OBJS:.o=.d)
|
||||
|
45
src/hash.c
45
src/hash.c
@ -3,6 +3,14 @@
|
||||
|
||||
// Hash a single data
|
||||
void hash_data(pub_data *data, unsigned char *buf) {
|
||||
/*
|
||||
data->length = 52 - 26;
|
||||
data->id = 34538;
|
||||
data->seqno = 4864;
|
||||
data->data = "Luke, je suis ton \"pair\" !";
|
||||
printf("Hash received: 9ffe841a99776f6d1295ac75b53a58d7\n");
|
||||
*/
|
||||
|
||||
// All three fields are concatenated into a single buffer
|
||||
int totlen = data->length + 10;
|
||||
unsigned char concat[totlen];
|
||||
@ -18,6 +26,16 @@ void hash_data(pub_data *data, unsigned char *buf) {
|
||||
|
||||
// Put truncated hash into buf
|
||||
hash_trunc(hash, buf);
|
||||
|
||||
/*
|
||||
printf("Hash built: ");
|
||||
for(int i = 0; i < 16; i++) {
|
||||
printf("%02x", buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
exit(1);
|
||||
*/
|
||||
}
|
||||
|
||||
// Hash every data contained in data_list then return a network hash
|
||||
@ -61,15 +79,36 @@ void hash_trunc(unsigned char *hash32oct, unsigned char *buf) {
|
||||
|
||||
// Concat all fields of data and put them in buf
|
||||
void concat_data(pub_data *data, unsigned char *buf) {
|
||||
if (memcpy(buf, &(data->id), 8) == NULL) {
|
||||
// Turn seqno to big endian
|
||||
uint16_t seqno = htobe16(data->seqno);
|
||||
|
||||
if (memcpy(buf, (char*) &data->id, 8) == NULL) {
|
||||
print_debug(">> Concat the data (id) didn't work !");
|
||||
};
|
||||
if (memcpy(buf+8, &(data->seqno), 2) == NULL) {
|
||||
}
|
||||
if (memcpy(buf+8, (char*) &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 !");
|
||||
}
|
||||
|
||||
/*
|
||||
uint64_t *id = (uint64_t *) buf;
|
||||
uint16_t *seqno2 = (uint16_t *) (buf + 8);
|
||||
char *message = (char*) (buf + 10);
|
||||
char fuck[100];
|
||||
|
||||
printf("id: %ld\nseqno: %d\nmessage: ", *id, *seqno2);
|
||||
for(int i = 0; i < data->length; i++) {
|
||||
printf("%c", message[i]);
|
||||
}
|
||||
printf("\nORIGINAL\n");
|
||||
printf("id: %ld\nseqno: %d\nmessage: ", data->id, htobe16(data->seqno));
|
||||
for(int i = 0; i < data->length; i++) {
|
||||
printf("%c", data->data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
*/
|
||||
}
|
||||
|
||||
// Concat hash2 to hash1 (hash1 is modified)
|
||||
|
70
src/node.c
70
src/node.c
@ -192,9 +192,9 @@ pub_data *get_data(uint64_t id) {
|
||||
}
|
||||
|
||||
// Take data as args and create a pub_data structure in the heap
|
||||
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data) {
|
||||
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) {
|
||||
pub_data *new_data = (pub_data*) malloc(sizeof(pub_data));
|
||||
unsigned char *_data = (unsigned char*) malloc(len);
|
||||
char *_data = (char*) malloc(len);
|
||||
if (_data == NULL) {
|
||||
print_error("Failed to allocate memory for copying the data !");
|
||||
return NULL;
|
||||
@ -213,7 +213,7 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned cha
|
||||
}
|
||||
|
||||
// A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added
|
||||
int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found) {
|
||||
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) {
|
||||
// Check if it's our own id
|
||||
if(id == NODE_ID) {
|
||||
// wtf
|
||||
@ -249,7 +249,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data
|
||||
|
||||
// Updata message
|
||||
free(found->data);
|
||||
found->data = (unsigned char*) malloc(len);
|
||||
found->data = (char*) malloc(len);
|
||||
memcpy(found->data, data, len);
|
||||
|
||||
printf(">> Updated %li's published data.\n", id);
|
||||
@ -483,12 +483,12 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
||||
int error_while_sending = 0;
|
||||
|
||||
// Creating the struct to send out with sendmsg
|
||||
struct msghdr packet_tlv_send_out = {
|
||||
.msg_name = dest,
|
||||
.msg_namelen = sizeof(struct sockaddr_in6),
|
||||
.msg_iov = vec_buff,
|
||||
.msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
|
||||
};
|
||||
struct msghdr packet_tlv_send_out;
|
||||
memset(&packet_tlv_send_out, 0, sizeof(struct msghdr));
|
||||
packet_tlv_send_out.msg_name = dest;
|
||||
packet_tlv_send_out.msg_namelen = sizeof(struct sockaddr_in6);
|
||||
packet_tlv_send_out.msg_iov = vec_buff;
|
||||
packet_tlv_send_out.msg_iovlen = 1; // We have only one iovec buffer. But if we had 2, we would write 2.
|
||||
|
||||
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
|
||||
if (response_code < 0) {
|
||||
@ -504,6 +504,7 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
||||
if (error_while_sending == 1) {
|
||||
return -1;
|
||||
} else {
|
||||
printf(">> Packet successfully sent to peer. %d bytes sent.\n", response_code);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -794,8 +795,8 @@ int add_message(char * message, int message_len){
|
||||
our_data->seqno = (our_data->seqno + 1) % 65535;
|
||||
our_data->length = message_len;
|
||||
free(our_data->data);
|
||||
our_data->data = (unsigned char*) malloc(message_len);
|
||||
memcpy(our_data->data, (unsigned char *) message, message_len);
|
||||
our_data->data = (char*) malloc(message_len);
|
||||
memcpy(our_data->data, message, message_len);
|
||||
|
||||
print_debug(">> Message added.");
|
||||
|
||||
@ -990,6 +991,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
|
||||
pdata = get_data(be64toh(*id));
|
||||
|
||||
printf("Received: %ld\n", be64toh(*id));
|
||||
|
||||
// If data is found for this id then we check that both hashes are the same
|
||||
if(pdata != NULL) {
|
||||
print_debug(">> Comparing hashes...");
|
||||
@ -1001,13 +1004,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
// The position is updated
|
||||
tlv_len = data[pos+1];
|
||||
pos += tlv_len + 2;
|
||||
print_debug(">> Both hashs are the same, nothing to do.");
|
||||
print_debug(">> Both hashes are the same, nothing to do.");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
print_debug(">> No hash found, or hashs are different, sending back node state request.");
|
||||
print_debug(">> No hash found, or hashes are different, sending back 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, be64toh(*id));
|
||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||
@ -1033,6 +1036,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||
} else {
|
||||
print_debug(">> Found no data for the requested node, skipping...");
|
||||
printf("Received: %ld\n", be64toh(*id));
|
||||
}
|
||||
|
||||
// The position is updated
|
||||
@ -1063,7 +1067,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
fflush(0);
|
||||
}
|
||||
printf("\n");
|
||||
for(int x = 0; x < tlv_len; x++){
|
||||
for(int x = 0; x < tlv_len - 26; x++){
|
||||
printf("%c", cur_message[x]);
|
||||
fflush(0);
|
||||
}
|
||||
@ -1071,16 +1075,33 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
|
||||
// Check if it has the right hash
|
||||
pub_data pdata_check = (pub_data) {
|
||||
.length = tlv_len,
|
||||
.id = *id,
|
||||
.seqno = *seqno,
|
||||
.data = (unsigned char*) malloc(tlv_len)
|
||||
.length = tlv_len - 26,
|
||||
.id = be64toh(*id),
|
||||
.seqno = be16toh(*seqno),
|
||||
.data = (char*) malloc(tlv_len)
|
||||
};
|
||||
|
||||
memcpy(pdata_check.data, cur_message, tlv_len);
|
||||
memcpy(pdata_check.data, cur_message, tlv_len - 26);
|
||||
|
||||
hash_data(&pdata_check, hash2);
|
||||
|
||||
print_debug(">> Built message: ");
|
||||
printf("Type : %d\n", data[pos] );
|
||||
printf("Length : %d\n", pdata_check.length );
|
||||
printf("Node ID : %lu\n", pdata_check.id);
|
||||
printf("Seqno : %hu\n", pdata_check.seqno);
|
||||
printf("Hash :");
|
||||
for(int x = 0; x < 16; x++){
|
||||
printf("%02x", hash2[x]);
|
||||
fflush(0);
|
||||
}
|
||||
printf("\n");
|
||||
for(int x = 0; x < tlv_len - 26; x++){
|
||||
printf("%c", pdata_check.data[x]);
|
||||
fflush(0);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
if(memcmp(hash2, cur_hash, 16) != 0) {
|
||||
print_debug(">> Malformed hash.");
|
||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : ");
|
||||
@ -1104,6 +1125,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
break;
|
||||
}
|
||||
|
||||
free(pdata_check.data);
|
||||
|
||||
/*
|
||||
if (DEBUG_LEVEL > 0) {
|
||||
@ -1137,7 +1159,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
}
|
||||
|
||||
// Else, we update the data
|
||||
int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), (unsigned char*) cur_message, pdata);
|
||||
int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), cur_message, pdata);
|
||||
if (rc < 0) {
|
||||
print_error("Error while adding node state !");
|
||||
}
|
||||
@ -1154,7 +1176,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
cur_message = data + pos + 2;
|
||||
|
||||
// Print exactly new_tlv.length characters from new_tlv.message
|
||||
sprintf(warn, ">> WARNING:\n%%.%ds", tlv_len + 1);
|
||||
sprintf(warn, ">> WARNING:\n%%.%ds\n", tlv_len + 1);
|
||||
printf(warn, cur_message);
|
||||
|
||||
// The position is updated
|
||||
@ -1166,7 +1188,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
||||
strcpy(warn, "Packet is malformed.");
|
||||
print_debug(">> Malformed packet, we won't treat it.");
|
||||
print_debug(">> Sending back a Warning to sender.");
|
||||
build_warning(&new_tlv,(unsigned char *) warn, strlen(warn));
|
||||
build_warning(&new_tlv, warn, strlen(warn));
|
||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||
|
||||
return -1;
|
||||
@ -1371,7 +1393,7 @@ int run_node(int sock_fd){
|
||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer );
|
||||
}
|
||||
// Add message to the message table.
|
||||
if (add_message(input_buffer, bytes) < 0) {
|
||||
if (add_message(input_buffer, bytes-1) < 0) {
|
||||
print_debug(">> Error while trying to add the message to the list of messages, please try again..");
|
||||
// Reset buffer
|
||||
memset(input_buffer, 0, sizeof(input_buffer));
|
||||
|
@ -43,7 +43,7 @@ typedef struct pub_data {
|
||||
unsigned char length;
|
||||
uint64_t id;
|
||||
uint16_t seqno;
|
||||
unsigned char *data;
|
||||
char *data;
|
||||
} pub_data;
|
||||
|
||||
// General list
|
||||
@ -62,7 +62,8 @@ typedef struct list {
|
||||
|
||||
// The node ID
|
||||
// #define NODE_ID 42675882021843277
|
||||
#define NODE_ID 13809235719890846928
|
||||
// #define NODE_ID 13809235719890846928
|
||||
static uint64_t NODE_ID = 80927976239;
|
||||
|
||||
// The number of neighbours
|
||||
// The neighbour table has 15 entries
|
||||
@ -141,9 +142,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port);
|
||||
pub_data *get_data(uint64_t id);
|
||||
|
||||
// Take data as args and create a pub_data structure in the heap
|
||||
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data);
|
||||
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data);
|
||||
|
||||
// add new data to data list
|
||||
int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found);
|
||||
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found);
|
||||
|
||||
#endif
|
||||
|
41
src/tlv.c
41
src/tlv.c
@ -1,33 +1,5 @@
|
||||
#include "tlv.h"
|
||||
|
||||
// TODO this can be deleted ?
|
||||
// creer un tlv
|
||||
int build_tlv(tlv *tlv, cmd_token token) {
|
||||
switch(token.type) {
|
||||
case NEIGHBOUR_REQ:
|
||||
// a remplir
|
||||
break;
|
||||
case NETWORK_STATE_REQ:
|
||||
// a remplir
|
||||
break;
|
||||
case NODE_STATE_REQ:
|
||||
// a remplir
|
||||
break;
|
||||
case POST:
|
||||
// a remplir
|
||||
break;
|
||||
case ERROR:
|
||||
printf("Wrong format, use 'req {neighbour | network state | node state}' or 'post {message}'");
|
||||
break;
|
||||
default:
|
||||
perror("Unrecognized tlv type.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
// end deletion
|
||||
|
||||
int build_pad1(tlv *tlv) {
|
||||
// Free the previously allocated memory
|
||||
free(tlv->pad1);
|
||||
@ -119,7 +91,7 @@ int build_network_hash(tlv *tlv, list *data_list) {
|
||||
|
||||
new->type = 4;
|
||||
new->length = 16;
|
||||
hash_network(data_list, (unsigned char*) new->network_hash);
|
||||
hash_network(data_list, new->network_hash);
|
||||
|
||||
tlv->network_hash = new;
|
||||
|
||||
@ -144,7 +116,7 @@ int build_network_state_req(tlv *tlv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data) {
|
||||
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) {
|
||||
// Free the previously allocated memory
|
||||
free(tlv->pad1);
|
||||
|
||||
@ -160,7 +132,7 @@ int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *d
|
||||
new->seqno = htobe16(seqno);
|
||||
|
||||
pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data};
|
||||
hash_data(&pdata, (unsigned char*) new->node_hash);
|
||||
hash_data(&pdata, new->node_hash);
|
||||
|
||||
tlv->node_hash = new;
|
||||
|
||||
@ -174,7 +146,6 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) {
|
||||
node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req));
|
||||
memset(new, 0, sizeof(node_state_req));
|
||||
|
||||
|
||||
if(new == NULL)
|
||||
return -1;
|
||||
|
||||
@ -187,7 +158,7 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len) {
|
||||
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) {
|
||||
// Free the previously allocated memory
|
||||
free(tlv->pad1);
|
||||
|
||||
@ -212,14 +183,14 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *
|
||||
memcpy(new->data, data, len);
|
||||
|
||||
pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data};
|
||||
hash_data(&pdata, (unsigned char*) new->node_hash);
|
||||
hash_data(&pdata, new->node_hash);
|
||||
|
||||
tlv->node_state = new;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int build_warning(tlv *tlv, unsigned char *message, size_t message_len) {
|
||||
int build_warning(tlv *tlv, char *message, size_t message_len) {
|
||||
// Free the previously allocated memory
|
||||
free(tlv->pad1);
|
||||
|
||||
|
@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv);
|
||||
int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port);
|
||||
int build_network_hash(tlv *tlv, list *data_list);
|
||||
int build_network_state_req(tlv *tlv);
|
||||
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data);
|
||||
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data);
|
||||
int build_node_state_req(tlv *tlv, uint64_t node_id);
|
||||
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len);
|
||||
int build_warning(tlv *tlv, unsigned char *message, size_t message_len);
|
||||
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len);
|
||||
int build_warning(tlv *tlv, char *message, size_t message_len);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user