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