| 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 | enum tcp_port {
|
|---|
| 116 | TCP_PORT_ANY = 0
|
|---|
| 117 | };
|
|---|
| 118 |
|
|---|
| 119 | /** Connection incoming segments queue */
|
|---|
| 120 | typedef struct {
|
|---|
| 121 | struct tcp_conn *conn;
|
|---|
| 122 | list_t list;
|
|---|
| 123 | } tcp_iqueue_t;
|
|---|
| 124 |
|
|---|
| 125 | /** Retransmission queue */
|
|---|
| 126 | typedef struct {
|
|---|
| 127 | struct tcp_conn *conn;
|
|---|
| 128 | list_t list;
|
|---|
| 129 |
|
|---|
| 130 | /** Retransmission timer */
|
|---|
| 131 | fibril_timer_t *timer;
|
|---|
| 132 | } tcp_tqueue_t;
|
|---|
| 133 |
|
|---|
| 134 | typedef enum {
|
|---|
| 135 | ap_active,
|
|---|
| 136 | ap_passive
|
|---|
| 137 | } acpass_t;
|
|---|
| 138 |
|
|---|
| 139 | typedef enum {
|
|---|
| 140 | tcp_open_nonblock = 1
|
|---|
| 141 | } tcp_open_flags_t;
|
|---|
| 142 |
|
|---|
| 143 | typedef struct tcp_conn tcp_conn_t;
|
|---|
| 144 |
|
|---|
| 145 | /** Connection state change callback function */
|
|---|
| 146 | typedef void (*tcp_cstate_cb_t)(tcp_conn_t *, void *);
|
|---|
| 147 |
|
|---|
| 148 | /** Connection callbacks */
|
|---|
| 149 | typedef struct {
|
|---|
| 150 | void (*cstate_change)(tcp_conn_t *, void *, tcp_cstate_t);
|
|---|
| 151 | void (*recv_data)(tcp_conn_t *, void *);
|
|---|
| 152 | } tcp_cb_t;
|
|---|
| 153 |
|
|---|
| 154 | /** Connection */
|
|---|
| 155 | struct tcp_conn {
|
|---|
| 156 | char *name;
|
|---|
| 157 | link_t link;
|
|---|
| 158 |
|
|---|
| 159 | /** Connection callbacks function */
|
|---|
| 160 | tcp_cb_t *cb;
|
|---|
| 161 | /** Argument to @c cstate_cb */
|
|---|
| 162 | void *cb_arg;
|
|---|
| 163 |
|
|---|
| 164 | /** Connection identification (local and remote endpoint) */
|
|---|
| 165 | inet_ep2_t ident;
|
|---|
| 166 |
|
|---|
| 167 | /** Active or passive connection */
|
|---|
| 168 | acpass_t ap;
|
|---|
| 169 |
|
|---|
| 170 | /** Protects access to connection structure */
|
|---|
| 171 | fibril_mutex_t lock;
|
|---|
| 172 | /** Reference count */
|
|---|
| 173 | atomic_t refcnt;
|
|---|
| 174 |
|
|---|
| 175 | /** Connection state */
|
|---|
| 176 | tcp_cstate_t cstate;
|
|---|
| 177 | /** True if connection was reset */
|
|---|
| 178 | bool reset;
|
|---|
| 179 | /** True if connection was deleted by user */
|
|---|
| 180 | bool deleted;
|
|---|
| 181 | /** Signalled when @c cstate changes */
|
|---|
| 182 | fibril_condvar_t cstate_cv;
|
|---|
| 183 |
|
|---|
| 184 | /** Set when FIN is removed from the retransmission queue */
|
|---|
| 185 | bool fin_is_acked;
|
|---|
| 186 |
|
|---|
| 187 | /** Queue of incoming segments */
|
|---|
| 188 | tcp_iqueue_t incoming;
|
|---|
| 189 |
|
|---|
| 190 | /** Retransmission queue */
|
|---|
| 191 | tcp_tqueue_t retransmit;
|
|---|
| 192 |
|
|---|
| 193 | /** Time-Wait timeout timer */
|
|---|
| 194 | fibril_timer_t *tw_timer;
|
|---|
| 195 |
|
|---|
| 196 | /** Receive buffer */
|
|---|
| 197 | uint8_t *rcv_buf;
|
|---|
| 198 | /** Receive buffer size */
|
|---|
| 199 | size_t rcv_buf_size;
|
|---|
| 200 | /** Receive buffer number of bytes used */
|
|---|
| 201 | size_t rcv_buf_used;
|
|---|
| 202 | /** Receive buffer contains FIN */
|
|---|
| 203 | bool rcv_buf_fin;
|
|---|
| 204 | /** Receive buffer CV. Broadcast when new data is inserted */
|
|---|
| 205 | fibril_condvar_t rcv_buf_cv;
|
|---|
| 206 |
|
|---|
| 207 | /** Send buffer */
|
|---|
| 208 | uint8_t *snd_buf;
|
|---|
| 209 | /** Send buffer size */
|
|---|
| 210 | size_t snd_buf_size;
|
|---|
| 211 | /** Send buffer number of bytes used */
|
|---|
| 212 | size_t snd_buf_used;
|
|---|
| 213 | /** Send buffer contains FIN */
|
|---|
| 214 | bool snd_buf_fin;
|
|---|
| 215 | /** Send buffer CV. Broadcast when space is made available in buffer */
|
|---|
| 216 | fibril_condvar_t snd_buf_cv;
|
|---|
| 217 |
|
|---|
| 218 | /** Send unacknowledged */
|
|---|
| 219 | uint32_t snd_una;
|
|---|
| 220 | /** Send next */
|
|---|
| 221 | uint32_t snd_nxt;
|
|---|
| 222 | /** Send window */
|
|---|
| 223 | uint32_t snd_wnd;
|
|---|
| 224 | /** Send urgent pointer */
|
|---|
| 225 | uint32_t snd_up;
|
|---|
| 226 | /** Segment sequence number used for last window update */
|
|---|
| 227 | uint32_t snd_wl1;
|
|---|
| 228 | /** Segment acknowledgement number used for last window update */
|
|---|
| 229 | uint32_t snd_wl2;
|
|---|
| 230 | /** Initial send sequence number */
|
|---|
| 231 | uint32_t iss;
|
|---|
| 232 |
|
|---|
| 233 | /** Receive next */
|
|---|
| 234 | uint32_t rcv_nxt;
|
|---|
| 235 | /** Receive window */
|
|---|
| 236 | uint32_t rcv_wnd;
|
|---|
| 237 | /** Receive urgent pointer */
|
|---|
| 238 | uint32_t rcv_up;
|
|---|
| 239 | /** Initial receive sequence number */
|
|---|
| 240 | uint32_t irs;
|
|---|
| 241 | };
|
|---|
| 242 |
|
|---|
| 243 | /** Data returned by Status user call */
|
|---|
| 244 | typedef struct {
|
|---|
| 245 | /** Connection state */
|
|---|
| 246 | tcp_cstate_t cstate;
|
|---|
| 247 | } tcp_conn_status_t;
|
|---|
| 248 |
|
|---|
| 249 | typedef struct {
|
|---|
| 250 | /** SYN, FIN */
|
|---|
| 251 | tcp_control_t ctrl;
|
|---|
| 252 |
|
|---|
| 253 | /** Segment sequence number */
|
|---|
| 254 | uint32_t seq;
|
|---|
| 255 | /** Segment acknowledgement number */
|
|---|
| 256 | uint32_t ack;
|
|---|
| 257 | /** Segment length in sequence space */
|
|---|
| 258 | uint32_t len;
|
|---|
| 259 | /** Segment window */
|
|---|
| 260 | uint32_t wnd;
|
|---|
| 261 | /** Segment urgent pointer */
|
|---|
| 262 | uint32_t up;
|
|---|
| 263 |
|
|---|
| 264 | /** Segment data, may be moved when trimming segment */
|
|---|
| 265 | void *data;
|
|---|
| 266 | /** Segment data, original pointer used to free data */
|
|---|
| 267 | void *dfptr;
|
|---|
| 268 | } tcp_segment_t;
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 | typedef struct {
|
|---|
| 272 | link_t link;
|
|---|
| 273 | inet_ep2_t epp;
|
|---|
| 274 | tcp_segment_t *seg;
|
|---|
| 275 | } tcp_rqueue_entry_t;
|
|---|
| 276 |
|
|---|
| 277 | /** NCSim queue entry */
|
|---|
| 278 | typedef struct {
|
|---|
| 279 | link_t link;
|
|---|
| 280 | suseconds_t delay;
|
|---|
| 281 | inet_ep2_t epp;
|
|---|
| 282 | tcp_segment_t *seg;
|
|---|
| 283 | } tcp_squeue_entry_t;
|
|---|
| 284 |
|
|---|
| 285 | typedef struct {
|
|---|
| 286 | link_t link;
|
|---|
| 287 | tcp_segment_t *seg;
|
|---|
| 288 | } tcp_iqueue_entry_t;
|
|---|
| 289 |
|
|---|
| 290 | /** Retransmission queue entry */
|
|---|
| 291 | typedef struct {
|
|---|
| 292 | link_t link;
|
|---|
| 293 | tcp_conn_t *conn;
|
|---|
| 294 | tcp_segment_t *seg;
|
|---|
| 295 | } tcp_tqueue_entry_t;
|
|---|
| 296 |
|
|---|
| 297 | typedef enum {
|
|---|
| 298 | cp_continue,
|
|---|
| 299 | cp_done
|
|---|
| 300 | } cproc_t;
|
|---|
| 301 |
|
|---|
| 302 | /** Encoded PDU */
|
|---|
| 303 | typedef struct {
|
|---|
| 304 | /** Source address */
|
|---|
| 305 | inet_addr_t src;
|
|---|
| 306 | /** Destination address */
|
|---|
| 307 | inet_addr_t dest;
|
|---|
| 308 | /** Encoded header */
|
|---|
| 309 | void *header;
|
|---|
| 310 | /** Encoded header size */
|
|---|
| 311 | size_t header_size;
|
|---|
| 312 | /** Text */
|
|---|
| 313 | void *text;
|
|---|
| 314 | /** Text size */
|
|---|
| 315 | size_t text_size;
|
|---|
| 316 | } tcp_pdu_t;
|
|---|
| 317 |
|
|---|
| 318 | /** TCP client connection */
|
|---|
| 319 | typedef struct tcp_cconn {
|
|---|
| 320 | /** Connection */
|
|---|
| 321 | tcp_conn_t *conn;
|
|---|
| 322 | /** Connection ID for the client */
|
|---|
| 323 | sysarg_t id;
|
|---|
| 324 | /** Client */
|
|---|
| 325 | struct tcp_client *client;
|
|---|
| 326 | link_t lclient;
|
|---|
| 327 | } tcp_cconn_t;
|
|---|
| 328 |
|
|---|
| 329 | /** TCP client listener */
|
|---|
| 330 | typedef struct tcp_clst {
|
|---|
| 331 | /** Local endpoint */
|
|---|
| 332 | inet_ep_t elocal;
|
|---|
| 333 | /** Connection */
|
|---|
| 334 | tcp_conn_t *conn;
|
|---|
| 335 | /** Listener ID for the client */
|
|---|
| 336 | sysarg_t id;
|
|---|
| 337 | /** Client */
|
|---|
| 338 | struct tcp_client *client;
|
|---|
| 339 | /** Link to tcp_client_t.clst */
|
|---|
| 340 | link_t lclient;
|
|---|
| 341 | } tcp_clst_t;
|
|---|
| 342 |
|
|---|
| 343 | /** TCP client */
|
|---|
| 344 | typedef struct tcp_client {
|
|---|
| 345 | /** Client callbac session */
|
|---|
| 346 | async_sess_t *sess;
|
|---|
| 347 | /** Client's connections */
|
|---|
| 348 | list_t cconn; /* of tcp_cconn_t */
|
|---|
| 349 | /** Client's listeners */
|
|---|
| 350 | list_t clst;
|
|---|
| 351 | } tcp_client_t;
|
|---|
| 352 |
|
|---|
| 353 | #endif
|
|---|
| 354 |
|
|---|
| 355 | /** @}
|
|---|
| 356 | */
|
|---|