change data types
This commit is contained in:
parent
cdef7ee0ac
commit
50bb64059b
@ -2,6 +2,7 @@
|
||||
#define HASH_H
|
||||
|
||||
#include <openssl/sha.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "tlv.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
|
||||
// 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 "tlv.h"
|
||||
#include "hash.h"
|
||||
#include "parser.h"
|
||||
|
||||
// Static variables
|
||||
static list *data_list;
|
||||
static list *neighbour_list;
|
||||
|
||||
/* ---- Fonctions utilitaires ---- */
|
||||
|
||||
@ -47,7 +40,7 @@ neighbour_peer *get_random_neighbour() {
|
||||
}
|
||||
|
||||
// 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;
|
||||
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
|
||||
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));
|
||||
char *_data = (char*) malloc(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
|
||||
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 == 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 *last = NULL;
|
||||
list *new_node;
|
||||
long cur_id;
|
||||
int64_t cur_id;
|
||||
|
||||
while(tmp != NULL) {
|
||||
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
|
||||
// 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];
|
||||
|
||||
// Nothing to do in this case
|
||||
@ -309,7 +302,7 @@ int update_neighbours(){
|
||||
};
|
||||
|
||||
// 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;
|
||||
unsigned char tlv_len, hash[16];
|
||||
char warn[32];
|
||||
|
23
src/node.h
23
src/node.h
@ -4,12 +4,13 @@
|
||||
#define NODE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.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)),
|
||||
* et dont chaque entrée contient un booléen indiquant si le pair est permanent
|
||||
@ -18,7 +19,7 @@
|
||||
*/
|
||||
typedef struct neighbour_peer {
|
||||
struct in6_addr ip;
|
||||
short port;
|
||||
int16_t port;
|
||||
char is_temporary;
|
||||
struct timeval last_seen;
|
||||
} neighbour_peer;
|
||||
@ -32,8 +33,8 @@ typedef struct neighbour_peer {
|
||||
|
||||
typedef struct pub_data {
|
||||
unsigned char length;
|
||||
long id;
|
||||
short seqno;
|
||||
int64_t id;
|
||||
int16_t seqno;
|
||||
char *data;
|
||||
} pub_data;
|
||||
|
||||
@ -57,10 +58,6 @@ typedef struct list {
|
||||
// The neighbour table has 15 entries
|
||||
#define NEIGHBOUR_MAX 15
|
||||
|
||||
// Static variables
|
||||
static list *data_list;
|
||||
static list *neighbour_list;
|
||||
|
||||
// TODO
|
||||
|
||||
// fonctions signatures
|
||||
@ -68,11 +65,11 @@ void listen_for_packets();
|
||||
|
||||
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 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);
|
||||
|
||||
@ -101,12 +98,12 @@ int len_list(list *l);
|
||||
neighbour_peer *get_random_neighbour();
|
||||
|
||||
// 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
|
||||
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
|
||||
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
|
||||
|
14
src/tlv.c
14
src/tlv.c
@ -68,7 +68,7 @@ int build_neighbour_req(tlv *tlv) {
|
||||
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));
|
||||
|
||||
if(new == NULL)
|
||||
@ -92,7 +92,7 @@ int build_network_hash(tlv *tlv, list *data_list) {
|
||||
|
||||
new->type = 4;
|
||||
new->length = 16;
|
||||
hash_network(data_list, new->network_hash);
|
||||
hash_network(data_list, (unsigned char*) new->network_hash);
|
||||
|
||||
tlv->network_hash = new;
|
||||
|
||||
@ -113,7 +113,7 @@ int build_network_state_req(tlv *tlv) {
|
||||
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));
|
||||
|
||||
if(new == NULL)
|
||||
@ -125,14 +125,14 @@ int build_node_hash(tlv *tlv, long node_id, short seqno, char *data) {
|
||||
new->seqno = seqno;
|
||||
|
||||
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;
|
||||
|
||||
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));
|
||||
|
||||
if(new == NULL)
|
||||
@ -147,7 +147,7 @@ int build_node_state_req(tlv *tlv, long node_id) {
|
||||
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));
|
||||
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);
|
||||
|
||||
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;
|
||||
|
||||
|
13
src/tlv.h
13
src/tlv.h
@ -4,6 +4,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define LEN_NEIGHBOUR_REQ 0
|
||||
#define LEN_NEIGHBOUR 18
|
||||
@ -65,7 +66,7 @@ typedef struct network_state_req {
|
||||
typedef struct node_hash {
|
||||
unsigned char type;
|
||||
unsigned char length;
|
||||
long node_id;
|
||||
int64_t node_id;
|
||||
short seqno;
|
||||
char node_hash[16];
|
||||
} node_hash;
|
||||
@ -74,14 +75,14 @@ typedef struct node_hash {
|
||||
typedef struct node_state_req {
|
||||
unsigned char type;
|
||||
unsigned char length;
|
||||
long node_id;
|
||||
int64_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;
|
||||
long node_id;
|
||||
int64_t node_id;
|
||||
short seqno;
|
||||
char node_hash[16];
|
||||
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_network_hash(tlv *tlv, list *data_list);
|
||||
int build_network_state_req(tlv *tlv);
|
||||
int build_node_hash(tlv *tlv, long node_id, short seqno, char *data);
|
||||
int build_node_state_req(tlv *tlv, long node_id);
|
||||
int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len);
|
||||
int build_node_hash(tlv *tlv, int64_t node_id, short seqno, char *data);
|
||||
int build_node_state_req(tlv *tlv, int64_t node_id);
|
||||
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);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user