int64_t to uint64_t and endianness problem

This commit is contained in:
gonzalef 2020-04-29 22:22:30 +02:00
parent 352aed242d
commit 0bb80d078c
4 changed files with 42 additions and 56 deletions

View File

@ -1,22 +1,6 @@
// This is the main file of the Dazibao project. It represents the node, and
// handles all of the main logic, including the network connexions.
<<<<<<< HEAD
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <arpa/inet.h>
#include <wchar.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <locale.h>
=======
>>>>>>> add-message
#include "node.h"
// Static variables
@ -61,7 +45,7 @@ int ask_for_peers(int socket_num) {
memset(&dest, 0, sizeof(struct sockaddr_in6));
dest.sin6_family = AF_INET6;
memcpy(&dest.sin6_addr, &ip, 16);
dest.sin6_port = htons(port);
dest.sin6_port = htobe16(port);
dest.sin6_scope_id = ifindex;
// Send neighbour request TLV
@ -193,7 +177,7 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
}
// get data associated with id, if it doesn't exist return NULL
pub_data *get_data(int64_t id) {
pub_data *get_data(uint64_t id) {
list *tmp = data_list;
pub_data *data;
@ -208,7 +192,7 @@ pub_data *get_data(int64_t id) {
}
// Take data as args and create a pub_data structure in the heap
pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, 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));
char *_data = (char*) malloc(len);
if (_data == NULL) {
@ -229,7 +213,7 @@ pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) {
}
// 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, int64_t id, uint16_t seqno, 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
@ -280,7 +264,7 @@ int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data
list *tmp = data_list;
list *last = NULL;
list *new_node;
int64_t cur_id;
uint64_t cur_id;
while(tmp != NULL) {
cur_id = ((pub_data*) tmp->data)->id;
@ -489,7 +473,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
// Send length bytes from packet
int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) {
((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length);
((packet*) packet_buff)->length = htobe16(((packet*) packet_buff)->length);
// Vectorized buffer
struct iovec vec_buff[1];
@ -743,7 +727,7 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
}
// Convert to hardware order.
((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length);
((packet*) packet_to_return)->length = be16toh(((packet*) packet_to_return)->length);
if (packet_to_return->length + 4 < received_data_len ) {
print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics.");
return -1;
@ -866,7 +850,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
memset(&new_neighbour, 0, sizeof(new_neighbour));
new_neighbour.sin6_family = AF_INET6;
memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16);
new_neighbour.sin6_port = ntohs(cur_tlv.neighbour->port);
new_neighbour.sin6_port = be16toh(cur_tlv.neighbour->port);
new_neighbour.sin6_scope_id = ifindex;
// Build network hash
@ -924,6 +908,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
pdata = (pub_data*) tmp_list->data;
build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data);
add_tlv(&pack, &new_tlv, sender, socket_num);
tmp_list = tmp_list->next;
}
// The position is updated
@ -937,7 +922,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
//if the hashes are identical nothing has to be done
print_debug(">> Received node hash, updating message entry...");
cur_tlv.node_hash = (node_hash*) (data + pos);
pdata = get_data(ntohl(cur_tlv.node_hash->node_id));
pdata = get_data(be64toh(cur_tlv.node_hash->node_id));
// If data is found for this id then we check that both hashes are the same
if(pdata != NULL) {
@ -956,7 +941,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
}
// If no pub_data was found or the hashes differ then we send a node state request
build_node_state_req(&new_tlv, ntohl(cur_tlv.node_hash->node_id));
build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id));
add_tlv(&pack, &new_tlv, sender, socket_num);
// The position is updated
@ -969,8 +954,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
// so a node state tlv for this node id has to be sent,
// if no pub_data exists for this id nothing is sent
print_debug(">> Received node state request. Processing...");
cur_tlv.node_state_req = (node_state_req*) (data[pos]);
pdata = get_data(ntohl(cur_tlv.node_state_req->node_id));
cur_tlv.node_state_req = (node_state_req*) (data + pos);
pdata = get_data(be64toh(cur_tlv.node_state_req->node_id));
if(pdata != NULL) {
build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length);
@ -995,12 +980,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
print_error("The data in the current node is NULL !");
return -1;
}
printf("%s\n", data + pos );
printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0\n", (const wchar_t*) cur_tlv.node_state->data);
}
// Compare hashes
pdata = get_data(ntohl(cur_tlv.node_state->node_id));
pdata = get_data(be64toh(cur_tlv.node_state->node_id));
// If data is found for this id then we check that both hashes are the same
if(pdata != NULL) {
@ -1008,7 +992,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
hash_data(pdata, hash);
// If both hashes are the same then nothing has to be done
if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) {
if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) {
// The position is updated
tlv_len = data[pos+1];
pos += 2;
@ -1019,7 +1003,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
}
// Else, we update the 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, pdata);
int rc = add_data(cur_tlv.node_state->length - 26, be64toh(cur_tlv.node_state->node_id), be16toh(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata);
if (rc < 0) {
print_error("Error while adding node state !");
}
@ -1076,7 +1060,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
// Neighbour check
struct in6_addr ip = sender->sin6_addr;
int16_t port = htons(sender->sin6_port);
int16_t port = htobe16(sender->sin6_port);
int rc = add_n_update_neighbour(&ip, port);
@ -1143,7 +1127,7 @@ int t_get_network_state(int sock_fd){
}
receiver->sin6_family = AF_INET6;
receiver->sin6_addr = peer->ip;
receiver->sin6_port = htons(peer->port);
receiver->sin6_port = htobe16(peer->port);
receiver->sin6_scope_id = 0;
// Send out a TLV network state.
@ -1320,7 +1304,7 @@ int bootstrap_node(int * sock_fd){
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin6_family = AF_INET6;
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY);
server_addr.sin6_port = htons(LISTEN_PORT);
server_addr.sin6_port = htobe16(LISTEN_PORT);
if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
print_debug(">> Error - failed to bind socket");
return -2;

View File

@ -13,10 +13,12 @@
#include <stdint.h>
#include <net/if.h>
#include <errno.h>
#include <arpa/inet.h>
#include <endian.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <locale.h>
/* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)),
* et dont chaque entrée contient un booléen indiquant si le pair est permanent
@ -39,7 +41,7 @@ typedef struct neighbour_peer {
typedef struct pub_data {
unsigned char length;
int64_t id;
uint64_t id;
uint16_t seqno;
char *data;
} pub_data;
@ -136,12 +138,12 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port);
int add_n_update_neighbour(struct in6_addr *ip, int16_t port);
// get data associated with id, if it doesn't exist return NULL
pub_data *get_data(int64_t id);
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, int64_t id, uint16_t seqno, 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, int64_t id, uint16_t seqno, char *data, pub_data *found);
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found);
#endif

View File

@ -100,7 +100,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) {
new->type = 3;
new->length = 18;
new->ip = ip;
new->port = htons(port);
new->port = htobe16(port);
tlv->neighbour = new;
@ -144,7 +144,7 @@ int build_network_state_req(tlv *tlv) {
return 0;
}
int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, 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);
@ -156,8 +156,8 @@ int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) {
new->type = 6;
new->length = 26;
new->node_id = htonl(node_id);
new->seqno = htons(seqno);
new->node_id = htobe64(node_id);
new->seqno = htobe16(seqno);
pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data};
hash_data(&pdata, (unsigned char*) new->node_hash);
@ -167,7 +167,7 @@ int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) {
return 0;
}
int build_node_state_req(tlv *tlv, int64_t node_id) {
int build_node_state_req(tlv *tlv, uint64_t node_id) {
// Free the previously allocated memory
free(tlv->pad1);
@ -180,14 +180,14 @@ int build_node_state_req(tlv *tlv, int64_t node_id) {
new->type = 7;
new->length = 8;
new->node_id = htonl(node_id);
new->node_id = htobe64(node_id);
tlv->node_state_req = new;
return 0;
}
int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, 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);
@ -207,8 +207,8 @@ int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size
new->type = 8;
new->length = 26 + len;
new->node_id = htonl(node_id);
new->seqno = htons(seqno);
new->node_id = htobe64(node_id);
new->seqno = htobe16(seqno);
memcpy(new->data, data, len);
pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data};

View File

@ -66,7 +66,7 @@ typedef struct network_state_req {
typedef struct node_hash {
unsigned char type;
unsigned char length;
int64_t node_id;
uint64_t node_id;
uint16_t seqno;
char node_hash[16];
} node_hash;
@ -75,14 +75,14 @@ typedef struct node_hash {
typedef struct node_state_req {
unsigned char type;
unsigned char length;
int64_t node_id;
uint64_t node_id;
} node_state_req;
// 28 octets min, 220 octets max (data 0 -> 192)
typedef struct node_state {
unsigned char type;
unsigned char length;
int64_t node_id;
uint64_t node_id;
uint16_t seqno;
char node_hash[16];
char data[192];
@ -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, int64_t node_id, uint16_t seqno, char *data);
int build_node_state_req(tlv *tlv, int64_t node_id);
int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len);
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, char *data, size_t data_len);
int build_warning(tlv *tlv, char *message, size_t message_len);
#endif