change data types
This commit is contained in:
parent
cdef7ee0ac
commit
50bb64059b
@ -2,6 +2,7 @@
|
|||||||
#define HASH_H
|
#define HASH_H
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
25
src/node.c
25
src/node.c
@ -1,18 +1,11 @@
|
|||||||
// 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 <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "tlv.h"
|
|
||||||
#include "hash.h"
|
|
||||||
#include "parser.h"
|
|
||||||
|
|
||||||
|
// Static variables
|
||||||
|
static list *data_list;
|
||||||
|
static list *neighbour_list;
|
||||||
|
|
||||||
/* ---- Fonctions utilitaires ---- */
|
/* ---- Fonctions utilitaires ---- */
|
||||||
|
|
||||||
@ -47,7 +40,7 @@ neighbour_peer *get_random_neighbour() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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(long id) {
|
pub_data *get_data(int64_t id) {
|
||||||
list *tmp = data_list;
|
list *tmp = data_list;
|
||||||
pub_data *data;
|
pub_data *data;
|
||||||
|
|
||||||
@ -62,7 +55,7 @@ pub_data *get_data(long 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, long id, short 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);
|
||||||
new_data->length = len;
|
new_data->length = len;
|
||||||
@ -75,7 +68,7 @@ pub_data *copy_data(unsigned char len, long id, short seqno, char *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add new data to data list
|
// Add new data to data list
|
||||||
void add_data(unsigned char len, long id, short seqno, char *data) {
|
void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
||||||
// If id is the same as this node's id then we only update seqno
|
// If id is the same as this node's id then we only update seqno
|
||||||
if(id == NODE_ID) {
|
if(id == NODE_ID) {
|
||||||
pub_data *node_data = get_data(NODE_ID);
|
pub_data *node_data = get_data(NODE_ID);
|
||||||
@ -103,7 +96,7 @@ void add_data(unsigned char len, long id, short seqno, char *data) {
|
|||||||
list *tmp = data_list;
|
list *tmp = data_list;
|
||||||
list *last = NULL;
|
list *last = NULL;
|
||||||
list *new_node;
|
list *new_node;
|
||||||
long cur_id;
|
int64_t cur_id;
|
||||||
|
|
||||||
while(tmp != NULL) {
|
while(tmp != NULL) {
|
||||||
cur_id = ((pub_data*) tmp->data)->id;
|
cur_id = ((pub_data*) tmp->data)->id;
|
||||||
@ -224,7 +217,7 @@ int send_tlv(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_l
|
|||||||
|
|
||||||
// We need to make sure the TLV announces a length that will no go onto
|
// We need to make sure the TLV announces a length that will no go onto
|
||||||
// another tlv, as we might end up reading bullshit.
|
// another tlv, as we might end up reading bullshit.
|
||||||
int validate_tlv(char *data, int pos, short packet_len){
|
int validate_tlv(char *data, int pos, int16_t packet_len){
|
||||||
char type = data[pos];
|
char type = data[pos];
|
||||||
|
|
||||||
// Nothing to do in this case
|
// Nothing to do in this case
|
||||||
@ -309,7 +302,7 @@ 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, int16_t packet_len, struct sockaddr_in6 sender){
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
unsigned char tlv_len, hash[16];
|
unsigned char tlv_len, hash[16];
|
||||||
char warn[32];
|
char warn[32];
|
||||||
|
23
src/node.h
23
src/node.h
@ -4,12 +4,13 @@
|
|||||||
#define NODE_H
|
#define NODE_H
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdint.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
|
||||||
@ -18,7 +19,7 @@
|
|||||||
*/
|
*/
|
||||||
typedef struct neighbour_peer {
|
typedef struct neighbour_peer {
|
||||||
struct in6_addr ip;
|
struct in6_addr ip;
|
||||||
short port;
|
int16_t port;
|
||||||
char is_temporary;
|
char is_temporary;
|
||||||
struct timeval last_seen;
|
struct timeval last_seen;
|
||||||
} neighbour_peer;
|
} neighbour_peer;
|
||||||
@ -32,8 +33,8 @@ typedef struct neighbour_peer {
|
|||||||
|
|
||||||
typedef struct pub_data {
|
typedef struct pub_data {
|
||||||
unsigned char length;
|
unsigned char length;
|
||||||
long id;
|
int64_t id;
|
||||||
short seqno;
|
int16_t seqno;
|
||||||
char *data;
|
char *data;
|
||||||
} pub_data;
|
} pub_data;
|
||||||
|
|
||||||
@ -57,10 +58,6 @@ typedef struct list {
|
|||||||
// The neighbour table has 15 entries
|
// The neighbour table has 15 entries
|
||||||
#define NEIGHBOUR_MAX 15
|
#define NEIGHBOUR_MAX 15
|
||||||
|
|
||||||
// Static variables
|
|
||||||
static list *data_list;
|
|
||||||
static list *neighbour_list;
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
||||||
// fonctions signatures
|
// fonctions signatures
|
||||||
@ -68,11 +65,11 @@ void listen_for_packets();
|
|||||||
|
|
||||||
int check_header(char * received_datagram, int buffer_len, packet * packet_to_return);
|
int check_header(char * received_datagram, int buffer_len, packet * packet_to_return);
|
||||||
|
|
||||||
int validate_tlv(char *data, int pos, short packet_len);
|
int validate_tlv(char *data, int pos, int16_t packet_len);
|
||||||
|
|
||||||
int update_neighbours();
|
int update_neighbours();
|
||||||
|
|
||||||
int work_with_tlvs(char * data, short packet_len, struct sockaddr_in6 sender);
|
int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender);
|
||||||
|
|
||||||
void add_tlv(struct packet *packet, union tlv *tlv, char type);
|
void add_tlv(struct packet *packet, union tlv *tlv, char type);
|
||||||
|
|
||||||
@ -101,12 +98,12 @@ int len_list(list *l);
|
|||||||
neighbour_peer *get_random_neighbour();
|
neighbour_peer *get_random_neighbour();
|
||||||
|
|
||||||
// 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(long id);
|
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, long id, short seqno, char *data);
|
pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data);
|
||||||
|
|
||||||
// add new data to data list
|
// add new data to data list
|
||||||
void add_data(unsigned char len, long id, short seqno, char *data);
|
void add_data(unsigned char len, int64_t id, int16_t seqno, char *data);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
14
src/tlv.c
14
src/tlv.c
@ -68,7 +68,7 @@ int build_neighbour_req(tlv *tlv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_neighbour(tlv *tlv, struct in6_addr ip, short port) {
|
int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) {
|
||||||
neighbour *new = (neighbour*) malloc(sizeof(neighbour));
|
neighbour *new = (neighbour*) malloc(sizeof(neighbour));
|
||||||
|
|
||||||
if(new == NULL)
|
if(new == NULL)
|
||||||
@ -92,7 +92,7 @@ int build_network_hash(tlv *tlv, list *data_list) {
|
|||||||
|
|
||||||
new->type = 4;
|
new->type = 4;
|
||||||
new->length = 16;
|
new->length = 16;
|
||||||
hash_network(data_list, new->network_hash);
|
hash_network(data_list, (unsigned char*) new->network_hash);
|
||||||
|
|
||||||
tlv->network_hash = new;
|
tlv->network_hash = new;
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ int build_network_state_req(tlv *tlv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_node_hash(tlv *tlv, long node_id, short seqno, char *data) {
|
int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) {
|
||||||
node_hash *new = (node_hash*) malloc(sizeof(node_hash));
|
node_hash *new = (node_hash*) malloc(sizeof(node_hash));
|
||||||
|
|
||||||
if(new == NULL)
|
if(new == NULL)
|
||||||
@ -125,14 +125,14 @@ int build_node_hash(tlv *tlv, long node_id, short seqno, char *data) {
|
|||||||
new->seqno = seqno;
|
new->seqno = 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, new->node_hash);
|
hash_data(&pdata, (unsigned char*) new->node_hash);
|
||||||
|
|
||||||
tlv->node_hash = new;
|
tlv->node_hash = new;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_node_state_req(tlv *tlv, long node_id) {
|
int build_node_state_req(tlv *tlv, int64_t node_id) {
|
||||||
node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req));
|
node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req));
|
||||||
|
|
||||||
if(new == NULL)
|
if(new == NULL)
|
||||||
@ -147,7 +147,7 @@ int build_node_state_req(tlv *tlv, long node_id) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len) {
|
int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len) {
|
||||||
node_state *new = (node_state*) malloc(sizeof(node_state));
|
node_state *new = (node_state*) malloc(sizeof(node_state));
|
||||||
int len = data_len + 26;
|
int len = data_len + 26;
|
||||||
|
|
||||||
@ -167,7 +167,7 @@ int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t dat
|
|||||||
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};
|
||||||
hash_data(&pdata, new->node_hash);
|
hash_data(&pdata, (unsigned char*) new->node_hash);
|
||||||
|
|
||||||
tlv->node_state = new;
|
tlv->node_state = new;
|
||||||
|
|
||||||
|
13
src/tlv.h
13
src/tlv.h
@ -4,6 +4,7 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define LEN_NEIGHBOUR_REQ 0
|
#define LEN_NEIGHBOUR_REQ 0
|
||||||
#define LEN_NEIGHBOUR 18
|
#define LEN_NEIGHBOUR 18
|
||||||
@ -65,7 +66,7 @@ typedef struct network_state_req {
|
|||||||
typedef struct node_hash {
|
typedef struct node_hash {
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
unsigned char length;
|
unsigned char length;
|
||||||
long node_id;
|
int64_t node_id;
|
||||||
short seqno;
|
short seqno;
|
||||||
char node_hash[16];
|
char node_hash[16];
|
||||||
} node_hash;
|
} node_hash;
|
||||||
@ -74,14 +75,14 @@ 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;
|
||||||
long node_id;
|
int64_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)
|
||||||
typedef struct node_state {
|
typedef struct node_state {
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
unsigned char length;
|
unsigned char length;
|
||||||
long node_id;
|
int64_t node_id;
|
||||||
short seqno;
|
short seqno;
|
||||||
char node_hash[16];
|
char node_hash[16];
|
||||||
char data[192];
|
char data[192];
|
||||||
@ -121,9 +122,9 @@ 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, 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, int64_t node_id, short seqno, char *data);
|
||||||
int build_node_state_req(tlv *tlv, long node_id);
|
int build_node_state_req(tlv *tlv, int64_t node_id);
|
||||||
int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len);
|
int build_node_state(tlv *tlv, int64_t node_id, short 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
|
||||||
|
Loading…
Reference in New Issue
Block a user