dazibao/tlv.h

113 lines
2.5 KiB
C
Raw Normal View History

2020-03-07 21:33:11 +01:00
#include <sys/socket.h>
#include <netinet/in.h>
2020-03-20 17:39:17 +01:00
#include <stdlib.h>
2020-03-19 19:39:37 +01:00
#include "parser.h"
2020-03-07 21:33:11 +01:00
2020-03-13 14:27:44 +01:00
// 8 octets min (struct pointer 4 octets), 1024 octets max
typedef struct packet {
unsigned char magic; // 95 (si autre, ignorer)
unsigned char version; // 1 (si autre, ignorer)
short length; // 1020 max
char *body;
} packet;
2020-03-13 13:36:51 +01:00
// 1 octet
2020-03-07 21:33:11 +01:00
typedef struct pad1 {
2020-03-20 17:39:17 +01:00
unsigned char type;
2020-03-07 21:33:11 +01:00
} pad1;
2020-03-20 17:39:17 +01:00
// 2 octets min, 258 octets max (unsigned char 0 -> 255)
2020-03-07 21:33:11 +01:00
typedef struct padn {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-24 15:39:31 +01:00
char mbz[256];
2020-03-07 21:33:11 +01:00
} padn;
2020-03-13 13:36:51 +01:00
// 2 octets
2020-03-07 21:33:11 +01:00
typedef struct neighbour_req {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
} neighbour_req;
2020-03-20 17:39:17 +01:00
// 20 octets
2020-03-07 21:33:11 +01:00
typedef struct neighbour {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
struct in6_addr ip;
short port;
} neighbour;
2020-03-13 13:36:51 +01:00
// 18 octets
2020-03-07 21:33:11 +01:00
typedef struct network_hash {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-20 17:39:17 +01:00
char network_hash[16];
2020-03-07 21:33:11 +01:00
} network_hash;
2020-03-13 13:36:51 +01:00
// 2 octets
2020-03-07 21:33:11 +01:00
typedef struct network_state_req {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
} network_state_req;
2020-03-13 13:36:51 +01:00
// 28 octets
2020-03-07 21:33:11 +01:00
typedef struct node_hash {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
long node_id;
short seqno;
2020-03-20 17:39:17 +01:00
char node_hash[16];
2020-03-07 21:33:11 +01:00
} node_hash;
2020-03-13 13:36:51 +01:00
// 10 octets
2020-03-07 21:33:11 +01:00
typedef struct node_state_req {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
long node_id;
} node_state_req;
2020-03-13 13:36:51 +01:00
// 28 octets min, 220 octets max (data 0 -> 192)
2020-03-07 21:33:11 +01:00
typedef struct node_state {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
long node_id;
short seqno;
2020-03-20 17:39:17 +01:00
char node_hash[16];
2020-03-24 15:39:31 +01:00
char data[192];
2020-03-07 21:33:11 +01:00
} node_state;
2020-03-20 17:39:17 +01:00
// 2 octets min, 258 ocets max (unsigned char 0 -> 255)
2020-03-07 21:33:11 +01:00
typedef struct warning {
2020-03-13 13:25:25 +01:00
unsigned char type;
unsigned char length;
2020-03-07 21:33:11 +01:00
char *message;
2020-03-19 19:39:37 +01:00
} warning;
2020-03-20 17:39:17 +01:00
typedef union tlv {
pad1 *pad1;
padn *padn;
neighbour_req *neighbour_req;
neighbour *neighbour;
network_hash *network_hash;
network_state_req *network_state_req;
node_hash *node_hash;
node_state_req *node_state_req;
node_state *node_state;
warning *warning;
} tlv;
2020-03-19 19:39:37 +01:00
// creer un tlv
2020-03-20 17:39:17 +01:00
int build_tlv(tlv *tlv, cmd_token token);
2020-03-19 19:39:37 +01:00
2020-03-20 17:39:17 +01:00
// creer un tlv specifique
int build_pad1(tlv *tlv);
int build_padn(tlv *tlv, size_t len);
int build_neighbour_req(tlv *tlv);
int build_neighbour(tlv *tlv, struct in6_addr ip, short seqno);
int build_network_hash(tlv *tlv, char *network_hash);
int build_network_state_req(tlv *tlv);
int build_node_hash(tlv *tlv, long node_id, short seqno, char *node_hash);
int build_node_state_req(tlv *tlv, long node_id);
int build_node_state(tlv *tlv, long node_id, short seqno, char *node_hash, char *data);
int build_warning(tlv *tlv, char *message);