Skip to content
Snippets Groups Projects
Commit 4d4b4bf3 authored by Fabian Nack's avatar Fabian Nack
Browse files

sys/nhdp: Allocate memory for NHDP's if table static

parent 329d9f5b
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "msg.h" #include "msg.h"
#include "netapi.h" #include "netapi.h"
#include "net/ng_netif.h"
#include "thread.h" #include "thread.h"
#include "utlist.h" #include "utlist.h"
#include "mutex.h" #include "mutex.h"
...@@ -41,7 +42,7 @@ char nhdp_rcv_stack[NHDP_STACK_SIZE]; ...@@ -41,7 +42,7 @@ char nhdp_rcv_stack[NHDP_STACK_SIZE];
static kernel_pid_t nhdp_pid = KERNEL_PID_UNDEF; static kernel_pid_t nhdp_pid = KERNEL_PID_UNDEF;
static kernel_pid_t nhdp_rcv_pid = KERNEL_PID_UNDEF; static kernel_pid_t nhdp_rcv_pid = KERNEL_PID_UNDEF;
static kernel_pid_t helper_pid = KERNEL_PID_UNDEF; static kernel_pid_t helper_pid = KERNEL_PID_UNDEF;
static nhdp_if_entry_t *nhdp_if_entry_head = NULL; static nhdp_if_entry_t nhdp_if_table[NG_NETIF_NUMOF];
static mutex_t send_rcv_mutex = MUTEX_INIT; static mutex_t send_rcv_mutex = MUTEX_INIT;
static sockaddr6_t sa_bcast; static sockaddr6_t sa_bcast;
static int sock_rcv; static int sock_rcv;
...@@ -64,6 +65,12 @@ void nhdp_init(void) ...@@ -64,6 +65,12 @@ void nhdp_init(void)
return; return;
} }
/* Prepare interface table */
for (int i = 0; i < NG_NETIF_NUMOF; i++) {
nhdp_if_table[i].if_pid = KERNEL_PID_UNDEF;
memset(&nhdp_if_table[i].wr_target, 0, sizeof(struct rfc5444_writer_target));
}
/* Initialize reader and writer */ /* Initialize reader and writer */
nhdp_writer_init(); nhdp_writer_init();
nhdp_reader_init(); nhdp_reader_init();
...@@ -98,7 +105,7 @@ int nhdp_register_if_default(kernel_pid_t if_pid, uint8_t *addr, size_t addr_siz ...@@ -98,7 +105,7 @@ int nhdp_register_if_default(kernel_pid_t if_pid, uint8_t *addr, size_t addr_siz
int nhdp_register_if(kernel_pid_t if_pid, uint8_t *addr, size_t addr_size, uint8_t addr_type, int nhdp_register_if(kernel_pid_t if_pid, uint8_t *addr, size_t addr_size, uint8_t addr_type,
uint16_t max_pl_size, uint16_t hello_int_ms, uint16_t val_time_ms) uint16_t max_pl_size, uint16_t hello_int_ms, uint16_t val_time_ms)
{ {
nhdp_if_entry_t *if_entry; nhdp_if_entry_t *if_entry = NULL;
nhdp_addr_t *nhdp_addr; nhdp_addr_t *nhdp_addr;
msg_t signal_msg; msg_t signal_msg;
...@@ -106,63 +113,42 @@ int nhdp_register_if(kernel_pid_t if_pid, uint8_t *addr, size_t addr_size, uint8 ...@@ -106,63 +113,42 @@ int nhdp_register_if(kernel_pid_t if_pid, uint8_t *addr, size_t addr_size, uint8
return -2; return -2;
} }
if_entry = (nhdp_if_entry_t *) malloc(sizeof(nhdp_if_entry_t)); for (int i = 0; i < NG_NETIF_NUMOF; i++) {
if (nhdp_if_table[i].if_pid == KERNEL_PID_UNDEF) {
if (!if_entry) { if_entry = &nhdp_if_table[i];
/* Insufficient memory */ break;
return -1; }
} }
/* Create an interface writer targer for the nhdp_writer */ if (!if_entry) {
if_entry->wr_target = (struct rfc5444_writer_target *) /* Maximum number of registerable interfaces reached */
calloc(1, sizeof(struct rfc5444_writer_target)); return -2;
if (!if_entry->wr_target) {
/* Insufficient memory */
free(if_entry);
return -1;
} }
uint16_t payload_size = max_pl_size > NHDP_MAX_RFC5444_PACKET_SZ uint16_t payload_size = max_pl_size > NHDP_MAX_RFC5444_PACKET_SZ
? NHDP_MAX_RFC5444_PACKET_SZ : max_pl_size; ? NHDP_MAX_RFC5444_PACKET_SZ : max_pl_size;
if_entry->wr_target->packet_buffer = (uint8_t *) calloc(payload_size, sizeof(uint8_t)); if_entry->wr_target.packet_buffer = (uint8_t *) calloc(payload_size, sizeof(uint8_t));
if (!if_entry->wr_target->packet_buffer) { if (!if_entry->wr_target.packet_buffer) {
/* Insufficient memory */ /* Insufficient memory */
free(if_entry->wr_target);
free(if_entry);
return -1; return -1;
} }
if_entry->wr_target->packet_size = payload_size; if_entry->wr_target.packet_size = payload_size;
if_entry->wr_target->sendPacket = write_packet; if_entry->wr_target.sendPacket = write_packet;
/* Get NHDP address entry for the given address */ /* Get NHDP address entry for the given address */
nhdp_addr = nhdp_addr_db_get_address(addr, addr_size, addr_type); nhdp_addr = nhdp_addr_db_get_address(addr, addr_size, addr_type);
if (!nhdp_addr) { if (!nhdp_addr) {
/* Insufficient memory */ /* Insufficient memory */
free(if_entry->wr_target->packet_buffer); free(if_entry->wr_target.packet_buffer);
free(if_entry->wr_target);
free(if_entry);
return -1; return -1;
} }
/* Set Interface's PID */
if_entry->if_pid = if_pid;
/* Set HELLO_INTERVAL and H_HOLD_TIME (validity time) */
if_entry->hello_interval.seconds = 0;
if_entry->hello_interval.microseconds = MS_IN_USEC * hello_int_ms;
if_entry->validity_time.seconds = 0;
if_entry->validity_time.microseconds = MS_IN_USEC * val_time_ms;
timex_normalize(&if_entry->hello_interval);
timex_normalize(&if_entry->validity_time);
/* Add the interface to the LIB */ /* Add the interface to the LIB */
if (lib_add_if_addr(if_entry->if_pid, nhdp_addr) != 0) { if (lib_add_if_addr(if_pid, nhdp_addr) != 0) {
free(if_entry->wr_target->packet_buffer); free(if_entry->wr_target.packet_buffer);
free(if_entry->wr_target);
free(if_entry);
nhdp_decrement_addr_usage(nhdp_addr); nhdp_decrement_addr_usage(nhdp_addr);
return -1; return -1;
} }
...@@ -170,17 +156,24 @@ int nhdp_register_if(kernel_pid_t if_pid, uint8_t *addr, size_t addr_size, uint8 ...@@ -170,17 +156,24 @@ int nhdp_register_if(kernel_pid_t if_pid, uint8_t *addr, size_t addr_size, uint8
/* Create new IIB for the interface */ /* Create new IIB for the interface */
if (iib_register_if(if_pid) != 0) { if (iib_register_if(if_pid) != 0) {
/* TODO: Cleanup lib entry */ /* TODO: Cleanup lib entry */
free(if_entry->wr_target->packet_buffer); free(if_entry->wr_target.packet_buffer);
free(if_entry->wr_target);
free(if_entry);
nhdp_decrement_addr_usage(nhdp_addr); nhdp_decrement_addr_usage(nhdp_addr);
return -1; return -1;
} }
/* Set Interface's PID */
if_entry->if_pid = if_pid;
/* Set HELLO_INTERVAL and H_HOLD_TIME (validity time) */
if_entry->hello_interval.seconds = 0;
if_entry->hello_interval.microseconds = MS_IN_USEC * hello_int_ms;
if_entry->validity_time.seconds = 0;
if_entry->validity_time.microseconds = MS_IN_USEC * val_time_ms;
timex_normalize(&if_entry->hello_interval);
timex_normalize(&if_entry->validity_time);
/* Everything went well */ /* Everything went well */
nhdp_decrement_addr_usage(nhdp_addr); nhdp_decrement_addr_usage(nhdp_addr);
nhdp_writer_register_if(if_entry->wr_target); nhdp_writer_register_if(&if_entry->wr_target);
LL_PREPEND(nhdp_if_entry_head, if_entry);
helper_pid = if_pid; helper_pid = if_pid;
/* Start the receiving thread */ /* Start the receiving thread */
......
...@@ -92,8 +92,7 @@ typedef struct nhdp_if_entry_t { ...@@ -92,8 +92,7 @@ typedef struct nhdp_if_entry_t {
vtimer_t if_timer; /**< Vtimer used for the periodic signaling */ vtimer_t if_timer; /**< Vtimer used for the periodic signaling */
timex_t hello_interval; /**< Interval time for periodic HELLOs */ timex_t hello_interval; /**< Interval time for periodic HELLOs */
timex_t validity_time; /**< Validity time for propagated information */ timex_t validity_time; /**< Validity time for propagated information */
struct rfc5444_writer_target *wr_target; /**< Interface specific writer target */ struct rfc5444_writer_target wr_target; /**< Interface specific writer target */
struct nhdp_if_entry_t *next; /**< Pointer to next list entry */
} nhdp_if_entry_t; } nhdp_if_entry_t;
/** /**
......
...@@ -123,8 +123,8 @@ void nhdp_writer_send_hello(nhdp_if_entry_t *if_entry) ...@@ -123,8 +123,8 @@ void nhdp_writer_send_hello(nhdp_if_entry_t *if_entry)
/* Create HELLO message and send it using the given interface */ /* Create HELLO message and send it using the given interface */
rfc5444_writer_create_message(&nhdp_writer, RFC5444_MSGTYPE_HELLO, rfc5444_writer_create_message(&nhdp_writer, RFC5444_MSGTYPE_HELLO,
rfc5444_writer_singletarget_selector, if_entry->wr_target); rfc5444_writer_singletarget_selector, &if_entry->wr_target);
rfc5444_writer_flush(&nhdp_writer, if_entry->wr_target, false); rfc5444_writer_flush(&nhdp_writer, &if_entry->wr_target, false);
mutex_unlock(&mtx_packet_write); mutex_unlock(&mtx_packet_write);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment