1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

gnrc: add GNRC-specific conn implementation

This commit is contained in:
Martine Lenders 2015-08-12 05:41:02 +02:00 committed by Martine Lenders
parent b87a3eab89
commit ef9acf6aee
12 changed files with 546 additions and 0 deletions

View File

@ -10,6 +10,14 @@ ifneq (,$(filter gnrc_%,$(filter-out gnrc_netapi gnrc_netreg gnrc_netif% gnrc_pk
USEMODULE += gnrc
endif
ifneq (,$(filter gnrc_conn_%,$(USEMODULE)))
USEMODULE += gnrc_conn
endif
ifneq (,$(filter gnrc_conn_udp,$(USEMODULE)))
USEMODULE += gnrc_udp
endif
ifneq (,$(filter schedstatistics,$(USEMODULE)))
USEMODULE += xtimer
endif

View File

@ -23,6 +23,10 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef MODULE_GNRC_CONN_IP
#include "net/gnrc/conn.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif

View File

@ -23,6 +23,10 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef MODULE_GNRC_CONN_TCP
#include "net/gnrc/conn.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif

View File

@ -23,6 +23,10 @@
#include <stdint.h>
#include <stdlib.h>
#ifdef MODULE_GNRC_CONN_UDP
#include "net/gnrc/conn.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif

130
sys/include/net/gnrc/conn.h Normal file
View File

@ -0,0 +1,130 @@
/*
* 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.
*/
/**
* @defgroup net_gnrc_conn GNRC-specific @ref net_conn implementation
* @ingroup net_gnrc
* @brief GNRC-specific @ref net_conn implementation
* @{
*
* @file
* @brief GNRC-specific types and function definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef GNRC_CONN_H_
#define GNRC_CONN_H_
#include <stdbool.h>
#include <stdint.h>
#include "net/ipv6/addr.h"
#include "net/gnrc.h"
#include "sched.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Connection base class
* @internal
*/
typedef struct {
gnrc_nettype_t l3_type; /**< Network layer type of the connection */
gnrc_nettype_t l4_type; /**< Transport layer type of the connection */
gnrc_netreg_entry_t netreg_entry; /**< @p net_ng_netreg entry for the connection */
} conn_t;
/**
* @brief Raw connection type
* @internal
* @extends conn_t
*/
struct conn_ip {
gnrc_nettype_t l3_type; /**< Network layer type of the connection. */
gnrc_nettype_t l4_type; /**< Transport layer type of the connection.
* Always GNRC_NETTYPE_UNDEF */
gnrc_netreg_entry_t netreg_entry; /**< @p net_ng_netreg entry for the connection */
uint8_t local_addr[sizeof(ipv6_addr_t)]; /**< local IP address */
size_t local_addr_len; /**< length of struct conn_ip::local_addr */
};
/**
* @brief UDP connection type
* @internal
* @extends conn_t
*/
struct conn_udp {
gnrc_nettype_t l3_type; /**< Network layer type of the connection.
* Always GNRC_NETTYPE_IPV6 */
gnrc_nettype_t l4_type; /**< Transport layer type of the connection.
* Always GNRC_NETTYPE_UDP */
gnrc_netreg_entry_t netreg_entry; /**< @p net_ng_netreg entry for the connection */
uint8_t local_addr[sizeof(ipv6_addr_t)]; /**< local IP address */
size_t local_addr_len; /**< length of struct conn_ip::local_addr */
};
/**
* @brief Bind connection to demux context
*
* @internal
*
* @param[out] entry @ref net_ng_netreg entry.
* @param[in] type @ref net_ng_nettype.
* @param[in] demux_ctx demux context (port or proto) for the connection.
*/
static inline void gnrc_conn_reg(gnrc_netreg_entry_t *entry, gnrc_nettype_t type,
uint32_t demux_ctx)
{
entry->pid = sched_active_pid;
entry->demux_ctx = demux_ctx;
gnrc_netreg_register(type, entry);
}
/**
* @brief Sets local address for a connection
*
* @internal
*
* @param[out] conn_addr Pointer to the local address on the connection.
* @param[in] addr An IPv6 address.
*
* @return true, if @p addr was a legal address (`::`, `::1` or an address assigned to any
* interface of this node) for the connection.
* @return false if @p addr was not a legal address for the connection.
*/
bool gnrc_conn6_set_local_addr(uint8_t *conn_addr, const ipv6_addr_t *addr);
/**
* @brief Generic recvfrom
*
* @internal
*
* @param[in] conn Connection object.
* @param[out] data Pointer where the received data should be stored.
* @param[in] max_len Maximum space available at @p data.
* @param[out] addr NULL pointer or the sender's IP address. Must fit address of connection's
* family if not NULL.
* @param[out] addr_len Length of @p addr. May be NULL if @p addr is NULL.
* @param[out] port NULL pointer or the sender's port.
*
* @return The number of bytes received on success.
* @return 0, if no received data is available, but everything is in order.
* @return -ENOMEM, if received data was more than max_len.
* @returne -ETIMEDOUT, if more than 3 IPC messages were not @ref net_ng_netapi receive commands
* with the required headers in the packet
*/
int gnrc_conn_recvfrom(conn_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len,
uint16_t *port);
#ifdef __cplusplus
}
#endif
#endif /* GNRC_CONN_H_ */
/** @} */

View File

@ -1,3 +1,12 @@
ifneq (,$(filter gnrc_conn,$(USEMODULE)))
DIRS += conn
endif
ifneq (,$(filter gnrc_conn_ip,$(USEMODULE)))
DIRS += conn/ip
endif
ifneq (,$(filter gnrc_conn_udp,$(USEMODULE)))
DIRS += conn/udp
endif
ifneq (,$(filter gnrc_icmpv6,$(USEMODULE)))
DIRS += network_layer/icmpv6
endif

View File

@ -0,0 +1,3 @@
MODULE = gnrc_conn
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,115 @@
/*
* 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
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include "net/conn.h"
#include "net/ipv6/hdr.h"
#include "net/gnrc/conn.h"
#include "net/gnrc/ipv6/hdr.h"
#include "net/gnrc/ipv6/netif.h"
#include "net/udp.h"
static inline size_t _srcaddr(void *addr, gnrc_pktsnip_t *hdr)
{
switch (hdr->type) {
#ifdef MODULE_GNRC_IPV6
case GNRC_NETTYPE_IPV6:
memcpy(addr, &((ipv6_hdr_t *)hdr->data)->src, sizeof(ipv6_addr_t));
return sizeof(ipv6_addr_t);
#endif
default:
(void)addr;
return 0;
}
}
static inline void _srcport(uint16_t *port, gnrc_pktsnip_t *hdr)
{
switch (hdr->type) {
#ifdef MODULE_GNRC_UDP
case GNRC_NETTYPE_UDP:
memcpy(port, &((udp_hdr_t *)hdr->data)->src_port, sizeof(uint16_t));
break;
#endif
default:
(void)port;
(void)hdr;
break;
}
}
int gnrc_conn_recvfrom(conn_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len,
uint16_t *port)
{
msg_t msg;
int timeout = 3;
while ((timeout--) > 0) {
gnrc_pktsnip_t *pkt, *l3hdr;
msg_receive(&msg);
switch (msg.type) {
case GNRC_NETAPI_MSG_TYPE_RCV:
pkt = (gnrc_pktsnip_t *)msg.content.ptr;
if (pkt->size > max_len) {
return -ENOMEM;
}
LL_SEARCH_SCALAR(pkt, l3hdr, type, conn->l3_type);
if (l3hdr == NULL) {
msg_send_to_self(&msg); /* requeue invalid messages */
continue;
}
#if defined(MODULE_CONN_UDP) || defined(MODULE_CONN_TCP)
if ((conn->l4_type != GNRC_NETTYPE_UNDEF) && (port != NULL)) {
gnrc_pktsnip_t *l4hdr;
LL_SEARCH_SCALAR(pkt, l4hdr, type, conn->l4_type);
if (l4hdr == NULL) {
msg_send_to_self(&msg); /* requeue invalid messages */
continue;
}
_srcport(port, l4hdr);
}
#endif /* defined(MODULE_CONN_UDP) */
if (addr != NULL) {
*addr_len = _srcaddr(addr, l3hdr);
}
memcpy(data, pkt->data, pkt->size);
return pkt->size;
default:
(void)port;
msg_send_to_self(&msg); /* requeue invalid messages */
break;
}
}
return -ETIMEDOUT;
}
#ifdef MODULE_GNRC_IPV6
bool gnrc_conn6_set_local_addr(uint8_t *conn_addr, const ipv6_addr_t *addr)
{
ipv6_addr_t *tmp;
if (!ipv6_addr_is_unspecified(addr) &&
!ipv6_addr_is_loopback(addr) &&
gnrc_ipv6_netif_find_by_addr(&tmp, addr) == KERNEL_PID_UNDEF) {
return false;
}
else if (ipv6_addr_is_loopback(addr) || ipv6_addr_is_unspecified(addr)) {
ipv6_addr_set_unspecified((ipv6_addr_t *)conn_addr);
}
else {
memcpy(conn_addr, addr, sizeof(ipv6_addr_t));
}
return true;
}
#endif
/** @} */

