This commit is contained in:
gonzalef 2020-03-19 19:39:37 +01:00
parent b6b87e575e
commit a8c32c2be2
2 changed files with 66 additions and 1 deletions

41
parser.h Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
enum cmd_type {
NEIGHBOUR_REQ, NETWORK_STATE_REQ, NODE_STATE_REQ, SEND, ERROR
};
struct cmd_token {
enum cmd_type type;
char arg[193];
};
// retourne le type de commande à exécuter
struct cmd_token parse_cmd() {
char buf[198], cmd[5], arg[193];
struct 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("send", cmd) == 0) {
token.type = SEND;
//arg[192] = 0;
strcpy(token.arg, arg);
}
return token;
}

24
tlv.h
View File

@ -1,5 +1,6 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include "parser.h"
// 8 octets min (struct pointer 4 octets), 1024 octets max
typedef struct packet {
@ -94,3 +95,26 @@ typedef struct warning {
unsigned char length;
char *message;
} warning;
// creer un tlv
void build_tlv(tlv *t) {
struct cmd_token token = parse_cmd();
switch(token.type) {
case NEIGHBOUR_REQ:
// a remplir
break;
case NETWORK_STATE_REQ:
// a remplir
break;
case NODE_STATE_REQ:
// a remplir
break;
case SEND:
// a remplir
break;
case ERROR:
printf("Wrong format, use 'req {neighbour | network state | node state}' or 'send {message}'");
break;
}
}