forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.h
231 lines (212 loc) · 8.74 KB
/
tcp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (C) 2015 Simon Brummer
*
* 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
* @brief RIOT's tcp implementation for the gnrc stack
*
* @{
*
* @file
* @brief TCP interface definition
*
* @author Simon Brummer <simon.brummer@haw-hamburg.de>
*/
#ifndef GNRC_TCP_H_
#define GNRC_TCP_H_
#include "net/tcp.h"
#include "net/gnrc/netapi.h"
#include "net/gnrc/nettype.h"
#include "net/gnrc/tcp/tcb.h"
#ifdef MODULE_GNRC_IPV6
#include "net/gnrc/ipv6.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Port unspecified.
*
* @note PORT 0 is reserved, according to rfc 1700(https://www.ietf.org/rfc/rfc1700.txt)
*/
#define GNRC_TCP_PORT_UNSPEC 0
/**
* @brief Head of conn linked list.
*/
extern gnrc_tcp_tcb_t *_list_gnrc_tcp_tcb_head;
/**
* @brief Mutex to protect linked list.
*/
extern mutex_t _list_gnrc_tcp_tcb_lock;
/**
* @brief Initialize and start TCP
*
* @return PID of TCP thread on success
* @return -1 if thread 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);
/**
* @brief Initialize Transmission Control Block (tcb)
* @pre tcb must not be NULL.
*
* @param[in,out] tcb Transmission that should be initialized.
*/
void gnrc_tcp_tcb_init(gnrc_tcp_tcb_t* tcb);
/**
* @brief Opens a connection actively.
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre tcb must not be NULL
* @pre target_addr must not be NULL.
* @pre target_port must not be 0.
*
* @note Blocks until a connection has been established or an error occured.
*
* @param[in,out] tcb This connections Transmission control block.
* @param[in] address_family Address Family of @p target_addr.
* @param[in] target_addr Pointer to target address.
* @param[in] target_port Targets port number.
* @param[in] local_port If zero or GNRC_TCP_PORT_UNSPEC, the connections
* source port is randomly choosen. If local_port is non-zero
* the local_port is used as source port.
*
* @return Zero 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.
* @return -EISCONN if transmission control block is already in use.
* @return -ENOMEM if the receive buffer for the tcb could not be allocated.
* Increase "GNRC_TCP_RCV_BUFFERS".
* @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 resetted by the peer.
*/
int gnrc_tcp_open_active(gnrc_tcp_tcb_t *tcb, const uint8_t address_family,
const uint8_t *target_addr, const uint16_t target_port,
const uint16_t local_port);
/**
* @brief Opens a connection passively, by waiting for an incomming request.
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre 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.
*
* @note Blocks until a connection has been established (incomming connection request
* to @p local_port) or an error occured.
*
* @param[in,out] tcb This connections Transmission control block.
* @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 the address in @p local_addr.
* If NULL a connection request to every local ip address is valid.
* @param[in] local_port Portnumber that should used for incomming connection requests.
*
* @return Zero 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 use by the tcb.
* @return -EISCONN if transmission control block is already in use.
* @return -ENOMEM if the receive buffer for the tcb could not be allocated.
* Increase "GNRC_TCP_RCV_BUFFERS".
*/
int gnrc_tcp_open_passive(gnrc_tcp_tcb_t *tcb, const uint8_t address_family,
const uint8_t *local_addr, const uint16_t local_port);
/**
* @brief Transmit Data to Peer.
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre tcb must not be NULL.
* @pre data must not be NULL.
*
* @note Blocks until up to @p len bytes were transmitted or an error occured.
*
* @param[in,out] tcb This connections Transmission control block.
* @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 were not data transmitted
* successfully, the function call returns after
* user_timeout_duration_us. If zero not timeout will be
* triggered.
*
* @return On success, the number of successfully transmitted bytes.
* @return -ENOTCONN if connection is not established.
* @return -ECONNRESET if connection was resetted 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);
/**
* @brief Receive Data from the Peer.
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre tcb must not be NULL.
* @pre data must not be NULL.
*
* @note Function blocks if user_timeout_duration_us is not zero.
*
* @param[in,out] tcb This connections Transmission control block.
* @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 reeived.
* Should not exceed size of @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 block until data is available or
* user_timeout_duration_us microseconds have passed.
*
* @return On success, the number of bytes read into @p data.
* @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 resetted 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);
/**
* @brief Close a tcp connection.
*
* @pre gnrc_tcp_tcb_init() must have been successfully called.
* @pre tcb must not be NULL.
*
* @param[in,out] tcb This connections Transmission control block.
*
* @return Zero on success.
*/
int gnrc_tcp_close(gnrc_tcp_tcb_t *tcb);
/**
* @brief Set checksum calculated from tcp and network-layer header in tcp-header.
*
* @param[in] hdr ng_pktsnip that contains tcp header.
* @param[in] pseudo_hdr ng_pktsnip that contains networklayer header.
*
* @return zero on succeed.
* @return -EFAULT if hdr or pseudo_hdr were NULL
* @return -EBADMSG if hdr is not of type GNRC_NETTYPE_TCP
* @return -ENOENT if pseudo_hdr protocol is unsupported.
*/
int gnrc_tcp_calc_csum(const gnrc_pktsnip_t *hdr, const gnrc_pktsnip_t *pseudo_hdr);
/**
* @brief Adds a tcp header to a given payload. Be carefull, leads to huge headers.
* Allocates all option bytes
*
* @param[in] payload payload that follows the tcp header
* @param[in] src Source port in host byte order
* @param[in] dst Destination port in host byte order
*
* @return NULL, if paket buffer is full
* @return Not NULL on success
*/
gnrc_pktsnip_t *gnrc_tcp_hdr_build(gnrc_pktsnip_t *payload, uint16_t src, uint16_t dst);
#ifdef __cplusplus
}
#endif
#endif /* GNRC_TCP_H_ */
/** @} */