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 "tcp_type.h"
|
---|
43 | #include "tqueue.h"
|
---|
44 | #include "ucall.h"
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * User calls
|
---|
48 | */
|
---|
49 |
|
---|
50 | /** OPEN user call
|
---|
51 | *
|
---|
52 | * @param lsock Local socket
|
---|
53 | * @param fsock Foreign socket
|
---|
54 | * @param acpass Active/passive
|
---|
55 | * @param conn Connection
|
---|
56 | *
|
---|
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 | *
|
---|
61 | * XXX We should be able to call active open on an existing listening
|
---|
62 | * connection.
|
---|
63 | * XXX We should be able to get connection structure immediately, before
|
---|
64 | * establishment.
|
---|
65 | */
|
---|
66 | tcp_error_t tcp_uc_open(tcp_sock_t *lsock, tcp_sock_t *fsock, acpass_t acpass,
|
---|
67 | tcp_conn_t **conn)
|
---|
68 | {
|
---|
69 | tcp_conn_t *nconn;
|
---|
70 |
|
---|
71 | log_msg(LVL_DEBUG, "tcp_uc_open(%p, %p, %s, %p)",
|
---|
72 | lsock, fsock, acpass == ap_active ? "active" : "passive",
|
---|
73 | conn);
|
---|
74 |
|
---|
75 | nconn = tcp_conn_new(lsock, fsock);
|
---|
76 | tcp_conn_add(nconn);
|
---|
77 |
|
---|
78 | if (acpass == ap_active) {
|
---|
79 | /* Synchronize (initiate) connection */
|
---|
80 | tcp_conn_sync(nconn);
|
---|
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->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->lock);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (nconn->cstate != st_established) {
|
---|
93 | log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset.");
|
---|
94 | assert(nconn->cstate == st_closed);
|
---|
95 | fibril_mutex_unlock(&nconn->lock);
|
---|
96 | return TCP_ERESET;
|
---|
97 | }
|
---|
98 |
|
---|
99 | fibril_mutex_unlock(&nconn->lock);
|
---|
100 | log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established.");
|
---|
101 |
|
---|
102 | *conn = nconn;
|
---|
103 | return TCP_EOK;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** SEND user call */
|
---|
107 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
---|
108 | xflags_t flags)
|
---|
109 | {
|
---|
110 | size_t buf_free;
|
---|
111 | size_t xfer_size;
|
---|
112 |
|
---|
113 | log_msg(LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
|
---|
114 |
|
---|
115 | fibril_mutex_lock(&conn->lock);
|
---|
116 |
|
---|
117 | if (conn->cstate == st_closed) {
|
---|
118 | fibril_mutex_unlock(&conn->lock);
|
---|
119 | return TCP_ENOTEXIST;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (conn->cstate == st_listen) {
|
---|
123 | /* Change connection to active */
|
---|
124 | tcp_conn_sync(conn);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | if (conn->snd_buf_fin) {
|
---|
129 | fibril_mutex_unlock(&conn->lock);
|
---|
130 | return TCP_ECLOSING;
|
---|
131 | }
|
---|
132 |
|
---|
133 | while (size > 0) {
|
---|
134 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
135 | while (buf_free == 0 && !conn->reset) {
|
---|
136 | log_msg(LVL_DEBUG, "%s: buf_free == 0, waiting.",
|
---|
137 | conn->name);
|
---|
138 | fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
|
---|
139 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (conn->reset) {
|
---|
143 | fibril_mutex_unlock(&conn->lock);
|
---|
144 | return TCP_ERESET;
|
---|
145 | }
|
---|
146 |
|
---|
147 | xfer_size = min(size, buf_free);
|
---|
148 |
|
---|
149 | /* Copy data to buffer */
|
---|
150 | memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
|
---|
151 | data += xfer_size;
|
---|
152 | conn->snd_buf_used += xfer_size;
|
---|
153 | size -= xfer_size;
|
---|
154 |
|
---|
155 | tcp_tqueue_new_data(conn);
|
---|
156 | }
|
---|
157 |
|
---|
158 | tcp_tqueue_new_data(conn);
|
---|
159 | fibril_mutex_unlock(&conn->lock);
|
---|
160 |
|
---|
161 | return TCP_EOK;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** RECEIVE user call */
|
---|
165 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
---|
166 | size_t *rcvd, xflags_t *xflags)
|
---|
167 | {
|
---|
168 | size_t xfer_size;
|
---|
169 |
|
---|
170 | log_msg(LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
---|
171 |
|
---|
172 | fibril_mutex_lock(&conn->lock);
|
---|
173 |
|
---|
174 | if (conn->cstate == st_closed) {
|
---|
175 | fibril_mutex_unlock(&conn->lock);
|
---|
176 | return TCP_ENOTEXIST;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* Wait for data to become available */
|
---|
180 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
---|
181 | log_msg(LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
---|
182 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (conn->rcv_buf_used == 0) {
|
---|
186 | *rcvd = 0;
|
---|
187 | *xflags = 0;
|
---|
188 |
|
---|
189 | if (conn->rcv_buf_fin) {
|
---|
190 | /* End of data, peer closed connection */
|
---|
191 | fibril_mutex_unlock(&conn->lock);
|
---|
192 | return TCP_ECLOSING;
|
---|
193 | } else {
|
---|
194 | /* Connection was reset */
|
---|
195 | assert(conn->reset);
|
---|
196 | fibril_mutex_unlock(&conn->lock);
|
---|
197 | return TCP_ERESET;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* Copy data from receive buffer to user buffer */
|
---|
202 | xfer_size = min(size, conn->rcv_buf_used);
|
---|
203 | memcpy(buf, conn->rcv_buf, xfer_size);
|
---|
204 | *rcvd = xfer_size;
|
---|
205 |
|
---|
206 | /* Remove data from receive buffer */
|
---|
207 | memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
|
---|
208 | xfer_size);
|
---|
209 | conn->rcv_buf_used -= xfer_size;
|
---|
210 | conn->rcv_wnd += xfer_size;
|
---|
211 |
|
---|
212 | /* TODO */
|
---|
213 | *xflags = 0;
|
---|
214 |
|
---|
215 | /* Send new size of receive window */
|
---|
216 | tcp_tqueue_ctrl_seg(conn, CTL_ACK);
|
---|
217 |
|
---|
218 | log_msg(LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
---|
219 | conn->name, xfer_size);
|
---|
220 |
|
---|
221 | fibril_mutex_unlock(&conn->lock);
|
---|
222 |
|
---|
223 | return TCP_EOK;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /** CLOSE user call */
|
---|
227 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
---|
228 | {
|
---|
229 | log_msg(LVL_DEBUG, "%s: tcp_uc_close()", conn->name);
|
---|
230 |
|
---|
231 | fibril_mutex_lock(&conn->lock);
|
---|
232 |
|
---|
233 | if (conn->cstate == st_closed) {
|
---|
234 | fibril_mutex_unlock(&conn->lock);
|
---|
235 | return TCP_ENOTEXIST;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (conn->snd_buf_fin) {
|
---|
239 | fibril_mutex_unlock(&conn->lock);
|
---|
240 | return TCP_ECLOSING;
|
---|
241 | }
|
---|
242 |
|
---|
243 | conn->snd_buf_fin = true;
|
---|
244 | tcp_tqueue_new_data(conn);
|
---|
245 |
|
---|
246 | fibril_mutex_unlock(&conn->lock);
|
---|
247 | return TCP_EOK;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /** ABORT user call */
|
---|
251 | void tcp_uc_abort(tcp_conn_t *conn)
|
---|
252 | {
|
---|
253 | log_msg(LVL_DEBUG, "tcp_uc_abort()");
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** STATUS user call */
|
---|
257 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
---|
258 | {
|
---|
259 | log_msg(LVL_DEBUG, "tcp_uc_status()");
|
---|
260 | }
|
---|
261 |
|
---|
262 | /** Delete connection user call.
|
---|
263 | *
|
---|
264 | * (Not in spec.) Inform TCP that the user is done with this connection
|
---|
265 | * and will not make any further calls/references to it. TCP can deallocate
|
---|
266 | * the connection from now on.
|
---|
267 | */
|
---|
268 | void tcp_uc_delete(tcp_conn_t *conn)
|
---|
269 | {
|
---|
270 | log_msg(LVL_DEBUG, "tcp_uc_delete()");
|
---|
271 | tcp_conn_delete(conn);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * Arriving segments
|
---|
276 | */
|
---|
277 |
|
---|
278 | /** Segment arrived */
|
---|
279 | void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
280 | {
|
---|
281 | tcp_conn_t *conn;
|
---|
282 |
|
---|
283 | log_msg(LVL_DEBUG, "tcp_as_segment_arrived(f:(%x,%u), l:(%x,%u))",
|
---|
284 | sp->foreign.addr.ipv4, sp->foreign.port,
|
---|
285 | sp->local.addr.ipv4, sp->local.port);
|
---|
286 |
|
---|
287 | conn = tcp_conn_find_ref(sp);
|
---|
288 | if (conn == NULL) {
|
---|
289 | log_msg(LVL_WARN, "No connection found.");
|
---|
290 | tcp_unexpected_segment(sp, seg);
|
---|
291 | return;
|
---|
292 | }
|
---|
293 |
|
---|
294 | fibril_mutex_lock(&conn->lock);
|
---|
295 |
|
---|
296 | if (conn->cstate == st_closed) {
|
---|
297 | log_msg(LVL_WARN, "Connection is closed.");
|
---|
298 | tcp_unexpected_segment(sp, seg);
|
---|
299 | fibril_mutex_unlock(&conn->lock);
|
---|
300 | tcp_conn_delref(conn);
|
---|
301 | return;
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (conn->ident.foreign.addr.ipv4 == TCP_IPV4_ANY)
|
---|
305 | conn->ident.foreign.addr.ipv4 = sp->foreign.addr.ipv4;
|
---|
306 | if (conn->ident.foreign.port == TCP_PORT_ANY)
|
---|
307 | conn->ident.foreign.port = sp->foreign.port;
|
---|
308 | if (conn->ident.local.addr.ipv4 == TCP_IPV4_ANY)
|
---|
309 | conn->ident.local.addr.ipv4 = sp->local.addr.ipv4;
|
---|
310 |
|
---|
311 | tcp_conn_segment_arrived(conn, seg);
|
---|
312 |
|
---|
313 | fibril_mutex_unlock(&conn->lock);
|
---|
314 | tcp_conn_delref(conn);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Timeouts
|
---|
319 | */
|
---|
320 |
|
---|
321 | /** User timeout */
|
---|
322 | void tcp_to_user(void)
|
---|
323 | {
|
---|
324 | log_msg(LVL_DEBUG, "tcp_to_user()");
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * @}
|
---|
329 | */
|
---|