Get rid of sys/time.h
[helenos.git] / uspace / srv / net / tcp / tcp_type.h
blobaf07b13e661453f046a44d306ededc85cf4af98f
1 /*
2 * Copyright (c) 2015 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup tcp
30 * @{
32 /** @file TCP type definitions
35 #ifndef TCP_TYPE_H
36 #define TCP_TYPE_H
38 #include <adt/list.h>
39 #include <async.h>
40 #include <stdbool.h>
41 #include <fibril.h>
42 #include <fibril_synch.h>
43 #include <refcount.h>
44 #include <stddef.h>
45 #include <stdint.h>
46 #include <inet/addr.h>
47 #include <inet/endpoint.h>
49 struct tcp_conn;
51 /** Connection state */
52 typedef enum {
53 /** Listen */
54 st_listen,
55 /** Syn-sent */
56 st_syn_sent,
57 /** Syn-received */
58 st_syn_received,
59 /** Established */
60 st_established,
61 /** Fin-wait-1 */
62 st_fin_wait_1,
63 /** Fin-wait-2 */
64 st_fin_wait_2,
65 /** Close-wait */
66 st_close_wait,
67 /** Closing */
68 st_closing,
69 /** Last-ack */
70 st_last_ack,
71 /** Time-wait */
72 st_time_wait,
73 /** Closed */
74 st_closed
75 } tcp_cstate_t;
77 /** Error codes returned by TCP user calls (per the spec). */
78 typedef enum {
79 /* OK */
80 TCP_EOK,
81 /* Connection aborted due to user timeout */
82 TCP_EABORTED,
83 /* Connection already exists */
84 TCP_EEXISTS,
85 /* Connection closing */
86 TCP_ECLOSING,
87 /* Connection does not exist */
88 TCP_ENOTEXIST,
89 /* Connection illegal for this process */
90 TCP_EILLEGAL,
91 /* Connection not open */
92 TCP_ENOTOPEN,
93 /* Connection reset */
94 TCP_ERESET,
95 /* Remote endpoint unspecified */
96 TCP_EUNSPEC,
97 /* Insufficient resources */
98 TCP_ENORES,
99 /* Precedence not allowed */
100 TCP_EINVPREC,
101 /* Security/compartment not allowed */
102 TCP_EINVCOMP,
103 TCP_EAGAIN
104 } tcp_error_t;
106 /** Transfer flags */
107 typedef enum {
108 XF_PUSH = 0x1,
109 XF_URGENT = 0x2
110 } xflags_t;
112 /** Control message bits
114 * Note this is not the actual on-the-wire encoding
116 typedef enum {
117 CTL_SYN = 0x1,
118 CTL_FIN = 0x2,
119 CTL_RST = 0x4,
120 CTL_ACK = 0x8
121 } tcp_control_t;
123 /** Connection incoming segments queue */
124 typedef struct {
125 struct tcp_conn *conn;
126 list_t list;
127 } tcp_iqueue_t;
129 /** Active or passive connection */
130 typedef enum {
131 ap_active,
132 ap_passive
133 } acpass_t;
135 /** Flags for TCP open operation */
136 typedef enum {
137 tcp_open_nonblock = 1
138 } tcp_open_flags_t;
140 typedef struct tcp_conn tcp_conn_t;
142 /** Connection state change callback function */
143 typedef void (*tcp_cstate_cb_t)(tcp_conn_t *, void *);
145 /** Connection callbacks */
146 typedef struct {
147 void (*cstate_change)(tcp_conn_t *, void *, tcp_cstate_t);
148 void (*recv_data)(tcp_conn_t *, void *);
149 } tcp_cb_t;
151 /** Data returned by Status user call */
152 typedef struct {
153 /** Connection state */
154 tcp_cstate_t cstate;
155 } tcp_conn_status_t;
157 typedef struct {
158 /** SYN, FIN */
159 tcp_control_t ctrl;
161 /** Segment sequence number */
162 uint32_t seq;
163 /** Segment acknowledgement number */
164 uint32_t ack;
165 /** Segment length in sequence space */
166 uint32_t len;
167 /** Segment window */
168 uint32_t wnd;
169 /** Segment urgent pointer */
170 uint32_t up;
172 /** Segment data, may be moved when trimming segment */
173 void *data;
174 /** Segment data, original pointer used to free data */
175 void *dfptr;
176 } tcp_segment_t;
178 /** Receive queue entry */
179 typedef struct {
180 link_t link;
181 inet_ep2_t epp;
182 tcp_segment_t *seg;
183 } tcp_rqueue_entry_t;
185 /** Receive queue callbacks */
186 typedef struct {
187 /** Segment received */
188 void (*seg_received)(inet_ep2_t *, tcp_segment_t *);
189 } tcp_rqueue_cb_t;
191 /** NCSim queue entry */
192 typedef struct {
193 link_t link;
194 usec_t delay;
195 inet_ep2_t epp;
196 tcp_segment_t *seg;
197 } tcp_squeue_entry_t;
199 /** Incoming queue entry */
200 typedef struct {
201 link_t link;
202 tcp_segment_t *seg;
203 } tcp_iqueue_entry_t;
205 /** Retransmission queue entry */
206 typedef struct {
207 link_t link;
208 tcp_conn_t *conn;
209 tcp_segment_t *seg;
210 } tcp_tqueue_entry_t;
212 /** Retransmission queue callbacks */
213 typedef struct {
214 /** Segment received */
215 void (*transmit_seg)(inet_ep2_t *, tcp_segment_t *);
216 } tcp_tqueue_cb_t;
218 /** Retransmission queue */
219 typedef struct {
220 struct tcp_conn *conn;
221 list_t list;
223 /** Retransmission timer */
224 fibril_timer_t *timer;
226 /** Callbacks */
227 tcp_tqueue_cb_t *cb;
228 } tcp_tqueue_t;
230 /** Connection */
231 struct tcp_conn {
232 char *name;
233 link_t link;
235 /** Connection callbacks function */
236 tcp_cb_t *cb;
237 /** Argument to @c cstate_cb */
238 void *cb_arg;
240 /** Connection identification (local and remote endpoint) */
241 inet_ep2_t ident;
242 /** Connection is in association map */
243 bool mapped;
245 /** Active or passive connection */
246 acpass_t ap;
248 /** Protects access to connection structure */
249 fibril_mutex_t lock;
250 /** Reference count */
251 atomic_refcount_t refcnt;
253 /** Connection state */
254 tcp_cstate_t cstate;
255 /** True if connection was reset */
256 bool reset;
257 /** True if connection was deleted by user */
258 bool deleted;
259 /** Signalled when @c cstate changes */
260 fibril_condvar_t cstate_cv;
262 /** Set when FIN is removed from the retransmission queue */
263 bool fin_is_acked;
265 /** Queue of incoming segments */
266 tcp_iqueue_t incoming;
268 /** Retransmission queue */
269 tcp_tqueue_t retransmit;
271 /** Time-Wait timeout timer */
272 fibril_timer_t *tw_timer;
274 /** Receive buffer */
275 uint8_t *rcv_buf;
276 /** Receive buffer size */
277 size_t rcv_buf_size;
278 /** Receive buffer number of bytes used */
279 size_t rcv_buf_used;
280 /** Receive buffer contains FIN */
281 bool rcv_buf_fin;
282 /** Receive buffer CV. Broadcast when new data is inserted */
283 fibril_condvar_t rcv_buf_cv;
285 /** Send buffer */
286 uint8_t *snd_buf;
287 /** Send buffer size */
288 size_t snd_buf_size;
289 /** Send buffer number of bytes used */
290 size_t snd_buf_used;
291 /** Send buffer contains FIN */
292 bool snd_buf_fin;
293 /** Send buffer CV. Broadcast when space is made available in buffer */
294 fibril_condvar_t snd_buf_cv;
296 /** Send unacknowledged */
297 uint32_t snd_una;
298 /** Send next */
299 uint32_t snd_nxt;
300 /** Send window */
301 uint32_t snd_wnd;
302 /** Send urgent pointer */
303 uint32_t snd_up;
304 /** Segment sequence number used for last window update */
305 uint32_t snd_wl1;
306 /** Segment acknowledgement number used for last window update */
307 uint32_t snd_wl2;
308 /** Initial send sequence number */
309 uint32_t iss;
311 /** Receive next */
312 uint32_t rcv_nxt;
313 /** Receive window */
314 uint32_t rcv_wnd;
315 /** Receive urgent pointer */
316 uint32_t rcv_up;
317 /** Initial receive sequence number */
318 uint32_t irs;
321 /** Continuation of processing.
323 * When processing incoming segment, are we done or should we continue
324 * processing it?
326 typedef enum {
327 cp_continue,
328 cp_done
329 } cproc_t;
331 /** Encoded PDU */
332 typedef struct {
333 /** Source address */
334 inet_addr_t src;
335 /** Destination address */
336 inet_addr_t dest;
337 /** Encoded header */
338 void *header;
339 /** Encoded header size */
340 size_t header_size;
341 /** Text */
342 void *text;
343 /** Text size */
344 size_t text_size;
345 } tcp_pdu_t;
347 /** TCP client connection */
348 typedef struct tcp_cconn {
349 /** Connection */
350 tcp_conn_t *conn;
351 /** Connection ID for the client */
352 sysarg_t id;
353 /** Client */
354 struct tcp_client *client;
355 link_t lclient;
356 } tcp_cconn_t;
358 /** TCP client listener */
359 typedef struct tcp_clst {
360 /** Local endpoint */
361 inet_ep_t elocal;
362 /** Connection */
363 tcp_conn_t *conn;
364 /** Listener ID for the client */
365 sysarg_t id;
366 /** Client */
367 struct tcp_client *client;
368 /** Link to tcp_client_t.clst */
369 link_t lclient;
370 } tcp_clst_t;
372 /** TCP client */
373 typedef struct tcp_client {
374 /** Client callback session */
375 async_sess_t *sess;
376 /** Client's connections */
377 list_t cconn; /* of tcp_cconn_t */
378 /** Client's listeners */
379 list_t clst;
380 } tcp_client_t;
382 /** Internal loopback type */
383 typedef enum {
384 /** No loopback */
385 tcp_lb_none,
386 /** Segment loopback */
387 tcp_lb_segment,
388 /** PDU loopback */
389 tcp_lb_pdu
390 } tcp_lb_t;
392 #endif
394 /** @}