Skip to content

Commit

Permalink
conn: Simplify and overhaul conn API
Browse files Browse the repository at this point in the history
This change overhauls the `conn` API in the following manner:

* for every `conn` objects now also one can add a callback for asynchronous
  reception on creation
* a common address type for both IPv4 and IPv6 addresses was introduced
* instead of having addresses, address length and ports/protocols given
  separately a new data type called "end points" for every connectivity type is
  introduced `conn_ep_x_t`, consisting of the address, its family and its
  port/protocol
* send for connection-less communication was simplified by not requiring source
  information anymore. It can be supplied however by (optionally) providing an
  already created `conn` object
* TCP connection establishment was simplified: listen/connect functions were
  dropped, instead a user can either give the remote end point on creation
  (implicit connect) or omit it (implicit listen)
  • Loading branch information
miri64 committed Jun 4, 2016
1 parent b807a9c commit 928593f
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 162 deletions.
20 changes: 13 additions & 7 deletions sys/include/net/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@
#ifndef NET_CONN_H_
#define NET_CONN_H_

#include "net/conn/addr.h"
#include "net/conn/ip.h"
#include "net/conn/tcp.h"
#include "net/conn/udp.h"
#include "net/ipv6/addr.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -87,14 +87,20 @@ extern "C" {
/**
* @brief Find the best matching source address for a given prefix
*
* @param[in] dst Pointer to the IPv6 address to find a match for
* Must not be NULL
* @param[out] src Best matching address on any interface, if return value
* is 1. Otherwise undefined
* @param[in] dst Pointer to the IPv6 address to find a match for
* Must not be NULL
* @param[in] family @ref net_af "Address family" of both @p dst and return
* value.
*
* @return NULL if no matching address on any interface could be found
* @return pointer to an IPv6 address configured on an interface with the best
* match to @p dst
* @return 1 if an IP address with the best match to @p dst is configured on an
* interface.
* @return 0 if no matching IP address could be found.
*/
ipv6_addr_t *conn_find_best_source(const ipv6_addr_t *dst);
int conn_find_best_src_ip(conn_addr_ip_t *src,
const conn_addr_ip_t *dst,
int family);

#ifdef __cplusplus
}
Expand Down
45 changes: 45 additions & 0 deletions sys/include/net/conn/addr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2016 Freie Universität Berlin
*
* 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_addr Address abstractions
* @ingroup net_conn
* @brief Address abstractions for usage with @ref net_conn.
* @{
*
* @file
* @brief Address abstraction definitions for @ref net_conn.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef CONN_ADDR_H_
#define CONN_ADDR_H_

#include "net/ipv4/addr.h"
#include "net/ipv6/addr.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Type to abstract both IPv4 and IPv6 addresses
*/
typedef union {
#ifdef CONN_HAS_IPV6
ipv6_addr_t ipv6; /**< IPv6 address mode */
#endif
ipv4_addr_t ipv4; /**< IPv4 address mode */
} conn_addr_ip_t;

#ifdef __cplusplus
}
#endif

#endif /* CONn_ADDR_H_ */
/** @} */
64 changes: 64 additions & 0 deletions sys/include/net/conn/ep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2016 Freie Universität Berlin
*
* 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_ep End points
* @ingroup net_conn
* @brief Protocol specific end points for connectivity objects.
* @{
*
* @file
* @brief Type definitions of protocol specific end points for connectivity
* objects.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef CONN_EP_H_
#define CONN_EP_H_

#include <stdint.h>

#include "net/conn/addr.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief An end point for a raw IPv4/IPv6 connectivity object.
*/
typedef struct {
conn_addr_ip_t addr; /**< IP address */
int family; /**< family of conn_ep_ip_t::addr */
uint8_t proto; /**< protocol for the IPv4/IPv6 end point */
} conn_ep_ip_t;

/**
* @brief An end point for a UDP connectivity object.
*/
typedef struct {
conn_addr_ip_t addr; /**< IP address */
int family; /**< family of conn_ep_udp_t::addr */
uint16_t port; /**< port for the UDP end point */
} conn_ep_udp_t;

/**
* @brief An end point for a TCP connectivity object.
*/
typedef struct {
conn_addr_ip_t addr; /**< IP address */
int family; /**< family of conn_ep_tcp_t::addr */
uint16_t port; /**< port for the TCP end point */
} conn_ep_tcp_t;

#ifdef __cplusplus
}
#endif

