source: mainline/uspace/srv/net/tl/tcp/state.c@ 6896409c

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

Make active OPEN wait for connection establishment (or reset).

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[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 "state.h"
43#include "tcp_type.h"
[32105348]44#include "tqueue.h"
[c5808b41]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
[854e79a6]56 *
57 * XXX We should be able to call active open on an existing listening
58 * connection.
[c5808b41]59 */
[004a5fe]60tcp_error_t tcp_uc_open(uint16_t lport, tcp_sock_t *fsock, acpass_t acpass,
[c5808b41]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);
[004a5fe]79
80 /* Wait for connection to be established or reset */
81 log_msg(LVL_DEBUG, "tcp_uc_open: Wait for connection.");
82 fibril_mutex_lock(&nconn->cstate_lock);
83 while (nconn->cstate == st_syn_sent ||
84 nconn->cstate == st_syn_received) {
85 fibril_condvar_wait(&nconn->cstate_cv, &nconn->cstate_lock);
86 }
87 if (nconn->cstate != st_established) {
88 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset.");
89 assert(nconn->cstate == st_closed);
90 fibril_mutex_unlock(&nconn->cstate_lock);
91 return TCP_ERESET;
92 }
93 fibril_mutex_unlock(&nconn->cstate_lock);
94 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established.");
[c5808b41]95 }
96
97 *conn = nconn;
[004a5fe]98 return TCP_EOK;
[c5808b41]99}
100
101/** SEND user call */
[854e79a6]102tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
103 xflags_t flags)
[c5808b41]104{
[32105348]105 size_t buf_free;
106 size_t xfer_size;
107
[c5808b41]108 log_msg(LVL_DEBUG, "tcp_uc_send()");
[32105348]109
[854e79a6]110 if (conn->cstate == st_closed)
111 return TCP_ENOTEXIST;
112
113 if (conn->cstate == st_listen) {
114 /* Change connection to active */
115 tcp_conn_sync(conn);
116 }
117
118 if (conn->snd_buf_fin)
119 return TCP_ECLOSING;
120
[32105348]121 while (size > 0) {
122 buf_free = conn->snd_buf_size - conn->snd_buf_used;
123 while (buf_free == 0)
124 tcp_tqueue_new_data(conn);
125
126 xfer_size = min(size, buf_free);
127
128 /* Copy data to buffer */
129 memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
130 data += xfer_size;
131 conn->snd_buf_used += xfer_size;
132 size -= xfer_size;
133 }
134
135 tcp_tqueue_new_data(conn);
[854e79a6]136
137 return TCP_EOK;
[c5808b41]138}
139
140/** RECEIVE user call */
[854e79a6]141tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
142 size_t *rcvd, xflags_t *xflags)
[c5808b41]143{
[d9ce049]144 size_t xfer_size;
145
[c5808b41]146 log_msg(LVL_DEBUG, "tcp_uc_receive()");
[d9ce049]147
[7cf7ded]148 if (conn->cstate == st_closed)
[854e79a6]149 return TCP_ENOTEXIST;
[7cf7ded]150
[d9ce049]151 fibril_mutex_lock(&conn->rcv_buf_lock);
152
153 /* Wait for data to become available */
[8c7a054]154 while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin) {
[d9ce049]155 log_msg(LVL_DEBUG, "tcp_uc_receive() - wait for data");
156 fibril_condvar_wait(&conn->rcv_buf_cv, &conn->rcv_buf_lock);
157 }
158
[8c7a054]159 if (conn->rcv_buf_used == 0) {
160 /* End of data, peer closed connection. */
161 assert(conn->rcv_buf_fin);
162 *rcvd = 0;
163 *xflags = 0;
[854e79a6]164 return TCP_ECLOSING;
[8c7a054]165 }
166
[d9ce049]167 /* Copy data from receive buffer to user buffer */
168 xfer_size = min(size, conn->rcv_buf_used);
169 memcpy(buf, conn->rcv_buf, xfer_size);
170 *rcvd = xfer_size;
171
172 /* Remove data from receive buffer */
173 memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
174 xfer_size);
175 conn->rcv_buf_used -= xfer_size;
176 conn->rcv_wnd += xfer_size;
177
178 fibril_mutex_unlock(&conn->rcv_buf_lock);
179
180 /* TODO */
181 *xflags = 0;
182
183 /* Send new size of receive window */
184 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
185
186 log_msg(LVL_DEBUG, "tcp_uc_receive() - returning %zu bytes",
187 xfer_size);
[854e79a6]188
189 return TCP_EOK;
[c5808b41]190}
191
192/** CLOSE user call */
[854e79a6]193tcp_error_t tcp_uc_close(tcp_conn_t *conn)
[c5808b41]194{
195 log_msg(LVL_DEBUG, "tcp_uc_close()");
[8c7a054]196
[854e79a6]197 if (conn->cstate == st_closed)
198 return TCP_ENOTEXIST;
199
200 if (conn->snd_buf_fin)
201 return TCP_ECLOSING;
202
[8c7a054]203 conn->snd_buf_fin = true;
204 tcp_tqueue_new_data(conn);
[854e79a6]205
206 return TCP_EOK;
[c5808b41]207}
208
209/** ABORT user call */
210void tcp_uc_abort(tcp_conn_t *conn)
211{
212 log_msg(LVL_DEBUG, "tcp_uc_abort()");
213}
214
215/** STATUS user call */
216void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
217{
218 log_msg(LVL_DEBUG, "tcp_uc_status()");
219}
220
221
222/*
223 * Arriving segments
224 */
225
226/** Segment arrived */
227void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
228{
229 tcp_conn_t *conn;
230
231 log_msg(LVL_DEBUG, "tcp_as_segment_arrived()");
232
233 conn = tcp_conn_find(sp);
[7cf7ded]234 if (conn != NULL && conn->cstate != st_closed) {
[c5808b41]235 tcp_conn_segment_arrived(conn, seg);
236 } else {
237 tcp_unexpected_segment(sp, seg);
238 }
239}
240
241/*
242 * Timeouts
243 */
244
245/** User timeout */
246void tcp_to_user(void)
247{
248 log_msg(LVL_DEBUG, "tcp_to_user()");
249}
250
251/**
252 * @}
253 */
Note: See TracBrowser for help on using the repository browser.