source: mainline/uspace/srv/net/tl/tcp/tcp.c@ 014dd57b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 014dd57b was 014dd57b, checked in by Martin Decky <martin@…>, 14 years ago

streamline and create a common skeleton for the transport layer
(in the line of the previous updates to the lower layers)

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