From ac657e4bbdeda64b45b67f50111266dd6a1e839e Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 24 Mar 2020 18:19:35 +0100 Subject: [PATCH 1/2] Added tlv_list and check_header --- src/node.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++------ src/node.h | 13 ++++-- src/tlv.h | 5 +++ 3 files changed, 128 insertions(+), 18 deletions(-) diff --git a/src/node.c b/src/node.c index f826554..51b0ba1 100644 --- a/src/node.c +++ b/src/node.c @@ -1,15 +1,124 @@ // 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 +#include +#include +#include +#include +#include + +#include "tlv.h" +#include "node.h" -// Will return a packet when we receive one that's valid. -packet listen_for_packets(){ +// For every packet recivied, +// then we make sure it's conform +// We then extract the data from it to make it easy to work with +int check_header(char * req[], int buffer_size, packet * packet_to_return){ + packet * packet_to_return = (packet*) req; + + // We need to check a few things ; + // The first byte must be worth 95, + if (packet_to_return->magic != 95) { + perror(">> The magic number of the packet is no good."); + return -1; + } + + // The second byte must be worth 1, + if (packet_to_return->version != 1) { + perror(">> The version number of the packet is no good."); + return -1; + } + + if (packet_to_return.length + 4 > buffer_size ) { + perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); + return -1; + } + + return 0; } +// We listen forever for new paquets; +void listen_for_packets(){ + + // Create new socket for UDP + int s = socket(AF_INET6, SOCK_DGRAM, 0); + + if(s < 0) { + perror(">> Error, cannot create socket."); + perror(">> Exiting...") + exit(1); + } + + struct sockaddr_in6 server; + memset(&server, 0, sizeof(server)); + + server.sin6_family = AF_INET6; + server.sin6_port = htons(LISTEN_PORT); + int rc = bind(s, (struct sockaddr*)&server, sizeof(server)); + if(rc < 0) { + perror(">> Error, cannot bind socket to choosen port."); + perror(">> Exiting...") + exit(1); + } + + // A paquet has at most a length of 1024 bytes + char req[1024]; + struct sockaddr_in6 client; + struct iovec io = { .iov_len = 1024, .iov_base = req }; + struct msghdr msg_to_receive = { + .msg_name = &client, + .msg_namelen = sizeof(client), + .msg_iov = &io, + .msg_iovlen = 1 + }; + while(1){ + memset(req, '\0', 1024); + + struct sockaddr_in6 client; + unsigned int client_len = sizeof(client); + + rc = recvmsg(s, &msg_to_receive, 0); + if(rc < 0) { + perror(">> Error while receiving a new datagram."); + perror(">> Ignoring, continuing...") + continue; + } + + printf(">> New paquet received :\n"); + printf("%s\n", req); + + // We verify the received packet is well formated, + // and we return it in the struct designed to work with it. + struct packet * formated_rec_datagram; + if(check_header(&req, 1024, formated_rec_datagram) < 0){ + perror(">> Error while checking the header, aborting this packet, by choice, and conviction."); + continue; + } + + struct tlv_list received_tlvs = validate_tlvs(formated_rec_datagram); + } +} + + + + +// If the sender is not in the neighbourhood, and we have 15 neighbours, we +// ignore the packet. Otherwise, we add him to the neighbourhood, marked as +// temporary. +int update_neighbours(){ + +}; + +// We then look at the differents TLVs in the packet. +void work_with_tlvs(){ + +}; + // 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_tlvs(union tlv tlv_to_validate){ +tlv_list validate_tlvs(packet packet_to_validate){ } @@ -54,7 +163,7 @@ void work_with_tlvs(struct tlvs_list receivied_tlvs){ int main(int argc, char const *argv[]) { int continue = 1; - + while(continue){ // We create the neighbourhood table @@ -65,17 +174,8 @@ int main(int argc, char const *argv[]) { // Listen for incoming packets listen_for_packets(); - // For every packet recivied, we fork, - // then we make sure it's conform - // We then extract the data from it to make it easy to work with - check_header(); - // If the sender is not in the neighbourhood, and we have 15 neighbours, we - // ignore the packet. Otherwise, we add him to the neighbourhood, marked as - // temporary. - update_neighbours(); - // We then look at the differents TLVs in the packet. - work_with_tlvs(); + // This is in it's own fork. time_t delay = time(NULL) + 20; while(! (delay < time(NULL)){ // Theses functions are there for general book-keeping,and run in there own diff --git a/src/node.h b/src/node.h index b871865..ad988fd 100644 --- a/src/node.h +++ b/src/node.h @@ -3,6 +3,11 @@ #ifndef NODE_H #define NODE_H +#include "tlv.h" + +// On which port do we listen to +#define LISTEN_PORT 1212 + // The node ID #define NODE_ID 203242402519736214145149136169422092269247115186189140178187251487819615911212154252117172522111472481308026129190139512419121015210238252292031613214452118122204415160254 @@ -40,14 +45,14 @@ typedef struct message { // fonctions signatures void listen_for_packets(); -void check_header(); +int check_header(char * received_datagram[], int len, packet pack); -void update_neighbours(); +int validate_tlvs(packet * pack, tlv_list * tlv_l); + +int update_neighbours(); void work_with_tlvs(); -int validate_tlvs(); - // threaded functions void t_ask_for_more_peers(); diff --git a/src/tlv.h b/src/tlv.h index 33083b8..61df5cb 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -99,6 +99,11 @@ typedef union tlv { warning *warning; } tlv; +typedef struct tlv_list { + union tlv one_tlv; + struct tlv_list * next; +} tlv_list; + // creer un tlv int build_tlv(tlv *tlv, cmd_token token); From 882b75433aa6f3c59c5571e217eefda6f2692103 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 24 Mar 2020 18:27:31 +0100 Subject: [PATCH 2/2] Modif valid tlv --- src/node.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/node.c b/src/node.c index 51b0ba1..c47bc62 100644 --- a/src/node.c +++ b/src/node.c @@ -10,6 +10,11 @@ #include "tlv.h" #include "node.h" +// We need to make sure the TLV announces a length that will no go onto +// another tlv, as we might end up reading bullshit. +tlv_list validate_tlvs(packet packet_to_validate){ + +} // For every packet recivied, // then we make sure it's conform @@ -97,7 +102,11 @@ void listen_for_packets(){ continue; } - struct tlv_list received_tlvs = validate_tlvs(formated_rec_datagram); + struct tlv_list received_tlvs; + if (validate_tlvs(formated_rec_datagram) < 0) { + + /* code */ + } } } @@ -116,11 +125,7 @@ void work_with_tlvs(){ }; -// We need to make sure the TLV announces a length that will no go onto -// another tlv, as we might end up reading bullshit. -tlv_list validate_tlvs(packet packet_to_validate){ -} void work_with_tlvs(struct tlvs_list receivied_tlvs){