Added tlv_list and check_header
This commit is contained in:
parent
fafebaa32f
commit
ac657e4bbd
128
src/node.c
128
src/node.c
@ -1,15 +1,124 @@
|
|||||||
// 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 "tlv.h"
|
||||||
|
#include "node.h"
|
||||||
|
|
||||||
|
|
||||||
// Will return a packet when we receive one that's valid.
|
// For every packet recivied,
|
||||||
packet listen_for_packets(){
|
// 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
|
// 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_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 main(int argc, char const *argv[]) {
|
||||||
int continue = 1;
|
int continue = 1;
|
||||||
|
|
||||||
while(continue){
|
while(continue){
|
||||||
|
|
||||||
// We create the neighbourhood table
|
// We create the neighbourhood table
|
||||||
@ -65,17 +174,8 @@ int main(int argc, char const *argv[]) {
|
|||||||
|
|
||||||
// Listen for incoming packets
|
// Listen for incoming packets
|
||||||
listen_for_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;
|
time_t delay = time(NULL) + 20;
|
||||||
while(! (delay < time(NULL)){
|
while(! (delay < time(NULL)){
|
||||||
// Theses functions are there for general book-keeping,and run in there own
|
// Theses functions are there for general book-keeping,and run in there own
|
||||||
|
13
src/node.h
13
src/node.h
@ -3,6 +3,11 @@
|
|||||||
#ifndef NODE_H
|
#ifndef NODE_H
|
||||||
#define NODE_H
|
#define NODE_H
|
||||||
|
|
||||||
|
#include "tlv.h"
|
||||||
|
|
||||||
|
// On which port do we listen to
|
||||||
|
#define LISTEN_PORT 1212
|
||||||
|
|
||||||
// The node ID
|
// The node ID
|
||||||
#define NODE_ID 203242402519736214145149136169422092269247115186189140178187251487819615911212154252117172522111472481308026129190139512419121015210238252292031613214452118122204415160254
|
#define NODE_ID 203242402519736214145149136169422092269247115186189140178187251487819615911212154252117172522111472481308026129190139512419121015210238252292031613214452118122204415160254
|
||||||
|
|
||||||
@ -40,14 +45,14 @@ typedef struct message {
|
|||||||
// fonctions signatures
|
// fonctions signatures
|
||||||
void listen_for_packets();
|
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();
|
void work_with_tlvs();
|
||||||
|
|
||||||
int validate_tlvs();
|
|
||||||
|
|
||||||
// threaded functions
|
// threaded functions
|
||||||
|
|
||||||
void t_ask_for_more_peers();
|
void t_ask_for_more_peers();
|
||||||
|
@ -99,6 +99,11 @@ typedef union tlv {
|
|||||||
warning *warning;
|
warning *warning;
|
||||||
} tlv;
|
} tlv;
|
||||||
|
|
||||||
|
typedef struct tlv_list {
|
||||||
|
union tlv one_tlv;
|
||||||
|
struct tlv_list * next;
|
||||||
|
} tlv_list;
|
||||||
|
|
||||||
|
|
||||||
// creer un tlv
|
// creer un tlv
|
||||||
int build_tlv(tlv *tlv, cmd_token token);
|
int build_tlv(tlv *tlv, cmd_token token);
|
||||||
|
Loading…
Reference in New Issue
Block a user