#endif /* CONN_EP_H_ */
/** @} */
87 changes: 47 additions & 40 deletions sys/include/net/conn/ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <stdint.h>
#include <stdlib.h>

#include "net/conn/ep.h"

#ifdef MODULE_GNRC_CONN_IP
#include "net/gnrc/conn.h"
#endif
Expand All @@ -36,30 +38,39 @@ extern "C" {
#endif

/**
* @brief Forward declaration of @ref conn_ip_t to allow for external definition.
* @brief Implementation-specific type of a raw IPv4/IPv6 connectivity object
*/
struct conn_ip;
typedef struct conn_ip conn_ip_t;

/**
* @brief Implementation-specific type of a raw IPv4/IPv6 connectivity object
* @brief Asynchronous receive callback for a raw IPv4/IPv6 connectivity
* object
*
* @param[in] conn A raw IPv4/IPv6 connectivity object.
* @param[out] data Pointer where the received data should be stored.
* @param[in] max_len Maximum space available at @p data.
* @param[out] src_ep Remote end point of the received data.
*/
typedef struct conn_ip conn_ip_t;
typedef void (*conn_ip_recv_cb_t)(conn_udp_t *conn, void *data,
size_t data_len, conn_udp_ep_t *src);

/**
* @brief Creates a new raw IPv4/IPv6 connectivity object
*
* @param[out] conn Preallocated connectivity object. Must fill the size of the
* stack-specific connectivity 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.
* @param[out] conn Preallocated connectivity object. Must fill the size of the
* stack-specific connectivity descriptor.
* @param[in] ep Local end point for the connectivity object.
* @param[in] cb Callback called asynchronously, when a packet is received.
* May be NULL in which case asynchronous reception is not
* possible.
*
* @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.
* @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);
int conn_ip_create(conn_ip_t *conn, const conn_ep_ip_t *ep,
conn_ip_recv_cb_t cb);

/**
* @brief Closes a raw IPv4/IPv6 connectivity
Expand All @@ -72,57 +83,53 @@ void conn_ip_close(conn_ip_t *conn);
* @brief Gets the local end point of a raw IPv4/IPv6 connectivity
*
* @param[in] conn A raw IPv4/IPv6 connectivity object.
* @param[out] addr The local IP address. Must have space for any address of the connection's
* family.
* @param[out] ep The local end point.
*
* @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.
* @return 0 on success.
* @return any other negative number in case of an error. For portabilit
* implementations should draw inspiration of the errno values from the
* POSIX' getsockname() function specification.
*/
int conn_ip_getlocaladdr(conn_ip_t *conn, void *addr);
int conn_ip_get_local(conn_ip_t *conn, conn_ep_ip_t *ep);

/**
* @brief Receives a message over IPv4/IPv6
*
* @param[in] conn A raw IPv4/IPv6 connectivity 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.
* @param[out] src_ep Remote end point of the received data.
*
* @note Function may block.
* @note Function blocks if no packet is currently waiting.
*
* @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.
* @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);
int conn_ip_recv(conn_ip_t *conn, void *data, size_t max_len,
conn_ep_ip_t *src_ep);

/**
* @brief Sends a message over IPv4/IPv6
*
* @param[in] conn A raw IPv4/IPv6 connectivity object. May be NULL.
* A sensible local end point should be selected by the
* stack in that case.
* @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.
* @param[in] dst_ep Remote end point for the send data.
*
* @note Function may block.
* @note Function blocks until packet is handed to the stack.
*
* @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.
* @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);
int conn_ip_send(conn_ip_t *conn, const void *data, size_t len,
conn_ep_ip_t *dst_ep);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 928593f

Please sign in to comment.