Merge branch 'get-network-state' of gaufre.informatique.univ-paris-diderot.fr:perdriau/dazibao into get-network-state

This commit is contained in:
n07070 2020-04-29 22:25:31 +02:00
commit e96c21027e
4 changed files with 36 additions and 36 deletions

View File

@ -1,5 +1,6 @@
// This is the main file of the Dazibao project. It represents the node, and // This is the main file of the Dazibao project. It represents the node, and
// handles all of the main logic, including the network connexions. // handles all of the main logic, including the network connexions.
#include "node.h" #include "node.h"
// Static variables // Static variables
@ -44,7 +45,7 @@ int ask_for_peers(int socket_num) {
memset(&dest, 0, sizeof(struct sockaddr_in6)); memset(&dest, 0, sizeof(struct sockaddr_in6));
dest.sin6_family = AF_INET6; dest.sin6_family = AF_INET6;
memcpy(&dest.sin6_addr, &ip, 16); memcpy(&dest.sin6_addr, &ip, 16);
dest.sin6_port = htons(port); dest.sin6_port = htobe16(port);
dest.sin6_scope_id = ifindex; dest.sin6_scope_id = ifindex;
// Send neighbour request TLV // Send neighbour request TLV
@ -176,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 // 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; list *tmp = data_list;
pub_data *data; pub_data *data;
@ -191,7 +192,7 @@ pub_data *get_data(int64_t id) {
} }
// Take data as args and create a pub_data structure in the heap // 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)); pub_data *new_data = (pub_data*) malloc(sizeof(pub_data));
char *_data = (char*) malloc(len); char *_data = (char*) malloc(len);
if (_data == NULL) { if (_data == NULL) {
@ -212,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 // 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 // Check if it's our own id
if(id == NODE_ID) { if(id == NODE_ID) {
// wtf // wtf
@ -263,7 +264,7 @@ int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data
list *tmp = data_list; list *tmp = data_list;
list *last = NULL; list *last = NULL;
list *new_node; list *new_node;
int64_t cur_id; uint64_t cur_id;
while(tmp != NULL) { while(tmp != NULL) {
cur_id = ((pub_data*) tmp->data)->id; cur_id = ((pub_data*) tmp->data)->id;
@ -472,7 +473,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
// Send length bytes from packet // Send length bytes from packet
int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { 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 // Vectorized buffer
struct iovec vec_buff[1]; struct iovec vec_buff[1];
@ -726,7 +727,7 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
} }
// Convert to hardware order. // 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 ) { 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."); print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics.");
return -1; return -1;
@ -851,7 +852,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
memset(&new_neighbour, 0, sizeof(new_neighbour)); memset(&new_neighbour, 0, sizeof(new_neighbour));
new_neighbour.sin6_family = AF_INET6; new_neighbour.sin6_family = AF_INET6;
memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); 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; new_neighbour.sin6_scope_id = ifindex;
// Build network hash // Build network hash
@ -924,7 +925,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 //if the hashes are identical nothing has to be done
print_debug(">> Received node hash, updating message entry..."); print_debug(">> Received node hash, updating message entry...");
cur_tlv.node_hash = (node_hash*) (data + pos); 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 data is found for this id then we check that both hashes are the same
if(pdata != NULL) { if(pdata != NULL) {
@ -943,7 +944,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 // 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); add_tlv(&pack, &new_tlv, sender, socket_num);
// The position is updated // The position is updated
@ -957,9 +958,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, // 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(be64toh(cur_tlv.node_state_req->node_id));
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);
@ -995,7 +995,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
} }
// Compare hashes // Compare hashes
pdata = get_data(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 data is found for this id then we check that both hashes are the same
if(pdata != NULL) { if(pdata != NULL) {
@ -1015,7 +1015,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
} }
// Else, we update the data // 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) { if (rc < 0) {
print_error("Error while adding node state !"); print_error("Error while adding node state !");
} }
@ -1072,7 +1072,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
// Neighbour check // Neighbour check
struct in6_addr ip = sender->sin6_addr; 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); int rc = add_n_update_neighbour(&ip, port);
@ -1139,7 +1139,7 @@ int t_get_network_state(int sock_fd){
} }
receiver->sin6_family = AF_INET6; receiver->sin6_family = AF_INET6;
receiver->sin6_addr = peer->ip; receiver->sin6_addr = peer->ip;
receiver->sin6_port = htons(peer->port); receiver->sin6_port = htobe16(peer->port);
receiver->sin6_scope_id = 0; receiver->sin6_scope_id = 0;
// Send out a TLV network state. // Send out a TLV network state.
@ -1316,7 +1316,7 @@ int bootstrap_node(int * sock_fd){
memset(&server_addr, 0, sizeof(server_addr)); memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin6_family = AF_INET6; server_addr.sin6_family = AF_INET6;
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); // 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) { if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
print_debug(">> Error - failed to bind socket"); print_debug(">> Error - failed to bind socket");
return -2; return -2;

View File

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

View File

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

View File

@ -75,7 +75,7 @@ typedef struct node_hash {
typedef struct node_state_req { typedef struct node_state_req {
unsigned char type; unsigned char type;
unsigned char length; unsigned char length;
int64_t node_id; uint64_t node_id;
} node_state_req; } node_state_req;
// 28 octets min, 220 octets max (data 0 -> 192) // 28 octets min, 220 octets max (data 0 -> 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_neighbour(tlv *tlv, struct in6_addr ip, int16_t port);
int build_network_hash(tlv *tlv, list *data_list); int build_network_hash(tlv *tlv, list *data_list);
int build_network_state_req(tlv *tlv); 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_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data);
int build_node_state_req(tlv *tlv, int64_t node_id); int build_node_state_req(tlv *tlv, uint64_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_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); int build_warning(tlv *tlv, char *message, size_t message_len);
#endif #endif