| 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 | /**
|
|---|
| 34 | * @file TCP entry points (close to those defined in the RFC)
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <fibril_synch.h>
|
|---|
| 38 | #include <io/log.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| 40 | #include <mem.h>
|
|---|
| 41 | #include "conn.h"
|
|---|
| 42 | #include "state.h"
|
|---|
| 43 | #include "tcp_type.h"
|
|---|
| 44 | #include "tqueue.h"
|
|---|
| 45 |
|
|---|
| 46 | /*
|
|---|
| 47 | * User calls
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | /** OPEN user call
|
|---|
| 51 | *
|
|---|
| 52 | * @param lport Local port
|
|---|
| 53 | * @param fsock Foreign socket
|
|---|
| 54 | * @param acpass Active/passive
|
|---|
| 55 | * @param conn Connection
|
|---|
| 56 | *
|
|---|
| 57 | * XXX We should be able to call active open on an existing listening
|
|---|
| 58 | * connection.
|
|---|
| 59 | */
|
|---|
| 60 | void tcp_uc_open(uint16_t lport, tcp_sock_t *fsock, acpass_t acpass,
|
|---|
| 61 | tcp_conn_t **conn)
|
|---|
| 62 | {
|
|---|
| 63 | tcp_conn_t *nconn;
|
|---|
| 64 | tcp_sock_t lsock;
|
|---|
| 65 |
|
|---|
| 66 | log_msg(LVL_DEBUG, "tcp_uc_open(%" PRIu16 ", %p, %s, %p)",
|
|---|
| 67 | lport, fsock, acpass == ap_active ? "active" : "passive",
|
|---|
| 68 | conn);
|
|---|
| 69 |
|
|---|
| 70 | lsock.port = lport;
|
|---|
| 71 | lsock.addr.ipv4 = 0x7f000001;
|
|---|
| 72 |
|
|---|
| 73 | nconn = tcp_conn_new(&lsock, fsock);
|
|---|
| 74 | tcp_conn_add(nconn);
|
|---|
| 75 |
|
|---|
| 76 | if (acpass == ap_active) {
|
|---|
| 77 | /* Synchronize (initiate) connection */
|
|---|
| 78 | tcp_conn_sync(nconn);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | *conn = nconn;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /** SEND user call */
|
|---|
| 85 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
|---|
| 86 | xflags_t flags)
|
|---|
| 87 | {
|
|---|
| 88 | size_t buf_free;
|
|---|
| 89 | size_t xfer_size;
|
|---|
| 90 |
|
|---|
| 91 | log_msg(LVL_DEBUG, "tcp_uc_send()");
|
|---|
| 92 |
|
|---|
| 93 | if (conn->cstate == st_closed)
|
|---|
| 94 | return TCP_ENOTEXIST;
|
|---|
| 95 |
|
|---|
| 96 | if (conn->cstate == st_listen) {
|
|---|
| 97 | /* Change connection to active */
|
|---|
| 98 | tcp_conn_sync(conn);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | if (conn->snd_buf_fin)
|
|---|
| 102 | return TCP_ECLOSING;
|
|---|
| 103 |
|
|---|
| 104 | while (size > 0) {
|
|---|
| 105 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
|---|
| 106 | while (buf_free == 0)
|
|---|
| 107 | tcp_tqueue_new_data(conn);
|
|---|
| 108 |
|
|---|
| 109 | xfer_size = min(size, buf_free);
|
|---|
| 110 |
|
|---|
| 111 | /* Copy data to buffer */
|
|---|
| 112 | memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
|
|---|
| 113 | data += xfer_size;
|
|---|
| 114 | conn->snd_buf_used += xfer_size;
|
|---|
| 115 | size -= xfer_size;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | tcp_tqueue_new_data(conn);
|
|---|
| 119 |
|
|---|
| 120 | return TCP_EOK;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** RECEIVE user call */
|
|---|
| 124 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
|---|
| 125 | size_t *rcvd, xflags_t *xflags)
|
|---|
| 126 | {
|
|---|
| 127 | size_t xfer_size;
|
|---|
| 128 |
|
|---|
| 129 | log_msg(LVL_DEBUG, "tcp_uc_receive()");
|
|---|
| 130 |
|
|---|
| 131 | if (conn->cstate == st_closed)
|
|---|
| 132 | return TCP_ENOTEXIST;
|
|---|
| 133 |
|
|---|
| 134 | fibril_mutex_lock(&conn->rcv_buf_lock);
|
|---|
| 135 |
|
|---|
| 136 | /* Wait for data to become available */
|
|---|
| 137 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin) {
|
|---|
| 138 | log_msg(LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
|---|
| 139 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->rcv_buf_lock);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | if (conn->rcv_buf_used == 0) {
|
|---|
| 143 | /* End of data, peer closed connection. */
|
|---|
| 144 | assert(conn->rcv_buf_fin);
|
|---|
| 145 | *rcvd = 0;
|
|---|
| 146 | *xflags = 0;
|
|---|
| 147 | return TCP_ECLOSING;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /* Copy data from receive buffer to user buffer */
|
|---|
| 151 | xfer_size = min(size, conn->rcv_buf_used);
|
|---|
| 152 | memcpy(buf, conn->rcv_buf, xfer_size);
|
|---|
| 153 | *rcvd = xfer_size;
|
|---|
| 154 |
|
|---|
| 155 | /* Remove data from receive buffer */
|
|---|
| 156 | memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
|
|---|
| 157 | xfer_size);
|
|---|
| 158 | conn->rcv_buf_used -= xfer_size;
|
|---|
| 159 | conn->rcv_wnd += xfer_size;
|
|---|
| 160 |
|
|---|
| 161 | fibril_mutex_unlock(&conn->rcv_buf_lock);
|
|---|
| 162 |
|
|---|
| 163 | /* TODO */
|
|---|
| 164 | *xflags = 0;
|
|---|
| 165 |
|
|---|
| 166 | /* Send new size of receive window */
|
|---|
| 167 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
|---|
| 168 |
|
|---|
| 169 | log_msg(LVL_DEBUG, "tcp_uc_receive() - returning %zu bytes",
|
|---|
| 170 | xfer_size);
|
|---|
| 171 |
|
|---|
| 172 | return TCP_EOK;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** CLOSE user call */
|
|---|
| 176 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
|---|
| 177 | {
|
|---|
| 178 | log_msg(LVL_DEBUG, "tcp_uc_close()");
|
|---|
| 179 |
|
|---|
| 180 | if (conn->cstate == st_closed)
|
|---|
| 181 | return TCP_ENOTEXIST;
|
|---|
| 182 |
|
|---|
| 183 | if (conn->snd_buf_fin)
|
|---|
| 184 | return TCP_ECLOSING;
|
|---|
| 185 |
|
|---|
| 186 | conn->snd_buf_fin = true;
|
|---|
| 187 | tcp_tqueue_new_data(conn);
|
|---|
| 188 |
|
|---|
| 189 | return TCP_EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /** ABORT user call */
|
|---|
| 193 | void tcp_uc_abort(tcp_conn_t *conn)
|
|---|
| 194 | {
|
|---|
| 195 | log_msg(LVL_DEBUG, "tcp_uc_abort()");
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /** STATUS user call */
|
|---|
| 199 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
|---|
| 200 | {
|
|---|
| 201 | log_msg(LVL_DEBUG, "tcp_uc_status()");
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 | /*
|
|---|
| 206 | * Arriving segments
|
|---|
| 207 | */
|
|---|
| 208 |
|
|---|
| 209 | /** Segment arrived */
|
|---|
| 210 | void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
|---|
| 211 | {
|
|---|
| 212 | tcp_conn_t *conn;
|
|---|
| 213 |
|
|---|
| 214 | log_msg(LVL_DEBUG, "tcp_as_segment_arrived()");
|
|---|
| 215 |
|
|---|
| 216 | conn = tcp_conn_find(sp);
|
|---|
| 217 | if (conn != NULL && conn->cstate != st_closed) {
|
|---|
| 218 | tcp_conn_segment_arrived(conn, seg);
|
|---|
| 219 | } else {
|
|---|
| 220 | tcp_unexpected_segment(sp, seg);
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /*
|
|---|
| 225 | * Timeouts
|
|---|
| 226 | */
|
|---|
| 227 |
|
|---|
| 228 | /** User timeout */
|
|---|
| 229 | void tcp_to_user(void)
|
|---|
| 230 | {
|
|---|
| 231 | log_msg(LVL_DEBUG, "tcp_to_user()");
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /**
|
|---|
| 235 | * @}
|
|---|
| 236 | */
|
|---|