#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; }