source: mainline/uspace/srv/net/tl/tcp/ucall.c@ b0d82d1

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

Implement socket close. Unlock mutexes on error paths.

  • Property mode set to 100644
File size: 6.3 KB
Line 
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 lport Local port
53 * @param fsock Foreign socket
54 * @param acpass Active/passive
55 * @param conn Connection
56 *
57 * XXX We should be able to call active open on an existing listening
58 * connection.
59 * XXX We should be able to get connection structure immediately, before
60 * establishment.
61 */
62tcp_error_t tcp_uc_open(uint16_t lport, tcp_sock_t *fsock, acpass_t acpass,
63 tcp_conn_t **conn)
64{
65 tcp_conn_t *nconn;
66 tcp_sock_t lsock;
67
68 log_msg(LVL_DEBUG, "tcp_uc_open(%" PRIu16 ", %p, %s, %p)",
69 lport, fsock, acpass == ap_active ? "active" : "passive",
70 conn);
71
72 lsock.port = lport;
73 lsock.addr.ipv4 = 0x7f000001;
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->cstate_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->cstate_lock);
90 }
91
92 if (nconn->cstate != st_established) {
93 fibril_mutex_unlock(&nconn->cstate_lock);
94
95 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset.");
96 assert(nconn->cstate == st_closed);
97 fibril_mutex_unlock(&nconn->cstate_lock);
98 return TCP_ERESET;
99 }
100
101 fibril_mutex_unlock(&nconn->cstate_lock);
102 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established.");
103
104 *conn = nconn;
105 return TCP_EOK;
106}
107
108/** SEND user call */
109tcp_error_t tcp_uc_send(tcp_conn_t *conn, void *data, size_t size,
110 xflags_t flags)
111{
112 size_t buf_free;
113 size_t xfer_size;
114
115 log_msg(LVL_DEBUG, "%s: tcp_uc_send()", conn->name);
116
117 if (conn->cstate == st_closed)
118 return TCP_ENOTEXIST;
119
120 if (conn->cstate == st_listen) {
121 /* Change connection to active */
122 tcp_conn_sync(conn);
123 }
124
125 if (conn->snd_buf_fin)
126 return TCP_ECLOSING;
127
128 while (size > 0) {
129 buf_free = conn->snd_buf_size - conn->snd_buf_used;
130 while (buf_free == 0)
131 tcp_tqueue_new_data(conn);
132
133 xfer_size = min(size, buf_free);
134
135 /* Copy data to buffer */
136 memcpy(conn->snd_buf + conn->snd_buf_used, data, xfer_size);
137 data += xfer_size;
138 conn->snd_buf_used += xfer_size;
139 size -= xfer_size;
140 }
141
142 tcp_tqueue_new_data(conn);
143
144 return TCP_EOK;
145}
146
147/** RECEIVE user call */
148tcp_error_t tcp_uc_receive(tcp_conn_t *conn, void *buf, size_t size,
149 size_t *rcvd, xflags_t *xflags)
150{
151 size_t xfer_size;
152
153 log_msg(LVL_DEBUG, "%s: tcp_uc_receive()", conn->name);
154
155 if (conn->cstate == st_closed)
156 return TCP_ENOTEXIST;
157
158 fibril_mutex_lock(&conn->rcv_buf_lock);
159
160 /* Wait for data to become available */
161 while (conn->rcv_buf_used == 0 && !conn->rcv_buf_fin) {
162 log_msg(LVL_DEBUG, "tcp_uc_receive() - wait for data");
163 fibril_condvar_wait(&conn->rcv_buf_cv, &conn->rcv_buf_lock);
164 }
165
166 if (conn->rcv_buf_used == 0) {
167 fibril_mutex_unlock(&conn->rcv_buf_lock);
168
169 /* End of data, peer closed connection. */
170 assert(conn->rcv_buf_fin);
171 *rcvd = 0;
172 *xflags = 0;
173 return TCP_ECLOSING;
174 }
175
176 /* Copy data from receive buffer to user buffer */
177 xfer_size = min(size, conn->rcv_buf_used);
178 memcpy(buf, conn->rcv_buf, xfer_size);
179 *rcvd = xfer_size;
180
181 /* Remove data from receive buffer */
182 memmove(conn->rcv_buf, conn->rcv_buf + xfer_size, conn->rcv_buf_used -
183 xfer_size);
184 conn->rcv_buf_used -= xfer_size;
185 conn->rcv_wnd += xfer_size;
186
187 fibril_mutex_unlock(&conn->rcv_buf_lock);
188
189 /* TODO */
190 *xflags = 0;
191
192 /* Send new size of receive window */
193 tcp_tqueue_ctrl_seg(conn, CTL_ACK);
194
195 log_msg(LVL_DEBUG, "%s: tcp_uc_receive() - returning %zu bytes",
196 conn->name, xfer_size);
197
198 return TCP_EOK;
199}
200
201/** CLOSE user call */
202tcp_error_t tcp_uc_close(tcp_conn_t *conn)
203{
204 log_msg(LVL_DEBUG, "%s: tcp_uc_close()", conn->name);
205
206 if (conn->cstate == st_closed)
207 return TCP_ENOTEXIST;
208
209 if (conn->snd_buf_fin)
210 return TCP_ECLOSING;
211
212 conn->snd_buf_fin = true;
213 tcp_tqueue_new_data(conn);
214
215 return TCP_EOK;
216}
217
218/** ABORT user call */
219void tcp_uc_abort(tcp_conn_t *conn)
220{
221 log_msg(LVL_DEBUG, "tcp_uc_abort()");
222}
223
224/** STATUS user call */
225void tcp_uc_status(tcp_conn_t *conn, tcp_conn_status_t *cstatus)
226{
227 log_msg(LVL_DEBUG, "tcp_uc_status()");
228}
229
230
231/*
232 * Arriving segments
233 */
234
235/** Segment arrived */
236void tcp_as_segment_arrived(tcp_sockpair_t *sp, tcp_segment_t *seg)
237{
238 tcp_conn_t *conn;
239
240 log_msg(LVL_DEBUG, "tcp_as_segment_arrived()");
241
242 conn = tcp_conn_find(sp);
243 if (conn != NULL && conn->cstate != st_closed) {
244 tcp_conn_segment_arrived(conn, seg);
245 } else {
246 tcp_unexpected_segment(sp, seg);
247 }
248}
249
250/*
251 * Timeouts
252 */
253
254/** User timeout */
255void tcp_to_user(void)
256{
257 log_msg(LVL_DEBUG, "tcp_to_user()");
258}
259
260/**
261 * @}
262 */
Note: See TracBrowser for help on using the repository browser.