1 | /*
|
---|
2 | * Copyright (c) 2015 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 epp Endpoint pair
|
---|
53 | * @param acpass Active/passive
|
---|
54 | * @param oflags Open flags
|
---|
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(inet_ep2_t *epp, acpass_t acpass,
|
---|
67 | tcp_open_flags_t oflags, tcp_conn_t **conn)
|
---|
68 | {
|
---|
69 | tcp_conn_t *nconn;
|
---|
70 |
|
---|
71 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open(%p, %s, %s, %p)",
|
---|
72 | epp, acpass == ap_active ? "active" : "passive",
|
---|
73 | oflags == tcp_open_nonblock ? "nonblock" : "none", conn);
|
---|
74 |
|
---|
75 | nconn = tcp_conn_new(epp);
|
---|
76 | tcp_conn_add(nconn);
|
---|
77 | tcp_conn_lock(nconn);
|
---|
78 |
|
---|
79 | if (acpass == ap_active) {
|
---|
80 | /* Synchronize (initiate) connection */
|
---|
81 | tcp_conn_sync(nconn);
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (oflags == tcp_open_nonblock) {
|
---|
85 | tcp_conn_unlock(nconn);
|
---|
86 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
|
---|
87 | *conn = nconn;
|
---|
88 | return TCP_EOK;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* Wait for connection to be established or reset */
|
---|
92 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Wait for connection.");
|
---|
93 | while (nconn->cstate == st_listen ||
|
---|
94 | nconn->cstate == st_syn_sent ||
|
---|
95 | nconn->cstate == st_syn_received) {
|
---|
96 | fibril_condvar_wait(&nconn->cstate_cv, &nconn->lock);
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (nconn->cstate != st_established) {
|
---|
100 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was reset.");
|
---|
101 | assert(nconn->cstate == st_closed);
|
---|
102 | tcp_conn_unlock(nconn);
|
---|
103 | return TCP_ERESET;
|
---|
104 | }
|
---|
105 |
|
---|
106 | tcp_conn_unlock(nconn);
|
---|
107 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open: Connection was established.");
|
---|
108 |
|
---|
109 | *conn = nconn;
|
---|
110 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_open -> %p", nconn);
|
---|
111 | return TCP_EOK;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /** SEND user call */
|
---|
115 | tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
|
---|
116 | xflags_t flags)
|
---|
117 | {
|
---|
118 | size_t buf_free;
|
---|
119 | size_t xfer_size;
|
---|
120 |
|
---|
121 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
|
---|
122 |
|
---|
123 | tcp_conn_lock(conn);
|
---|
124 |
|
---|
125 | if (conn->cstate == st_closed) {
|
---|
126 | tcp_conn_unlock(conn);
|
---|
127 | return TCP_ENOTEXIST;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (conn->cstate == st_listen) {
|
---|
131 | /* Change connection to active */
|
---|
132 | tcp_conn_sync(conn);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | if (conn->snd_buf_fin) {
|
---|
137 | tcp_conn_unlock(conn);
|
---|
138 | return TCP_ECLOSING;
|
---|
139 | }
|
---|
140 |
|
---|
141 | while (size > 0) {
|
---|
142 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
143 | while (buf_free == 0 && !conn->reset) {
|
---|
144 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: buf_free == 0, waiting.",
|
---|
145 | conn->name);
|
---|
146 | fibril_condvar_wait(&conn->snd_buf_cv, &conn->lock);
|
---|
147 | buf_free = conn->snd_buf_size - conn->snd_buf_used;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (conn->reset) {
|
---|
151 | tcp_conn_unlock(conn);
|
---|
152 | return TCP_ERESET;
|
---|
153 | }
|
---|
154 |
|
---|
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;
|
---|
162 |
|
---|
163 | tcp_tqueue_new_data(conn);
|
---|
164 | }
|
---|
165 |
|
---|
166 | tcp_tqueue_new_data(conn);
|
---|
167 | tcp_conn_unlock(conn);
|
---|
168 |
|
---|
169 | return TCP_EOK;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /** RECEIVE user call */
|
---|
173 | tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
|
---|
174 | size_t *rcvd, xflags_t *xflags)
|
---|
175 | {
|
---|
176 | size_t xfer_size;
|
---|
177 |
|
---|
178 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
|
---|
179 |
|
---|
180 | tcp_conn_lock(conn);
|
---|
181 |
|
---|
182 | if (conn->cstate == st_closed) {
|
---|
183 | tcp_conn_unlock(conn);
|
---|
184 | return TCP_ENOTEXIST;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* Wait for data to become available */
|
---|
188 | while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin && !conn->reset) {
|
---|
189 | tcp_conn_unlock(conn);
|
---|
190 | return TCP_EAGAIN;
|
---|
191 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_receive() - wait for data");
|
---|
192 | fibril_condvar_wait(&conn->rcv_buf_cv, &conn->lock);
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (conn->rcv_buf_used == 0) {
|
---|
196 | *rcvd = 0;
|
---|
197 | *xflags = 0;
|
---|
198 |
|
---|
199 | if (conn->rcv_buf_fin) {
|
---|
200 | /* End of data, peer closed connection */
|
---|
201 | tcp_conn_unlock(conn);
|
---|
202 | return TCP_ECLOSING;
|
---|
203 | } else {
|
---|
204 | /* Connection was reset */
|
---|
205 | assert(conn->reset);
|
---|
206 | tcp_conn_unlock(conn);
|
---|
207 | return TCP_ERESET;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
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 |
|
---|
228 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
|
---|
229 | conn->name, xfer_size);
|
---|
230 |
|
---|
231 | tcp_conn_unlock(conn);
|
---|
232 |
|
---|
233 | return TCP_EOK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** CLOSE user call */
|
---|
237 | tcp_error_t tcp_uc_close(tcp_conn_t *conn)
|
---|
238 | {
|
---|
239 | log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: tcp_uc_close(%p)", conn->name,
|
---|
240 | conn);
|
---|
241 |
|
---|
242 | tcp_conn_lock(conn);
|
---|
243 |
|
---|
244 | if (conn->cstate == st_closed) {
|
---|
245 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ENOTEXIST");
|
---|
246 | tcp_conn_unlock(conn);
|
---|
247 | return TCP_ENOTEXIST;
|
---|
248 | }
|
---|
249 |
|
---|
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);
|
---|
254 | tcp_conn_unlock(conn);
|
---|
255 | return TCP_EOK;
|
---|
256 | }
|
---|
257 |
|
---|
258 | if (conn->snd_buf_fin) {
|
---|
259 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - ECLOSING");
|
---|
260 | tcp_conn_unlock(conn);
|
---|
261 | return TCP_ECLOSING;
|
---|
262 | }
|
---|
263 |
|
---|
264 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_close - set snd_buf_fin");
|
---|
265 | conn->snd_buf_fin = true;
|
---|
266 | tcp_tqueue_new_data(conn);
|
---|
267 |
|
---|
268 | tcp_conn_unlock(conn);
|
---|
269 | return TCP_EOK;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** ABORT user call */
|
---|
273 | void tcp_uc_abort(tcp_conn_t *conn)
|
---|
274 | {
|
---|
275 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_abort()");
|
---|
276 | }
|
---|
277 |
|
---|
278 | /** STATUS user call */
|
---|
279 | void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
|
---|
280 | {
|
---|
281 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_status()");
|
---|
282 | cstatus->cstate = conn->cstate;
|
---|
283 | }
|
---|
284 |
|
---|
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 | */
|
---|
291 | void tcp_uc_delete(tcp_conn_t *conn)
|
---|
292 | {
|
---|
293 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_delete()");
|
---|
294 | tcp_conn_delete(conn);
|
---|
295 | }
|
---|
296 |
|
---|
297 | void tcp_uc_set_cb(tcp_conn_t *conn, tcp_cb_t *cb, void *arg)
|
---|
298 | {
|
---|
299 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_uc_set_cb(%p, %p, %p)",
|
---|
300 | conn, cb, arg);
|
---|
301 |
|
---|
302 | conn->cb = cb;
|
---|
303 | conn->cb_arg = arg;
|
---|
304 | }
|
---|
305 |
|
---|
306 | void *tcp_uc_get_userptr(tcp_conn_t *conn)
|
---|
307 | {
|
---|
308 | return conn->cb_arg;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*
|
---|
312 | * Arriving segments
|
---|
313 | */
|
---|
314 |
|
---|
315 | /** Segment arrived */
|
---|
316 | void tcp_as_segment_arrived(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
317 | {
|
---|
318 | tcp_conn_t *conn;
|
---|
319 |
|
---|
320 | log_msg(LOG_DEFAULT, LVL_DEBUG,
|
---|
321 | "tcp_as_segment_arrived(f:(%u), l:(%u))",
|
---|
322 | epp->remote.port, epp->local.port);
|
---|
323 |
|
---|
324 | conn = tcp_conn_find_ref(epp);
|
---|
325 | if (conn == NULL) {
|
---|
326 | log_msg(LOG_DEFAULT, LVL_WARN, "No connection found.");
|
---|
327 | tcp_unexpected_segment(epp, seg);
|
---|
328 | return;
|
---|
329 | }
|
---|
330 |
|
---|
331 | tcp_conn_lock(conn);
|
---|
332 |
|
---|
333 | if (conn->cstate == st_closed) {
|
---|
334 | log_msg(LOG_DEFAULT, LVL_WARN, "Connection is closed.");
|
---|
335 | tcp_unexpected_segment(epp, seg);
|
---|
336 | tcp_conn_unlock(conn);
|
---|
337 | tcp_conn_delref(conn);
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
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 |
|
---|
347 | if (inet_addr_is_any(&conn->ident.local.addr))
|
---|
348 | conn->ident.local.addr = epp->local.addr;
|
---|
349 |
|
---|
350 | tcp_conn_segment_arrived(conn, seg);
|
---|
351 |
|
---|
352 | tcp_conn_unlock(conn);
|
---|
353 | tcp_conn_delref(conn);
|
---|
354 | }
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * Timeouts
|
---|
358 | */
|
---|
359 |
|
---|
360 | /** User timeout */
|
---|
361 | void tcp_to_user(void)
|
---|
362 | {
|
---|
363 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_to_user()");
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * @}
|
---|
368 | */
|
---|