[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) {
|
---|
| 85 | *conn = nconn;
|
---|
| 86 | return TCP_EOK;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[a4ee3ab2] | 89 | /* Wait for connection to be established or reset */
|
---|
[a1a101d] | 90 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Wait for connection.");
|
---|
[0edaf0f6] | 91 | fibril_mutex_lock(&nconn->lock);
|
---|
[a4ee3ab2] | 92 | while (nconn->cstate == st_listen ||
|
---|
| 93 | nconn->cstate == st_syn_sent ||
|
---|
| 94 | nconn->cstate == st_syn_received) {
|
---|
[0edaf0f6] | 95 | fibril_condvar_wait(&nconn->cstate_cv, &nconn->lock);
|
---|
[a4ee3ab2] | 96 | }
|
---|
[004a5fe] | 97 |
|
---|
[a4ee3ab2] | 98 | if (nconn->cstate != st_established) {
|
---|
[a1a101d] | 99 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was reset.");
|
---|
[a4ee3ab2] | 100 | assert(nconn->cstate == st_closed);
|
---|
[0edaf0f6] | 101 | fibril_mutex_unlock(&nconn->lock);
|
---|
[a4ee3ab2] | 102 | return TCP_ERESET;
|
---|
[c5808b41] | 103 | }
|
---|
| 104 |
|
---|
[0edaf0f6] | 105 | fibril_mutex_unlock(&nconn->lock);
|
---|
[a1a101d] | 106 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was established.");
|
---|
[a4ee3ab2] | 107 |
|
---|
[c5808b41] | 108 | *conn = nconn;
|
---|
[a1a101d] | 109 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
|
---|
[004a5fe] | 110 | return TCP_EOK;
|
---|
[c5808b41] | 111 | }
|
---|
| 112 |
|
---|
| 113 | /** SEND user call */
|
---|
[854e79a6] | 114 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
---|
| 115 | xflags_t flags)
|
---|
[c5808b41] | 116 | {
|
---|
[32105348] | 117 | size_t buf_free;
|
---|
| 118 | size_t xfer_size;
|
---|
| 119 |
|
---|
[a1a101d] | 120 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
|
---|
[32105348] | 121 |
|
---|
[0edaf0f6] | 122 | fibril_mutex_lock(&conn->lock);
|
---|
| 123 |
|
---|
| 124 | if (conn->cstate == st_closed) {
|
---|
| 125 | fibril_mutex_unlock(&conn->lock);
|
---|
[854e79a6] | 126 | return TCP_ENOTEXIST;
|
---|
[0edaf0f6] | 127 | }
|
---|
[854e79a6] | 128 |
|
---|
| 129 | if (conn->cstate == st_listen) {
|
---|
| 130 | /* Change connection to active */
|
---|
| 131 | tcp_conn_sync(conn);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[bbf159a] | 134 |
|
---|
| 135 | if (conn->snd_buf_fin) {
|
---|
[0edaf0f6] | 136 | fibril_mutex_unlock(&conn->lock);
|
---|
[854e79a6] | 137 | return TCP_ECLOSING;
|
---|
[bbf159a] | 138 | }
|
---|
[854e79a6] | 139 |
|
---|
[32105348] | 140 | while (size > 0) {
|
---|
| 141 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
[0d29e0cd] | 142 | while (buf_free == 0 && !conn->reset) {
|
---|
[a1a101d] | 143 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
|
---|
[bbf159a] | 144 | conn->name);
|
---|
[0edaf0f6] | 145 | fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
|
---|
[0d29e0cd] | 146 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
| 147 | }
|
---|
[32105348] | 148 |
|
---|
[bbf159a] | 149 | if (conn->reset) {
|
---|
[0edaf0f6] | 150 | fibril_mutex_unlock(&conn->lock);
|
---|
[d9e14fa4] | 151 | return TCP_ERESET;
|
---|
[bbf159a] | 152 | }
|
---|
[d9e14fa4] | 153 |
|
---|
[32105348] | 154 | xfer_size = min(size, buf_free);
|
---|
| 155 |
|
---|
| 156 | /* Copy data to buffer */
|
---|
| 157 | memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
|
---|
| 158 | data += xfer_size;
|
---|
| 159 | conn->snd_buf_used += xfer_size;
|
---|
| 160 | size -= xfer_size;
|
---|
[bbf159a] | 161 |
|
---|
| 162 | tcp_tqueue_new_data(conn);
|
---|
[32105348] | 163 | }
|
---|
| 164 |
|
---|
| 165 | tcp_tqueue_new_data(conn);
|
---|
[0edaf0f6] | 166 | fibril_mutex_unlock(&conn->lock);
|
---|
[854e79a6] | 167 |
|
---|
| 168 | return TCP_EOK;
|
---|
[c5808b41] | 169 | }
|
---|
| 170 |
|
---|
| 171 | /** RECEIVE user call */
|
---|
[854e79a6] | 172 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
---|
| 173 | size_t *rcvd, xflags_t *xflags)
|
---|
[c5808b41] | 174 | {
|
---|
[d9ce049] | 175 | size_t xfer_size;
|
---|
| 176 |
|
---|
[a1a101d] | 177 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
---|
[d9ce049] | 178 |
|
---|
[0edaf0f6] | 179 | fibril_mutex_lock(&conn->lock);
|
---|
[7cf7ded] | 180 |
|
---|
[0edaf0f6] | 181 | if (conn->cstate == st_closed) {
|
---|
| 182 | fibril_mutex_unlock(&conn->lock);
|
---|
| 183 | return TCP_ENOTEXIST;
|
---|
| 184 | }
|
---|
[d9ce049] | 185 |
|
---|
| 186 | /* Wait for data to become available */
|
---|
[d9e14fa4] | 187 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
---|
[a1a101d] | 188 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
---|
[0edaf0f6] | 189 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
|
---|
[d9ce049] | 190 | }
|
---|
| 191 |
|
---|
[8c7a054] | 192 | if (conn->rcv_buf_used == 0) {
|
---|
| 193 | *rcvd = 0;
|
---|
| 194 | *xflags = 0;
|
---|
[d9e14fa4] | 195 |
|
---|
| 196 | if (conn->rcv_buf_fin) {
|
---|
| 197 | /* End of data, peer closed connection */
|
---|
[0edaf0f6] | 198 | fibril_mutex_unlock(&conn->lock);
|
---|
[d9e14fa4] | 199 | return TCP_ECLOSING;
|
---|
| 200 | } else {
|
---|
| 201 | /* Connection was reset */
|
---|
| 202 | assert(conn->reset);
|
---|
[0edaf0f6] | 203 | fibril_mutex_unlock(&conn->lock);
|
---|
[d9e14fa4] | 204 | return TCP_ERESET;
|
---|
| 205 | }
|
---|
[8c7a054] | 206 | }
|
---|
| 207 |
|
---|
[d9ce049] | 208 | /* Copy data from receive buffer to user buffer */
|
---|
| 209 | xfer_size = min(size, conn->rcv_buf_used);
|
---|
| 210 | memcpy(buf, conn->rcv_buf, xfer_size);
|
---|
| 211 | *rcvd = xfer_size;
|
---|
| 212 |
|
---|
| 213 | /* Remove data from receive buffer */
|
---|
| 214 | memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
|
---|
| 215 | xfer_size);
|
---|
| 216 | conn->rcv_buf_used -= xfer_size;
|
---|
| 217 | conn->rcv_wnd += xfer_size;
|
---|
| 218 |
|
---|
| 219 | /* TODO */
|
---|
| 220 | *xflags = 0;
|
---|
| 221 |
|
---|
| 222 | /* Send new size of receive window */
|
---|
| 223 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 224 |
|
---|
[a1a101d] | 225 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
---|
[23fe06c] | 226 | conn->name, xfer_size);
|
---|
[854e79a6] | 227 |
|
---|
[0edaf0f6] | 228 | fibril_mutex_unlock(&conn->lock);
|
---|
| 229 |
|
---|
[854e79a6] | 230 | return TCP_EOK;
|
---|
[c5808b41] | 231 | }
|
---|
| 232 |
|
---|
| 233 | /** CLOSE user call */
|
---|
[854e79a6] | 234 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
---|
[c5808b41] | 235 | {
|
---|
[a1a101d] | 236 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close()", conn->name);
|
---|
[8c7a054] | 237 |
|
---|
[0edaf0f6] | 238 | fibril_mutex_lock(&conn->lock);
|
---|
| 239 |
|
---|
| 240 | if (conn->cstate == st_closed) {
|
---|
| 241 | fibril_mutex_unlock(&conn->lock);
|
---|
[854e79a6] | 242 | return TCP_ENOTEXIST;
|
---|
[0edaf0f6] | 243 | }
|
---|
[854e79a6] | 244 |
|
---|
[0edaf0f6] | 245 | if (conn->snd_buf_fin) {
|
---|
| 246 | fibril_mutex_unlock(&conn->lock);
|
---|
[854e79a6] | 247 | return TCP_ECLOSING;
|
---|
[0edaf0f6] | 248 | }
|
---|
[854e79a6] | 249 |
|
---|
[8c7a054] | 250 | conn->snd_buf_fin = true;
|
---|
| 251 | tcp_tqueue_new_data(conn);
|
---|
[854e79a6] | 252 |
|
---|
[0edaf0f6] | 253 | fibril_mutex_unlock(&conn->lock);
|
---|
[854e79a6] | 254 | return TCP_EOK;
|
---|
[c5808b41] | 255 | }
|
---|
| 256 |
|
---|
| 257 | /** ABORT user call */
|
---|
| 258 | void tcp_uc_abort(tcp_conn_t *conn)
|
---|
| 259 | {
|
---|
[a1a101d] | 260 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
|
---|
[c5808b41] | 261 | }
|
---|
| 262 |
|
---|
| 263 | /** STATUS user call */
|
---|
| 264 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
---|
| 265 | {
|
---|
[a1a101d] | 266 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
|
---|
[ae481e0] | 267 | cstatus->cstate = conn->cstate;
|
---|
[c5808b41] | 268 | }
|
---|
| 269 |
|
---|
[7a8c1c4e] | 270 | /** Delete connection user call.
|
---|
| 271 | *
|
---|
| 272 | * (Not in spec.) Inform TCP that the user is done with this connection
|
---|
| 273 | * and will not make any further calls/references to it. TCP can deallocate
|
---|
| 274 | * the connection from now on.
|
---|
| 275 | */
|
---|
| 276 | void tcp_uc_delete(tcp_conn_t *conn)
|
---|
| 277 | {
|
---|
[a1a101d] | 278 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
|
---|
[7a8c1c4e] | 279 | tcp_conn_delete(conn);
|
---|
| 280 | }
|
---|
[c5808b41] | 281 |
|
---|
[ae481e0] | 282 | void tcp_uc_set_cstate_cb(tcp_conn_t *conn, tcp_cstate_cb_t cb, void *arg)
|
---|
| 283 | {
|
---|
[a1a101d] | 284 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_ctate_cb(%p, %p, %p)",
|
---|
[ae481e0] | 285 | conn, cb, arg);
|
---|
| 286 |
|
---|
| 287 | conn->cstate_cb = cb;
|
---|
| 288 | conn->cstate_cb_arg = arg;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[c5808b41] | 291 | /*
|
---|
| 292 | * Arriving segments
|
---|
| 293 | */
|
---|
| 294 |
|
---|
| 295 | /** Segment arrived */
|
---|
| 296 | void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 297 | {
|
---|
| 298 | tcp_conn_t *conn;
|
---|
| 299 |
|
---|
[0edaf0f6] | 300 | conn = tcp_conn_find_ref(sp);
|
---|
| 301 | if (conn == NULL) {
|
---|
[a1a101d] | 302 | log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
|
---|
[c5808b41] | 303 | tcp_unexpected_segment(sp, seg);
|
---|
[0edaf0f6] | 304 | return;
|
---|
[c5808b41] | 305 | }
|
---|
[0edaf0f6] | 306 |
|
---|
| 307 | fibril_mutex_lock(&conn->lock);
|
---|
| 308 |
|
---|
| 309 | if (conn->cstate == st_closed) {
|
---|
[a1a101d] | 310 | log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
|
---|
[0edaf0f6] | 311 | tcp_unexpected_segment(sp, seg);
|
---|
| 312 | fibril_mutex_unlock(&conn->lock);
|
---|
| 313 | tcp_conn_delref(conn);
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[a2e3ee6] | 317 | if (inet_addr_is_any(&conn->ident.foreign.addr))
|
---|
| 318 | conn->ident.foreign.addr = sp->foreign.addr;
|
---|
| 319 |
|
---|
[0edaf0f6] | 320 | if (conn->ident.foreign.port == TCP_PORT_ANY)
|
---|
| 321 | conn->ident.foreign.port = sp->foreign.port;
|
---|
[a2e3ee6] | 322 |
|
---|
| 323 | if (inet_addr_is_any(&conn->ident.local.addr))
|
---|
| 324 | conn->ident.local.addr = sp->local.addr;
|
---|
[0edaf0f6] | 325 |
|
---|
| 326 | tcp_conn_segment_arrived(conn, seg);
|
---|
| 327 |
|
---|
| 328 | fibril_mutex_unlock(&conn->lock);
|
---|
| 329 | tcp_conn_delref(conn);
|
---|
[c5808b41] | 330 | }
|
---|
| 331 |
|
---|
| 332 | /*
|
---|
| 333 | * Timeouts
|
---|
| 334 | */
|
---|
| 335 |
|
---|
| 336 | /** User timeout */
|
---|
| 337 | void tcp_to_user(void)
|
---|
| 338 | {
|
---|
[a1a101d] | 339 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
|
---|
[c5808b41] | 340 | }
|
---|
| 341 |
|
---|
| 342 | /**
|
---|
| 343 | * @}
|
---|
| 344 | */
|
---|