[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
|
---|
| 55 | * @param conn Connection
|
---|
[854e79a6] | 56 | *
|
---|
[0ac2158] | 57 | * Unlike in the spec we allow specifying the local address. This means
|
---|
| 58 | * the implementation does not need to magically guess it, especially
|
---|
| 59 | * considering there can be more than one local address.
|
---|
| 60 | *
|
---|
[854e79a6] | 61 | * XXX We should be able to call active open on an existing listening
|
---|
| 62 | * connection.
|
---|
[a4ee3ab2] | 63 | * XXX We should be able to get connection structure immediately, before
|
---|
| 64 | * establishment.
|
---|
[c5808b41] | 65 | */
|
---|
[0ac2158] | 66 | tcp_error_t tcp_uc_open(tcp_sock_t *lsock, tcp_sock_t *fsock, acpass_t acpass,
|
---|
[c5808b41] | 67 | tcp_conn_t **conn)
|
---|
| 68 | {
|
---|
| 69 | tcp_conn_t *nconn;
|
---|
| 70 |
|
---|
[0ac2158] | 71 | log_msg(LVL_DEBUG, "tcp_uc_open(%p, %p, %s, %p)",
|
---|
| 72 | lsock, fsock, acpass == ap_active ? "active" : "passive",
|
---|
[c5808b41] | 73 | conn);
|
---|
| 74 |
|
---|
[0ac2158] | 75 | nconn = tcp_conn_new(lsock, fsock);
|
---|
[c5808b41] | 76 | tcp_conn_add(nconn);
|
---|
| 77 |
|
---|
| 78 | if (acpass == ap_active) {
|
---|
| 79 | /* Synchronize (initiate) connection */
|
---|
| 80 | tcp_conn_sync(nconn);
|
---|
[a4ee3ab2] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /* Wait for connection to be established or reset */
|
---|
| 84 | log_msg(LVL_DEBUG, "tcp_uc_open: Wait for connection.");
|
---|
| 85 | fibril_mutex_lock(&nconn->cstate_lock);
|
---|
| 86 | while (nconn->cstate == st_listen ||
|
---|
| 87 | nconn->cstate == st_syn_sent ||
|
---|
| 88 | nconn->cstate == st_syn_received) {
|
---|
| 89 | fibril_condvar_wait(&nconn->cstate_cv, &nconn->cstate_lock);
|
---|
| 90 | }
|
---|
[004a5fe] | 91 |
|
---|
[a4ee3ab2] | 92 | if (nconn->cstate != st_established) {
|
---|
| 93 | log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset.");
|
---|
| 94 | assert(nconn->cstate == st_closed);
|
---|
[004a5fe] | 95 | fibril_mutex_unlock(&nconn->cstate_lock);
|
---|
[a4ee3ab2] | 96 | return TCP_ERESET;
|
---|
[c5808b41] | 97 | }
|
---|
| 98 |
|
---|
[a4ee3ab2] | 99 | fibril_mutex_unlock(&nconn->cstate_lock);
|
---|
| 100 | log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established.");
|
---|
| 101 |
|
---|
[c5808b41] | 102 | *conn = nconn;
|
---|
[004a5fe] | 103 | return TCP_EOK;
|
---|
[c5808b41] | 104 | }
|
---|
| 105 |
|
---|
| 106 | /** SEND user call */
|
---|
[854e79a6] | 107 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
---|
| 108 | xflags_t flags)
|
---|
[c5808b41] | 109 | {
|
---|
[32105348] | 110 | size_t buf_free;
|
---|
| 111 | size_t xfer_size;
|
---|
| 112 |
|
---|
[23fe06c] | 113 | log_msg(LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
|
---|
[32105348] | 114 |
|
---|
[854e79a6] | 115 | if (conn->cstate == st_closed)
|
---|
| 116 | return TCP_ENOTEXIST;
|
---|
| 117 |
|
---|
| 118 | if (conn->cstate == st_listen) {
|
---|
| 119 | /* Change connection to active */
|
---|
| 120 | tcp_conn_sync(conn);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | if (conn->snd_buf_fin)
|
---|
| 124 | return TCP_ECLOSING;
|
---|
| 125 |
|
---|
[32105348] | 126 | while (size > 0) {
|
---|
| 127 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
[d9e14fa4] | 128 | while (buf_free == 0 && !conn->reset)
|
---|
[32105348] | 129 | tcp_tqueue_new_data(conn);
|
---|
| 130 |
|
---|
[d9e14fa4] | 131 | if (conn->reset)
|
---|
| 132 | return TCP_ERESET;
|
---|
| 133 |
|
---|
[32105348] | 134 | xfer_size = min(size, buf_free);
|
---|
| 135 |
|
---|
| 136 | /* Copy data to buffer */
|
---|
| 137 | memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
|
---|
| 138 | data += xfer_size;
|
---|
| 139 | conn->snd_buf_used += xfer_size;
|
---|
| 140 | size -= xfer_size;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | tcp_tqueue_new_data(conn);
|
---|
[854e79a6] | 144 |
|
---|
| 145 | return TCP_EOK;
|
---|
[c5808b41] | 146 | }
|
---|
| 147 |
|
---|
| 148 | /** RECEIVE user call */
|
---|
[854e79a6] | 149 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
---|
| 150 | size_t *rcvd, xflags_t *xflags)
|
---|
[c5808b41] | 151 | {
|
---|
[d9ce049] | 152 | size_t xfer_size;
|
---|
| 153 |
|
---|
[23fe06c] | 154 | log_msg(LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
---|
[d9ce049] | 155 |
|
---|
[7cf7ded] | 156 | if (conn->cstate == st_closed)
|
---|
[854e79a6] | 157 | return TCP_ENOTEXIST;
|
---|
[7cf7ded] | 158 |
|
---|
[d9ce049] | 159 | fibril_mutex_lock(&conn->rcv_buf_lock);
|
---|
| 160 |
|
---|
| 161 | /* Wait for data to become available */
|
---|
[d9e14fa4] | 162 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
---|
[d9ce049] | 163 | log_msg(LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
---|
| 164 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->rcv_buf_lock);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[8c7a054] | 167 | if (conn->rcv_buf_used == 0) {
|
---|
[b0d82d1] | 168 | fibril_mutex_unlock(&conn->rcv_buf_lock);
|
---|
| 169 |
|
---|
[8c7a054] | 170 | *rcvd = 0;
|
---|
| 171 | *xflags = 0;
|
---|
[d9e14fa4] | 172 |
|
---|
| 173 | if (conn->rcv_buf_fin) {
|
---|
| 174 | /* End of data, peer closed connection */
|
---|
| 175 | return TCP_ECLOSING;
|
---|
| 176 | } else {
|
---|
| 177 | /* Connection was reset */
|
---|
| 178 | assert(conn->reset);
|
---|
| 179 | return TCP_ERESET;
|
---|
| 180 | }
|
---|
[8c7a054] | 181 | }
|
---|
| 182 |
|
---|
[d9ce049] | 183 | /* Copy data from receive buffer to user buffer */
|
---|
| 184 | xfer_size = min(size, conn->rcv_buf_used);
|
---|
| 185 | memcpy(buf, conn->rcv_buf, xfer_size);
|
---|
| 186 | *rcvd = xfer_size;
|
---|
| 187 |
|
---|
| 188 | /* Remove data from receive buffer */
|
---|
| 189 | memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
|
---|
| 190 | xfer_size);
|
---|
| 191 | conn->rcv_buf_used -= xfer_size;
|
---|
| 192 | conn->rcv_wnd += xfer_size;
|
---|
| 193 |
|
---|
| 194 | fibril_mutex_unlock(&conn->rcv_buf_lock);
|
---|
| 195 |
|
---|
| 196 | /* TODO */
|
---|
| 197 | *xflags = 0;
|
---|
| 198 |
|
---|
| 199 | /* Send new size of receive window */
|
---|
| 200 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
| 201 |
|
---|
[23fe06c] | 202 | log_msg(LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
---|
| 203 | conn->name, xfer_size);
|
---|
[854e79a6] | 204 |
|
---|
| 205 | return TCP_EOK;
|
---|
[c5808b41] | 206 | }
|
---|
| 207 |
|
---|
| 208 | /** CLOSE user call */
|
---|
[854e79a6] | 209 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
---|
[c5808b41] | 210 | {
|
---|
[23fe06c] | 211 | log_msg(LVL_DEBUG, "%s: tcp_uc_close()", conn->name);
|
---|
[8c7a054] | 212 |
|
---|
[854e79a6] | 213 | if (conn->cstate == st_closed)
|
---|
| 214 | return TCP_ENOTEXIST;
|
---|
| 215 |
|
---|
| 216 | if (conn->snd_buf_fin)
|
---|
| 217 | return TCP_ECLOSING;
|
---|
| 218 |
|
---|
[8c7a054] | 219 | conn->snd_buf_fin = true;
|
---|
| 220 | tcp_tqueue_new_data(conn);
|
---|
[854e79a6] | 221 |
|
---|
| 222 | return TCP_EOK;
|
---|
[c5808b41] | 223 | }
|
---|
| 224 |
|
---|
| 225 | /** ABORT user call */
|
---|
| 226 | void tcp_uc_abort(tcp_conn_t *conn)
|
---|
| 227 | {
|
---|
| 228 | log_msg(LVL_DEBUG, "tcp_uc_abort()");
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** STATUS user call */
|
---|
| 232 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
---|
| 233 | {
|
---|
| 234 | log_msg(LVL_DEBUG, "tcp_uc_status()");
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | /*
|
---|
| 239 | * Arriving segments
|
---|
| 240 | */
|
---|
| 241 |
|
---|
| 242 | /** Segment arrived */
|
---|
| 243 | void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 244 | {
|
---|
| 245 | tcp_conn_t *conn;
|
---|
| 246 |
|
---|
[704586fb] | 247 | log_msg(LVL_DEBUG, "tcp_as_segment_arrived(f:(%x,%u), l:(%x,%u))",
|
---|
| 248 | sp->foreign.addr.ipv4, sp->foreign.port,
|
---|
| 249 | sp->local.addr.ipv4, sp->local.port);
|
---|
[c5808b41] | 250 |
|
---|
| 251 | conn = tcp_conn_find(sp);
|
---|
[7cf7ded] | 252 | if (conn != NULL && conn->cstate != st_closed) {
|
---|
[3aa2642a] | 253 | if (conn->ident.foreign.addr.ipv4 == TCP_IPV4_ANY)
|
---|
| 254 | conn->ident.foreign.addr.ipv4 = sp->foreign.addr.ipv4;
|
---|
| 255 | if (conn->ident.foreign.port == TCP_PORT_ANY)
|
---|
| 256 | conn->ident.foreign.port = sp->foreign.port;
|
---|
[0ac2158] | 257 | if (conn->ident.local.addr.ipv4 == TCP_IPV4_ANY)
|
---|
| 258 | conn->ident.local.addr.ipv4 = sp->local.addr.ipv4;
|
---|
[3aa2642a] | 259 |
|
---|
[c5808b41] | 260 | tcp_conn_segment_arrived(conn, seg);
|
---|
| 261 | } else {
|
---|
[74c99b5] | 262 | if (conn == NULL)
|
---|
| 263 | log_msg(LVL_WARN, "No connection found.");
|
---|
| 264 | else
|
---|
| 265 | log_msg(LVL_WARN, "Connection is closed.");
|
---|
[c5808b41] | 266 | tcp_unexpected_segment(sp, seg);
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /*
|
---|
| 271 | * Timeouts
|
---|
| 272 | */
|
---|
| 273 |
|
---|
| 274 | /** User timeout */
|
---|
| 275 | void tcp_to_user(void)
|
---|
| 276 | {
|
---|
| 277 | log_msg(LVL_DEBUG, "tcp_to_user()");
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /**
|
---|
| 281 | * @}
|
---|
| 282 | */
|
---|