This commit is contained in:
gonzalef 2020-03-24 16:34:45 +01:00
parent f4d3dcbd6d
commit 0609dc11b9
9 changed files with 367 additions and 0 deletions

1
src/Notes.md Normal file
View File

@ -0,0 +1 @@
# Notes et recherches sur le projet

5
src/README.md Normal file
View File

@ -0,0 +1,5 @@
# dazibao
Le but de ce projet est dimplémenter un dazibao (« journal à grandes lettres »), semblable à
un « mur » de réseau social, mais de façon complètement distribuée. Le protocole est basé sur un
algorithme non-fiable dinondation.

31
src/parser.c Normal file
View File

@ -0,0 +1,31 @@
#include "parser.h"
// retourne le type de commande à exécuter
cmd_token parse_cmd() {
char buf[198], cmd[5], arg[193];
cmd_token token;
token.type = ERROR;
memset(token.arg, 0, 193);
if(fgets(buf, 198, stdin) == NULL)
return token;
// cmd sera le premier mot rencontré et arg la suite de mots après celui ci, si les deux variables ne sont pas remplies alors il y a une erreur
if(sscanf(buf, "%s %[^\t\n]", cmd, arg) != 2)
return token;
if(strcmp("req", cmd) == 0) {
if(strcmp("neighbour", arg) == 0)
token.type = NEIGHBOUR_REQ;
else if(strcmp("network state", arg) == 0)
token.type = NETWORK_STATE_REQ;
else if(strcmp("node state", arg) == 0)
token.type = NODE_STATE_REQ;
} else if(strcmp("post", cmd) == 0) {
token.type = POST;
//arg[192] = 0;
strcpy(token.arg, arg);
}
return token;
}

14
src/parser.h Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <string.h>
typedef enum cmd_type {
NEIGHBOUR_REQ, NETWORK_STATE_REQ, NODE_STATE_REQ, POST, ERROR
} cmd_type;
typedef struct cmd_token {
cmd_type type;
char arg[193];
} cmd_token;
// retourne le type de commande à exécuter
cmd_token parse_cmd();

BIN
src/test Executable file

Binary file not shown.

15
src/test.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char a[1];
a[0] = 0;
if(fork() == 0) {
while(1)
printf("%d\n", a[0]);
} else {
a[1] = 1;
}
}

188
src/tlv.c Normal file
View File

