2 * Copyright (c) 2011 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
34 * @file TCP entry points (close to those defined in the RFC)
37 #include <fibril_synch.h>
52 * @param lsock Local socket
53 * @param fsock Foreign socket
54 * @param acpass Active/passive
55 * @param oflags Open flags
56 * @param conn Connection
58 * Unlike in the spec we allow specifying the local address. This means
59 * the implementation does not need to magically guess it, especially
60 * considering there can be more than one local address.
62 * XXX We should be able to call active open on an existing listening
64 * XXX We should be able to get connection structure immediately, before
67 tcp_error_t
tcp_uc_open(tcp_sock_t
*lsock
, tcp_sock_t
*fsock
, acpass_t acpass
,
68 tcp_open_flags_t oflags
, tcp_conn_t
**conn
)
72 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_open(%p, %p, %s, %s, %p)",
73 lsock
, fsock
, acpass
== ap_active
? "active" : "passive",
74 oflags
== tcp_open_nonblock
? "nonblock" : "none", conn
);
76 nconn
= tcp_conn_new(lsock
, fsock
);
79 if (acpass
== ap_active
) {
80 /* Synchronize (initiate) connection */
84 if (oflags
== tcp_open_nonblock
) {
89 /* Wait for connection to be established or reset */
90 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_open: Wait for connection.");
91 fibril_mutex_lock(&nconn
->lock
);
92 while (nconn
->cstate
== st_listen
||
93 nconn
->cstate
== st_syn_sent
||
94 nconn
->cstate
== st_syn_received
) {
95 fibril_condvar_wait(&nconn
->cstate_cv
, &nconn
->lock
);
98 if (nconn
->cstate
!= st_established
) {
99 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_open: Connection was reset.");
100 assert(nconn
->cstate
== st_closed
);
101 fibril_mutex_unlock(&nconn
->lock
);
105 fibril_mutex_unlock(&nconn
->lock
);
106 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_open: Connection was established.");
109 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_open -> %p", nconn
);
113 /** SEND user call */
114 tcp_error_t
tcp_uc_send(tcp_conn_t
*conn
, void *data
, size_t size
,
120 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "%s: tcp_uc_send()", conn
->name
);
122 fibril_mutex_lock(&conn
->lock
);
124 if (conn
->cstate
== st_closed
) {
125 fibril_mutex_unlock(&conn
->lock
);
126 return TCP_ENOTEXIST
;
129 if (conn
->cstate
== st_listen
) {
130 /* Change connection to active */
135 if (conn
->snd_buf_fin
) {
136 fibril_mutex_unlock(&conn
->lock
);
141 buf_free
= conn
->snd_buf_size
- conn
->snd_buf_used
;
142 while (buf_free
== 0 && !conn
->reset
) {
143 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "%s: buf_free == 0, waiting.",
145 fibril_condvar_wait(&conn
->snd_buf_cv
, &conn
->lock
);
146 buf_free
= conn
->snd_buf_size
- conn
->snd_buf_used
;
150 fibril_mutex_unlock(&conn
->lock
);
154 xfer_size
= min(size
, buf_free
);
156 /* Copy data to buffer */
157 memcpy(conn
->snd_buf
+ conn
->snd_buf_used
, data
, xfer_size
);
159 conn
->snd_buf_used
+= xfer_size
;
162 tcp_tqueue_new_data(conn
);
165 tcp_tqueue_new_data(conn
);
166 fibril_mutex_unlock(&conn
->lock
);
171 /** RECEIVE user call */
172 tcp_error_t
tcp_uc_receive(tcp_conn_t
*conn
, void *buf
, size_t size
,
173 size_t *rcvd
, xflags_t
*xflags
)
177 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "%s: tcp_uc_receive()", conn
->name
);
179 fibril_mutex_lock(&conn
->lock
);
181 if (conn
->cstate
== st_closed
) {
182 fibril_mutex_unlock(&conn
->lock
);
183 return TCP_ENOTEXIST
;
186 /* Wait for data to become available */
187 while (conn
->rcv_buf_used
== 0 && !conn
->rcv_buf_fin
&& !conn
->reset
) {
188 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_receive() - wait for data");
189 fibril_condvar_wait(&conn
->rcv_buf_cv
, &conn
->lock
);
192 if (conn
->rcv_buf_used
== 0) {
196 if (conn
->rcv_buf_fin
) {
197 /* End of data, peer closed connection */
198 fibril_mutex_unlock(&conn
->lock
);
201 /* Connection was reset */
203 fibril_mutex_unlock(&conn
->lock
);
208 /* Copy data from receive buffer to user buffer */
209 xfer_size
= min(size
, conn
->rcv_buf_used
);
210 memcpy(buf
, conn
->rcv_buf
, xfer_size
);
213 /* Remove data from receive buffer */
214 memmove(conn
->rcv_buf
, conn
->rcv_buf
+ xfer_size
, conn
->rcv_buf_used
-
216 conn
->rcv_buf_used
-= xfer_size
;
217 conn
->rcv_wnd
+= xfer_size
;
222 /* Send new size of receive window */
223 tcp_tqueue_ctrl_seg(conn
, CTL_ACK
);
225 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "%s: tcp_uc_receive() - returning %zu bytes",
226 conn
->name
, xfer_size
);
228 fibril_mutex_unlock(&conn
->lock
);
233 /** CLOSE user call */
234 tcp_error_t
tcp_uc_close(tcp_conn_t
*conn
)
236 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "%s: tcp_uc_close()", conn
->name
);
238 fibril_mutex_lock(&conn
->lock
);
240 if (conn
->cstate
== st_closed
) {
241 fibril_mutex_unlock(&conn
->lock
);
242 return TCP_ENOTEXIST
;
245 if (conn
->snd_buf_fin
) {
246 fibril_mutex_unlock(&conn
->lock
);
250 conn
->snd_buf_fin
= true;
251 tcp_tqueue_new_data(conn
);
253 fibril_mutex_unlock(&conn
->lock
);
257 /** ABORT user call */
258 void tcp_uc_abort(tcp_conn_t
*conn
)
260 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_abort()");
263 /** STATUS user call */
264 void tcp_uc_status(tcp_conn_t
*conn
, tcp_conn_status_t
*cstatus
)
266 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_status()");
267 cstatus
->cstate
= conn
->cstate
;
270 /** Delete connection user call.
272 * (Not in spec.) Inform TCP that the user is done with this connection
273 * and will not make any further calls/references to it. TCP can deallocate
274 * the connection from now on.
276 void tcp_uc_delete(tcp_conn_t
*conn
)
278 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_delete()");
279 tcp_conn_delete(conn
);
282 void tcp_uc_set_cstate_cb(tcp_conn_t
*conn
, tcp_cstate_cb_t cb
, void *arg
)
284 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_uc_set_ctate_cb(%p, %p, %p)",
287 conn
->cstate_cb
= cb
;
288 conn
->cstate_cb_arg
= arg
;
295 /** Segment arrived */
296 void tcp_as_segment_arrived(tcp_sockpair_t
*sp
, tcp_segment_t
*seg
)
300 conn
= tcp_conn_find_ref(sp
);
302 log_msg(LOG_DEFAULT
, LVL_WARN
, "No connection found.");
303 tcp_unexpected_segment(sp
, seg
);
307 fibril_mutex_lock(&conn
->lock
);
309 if (conn
->cstate
== st_closed
) {
310 log_msg(LOG_DEFAULT
, LVL_WARN
, "Connection is closed.");
311 tcp_unexpected_segment(sp
, seg
);
312 fibril_mutex_unlock(&conn
->lock
);
313 tcp_conn_delref(conn
);
317 if (inet_addr_is_any(&conn
->ident
.foreign
.addr
))
318 conn
->ident
.foreign
.addr
= sp
->foreign
.addr
;
320 if (conn
->ident
.foreign
.port
== TCP_PORT_ANY
)
321 conn
->ident
.foreign
.port
= sp
->foreign
.port
;
323 if (inet_addr_is_any(&conn
->ident
.local
.addr
))
324 conn
->ident
.local
.addr
= sp
->local
.addr
;
326 tcp_conn_segment_arrived(conn
, seg
);
328 fibril_mutex_unlock(&conn
->lock
);
329 tcp_conn_delref(conn
);
337 void tcp_to_user(void)
339 log_msg(LOG_DEFAULT
, LVL_DEBUG
, "tcp_to_user()");