[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 | if (conn->snd_buf_fin) {
|
---|
[78192cc7] | 144 | tcp_conn_unlock(conn);
|
---|
[854e79a6] | 145 | return TCP_ECLOSING;
|
---|
[bbf159a] | 146 | }
|
---|
[854e79a6] | 147 |
|
---|
[32105348] | 148 | while (size > 0) {
|
---|
| 149 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
[0d29e0cd] | 150 | while (buf_free == 0 && !conn->reset) {
|
---|
[a1a101d] | 151 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
|
---|
[bbf159a] | 152 | conn->name);
|
---|
[0edaf0f6] | 153 | fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
|
---|
[0d29e0cd] | 154 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
| 155 | }
|
---|
[32105348] | 156 |
|
---|
[bbf159a] | 157 | if (conn->reset) {
|
---|
[78192cc7] | 158 | tcp_conn_unlock(conn);
|
---|
[d9e14fa4] | 159 | return TCP_ERESET;
|
---|
[bbf159a] | 160 | }
|
---|
[d9e14fa4] | 161 |
|
---|
[32105348] | 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;
|
---|
[bbf159a] | 169 |
|
---|
| 170 | tcp_tqueue_new_data(conn);
|
---|
[32105348] | 171 | }
|
---|
| 172 |
|
---|
| 173 | tcp_tqueue_new_data(conn);
|
---|
[78192cc7] | 174 | tcp_conn_unlock(conn);
|
---|
[854e79a6] | 175 |
|
---|
| 176 | return TCP_EOK;
|
---|
[c5808b41] | 177 | }
|
---|
| 178 |
|
---|
| 179 | /** RECEIVE user call */
|
---|
[854e79a6] | 180 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
---|
| 181 | size_t *rcvd, xflags_t *xflags)
|
---|
[c5808b41] | 182 | {
|
---|
[d9ce049] | 183 | size_t xfer_size;
|
---|
| 184 |
|
---|
[a1a101d] | 185 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
---|
[d9ce049] | 186 |
|
---|
[78192cc7] | 187 | tcp_conn_lock(conn);
|
---|
[7cf7ded] | 188 |
|
---|
[0edaf0f6] | 189 | if (conn->cstate == st_closed) {
|
---|
[78192cc7] | 190 | tcp_conn_unlock(conn);
|
---|
[0edaf0f6] | 191 | return TCP_ENOTEXIST;
|
---|
| 192 | }
|
---|
[d9ce049] | 193 |
|
---|
| 194 | /* Wait for data to become available */
|
---|
[d9e14fa4] | 195 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
---|
[779541b] | 196 | tcp_conn_unlock(conn);
|
---|
| 197 | return TCP_EAGAIN;
|
---|
[a1a101d] | 198 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
---|
[0edaf0f6] | 199 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
|
---|
[d9ce049] | 200 | }
|
---|
| 201 |
|
---|
[8c7a054] | 202 | if (conn->rcv_buf_used == 0) {
|
---|
| 203 | *rcvd = 0;
|
---|
| 204 | *xflags = 0;
|
---|
[d9e14fa4] | 205 |
|
---|
| 206 | if (conn->rcv_buf_fin) {
|
---|
| 207 | /* End of data, peer closed connection */
|
---|
[78192cc7] | 208 | tcp_conn_unlock(conn);
|
---|
[d9e14fa4] | 209 | return TCP_ECLOSING;
|
---|
| 210 | } else {
|
---|
| 211 | /* Connection was reset */
|
---|
| 212 | assert(conn->reset);
|
---|
[78192cc7] | 213 | tcp_conn_unlock(conn);
|
---|
[d9e14fa4] | 214 | return TCP_ERESET;
|
---|
| 215 | }
|
---|
[8c7a054] | 216 | }
|
---|
| 217 |
|
---|
[d9ce049] | 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 |
|
---|
[a1a101d] | 235 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
---|
[23fe06c] | 236 | conn->name, xfer_size);
|
---|
[854e79a6] | 237 |
|
---|
[78192cc7] | 238 | tcp_conn_unlock(conn);
|
---|
[0edaf0f6] | 239 |
|
---|
[854e79a6] | 240 | return TCP_EOK;
|
---|
[c5808b41] | 241 | }
|
---|
| 242 |
|
---|
| 243 | /** CLOSE user call */
|
---|
[854e79a6] | 244 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
---|
[c5808b41] | 245 | {
|
---|
[b243da3] | 246 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
|
---|
| 247 | conn);
|
---|
[8c7a054] | 248 |
|
---|
[78192cc7] | 249 | tcp_conn_lock(conn);
|
---|
[0edaf0f6] | 250 |
|
---|
| 251 | if (conn->cstate == st_closed) {
|
---|
[b243da3] | 252 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
|
---|
[78192cc7] | 253 | tcp_conn_unlock(conn);
|
---|
[854e79a6] | 254 | return TCP_ENOTEXIST;
|
---|
[0edaf0f6] | 255 | }
|
---|
[854e79a6] | 256 |
|
---|
[b243da3] | 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);
|
---|
[78192cc7] | 260 | tcp_conn_unlock(conn);
|
---|
[b243da3] | 261 | return TCP_EOK;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[0edaf0f6] | 264 | if (conn->snd_buf_fin) {
|
---|
[b243da3] | 265 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
|
---|
[78192cc7] | 266 | tcp_conn_unlock(conn);
|
---|
[854e79a6] | 267 | return TCP_ECLOSING;
|
---|
[0edaf0f6] | 268 | }
|
---|
[854e79a6] | 269 |
|
---|
[b243da3] | 270 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
|
---|
[8c7a054] | 271 | conn->snd_buf_fin = true;
|
---|
| 272 | tcp_tqueue_new_data(conn);
|
---|
[854e79a6] | 273 |
|
---|
[78192cc7] | 274 | tcp_conn_unlock(conn);
|
---|
[854e79a6] | 275 | return TCP_EOK;
|
---|
[c5808b41] | 276 | }
|
---|
| 277 |
|
---|
| 278 | /** ABORT user call */
|
---|
| 279 | void tcp_uc_abort(tcp_conn_t *conn)
|
---|
| 280 | {
|
---|
[a1a101d] | 281 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
|
---|
[797dc79e] | 282 |
|
---|
[3e2291a9] | 283 | tcp_conn_lock(conn);
|
---|
| 284 | tcp_conn_reset(conn);
|
---|
| 285 | tcp_conn_unlock(conn);
|
---|
[c5808b41] | 286 | }
|
---|
| 287 |
|
---|
| 288 | /** STATUS user call */
|
---|
| 289 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
---|
| 290 | {
|
---|
[a1a101d] | 291 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
|
---|
[ae481e0] | 292 | cstatus->cstate = conn->cstate;
|
---|
[c5808b41] | 293 | }
|
---|
| 294 |
|
---|
[7a8c1c4e] | 295 | /** Delete connection user call.
|
---|
| 296 | *
|
---|
| 297 | * (Not in spec.) Inform TCP that the user is done with this connection
|
---|
| 298 | * and will not make any further calls/references to it. TCP can deallocate
|
---|
| 299 | * the connection from now on.
|
---|
| 300 | */
|
---|
| 301 | void tcp_uc_delete(tcp_conn_t *conn)
|
---|
| 302 | {
|
---|
[a1a101d] | 303 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
|
---|
[7a8c1c4e] | 304 | tcp_conn_delete(conn);
|
---|
| 305 | }
|
---|
[c5808b41] | 306 |
|
---|
[779541b] | 307 | void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
|
---|
[ae481e0] | 308 | {
|
---|
[779541b] | 309 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
|
---|
[ae481e0] | 310 | conn, cb, arg);
|
---|
| 311 |
|
---|
[779541b] | 312 | conn->cb = cb;
|
---|
| 313 | conn->cb_arg = arg;
|
---|
[ae481e0] | 314 | }
|
---|
| 315 |
|
---|
[1d4b815] | 316 | void *tcp_uc_get_userptr(tcp_conn_t *conn)
|
---|
| 317 | {
|
---|
| 318 | return conn->cb_arg;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[c5808b41] | 321 | /*
|
---|
| 322 | * Arriving segments
|
---|
| 323 | */
|
---|
| 324 |
|
---|
| 325 | /** Segment arrived */
|
---|
[2f19103] | 326 | void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
[c5808b41] | 327 | {
|
---|
| 328 | tcp_conn_t *conn;
|
---|
| 329 |
|
---|
[c0f3460] | 330 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
| 331 | "tcp_as_segment_arrived(f:(%u), l:(%u))",
|
---|
[2f19103] | 332 | epp->remote.port, epp->local.port);
|
---|
[c0f3460] | 333 |
|
---|
[2f19103] | 334 | conn = tcp_conn_find_ref(epp);
|
---|
[0edaf0f6] | 335 | if (conn == NULL) {
|
---|
[a1a101d] | 336 | log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
|
---|
[2f19103] | 337 | tcp_unexpected_segment(epp, seg);
|
---|
[0edaf0f6] | 338 | return;
|
---|
[c5808b41] | 339 | }
|
---|
[0edaf0f6] | 340 |
|
---|
[2c4bb828] | 341 | tcp_conn_segment_arrived(conn, epp, seg);
|
---|
[0edaf0f6] | 342 | tcp_conn_delref(conn);
|
---|
[c5808b41] | 343 | }
|
---|
| 344 |
|
---|
| 345 | /*
|
---|
| 346 | * Timeouts
|
---|
| 347 | */
|
---|
| 348 |
|
---|
| 349 | /** User timeout */
|
---|
| 350 | void tcp_to_user(void)
|
---|
| 351 | {
|
---|
[a1a101d] | 352 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
|
---|
[c5808b41] | 353 | }
|
---|
| 354 |
|
---|
| 355 | /**
|
---|
| 356 | * @}
|
---|
| 357 | */
|
---|