View File

@ -0,0 +1,3 @@
MODULE = gnrc_conn_ip
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,128 @@
/*
* 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
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include <errno.h>
#include "net/af.h"
#include "net/gnrc/conn.h"
#include "net/gnrc/ipv6.h"
#include "net/conn/ip.h"
int conn_ip_create(conn_ip_t *conn, const void *addr, size_t addr_len, int family, int proto)
{
switch (family) {
#ifdef MODULE_GNRC_IPV6
case AF_INET6:
if (addr_len != sizeof(ipv6_addr_t)) {
return -EINVAL;
}
if (gnrc_conn6_set_local_addr(conn->local_addr, addr)) {
conn->l3_type = GNRC_NETTYPE_IPV6;
conn->local_addr_len = addr_len;
conn_ip_close(conn); /* unregister possibly registered netreg entry */
gnrc_conn_reg(&conn->netreg_entry, conn->l3_type, (uint32_t)proto);
}
else {
return -EADDRNOTAVAIL;
}
break;
#endif
default:
(void)addr;
(void)addr_len;
(void)proto;
return -EAFNOSUPPORT;
}
conn->l4_type = GNRC_NETTYPE_UNDEF;
return 0;
}
void conn_ip_close(conn_ip_t *conn)
{
assert(conn->l4_type == GNRC_NETTYPE_UNDEF);
if (conn->netreg_entry.pid != KERNEL_PID_UNDEF) {
gnrc_netreg_unregister(conn->l3_type, &conn->netreg_entry);
}
}
int conn_ip_getlocaladdr(conn_ip_t *conn, void *addr)
{
assert(conn->l4_type == GNRC_NETTYPE_UNDEF);
memcpy(addr, &conn->local_addr, conn->local_addr_len);
return conn->local_addr_len;
}
int conn_ip_recvfrom(conn_ip_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len)
{
assert(conn->l4_type == GNRC_NETTYPE_UNDEF);
switch (conn->l3_type) {
#ifdef MODULE_GNRC_IPV6
case GNRC_NETTYPE_IPV6:
return gnrc_conn_recvfrom((conn_t *)conn, data, max_len, addr, addr_len, NULL);
#endif
default:
(void)data;
(void)max_len;
(void)addr;
(void)addr_len;
return -EBADF;
}
}
int conn_ip_sendto(const void *data, size_t len, const void *src, size_t src_len,
void *dst, size_t dst_len, int family, int proto)
{
gnrc_pktsnip_t *pkt, *hdr = NULL;
gnrc_nettype_t l3_type;
pkt = gnrc_pktbuf_add(NULL, (void *)data, len, GNRC_NETTYPE_UNDEF); /* data will only be copied */
switch (family) {
#ifdef MODULE_GNRC_IPV6
case AF_INET6:
if (((src != NULL) && (src_len != sizeof(ipv6_addr_t))) ||
(dst_len != sizeof(ipv6_addr_t)) ||
(((unsigned)proto) > 256U)) {
gnrc_pktbuf_release(pkt);
return -EINVAL;
}
/* addr will only be copied */
hdr = gnrc_ipv6_hdr_build(pkt, (uint8_t *)src, src_len, (uint8_t *)dst, dst_len);
if (hdr == NULL) {
gnrc_pktbuf_release(pkt);
return -ENOMEM;
}
/* set next header to connection's proto */
ipv6_hdr_t *ipv6_hdr = hdr->data;
ipv6_hdr->nh = (uint8_t)proto;
pkt = hdr;
l3_type = GNRC_NETTYPE_IPV6;
break;
#endif /* MODULE_GNRC_IPV6 */
default:
(void)src;
(void)src_len;
(void)dst;
(void)dst_len;
(void)proto;
(void)hdr;
gnrc_pktbuf_release(pkt);
return -EAFNOSUPPORT;
}
gnrc_netapi_dispatch_send(l3_type, GNRC_NETREG_DEMUX_CTX_ALL, pkt);
return len;
}

