1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

conn: Initial import of a light-weight network application API

This commit is contained in:
Martine Lenders 2015-07-21 14:46:40 +02:00
parent 35b1a30b78
commit b87a3eab89
6 changed files with 498 additions and 0 deletions

View File

@ -302,6 +302,22 @@ ifneq (,$(filter oonf_common,$(USEMODULE)))
USEMODULE += socket_base
endif
ifneq (,$(filter %_conn_ip,$(USEMODULE)))
USEMODULE += conn_ip
endif
ifneq (,$(filter %_conn_tcp,$(USEMODULE)))
USEMODULE += conn_tcp
endif
ifneq (,$(filter %_conn_udp,$(USEMODULE)))
USEMODULE += conn_udp
endif
ifneq (,$(filter conn_%,$(USEMODULE)))
USEMODULE += conn
endif
# if any log_* is used, also use LOG pseudomodule
ifneq (,$(filter log_%,$(USEMODULE)))
USEMODULE += log

View File

@ -1,3 +1,7 @@
PSEUDOMODULES += conn
PSEUDOMODULES += conn_ip
PSEUDOMODULES += conn_tcp
PSEUDOMODULES += conn_udp
PSEUDOMODULES += gnrc_netif_default
PSEUDOMODULES += gnrc_ipv6_default
PSEUDOMODULES += gnrc_ipv6_router

51
sys/include/net/conn.h Normal file
View File

@ -0,0 +1,51 @@
/*
* 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_conn Application connection API
* @ingroup net
* @brief Provides a minimal common API for applications to connect to the
* different network stacks.
*
* This module provides a minimal common API for applications to connect over different network
* stacks. For each network stack there is supposed to be at least one connection type
* implementation. Note that this definition gives no restriction on how a connection type should be
* structured for simplicity and modularity. As a result, it can't give any guarantee that an
* implementation keeps them compatible to each other. For example, an implementation might allow,
* that a UDP receive function is called on a raw IPv6 connection object and even choose to do so
* for valid reasons (e.g. code size), but this definition does not impose this on the
* implementation. Currently there are the following option types defined:
*
* * @ref conn_ip_t (net/conn/ip.h): raw IP connections
* * @ref conn_tcp_t (net/conn/tcp.h): TCP connections
* * @ref conn_udp_t (net/conn/udp.h): UDP connections
*
* @{
*
* @file
* @brief Application connection API definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NET_CONN_H_
#define NET_CONN_H_
#include "net/conn/ip.h"
#include "net/conn/tcp.h"
#include "net/conn/udp.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* NET_CONN_H_ */
/** @} */

124
sys/include/net/conn/ip.h Normal file
View File

@ -0,0 +1,124 @@
/*
* 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_conn_ip Raw IPv4/IPv6 connections
* @ingroup net_conn
* @brief Connection submodule for raw IPv4/IPv6 connections
* @{
*
* @file
* @brief Raw IPv4/IPv6 connection definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NET_CONN_IP_H_
#define NET_CONN_IP_H_
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Forward declaration of @ref conn_ip_t to allow for external definition.
*/
struct conn_ip;
/**
* @brief Implementation-specific type of a raw IPv4/IPv6 connection object
*/
typedef struct conn_ip conn_ip_t;
/**
* @brief Creates a new raw IPv4/IPv6 connection object
*
* @param[out] conn Preallocated connection object. Must fill the size of the stack-specific
* connection desriptor.
* @param[in] addr The local IP address for @p conn.
* @param[in] addr_len Length of @p addr. Must be fitting for the @p family.
* @param[in] family The family of @p addr (see @ref net_af).
* @param[in] proto @ref net_protnum for the IPv6 packets to receive.
*
* @return 0 on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' bind() function specification.
*/
int conn_ip_create(conn_ip_t *conn, const void *addr, size_t addr_len, int family, int proto);
/**
* @brief Closes a raw IPv4/IPv6 connection
*
* @param[in,out] conn A raw IPv4/IPv6 connection object.
*/
void conn_ip_close(conn_ip_t *conn);
/**
* @brief Gets the local address of a raw IPv4/IPv6 connection
*
* @param[in] conn A raw IPv4/IPv6 connection object.
* @param[out] addr The local IP address. Must have space for any address of the connection's
* family.
*
* @return length of @p addr on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' getsockname() function
* specification.
*/
int conn_ip_getlocaladdr(conn_ip_t *conn, void *addr);
/**
* @brief Receives a message over IPv4/IPv6
*
* @param[in] conn A raw IPv4/IPv6 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 have space for any address
* of the connection's family.
* @param[out] addr_len Length of @p addr. Can be NULL if @p addr is NULL.
*
* @note Function may block.
*
* @return The number of bytes received on success.
* @return 0, if no received data is available, but everything is in order.
* @return any other negative number in case of an error. For portability, implementations should
* draw inspiration of the errno values from the POSIX' recv(), recvfrom(), or recvmsg()
* function specification.
*/
int conn_ip_recvfrom(conn_ip_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len);
/**
* @brief Sends a message over IPv4/IPv6
*
* @param[in] data Pointer where the received data should be stored.
* @param[in] len Maximum space available at @p data.
* @param[in] src The source address. May be NULL for all any interface address.
* @param[in] src_len Length of @p src.
* @param[in] dst The receiver's network address.
* @param[in] dst_len Length of @p dst.
* @param[in] family The family of @p src and @p dst (see @ref net_af).
* @param[in] proto @ref net_protnum for the IPv6 packets to set.
*
* @note Function may block.
*
* @return The number of bytes send on success.
* @return any other negative number in case of an error. For portability, implementations should
* draw inspiration of the errno values from the POSIX' send(), sendfrom(), or sendmsg()
* function specification.
*/
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);
#ifdef __cplusplus
}
#endif
#endif /* NET_CONN_IP_H_ */
/** @} */