@ -0,0 +1,188 @@
#include "tlv.h"
// creer un tlv
int build_tlv(tlv *tlv, cmd_token token) {
switch(token.type) {
case NEIGHBOUR_REQ:
// a remplir
break;
case NETWORK_STATE_REQ:
// a remplir
break;
case NODE_STATE_REQ:
// a remplir
break;
case POST:
// a remplir
break;
case ERROR:
printf("Wrong format, use 'req {neighbour | network state | node state}' or 'post {message}'");
break;
}
}
int build_pad1(tlv *tlv) {
pad1 *new = (pad1*) malloc(sizeof(pad1));
if(new == NULL)
return -1;
new->type = 0;
tlv->pad1 = new;
return 0;
}
int build_padn(tlv *tlv, size_t len) {
padn *new = (padn*) malloc(sizeof(padn));
if(new == NULL)
return -1;
new->type = 1;
new->length = len;
new->mbz = (char*) calloc(sizeof(char), len);
tlv->padn = new;
return 0;
}
int build_neighbour_req(tlv *tlv) {
neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req));
if(new == NULL)
return -1;
new->type = 2;
new->length = 0;
tlv->neighbour_req = new;
return 0;
}
int build_neighbour(tlv *tlv, struct in6_addr ip, short port) {
neighbour *new = (neighbour*) malloc(sizeof(neighbour));
if(new == NULL)
return -1;
new->type = 3;
new->length = 18;
new->ip = ip;
new->port = port;
tlv->neighbour = new;
return 0;
}
int build_network_hash(tlv *tlv, char *hash) {
network_hash *new = (network_hash*) malloc(sizeof(network_hash));
if(new == NULL)
return -1;
new->type = 4;
new->length = 16;
memcpy(new->network_hash, hash, 16);
tlv->network_hash = new;
return 0;
}
int build_network_state_req(tlv *tlv) {
network_state_req *new = (network_state_req*) malloc(sizeof(network_state_req));
if(new == NULL)
return -1;
new->type = 5;
new->length = 0;
tlv->network_state_req = new;
return 0;
}
int build_node_hash(tlv *tlv, long node_id, short seqno, char *hash) {
node_hash *new = (node_hash*) malloc(sizeof(node_hash));
if(new == NULL)
return -1;
new->type = 6;
new->length = 26;
new->node_id = node_id;
new->seqno = seqno;
memcpy(new->node_hash, hash, 16);
tlv->node_hash = new;
return 0;
}
int build_node_state_req(tlv *tlv, long node_id) {
node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req));
if(new == NULL)
return -1;
new->type = 7;
new->length = 8;
new->node_id = node_id;
tlv->node_state_req = new;
return 0;
}
int build_node_state(tlv *tlv, long node_id, short seqno, char *node_hash, char *data) {
node_state *new = (node_state*) malloc(sizeof(node_state));
int len = strlen(data);
if(new == NULL)
return -1;
// en mettant cet octet à 0 on est surs de traiter un champ data de taille 192 max
if(len > 192) {
data[192] = 0;
len = 192;
}
new->type = 8;
new->length = 26 + len;
new->node_id = node_id;
new->seqno = seqno;
memcpy(new->node_hash, node_hash, 16);
memcpy(new->data, data, len);
tlv->node_state = new;
return 0;
}
int build_warning(tlv *tlv, char *message) {
warning *new = (warning*) malloc(sizeof(warning));
int len = strlen(message);
if(new == NULL)
return -1;
// en mettant cet octet à 0 on est surs de traiter un champ message de taille 256 max
if(len > 256) {
message[256] = 0;
len = 256;
}
new->type = 9;
new->length = len;
memcpy(new->message, message, len);
tlv->warning = new;
return 0;
}

113
src/tlv.h Normal file
View File

@ -0,0 +1,113 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include "parser.h"
// 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;
// 1 octet
typedef struct pad1 {
unsigned char type;
} pad1;
// 2 octets min, 258 octets max (unsigned char 0 -> 255)
typedef struct padn {
unsigned char type;
unsigned char length;
char mbz[256];
} padn;
// 2 octets
typedef struct neighbour_req {
unsigned char type;
unsigned char length;
} neighbour_req;
// 20 octets
typedef struct neighbour {
unsigned char type;
unsigned char length;
struct in6_addr ip;
short port;
} neighbour;
// 18 octets
typedef struct network_hash {
unsigned char type;
unsigned char length;
char network_hash[16];
} network_hash;
// 2 octets
typedef struct network_state_req {
unsigned char type;
unsigned char length;
} network_state_req;
// 28 octets
typedef struct node_hash {
unsigned char type;
unsigned char length;
long node_id;
short seqno;
char node_hash[16];
} node_hash;
// 10 octets
typedef struct node_state_req {
unsigned char type;
unsigned char length;
long node_id;
} node_state_req;
// 28 octets min, 220 octets max (data 0 -> 192)
typedef struct node_state {
unsigned char type;
unsigned char length;
long node_id;
short seqno;
char node_hash[16];
char data[192];
} node_state;
// 2 octets min, 258 ocets max (unsigned char 0 -> 255)
typedef struct warning {
unsigned char type;
unsigned char length;
char *message;
} warning;
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;
// creer un tlv
int build_tlv(tlv *tlv, cmd_token token);
// 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);

BIN
src/énoncé.pdf Normal file

Binary file not shown.