View File

@ -0,0 +1,3 @@
MODULE = gnrc_conn_udp
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,135 @@
/*
* 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
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include <errno.h>
#include "net/af.h"
#include "net/gnrc/conn.h"
#include "net/gnrc/ipv6.h"
#include "net/gnrc/udp.h"
#include "net/conn/udp.h"
int conn_udp_create(conn_udp_t *conn, const void *addr, size_t addr_len,
int family, uint16_t port)
{
conn->l4_type = GNRC_NETTYPE_UDP;
switch (family) {
#ifdef MODULE_GNRC_IPV6
case AF_INET6:
if (addr_len != sizeof(ipv6_addr_t)) {
return -EINVAL;
}
if (gnrc_conn6_set_local_addr(conn->local_addr, addr)) {
conn->l3_type = GNRC_NETTYPE_IPV6;
conn->local_addr_len = addr_len;
conn_udp_close(conn); /* unregister possibly registered netreg entry */
gnrc_conn_reg(&conn->netreg_entry, conn->l4_type, (uint32_t)port);
}
else {
return -EADDRNOTAVAIL;
}
break;
#endif
default:
(void)addr;
(void)addr_len;
(void)port;
return -EAFNOSUPPORT;
}
return 0;
}
void conn_udp_close(conn_udp_t *conn)
{
assert(conn->l4_type == GNRC_NETTYPE_UDP);
if (conn->netreg_entry.pid != KERNEL_PID_UNDEF) {
gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &conn->netreg_entry);
}
}
int conn_udp_getlocaladdr(conn_udp_t *conn, void *addr, uint16_t *port)
{
assert(conn->l4_type == GNRC_NETTYPE_UDP);
memcpy(addr, &conn->local_addr, conn->local_addr_len);
*port = (uint16_t)conn->netreg_entry.demux_ctx;
return conn->local_addr_len;
}
int conn_udp_recvfrom(conn_udp_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len,
uint16_t *port)
{
assert(conn->l4_type == GNRC_NETTYPE_UDP);
switch (conn->l3_type) {
#ifdef MODULE_GNRC_IPV6
case GNRC_NETTYPE_IPV6:
return gnrc_conn_recvfrom((conn_t *)conn, data, max_len, addr, addr_len, port);
#endif
default:
(void)data;
(void)max_len;
(void)addr;
(void)addr_len;
(void)port;
return -EBADF;
}
}
int conn_udp_sendto(const void *data, size_t len, const void *src, size_t src_len,
const void *dst, size_t dst_len, int family, uint16_t sport,
uint16_t dport)
{
gnrc_pktsnip_t *pkt, *hdr = NULL;
pkt = gnrc_pktbuf_add(NULL, (void *)data, len, GNRC_NETTYPE_UNDEF); /* data will only be copied */
hdr = gnrc_udp_hdr_build(pkt, (uint8_t *)&sport, sizeof(uint16_t), (uint8_t *)&dport,
sizeof(uint16_t));
if (hdr == NULL) {
gnrc_pktbuf_release(pkt);
return -ENOMEM;
}
pkt = hdr;
switch (family) {
#ifdef MODULE_GNRC_IPV6
case AF_INET6:
if (((src != NULL) && (src_len != sizeof(ipv6_addr_t))) ||
(dst_len != sizeof(ipv6_addr_t))) {
gnrc_pktbuf_release(pkt);
return -EINVAL;
}
/* addr will only be copied */
hdr = gnrc_ipv6_hdr_build(pkt, (uint8_t *)src, src_len, (uint8_t *)dst, dst_len);
if (hdr == NULL) {
gnrc_pktbuf_release(pkt);
return -ENOMEM;
}
pkt = hdr;
break;
#endif /* MODULE_GNRC_IPV6 */
default:
(void)hdr;
(void)src;
(void)src_len;
(void)dst;
(void)dst_len;
gnrc_pktbuf_release(pkt);
return -EAFNOSUPPORT;
}
gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, pkt);
return len;
}
/** @} */