173
sys/include/net/conn/tcp.h Normal file
View File

@ -0,0 +1,173 @@
/*
* 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_conn_tcp TCP connections
* @ingroup net_conn
* @brief Connection submodule for TCP connections
* @{
*
* @file
* @brief TCP connection definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NET_CONN_TCP_H_
#define NET_CONN_TCP_H_
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Forward declaration of @ref conn_tcp_t to allow for external definition.
*/
struct conn_tcp;
/**
* @brief Implementation-specific type of a TCP connection object
*/
typedef struct conn_tcp conn_tcp_t;
/**
* @brief Creates a new TCP connection object
*
* @param[out] conn Preallocated connection object. Must fill the size of the stack-specific
* connection desriptor.
* @param[in] addr The local network layer address for @p conn.
* @param[in] addr_len The length of @p addr. Must be fitting for the @p family.
* @param[in] family The family of @p addr (see @ref net_af).
* @param[in] port The local TCP port for @p conn.
*
* @return 0 on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' bind() function specification.
*/
int conn_tcp_create(conn_tcp_t *conn, const void *addr, size_t addr_len, int family,
uint16_t port);
/**
* @brief Closes a TCP connection
*
* @param[in,out] conn A TCP connection object.
*/
void conn_tcp_close(conn_tcp_t *conn);
/**
* @brief Gets the local address of a TCP connection
*
* @param[in] conn A TCP connection object.
* @param[out] addr The local network layer address. Must have space for any address of
* the connection's family.
* @param[out] port The local TCP port.
*
* @return length of @p addr on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' getsockname() function
* specification.
*/
int conn_tcp_getlocaladdr(conn_tcp_t *conn, void *addr, uint16_t *port);
/**
* @brief Gets the address of the connected peer of a TCP connection
*
* @param[in] conn A TCP connection object.
* @param[out] addr The network layer address of the connected peer. Must have space for any
* address of the connection's family.
* @param[out] port The TCP port of the connected peer.
*
* @return length of @p addr on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' getpeername() function
* specification.
*/
int conn_tcp_getpeeraddr(conn_tcp_t *conn, void *addr, uint16_t *port);
/**
* @brief Connects to a remote TCP peer
*
* @param[in] conn A TCP connection object.
* @param[in] addr The remote network layer address for @p conn.
* @param[in] addr_len Length of @p addr.
* @param[in] port The remote TCP port for @p conn.
*
* @return 0 on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' connect() function specification.
*/
int conn_tcp_connect(conn_tcp_t *conn, const void *addr, size_t addr_len, uint16_t port);
/**
* @brief Marks connection to listen for a connection request by a remote TCP peer
*
* @param[in] conn A TCP connection object.
* @param[in] queue_len Maximum length of the queue for connection requests.
* An implementation may choose to silently adapt this value to its needs
* (setting it to a minimum or maximum value). Any negative number must be
* set at least to 0.
*
* @return 0 on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' listen() function specification.
*/
int conn_tcp_listen(conn_tcp_t *conn, int queue_len);
/**
* @brief Receives and handles TCP connection requests from other peers
*
* @param[in] conn A TCP connection object.
* @param[out] out_conn A new TCP connection object for the established connection.
*
* @return 0 on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' accept() function specification.
*/
int conn_tcp_accept(conn_tcp_t *conn, conn_tcp_t *out_conn);
/**
* @brief Receives a TCP message
*
* @param[in] conn A TCP connection object.
* @param[out] data Pointer where the received data should be stored.
* @param[in] max_len Maximum space available at @p data.
*
* @note Function may block.
*
* @return The number of bytes received on success.
* @return 0, if no received data is available, but everything is in order.
* @return any other negative number in case of an error. For portability, implementations should
* draw inspiration of the errno values from the POSIX' recv(), recvfrom(), or recvmsg()
* function specification.
*/
int conn_tcp_recv(conn_tcp_t *conn, void *data, size_t max_len);
/**
* @brief Sends a TCP message
*
* @param[in] conn A TCP connection object.
* @param[in] data Pointer where the received data should be stored.
* @param[in] len Maximum space available at @p data.
*
* @note Function may block.
*
* @return The number of bytes send on success.
* @return any other negative number in case of an error. For portability, implementations should
* draw inspiration of the errno values from the POSIX' send(), sendfrom(), or sendmsg()
* function specification.
*/
int conn_tcp_send(conn_tcp_t *conn, const void *data, size_t len);
#ifdef __cplusplus
}
#endif
#endif /* NET_CONN_TCP_H_ */
/** @} */

