1 | /*
|
---|
2 | * Copyright (c) 2008 Lukas Mejdrech
|
---|
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 | /** @file
|
---|
34 | * TCP module implementation.
|
---|
35 | * @see tcp.h
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <assert.h>
|
---|
39 | #include <async.h>
|
---|
40 | #include <async_obsolete.h>
|
---|
41 | #include <fibril_synch.h>
|
---|
42 | #include <malloc.h>
|
---|
43 | /* TODO remove stdio */
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <errno.h>
|
---|
46 |
|
---|
47 | #include <ipc/services.h>
|
---|
48 | #include <ipc/net.h>
|
---|
49 | #include <ipc/tl.h>
|
---|
50 | #include <ipc/socket.h>
|
---|
51 |
|
---|
52 | #include <net/socket_codes.h>
|
---|
53 | #include <net/ip_protocols.h>
|
---|
54 | #include <net/in.h>
|
---|
55 | #include <net/in6.h>
|
---|
56 | #include <net/inet.h>
|
---|
57 | #include <net/modules.h>
|
---|
58 |
|
---|
59 | #include <adt/dynamic_fifo.h>
|
---|
60 | #include <packet_client.h>
|
---|
61 | #include <packet_remote.h>
|
---|
62 | #include <net_checksum.h>
|
---|
63 | #include <ip_client.h>
|
---|
64 | #include <ip_interface.h>
|
---|
65 | #include <icmp_client.h>
|
---|
66 | #include <icmp_remote.h>
|
---|
67 | #include <net_interface.h>
|
---|
68 | #include <socket_core.h>
|
---|
69 | #include <tl_common.h>
|
---|
70 | #include <tl_remote.h>
|
---|
71 | #include <tl_skel.h>
|
---|
72 |
|
---|
73 | #include "tcp.h"
|
---|
74 | #include "tcp_header.h"
|
---|
75 |
|
---|
76 | // FIXME: remove this header
|
---|
77 | #include <kernel/ipc/ipc_methods.h>
|
---|
78 |
|
---|
79 | /** TCP module name. */
|
---|
80 | #define NAME "tcp"
|
---|
81 |
|
---|
82 | /** The TCP window default value. */
|
---|
83 | #define NET_DEFAULT_TCP_WINDOW 10240
|
---|
84 |
|
---|
85 | /** Initial timeout for new connections. */
|
---|
86 | #define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L
|
---|
87 |
|
---|
88 | /** Default timeout for closing. */
|
---|
89 | #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
|
---|
90 |
|
---|
91 | /** The initial outgoing sequence number. */
|
---|
92 | #define TCP_INITIAL_SEQUENCE_NUMBER 2999
|
---|
93 |
|
---|
94 | /** Maximum TCP fragment size. */
|
---|
95 | #define MAX_TCP_FRAGMENT_SIZE 65535
|
---|
96 |
|
---|
97 | /** Free ports pool start. */
|
---|
98 | #define TCP_FREE_PORTS_START 1025
|
---|
99 |
|
---|
100 | /** Free ports pool end. */
|
---|
101 | #define TCP_FREE_PORTS_END 65535
|
---|
102 |
|
---|
103 | /** Timeout for connection initialization, SYN sent. */
|
---|
104 | #define TCP_SYN_SENT_TIMEOUT 1000000L
|
---|
105 |
|
---|
106 | /** The maximum number of timeouts in a row before singaling connection lost. */
|
---|
107 | #define TCP_MAX_TIMEOUTS 8
|
---|
108 |
|
---|
109 | /** The number of acknowledgements before retransmit. */
|
---|
110 | #define TCP_FAST_RETRANSMIT_COUNT 3
|
---|
111 |
|
---|
112 | /** Returns a value indicating whether the value is in the interval respecting
|
---|
113 | * the possible overflow.
|
---|
114 | *
|
---|
115 | * The high end and/or the value may overflow, be lower than the low value.
|
---|
116 | *
|
---|
117 | * @param[in] lower The last value before the interval.
|
---|
118 | * @param[in] value The value to be checked.
|
---|
119 | * @param[in] higher_equal The last value in the interval.
|
---|
120 | */
|
---|
121 | #define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \
|
---|
122 | ((((lower) < (value)) && (((value) <= (higher_equal)) || \
|
---|
123 | ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && \
|
---|
124 | ((higher_equal) < (lower))))
|
---|
125 |
|
---|
126 | /** Type definition of the TCP timeout.
|
---|
127 | * @see tcp_timeout
|
---|
128 | */
|
---|
129 | typedef struct tcp_timeout tcp_timeout_t;
|
---|
130 |
|
---|
131 | /** TCP reply timeout data.
|
---|
132 | * Used as a timeouting fibril argument.
|
---|
133 | * @see tcp_timeout()
|
---|
134 | */
|
---|
135 | struct tcp_timeout {
|
---|
136 | /** TCP global data are going to be read only. */
|
---|
137 | int globals_read_only;
|
---|
138 |
|
---|
139 | /** Socket port. */
|
---|
140 | int port;
|
---|
141 |
|
---|
142 | /** Local sockets. */
|
---|
143 | socket_cores_t *local_sockets;
|
---|
144 |
|
---|
145 | /** Socket identifier. */
|
---|
146 | int socket_id;
|
---|
147 |
|
---|
148 | /** Socket state. */
|
---|
149 | tcp_socket_state_t state;
|
---|
150 |
|
---|
151 | /** Sent packet sequence number. */
|
---|
152 | int sequence_number;
|
---|
153 |
|
---|
154 | /** Timeout in microseconds. */
|
---|
155 | suseconds_t timeout;
|
---|
156 |
|
---|
157 | /** Port map key. */
|
---|
158 | uint8_t *key;
|
---|
159 |
|
---|
160 | /** Port map key length. */
|
---|
161 | size_t key_length;
|
---|
162 | };
|
---|
163 |
|
---|
164 | static int tcp_release_and_return(packet_t *, int);
|
---|
165 | static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *,
|
---|
166 | tcp_header_t *, int synchronize, int);
|
---|
167 | static int tcp_prepare_timeout(int (*)(void *), socket_core_t *,
|
---|
168 | tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int);
|
---|
169 | static void tcp_free_socket_data(socket_core_t *);
|
---|
170 |
|
---|
171 | static int tcp_timeout(void *);
|
---|
172 |
|
---|
173 | static int tcp_release_after_timeout(void *);
|
---|
174 |
|
---|
175 | static int tcp_process_packet(device_id_t, packet_t *, services_t);
|
---|
176 | static int tcp_connect_core(socket_core_t *, socket_cores_t *,
|
---|
177 | struct sockaddr *, socklen_t);
|
---|
178 | static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *,
|
---|
179 | packet_t *, size_t);
|
---|
180 | static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *,
|
---|
181 | size_t);
|
---|
182 | static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *);
|
---|
183 | static void tcp_send_packets(device_id_t, packet_t *);
|
---|
184 |
|
---|
185 | static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *,
|
---|
186 | tcp_header_t *);
|
---|
187 | static packet_t *tcp_send_prepare_packet(socket_core_t *, tcp_socket_data_t *,
|
---|
188 | packet_t *, size_t, size_t);
|
---|
189 | static packet_t *tcp_prepare_copy(socket_core_t *, tcp_socket_data_t *,
|
---|
190 | packet_t *, size_t, size_t);
|
---|
191 | /* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *,
|
---|
192 | size_t);
|
---|
193 | static int tcp_create_notification_packet(packet_t **, socket_core_t *,
|
---|
194 | tcp_socket_data_t *, int, int);
|
---|
195 | static void tcp_refresh_socket_data(tcp_socket_data_t *);
|
---|
196 |
|
---|
197 | static void tcp_initialize_socket_data(tcp_socket_data_t *);
|
---|
198 |
|
---|
199 | static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *,
|
---|
200 | tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t);
|
---|
201 | static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *,
|
---|
202 | tcp_header_t *, packet_t *);
|
---|
203 | static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *,
|
---|
204 | tcp_header_t *, packet_t *);
|
---|
205 | static int tcp_process_established(socket_core_t *, tcp_socket_data_t *,
|
---|
206 | tcp_header_t *, packet_t *, int, size_t);
|
---|
207 | static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *,
|
---|
208 | packet_t *, int, size_t);
|
---|
209 | static void tcp_queue_received_end_of_data(socket_core_t *socket);
|
---|
210 |
|
---|
211 | static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t);
|
---|
212 | static int tcp_process_client_messages(ipc_callid_t, ipc_call_t);
|
---|
213 |
|
---|
214 | static int tcp_listen_message(socket_cores_t *, int, int);
|
---|
215 | static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *,
|
---|
216 | socklen_t);
|
---|
217 | static int tcp_recvfrom_message(socket_cores_t *, int, int, size_t *);
|
---|
218 | static int tcp_send_message(socket_cores_t *, int, int, size_t *, int);
|
---|
219 | static int tcp_accept_message(socket_cores_t *, int, int, size_t *, size_t *);
|
---|
220 | static int tcp_close_message(socket_cores_t *, int);
|
---|
221 |
|
---|
222 | /** TCP global data. */
|
---|
223 | tcp_globals_t tcp_globals;
|
---|
224 |
|
---|
225 | int tcp_received_msg(device_id_t device_id, packet_t *packet,
|
---|
226 | services_t receiver, services_t error)
|
---|
227 | {
|
---|
228 | int rc;
|
---|
229 |
|
---|
230 | if (receiver != SERVICE_TCP)
|
---|
231 | return EREFUSED;
|
---|
232 |
|
---|
233 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
234 | rc = tcp_process_packet(device_id, packet, error);
|
---|
235 | if (rc != EOK)
|
---|
236 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
237 |
|
---|
238 | printf("receive %d \n", rc);
|
---|
239 |
|
---|
240 | return rc;
|
---|
241 | }
|
---|
242 |
|
---|
243 | int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error)
|
---|
244 | {
|
---|
245 | size_t length;
|
---|
246 | size_t offset;
|
---|
247 | int result;
|
---|
248 | tcp_header_t *header;
|
---|
249 | socket_core_t *socket;
|
---|
250 | tcp_socket_data_t *socket_data;
|
---|
251 | packet_t *next_packet;
|
---|
252 | size_t total_length;
|
---|
253 | uint32_t checksum;
|
---|
254 | int fragments;
|
---|
255 | icmp_type_t type;
|
---|
256 | icmp_code_t code;
|
---|
257 | struct sockaddr *src;
|
---|
258 | struct sockaddr *dest;
|
---|
259 | size_t addrlen;
|
---|
260 | int rc;
|
---|
261 |
|
---|
262 | switch (error) {
|
---|
263 | case SERVICE_NONE:
|
---|
264 | break;
|
---|
265 | case SERVICE_ICMP:
|
---|
266 | /* Process error */
|
---|
267 | result = icmp_client_process_packet(packet, &type, &code, NULL,
|
---|
268 | NULL);
|
---|
269 | if (result < 0)
|
---|
270 | return tcp_release_and_return(packet, result);
|
---|
271 |
|
---|
272 | length = (size_t) result;
|
---|
273 | rc = packet_trim(packet, length, 0);
|
---|
274 | if (rc != EOK)
|
---|
275 | return tcp_release_and_return(packet, rc);
|
---|
276 | break;
|
---|
277 | default:
|
---|
278 | return tcp_release_and_return(packet, ENOTSUP);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* TODO process received ipopts? */
|
---|
282 | result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
|
---|
283 | if (result < 0)
|
---|
284 | return tcp_release_and_return(packet, result);
|
---|
285 |
|
---|
286 | offset = (size_t) result;
|
---|
287 |
|
---|
288 | length = packet_get_data_length(packet);
|
---|
289 | if (length <= 0)
|
---|
290 | return tcp_release_and_return(packet, EINVAL);
|
---|
291 |
|
---|
292 | if (length < TCP_HEADER_SIZE + offset)
|
---|
293 | return tcp_release_and_return(packet, NO_DATA);
|
---|
294 |
|
---|
295 | /* Trim all but TCP header */
|
---|
296 | rc = packet_trim(packet, offset, 0);
|
---|
297 | if (rc != EOK)
|
---|
298 | return tcp_release_and_return(packet, rc);
|
---|
299 |
|
---|
300 | /* Get tcp header */
|
---|
301 | header = (tcp_header_t *) packet_get_data(packet);
|
---|
302 | if (!header)
|
---|
303 | return tcp_release_and_return(packet, NO_DATA);
|
---|
304 |
|
---|
305 | #if 0
|
---|
306 | printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
|
---|
307 | ntohs(header->destination_port));
|
---|
308 | #endif
|
---|
309 | result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
|
---|
310 | if (result <= 0)
|
---|
311 | return tcp_release_and_return(packet, result);
|
---|
312 |
|
---|
313 | addrlen = (size_t) result;
|
---|
314 |
|
---|
315 | rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
|
---|
316 | if (rc != EOK)
|
---|
317 | return tcp_release_and_return(packet, rc);
|
---|
318 |
|
---|
319 | /* Find the destination socket */
|
---|
320 | socket = socket_port_find(&tcp_globals.sockets,
|
---|
321 | ntohs(header->destination_port), (uint8_t *) src, addrlen);
|
---|
322 | if (!socket) {
|
---|
323 | /* Find the listening destination socket */
|
---|
324 | socket = socket_port_find(&tcp_globals.sockets,
|
---|
325 | ntohs(header->destination_port),
|
---|
326 | (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
|
---|
327 | }
|
---|
328 |
|
---|
329 | if (!socket) {
|
---|
330 | if (tl_prepare_icmp_packet(tcp_globals.net_phone,
|
---|
331 | tcp_globals.icmp_phone, packet, error) == EOK) {
|
---|
332 | icmp_destination_unreachable_msg(tcp_globals.icmp_phone,
|
---|
333 | ICMP_PORT_UNREACH, 0, packet);
|
---|
334 | }
|
---|
335 | return EADDRNOTAVAIL;
|
---|
336 | }
|
---|
337 |
|
---|
338 | printf("socket id %d\n", socket->socket_id);
|
---|
339 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
340 | assert(socket_data);
|
---|
341 |
|
---|
342 | /* Some data received, clear the timeout counter */
|
---|
343 | socket_data->timeout_count = 0;
|
---|
344 |
|
---|
345 | /* Count the received packet fragments */
|
---|
346 | next_packet = packet;
|
---|
347 | fragments = 0;
|
---|
348 | checksum = 0;
|
---|
349 | total_length = 0;
|
---|
350 | do {
|
---|
351 | fragments++;
|
---|
352 | length = packet_get_data_length(next_packet);
|
---|
353 | if (length <= 0)
|
---|
354 | return tcp_release_and_return(packet, NO_DATA);
|
---|
355 |
|
---|
356 | total_length += length;
|
---|
357 |
|
---|
358 | /* Add partial checksum if set */
|
---|
359 | if (!error) {
|
---|
360 | checksum = compute_checksum(checksum,
|
---|
361 | packet_get_data(packet),
|
---|
362 | packet_get_data_length(packet));
|
---|
363 | }
|
---|
364 |
|
---|
365 | } while ((next_packet = pq_next(next_packet)));
|
---|
366 |
|
---|
367 | fibril_rwlock_write_lock(socket_data->local_lock);
|
---|
368 |
|
---|
369 | if (error)
|
---|
370 | goto has_error_service;
|
---|
371 |
|
---|
372 | if (socket_data->state == TCP_SOCKET_LISTEN) {
|
---|
373 | if (socket_data->pseudo_header) {
|
---|
374 | free(socket_data->pseudo_header);
|
---|
375 | socket_data->pseudo_header = NULL;
|
---|
376 | socket_data->headerlen = 0;
|
---|
377 | }
|
---|
378 |
|
---|
379 | rc = ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen,
|
---|
380 | dest, addrlen, total_length, &socket_data->pseudo_header,
|
---|
381 | &socket_data->headerlen);
|
---|
382 | if (rc != EOK) {
|
---|
383 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
384 | return tcp_release_and_return(packet, rc);
|
---|
385 | }
|
---|
386 | } else {
|
---|
387 | rc = ip_client_set_pseudo_header_data_length(
|
---|
388 | socket_data->pseudo_header, socket_data->headerlen,
|
---|
389 | total_length);
|
---|
390 | if (rc != EOK) {
|
---|
391 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
392 | return tcp_release_and_return(packet, rc);
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | checksum = compute_checksum(checksum, socket_data->pseudo_header,
|
---|
397 | socket_data->headerlen);
|
---|
398 | if (flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO) {
|
---|
399 | printf("checksum err %x -> %x\n", header->checksum,
|
---|
400 | flip_checksum(compact_checksum(checksum)));
|
---|
401 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
402 |
|
---|
403 | rc = tl_prepare_icmp_packet(tcp_globals.net_phone,
|
---|
404 | tcp_globals.icmp_phone, packet, error);
|
---|
405 | if (rc == EOK) {
|
---|
406 | /* Checksum error ICMP */
|
---|
407 | icmp_parameter_problem_msg(tcp_globals.icmp_phone,
|
---|
408 | ICMP_PARAM_POINTER,
|
---|
409 | ((size_t) ((void *) &header->checksum)) -
|
---|
410 | ((size_t) ((void *) header)), packet);
|
---|
411 | }
|
---|
412 |
|
---|
413 | return EINVAL;
|
---|
414 | }
|
---|
415 |
|
---|
416 | has_error_service:
|
---|
417 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
418 |
|
---|
419 | /* TODO error reporting/handling */
|
---|
420 | switch (socket_data->state) {
|
---|
421 | case TCP_SOCKET_LISTEN:
|
---|
422 | rc = tcp_process_listen(socket, socket_data, header, packet,
|
---|
423 | src, dest, addrlen);
|
---|
424 | break;
|
---|
425 | case TCP_SOCKET_SYN_RECEIVED:
|
---|
426 | rc = tcp_process_syn_received(socket, socket_data, header,
|
---|
427 | packet);
|
---|
428 | break;
|
---|
429 | case TCP_SOCKET_SYN_SENT:
|
---|
430 | rc = tcp_process_syn_sent(socket, socket_data, header, packet);
|
---|
431 | break;
|
---|
432 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
433 | /* ack changing the state to FIN_WAIT_2 gets processed later */
|
---|
434 | case TCP_SOCKET_FIN_WAIT_2:
|
---|
435 | /* fin changing state to LAST_ACK gets processed later */
|
---|
436 | case TCP_SOCKET_LAST_ACK:
|
---|
437 | /* ack releasing the socket get processed later */
|
---|
438 | case TCP_SOCKET_CLOSING:
|
---|
439 | /* ack releasing the socket gets processed later */
|
---|
440 | case TCP_SOCKET_ESTABLISHED:
|
---|
441 | rc = tcp_process_established(socket, socket_data, header,
|
---|
442 | packet, fragments, total_length);
|
---|
443 | break;
|
---|
444 | default:
|
---|
445 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
446 | }
|
---|
447 |
|
---|
448 | if (rc != EOK) {
|
---|
449 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
450 | printf("process %d\n", rc);
|
---|
451 | }
|
---|
452 |
|
---|
453 | return EOK;
|
---|
454 | }
|
---|
455 |
|
---|
456 | int tcp_process_established(socket_core_t *socket, tcp_socket_data_t *
|
---|
457 | socket_data, tcp_header_t *header, packet_t *packet, int fragments,
|
---|
458 | size_t total_length)
|
---|
459 | {
|
---|
460 | packet_t *next_packet;
|
---|
461 | packet_t *tmp_packet;
|
---|
462 | uint32_t old_incoming;
|
---|
463 | size_t order;
|
---|
464 | uint32_t sequence_number;
|
---|
465 | size_t length;
|
---|
466 | size_t offset;
|
---|
467 | uint32_t new_sequence_number;
|
---|
468 | bool forced_ack;
|
---|
469 | int rc;
|
---|
470 |
|
---|
471 | assert(socket);
|
---|
472 | assert(socket_data);
|
---|
473 | assert(socket->specific_data == socket_data);
|
---|
474 | assert(header);
|
---|
475 | assert(packet);
|
---|
476 |
|
---|
477 | forced_ack = false;
|
---|
478 |
|
---|
479 | new_sequence_number = ntohl(header->sequence_number);
|
---|
480 | old_incoming = socket_data->next_incoming;
|
---|
481 |
|
---|
482 | if (GET_TCP_HEADER_FINALIZE(header)) {
|
---|
483 | socket_data->fin_incoming = new_sequence_number +
|
---|
484 | total_length - TCP_HEADER_LENGTH(header);
|
---|
485 | }
|
---|
486 |
|
---|
487 | /* Trim begining if containing expected data */
|
---|
488 | if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
|
---|
489 | socket_data->next_incoming, new_sequence_number + total_length)) {
|
---|
490 |
|
---|
491 | /* Get the acknowledged offset */
|
---|
492 | if (socket_data->next_incoming < new_sequence_number) {
|
---|
493 | offset = new_sequence_number -
|
---|
494 | socket_data->next_incoming;
|
---|
495 | } else {
|
---|
496 | offset = socket_data->next_incoming -
|
---|
497 | new_sequence_number;
|
---|
498 | }
|
---|
499 |
|
---|
500 | new_sequence_number += offset;
|
---|
501 | total_length -= offset;
|
---|
502 | length = packet_get_data_length(packet);
|
---|
503 |
|
---|
504 | /* Trim the acknowledged data */
|
---|
505 | while (length <= offset) {
|
---|
506 | /* Release the acknowledged packets */
|
---|
507 | next_packet = pq_next(packet);
|
---|
508 | pq_release_remote(tcp_globals.net_phone,
|
---|
509 | packet_get_id(packet));
|
---|
510 | packet = next_packet;
|
---|
511 | offset -= length;
|
---|
512 | length = packet_get_data_length(packet);
|
---|
513 | }
|
---|
514 |
|
---|
515 | if (offset > 0) {
|
---|
516 | rc = packet_trim(packet, offset, 0);
|
---|
517 | if (rc != EOK)
|
---|
518 | return tcp_release_and_return(packet, rc);
|
---|
519 | }
|
---|
520 |
|
---|
521 | assert(new_sequence_number == socket_data->next_incoming);
|
---|
522 | }
|
---|
523 |
|
---|
524 | /* Release if overflowing the window */
|
---|
525 | /*
|
---|
526 | if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +
|
---|
527 | socket_data->window, new_sequence_number, new_sequence_number +
|
---|
528 | total_length)) {
|
---|
529 | return tcp_release_and_return(packet, EOVERFLOW);
|
---|
530 | }
|
---|
531 |
|
---|
532 | // trim end if overflowing the window
|
---|
533 | if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
|
---|
534 | socket_data->next_incoming + socket_data->window,
|
---|
535 | new_sequence_number + total_length)) {
|
---|
536 | // get the allowed data length
|
---|
537 | if (socket_data->next_incoming + socket_data->window <
|
---|
538 | new_sequence_number) {
|
---|
539 | offset = new_sequence_number -
|
---|
540 | socket_data->next_incoming + socket_data->window;
|
---|
541 | } else {
|
---|
542 | offset = socket_data->next_incoming +
|
---|
543 | socket_data->window - new_sequence_number;
|
---|
544 | }
|
---|
545 | next_packet = packet;
|
---|
546 | // trim the overflowing data
|
---|
547 | while (next_packet && (offset > 0)) {
|
---|
548 | length = packet_get_data_length(packet);
|
---|
549 | if (length <= offset)
|
---|
550 | next_packet = pq_next(next_packet);
|
---|
551 | else {
|
---|
552 | rc = packet_trim(next_packet, 0,
|
---|
553 | length - offset));
|
---|
554 | if (rc != EOK)
|
---|
555 | return tcp_release_and_return(packet,
|
---|
556 | rc);
|
---|
557 | }
|
---|
558 | offset -= length;
|
---|
559 | total_length -= length - offset;
|
---|
560 | }
|
---|
561 | // release the overflowing packets
|
---|
562 | next_packet = pq_next(next_packet);
|
---|
563 | if (next_packet) {
|
---|
564 | tmp_packet = next_packet;
|
---|
565 | next_packet = pq_next(next_packet);
|
---|
566 | pq_insert_after(tmp_packet, next_packet);
|
---|
567 | pq_release_remote(tcp_globals.net_phone,
|
---|
568 | packet_get_id(tmp_packet));
|
---|
569 | }
|
---|
570 | assert(new_sequence_number + total_length ==
|
---|
571 | socket_data->next_incoming + socket_data->window);
|
---|
572 | }
|
---|
573 | */
|
---|
574 | /* The expected one arrived? */
|
---|
575 | if (new_sequence_number == socket_data->next_incoming) {
|
---|
576 | printf("expected\n");
|
---|
577 | /* Process acknowledgement */
|
---|
578 | tcp_process_acknowledgement(socket, socket_data, header);
|
---|
579 |
|
---|
580 | /* Remove the header */
|
---|
581 | total_length -= TCP_HEADER_LENGTH(header);
|
---|
582 | rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
|
---|
583 | if (rc != EOK)
|
---|
584 | return tcp_release_and_return(packet, rc);
|
---|
585 |
|
---|
586 | if (total_length) {
|
---|
587 | rc = tcp_queue_received_packet(socket, socket_data,
|
---|
588 | packet, fragments, total_length);
|
---|
589 | if (rc != EOK)
|
---|
590 | return rc;
|
---|
591 | } else {
|
---|
592 | total_length = 1;
|
---|
593 | }
|
---|
594 |
|
---|
595 | socket_data->next_incoming = old_incoming + total_length;
|
---|
596 | packet = socket_data->incoming;
|
---|
597 | while (packet) {
|
---|
598 | rc = pq_get_order(socket_data->incoming, &order, NULL);
|
---|
599 | if (rc != EOK) {
|
---|
600 | /* Remove the corrupted packet */
|
---|
601 | next_packet = pq_detach(packet);
|
---|
602 | if (packet == socket_data->incoming)
|
---|
603 | socket_data->incoming = next_packet;
|
---|
604 | pq_release_remote(tcp_globals.net_phone,
|
---|
605 | packet_get_id(packet));
|
---|
606 | packet = next_packet;
|
---|
607 | continue;
|
---|
608 | }
|
---|
609 |
|
---|
610 | sequence_number = (uint32_t) order;
|
---|
611 | if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
|
---|
612 | old_incoming, socket_data->next_incoming)) {
|
---|
613 | /* Move to the next */
|
---|
614 | packet = pq_next(packet);
|
---|
615 | /* Coninual data? */
|
---|
616 | } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
|
---|
617 | sequence_number, socket_data->next_incoming)) {
|
---|
618 | /* Detach the packet */
|
---|
619 | next_packet = pq_detach(packet);
|
---|
620 | if (packet == socket_data->incoming)
|
---|
621 | socket_data->incoming = next_packet;
|
---|
622 | /* Get data length */
|
---|
623 | length = packet_get_data_length(packet);
|
---|
624 | new_sequence_number = sequence_number + length;
|
---|
625 | if (length <= 0) {
|
---|
626 | /* Remove the empty packet */
|
---|
627 | pq_release_remote(tcp_globals.net_phone,
|
---|
628 | packet_get_id(packet));
|
---|
629 | packet = next_packet;
|
---|
630 | continue;
|
---|
631 | }
|
---|
632 | /* Exactly following */
|
---|
633 | if (sequence_number ==
|
---|
634 | socket_data->next_incoming) {
|
---|
635 | /* Queue received data */
|
---|
636 | rc = tcp_queue_received_packet(socket,
|
---|
637 | socket_data, packet, 1,
|
---|
638 | packet_get_data_length(packet));
|
---|
639 | if (rc != EOK)
|
---|
640 | return rc;
|
---|
641 | socket_data->next_incoming =
|
---|
642 | new_sequence_number;
|
---|
643 | packet = next_packet;
|
---|
644 | continue;
|
---|
645 | /* At least partly following data? */
|
---|
646 | }
|
---|
647 | if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
|
---|
648 | socket_data->next_incoming, new_sequence_number)) {
|
---|
649 | if (socket_data->next_incoming <
|
---|
650 | new_sequence_number) {
|
---|
651 | length = new_sequence_number -
|
---|
652 | socket_data->next_incoming;
|
---|
653 | } else {
|
---|
654 | length =
|
---|
655 | socket_data->next_incoming -
|
---|
656 | new_sequence_number;
|
---|
657 | }
|
---|
658 | rc = packet_trim(packet,length, 0);
|
---|
659 | if (rc == EOK) {
|
---|
660 | /* Queue received data */
|
---|
661 | rc = tcp_queue_received_packet(
|
---|
662 | socket, socket_data, packet,
|
---|
663 | 1, packet_get_data_length(
|
---|
664 | packet));
|
---|
665 | if (rc != EOK)
|
---|
666 | return rc;
|
---|
667 | socket_data->next_incoming =
|
---|
668 | new_sequence_number;
|
---|
669 | packet = next_packet;
|
---|
670 | continue;
|
---|
671 | }
|
---|
672 | }
|
---|
673 | /* Remove the duplicit or corrupted packet */
|
---|
674 | pq_release_remote(tcp_globals.net_phone,
|
---|
675 | packet_get_id(packet));
|
---|
676 | packet = next_packet;
|
---|
677 | continue;
|
---|
678 | } else {
|
---|
679 | break;
|
---|
680 | }
|
---|
681 | }
|
---|
682 | } else if (IS_IN_INTERVAL(socket_data->next_incoming,
|
---|
683 | new_sequence_number,
|
---|
684 | socket_data->next_incoming + socket_data->window)) {
|
---|
685 | printf("in window\n");
|
---|
686 | /* Process acknowledgement */
|
---|
687 | tcp_process_acknowledgement(socket, socket_data, header);
|
---|
688 |
|
---|
689 | /* Remove the header */
|
---|
690 | total_length -= TCP_HEADER_LENGTH(header);
|
---|
691 | rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
|
---|
692 | if (rc != EOK)
|
---|
693 | return tcp_release_and_return(packet, rc);
|
---|
694 |
|
---|
695 | next_packet = pq_detach(packet);
|
---|
696 | length = packet_get_data_length(packet);
|
---|
697 | rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
|
---|
698 | length);
|
---|
699 | if (rc != EOK) {
|
---|
700 | /* Remove the corrupted packets */
|
---|
701 | pq_release_remote(tcp_globals.net_phone,
|
---|
702 | packet_get_id(packet));
|
---|
703 | pq_release_remote(tcp_globals.net_phone,
|
---|
704 | packet_get_id(next_packet));
|
---|
705 | } else {
|
---|
706 | while (next_packet) {
|
---|
707 | new_sequence_number += length;
|
---|
708 | tmp_packet = pq_detach(next_packet);
|
---|
709 | length = packet_get_data_length(next_packet);
|
---|
710 |
|
---|
711 | rc = pq_set_order(next_packet,
|
---|
712 | new_sequence_number, length);
|
---|
713 | if (rc != EOK) {
|
---|
714 | pq_release_remote(tcp_globals.net_phone,
|
---|
715 | packet_get_id(next_packet));
|
---|
716 | }
|
---|
717 | rc = pq_insert_after(packet, next_packet);
|
---|
718 | if (rc != EOK) {
|
---|
719 | pq_release_remote(tcp_globals.net_phone,
|
---|
720 | packet_get_id(next_packet));
|
---|
721 | }
|
---|
722 | next_packet = tmp_packet;
|
---|
723 | }
|
---|
724 | }
|
---|
725 | } else {
|
---|
726 | printf("unexpected\n");
|
---|
727 | /* Release duplicite or restricted */
|
---|
728 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
729 | forced_ack = true;
|
---|
730 | }
|
---|
731 |
|
---|
732 | /* If next in sequence is an incoming FIN */
|
---|
733 | if (socket_data->next_incoming == socket_data->fin_incoming) {
|
---|
734 | /* Advance sequence number */
|
---|
735 | socket_data->next_incoming += 1;
|
---|
736 |
|
---|
737 | /* Handle FIN */
|
---|
738 | switch (socket_data->state) {
|
---|
739 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
740 | case TCP_SOCKET_FIN_WAIT_2:
|
---|
741 | case TCP_SOCKET_CLOSING:
|
---|
742 | socket_data->state = TCP_SOCKET_CLOSING;
|
---|
743 | break;
|
---|
744 | case TCP_SOCKET_ESTABLISHED:
|
---|
745 | /* Queue end-of-data marker on the socket. */
|
---|
746 | tcp_queue_received_end_of_data(socket);
|
---|
747 | socket_data->state = TCP_SOCKET_CLOSE_WAIT;
|
---|
748 | break;
|
---|
749 | default:
|
---|
750 | socket_data->state = TCP_SOCKET_CLOSE_WAIT;
|
---|
751 | break;
|
---|
752 | }
|
---|
753 | }
|
---|
754 |
|
---|
755 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
756 | if (!packet && (socket_data->next_incoming != old_incoming || forced_ack)) {
|
---|
757 | /* Create the notification packet */
|
---|
758 | rc = tcp_create_notification_packet(&packet, socket,
|
---|
759 | socket_data, 0, 0);
|
---|
760 | if (rc != EOK)
|
---|
761 | return rc;
|
---|
762 | rc = tcp_queue_prepare_packet(socket, socket_data, packet, 1);
|
---|
763 | if (rc != EOK)
|
---|
764 | return rc;
|
---|
765 | packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
|
---|
766 | socket_data->last_outgoing + 1);
|
---|
767 | }
|
---|
768 |
|
---|
769 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
770 |
|
---|
771 | /* Send the packet */
|
---|
772 | tcp_send_packets(socket_data->device_id, packet);
|
---|
773 |
|
---|
774 | return EOK;
|
---|
775 | }
|
---|
776 |
|
---|
777 | int tcp_queue_received_packet(socket_core_t *socket,
|
---|
778 | tcp_socket_data_t *socket_data, packet_t *packet, int fragments,
|
---|
779 | size_t total_length)
|
---|
780 | {
|
---|
781 | packet_dimension_t *packet_dimension;
|
---|
782 | int rc;
|
---|
783 |
|
---|
784 | assert(socket);
|
---|
785 | assert(socket_data);
|
---|
786 | assert(socket->specific_data == socket_data);
|
---|
787 | assert(packet);
|
---|
788 | assert(fragments >= 1);
|
---|
789 | assert(socket_data->window > total_length);
|
---|
790 |
|
---|
791 | /* Queue the received packet */
|
---|
792 | rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
|
---|
793 | SOCKET_MAX_RECEIVED_SIZE);
|
---|
794 | if (rc != EOK)
|
---|
795 | return tcp_release_and_return(packet, rc);
|
---|
796 | rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
|
---|
797 | &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
|
---|
798 | if (rc != EOK)
|
---|
799 | return tcp_release_and_return(packet, rc);
|
---|
800 |
|
---|
801 | /* Decrease the window size */
|
---|
802 | socket_data->window -= total_length;
|
---|
803 |
|
---|
804 | /* Notify the destination socket */
|
---|
805 | async_obsolete_msg_5(socket->phone, NET_SOCKET_RECEIVED,
|
---|
806 | (sysarg_t) socket->socket_id,
|
---|
807 | ((packet_dimension->content < socket_data->data_fragment_size) ?
|
---|
808 | packet_dimension->content : socket_data->data_fragment_size), 0, 0,
|
---|
809 | (sysarg_t) fragments);
|
---|
810 |
|
---|
811 | return EOK;
|
---|
812 | }
|
---|
813 |
|
---|
814 | /** Queue end-of-data marker on the socket.
|
---|
815 | *
|
---|
816 | * Next element in the sequence space is FIN. Queue end-of-data marker
|
---|
817 | * on the socket.
|
---|
818 | *
|
---|
819 | * @param socket Socket
|
---|
820 | */
|
---|
821 | static void tcp_queue_received_end_of_data(socket_core_t *socket)
|
---|
822 | {
|
---|
823 | assert(socket != NULL);
|
---|
824 |
|
---|
825 | /* Notify the destination socket */
|
---|
826 | async_obsolete_msg_5(socket->phone, NET_SOCKET_RECEIVED,
|
---|
827 | (sysarg_t) socket->socket_id,
|
---|
828 | 0, 0, 0,
|
---|
829 | (sysarg_t) 0 /* 0 fragments == no more data */);
|
---|
830 | }
|
---|
831 |
|
---|
832 | int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t *
|
---|
833 | socket_data, tcp_header_t *header, packet_t *packet)
|
---|
834 | {
|
---|
835 | packet_t *next_packet;
|
---|
836 | int rc;
|
---|
837 |
|
---|
838 | assert(socket);
|
---|
839 | assert(socket_data);
|
---|
840 | assert(socket->specific_data == socket_data);
|
---|
841 | assert(header);
|
---|
842 | assert(packet);
|
---|
843 |
|
---|
844 | if (!GET_TCP_HEADER_SYNCHRONIZE(header))
|
---|
845 | return tcp_release_and_return(packet, EINVAL);
|
---|
846 |
|
---|
847 | /* Process acknowledgement */
|
---|
848 | tcp_process_acknowledgement(socket, socket_data, header);
|
---|
849 |
|
---|
850 | socket_data->next_incoming = ntohl(header->sequence_number) + 1;
|
---|
851 |
|
---|
852 | /* Release additional packets */
|
---|
853 | next_packet = pq_detach(packet);
|
---|
854 | if (next_packet) {
|
---|
855 | pq_release_remote(tcp_globals.net_phone,
|
---|
856 | packet_get_id(next_packet));
|
---|
857 | }
|
---|
858 |
|
---|
859 | /* Trim if longer than the header */
|
---|
860 | if (packet_get_data_length(packet) > sizeof(*header)) {
|
---|
861 | rc = packet_trim(packet, 0,
|
---|
862 | packet_get_data_length(packet) - sizeof(*header));
|
---|
863 | if (rc != EOK)
|
---|
864 | return tcp_release_and_return(packet, rc);
|
---|
865 | }
|
---|
866 |
|
---|
867 | tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
|
---|
868 | fibril_mutex_lock(&socket_data->operation.mutex);
|
---|
869 | socket_data->operation.result = tcp_queue_packet(socket, socket_data,
|
---|
870 | packet, 1);
|
---|
871 |
|
---|
872 | if (socket_data->operation.result == EOK) {
|
---|
873 | socket_data->state = TCP_SOCKET_ESTABLISHED;
|
---|
874 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
875 | if (packet) {
|
---|
876 | fibril_rwlock_write_unlock( socket_data->local_lock);
|
---|
877 | /* Send the packet */
|
---|
878 | tcp_send_packets(socket_data->device_id, packet);
|
---|
879 | /* Signal the result */
|
---|
880 | fibril_condvar_signal( &socket_data->operation.condvar);
|
---|
881 | fibril_mutex_unlock( &socket_data->operation.mutex);
|
---|
882 | return EOK;
|
---|
883 | }
|
---|
884 | }
|
---|
885 |
|
---|
886 | fibril_mutex_unlock(&socket_data->operation.mutex);
|
---|
887 | return tcp_release_and_return(packet, EINVAL);
|
---|
888 | }
|
---|
889 |
|
---|
890 | int tcp_process_listen(socket_core_t *listening_socket,
|
---|
891 | tcp_socket_data_t *listening_socket_data, tcp_header_t *header,
|
---|
892 | packet_t *packet, struct sockaddr *src, struct sockaddr *dest,
|
---|
893 | size_t addrlen)
|
---|
894 | {
|
---|
895 | packet_t *next_packet;
|
---|
896 | socket_core_t *socket;
|
---|
897 | tcp_socket_data_t *socket_data;
|
---|
898 | int socket_id;
|
---|
899 | int listening_socket_id = listening_socket->socket_id;
|
---|
900 | int listening_port = listening_socket->port;
|
---|
901 | int rc;
|
---|
902 |
|
---|
903 | assert(listening_socket);
|
---|
904 | assert(listening_socket_data);
|
---|
905 | assert(listening_socket->specific_data == listening_socket_data);
|
---|
906 | assert(header);
|
---|
907 | assert(packet);
|
---|
908 |
|
---|
909 | if (!GET_TCP_HEADER_SYNCHRONIZE(header))
|
---|
910 | return tcp_release_and_return(packet, EINVAL);
|
---|
911 |
|
---|
912 | socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data));
|
---|
913 | if (!socket_data)
|
---|
914 | return tcp_release_and_return(packet, ENOMEM);
|
---|
915 |
|
---|
916 | tcp_initialize_socket_data(socket_data);
|
---|
917 | socket_data->local_lock = listening_socket_data->local_lock;
|
---|
918 | socket_data->local_sockets = listening_socket_data->local_sockets;
|
---|
919 | socket_data->listening_socket_id = listening_socket->socket_id;
|
---|
920 | socket_data->next_incoming = ntohl(header->sequence_number);
|
---|
921 | socket_data->treshold = socket_data->next_incoming +
|
---|
922 | ntohs(header->window);
|
---|
923 | socket_data->addrlen = addrlen;
|
---|
924 | socket_data->addr = malloc(socket_data->addrlen);
|
---|
925 | if (!socket_data->addr) {
|
---|
926 | free(socket_data);
|
---|
927 | return tcp_release_and_return(packet, ENOMEM);
|
---|
928 | }
|
---|
929 |
|
---|
930 | memcpy(socket_data->addr, src, socket_data->addrlen);
|
---|
931 | socket_data->dest_port = ntohs(header->source_port);
|
---|
932 | rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
|
---|
933 | socket_data->dest_port);
|
---|
934 | if (rc != EOK) {
|
---|
935 | free(socket_data->addr);
|
---|
936 | free(socket_data);
|
---|
937 | return tcp_release_and_return(packet, rc);
|
---|
938 | }
|
---|
939 |
|
---|
940 | /* Create a socket */
|
---|
941 | socket_id = -1;
|
---|
942 | rc = socket_create(socket_data->local_sockets, listening_socket->phone,
|
---|
943 | socket_data, &socket_id);
|
---|
944 | if (rc != EOK) {
|
---|
945 | free(socket_data->addr);
|
---|
946 | free(socket_data);
|
---|
947 | return tcp_release_and_return(packet, rc);
|
---|
948 | }
|
---|
949 |
|
---|
950 | printf("new_sock %d\n", socket_id);
|
---|
951 | socket_data->pseudo_header = listening_socket_data->pseudo_header;
|
---|
952 | socket_data->headerlen = listening_socket_data->headerlen;
|
---|
953 | listening_socket_data->pseudo_header = NULL;
|
---|
954 | listening_socket_data->headerlen = 0;
|
---|
955 |
|
---|
956 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
957 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
958 |
|
---|
959 | /* Find the destination socket */
|
---|
960 | listening_socket = socket_port_find(&tcp_globals.sockets,
|
---|
961 | listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
|
---|
962 | if (!listening_socket ||
|
---|
963 | (listening_socket->socket_id != listening_socket_id)) {
|
---|
964 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
965 | /* A shadow may remain until app hangs up */
|
---|
966 | return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
|
---|
967 | }
|
---|
968 | listening_socket_data =
|
---|
969 | (tcp_socket_data_t *) listening_socket->specific_data;
|
---|
970 | assert(listening_socket_data);
|
---|
971 |
|
---|
972 | fibril_rwlock_write_lock(listening_socket_data->local_lock);
|
---|
973 |
|
---|
974 | socket = socket_cores_find(listening_socket_data->local_sockets,
|
---|
975 | socket_id);
|
---|
976 | if (!socket) {
|
---|
977 | /* Where is the socket?!? */
|
---|
978 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
979 | return ENOTSOCK;
|
---|
980 | }
|
---|
981 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
982 | assert(socket_data);
|
---|
983 |
|
---|
984 | rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
|
---|
985 | (uint8_t *) socket_data->addr, socket_data->addrlen);
|
---|
986 | assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
|
---|
987 | (uint8_t *) socket_data->addr, socket_data->addrlen));
|
---|
988 |
|
---|
989 | // rc = socket_bind_free_port(&tcp_globals.sockets, socket,
|
---|
990 | // TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
991 | // tcp_globals.last_used_port);
|
---|
992 | // tcp_globals.last_used_port = socket->port;
|
---|
993 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
994 | if (rc != EOK) {
|
---|
995 | socket_destroy(tcp_globals.net_phone, socket->socket_id,
|
---|
996 | socket_data->local_sockets, &tcp_globals.sockets,
|
---|
997 | tcp_free_socket_data);
|
---|
998 | return tcp_release_and_return(packet, rc);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | socket_data->state = TCP_SOCKET_LISTEN;
|
---|
1002 | socket_data->next_incoming = ntohl(header->sequence_number) + 1;
|
---|
1003 |
|
---|
1004 | /* Release additional packets */
|
---|
1005 | next_packet = pq_detach(packet);
|
---|
1006 | if (next_packet) {
|
---|
1007 | pq_release_remote(tcp_globals.net_phone,
|
---|
1008 | packet_get_id(next_packet));
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /* Trim if longer than the header */
|
---|
1012 | if (packet_get_data_length(packet) > sizeof(*header)) {
|
---|
1013 | rc = packet_trim(packet, 0,
|
---|
1014 | packet_get_data_length(packet) - sizeof(*header));
|
---|
1015 | if (rc != EOK) {
|
---|
1016 | socket_destroy(tcp_globals.net_phone, socket->socket_id,
|
---|
1017 | socket_data->local_sockets, &tcp_globals.sockets,
|
---|
1018 | tcp_free_socket_data);
|
---|
1019 | return tcp_release_and_return(packet, rc);
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
|
---|
1024 |
|
---|
1025 | rc = tcp_queue_packet(socket, socket_data, packet, 1);
|
---|
1026 | if (rc != EOK) {
|
---|
1027 | socket_destroy(tcp_globals.net_phone, socket->socket_id,
|
---|
1028 | socket_data->local_sockets, &tcp_globals.sockets,
|
---|
1029 | tcp_free_socket_data);
|
---|
1030 | return rc;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
1034 | if (!packet) {
|
---|
1035 | socket_destroy(tcp_globals.net_phone, socket->socket_id,
|
---|
1036 | socket_data->local_sockets, &tcp_globals.sockets,
|
---|
1037 | tcp_free_socket_data);
|
---|
1038 | return EINVAL;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | socket_data->state = TCP_SOCKET_SYN_RECEIVED;
|
---|
1042 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1043 |
|
---|
1044 | /* Send the packet */
|
---|
1045 | tcp_send_packets(socket_data->device_id, packet);
|
---|
1046 |
|
---|
1047 | return EOK;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | int tcp_process_syn_received(socket_core_t *socket,
|
---|
1051 | tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet)
|
---|
1052 | {
|
---|
1053 | socket_core_t *listening_socket;
|
---|
1054 | tcp_socket_data_t *listening_socket_data;
|
---|
1055 | int rc;
|
---|
1056 |
|
---|
1057 | assert(socket);
|
---|
1058 | assert(socket_data);
|
---|
1059 | assert(socket->specific_data == socket_data);
|
---|
1060 | assert(header);
|
---|
1061 | assert(packet);
|
---|
1062 |
|
---|
1063 | if (!GET_TCP_HEADER_ACKNOWLEDGE(header))
|
---|
1064 | return tcp_release_and_return(packet, EINVAL);
|
---|
1065 |
|
---|
1066 | /* Process acknowledgement */
|
---|
1067 | tcp_process_acknowledgement(socket, socket_data, header);
|
---|
1068 |
|
---|
1069 | socket_data->next_incoming = ntohl(header->sequence_number); /* + 1; */
|
---|
1070 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
1071 | socket_data->state = TCP_SOCKET_ESTABLISHED;
|
---|
1072 | listening_socket = socket_cores_find(socket_data->local_sockets,
|
---|
1073 | socket_data->listening_socket_id);
|
---|
1074 | if (listening_socket) {
|
---|
1075 | listening_socket_data =
|
---|
1076 | (tcp_socket_data_t *) listening_socket->specific_data;
|
---|
1077 | assert(listening_socket_data);
|
---|
1078 |
|
---|
1079 | /* Queue the received packet */
|
---|
1080 | rc = dyn_fifo_push(&listening_socket->accepted,
|
---|
1081 | (-1 * socket->socket_id), listening_socket_data->backlog);
|
---|
1082 | if (rc == EOK) {
|
---|
1083 | /* Notify the destination socket */
|
---|
1084 | async_obsolete_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
|
---|
1085 | (sysarg_t) listening_socket->socket_id,
|
---|
1086 | socket_data->data_fragment_size, TCP_HEADER_SIZE,
|
---|
1087 | 0, (sysarg_t) socket->socket_id);
|
---|
1088 |
|
---|
1089 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1090 | return EOK;
|
---|
1091 | }
|
---|
1092 | }
|
---|
1093 | /* Send FIN */
|
---|
1094 | socket_data->state = TCP_SOCKET_FIN_WAIT_1;
|
---|
1095 |
|
---|
1096 | /* Create the notification packet */
|
---|
1097 | rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
|
---|
1098 | if (rc != EOK)
|
---|
1099 | return rc;
|
---|
1100 |
|
---|
1101 | /* Send the packet */
|
---|
1102 | rc = tcp_queue_packet(socket, socket_data, packet, 1);
|
---|
1103 | if (rc != EOK)
|
---|
1104 | return rc;
|
---|
1105 |
|
---|
1106 | /* Flush packets */
|
---|
1107 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
1108 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1109 | if (packet) {
|
---|
1110 | /* Send the packet */
|
---|
1111 | tcp_send_packets(socket_data->device_id, packet);
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | return EOK;
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | void tcp_process_acknowledgement(socket_core_t *socket,
|
---|
1118 | tcp_socket_data_t *socket_data, tcp_header_t *header)
|
---|
1119 | {
|
---|
1120 | size_t number;
|
---|
1121 | size_t length;
|
---|
1122 | packet_t *packet;
|
---|
1123 | packet_t *next;
|
---|
1124 | packet_t *acknowledged = NULL;
|
---|
1125 | uint32_t old;
|
---|
1126 |
|
---|
1127 | assert(socket);
|
---|
1128 | assert(socket_data);
|
---|
1129 | assert(socket->specific_data == socket_data);
|
---|
1130 | assert(header);
|
---|
1131 |
|
---|
1132 | if (!GET_TCP_HEADER_ACKNOWLEDGE(header))
|
---|
1133 | return;
|
---|
1134 |
|
---|
1135 | number = ntohl(header->acknowledgement_number);
|
---|
1136 |
|
---|
1137 | /* If more data acknowledged */
|
---|
1138 | if (number != socket_data->expected) {
|
---|
1139 | old = socket_data->expected;
|
---|
1140 | if (IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing,
|
---|
1141 | number)) {
|
---|
1142 | switch (socket_data->state) {
|
---|
1143 | case TCP_SOCKET_FIN_WAIT_1:
|
---|
1144 | socket_data->state = TCP_SOCKET_FIN_WAIT_2;
|
---|
1145 | break;
|
---|
1146 | case TCP_SOCKET_LAST_ACK:
|
---|
1147 | case TCP_SOCKET_CLOSING:
|
---|
1148 | /*
|
---|
1149 | * FIN acknowledged - release the socket in
|
---|
1150 | * another fibril.
|
---|
1151 | */
|
---|
1152 | tcp_prepare_timeout(tcp_release_after_timeout,
|
---|
1153 | socket, socket_data, 0,
|
---|
1154 | TCP_SOCKET_TIME_WAIT,
|
---|
1155 | NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true);
|
---|
1156 | break;
|
---|
1157 | default:
|
---|
1158 | break;
|
---|
1159 | }
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /* Update the treshold if higher than set */
|
---|
1163 | if (number + ntohs(header->window) >
|
---|
1164 | socket_data->expected + socket_data->treshold) {
|
---|
1165 | socket_data->treshold = number + ntohs(header->window) -
|
---|
1166 | socket_data->expected;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | /* Set new expected sequence number */
|
---|
1170 | socket_data->expected = number;
|
---|
1171 | socket_data->expected_count = 1;
|
---|
1172 | packet = socket_data->outgoing;
|
---|
1173 | while (pq_get_order(packet, &number, &length) == EOK) {
|
---|
1174 | if (IS_IN_INTERVAL_OVERFLOW((uint32_t) old,
|
---|
1175 | (uint32_t) (number + length),
|
---|
1176 | (uint32_t) socket_data->expected)) {
|
---|
1177 | next = pq_detach(packet);
|
---|
1178 | if (packet == socket_data->outgoing)
|
---|
1179 | socket_data->outgoing = next;
|
---|
1180 |
|
---|
1181 | /* Add to acknowledged or release */
|
---|
1182 | if (pq_add(&acknowledged, packet, 0, 0) != EOK)
|
---|
1183 | pq_release_remote(tcp_globals.net_phone,
|
---|
1184 | packet_get_id(packet));
|
---|
1185 | packet = next;
|
---|
1186 | } else if (old < socket_data->expected)
|
---|
1187 | break;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /* Release acknowledged */
|
---|
1191 | if (acknowledged) {
|
---|
1192 | pq_release_remote(tcp_globals.net_phone,
|
---|
1193 | packet_get_id(acknowledged));
|
---|
1194 | }
|
---|
1195 | return;
|
---|
1196 | /* If the same as the previous time */
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | if (number == socket_data->expected) {
|
---|
1200 | /* Increase the counter */
|
---|
1201 | socket_data->expected_count++;
|
---|
1202 | if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
|
---|
1203 | socket_data->expected_count = 1;
|
---|
1204 | /* TODO retransmit lock */
|
---|
1205 | //tcp_retransmit_packet(socket, socket_data, number);
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 | }
|
---|
1209 |
|
---|
1210 | /** Per-connection initialization
|
---|
1211 | *
|
---|
1212 | */
|
---|
1213 | void tl_connection(void)
|
---|
1214 | {
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | /** Processes the TCP message.
|
---|
1218 | *
|
---|
1219 | * @param[in] callid The message identifier.
|
---|
1220 | * @param[in] call The message parameters.
|
---|
1221 | * @param[out] answer The message answer parameters.
|
---|
1222 | * @param[out] answer_count The last parameter for the actual answer in the
|
---|
1223 | * answer parameter.
|
---|
1224 | * @return EOK on success.
|
---|
1225 | * @return ENOTSUP if the message is not known.
|
---|
1226 | *
|
---|
1227 | * @see tcp_interface.h
|
---|
1228 | * @see IS_NET_TCP_MESSAGE()
|
---|
1229 | */
|
---|
1230 | int tl_message(ipc_callid_t callid, ipc_call_t *call,
|
---|
1231 | ipc_call_t *answer, size_t *answer_count)
|
---|
1232 | {
|
---|
1233 | assert(call);
|
---|
1234 | assert(answer);
|
---|
1235 | assert(answer_count);
|
---|
1236 |
|
---|
1237 | *answer_count = 0;
|
---|
1238 | switch (IPC_GET_IMETHOD(*call)) {
|
---|
1239 | case IPC_M_CONNECT_TO_ME:
|
---|
1240 | return tcp_process_client_messages(callid, *call);
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | return ENOTSUP;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
|
---|
1247 | {
|
---|
1248 | assert(socket_data);
|
---|
1249 |
|
---|
1250 | bzero(socket_data, sizeof(*socket_data));
|
---|
1251 | socket_data->state = TCP_SOCKET_INITIAL;
|
---|
1252 | socket_data->device_id = DEVICE_INVALID_ID;
|
---|
1253 | socket_data->window = NET_DEFAULT_TCP_WINDOW;
|
---|
1254 | socket_data->treshold = socket_data->window;
|
---|
1255 | socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER;
|
---|
1256 | socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT;
|
---|
1257 | socket_data->acknowledged = socket_data->last_outgoing;
|
---|
1258 | socket_data->next_outgoing = socket_data->last_outgoing + 1;
|
---|
1259 | socket_data->expected = socket_data->next_outgoing;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
|
---|
1263 | {
|
---|
1264 | assert(socket_data);
|
---|
1265 |
|
---|
1266 | tcp_refresh_socket_data(socket_data);
|
---|
1267 | fibril_mutex_initialize(&socket_data->operation.mutex);
|
---|
1268 | fibril_condvar_initialize(&socket_data->operation.condvar);
|
---|
1269 | socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
|
---|
1273 | {
|
---|
1274 | int res;
|
---|
1275 | socket_cores_t local_sockets;
|
---|
1276 | int app_phone = IPC_GET_PHONE(call);
|
---|
1277 | struct sockaddr *addr;
|
---|
1278 | int socket_id;
|
---|
1279 | size_t addrlen;
|
---|
1280 | size_t size;
|
---|
1281 | fibril_rwlock_t lock;
|
---|
1282 | ipc_call_t answer;
|
---|
1283 | size_t answer_count;
|
---|
1284 | tcp_socket_data_t *socket_data;
|
---|
1285 | socket_core_t *socket;
|
---|
1286 | packet_dimension_t *packet_dimension;
|
---|
1287 |
|
---|
1288 | /*
|
---|
1289 | * Accept the connection
|
---|
1290 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
1291 | */
|
---|
1292 | res = EOK;
|
---|
1293 | answer_count = 0;
|
---|
1294 |
|
---|
1295 | socket_cores_initialize(&local_sockets);
|
---|
1296 | fibril_rwlock_initialize(&lock);
|
---|
1297 |
|
---|
1298 | while (true) {
|
---|
1299 |
|
---|
1300 | /* Answer the call */
|
---|
1301 | answer_call(callid, res, &answer, answer_count);
|
---|
1302 | /* Refresh data */
|
---|
1303 | refresh_answer(&answer, &answer_count);
|
---|
1304 | /* Get the next call */
|
---|
1305 | callid = async_get_call(&call);
|
---|
1306 |
|
---|
1307 | if (!IPC_GET_IMETHOD(call)) {
|
---|
1308 | res = EHANGUP;
|
---|
1309 | break;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /* Process the call */
|
---|
1313 | switch (IPC_GET_IMETHOD(call)) {
|
---|
1314 | case NET_SOCKET:
|
---|
1315 | socket_data =
|
---|
1316 | (tcp_socket_data_t *) malloc(sizeof(*socket_data));
|
---|
1317 | if (!socket_data) {
|
---|
1318 | res = ENOMEM;
|
---|
1319 | break;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | tcp_initialize_socket_data(socket_data);
|
---|
1323 | socket_data->local_lock = &lock;
|
---|
1324 | socket_data->local_sockets = &local_sockets;
|
---|
1325 | fibril_rwlock_write_lock(&lock);
|
---|
1326 | socket_id = SOCKET_GET_SOCKET_ID(call);
|
---|
1327 | res = socket_create(&local_sockets, app_phone,
|
---|
1328 | socket_data, &socket_id);
|
---|
1329 | SOCKET_SET_SOCKET_ID(answer, socket_id);
|
---|
1330 | fibril_rwlock_write_unlock(&lock);
|
---|
1331 | if (res != EOK) {
|
---|
1332 | free(socket_data);
|
---|
1333 | break;
|
---|
1334 | }
|
---|
1335 | if (tl_get_ip_packet_dimension(tcp_globals.ip_phone,
|
---|
1336 | &tcp_globals.dimensions, DEVICE_INVALID_ID,
|
---|
1337 | &packet_dimension) == EOK) {
|
---|
1338 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
|
---|
1339 | ((packet_dimension->content <
|
---|
1340 | socket_data->data_fragment_size) ?
|
---|
1341 | packet_dimension->content :
|
---|
1342 | socket_data->data_fragment_size));
|
---|
1343 | }
|
---|
1344 | // SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
|
---|
1345 | SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
|
---|
1346 | answer_count = 3;
|
---|
1347 | break;
|
---|
1348 |
|
---|
1349 | case NET_SOCKET_BIND:
|
---|
1350 | res = async_data_write_accept((void **) &addr, false,
|
---|
1351 | 0, 0, 0, &addrlen);
|
---|
1352 | if (res != EOK)
|
---|
1353 | break;
|
---|
1354 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1355 | fibril_rwlock_write_lock(&lock);
|
---|
1356 | res = socket_bind(&local_sockets, &tcp_globals.sockets,
|
---|
1357 | SOCKET_GET_SOCKET_ID(call), addr, addrlen,
|
---|
1358 | TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
1359 | tcp_globals.last_used_port);
|
---|
1360 | if (res == EOK) {
|
---|
1361 | socket = socket_cores_find(&local_sockets,
|
---|
1362 | SOCKET_GET_SOCKET_ID(call));
|
---|
1363 | if (socket) {
|
---|
1364 | socket_data = (tcp_socket_data_t *)
|
---|
1365 | socket->specific_data;
|
---|
1366 | assert(socket_data);
|
---|
1367 | socket_data->state = TCP_SOCKET_LISTEN;
|
---|
1368 | }
|
---|
1369 | }
|
---|
1370 | fibril_rwlock_write_unlock(&lock);
|
---|
1371 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1372 | free(addr);
|
---|
1373 | break;
|
---|
1374 |
|
---|
1375 | case NET_SOCKET_LISTEN:
|
---|
1376 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1377 | // fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1378 | fibril_rwlock_write_lock(&lock);
|
---|
1379 | res = tcp_listen_message(&local_sockets,
|
---|
1380 | SOCKET_GET_SOCKET_ID(call),
|
---|
1381 | SOCKET_GET_BACKLOG(call));
|
---|
1382 | fibril_rwlock_write_unlock(&lock);
|
---|
1383 | // fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1384 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1385 | break;
|
---|
1386 |
|
---|
1387 | case NET_SOCKET_CONNECT:
|
---|
1388 | res = async_data_write_accept((void **) &addr, false,
|
---|
1389 | 0, 0, 0, &addrlen);
|
---|
1390 | if (res != EOK)
|
---|
1391 | break;
|
---|
1392 | /*
|
---|
1393 | * The global lock may be released in the
|
---|
1394 | * tcp_connect_message() function.
|
---|
1395 | */
|
---|
1396 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1397 | fibril_rwlock_write_lock(&lock);
|
---|
1398 | res = tcp_connect_message(&local_sockets,
|
---|
1399 | SOCKET_GET_SOCKET_ID(call), addr, addrlen);
|
---|
1400 | if (res != EOK) {
|
---|
1401 | fibril_rwlock_write_unlock(&lock);
|
---|
1402 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1403 | free(addr);
|
---|
1404 | }
|
---|
1405 | break;
|
---|
1406 |
|
---|
1407 | case NET_SOCKET_ACCEPT:
|
---|
1408 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1409 | fibril_rwlock_write_lock(&lock);
|
---|
1410 | res = tcp_accept_message(&local_sockets,
|
---|
1411 | SOCKET_GET_SOCKET_ID(call),
|
---|
1412 | SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
|
---|
1413 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
|
---|
1414 | fibril_rwlock_write_unlock(&lock);
|
---|
1415 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1416 | if (res > 0) {
|
---|
1417 | SOCKET_SET_SOCKET_ID(answer, res);
|
---|
1418 | SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
|
---|
1419 | answer_count = 3;
|
---|
1420 | }
|
---|
1421 | break;
|
---|
1422 |
|
---|
1423 | case NET_SOCKET_SEND:
|
---|
1424 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1425 | fibril_rwlock_write_lock(&lock);
|
---|
1426 | res = tcp_send_message(&local_sockets,
|
---|
1427 | SOCKET_GET_SOCKET_ID(call),
|
---|
1428 | SOCKET_GET_DATA_FRAGMENTS(call), &size,
|
---|
1429 | SOCKET_GET_FLAGS(call));
|
---|
1430 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
|
---|
1431 | if (res != EOK) {
|
---|
1432 | fibril_rwlock_write_unlock(&lock);
|
---|
1433 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1434 | } else {
|
---|
1435 | answer_count = 2;
|
---|
1436 | }
|
---|
1437 | break;
|
---|
1438 |
|
---|
1439 | case NET_SOCKET_SENDTO:
|
---|
1440 | res = async_data_write_accept((void **) &addr, false,
|
---|
1441 | 0, 0, 0, &addrlen);
|
---|
1442 | if (res != EOK)
|
---|
1443 | break;
|
---|
1444 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1445 | fibril_rwlock_write_lock(&lock);
|
---|
1446 | res = tcp_send_message(&local_sockets,
|
---|
1447 | SOCKET_GET_SOCKET_ID(call),
|
---|
1448 | SOCKET_GET_DATA_FRAGMENTS(call), &size,
|
---|
1449 | SOCKET_GET_FLAGS(call));
|
---|
1450 | SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
|
---|
1451 | if (res != EOK) {
|
---|
1452 | fibril_rwlock_write_unlock(&lock);
|
---|
1453 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1454 | } else {
|
---|
1455 | answer_count = 2;
|
---|
1456 | }
|
---|
1457 | free(addr);
|
---|
1458 | break;
|
---|
1459 |
|
---|
1460 | case NET_SOCKET_RECV:
|
---|
1461 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1462 | fibril_rwlock_write_lock(&lock);
|
---|
1463 | res = tcp_recvfrom_message(&local_sockets,
|
---|
1464 | SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
|
---|
1465 | NULL);
|
---|
1466 | fibril_rwlock_write_unlock(&lock);
|
---|
1467 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1468 | if (res > 0) {
|
---|
1469 | SOCKET_SET_READ_DATA_LENGTH(answer, res);
|
---|
1470 | answer_count = 1;
|
---|
1471 | res = EOK;
|
---|
1472 | }
|
---|
1473 | break;
|
---|
1474 |
|
---|
1475 | case NET_SOCKET_RECVFROM:
|
---|
1476 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1477 | fibril_rwlock_write_lock(&lock);
|
---|
1478 | res = tcp_recvfrom_message(&local_sockets,
|
---|
1479 | SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
|
---|
1480 | &addrlen);
|
---|
1481 | fibril_rwlock_write_unlock(&lock);
|
---|
1482 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1483 | if (res > 0) {
|
---|
1484 | SOCKET_SET_READ_DATA_LENGTH(answer, res);
|
---|
1485 | SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
|
---|
1486 | answer_count = 3;
|
---|
1487 | res = EOK;
|
---|
1488 | }
|
---|
1489 | break;
|
---|
1490 |
|
---|
1491 | case NET_SOCKET_CLOSE:
|
---|
1492 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1493 | fibril_rwlock_write_lock(&lock);
|
---|
1494 | res = tcp_close_message(&local_sockets,
|
---|
1495 | SOCKET_GET_SOCKET_ID(call));
|
---|
1496 | if (res != EOK) {
|
---|
1497 | fibril_rwlock_write_unlock(&lock);
|
---|
1498 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1499 | }
|
---|
1500 | break;
|
---|
1501 |
|
---|
1502 | case NET_SOCKET_GETSOCKOPT:
|
---|
1503 | case NET_SOCKET_SETSOCKOPT:
|
---|
1504 | default:
|
---|
1505 | res = ENOTSUP;
|
---|
1506 | break;
|
---|
1507 | }
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | /* Release the application phone */
|
---|
1511 | async_obsolete_hangup(app_phone);
|
---|
1512 |
|
---|
1513 | printf("release\n");
|
---|
1514 | /* Release all local sockets */
|
---|
1515 | socket_cores_release(tcp_globals.net_phone, &local_sockets,
|
---|
1516 | &tcp_globals.sockets, tcp_free_socket_data);
|
---|
1517 |
|
---|
1518 | return EOK;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | int tcp_timeout(void *data)
|
---|
1522 | {
|
---|
1523 | tcp_timeout_t *timeout = data;
|
---|
1524 | int keep_write_lock = false;
|
---|
1525 | socket_core_t *socket;
|
---|
1526 | tcp_socket_data_t *socket_data;
|
---|
1527 |
|
---|
1528 | assert(timeout);
|
---|
1529 |
|
---|
1530 | /* Sleep the given timeout */
|
---|
1531 | async_usleep(timeout->timeout);
|
---|
1532 | /* Lock the globals */
|
---|
1533 | if (timeout->globals_read_only)
|
---|
1534 | fibril_rwlock_read_lock(&tcp_globals.lock);
|
---|
1535 | else
|
---|
1536 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1537 |
|
---|
1538 | /* Find the pending operation socket */
|
---|
1539 | socket = socket_port_find(&tcp_globals.sockets, timeout->port,
|
---|
1540 | timeout->key, timeout->key_length);
|
---|
1541 | if (!socket || (socket->socket_id != timeout->socket_id))
|
---|
1542 | goto out;
|
---|
1543 |
|
---|
1544 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
1545 | assert(socket_data);
|
---|
1546 | if (socket_data->local_sockets != timeout->local_sockets)
|
---|
1547 | goto out;
|
---|
1548 |
|
---|
1549 | fibril_rwlock_write_lock(socket_data->local_lock);
|
---|
1550 | if (timeout->sequence_number) {
|
---|
1551 | /* Increase the timeout counter */
|
---|
1552 | socket_data->timeout_count++;
|
---|
1553 | if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
|
---|
1554 | /* TODO release as connection lost */
|
---|
1555 | //tcp_refresh_socket_data(socket_data);
|
---|
1556 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1557 | } else {
|
---|
1558 | /* Retransmit */
|
---|
1559 | // tcp_retransmit_packet(socket,
|
---|
1560 | // socket_data, timeout->sequence_number);
|
---|
1561 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1562 | }
|
---|
1563 | } else {
|
---|
1564 | fibril_mutex_lock(&socket_data->operation.mutex);
|
---|
1565 | /* Set the timeout operation result if state not changed */
|
---|
1566 | if (socket_data->state == timeout->state) {
|
---|
1567 | socket_data->operation.result = ETIMEOUT;
|
---|
1568 |
|
---|
1569 | /* Notify the main fibril */
|
---|
1570 | fibril_condvar_signal(&socket_data->operation.condvar);
|
---|
1571 |
|
---|
1572 | /* Keep the global write lock */
|
---|
1573 | keep_write_lock = true;
|
---|
1574 | } else {
|
---|
1575 | /*
|
---|
1576 | * Operation is ok, do nothing.
|
---|
1577 | * Unlocking from now on, so the unlocking
|
---|
1578 | * order does not matter.
|
---|
1579 | */
|
---|
1580 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1581 | }
|
---|
1582 | fibril_mutex_unlock(&socket_data->operation.mutex);
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | out:
|
---|
1586 | /* Unlock only if no socket */
|
---|
1587 | if (timeout->globals_read_only)
|
---|
1588 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
1589 | else if (!keep_write_lock)
|
---|
1590 | /* Release if not desired */
|
---|
1591 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1592 |
|
---|
1593 | /* Release the timeout structure */
|
---|
1594 | free(timeout);
|
---|
1595 | return EOK;
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | int tcp_release_after_timeout(void *data)
|
---|
1599 | {
|
---|
1600 | tcp_timeout_t *timeout = data;
|
---|
1601 | socket_core_t *socket;
|
---|
1602 | tcp_socket_data_t *socket_data;
|
---|
1603 | fibril_rwlock_t *local_lock;
|
---|
1604 |
|
---|
1605 | assert(timeout);
|
---|
1606 |
|
---|
1607 | /* Sleep the given timeout */
|
---|
1608 | async_usleep(timeout->timeout);
|
---|
1609 |
|
---|
1610 | /* Lock the globals */
|
---|
1611 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1612 |
|
---|
1613 | /* Find the pending operation socket */
|
---|
1614 | socket = socket_port_find(&tcp_globals.sockets, timeout->port,
|
---|
1615 | timeout->key, timeout->key_length);
|
---|
1616 |
|
---|
1617 | if (socket && (socket->socket_id == timeout->socket_id)) {
|
---|
1618 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
1619 | assert(socket_data);
|
---|
1620 | if (socket_data->local_sockets == timeout->local_sockets) {
|
---|
1621 | local_lock = socket_data->local_lock;
|
---|
1622 | fibril_rwlock_write_lock(local_lock);
|
---|
1623 | socket_destroy(tcp_globals.net_phone,
|
---|
1624 | timeout->socket_id, timeout->local_sockets,
|
---|
1625 | &tcp_globals.sockets, tcp_free_socket_data);
|
---|
1626 | fibril_rwlock_write_unlock(local_lock);
|
---|
1627 | }
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | /* Unlock the globals */
|
---|
1631 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1632 |
|
---|
1633 | /* Release the timeout structure */
|
---|
1634 | free(timeout);
|
---|
1635 |
|
---|
1636 | return EOK;
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 | void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t *
|
---|
1640 | socket_data, size_t sequence_number)
|
---|
1641 | {
|
---|
1642 | packet_t *packet;
|
---|
1643 | packet_t *copy;
|
---|
1644 | size_t data_length;
|
---|
1645 |
|
---|
1646 | assert(socket);
|
---|
1647 | assert(socket_data);
|
---|
1648 | assert(socket->specific_data == socket_data);
|
---|
1649 |
|
---|
1650 | /* Sent packet? */
|
---|
1651 | packet = pq_find(socket_data->outgoing, sequence_number);
|
---|
1652 | printf("retransmit %d\n", packet_get_id(packet));
|
---|
1653 | if (packet) {
|
---|
1654 | pq_get_order(packet, NULL, &data_length);
|
---|
1655 | copy = tcp_prepare_copy(socket, socket_data, packet,
|
---|
1656 | data_length, sequence_number);
|
---|
1657 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1658 | // printf("r send %d\n", packet_get_id(packet));
|
---|
1659 | if (copy)
|
---|
1660 | tcp_send_packets(socket_data->device_id, copy);
|
---|
1661 | } else {
|
---|
1662 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1663 | }
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,
|
---|
1667 | int backlog)
|
---|
1668 | {
|
---|
1669 | socket_core_t *socket;
|
---|
1670 | tcp_socket_data_t *socket_data;
|
---|
1671 |
|
---|
1672 | assert(local_sockets);
|
---|
1673 |
|
---|
1674 | if (backlog < 0)
|
---|
1675 | return EINVAL;
|
---|
1676 |
|
---|
1677 | /* Find the socket */
|
---|
1678 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
1679 | if (!socket)
|
---|
1680 | return ENOTSOCK;
|
---|
1681 |
|
---|
1682 | /* Get the socket specific data */
|
---|
1683 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
1684 | assert(socket_data);
|
---|
1685 |
|
---|
1686 | /* Set the backlog */
|
---|
1687 | socket_data->backlog = backlog;
|
---|
1688 |
|
---|
1689 | return EOK;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
|
---|
1693 | struct sockaddr *addr, socklen_t addrlen)
|
---|
1694 | {
|
---|
1695 | socket_core_t *socket;
|
---|
1696 | int rc;
|
---|
1697 |
|
---|
1698 | assert(local_sockets);
|
---|
1699 | assert(addr);
|
---|
1700 | assert(addrlen > 0);
|
---|
1701 |
|
---|
1702 | /* Find the socket */
|
---|
1703 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
1704 | if (!socket)
|
---|
1705 | return ENOTSOCK;
|
---|
1706 |
|
---|
1707 | rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
|
---|
1708 | if (rc != EOK) {
|
---|
1709 | tcp_free_socket_data(socket);
|
---|
1710 | /* Unbind if bound */
|
---|
1711 | if (socket->port > 0) {
|
---|
1712 | socket_ports_exclude(&tcp_globals.sockets,
|
---|
1713 | socket->port, free);
|
---|
1714 | socket->port = 0;
|
---|
1715 | }
|
---|
1716 | }
|
---|
1717 | return rc;
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
|
---|
1721 | struct sockaddr *addr, socklen_t addrlen)
|
---|
1722 | {
|
---|
1723 | tcp_socket_data_t *socket_data;
|
---|
1724 | packet_t *packet;
|
---|
1725 | int rc;
|
---|
1726 |
|
---|
1727 | assert(socket);
|
---|
1728 | assert(addr);
|
---|
1729 | assert(addrlen > 0);
|
---|
1730 |
|
---|
1731 | /* Get the socket specific data */
|
---|
1732 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
1733 | assert(socket_data);
|
---|
1734 | assert(socket->specific_data == socket_data);
|
---|
1735 | if ((socket_data->state != TCP_SOCKET_INITIAL) &&
|
---|
1736 | ((socket_data->state != TCP_SOCKET_LISTEN) ||
|
---|
1737 | (socket->port <= 0)))
|
---|
1738 | return EINVAL;
|
---|
1739 |
|
---|
1740 | /* Get the destination port */
|
---|
1741 | rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
|
---|
1742 | if (rc != EOK)
|
---|
1743 | return rc;
|
---|
1744 |
|
---|
1745 | if (socket->port <= 0) {
|
---|
1746 | /* Try to find a free port */
|
---|
1747 | rc = socket_bind_free_port(&tcp_globals.sockets, socket,
|
---|
1748 | TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
|
---|
1749 | tcp_globals.last_used_port);
|
---|
1750 | if (rc != EOK)
|
---|
1751 | return rc;
|
---|
1752 | /* Set the next port as the search starting port number */
|
---|
1753 | tcp_globals.last_used_port = socket->port;
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
|
---|
1757 | addr, addrlen, &socket_data->device_id,
|
---|
1758 | &socket_data->pseudo_header, &socket_data->headerlen);
|
---|
1759 | if (rc != EOK)
|
---|
1760 | return rc;
|
---|
1761 |
|
---|
1762 | /* Create the notification packet */
|
---|
1763 | rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
|
---|
1764 | if (rc != EOK)
|
---|
1765 | return rc;
|
---|
1766 |
|
---|
1767 | /* Unlock the globals and wait for an operation */
|
---|
1768 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
1769 |
|
---|
1770 | socket_data->addr = addr;
|
---|
1771 | socket_data->addrlen = addrlen;
|
---|
1772 |
|
---|
1773 | /* Send the packet */
|
---|
1774 |
|
---|
1775 | if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
|
---|
1776 | ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0,
|
---|
1777 | TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) !=
|
---|
1778 | EOK)) {
|
---|
1779 | socket_data->addr = NULL;
|
---|
1780 | socket_data->addrlen = 0;
|
---|
1781 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
1782 | } else {
|
---|
1783 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
1784 | if (packet) {
|
---|
1785 | fibril_mutex_lock(&socket_data->operation.mutex);
|
---|
1786 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
1787 |
|
---|
1788 | socket_data->state = TCP_SOCKET_SYN_SENT;
|
---|
1789 |
|
---|
1790 | /* Send the packet */
|
---|
1791 | printf("connecting %d\n", packet_get_id(packet));
|
---|
1792 | tcp_send_packets(socket_data->device_id, packet);
|
---|
1793 |
|
---|
1794 | /* Wait for a reply */
|
---|
1795 | fibril_condvar_wait(&socket_data->operation.condvar,
|
---|
1796 | &socket_data->operation.mutex);
|
---|
1797 | rc = socket_data->operation.result;
|
---|
1798 | if (rc != EOK) {
|
---|
1799 | socket_data->addr = NULL;
|
---|
1800 | socket_data->addrlen = 0;
|
---|
1801 | }
|
---|
1802 | } else {
|
---|
1803 | socket_data->addr = NULL;
|
---|
1804 | socket_data->addrlen = 0;
|
---|
1805 | rc = EINTR;
|
---|
1806 | }
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | fibril_mutex_unlock(&socket_data->operation.mutex);
|
---|
1810 | return rc;
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | int tcp_queue_prepare_packet(socket_core_t *socket,
|
---|
1814 | tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length)
|
---|
1815 | {
|
---|
1816 | tcp_header_t *header;
|
---|
1817 | int rc;
|
---|
1818 |
|
---|
1819 | assert(socket);
|
---|
1820 | assert(socket_data);
|
---|
1821 | assert(socket->specific_data == socket_data);
|
---|
1822 |
|
---|
1823 | /* Get TCP header */
|
---|
1824 | header = (tcp_header_t *) packet_get_data(packet);
|
---|
1825 | if (!header)
|
---|
1826 | return NO_DATA;
|
---|
1827 |
|
---|
1828 | header->destination_port = htons(socket_data->dest_port);
|
---|
1829 | header->source_port = htons(socket->port);
|
---|
1830 | header->sequence_number = htonl(socket_data->next_outgoing);
|
---|
1831 |
|
---|
1832 | rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
|
---|
1833 | socket_data->addrlen);
|
---|
1834 | if (rc != EOK)
|
---|
1835 | return tcp_release_and_return(packet, EINVAL);
|
---|
1836 |
|
---|
1837 | /* Remember the outgoing FIN */
|
---|
1838 | if (GET_TCP_HEADER_FINALIZE(header))
|
---|
1839 | socket_data->fin_outgoing = socket_data->next_outgoing;
|
---|
1840 |
|
---|
1841 | return EOK;
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data,
|
---|
1845 | packet_t *packet, size_t data_length)
|
---|
1846 | {
|
---|
1847 | int rc;
|
---|
1848 |
|
---|
1849 | assert(socket);
|
---|
1850 | assert(socket_data);
|
---|
1851 | assert(socket->specific_data == socket_data);
|
---|
1852 |
|
---|
1853 | rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
|
---|
1854 | if (rc != EOK)
|
---|
1855 | return rc;
|
---|
1856 |
|
---|
1857 | rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing,
|
---|
1858 | data_length);
|
---|
1859 | if (rc != EOK)
|
---|
1860 | return tcp_release_and_return(packet, rc);
|
---|
1861 |
|
---|
1862 | socket_data->next_outgoing += data_length;
|
---|
1863 | return EOK;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t *
|
---|
1867 | socket_data)
|
---|
1868 | {
|
---|
1869 | packet_t *packet;
|
---|
1870 | packet_t *copy;
|
---|
1871 | packet_t *sending = NULL;
|
---|
1872 | packet_t *previous = NULL;
|
---|
1873 | size_t data_length;
|
---|
1874 | int rc;
|
---|
1875 |
|
---|
1876 | assert(socket);
|
---|
1877 | assert(socket_data);
|
---|
1878 | assert(socket->specific_data == socket_data);
|
---|
1879 |
|
---|
1880 | packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
|
---|
1881 | while (packet) {
|
---|
1882 | pq_get_order(packet, NULL, &data_length);
|
---|
1883 |
|
---|
1884 | /*
|
---|
1885 | * Send only if fits into the window, respecting the possible
|
---|
1886 | * overflow.
|
---|
1887 | */
|
---|
1888 | if (!IS_IN_INTERVAL_OVERFLOW(
|
---|
1889 | (uint32_t) socket_data->last_outgoing,
|
---|
1890 | (uint32_t) (socket_data->last_outgoing + data_length),
|
---|
1891 | (uint32_t) (socket_data->expected + socket_data->treshold)))
|
---|
1892 | break;
|
---|
1893 |
|
---|
1894 | copy = tcp_prepare_copy(socket, socket_data, packet,
|
---|
1895 | data_length, socket_data->last_outgoing + 1);
|
---|
1896 | if (!copy)
|
---|
1897 | return sending;
|
---|
1898 |
|
---|
1899 | if (!sending) {
|
---|
1900 | sending = copy;
|
---|
1901 | } else {
|
---|
1902 | rc = pq_insert_after(previous, copy);
|
---|
1903 | if (rc != EOK) {
|
---|
1904 | pq_release_remote(tcp_globals.net_phone,
|
---|
1905 | packet_get_id(copy));
|
---|
1906 | return sending;
|
---|
1907 | }
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | previous = copy;
|
---|
1911 | packet = pq_next(packet);
|
---|
1912 |
|
---|
1913 | /* Overflow occurred? */
|
---|
1914 | if (!packet &&
|
---|
1915 | (socket_data->last_outgoing > socket_data->next_outgoing)) {
|
---|
1916 | printf("gpts overflow\n");
|
---|
1917 | /* Continue from the beginning */
|
---|
1918 | packet = socket_data->outgoing;
|
---|
1919 | }
|
---|
1920 | socket_data->last_outgoing += data_length;
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 | return sending;
|
---|
1924 | }
|
---|
1925 |
|
---|
1926 | packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t *
|
---|
1927 | socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
|
---|
1928 | {
|
---|
1929 | tcp_header_t *header;
|
---|
1930 | uint32_t checksum;
|
---|
1931 | int rc;
|
---|
1932 |
|
---|
1933 | assert(socket);
|
---|
1934 | assert(socket_data);
|
---|
1935 | assert(socket->specific_data == socket_data);
|
---|
1936 |
|
---|
1937 | /* Adjust the pseudo header */
|
---|
1938 | rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
|
---|
1939 | socket_data->headerlen, packet_get_data_length(packet));
|
---|
1940 | if (rc != EOK) {
|
---|
1941 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
1942 | return NULL;
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 | /* Get the header */
|
---|
1946 | header = (tcp_header_t *) packet_get_data(packet);
|
---|
1947 | if (!header) {
|
---|
1948 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
1949 | return NULL;
|
---|
1950 | }
|
---|
1951 | assert(ntohl(header->sequence_number) == sequence_number);
|
---|
1952 |
|
---|
1953 | /* Adjust the header */
|
---|
1954 | if (socket_data->next_incoming) {
|
---|
1955 | header->acknowledgement_number =
|
---|
1956 | htonl(socket_data->next_incoming);
|
---|
1957 | SET_TCP_HEADER_ACKNOWLEDGE(header, 1);
|
---|
1958 | }
|
---|
1959 | header->window = htons(socket_data->window);
|
---|
1960 |
|
---|
1961 | /* Checksum */
|
---|
1962 | header->checksum = 0;
|
---|
1963 | checksum = compute_checksum(0, socket_data->pseudo_header,
|
---|
1964 | socket_data->headerlen);
|
---|
1965 | checksum = compute_checksum(checksum,
|
---|
1966 | (uint8_t *) packet_get_data(packet),
|
---|
1967 | packet_get_data_length(packet));
|
---|
1968 | header->checksum = htons(flip_checksum(compact_checksum(checksum)));
|
---|
1969 |
|
---|
1970 | /* Prepare the packet */
|
---|
1971 | rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
|
---|
1972 | if (rc != EOK) {
|
---|
1973 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
1974 | return NULL;
|
---|
1975 | }
|
---|
1976 |
|
---|
1977 | rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
|
---|
1978 | sequence_number, socket_data->state, socket_data->timeout, true);
|
---|
1979 | if (rc != EOK) {
|
---|
1980 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
1981 | return NULL;
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 | return packet;
|
---|
1985 | }
|
---|
1986 |
|
---|
1987 | packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t *
|
---|
1988 | socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
|
---|
1989 | {
|
---|
1990 | packet_t *copy;
|
---|
1991 |
|
---|
1992 | assert(socket);
|
---|
1993 | assert(socket_data);
|
---|
1994 | assert(socket->specific_data == socket_data);
|
---|
1995 |
|
---|
1996 | /* Make a copy of the packet */
|
---|
1997 | copy = packet_get_copy(tcp_globals.net_phone, packet);
|
---|
1998 | if (!copy)
|
---|
1999 | return NULL;
|
---|
2000 |
|
---|
2001 | return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
|
---|
2002 | sequence_number);
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 | void tcp_send_packets(device_id_t device_id, packet_t *packet)
|
---|
2006 | {
|
---|
2007 | packet_t *next;
|
---|
2008 |
|
---|
2009 | while (packet) {
|
---|
2010 | next = pq_detach(packet);
|
---|
2011 | ip_send_msg(tcp_globals.ip_phone, device_id, packet,
|
---|
2012 | SERVICE_TCP, 0);
|
---|
2013 | packet = next;
|
---|
2014 | }
|
---|
2015 | }
|
---|
2016 |
|
---|
2017 | void tcp_prepare_operation_header(socket_core_t *socket,
|
---|
2018 | tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
|
---|
2019 | int finalize)
|
---|
2020 | {
|
---|
2021 | assert(socket);
|
---|
2022 | assert(socket_data);
|
---|
2023 | assert(socket->specific_data == socket_data);
|
---|
2024 | assert(header);
|
---|
2025 |
|
---|
2026 | bzero(header, sizeof(*header));
|
---|
2027 | header->source_port = htons(socket->port);
|
---|
2028 | header->source_port = htons(socket_data->dest_port);
|
---|
2029 | SET_TCP_HEADER_LENGTH(header,
|
---|
2030 | TCP_COMPUTE_HEADER_LENGTH(sizeof(*header)));
|
---|
2031 | SET_TCP_HEADER_SYNCHRONIZE(header, synchronize);
|
---|
2032 | SET_TCP_HEADER_FINALIZE(header, finalize);
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
|
---|
2036 | socket_core_t *socket, tcp_socket_data_t *socket_data,
|
---|
2037 | size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
|
---|
2038 | int globals_read_only)
|
---|
2039 | {
|
---|
2040 | tcp_timeout_t *operation_timeout;
|
---|
2041 | fid_t fibril;
|
---|
2042 |
|
---|
2043 | assert(socket);
|
---|
2044 | assert(socket_data);
|
---|
2045 | assert(socket->specific_data == socket_data);
|
---|
2046 |
|
---|
2047 | /* Prepare the timeout with key bundle structure */
|
---|
2048 | operation_timeout = malloc(sizeof(*operation_timeout) +
|
---|
2049 | socket->key_length + 1);
|
---|
2050 | if (!operation_timeout)
|
---|
2051 | return ENOMEM;
|
---|
2052 |
|
---|
2053 | bzero(operation_timeout, sizeof(*operation_timeout));
|
---|
2054 | operation_timeout->globals_read_only = globals_read_only;
|
---|
2055 | operation_timeout->port = socket->port;
|
---|
2056 | operation_timeout->local_sockets = socket_data->local_sockets;
|
---|
2057 | operation_timeout->socket_id = socket->socket_id;
|
---|
2058 | operation_timeout->timeout = timeout;
|
---|
2059 | operation_timeout->sequence_number = sequence_number;
|
---|
2060 | operation_timeout->state = state;
|
---|
2061 |
|
---|
2062 | /* Copy the key */
|
---|
2063 | operation_timeout->key = ((uint8_t *) operation_timeout) +
|
---|
2064 | sizeof(*operation_timeout);
|
---|
2065 | operation_timeout->key_length = socket->key_length;
|
---|
2066 | memcpy(operation_timeout->key, socket->key, socket->key_length);
|
---|
2067 | operation_timeout->key[operation_timeout->key_length] = '\0';
|
---|
2068 |
|
---|
2069 | /* Prepare the timeouting thread */
|
---|
2070 | fibril = fibril_create(timeout_function, operation_timeout);
|
---|
2071 | if (!fibril) {
|
---|
2072 | free(operation_timeout);
|
---|
2073 | return ENOMEM;
|
---|
2074 | }
|
---|
2075 |
|
---|
2076 | // fibril_mutex_lock(&socket_data->operation.mutex);
|
---|
2077 | /* Start the timeout fibril */
|
---|
2078 | fibril_add_ready(fibril);
|
---|
2079 | //socket_data->state = state;
|
---|
2080 | return EOK;
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 | int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
|
---|
2084 | int flags, size_t *addrlen)
|
---|
2085 | {
|
---|
2086 | socket_core_t *socket;
|
---|
2087 | tcp_socket_data_t *socket_data;
|
---|
2088 | int packet_id;
|
---|
2089 | packet_t *packet;
|
---|
2090 | size_t length;
|
---|
2091 | int rc;
|
---|
2092 |
|
---|
2093 | assert(local_sockets);
|
---|
2094 |
|
---|
2095 | /* Find the socket */
|
---|
2096 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
2097 | if (!socket)
|
---|
2098 | return ENOTSOCK;
|
---|
2099 |
|
---|
2100 | /* Get the socket specific data */
|
---|
2101 | if (!socket->specific_data)
|
---|
2102 | return NO_DATA;
|
---|
2103 |
|
---|
2104 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
2105 |
|
---|
2106 | /* Check state */
|
---|
2107 | if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
|
---|
2108 | (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
|
---|
2109 | return ENOTCONN;
|
---|
2110 |
|
---|
2111 | /* Send the source address if desired */
|
---|
2112 | if (addrlen) {
|
---|
2113 | rc = data_reply(socket_data->addr, socket_data->addrlen);
|
---|
2114 | if (rc != EOK)
|
---|
2115 | return rc;
|
---|
2116 | *addrlen = socket_data->addrlen;
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 | /* Get the next received packet */
|
---|
2120 | packet_id = dyn_fifo_value(&socket->received);
|
---|
2121 | if (packet_id < 0)
|
---|
2122 | return NO_DATA;
|
---|
2123 |
|
---|
2124 | rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
|
---|
2125 | if (rc != EOK)
|
---|
2126 | return rc;
|
---|
2127 |
|
---|
2128 | /* Reply the packets */
|
---|
2129 | rc = socket_reply_packets(packet, &length);
|
---|
2130 | if (rc != EOK)
|
---|
2131 | return rc;
|
---|
2132 |
|
---|
2133 | /* Release the packet */
|
---|
2134 | dyn_fifo_pop(&socket->received);
|
---|
2135 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
2136 |
|
---|
2137 | /* Return the total length */
|
---|
2138 | return (int) length;
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | int tcp_send_message(socket_cores_t *local_sockets, int socket_id,
|
---|
2142 | int fragments, size_t *data_fragment_size, int flags)
|
---|
2143 | {
|
---|
2144 | socket_core_t *socket;
|
---|
2145 | tcp_socket_data_t *socket_data;
|
---|
2146 | packet_dimension_t *packet_dimension;
|
---|
2147 | packet_t *packet;
|
---|
2148 | size_t total_length;
|
---|
2149 | tcp_header_t *header;
|
---|
2150 | int index;
|
---|
2151 | int result;
|
---|
2152 | int rc;
|
---|
2153 |
|
---|
2154 | assert(local_sockets);
|
---|
2155 | assert(data_fragment_size);
|
---|
2156 |
|
---|
2157 | /* Find the socket */
|
---|
2158 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
2159 | if (!socket)
|
---|
2160 | return ENOTSOCK;
|
---|
2161 |
|
---|
2162 | /* Get the socket specific data */
|
---|
2163 | if (!socket->specific_data)
|
---|
2164 | return NO_DATA;
|
---|
2165 |
|
---|
2166 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
2167 |
|
---|
2168 | /* Check state */
|
---|
2169 | if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
|
---|
2170 | (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
|
---|
2171 | return ENOTCONN;
|
---|
2172 |
|
---|
2173 | rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
|
---|
2174 | &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
|
---|
2175 | if (rc != EOK)
|
---|
2176 | return rc;
|
---|
2177 |
|
---|
2178 | *data_fragment_size =
|
---|
2179 | ((packet_dimension->content < socket_data->data_fragment_size) ?
|
---|
2180 | packet_dimension->content : socket_data->data_fragment_size);
|
---|
2181 |
|
---|
2182 | for (index = 0; index < fragments; index++) {
|
---|
2183 | /* Read the data fragment */
|
---|
2184 | result = tl_socket_read_packet_data(tcp_globals.net_phone,
|
---|
2185 | &packet, TCP_HEADER_SIZE, packet_dimension,
|
---|
2186 | socket_data->addr, socket_data->addrlen);
|
---|
2187 | if (result < 0)
|
---|
2188 | return result;
|
---|
2189 |
|
---|
2190 | total_length = (size_t) result;
|
---|
2191 |
|
---|
2192 | /* Prefix the TCP header */
|
---|
2193 | header = PACKET_PREFIX(packet, tcp_header_t);
|
---|
2194 | if (!header)
|
---|
2195 | return tcp_release_and_return(packet, ENOMEM);
|
---|
2196 |
|
---|
2197 | tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
|
---|
2198 | rc = tcp_queue_packet(socket, socket_data, packet, total_length);
|
---|
2199 | if (rc != EOK)
|
---|
2200 | return rc;
|
---|
2201 | }
|
---|
2202 |
|
---|
2203 | /* Flush packets */
|
---|
2204 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
2205 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
2206 | fibril_rwlock_read_unlock(&tcp_globals.lock);
|
---|
2207 |
|
---|
2208 | if (packet) {
|
---|
2209 | /* Send the packet */
|
---|
2210 | tcp_send_packets(socket_data->device_id, packet);
|
---|
2211 | }
|
---|
2212 |
|
---|
2213 | return EOK;
|
---|
2214 | }
|
---|
2215 |
|
---|
2216 | int
|
---|
2217 | tcp_close_message(socket_cores_t *local_sockets, int socket_id)
|
---|
2218 | {
|
---|
2219 | socket_core_t *socket;
|
---|
2220 | tcp_socket_data_t *socket_data;
|
---|
2221 | packet_t *packet;
|
---|
2222 | int rc;
|
---|
2223 |
|
---|
2224 | /* Find the socket */
|
---|
2225 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
2226 | if (!socket)
|
---|
2227 | return ENOTSOCK;
|
---|
2228 |
|
---|
2229 | /* Get the socket specific data */
|
---|
2230 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
2231 | assert(socket_data);
|
---|
2232 |
|
---|
2233 | /* Check state */
|
---|
2234 | switch (socket_data->state) {
|
---|
2235 | case TCP_SOCKET_ESTABLISHED:
|
---|
2236 | socket_data->state = TCP_SOCKET_FIN_WAIT_1;
|
---|
2237 | break;
|
---|
2238 |
|
---|
2239 | case TCP_SOCKET_CLOSE_WAIT:
|
---|
2240 | socket_data->state = TCP_SOCKET_LAST_ACK;
|
---|
2241 | break;
|
---|
2242 |
|
---|
2243 | // case TCP_SOCKET_LISTEN:
|
---|
2244 |
|
---|
2245 | default:
|
---|
2246 | /* Just destroy */
|
---|
2247 | rc = socket_destroy(tcp_globals.net_phone, socket_id,
|
---|
2248 | local_sockets, &tcp_globals.sockets,
|
---|
2249 | tcp_free_socket_data);
|
---|
2250 | if (rc == EOK) {
|
---|
2251 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
2252 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
2253 | }
|
---|
2254 | return rc;
|
---|
2255 | }
|
---|
2256 |
|
---|
2257 | /*
|
---|
2258 | * Send FIN.
|
---|
2259 | * TODO should I wait to complete?
|
---|
2260 | */
|
---|
2261 |
|
---|
2262 | /* Create the notification packet */
|
---|
2263 | rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
|
---|
2264 | if (rc != EOK)
|
---|
2265 | return rc;
|
---|
2266 |
|
---|
2267 | /* Send the packet */
|
---|
2268 | rc = tcp_queue_packet(socket, socket_data, packet, 1);
|
---|
2269 | if (rc != EOK)
|
---|
2270 | return rc;
|
---|
2271 |
|
---|
2272 | /* Flush packets */
|
---|
2273 | packet = tcp_get_packets_to_send(socket, socket_data);
|
---|
2274 | fibril_rwlock_write_unlock(socket_data->local_lock);
|
---|
2275 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
2276 |
|
---|
2277 | if (packet) {
|
---|
2278 | /* Send the packet */
|
---|
2279 | tcp_send_packets(socket_data->device_id, packet);
|
---|
2280 | }
|
---|
2281 |
|
---|
2282 | return EOK;
|
---|
2283 | }
|
---|
2284 |
|
---|
2285 | int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket,
|
---|
2286 | tcp_socket_data_t *socket_data, int synchronize, int finalize)
|
---|
2287 | {
|
---|
2288 | packet_dimension_t *packet_dimension;
|
---|
2289 | tcp_header_t *header;
|
---|
2290 | int rc;
|
---|
2291 |
|
---|
2292 | assert(packet);
|
---|
2293 |
|
---|
2294 | /* Get the device packet dimension */
|
---|
2295 | rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
|
---|
2296 | &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
|
---|
2297 | if (rc != EOK)
|
---|
2298 | return rc;
|
---|
2299 |
|
---|
2300 | /* Get a new packet */
|
---|
2301 | *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
|
---|
2302 | packet_dimension->addr_len, packet_dimension->prefix,
|
---|
2303 | packet_dimension->suffix);
|
---|
2304 |
|
---|
2305 | if (!*packet)
|
---|
2306 | return ENOMEM;
|
---|
2307 |
|
---|
2308 | /* Allocate space in the packet */
|
---|
2309 | header = PACKET_SUFFIX(*packet, tcp_header_t);
|
---|
2310 | if (!header)
|
---|
2311 | tcp_release_and_return(*packet, ENOMEM);
|
---|
2312 |
|
---|
2313 | tcp_prepare_operation_header(socket, socket_data, header, synchronize,
|
---|
2314 | finalize);
|
---|
2315 |
|
---|
2316 | return EOK;
|
---|
2317 | }
|
---|
2318 |
|
---|
2319 | int tcp_accept_message(socket_cores_t *local_sockets, int socket_id,
|
---|
2320 | int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
|
---|
2321 | {
|
---|
2322 | socket_core_t *accepted;
|
---|
2323 | socket_core_t *socket;
|
---|
2324 | tcp_socket_data_t *socket_data;
|
---|
2325 | packet_dimension_t *packet_dimension;
|
---|
2326 | int rc;
|
---|
2327 |
|
---|
2328 | assert(local_sockets);
|
---|
2329 | assert(data_fragment_size);
|
---|
2330 | assert(addrlen);
|
---|
2331 |
|
---|
2332 | /* Find the socket */
|
---|
2333 | socket = socket_cores_find(local_sockets, socket_id);
|
---|
2334 | if (!socket)
|
---|
2335 | return ENOTSOCK;
|
---|
2336 |
|
---|
2337 | /* Get the socket specific data */
|
---|
2338 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
2339 | assert(socket_data);
|
---|
2340 |
|
---|
2341 | /* Check state */
|
---|
2342 | if (socket_data->state != TCP_SOCKET_LISTEN)
|
---|
2343 | return EINVAL;
|
---|
2344 |
|
---|
2345 | do {
|
---|
2346 | socket_id = dyn_fifo_value(&socket->accepted);
|
---|
2347 | if (socket_id < 0)
|
---|
2348 | return ENOTSOCK;
|
---|
2349 | socket_id *= -1;
|
---|
2350 |
|
---|
2351 | accepted = socket_cores_find(local_sockets, socket_id);
|
---|
2352 | if (!accepted)
|
---|
2353 | return ENOTSOCK;
|
---|
2354 |
|
---|
2355 | /* Get the socket specific data */
|
---|
2356 | socket_data = (tcp_socket_data_t *) accepted->specific_data;
|
---|
2357 | assert(socket_data);
|
---|
2358 | /* TODO can it be in another state? */
|
---|
2359 | if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
|
---|
2360 | rc = data_reply(socket_data->addr,
|
---|
2361 | socket_data->addrlen);
|
---|
2362 | if (rc != EOK)
|
---|
2363 | return rc;
|
---|
2364 | rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
|
---|
2365 | &tcp_globals.dimensions, socket_data->device_id,
|
---|
2366 | &packet_dimension);
|
---|
2367 | if (rc != EOK)
|
---|
2368 | return rc;
|
---|
2369 | *addrlen = socket_data->addrlen;
|
---|
2370 |
|
---|
2371 | *data_fragment_size =
|
---|
2372 | ((packet_dimension->content <
|
---|
2373 | socket_data->data_fragment_size) ?
|
---|
2374 | packet_dimension->content :
|
---|
2375 | socket_data->data_fragment_size);
|
---|
2376 |
|
---|
2377 | if (new_socket_id > 0) {
|
---|
2378 | rc = socket_cores_update(local_sockets,
|
---|
2379 | accepted->socket_id, new_socket_id);
|
---|
2380 | if (rc != EOK)
|
---|
2381 | return rc;
|
---|
2382 | accepted->socket_id = new_socket_id;
|
---|
2383 | }
|
---|
2384 | }
|
---|
2385 | dyn_fifo_pop(&socket->accepted);
|
---|
2386 | } while (socket_data->state != TCP_SOCKET_ESTABLISHED);
|
---|
2387 |
|
---|
2388 | printf("ret accept %d\n", accepted->socket_id);
|
---|
2389 | return accepted->socket_id;
|
---|
2390 | }
|
---|
2391 |
|
---|
2392 | void tcp_free_socket_data(socket_core_t *socket)
|
---|
2393 | {
|
---|
2394 | tcp_socket_data_t *socket_data;
|
---|
2395 |
|
---|
2396 | assert(socket);
|
---|
2397 |
|
---|
2398 | printf("destroy_socket %d\n", socket->socket_id);
|
---|
2399 |
|
---|
2400 | /* Get the socket specific data */
|
---|
2401 | socket_data = (tcp_socket_data_t *) socket->specific_data;
|
---|
2402 | assert(socket_data);
|
---|
2403 |
|
---|
2404 | /* Free the pseudo header */
|
---|
2405 | if (socket_data->pseudo_header) {
|
---|
2406 | if (socket_data->headerlen) {
|
---|
2407 | printf("d pseudo\n");
|
---|
2408 | free(socket_data->pseudo_header);
|
---|
2409 | socket_data->headerlen = 0;
|
---|
2410 | }
|
---|
2411 | socket_data->pseudo_header = NULL;
|
---|
2412 | }
|
---|
2413 |
|
---|
2414 | socket_data->headerlen = 0;
|
---|
2415 |
|
---|
2416 | /* Free the address */
|
---|
2417 | if (socket_data->addr) {
|
---|
2418 | if (socket_data->addrlen) {
|
---|
2419 | printf("d addr\n");
|
---|
2420 | free(socket_data->addr);
|
---|
2421 | socket_data->addrlen = 0;
|
---|
2422 | }
|
---|
2423 | socket_data->addr = NULL;
|
---|
2424 | }
|
---|
2425 | socket_data->addrlen = 0;
|
---|
2426 | }
|
---|
2427 |
|
---|
2428 | /** Releases the packet and returns the result.
|
---|
2429 | *
|
---|
2430 | * @param[in] packet The packet queue to be released.
|
---|
2431 | * @param[in] result The result to be returned.
|
---|
2432 | * @return The result parameter.
|
---|
2433 | */
|
---|
2434 | int tcp_release_and_return(packet_t *packet, int result)
|
---|
2435 | {
|
---|
2436 | pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
|
---|
2437 | return result;
|
---|
2438 | }
|
---|
2439 |
|
---|
2440 | /** Process IPC messages from the IP module
|
---|
2441 | *
|
---|
2442 | * @param[in] iid Message identifier.
|
---|
2443 | * @param[in,out] icall Message parameters.
|
---|
2444 | * @param[in] arg Local argument.
|
---|
2445 | */
|
---|
2446 | static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
2447 | {
|
---|
2448 | packet_t *packet;
|
---|
2449 | int rc;
|
---|
2450 |
|
---|
2451 | while (true) {
|
---|
2452 | switch (IPC_GET_IMETHOD(*icall)) {
|
---|
2453 | case NET_TL_RECEIVED:
|
---|
2454 | rc = packet_translate_remote(tcp_globals.net_phone, &packet,
|
---|
2455 | IPC_GET_PACKET(*icall));
|
---|
2456 | if (rc == EOK)
|
---|
2457 | rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet,
|
---|
2458 | SERVICE_TCP, IPC_GET_ERROR(*icall));
|
---|
2459 |
|
---|
2460 | async_answer_0(iid, (sysarg_t) rc);
|
---|
2461 | break;
|
---|
2462 | default:
|
---|
2463 | async_answer_0(iid, (sysarg_t) ENOTSUP);
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | iid = async_get_call(icall);
|
---|
2467 | }
|
---|
2468 | }
|
---|
2469 |
|
---|
2470 | /** Initialize the TCP module.
|
---|
2471 | *
|
---|
2472 | * @param[in] net_phone Network module phone.
|
---|
2473 | *
|
---|
2474 | * @return EOK on success.
|
---|
2475 | * @return ENOMEM if there is not enough memory left.
|
---|
2476 | *
|
---|
2477 | */
|
---|
2478 | int tl_initialize(int net_phone)
|
---|
2479 | {
|
---|
2480 | fibril_rwlock_initialize(&tcp_globals.lock);
|
---|
2481 | fibril_rwlock_write_lock(&tcp_globals.lock);
|
---|
2482 |
|
---|
2483 | tcp_globals.net_phone = net_phone;
|
---|
2484 |
|
---|
2485 | tcp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT);
|
---|
2486 | tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
|
---|
2487 | SERVICE_TCP, tcp_receiver);
|
---|
2488 | if (tcp_globals.ip_phone < 0) {
|
---|
2489 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
2490 | return tcp_globals.ip_phone;
|
---|
2491 | }
|
---|
2492 |
|
---|
2493 | int rc = socket_ports_initialize(&tcp_globals.sockets);
|
---|
2494 | if (rc != EOK)
|
---|
2495 | goto out;
|
---|
2496 |
|
---|
2497 | rc = packet_dimensions_initialize(&tcp_globals.dimensions);
|
---|
2498 | if (rc != EOK) {
|
---|
2499 | socket_ports_destroy(&tcp_globals.sockets, free);
|
---|
2500 | goto out;
|
---|
2501 | }
|
---|
2502 |
|
---|
2503 | tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
|
---|
2504 |
|
---|
2505 | out:
|
---|
2506 | fibril_rwlock_write_unlock(&tcp_globals.lock);
|
---|
2507 | return rc;
|
---|
2508 | }
|
---|
2509 |
|
---|
2510 | int main(int argc, char *argv[])
|
---|
2511 | {
|
---|
2512 | return tl_module_start(SERVICE_TCP);
|
---|
2513 | }
|
---|
2514 |
|
---|
2515 | /** @}
|
---|
2516 | */
|
---|