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