| 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 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file TCP entry points (close to those defined in the RFC)
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <fibril_synch.h>
|
|---|
| 39 | #include <io/log.h>
|
|---|
| 40 | #include <macros.h>
|
|---|
| 41 | #include <mem.h>
|
|---|
| 42 | #include "conn.h"
|
|---|
| 43 | #include "tcp_type.h"
|
|---|
| 44 | #include "tqueue.h"
|
|---|
| 45 | #include "ucall.h"
|
|---|
| 46 |
|
|---|
| 47 | /*
|
|---|
| 48 | * User calls
|
|---|
| 49 | */
|
|---|
| 50 |
|
|---|
| 51 | /** OPEN user call
|
|---|
| 52 | *
|
|---|
| 53 | * @param epp Endpoint pair
|
|---|
| 54 | * @param acpass Active/passive
|
|---|
| 55 | * @param oflags Open flags
|
|---|
| 56 | * @param conn Connection
|
|---|
| 57 | *
|
|---|
| 58 | * Unlike in the spec we allow specifying the local address. This means
|
|---|
| 59 | * the implementation does not need to magically guess it, especially
|
|---|
| 60 | * considering there can be more than one local address.
|
|---|
| 61 | *
|
|---|
| 62 | * XXX We should be able to call active open on an existing listening
|
|---|
| 63 | * connection.
|
|---|
| 64 | * XXX We should be able to get connection structure immediately, before
|
|---|
| 65 | * establishment.
|
|---|
| 66 | */
|
|---|
| 67 | tcp_error_t tcp_uc_open(inet_ep2_t *epp, acpass_t acpass,
|
|---|
| 68 | tcp_open_flags_t oflags, tcp_conn_t **conn)
|
|---|
| 69 | {
|
|---|
| 70 | tcp_conn_t *nconn;
|
|---|
| 71 | int rc;
|
|---|
| 72 |
|
|---|
| 73 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %s, %s, %p)",
|
|---|
| 74 | epp, acpass == ap_active ? "active" : "passive",
|
|---|
| 75 | oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
|
|---|
| 76 |
|
|---|
| 77 | nconn = tcp_conn_new(epp);
|
|---|
| 78 | rc = tcp_conn_add(nconn);
|
|---|
| 79 | if (rc != EOK) {
|
|---|
| 80 | tcp_conn_delete(nconn);
|
|---|
| 81 | return TCP_EEXISTS;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | tcp_conn_lock(nconn);
|
|---|
| 85 |
|
|---|
| 86 | if (acpass == ap_active) {
|
|---|
| 87 | /* Synchronize (initiate) connection */
|
|---|
| 88 | tcp_conn_sync(nconn);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (oflags == tcp_open_nonblock) {
|
|---|
| 92 | tcp_conn_unlock(nconn);
|
|---|
| 93 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
|
|---|
| 94 | *conn = nconn;
|
|---|
| 95 | return TCP_EOK;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /* Wait for connection to be established or reset */
|
|---|
| 99 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Wait for connection.");
|
|---|
| 100 | while (nconn->cstate == st_listen ||
|
|---|
| 101 | nconn->cstate == st_syn_sent ||
|
|---|
| 102 | nconn->cstate == st_syn_received) {
|
|---|
| 103 | fibril_condvar_wait(&nconn->cstate_cv, &nconn->lock);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | if (nconn->cstate != st_established) {
|
|---|
| 107 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was reset.");
|
|---|
| 108 | assert(nconn->cstate == st_closed);
|
|---|
| 109 | tcp_conn_unlock(nconn);
|
|---|
| 110 | return TCP_ERESET;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | tcp_conn_unlock(nconn);
|
|---|
| 114 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was established.");
|
|---|
| 115 |
|
|---|
| 116 | *conn = nconn;
|
|---|
| 117 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
|
|---|
| 118 | return TCP_EOK;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /** SEND user call */
|
|---|
| 122 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
|---|
| 123 | xflags_t flags)
|
|---|
| 124 | {
|
|---|
| 125 | size_t buf_free;
|
|---|
| 126 | size_t xfer_size;
|
|---|
| 127 |
|
|---|
| 128 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
|
|---|
| 129 |
|
|---|
| 130 | tcp_conn_lock(conn);
|
|---|
| 131 |
|
|---|
| 132 | if (conn->cstate == st_closed) {
|
|---|
| 133 | tcp_conn_unlock(conn);
|
|---|
| 134 | return TCP_ENOTEXIST;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | if (conn->cstate == st_listen) {
|
|---|
| 138 | /* Change connection to active */
|
|---|
| 139 | tcp_conn_sync(conn);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | if (conn->snd_buf_fin) {
|
|---|
| 144 | tcp_conn_unlock(conn);
|
|---|
| 145 | return TCP_ECLOSING;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | while (size > 0) {
|
|---|
| 149 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
|---|
| 150 | while (buf_free == 0 && !conn->reset) {
|
|---|
| 151 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
|
|---|
| 152 | conn->name);
|
|---|
| 153 | fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
|
|---|
| 154 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | if (conn->reset) {
|
|---|
| 158 | tcp_conn_unlock(conn);
|
|---|
| 159 | return TCP_ERESET;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | xfer_size = min(size, buf_free);
|
|---|
| 163 |
|
|---|
| 164 | /* Copy data to buffer */
|
|---|
| 165 | memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
|
|---|
| 166 | data += xfer_size;
|
|---|
| 167 | conn->snd_buf_used += xfer_size;
|
|---|
| 168 | size -= xfer_size;
|
|---|
| 169 |
|
|---|
| 170 | tcp_tqueue_new_data(conn);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | tcp_tqueue_new_data(conn);
|
|---|
| 174 | tcp_conn_unlock(conn);
|
|---|
| 175 |
|
|---|
| 176 | return TCP_EOK;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** RECEIVE user call */
|
|---|
| 180 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
|---|
| 181 | size_t *rcvd, xflags_t *xflags)
|
|---|
| 182 | {
|
|---|
| 183 | size_t xfer_size;
|
|---|
| 184 |
|
|---|
| 185 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
|---|
| 186 |
|
|---|
| 187 | tcp_conn_lock(conn);
|
|---|
| 188 |
|
|---|
| 189 | if (conn->cstate == st_closed) {
|
|---|
| 190 | tcp_conn_unlock(conn);
|
|---|
| 191 | return TCP_ENOTEXIST;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /* Wait for data to become available */
|
|---|
| 195 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
|---|
| 196 | tcp_conn_unlock(conn);
|
|---|
| 197 | return TCP_EAGAIN;
|
|---|
| 198 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
|---|
| 199 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | if (conn->rcv_buf_used == 0) {
|
|---|
| 203 | *rcvd = 0;
|
|---|
| 204 | *xflags = 0;
|
|---|
| 205 |
|
|---|
| 206 | if (conn->rcv_buf_fin) {
|
|---|
| 207 | /* End of data, peer closed connection */
|
|---|
| 208 | tcp_conn_unlock(conn);
|
|---|
| 209 | return TCP_ECLOSING;
|
|---|
| 210 | } else {
|
|---|
| 211 | /* Connection was reset */
|
|---|
| 212 | assert(conn->reset);
|
|---|
| 213 | tcp_conn_unlock(conn);
|
|---|
| 214 | return TCP_ERESET;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /* Copy data from receive buffer to user buffer */
|
|---|
| 219 | xfer_size = min(size, conn->rcv_buf_used);
|
|---|
| 220 | memcpy(buf, conn->rcv_buf, xfer_size);
|
|---|
| 221 | *rcvd = xfer_size;
|
|---|
| 222 |
|
|---|
| 223 | /* Remove data from receive buffer */
|
|---|
| 224 | memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
|
|---|
| 225 | xfer_size);
|
|---|
| 226 | conn->rcv_buf_used -= xfer_size;
|
|---|
| 227 | conn->rcv_wnd += xfer_size;
|
|---|
| 228 |
|
|---|
| 229 | /* TODO */
|
|---|
| 230 | *xflags = 0;
|
|---|
| 231 |
|
|---|
| 232 | /* Send new size of receive window */
|
|---|
| 233 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
|---|
| 234 |
|
|---|
| 235 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
|---|
| 236 | conn->name, xfer_size);
|
|---|
| 237 |
|
|---|
| 238 | tcp_conn_unlock(conn);
|
|---|
| 239 |
|
|---|
| 240 | return TCP_EOK;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /** CLOSE user call */
|
|---|
| 244 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
|---|
| 245 | {
|
|---|
| 246 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
|
|---|
| 247 | conn);
|
|---|
| 248 |
|
|---|
| 249 | tcp_conn_lock(conn);
|
|---|
| 250 |
|
|---|
| 251 | if (conn->cstate == st_closed) {
|
|---|
| 252 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
|
|---|
| 253 | tcp_conn_unlock(conn);
|
|---|
| 254 | return TCP_ENOTEXIST;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (conn->cstate == st_listen || conn->cstate == st_syn_sent) {
|
|---|
| 258 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - listen/syn_sent");
|
|---|
| 259 | tcp_conn_reset(conn);
|
|---|
| 260 | tcp_conn_unlock(conn);
|
|---|
| 261 | return TCP_EOK;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | if (conn->snd_buf_fin) {
|
|---|
| 265 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
|
|---|
| 266 | tcp_conn_unlock(conn);
|
|---|
| 267 | return TCP_ECLOSING;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
|
|---|
| 271 | conn->snd_buf_fin = true;
|
|---|
| 272 | tcp_tqueue_new_data(conn);
|
|---|
| 273 |
|
|---|
| 274 | tcp_conn_unlock(conn);
|
|---|
| 275 | return TCP_EOK;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /** ABORT user call */
|
|---|
| 279 | void tcp_uc_abort(tcp_conn_t *conn)
|
|---|
| 280 | {
|
|---|
| 281 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** STATUS user call */
|
|---|
| 285 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
|---|
| 286 | {
|
|---|
| 287 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
|
|---|
| 288 | cstatus->cstate = conn->cstate;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /** Delete connection user call.
|
|---|
| 292 | *
|
|---|
| 293 | * (Not in spec.) Inform TCP that the user is done with this connection
|
|---|
| 294 | * and will not make any further calls/references to it. TCP can deallocate
|
|---|
| 295 | * the connection from now on.
|
|---|
| 296 | */
|
|---|
| 297 | void tcp_uc_delete(tcp_conn_t *conn)
|
|---|
| 298 | {
|
|---|
| 299 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
|
|---|
| 300 | tcp_conn_delete(conn);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
|
|---|
| 304 | {
|
|---|
| 305 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
|
|---|
| 306 | conn, cb, arg);
|
|---|
| 307 |
|
|---|
| 308 | conn->cb = cb;
|
|---|
| 309 | conn->cb_arg = arg;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | void *tcp_uc_get_userptr(tcp_conn_t *conn)
|
|---|
| 313 | {
|
|---|
| 314 | return conn->cb_arg;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | /*
|
|---|
| 318 | * Arriving segments
|
|---|
| 319 | */
|
|---|
| 320 |
|
|---|
| 321 | /** Segment arrived */
|
|---|
| 322 | void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
|
|---|
| 323 | {
|
|---|
| 324 | tcp_conn_t *conn;
|
|---|
| 325 |
|
|---|
| 326 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
|---|
| 327 | "tcp_as_segment_arrived(f:(%u), l:(%u))",
|
|---|
| 328 | epp->remote.port, epp->local.port);
|
|---|
| 329 |
|
|---|
| 330 | conn = tcp_conn_find_ref(epp);
|
|---|
| 331 | if (conn == NULL) {
|
|---|
| 332 | log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
|
|---|
| 333 | tcp_unexpected_segment(epp, seg);
|
|---|
| 334 | return;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | tcp_conn_segment_arrived(conn, epp, seg);
|
|---|
| 338 | tcp_conn_delref(conn);
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /*
|
|---|
| 342 | * Timeouts
|
|---|
| 343 | */
|
|---|
| 344 |
|
|---|
| 345 | /** User timeout */
|
|---|
| 346 | void tcp_to_user(void)
|
|---|
| 347 | {
|
|---|
| 348 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /**
|
|---|
| 352 | * @}
|
|---|
| 353 | */
|
|---|