Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnrc_tcp: Prepare for sock integration. #13026

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 100 additions & 60 deletions sys/include/net/gnrc/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,62 @@
extern "C" {
#endif

/**
* @brief Address information for a single TCP connection endpoint.
* @extends sock_tcp_ep_t
*/
typedef struct {
int family; /**< IP address family. */
union {
#ifdef MODULE_GNRC_IPV6
uint8_t ipv6[sizeof(ipv6_addr_t)]; /**< IPv6 address storage */
#endif
uint8_t dummy; /**< Enable build without network module */
} addr; /**< IP address storage */
uint16_t netif; /**< Network interface ID */
uint16_t port; /**< Port number (in host byte order) */
} gnrc_tcp_ep_t;

/**
* @brief Initialize TCP connection endpoint.
*
* @param[in,out] ep Endpoint to initialize.
* @param[in] family Address family of @p addr.
* @param[in] addr Address for endpoint.
* @param[in] addr_size Size of @p addr.
* @param[in] port Port number for endpoint.
* @param[in] netif Network inferface to use.
*
* @return 0 on success.
* @return -EAFNOSUPPORT if @p address_family is not supported.
* @return -EINVAL if @p addr_size does not match @p family.
*/
int gnrc_tcp_ep_init(gnrc_tcp_ep_t *ep, int family, const uint8_t *addr, size_t addr_size,
uint16_t port, uint16_t netif);

/**
* @brief Construct TCP connection endpoint from string.
* @note This function expects @p str in the IPv6 "URL" notation.
* The following strings specify a valid endpoint:
* - [fe80::0a00:27ff:fe9f:7a5b%5]:8080 (with Port and Interface)
* - [2001::0200:f8ff:fe21:67cf]:8080 (with Port)
* - [2001::0200:f8ff:fe21:67cf] (addr only)
*
* @param[in,out] ep Endpoint to initialize.
* @param[in] str String containing IPv6-Address to parse.
*
* @return 0 on success.
* @return -EINVAL if parsing of @p str failed.
*/
int gnrc_tcp_ep_from_str(gnrc_tcp_ep_t *ep, const char *str);

/**
* @brief Initialize TCP
*
* @returns PID of TCP thread on success
* -1 if TCB is already running.
* -EINVAL, if priority is greater than or equal SCHED_PRIO_LEVELS
* -EOVERFLOW, if there are too many threads running.
* @return PID of TCP thread on success
* @return -1 if TCB is already running.
* @return -EINVAL, if priority is greater than or equal SCHED_PRIO_LEVELS
* @return -EOVERFLOW, if there are too many threads running.
*/
int gnrc_tcp_init(void);

Expand All @@ -57,62 +106,53 @@ void gnrc_tcp_tcb_init(gnrc_tcp_tcb_t *tcb);
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre @p tcb must not be NULL
* @pre @p target_addr must not be NULL.
* @pre @p target_port must not be 0.
* @pre @p remote must not be NULL.
*
* @note Blocks until a connection has been established or an error occurred.
*
* @param[in,out] tcb TCB holding the connection information.
* @param[in] address_family Address family of @p target_addr.
* @param[in] target_addr Pointer to target address.
* @param[in] target_port Target port number.
* @param[in] local_port If zero or PORT_UNSPEC, the connections
* source port is randomly chosen. If local_port is non-zero
* the local_port is used as source port.
*
* @returns 0 on success.
* -EAFNOSUPPORT if @p address_family is not supported.
* -EINVAL if @p address_family is not the same the address_family use by the TCB.
* @param[in,out] tcb TCB holding the connection information.
* @param[in] remote Remote endpoint of the host to connect to.
* @param[in] local_port If zero or PORT_UNSPEC, the connections source port
* is randomly chosen. If local_port is non-zero
* the local_port is used as source port.
*
* @return 0 on success.
* @return -EAFNOSUPPORT if @p address_family is not supported.
* @return -EINVAL if @p address_family is not the same the address_family use by the TCB.
* or @p target_addr is invalid.
* -EISCONN if TCB is already in use.
* -ENOMEM if the receive buffer for the TCB could not be allocated.
* -EADDRINUSE if @p local_port is already used by another connection.
* -ETIMEDOUT if the connection could not be opened.
* -ECONNREFUSED if the connection was reset by the peer.
* @return -EISCONN if TCB is already in use.
* @return -ENOMEM if the receive buffer for the TCB could not be allocated.
* @return -EADDRINUSE if @p local_port is already used by another connection.
* @return -ETIMEDOUT if the connection could not be opened.
* @return -ECONNREFUSED if the connection was reset by the peer.
*/
int gnrc_tcp_open_active(gnrc_tcp_tcb_t *tcb, uint8_t address_family,
char *target_addr, uint16_t target_port,
int gnrc_tcp_open_active(gnrc_tcp_tcb_t *tcb, const gnrc_tcp_ep_t *remote,
uint16_t local_port);

/**
* @brief Opens a connection passively, by waiting for an incoming request.
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre @p tcb must not be NULL.
* @pre if local_addr is not NULL, local_addr must be assigned to a network interface.
* @pre if local_port is not zero.
* @pre @p local must not be NULL.
* @pre port in @p local must not be zero.
*
* @note Blocks until a connection has been established (incoming connection request
* to @p local_port) or an error occurred.
*
* @param[in,out] tcb TCB holding the connection information.
* @param[in] address_family Address family of @p local_addr.
* If local_addr == NULL, address_family is ignored.
* @param[in] local_addr If not NULL the connection is bound to @p local_addr.
* If NULL a connection request to all local ip
* addresses is valid.
* @param[in] local_port Port number to listen on.
*
* @returns 0 on success.
* -EAFNOSUPPORT if local_addr != NULL and @p address_family is not supported.
* -EINVAL if @p address_family is not the same the address_family used in TCB.
* or @p target_addr is invalid.
* -EISCONN if TCB is already in use.
* -ENOMEM if the receive buffer for the TCB could not be allocated.
* @param[in,out] tcb TCB holding the connection information.
* @param[in] local Endpoint specifying the port and address used to wait for
* incoming connections.
*
* @return 0 on success.
* @return -EAFNOSUPPORT if local_addr != NULL and @p address_family is not supported.
* @return -EINVAL if @p address_family is not the same the address_family used in TCB.
* or the address in @p local is invalid.
* @return -EISCONN if TCB is already in use.
* @return -ENOMEM if the receive buffer for the TCB could not be allocated.
* Hint: Increase "GNRC_TCP_RCV_BUFFERS".
*/
int gnrc_tcp_open_passive(gnrc_tcp_tcb_t *tcb, uint8_t address_family,
const char *local_addr, uint16_t local_port);
int gnrc_tcp_open_passive(gnrc_tcp_tcb_t *tcb, const gnrc_tcp_ep_t *local);

/**
* @brief Transmit data to connected peer.
Expand All @@ -130,11 +170,11 @@ int gnrc_tcp_open_passive(gnrc_tcp_tcb_t *tcb, uint8_t address_family,
* the function returns after user_timeout_duration_us.
* If zero, no timeout will be triggered.
*
* @returns The number of successfully transmitted bytes.
* -ENOTCONN if connection is not established.
* -ECONNRESET if connection was reset by the peer.
* -ECONNABORTED if the connection was aborted.
* -ETIMEDOUT if @p user_timeout_duration_us expired.
* @return The number of successfully transmitted bytes.
* @return -ENOTCONN if connection is not established.
* @return -ECONNRESET if connection was reset by the peer.
* @return -ECONNABORTED if the connection was aborted.
* @return -ETIMEDOUT if @p user_timeout_duration_us expired.
*/
ssize_t gnrc_tcp_send(gnrc_tcp_tcb_t *tcb, const void *data, const size_t len,
const uint32_t user_timeout_duration_us);
Expand All @@ -159,13 +199,13 @@ ssize_t gnrc_tcp_send(gnrc_tcp_tcb_t *tcb, const void *data, const size_t len,
* blocks until data is available or
* @p user_timeout_duration_us microseconds passed.
*
* @returns The number of bytes read into @p data.
* 0, if the connection is closing and no further data can be read.
* -ENOTCONN if connection is not established.
* -EAGAIN if user_timeout_duration_us is zero and no data is available.
* -ECONNRESET if connection was reset by the peer.
* -ECONNABORTED if the connection was aborted.
* -ETIMEDOUT if @p user_timeout_duration_us expired.
* @return The number of bytes read into @p data.
* @return 0, if the connection is closing and no further data can be read.
* @return -ENOTCONN if connection is not established.
* @return -EAGAIN if user_timeout_duration_us is zero and no data is available.
* @return -ECONNRESET if connection was reset by the peer.
* @return -ECONNABORTED if the connection was aborted.
* @return -ETIMEDOUT if @p user_timeout_duration_us expired.
*/
ssize_t gnrc_tcp_recv(gnrc_tcp_tcb_t *tcb, void *data, const size_t max_len,
const uint32_t user_timeout_duration_us);
Expand Down Expand Up @@ -196,10 +236,10 @@ void gnrc_tcp_abort(gnrc_tcp_tcb_t *tcb);
* @param[in] hdr Gnrc_pktsnip that contains TCP header.
* @param[in] pseudo_hdr Gnrc_pktsnip that contains network layer header.
*
* @returns 0 on success.
* -EFAULT if @p hdr or pseudo_hdr were NULL
* -EBADMSG if @p hdr is not of type GNRC_NETTYPE_TCP
* -ENOENT if @p pseudo_hdr protocol is unsupported.
* @return 0 on success.
* @return -EFAULT if @p hdr or pseudo_hdr were NULL
* @return -EBADMSG if @p hdr is not of type GNRC_NETTYPE_TCP
* @return -ENOENT if @p pseudo_hdr protocol is unsupported.
*/
int gnrc_tcp_calc_csum(const gnrc_pktsnip_t *hdr, const gnrc_pktsnip_t *pseudo_hdr);

Expand All @@ -210,8 +250,8 @@ int gnrc_tcp_calc_csum(const gnrc_pktsnip_t *hdr, const gnrc_pktsnip_t *pseudo_h
* @param[in] src Source port number.
* @param[in] dst Destination port number.
*
* @returns Not NULL on success.
* NULL if TCP header was not allocated.
* @return Not NULL on success.
* @return NULL if TCP header was not allocated.
*/
gnrc_pktsnip_t *gnrc_tcp_hdr_build(gnrc_pktsnip_t *payload, uint16_t src, uint16_t dst);

Expand Down
Loading