Résolution du merge
Changement de place des fonctions Ajout d'une section pour les fonctions utilitaires
This commit is contained in:
parent
4c2ebba255
commit
728283acdd
370
src/node.c
370
src/node.c
@ -11,226 +11,39 @@
|
|||||||
#include "tlv.h"
|
#include "tlv.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
|
||||||
// We then look at the differents TLVs in the packet.
|
/* ---- Fonctions utilitaires ---- */
|
||||||
int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender){
|
|
||||||
|
|
||||||
// For every TLV,
|
// Get list length
|
||||||
// We make sure the TLV is legal.
|
int len_list(list *l) {
|
||||||
int number_of_good_tlvs = 0;
|
int len = 0;
|
||||||
int padding_to_next_tlv = 0;
|
list *tmp = l;
|
||||||
int end_analysis = 0;
|
|
||||||
while (!end_analysis) {
|
|
||||||
|
|
||||||
// Switch
|
while(tmp != NULL) {
|
||||||
switch (data_from_packet[4 + padding_to_next_tlv]) {
|
tmp = tmp->next;
|
||||||
case 0:
|
len++;
|
||||||
// Padding tlv, we can ignore, pass to next tlv.
|
|
||||||
padding_to_next_tlv++;
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
// PadN TLV, we can ignore, pass to next tlv.
|
|
||||||
// Add the length to get to next TLV
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
// Neighbour Request TLV
|
|
||||||
padding_to_next_tlv += 2;
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
// Neighbour TLV
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
// Network hash TLV
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
|
|
||||||
// TLV Network Hash
|
|
||||||
// We calculate a network hash,
|
|
||||||
|
|
||||||
// We compare both,
|
|
||||||
|
|
||||||
// If they differ, we send a TLV Network State Request
|
|
||||||
// back to the sender.$
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
// Network State Request
|
|
||||||
padding_to_next_tlv += 2;
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
|
|
||||||
// TLV Network State Request
|
|
||||||
// We check our neighbourhood, and for each peer, we send back
|
|
||||||
// to the sender a TLV Node Hash
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
// Node Hash
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
// TLV Node hash
|
|
||||||
// We get a hash _h_ for the node _l_
|
|
||||||
// If we don't have an entry for _l_, or if we have the same one as the
|
|
||||||
// on we just receivied, we send out a TLV Node State Request back.
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
// Node State Request
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
// Node State
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
// TLV Node State
|
|
||||||
// We get a hash _h_, sequence number _s_, data _d_ for node _l_.
|
|
||||||
// We compute a network hash,
|
|
||||||
// We compare the hash, if they differ, then with l',s',d' our data and
|
|
||||||
// h' the corresponding hash,
|
|
||||||
// if _l_ is our own node id, then
|
|
||||||
// if s >> s' then we update our sequence number to s ⊕ 1 mod 2^16
|
|
||||||
// If it's another's node id, then
|
|
||||||
// If there is no entry for the sender,
|
|
||||||
// we store the entry in our data table.
|
|
||||||
break;
|
|
||||||
case 9:
|
|
||||||
// Warning
|
|
||||||
padding_to_next_tlv += data_from_packet[4 + padding_to_next_tlv + 1];
|
|
||||||
number_of_good_tlvs++;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return -number_of_good_tlvs;
|
|
||||||
// We have a faulty TLV, thus we ignore and return.
|
|
||||||
}
|
}
|
||||||
// If we arrived at the end of the packet
|
|
||||||
// or if the length is not possible
|
return len;
|
||||||
if (received_packet.length <= padding_to_next_tlv + 4) {
|
|
||||||
end_analysis = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to make sure the TLV announces a length that will no go onto
|
// Get a random neighbour
|
||||||
// another tlv, as we might end up reading bullshit.
|
neighbour_peer *get_random_neighbour() {
|
||||||
int validate_tlvs(struct packet packet_to_validate){
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// For every packet recivied,
|
/* ---- Fin fonctions utilitaires ---- */
|
||||||
// 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, struct 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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(){
|
|
||||||
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 sender;
|
|
||||||
struct iovec io = { .iov_len = 1024, .iov_base = req };
|
|
||||||
struct msghdr msg_to_receive = {
|
|
||||||
.msg_name = &sender,
|
|
||||||
.msg_namelen = sizeof(sender),
|
|
||||||
.msg_iov = &io,
|
|
||||||
.msg_iovlen = 1
|
|
||||||
};
|
|
||||||
while(1){
|
|
||||||
memset(req, '\0', 1024);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// TODO : Here, we need to fork.
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO : Add the neighbour check here.
|
|
||||||
|
|
||||||
// struct tlv_list received_tlvs;
|
|
||||||
// if (validate_tlvs(formated_rec_datagram) < 0)
|
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
int nbr_success_tlv = work_with_tlvs(formated_rec_datagram, &req, sender);
|
|
||||||
if (nbr_success_tlv < 0){
|
|
||||||
perror(">> Error while treating the TLVs of the packet.");
|
|
||||||
printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv );
|
|
||||||
} else {
|
|
||||||
printf(">> Done working with the TLVs of the packet, listenin for new packets.\n");
|
|
||||||
=======
|
|
||||||
// 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 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.
|
||||||
@ -283,8 +96,43 @@ int validate_tlv(char *data, int pos, short packet_len){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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, struct 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(){
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
// We then look at the differents TLVs in the packet.
|
// We then look at the differents TLVs in the packet.
|
||||||
void work_with_tlvs(char *data, short packet_len){
|
void work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
unsigned char tlv_len;
|
unsigned char tlv_len;
|
||||||
tlv tmp_tlv;
|
tlv tmp_tlv;
|
||||||
@ -371,47 +219,75 @@ void work_with_tlvs(char *data, short packet_len){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We listen forever for new paquets;
|
||||||
|
void listen_for_packets(){
|
||||||
|
|
||||||
|
// Create new socket for UDP
|
||||||
|
int s = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
void work_with_tlvs(struct tlvs_list receivied_tlvs){
|
if(s < 0) {
|
||||||
|
perror(">> Error, cannot create socket.");
|
||||||
// For every TLV,
|
perror(">> Exiting...");
|
||||||
// We make sure the TLV is legal.
|
exit(1);
|
||||||
if(!validate_tlvs(tlv)){
|
|
||||||
perror(">> Invalid TLV receivied, it will be ignored.");
|
|
||||||
>>>>>>> dev-felipe
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get list length
|
|
||||||
int len_list(list *l) {
|
|
||||||
int len = 0;
|
|
||||||
list *tmp = l;
|
|
||||||
|
|
||||||
while(tmp != NULL) {
|
|
||||||
tmp = tmp->next;
|
|
||||||
len++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
struct sockaddr_in6 server;
|
||||||
}
|
memset(&server, 0, sizeof(server));
|
||||||
|
|
||||||
// Get a random neighbour
|
server.sin6_family = AF_INET6;
|
||||||
neighbour_peer *get_random_neighbour() {
|
server.sin6_port = htons(LISTEN_PORT);
|
||||||
// Get a random number
|
int rc = bind(s, (struct sockaddr*)&server, sizeof(server));
|
||||||
time_t t;
|
if(rc < 0) {
|
||||||
srand((unsigned) time(&t));
|
perror(">> Error, cannot bind socket to choosen port.");
|
||||||
int n = rand() % len_list(neighbour_list);
|
perror(">> Exiting...");
|
||||||
|
exit(1);
|
||||||
// Get nth neighbour
|
|
||||||
list *tmp = neighbour_list;
|
|
||||||
|
|
||||||
for(int i=0; i<n; i++) {
|
|
||||||
tmp = tmp->next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (neighbour_peer*) tmp->data;
|
// A paquet has at most a length of 1024 bytes
|
||||||
|
char req[1024];
|
||||||
|
struct sockaddr_in6 sender;
|
||||||
|
struct iovec io = { .iov_len = 1024, .iov_base = req };
|
||||||
|
struct msghdr msg_to_receive = {
|
||||||
|
.msg_name = &sender,
|
||||||
|
.msg_namelen = sizeof(sender),
|
||||||
|
.msg_iov = &io,
|
||||||
|
.msg_iovlen = 1
|
||||||
|
};
|
||||||
|
while(1){
|
||||||
|
memset(req, '\0', 1024);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// TODO : Here, we need to fork.
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : Add the neighbour check here.
|
||||||
|
|
||||||
|
// struct tlv_list received_tlvs;
|
||||||
|
// if (validate_tlvs(formated_rec_datagram) < 0)
|
||||||
|
int nbr_success_tlv = work_with_tlvs(formated_rec_datagram, &req, sender);
|
||||||
|
if (nbr_success_tlv < 0){
|
||||||
|
perror(">> Error while treating the TLVs of the packet.");
|
||||||
|
printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv );
|
||||||
|
} else {
|
||||||
|
printf(">> Done working with the TLVs of the packet, listenin for new packets.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
|
Loading…
Reference in New Issue
Block a user