| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 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.
|
|---|
| 16 | *
|
|---|
| 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.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup tcp
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file TCP type definitions
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef TCP_TYPE_H
|
|---|
| 36 | #define TCP_TYPE_H
|
|---|
| 37 |
|
|---|
| 38 | #include <adt/list.h>
|
|---|
| 39 | #include <async.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| 41 | #include <fibril.h>
|
|---|
| 42 | #include <fibril_synch.h>
|
|---|
| 43 | #include <sys/types.h>
|
|---|
| 44 | #include <inet/addr.h>
|
|---|
| 45 | #include <inet/endpoint.h>
|
|---|
| 46 |
|
|---|
| 47 | struct tcp_conn;
|
|---|
| 48 |
|
|---|
| 49 | typedef enum {
|
|---|
| 50 | /** Listen */
|
|---|
| 51 | st_listen,
|
|---|
| 52 | /** Syn-sent */
|
|---|
| 53 | st_syn_sent,
|
|---|
| 54 | /** Syn-received */
|
|---|
| 55 | st_syn_received,
|
|---|
| 56 | /** Established */
|
|---|
| 57 | st_established,
|
|---|
| 58 | /** Fin-wait-1 */
|
|---|
| 59 | st_fin_wait_1,
|
|---|
| 60 | /** Fin-wait-2 */
|
|---|
| 61 | st_fin_wait_2,
|
|---|
| 62 | /** Close-wait */
|
|---|
| 63 | st_close_wait,
|
|---|
| 64 | /** Closing */
|
|---|
| 65 | st_closing,
|
|---|
| 66 | /** Last-ack */
|
|---|
| 67 | st_last_ack,
|
|---|
| 68 | /** Time-wait */
|
|---|
| 69 | st_time_wait,
|
|---|
| 70 | /** Closed */
|
|---|
| 71 | st_closed
|
|---|
| 72 | } tcp_cstate_t;
|
|---|
| 73 |
|
|---|
| 74 | /** Error codes returned by TCP user calls (per the spec). */
|
|---|
| 75 | typedef enum {
|
|---|
| 76 | /* OK */
|
|---|
| 77 | TCP_EOK,
|
|---|
| 78 | /* Connection aborted due to user timeout */
|
|---|
| 79 | TCP_EABORTED,
|
|---|
| 80 | /* Connection already exists */
|
|---|
| 81 | TCP_EEXISTS,
|
|---|
| 82 | /* Connection closing */
|
|---|
| 83 | TCP_ECLOSING,
|
|---|
| 84 | /* Connection does not exist */
|
|---|
| 85 | TCP_ENOTEXIST,
|
|---|
| 86 | /* Connection illegal for this process */
|
|---|
| 87 | TCP_EILLEGAL,
|
|---|
| 88 | /* Connection not open */
|
|---|
| 89 | TCP_ENOTOPEN,
|
|---|
| 90 | /* Connection reset */
|
|---|
| 91 | TCP_ERESET,
|
|---|
| 92 | /* Remote endpoint unspecified */
|
|---|
| 93 | TCP_EUNSPEC,
|
|---|
| 94 | /* Insufficient resources */
|
|---|
| 95 | TCP_ENORES,
|
|---|
| 96 | /* Precedence not allowed */
|
|---|
| 97 | TCP_EINVPREC,
|
|---|
| 98 | /* Security/compartment not allowed */
|
|---|
| 99 | TCP_EINVCOMP,
|
|---|
| 100 | TCP_EAGAIN
|
|---|
| 101 | } tcp_error_t;
|
|---|
| 102 |
|
|---|
| 103 | typedef enum {
|
|---|
| 104 | XF_PUSH = 0x1,
|
|---|
| 105 | XF_URGENT = 0x2
|
|---|
| 106 | } xflags_t;
|
|---|
| 107 |
|
|---|
| 108 | typedef enum {
|
|---|
| 109 | CTL_SYN = 0x1,
|
|---|
| 110 | CTL_FIN = 0x2,
|
|---|
| 111 | CTL_RST = 0x4,
|
|---|
| 112 | CTL_ACK = 0x8
|
|---|
| 113 | } tcp_control_t;
|
|---|
| 114 |
|
|---|
| 115 | /** Connection incoming segments queue */
|
|---|
| 116 | typedef struct {
|
|---|
| 117 | struct tcp_conn *conn;
|
|---|
| 118 | list_t list;
|
|---|
| 119 | } tcp_iqueue_t;
|
|---|
| 120 |
|
|---|
| 121 | /** Retransmission queue */
|
|---|
| 122 | typedef struct {
|
|---|
| 123 | struct tcp_conn *conn;
|
|---|
| 124 | list_t list;
|
|---|
| 125 |
|
|---|
| 126 | /** Retransmission timer */
|
|---|
| 127 | fibril_timer_t *timer;
|
|---|
| 128 | } tcp_tqueue_t;
|
|---|
| 129 |
|
|---|
| 130 | typedef enum {
|
|---|
| 131 | ap_active,
|
|---|
| 132 | ap_passive
|
|---|
| 133 | } acpass_t;
|
|---|
| 134 |
|
|---|
| 135 | typedef enum {
|
|---|
| 136 | tcp_open_nonblock = 1
|
|---|
| 137 | } tcp_open_flags_t;
|
|---|
| 138 |
|
|---|
| 139 | typedef struct tcp_conn tcp_conn_t;
|
|---|
| 140 |
|
|---|
| 141 | /** Connection state change callback function */
|
|---|
| 142 | typedef void (*tcp_cstate_cb_t)(tcp_conn_t *, void *);
|
|---|
| 143 |
|
|---|
| 144 | /** Connection callbacks */
|
|---|
| 145 | typedef struct {
|
|---|
| 146 | void (*cstate_change)(tcp_conn_t *, void *, tcp_cstate_t);
|
|---|
| 147 | void (*recv_data)(tcp_conn_t *, void *);
|
|---|
| 148 | } tcp_cb_t;
|
|---|
| 149 |
|
|---|
| 150 | /** Connection */
|
|---|
| 151 | struct tcp_conn {
|
|---|
| 152 | char *name;
|
|---|
| 153 | link_t link;
|
|---|
| 154 |
|
|---|
| 155 | /** Connection callbacks function */
|
|---|
| 156 | tcp_cb_t *cb;
|
|---|
| 157 | /** Argument to @c cstate_cb */
|
|---|
| 158 | void *cb_arg;
|
|---|
| 159 |
|
|---|
| 160 | /** Connection identification (local and remote endpoint) */
|
|---|
| 161 | inet_ep2_t ident;
|
|---|
| 162 |
|
|---|
| 163 | /** Active or passive connection */
|
|---|
| 164 | acpass_t ap;
|
|---|
| 165 |
|
|---|
| 166 | /** Protects access to connection structure */
|
|---|
| 167 | fibril_mutex_t lock;
|
|---|
| 168 | /** Reference count */
|
|---|
| 169 | atomic_t refcnt;
|
|---|
| 170 |
|
|---|
| 171 | /** Connection state */
|
|---|
| 172 | tcp_cstate_t cstate;
|
|---|
| 173 | /** True if connection was reset */
|
|---|
| 174 | bool reset;
|
|---|
| 175 | /** True if connection was deleted by user */
|
|---|
| 176 | bool deleted;
|
|---|
| 177 | /** Signalled when @c cstate changes */
|
|---|
| 178 | fibril_condvar_t cstate_cv;
|
|---|
| 179 |
|
|---|
| 180 | /** Set when FIN is removed from the retransmission queue */
|
|---|
| 181 | bool fin_is_acked;
|
|---|
| 182 |
|
|---|
| 183 | /** Queue of incoming segments */
|
|---|
| 184 | tcp_iqueue_t incoming;
|
|---|
| 185 |
|
|---|
| 186 | /** Retransmission queue */
|
|---|
| 187 | tcp_tqueue_t retransmit;
|
|---|
| 188 |
|
|---|
| 189 | /** Time-Wait timeout timer */
|
|---|
| 190 | fibril_timer_t *tw_timer;
|
|---|
| 191 |
|
|---|
| 192 | /** Receive buffer */
|
|---|
| 193 | uint8_t *rcv_buf;
|
|---|
| 194 | /** Receive buffer size */
|
|---|
| 195 | size_t rcv_buf_size;
|
|---|
| 196 | /** Receive buffer number of bytes used */
|
|---|
| 197 | size_t rcv_buf_used;
|
|---|
| 198 | /** Receive buffer contains FIN */
|
|---|
| 199 | bool rcv_buf_fin;
|
|---|
| 200 | /** Receive buffer CV. Broadcast when new data is inserted */
|
|---|
| 201 | fibril_condvar_t rcv_buf_cv;
|
|---|
| 202 |
|
|---|
| 203 | /** Send buffer */
|
|---|
| 204 | uint8_t *snd_buf;
|
|---|
| 205 | /** Send buffer size */
|
|---|
| 206 | size_t snd_buf_size;
|
|---|
| 207 | /** Send buffer number of bytes used */
|
|---|
| 208 | size_t snd_buf_used;
|
|---|
| 209 | /** Send buffer contains FIN */
|
|---|
| 210 | bool snd_buf_fin;
|
|---|
| 211 | /** Send buffer CV. Broadcast when space is made available in buffer */
|
|---|
| 212 | fibril_condvar_t snd_buf_cv;
|
|---|
| 213 |
|
|---|
| 214 | /** Send unacknowledged */
|
|---|
| 215 | uint32_t snd_una;
|
|---|
| 216 | /** Send next */
|
|---|
| 217 | uint32_t snd_nxt;
|
|---|
| 218 | /** Send window */
|
|---|
| 219 | uint32_t snd_wnd;
|
|---|
| 220 | /** Send urgent pointer */
|
|---|
| 221 | uint32_t snd_up;
|
|---|
| 222 | /** Segment sequence number used for last window update */
|
|---|
| 223 | uint32_t snd_wl1;
|
|---|
| 224 | /** Segment acknowledgement number used for last window update */
|
|---|
| 225 | uint32_t snd_wl2;
|
|---|
| 226 | /** Initial send sequence number */
|
|---|
| 227 | uint32_t iss;
|
|---|
| 228 |
|
|---|
| 229 | /** Receive next */
|
|---|
| 230 | uint32_t rcv_nxt;
|
|---|
| 231 | /** Receive window */
|
|---|
| 232 | uint32_t rcv_wnd;
|
|---|
| 233 | /** Receive urgent pointer */
|
|---|
| 234 | uint32_t rcv_up;
|
|---|
| 235 | /** Initial receive sequence number */
|
|---|
| 236 | uint32_t irs;
|
|---|
| 237 | };
|
|---|
| 238 |
|
|---|
| 239 | /** Data returned by Status user call */
|
|---|
| 240 | typedef struct {
|
|---|
| 241 | /** Connection state */
|
|---|
| 242 | tcp_cstate_t cstate;
|
|---|
| 243 | } tcp_conn_status_t;
|
|---|
| 244 |
|
|---|
| 245 | typedef struct {
|
|---|
| 246 | /** SYN, FIN */
|
|---|
| 247 | tcp_control_t ctrl;
|
|---|
| 248 |
|
|---|
| 249 | /** Segment sequence number */
|
|---|
| 250 | uint32_t seq;
|
|---|
| 251 | /** Segment acknowledgement number */
|
|---|
| 252 | uint32_t ack;
|
|---|
| 253 | /** Segment length in sequence space */
|
|---|
| 254 | uint32_t len;
|
|---|
| 255 | /** Segment window */
|
|---|
| 256 | uint32_t wnd;
|
|---|
| 257 | /** Segment urgent pointer */
|
|---|
| 258 | uint32_t up;
|
|---|
| 259 |
|
|---|
| 260 | /** Segment data, may be moved when trimming segment */
|
|---|
| 261 | void *data;
|
|---|
| 262 | /** Segment data, original pointer used to free data */
|
|---|
| 263 | void *dfptr;
|
|---|
| 264 | } tcp_segment_t;
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | typedef struct {
|
|---|
| 268 | link_t link;
|
|---|
| 269 | inet_ep2_t epp;
|
|---|
| 270 | tcp_segment_t *seg;
|
|---|
| 271 | } tcp_rqueue_entry_t;
|
|---|
| 272 |
|
|---|
| 273 | /** NCSim queue entry */
|
|---|
| 274 | typedef struct {
|
|---|
| 275 | link_t link;
|
|---|
| 276 | suseconds_t delay;
|
|---|
| 277 | inet_ep2_t epp;
|
|---|
| 278 | tcp_segment_t *seg;
|
|---|
| 279 | } tcp_squeue_entry_t;
|
|---|
| 280 |
|
|---|
| 281 | typedef struct {
|
|---|
| 282 | link_t link;
|
|---|
| 283 | tcp_segment_t *seg;
|
|---|
| 284 | } tcp_iqueue_entry_t;
|
|---|
| 285 |
|
|---|
| 286 | /** Retransmission queue entry */
|
|---|
| 287 | typedef struct {
|
|---|
| 288 | link_t link;
|
|---|
| 289 | tcp_conn_t *conn;
|
|---|
| 290 | tcp_segment_t *seg;
|
|---|
| 291 | } tcp_tqueue_entry_t;
|
|---|
| 292 |
|
|---|
| 293 | typedef enum {
|
|---|
| 294 | cp_continue,
|
|---|
| 295 | cp_done
|
|---|
| 296 | } cproc_t;
|
|---|
| 297 |
|
|---|
| 298 | /** Encoded PDU */
|
|---|
| 299 | typedef struct {
|
|---|
| 300 | /** Source address */
|
|---|
| 301 | inet_addr_t src;
|
|---|
| 302 | /** Destination address */
|
|---|
| 303 | inet_addr_t dest;
|
|---|
| 304 | /** Encoded header */
|
|---|
| 305 | void *header;
|
|---|
| 306 | /** Encoded header size */
|
|---|
| 307 | size_t header_size;
|
|---|
| 308 | /** Text */
|
|---|
| 309 | void *text;
|
|---|
| 310 | /** Text size */
|
|---|
| 311 | size_t text_size;
|
|---|
| 312 | } tcp_pdu_t;
|
|---|
| 313 |
|
|---|
| 314 | /** TCP client connection */
|
|---|
| 315 | typedef struct tcp_cconn {
|
|---|
| 316 | /** Connection */
|
|---|
| 317 | tcp_conn_t *conn;
|
|---|
| 318 | /** Connection ID for the client */
|
|---|
| 319 | sysarg_t id;
|
|---|
| 320 | /** Client */
|
|---|
| 321 | struct tcp_client *client;
|
|---|
| 322 | link_t lclient;
|
|---|
| 323 | } tcp_cconn_t;
|
|---|
| 324 |
|
|---|
| 325 | /** TCP client listener */
|
|---|
| 326 | typedef struct tcp_clst {
|
|---|
| 327 | /** Local endpoint */
|
|---|
| 328 | inet_ep_t elocal;
|
|---|
| 329 | /** Connection */
|
|---|
| 330 | tcp_conn_t *conn;
|
|---|
| 331 | /** Listener ID for the client */
|
|---|
| 332 | sysarg_t id;
|
|---|
| 333 | /** Client */
|
|---|
| 334 | struct tcp_client *client;
|
|---|
| 335 | /** Link to tcp_client_t.clst */
|
|---|
| 336 | link_t lclient;
|
|---|
| 337 | } tcp_clst_t;
|
|---|
| 338 |
|
|---|
| 339 | /** TCP client */
|
|---|
| 340 | typedef struct tcp_client {
|
|---|
| 341 | /** Client callbac session */
|
|---|
| 342 | async_sess_t *sess;
|
|---|
| 343 | /** Client's connections */
|
|---|
| 344 | list_t cconn; /* of tcp_cconn_t */
|
|---|
| 345 | /** Client's listeners */
|
|---|
| 346 | list_t clst;
|
|---|
| 347 | } tcp_client_t;
|
|---|
| 348 |
|
|---|
| 349 | #endif
|
|---|
| 350 |
|
|---|
| 351 | /** @}
|
|---|
| 352 | */
|
|---|