source: mainline/uspace/srv/net/tcp/ucall.c@ 2f19103

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2f19103 was 2f19103, checked in by Jiri Svoboda <jiri@…>, 10 years ago

TCP and UDP servers can make use of inet/endpoint.h types internally.

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[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
[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 *
[2f19103]52 * @param epp Endpoint pair
[c5808b41]53 * @param acpass Active/passive
[ae481e0]54 * @param oflags Open flags
[c5808b41]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 */
[2f19103]66tcp_error_t tcp_uc_open(inet_ep2_t *epp, acpass_t acpass,
[ae481e0]67 tcp_open_flags_t oflags, tcp_conn_t **conn)
[c5808b41]68{
69 tcp_conn_t *nconn;
70
[2f19103]71 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %s, %s, %p)",
72 epp, acpass == ap_active ? "active" : "passive",
[ae481e0]73 oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
[c5808b41]74
[2f19103]75 nconn = tcp_conn_new(epp);
[c5808b41]76 tcp_conn_add(nconn);
[5441670]77 tcp_conn_lock(nconn);
[c5808b41]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) {
[5441670]85 tcp_conn_unlock(nconn);
[b243da3]86 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
[ae481e0]87 *conn = nconn;
88 return TCP_EOK;
89 }
90
[a4ee3ab2]91 /* Wait for connection to be established or reset */
[a1a101d]92 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Wait for connection.");
[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]115tcp_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]173tcp_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) {
[779541b]189 tcp_conn_unlock(conn);
190 return TCP_EAGAIN;
[a1a101d]191 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
[0edaf0f6]192 fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
[d9ce049]193 }
194
[8c7a054]195 if (conn->rcv_buf_used == 0) {
196 *rcvd = 0;
197 *xflags = 0;
[d9e14fa4]198
199 if (conn->rcv_buf_fin) {
200 /* End of data, peer closed connection */
[78192cc7]201 tcp_conn_unlock(conn);
[d9e14fa4]202 return TCP_ECLOSING;
203 } else {
204 /* Connection was reset */
205 assert(conn->reset);
[78192cc7]206 tcp_conn_unlock(conn);
[d9e14fa4]207 return TCP_ERESET;
208 }
[8c7a054]209 }
210
[d9ce049]211 /* Copy data from receive buffer to user buffer */
212 xfer_size = min(size, conn->rcv_buf_used);
213 memcpy(buf, conn->rcv_buf, xfer_size);
214 *rcvd = xfer_size;
215
216 /* Remove data from receive buffer */
217 memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
218 xfer_size);
219 conn->rcv_buf_used -= xfer_size;
220 conn->rcv_wnd += xfer_size;
221
222 /* TODO */
223 *xflags = 0;
224
225 /* Send new size of receive window */
226 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
227
[a1a101d]228 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
[23fe06c]229 conn->name, xfer_size);
[854e79a6]230
[78192cc7]231 tcp_conn_unlock(conn);
[0edaf0f6]232
[854e79a6]233 return TCP_EOK;
[c5808b41]234}
235
236/** CLOSE user call */
[854e79a6]237tcp_error_t tcp_uc_close(tcp_conn_t *conn)
[c5808b41]238{
[b243da3]239 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
240 conn);
[8c7a054]241
[78192cc7]242 tcp_conn_lock(conn);
[0edaf0f6]243
244 if (conn->cstate == st_closed) {
[b243da3]245 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
[78192cc7]246 tcp_conn_unlock(conn);
[854e79a6]247 return TCP_ENOTEXIST;
[0edaf0f6]248 }
[854e79a6]249
[b243da3]250 if (conn->cstate == st_listen || conn->cstate == st_syn_sent) {
251 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - listen/syn_sent");
252 tcp_conn_reset(conn);
253 tcp_conn_remove(conn);
[78192cc7]254 tcp_conn_unlock(conn);
[b243da3]255 return TCP_EOK;
256 }
257
[0edaf0f6]258 if (conn->snd_buf_fin) {
[b243da3]259 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
[78192cc7]260 tcp_conn_unlock(conn);
[854e79a6]261 return TCP_ECLOSING;
[0edaf0f6]262 }
[854e79a6]263
[b243da3]264 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
[8c7a054]265 conn->snd_buf_fin = true;
266 tcp_tqueue_new_data(conn);
[854e79a6]267
[78192cc7]268 tcp_conn_unlock(conn);
[854e79a6]269 return TCP_EOK;
[c5808b41]270}
271
272/** ABORT user call */
273void tcp_uc_abort(tcp_conn_t *conn)
274{
[a1a101d]275 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
[c5808b41]276}
277
278/** STATUS user call */
279void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
280{
[a1a101d]281 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
[ae481e0]282 cstatus->cstate = conn->cstate;
[c5808b41]283}
284
[7a8c1c4e]285/** Delete connection user call.
286 *
287 * (Not in spec.) Inform TCP that the user is done with this connection
288 * and will not make any further calls/references to it. TCP can deallocate
289 * the connection from now on.
290 */
291void tcp_uc_delete(tcp_conn_t *conn)
292{
[a1a101d]293 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
[7a8c1c4e]294 tcp_conn_delete(conn);
295}
[c5808b41]296
[779541b]297void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
[ae481e0]298{
[779541b]299 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
[ae481e0]300 conn, cb, arg);
301
[779541b]302 conn->cb = cb;
303 conn->cb_arg = arg;
[ae481e0]304}
305
[1d4b815]306void *tcp_uc_get_userptr(tcp_conn_t *conn)
307{
308 return conn->cb_arg;
309}
310
[c5808b41]311/*
312 * Arriving segments
313 */
314
315/** Segment arrived */
[2f19103]316void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
[c5808b41]317{
318 tcp_conn_t *conn;
319
[c0f3460]320 log_msg(LOG_DEFAULT, LVL_DEBUG,
321 "tcp_as_segment_arrived(f:(%u), l:(%u))",
[2f19103]322 epp->remote.port, epp->local.port);
[c0f3460]323
[2f19103]324 conn = tcp_conn_find_ref(epp);
[0edaf0f6]325 if (conn == NULL) {
[a1a101d]326 log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
[2f19103]327 tcp_unexpected_segment(epp, seg);
[0edaf0f6]328 return;
[c5808b41]329 }
[0edaf0f6]330
[78192cc7]331 tcp_conn_lock(conn);
[0edaf0f6]332
333 if (conn->cstate == st_closed) {
[a1a101d]334 log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
[2f19103]335 tcp_unexpected_segment(epp, seg);
[78192cc7]336 tcp_conn_unlock(conn);
[0edaf0f6]337 tcp_conn_delref(conn);
338 return;
339 }
340
[2f19103]341 if (inet_addr_is_any(&conn->ident.remote.addr))
342 conn->ident.remote.addr = epp->remote.addr;
343
344 if (conn->ident.remote.port == TCP_PORT_ANY)
345 conn->ident.remote.port = epp->remote.port;
346
[a2e3ee6]347 if (inet_addr_is_any(&conn->ident.local.addr))
[2f19103]348 conn->ident.local.addr = epp->local.addr;
[0edaf0f6]349
350 tcp_conn_segment_arrived(conn, seg);
351
[78192cc7]352 tcp_conn_unlock(conn);
[0edaf0f6]353 tcp_conn_delref(conn);
[c5808b41]354}
355
356/*
357 * Timeouts
358 */
359
360/** User timeout */
361void tcp_to_user(void)
362{
[a1a101d]363 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
[c5808b41]364}
365
366/**
367 * @}
368 */
Note: See TracBrowser for help on using the repository browser.