| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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 <bool.h>
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 | #include <sys/types.h>
|
|---|
| 42 |
|
|---|
| 43 | struct tcp_conn;
|
|---|
| 44 |
|
|---|
| 45 | typedef enum {
|
|---|
| 46 | /** Listen */
|
|---|
| 47 | st_listen,
|
|---|
| 48 | /** Syn-sent */
|
|---|
| 49 | st_syn_sent,
|
|---|
| 50 | /** Syn-received */
|
|---|
| 51 | st_syn_received,
|
|---|
| 52 | /** Established */
|
|---|
| 53 | st_established,
|
|---|
| 54 | /** Fin-wait-1 */
|
|---|
| 55 | st_fin_wait_1,
|
|---|
| 56 | /** Fin-wait-2 */
|
|---|
| 57 | st_fin_wait_2,
|
|---|
| 58 | /** Close-wait */
|
|---|
| 59 | st_close_wait,
|
|---|
| 60 | /** Closing */
|
|---|
| 61 | st_closing,
|
|---|
| 62 | /** Last-ack */
|
|---|
| 63 | st_last_ack,
|
|---|
| 64 | /** Time-wait */
|
|---|
| 65 | st_time_wait,
|
|---|
| 66 | /** Closed */
|
|---|
| 67 | st_closed
|
|---|
| 68 | } tcp_cstate_t;
|
|---|
| 69 |
|
|---|
| 70 | /** Error codes returned by TCP user calls (per the spec). */
|
|---|
| 71 | typedef enum {
|
|---|
| 72 | /* OK */
|
|---|
| 73 | TCP_EOK,
|
|---|
| 74 | /* Connection aborted due to user timeout */
|
|---|
| 75 | TCP_EABORTED,
|
|---|
| 76 | /* Connection already exists */
|
|---|
| 77 | TCP_EEXISTS,
|
|---|
| 78 | /* Connection closing */
|
|---|
| 79 | TCP_ECLOSING,
|
|---|
| 80 | /* Connection does not exist */
|
|---|
| 81 | TCP_ENOTEXIST,
|
|---|
| 82 | /* Connection illegal for this process */
|
|---|
| 83 | TCP_EILLEGAL,
|
|---|
| 84 | /* Connection not open */
|
|---|
| 85 | TCP_ENOTOPEN,
|
|---|
| 86 | /* Connection reset */
|
|---|
| 87 | TCP_ERESET,
|
|---|
| 88 | /* Foreign socket unspecified */
|
|---|
| 89 | TCP_EUNSPEC,
|
|---|
| 90 | /* Insufficient resources */
|
|---|
| 91 | TCP_ENORES,
|
|---|
| 92 | /* Precedence not allowed */
|
|---|
| 93 | TCP_EINVPREC,
|
|---|
| 94 | /* Security/compartment not allowed */
|
|---|
| 95 | TCP_EINVCOMP
|
|---|
| 96 | } tcp_error_t;
|
|---|
| 97 |
|
|---|
| 98 | typedef enum {
|
|---|
| 99 | XF_PUSH = 0x1,
|
|---|
| 100 | XF_URGENT = 0x2
|
|---|
| 101 | } xflags_t;
|
|---|
| 102 |
|
|---|
| 103 | typedef enum {
|
|---|
| 104 | CTL_SYN = 0x1,
|
|---|
| 105 | CTL_FIN = 0x2,
|
|---|
| 106 | CTL_RST = 0x4,
|
|---|
| 107 | CTL_ACK = 0x8
|
|---|
| 108 | } tcp_control_t;
|
|---|
| 109 |
|
|---|
| 110 | typedef struct {
|
|---|
| 111 | uint32_t ipv4;
|
|---|
| 112 | } netaddr_t;
|
|---|
| 113 |
|
|---|
| 114 | typedef struct {
|
|---|
| 115 | netaddr_t addr;
|
|---|
| 116 | uint16_t port;
|
|---|
| 117 | } tcp_sock_t;
|
|---|
| 118 |
|
|---|
| 119 | typedef struct {
|
|---|
| 120 | tcp_sock_t local;
|
|---|
| 121 | tcp_sock_t foreign;
|
|---|
| 122 | } tcp_sockpair_t;
|
|---|
| 123 |
|
|---|
| 124 | /** Connection incoming segments queue */
|
|---|
| 125 | typedef struct {
|
|---|
| 126 | struct tcp_conn *conn;
|
|---|
| 127 | list_t list;
|
|---|
| 128 | } tcp_iqueue_t;
|
|---|
| 129 |
|
|---|
| 130 | /** Retransmission queue */
|
|---|
| 131 | typedef struct {
|
|---|
| 132 | struct tcp_conn *conn;
|
|---|
| 133 | list_t list;
|
|---|
| 134 |
|
|---|
| 135 | /** Retransmission timer */
|
|---|
| 136 | fibril_timer_t *timer;
|
|---|
| 137 | } tcp_tqueue_t;
|
|---|
| 138 |
|
|---|
| 139 | typedef struct tcp_conn {
|
|---|
| 140 | char *name;
|
|---|
| 141 | link_t link;
|
|---|
| 142 |
|
|---|
| 143 | /** Connection identification (local and foreign socket) */
|
|---|
| 144 | tcp_sockpair_t ident;
|
|---|
| 145 |
|
|---|
| 146 | /** Connection state */
|
|---|
| 147 | tcp_cstate_t cstate;
|
|---|
| 148 |
|
|---|
| 149 | /** Set when FIN is removed from the retransmission queue */
|
|---|
| 150 | bool fin_is_acked;
|
|---|
| 151 |
|
|---|
| 152 | /** Queue of incoming segments */
|
|---|
| 153 | tcp_iqueue_t incoming;
|
|---|
| 154 |
|
|---|
| 155 | /** Retransmission queue */
|
|---|
| 156 | tcp_tqueue_t retransmit;
|
|---|
| 157 |
|
|---|
| 158 | /** Time-Wait timeout timer */
|
|---|
| 159 | fibril_timer_t *tw_timer;
|
|---|
| 160 |
|
|---|
| 161 | /** Receive buffer */
|
|---|
| 162 | uint8_t *rcv_buf;
|
|---|
| 163 | /** Receive buffer size */
|
|---|
| 164 | size_t rcv_buf_size;
|
|---|
| 165 | /** Receive buffer number of bytes used */
|
|---|
| 166 | size_t rcv_buf_used;
|
|---|
| 167 | /** Receive buffer contains FIN */
|
|---|
| 168 | bool rcv_buf_fin;
|
|---|
| 169 | /** Receive buffer lock */
|
|---|
| 170 | fibril_mutex_t rcv_buf_lock;
|
|---|
| 171 | /** Receive buffer CV. Broadcast when new data is inserted */
|
|---|
| 172 | fibril_condvar_t rcv_buf_cv;
|
|---|
| 173 |
|
|---|
| 174 | /** Send buffer */
|
|---|
| 175 | uint8_t *snd_buf;
|
|---|
| 176 | /** Send buffer size */
|
|---|
| 177 | size_t snd_buf_size;
|
|---|
| 178 | /** Send buffer number of bytes used */
|
|---|
| 179 | size_t snd_buf_used;
|
|---|
| 180 | /** Send buffer contains FIN */
|
|---|
| 181 | bool snd_buf_fin;
|
|---|
| 182 |
|
|---|
| 183 | /** Send unacknowledged */
|
|---|
| 184 | uint32_t snd_una;
|
|---|
| 185 | /** Send next */
|
|---|
| 186 | uint32_t snd_nxt;
|
|---|
| 187 | /** Send window */
|
|---|
| 188 | uint32_t snd_wnd;
|
|---|
| 189 | /** Send urgent pointer */
|
|---|
| 190 | uint32_t snd_up;
|
|---|
| 191 | /** Segment sequence number used for last window update */
|
|---|
| 192 | uint32_t snd_wl1;
|
|---|
| 193 | /** Segment acknowledgement number used for last window update */
|
|---|
| 194 | uint32_t snd_wl2;
|
|---|
| 195 | /** Initial send sequence number */
|
|---|
| 196 | uint32_t iss;
|
|---|
| 197 |
|
|---|
| 198 | /** Receive next */
|
|---|
| 199 | uint32_t rcv_nxt;
|
|---|
| 200 | /** Receive window */
|
|---|
| 201 | uint32_t rcv_wnd;
|
|---|
| 202 | /** Receive urgent pointer */
|
|---|
| 203 | uint32_t rcv_up;
|
|---|
| 204 | /** Initial receive sequence number */
|
|---|
| 205 | uint32_t irs;
|
|---|
| 206 | } tcp_conn_t;
|
|---|
| 207 |
|
|---|
| 208 | typedef struct {
|
|---|
| 209 | unsigned dummy;
|
|---|
| 210 | } tcp_conn_status_t;
|
|---|
| 211 |
|
|---|
| 212 | typedef struct {
|
|---|
| 213 | /** SYN, FIN */
|
|---|
| 214 | tcp_control_t ctrl;
|
|---|
| 215 |
|
|---|
| 216 | /** Segment sequence number */
|
|---|
| 217 | uint32_t seq;
|
|---|
| 218 | /** Segment acknowledgement number */
|
|---|
| 219 | uint32_t ack;
|
|---|
| 220 | /** Segment length in sequence space */
|
|---|
| 221 | uint32_t len;
|
|---|
| 222 | /** Segment window */
|
|---|
| 223 | uint32_t wnd;
|
|---|
| 224 | /** Segment urgent pointer */
|
|---|
| 225 | uint32_t up;
|
|---|
| 226 |
|
|---|
| 227 | /** Segment data, may be moved when trimming segment */
|
|---|
| 228 | void *data;
|
|---|
| 229 | /** Segment data, original pointer used to free data */
|
|---|
| 230 | void *dfptr;
|
|---|
| 231 | } tcp_segment_t;
|
|---|
| 232 |
|
|---|
| 233 | typedef enum {
|
|---|
| 234 | ap_active,
|
|---|
| 235 | ap_passive
|
|---|
| 236 | } acpass_t;
|
|---|
| 237 |
|
|---|
| 238 | typedef struct {
|
|---|
| 239 | link_t link;
|
|---|
| 240 | tcp_sockpair_t sp;
|
|---|
| 241 | tcp_segment_t *seg;
|
|---|
| 242 | } tcp_rqueue_entry_t;
|
|---|
| 243 |
|
|---|
| 244 | /** NCSim queue entry */
|
|---|
| 245 | typedef struct {
|
|---|
| 246 | link_t link;
|
|---|
| 247 | suseconds_t delay;
|
|---|
| 248 | tcp_sockpair_t sp;
|
|---|
| 249 | tcp_segment_t *seg;
|
|---|
| 250 | } tcp_squeue_entry_t;
|
|---|
| 251 |
|
|---|
| 252 | typedef struct {
|
|---|
| 253 | link_t link;
|
|---|
| 254 | tcp_segment_t *seg;
|
|---|
| 255 | } tcp_iqueue_entry_t;
|
|---|
| 256 |
|
|---|
| 257 | /** Retransmission queue entry */
|
|---|
| 258 | typedef struct {
|
|---|
| 259 | link_t link;
|
|---|
| 260 | tcp_conn_t *conn;
|
|---|
| 261 | tcp_segment_t *seg;
|
|---|
| 262 | } tcp_tqueue_entry_t;
|
|---|
| 263 |
|
|---|
| 264 | typedef enum {
|
|---|
| 265 | cp_continue,
|
|---|
| 266 | cp_done
|
|---|
| 267 | } cproc_t;
|
|---|
| 268 |
|
|---|
| 269 | #endif
|
|---|
| 270 |
|
|---|
| 271 | /** @}
|
|---|
| 272 | */
|
|---|