130
sys/include/net/conn/udp.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_conn_udp UDP connections
* @ingroup net_conn
* @brief Connection submodule for UDP connections
* @{
*
* @file
* @brief UDP connection definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NET_CONN_UDP_H_
#define NET_CONN_UDP_H_
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Forward declaration of @ref conn_udp_t to allow for external definition.
*/
struct conn_udp;
/**
* @brief Implementation-specific type of a UDP connection object
*/
typedef struct conn_udp conn_udp_t;
/**
* @brief Creates a new UDP connection object
*
* @param[out] conn Preallocated connection object. Must fill the size of the stack-specific
* connection desriptor.
* @param[in] addr The local network layer address for @p conn.
* @param[in] addr_len The length of @p addr. Must be fitting for the @p family.
* @param[in] family The family of @p addr (see @ref net_af).
* @param[in] port The local UDP port for @p conn.
*
* @return 0 on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' bind() function specification.
*/
int conn_udp_create(conn_udp_t *conn, const void *addr, size_t addr_len, int family,
uint16_t port);
/**
* @brief Closes a UDP connection
*
* @param[in,out] conn A UDP connection object.
*/
void conn_udp_close(conn_udp_t *conn);
/**
* @brief Gets the local address of a UDP connection
*
* @param[in] conn A UDP connection object.
* @param[out] addr The local network layer address. Must have space for any address of
* the connection's family.
* @param[out] port The local UDP port.
*
* @return length of @p addr on success.
* @return any other negative number in case of an error. For portability implementations should
* draw inspiration of the errno values from the POSIX' getsockname() function
* specification.
*/
int conn_udp_getlocaladdr(conn_udp_t *conn, void *addr, uint16_t *port);
/**
* @brief Receives a UDP message
*
* @param[in] conn A UDP 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 network layer address. Must have space
* for any address of the connection's family.
* @param[out] addr_len Length of @p addr. Can be NULL if @p addr is NULL.
* @param[out] port NULL pointer or the sender's UDP port.
*
* @note Function may block.
*
* @return The number of bytes received on success.
* @return 0, if no received data is available, but everything is in order.
* @return any other negative number in case of an error. For portability, implementations should
* draw inspiration of the errno values from the POSIX' recv(), recvfrom(), or recvmsg()
* function specification.
*/
int conn_udp_recvfrom(conn_udp_t *conn, void *data, size_t max_len, void *addr, size_t *addr_len,
uint16_t *port);
/**
* @brief Sends a UDP message
*
* @param[in] data Pointer where the received data should be stored.
* @param[in] len Maximum space available at @p data.
* @param[in] src The source address. May be NULL for all any interface address.
* @param[in] src_len Length of @p src. May be 0 if @p src is NULL
* @param[in] dst The receiver's network address.
* @param[in] dst_len Length of @p dst.
* @param[in] family The family of @p src and @p dst (see @ref net_af).
* @param[in] sport The source UDP port.
* @param[in] dport The receiver's UDP port.
*
* @note Function may block.
*
* @return The number of bytes send on success.
* @return any other negative number in case of an error. For portability, implementations should
* draw inspiration of the errno values from the POSIX' send(), sendfrom(), or sendmsg()
* function specification.
*/
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);
#ifdef __cplusplus
}
#endif
#endif /* NET_CONN_UDP4_H_ */
/** @} */