build tlvs #NOT FINISHED#

This commit is contained in:
gonzalef 2020-03-31 19:16:23 +02:00
parent a82c8cb60a
commit 12745c0100
3 changed files with 55 additions and 11 deletions

View File

@ -175,21 +175,33 @@ void work_with_tlvs(char *data, short packet_len){
pos += tlv_len + 2;
break;
case 2:
// We received a neighbour request so a neighbor tlv has to be sent
// We received a neighbour request so a random neighbor tlv has to be sent
tlv_len = data[pos+1];
pos += tlv_len + 2;
// NOT FINISHED - Where are ip and seqno stored?
build_neighbour(&tmp_tlv, ip, seqno);
// Send a neighbour tlv
neighbour_peer *random = get_random_neighbour();
build_neighbour(&tmp_tlv, random->ip, random->port);
// NOT FINISHED - What packet is it added to?
add_tlv(packet, &tmp_tlv, 3);
break;
case 3:
// We received a neighbour tlv so a tlv network hash is sent to that address
neighbour* cur_tlv = ((neighbour*) data) + pos;
struct in6_addr ip = cur_tlv->ip;
short port = cur_tlv->port;
tlv_len = data[pos+1];
pos += tlv_len + 2;
tlv.neighbour = (neighbour*) (data + pos);
// Build network hash
unsigned char hash[16];
hash_network(neighbour_list, hash);
build_network_hash(&tmp_tlv, hash);
// NOT FINISHED - What packet is it added to?
add_tlv(packet, &tmp_tlv, 4);
break;
case 4:
// We reveived a network hash tlv so
@ -274,6 +286,36 @@ void work_with_tlvs(struct tlvs_list receivied_tlvs){
// we store the entry in our data table.
}
// Get list length
int len_list(list *l) {
int len = 0;
list *tmp = l;
while(tmp != NULL) {
tmp = tmp->next;
len++;
}
return len;
}
// Get a random neighbour
neighbour_peer *get_random_neighbour() {
// Get a random number
time_t t;
srand((unsigned) time(&t));
int n = rand() % len_list(neighbour_list);
// Get nth neighbour
list *tmp = neighbour_list;
for(int i=0; i<n; i++) {
tmp = tmp->next;
}
return (neighbour_peer*) tmp->data;
}
int main(int argc, const char *argv[]) {
int cont = 1;

View File

@ -4,6 +4,7 @@
#define NODE_H
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -55,7 +56,7 @@ typedef struct list {
void *next;
} list;
// static lists
// Static variables
static list *data_list;
static list *neighbour_list;
@ -83,8 +84,8 @@ void t_update_neighbours();
void t_get_network_state();
// Helper functions
char * hash();
int len_list(list *l);
short * get_seq_no(short s, int n);
neighbour_peer *get_random_neighbour();
#endif

View File

@ -1,10 +1,11 @@
#ifndef TLV_H
#define TLV_H
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include "parser.h"
#ifndef TLV_H
#define TLV_H
#include "hash.h"
#define LEN_NEIGHBOUR_REQ 0
#define LEN_NEIGHBOUR 18
@ -115,7 +116,7 @@ int build_tlv(tlv *tlv, cmd_token token);
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_neighbour(tlv *tlv, struct in6_addr ip, short port);
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);