2015-08-17 15:41:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-03-21 12:53:00 +01:00
|
|
|
#include "assert.h"
|
2017-06-14 13:25:29 +02:00
|
|
|
#include "log.h"
|
2015-08-17 15:41:29 +02:00
|
|
|
#include "utlist.h"
|
|
|
|
#include "net/gnrc/netreg.h"
|
|
|
|
#include "net/gnrc/nettype.h"
|
|
|
|
#include "net/gnrc/pkt.h"
|
|
|
|
#include "net/gnrc/icmpv6.h"
|
|
|
|
#include "net/gnrc/ipv6.h"
|
|
|
|
#include "net/gnrc/udp.h"
|
2020-08-20 07:16:18 +02:00
|
|
|
#ifdef MODULE_GNRC_TCP
|
2016-02-04 14:37:35 +01:00
|
|
|
#include "net/gnrc/tcp.h"
|
2020-08-20 07:16:18 +02:00
|
|
|
#endif
|
2015-08-17 15:41:29 +02:00
|
|
|
|
|
|
|
#define _INVALID_TYPE(type) (((type) < GNRC_NETTYPE_UNDEF) || ((type) >= GNRC_NETTYPE_NUMOF))
|
|
|
|
|
|
|
|
/* The registry as lookup table by gnrc_nettype_t */
|
|
|
|
static gnrc_netreg_entry_t *netreg[GNRC_NETTYPE_NUMOF];
|
|
|
|
|
|
|
|
void gnrc_netreg_init(void)
|
|
|
|
{
|
|
|
|
/* set all pointers in registry to NULL */
|
|
|
|
memset(netreg, 0, GNRC_NETTYPE_NUMOF * sizeof(gnrc_netreg_entry_t *));
|
|
|
|
}
|
|
|
|
|
|
|
|
int gnrc_netreg_register(gnrc_nettype_t type, gnrc_netreg_entry_t *entry)
|
|
|
|
{
|
2018-11-15 14:06:57 +01:00
|
|
|
#if DEVELHELP
|
|
|
|
# if defined(MODULE_GNRC_NETAPI_MBOX) || defined(MODULE_GNRC_NETAPI_CALLBACKS)
|
2017-06-14 13:25:29 +02:00
|
|
|
bool has_msg_q = (entry->type != GNRC_NETREG_TYPE_DEFAULT) ||
|
2020-08-23 21:25:54 +02:00
|
|
|
thread_has_msg_queue(thread_get(entry->target.pid));
|
2018-11-15 14:06:57 +01:00
|
|
|
# else
|
2020-08-23 21:25:54 +02:00
|
|
|
bool has_msg_q = thread_has_msg_queue(thread_get(entry->target.pid));
|
2018-11-15 14:06:57 +01:00
|
|
|
# endif
|
2017-06-14 13:25:29 +02:00
|
|
|
|
2016-06-07 13:31:32 +02:00
|
|
|
/* only threads with a message queue are allowed to register at gnrc */
|
2017-06-14 13:25:29 +02:00
|
|
|
if (!has_msg_q) {
|
|
|
|
LOG_ERROR("\n!!!! gnrc_netreg: initialize message queue of thread %u "
|
|
|
|
"using msg_init_queue() !!!!\n\n", entry->target.pid);
|
|
|
|
}
|
|
|
|
assert(has_msg_q);
|
2018-11-15 14:06:57 +01:00
|
|
|
#endif /* DEVELHELP */
|
2015-09-30 19:12:27 +02:00
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
if (_INVALID_TYPE(type)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_PREPEND(netreg[type], entry);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gnrc_netreg_unregister(gnrc_nettype_t type, gnrc_netreg_entry_t *entry)
|
|
|
|
{
|
|
|
|
if (_INVALID_TYPE(type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_DELETE(netreg[type], entry);
|
|
|
|
}
|
|
|
|
|
2018-02-06 21:37:39 +01:00
|
|
|
/**
|
|
|
|
* @brief Searches the next entry in the registry that matches given
|
|
|
|
* parameters, start lookup from beginning or given entry.
|
|
|
|
*
|
|
|
|
* @param[in] from A registry entry to lookup from or NULL to start fresh
|
|
|
|
* @param[in] type Type of the protocol.
|
|
|
|
* @param[in] demux_ctx The demultiplexing context for the registered thread.
|
|
|
|
* See gnrc_netreg_entry_t::demux_ctx.
|
|
|
|
*
|
|
|
|
* @return The first entry fitting the given parameters on success
|
|
|
|
* @return NULL if no entry can be found.
|
|
|
|
*/
|
|
|
|
static gnrc_netreg_entry_t *_netreg_lookup(gnrc_netreg_entry_t *from,
|
|
|
|
gnrc_nettype_t type,
|
|
|
|
uint32_t demux_ctx)
|
2015-08-17 15:41:29 +02:00
|
|
|
{
|
2018-02-06 21:37:39 +01:00
|
|
|
gnrc_netreg_entry_t *res = NULL;
|
2015-08-17 15:41:29 +02:00
|
|
|
|
2018-02-06 21:37:39 +01:00
|
|
|
if (from || !_INVALID_TYPE(type)) {
|
|
|
|
gnrc_netreg_entry_t *head = (from) ? from->next : netreg[type];
|
|
|
|
LL_SEARCH_SCALAR(head, res, demux_ctx, demux_ctx);
|
2015-08-17 15:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-02-06 21:37:39 +01:00
|
|
|
gnrc_netreg_entry_t *gnrc_netreg_lookup(gnrc_nettype_t type, uint32_t demux_ctx)
|
|
|
|
{
|
|
|
|
return _netreg_lookup(NULL, type, demux_ctx);
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:28:32 +01:00
|
|
|
int gnrc_netreg_num(gnrc_nettype_t type, uint32_t demux_ctx)
|
|
|
|
{
|
|
|
|
int num = 0;
|
2018-02-22 20:39:23 +01:00
|
|
|
gnrc_netreg_entry_t *entry = NULL;
|
2018-02-22 20:28:32 +01:00
|
|
|
|
2018-02-22 20:39:23 +01:00
|
|
|
while((entry = _netreg_lookup(entry, type, demux_ctx)) != NULL) {
|
|
|
|
num++;
|
2018-02-22 20:28:32 +01:00
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
gnrc_netreg_entry_t *gnrc_netreg_getnext(gnrc_netreg_entry_t *entry)
|
|
|
|
{
|
2018-02-06 21:37:39 +01:00
|
|
|
return (entry ? _netreg_lookup(entry, 0, entry->demux_ctx) : NULL);
|
2015-08-17 15:41:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int gnrc_netreg_calc_csum(gnrc_pktsnip_t *hdr, gnrc_pktsnip_t *pseudo_hdr)
|
|
|
|
{
|
|
|
|
if (pseudo_hdr == NULL) {
|
|
|
|
/* XXX: Might be allowed for future checksums.
|
|
|
|
* If this is the case: move this to the branches were it
|
|
|
|
* is needed. */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (hdr->type) {
|
|
|
|
#ifdef MODULE_GNRC_ICMPV6
|
|
|
|
case GNRC_NETTYPE_ICMPV6:
|
|
|
|
return gnrc_icmpv6_calc_csum(hdr, pseudo_hdr);
|
|
|
|
#endif
|
|
|
|
#ifdef MODULE_GNRC_TCP
|
|
|
|
case GNRC_NETTYPE_TCP:
|
|
|
|
return gnrc_tcp_calc_csum(hdr, pseudo_hdr);
|
|
|
|
#endif
|
|
|
|
#ifdef MODULE_GNRC_UDP
|
|
|
|
case GNRC_NETTYPE_UDP:
|
|
|
|
return gnrc_udp_calc_csum(hdr, pseudo_hdr);
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|