#include "tlv.h" int build_pad1(tlv *tlv) { // Free the previously allocated memory free(tlv->pad1); pad1 *new = (pad1*) malloc(sizeof(pad1)); memset(new, 0, sizeof(pad1)); if(new == NULL) return -1; new->type = 0; tlv->pad1 = new; return 0; } int build_padn(tlv *tlv, size_t len) { // Free the previously allocated memory free(tlv->pad1); padn *new = (padn*) malloc(sizeof(padn)); memset(new, 0, sizeof(padn)); if(new == NULL) return -1; new->type = 1; new->length = len; memset(new->mbz, 0, 256); tlv->padn = new; return 0; } int build_neighbour_req(tlv *tlv) { // Free the previously allocated memory free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); memset(new, 0, 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, int16_t port) { // Free the previously allocated memory // TODO : why do we free this part ? Doesn't it mean that, once the // tlv has entered this function, it's not on the heap anymore, // and thus, setting a value will not be accessible from other part of the // code ? free(tlv->pad1); neighbour *new = (neighbour*) malloc(sizeof(neighbour)); memset(new, 0, sizeof(neighbour)); if(new == NULL) return -1; new->type = 3; new->length = 18; new->ip = ip; new->port = htobe16(port); tlv->neighbour = new; return 0; } int build_network_hash(tlv *tlv, list *data_list) { // Free the previously allocated memory free(tlv->pad1); network_hash *new = (network_hash*) malloc(sizeof(network_hash)); memset(new, 0, sizeof(network_hash)); if(new == NULL) return -1; new->type = 4; new->length = 16; hash_network(data_list, new->network_hash); tlv->network_hash = new; return 0; } int build_network_state_req(tlv *tlv) { // Free the previously allocated memory free(tlv->pad1); network_state_req *new = (network_state_req*) malloc(sizeof(network_state_req)); memset(new,0,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, uint64_t node_id, uint16_t seqno, char *data, unsigned char length) { // Free the previously allocated memory free(tlv->pad1); node_hash *new = (node_hash*) malloc(sizeof(node_hash)); memset(new,0,sizeof(node_hash)); if(new == NULL) return -1; new->type = 6; new->length = 26; new->node_id = htobe64(node_id); new->seqno = htobe16(seqno); pub_data pdata = (pub_data) {.length = length, .id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, new->node_hash); tlv->node_hash = new; return 0; } int build_node_state_req(tlv *tlv, uint64_t node_id) { // Free the previously allocated memory free(tlv->pad1); node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); memset(new, 0, sizeof(node_state_req)); if(new == NULL) return -1; new->type = 7; new->length = 8; new->node_id = htobe64(node_id); tlv->node_state_req = new; return 0; } int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); node_state *new = (node_state*) malloc(sizeof(node_state)); memset(new, 0, sizeof(node_state)); if(new == NULL) return -1; new->type = 8; new->length = data_len + 26; new->node_id = htobe64(node_id); new->seqno = htobe16(seqno); memcpy(new->data, data, data_len); pub_data pdata = (pub_data) {.length = data_len, .id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, new->node_hash); tlv->node_state = new; return 0; } int build_warning(tlv *tlv, char *message, size_t message_len) { // Free the previously allocated memory free(tlv->pad1); warning *new = (warning*) malloc(sizeof(warning)); memset(new, 0, sizeof(warning)); int len = message_len; if(new == NULL) return -1; new->type = 9; new->length = len; memcpy(new->message, message, len); tlv->warning = new; return 0; }