2016-02-04 14:37:35 +01:00
|
|
|
/*
|
2017-02-06 18:26:45 +01:00
|
|
|
* Copyright (C) 2015-2017 Simon Brummer
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* 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_tcp TCP
|
|
|
|
* @ingroup net_gnrc
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief RIOT's TCP implementation for the GNRC network stack.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2017-02-06 18:26:45 +01:00
|
|
|
* @brief GNRC TCP API
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-02-01 16:33:31 +01:00
|
|
|
* @author Simon Brummer <simon.brummer@posteo.de>
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
|
|
|
|
2017-05-23 18:19:52 +02:00
|
|
|
#ifndef NET_GNRC_TCP_H
|
|
|
|
#define NET_GNRC_TCP_H
|
2016-02-04 14:37:35 +01:00
|
|
|
|
2017-02-26 17:31:23 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "net/gnrc/pkt.h"
|
2016-02-04 14:37:35 +01:00
|
|
|
#include "net/gnrc/tcp/tcb.h"
|
|
|
|
|
|
|
|
#ifdef MODULE_GNRC_IPV6
|
|
|
|
#include "net/gnrc/ipv6.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Initialize TCP
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @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.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
|
|
|
int gnrc_tcp_init(void);
|
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Initialize Transmission Control Block (TCB)
|
|
|
|
* @pre @p tcb must not be NULL.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @param[in,out] tcb TCB that should be initialized.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
2017-02-04 10:19:59 +01:00
|
|
|
void gnrc_tcp_tcb_init(gnrc_tcp_tcb_t *tcb);
|
2016-02-04 14:37:35 +01:00
|
|
|
|
2018-05-11 10:06:14 +02:00
|
|
|
/**
|
|
|
|
* @brief Opens a connection actively.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @note Blocks until a connection has been established or an error occured.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
2019-10-06 09:04:49 +02:00
|
|
|
* @returns 0 on success.
|
2018-05-11 10:06:14 +02:00
|
|
|
* -EAFNOSUPPORT if @p address_family is not supported.
|
|
|
|
* -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 resetted by the peer.
|
|
|
|
*/
|
|
|
|
int gnrc_tcp_open_active(gnrc_tcp_tcb_t *tcb, uint8_t address_family,
|
|
|
|
char *target_addr, uint16_t target_port,
|
|
|
|
uint16_t local_port);
|
2016-02-04 14:37:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Opens a connection passively, by waiting for an incomming request.
|
|
|
|
*
|
|
|
|
* @pre gnrc_tcp_tcb_init() must have been successfully called.
|
2017-03-06 08:47:06 +01:00
|
|
|
* @pre @p tcb must not be NULL.
|
2016-02-04 14:37:35 +01:00
|
|
|
* @pre if local_addr is not NULL, local_addr must be assigned to a network interface.
|
|
|
|
* @pre if local_port is not zero.
|
|
|
|
*
|
|
|
|
* @note Blocks until a connection has been established (incomming connection request
|
|
|
|
* to @p local_port) or an error occured.
|
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @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 valied.
|
|
|
|
* @param[in] local_port Port number to listen on.
|
|
|
|
*
|
2019-10-06 09:04:49 +02:00
|
|
|
* @returns 0 on success.
|
2017-03-06 08:47:06 +01:00
|
|
|
* -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.
|
2018-05-11 10:06:14 +02:00
|
|
|
* or @p target_addr is invalid.
|
2017-03-06 08:47:06 +01:00
|
|
|
* -EISCONN if TCB is already in use.
|
|
|
|
* -ENOMEM if the receive buffer for the TCB could not be allocated.
|
|
|
|
* Hint: Increase "GNRC_TCP_RCV_BUFFERS".
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
2018-05-11 10:06:14 +02:00
|
|
|
int gnrc_tcp_open_passive(gnrc_tcp_tcb_t *tcb, uint8_t address_family,
|
|
|
|
const char *local_addr, uint16_t local_port);
|
2016-02-04 14:37:35 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Transmit data to connected peer.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* @pre gnrc_tcp_tcb_init() must have been successfully called.
|
2017-03-06 08:47:06 +01:00
|
|
|
* @pre @p tcb must not be NULL.
|
|
|
|
* @pre @p data must not be NULL.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* @note Blocks until up to @p len bytes were transmitted or an error occured.
|
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @param[in,out] tcb TCB holding the connection information.
|
|
|
|
* @param[in] data Pointer to the data that should be transmitted.
|
|
|
|
* @param[in] len Number of bytes that should be transmitted.
|
|
|
|
* @param[in] user_timeout_duration_us If not zero and there was not data transmitted
|
|
|
|
* 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 resetted by the peer.
|
|
|
|
* -ECONNABORTED if the connection was aborted.
|
|
|
|
* -ETIMEDOUT if @p user_timeout_duration_us expired.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
|
|
|
ssize_t gnrc_tcp_send(gnrc_tcp_tcb_t *tcb, const void *data, const size_t len,
|
|
|
|
const uint32_t user_timeout_duration_us);
|
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Receive Data from the peer.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* @pre gnrc_tcp_tcb_init() must have been successfully called.
|
2017-03-06 08:47:06 +01:00
|
|
|
* @pre @p tcb must not be NULL.
|
|
|
|
* @pre @p data must not be NULL.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* @note Function blocks if user_timeout_duration_us is not zero.
|
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @param[in,out] tcb TCB holding the connection information.
|
|
|
|
* @param[out] data Pointer to the buffer where the received data
|
|
|
|
* should be copied into.
|
|
|
|
* @param[in] max_len Maximum amount to bytes that should be read
|
|
|
|
* into @p data.
|
|
|
|
* @param[in] user_timeout_duration_us Timeout for receive in microseconds.
|
|
|
|
* If zero and no data is available, the function
|
|
|
|
* returns immediately. If not zero the function
|
|
|
|
* blocks until data is available or
|
|
|
|
* @p user_timeout_duration_us microseconds passed.
|
|
|
|
*
|
|
|
|
* @returns The number of bytes read into @p data.
|
2019-08-09 10:42:51 +02:00
|
|
|
* 0, if the connection is closing and no further data can be read.
|
2017-03-06 08:47:06 +01:00
|
|
|
* -ENOTCONN if connection is not established.
|
|
|
|
* -EAGAIN if user_timeout_duration_us is zero and no data is available.
|
|
|
|
* -ECONNRESET if connection was resetted by the peer.
|
|
|
|
* -ECONNABORTED if the connection was aborted.
|
|
|
|
* -ETIMEDOUT if @p user_timeout_duration_us expired.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
|
|
|
ssize_t gnrc_tcp_recv(gnrc_tcp_tcb_t *tcb, void *data, const size_t max_len,
|
|
|
|
const uint32_t user_timeout_duration_us);
|
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Close a TCP connection.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
|
|
|
* @pre gnrc_tcp_tcb_init() must have been successfully called.
|
2017-03-06 08:47:06 +01:00
|
|
|
* @pre @p tcb must not be NULL.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @param[in,out] tcb TCB holding the connection information.
|
2017-05-03 14:37:51 +02:00
|
|
|
*/
|
|
|
|
void gnrc_tcp_close(gnrc_tcp_tcb_t *tcb);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Abort a TCP connection.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-05-03 14:37:51 +02:00
|
|
|
* @pre gnrc_tcp_tcb_init() must have been successfully called.
|
|
|
|
* @pre @p tcb must not be NULL.
|
|
|
|
*
|
|
|
|
* @param[in,out] tcb TCB holding the connection information.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
2017-05-03 14:37:51 +02:00
|
|
|
void gnrc_tcp_abort(gnrc_tcp_tcb_t *tcb);
|
2016-02-04 14:37:35 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Calculate and set checksum in TCP header.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @param[in] hdr Gnrc_pktsnip that contains TCP header.
|
|
|
|
* @param[in] pseudo_hdr Gnrc_pktsnip that contains network layer header.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2019-10-06 09:04:49 +02:00
|
|
|
* @returns 0 on succees.
|
2017-03-06 08:47:06 +01:00
|
|
|
* -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.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
|
|
|
int gnrc_tcp_calc_csum(const gnrc_pktsnip_t *hdr, const gnrc_pktsnip_t *pseudo_hdr);
|
|
|
|
|
|
|
|
/**
|
2017-03-06 08:47:06 +01:00
|
|
|
* @brief Adds a TCP header to a given payload.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @param[in] payload Payload that follows the TCP header.
|
|
|
|
* @param[in] src Source port number.
|
|
|
|
* @param[in] dst Destination port number.
|
2016-02-04 14:37:35 +01:00
|
|
|
*
|
2017-03-06 08:47:06 +01:00
|
|
|
* @returns Not NULL on success.
|
|
|
|
* NULL if TCP header was not allocated.
|
2016-02-04 14:37:35 +01:00
|
|
|
*/
|
|
|
|
gnrc_pktsnip_t *gnrc_tcp_hdr_build(gnrc_pktsnip_t *payload, uint16_t src, uint16_t dst);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-23 18:19:52 +02:00
|
|
|
#endif /* NET_GNRC_TCP_H */
|
2016-02-04 14:37:35 +01:00
|
|
|
/** @} */
|