source: mainline/uspace/srv/net/tl/tcp/tcp.c@ 64d2b10

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

streamline ICMP implementation

  • generate ICMP identification based on fibril counter
  • use fibril-local ICMP sequence numbers
  • use hash table (instead of char map) for ICMP replies
  • simplify locking
  • simplify ICMP communication
  • Property mode set to 100644
File size: 69.1 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_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/** 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/** Per-connection initialization
1207 *
1208 */
1209void tl_connection(void)
1210{
1211}
1212
1213/** Processes the TCP message.
1214 *
1215 * @param[in] callid The message identifier.
1216 * @param[in] call The message parameters.
1217 * @param[out] answer The message answer parameters.
1218 * @param[out] answer_count The last parameter for the actual answer in the
1219 * answer parameter.
1220 * @return EOK on success.
1221 * @return ENOTSUP if the message is not known.
1222 *
1223 * @see tcp_interface.h
1224 * @see IS_NET_TCP_MESSAGE()
1225 */
1226int tl_message(ipc_callid_t callid, ipc_call_t *call,
1227 ipc_call_t *answer, size_t *answer_count)
1228{
1229 assert(call);
1230 assert(answer);
1231 assert(answer_count);
1232
1233 *answer_count = 0;
1234 switch (IPC_GET_IMETHOD(*call)) {
1235 case IPC_M_CONNECT_TO_ME:
1236 return tcp_process_client_messages(callid, *call);
1237 }
1238
1239 return ENOTSUP;
1240}
1241
1242void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
1243{
1244 assert(socket_data);
1245
1246 bzero(socket_data, sizeof(*socket_data));
1247 socket_data->state = TCP_SOCKET_INITIAL;
1248 socket_data->device_id = DEVICE_INVALID_ID;
1249 socket_data->window = NET_DEFAULT_TCP_WINDOW;
1250 socket_data->treshold = socket_data->window;
1251 socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER;
1252 socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT;
1253 socket_data->acknowledged = socket_data->last_outgoing;
1254 socket_data->next_outgoing = socket_data->last_outgoing + 1;
1255 socket_data->expected = socket_data->next_outgoing;
1256}
1257
1258void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
1259{
1260 assert(socket_data);
1261
1262 tcp_refresh_socket_data(socket_data);
1263 fibril_mutex_initialize(&socket_data->operation.mutex);
1264 fibril_condvar_initialize(&socket_data->operation.condvar);
1265 socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE;
1266}
1267
1268int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
1269{
1270 int res;
1271 bool keep_on_going = true;
1272 socket_cores_t local_sockets;
1273 int app_phone = IPC_GET_PHONE(call);
1274 struct sockaddr *addr;
1275 int socket_id;
1276 size_t addrlen;
1277 size_t size;
1278 fibril_rwlock_t lock;
1279 ipc_call_t answer;
1280 size_t answer_count;
1281 tcp_socket_data_t *socket_data;
1282 socket_core_t *socket;
1283 packet_dimension_t *packet_dimension;
1284
1285 /*
1286 * Accept the connection
1287 * - Answer the first IPC_M_CONNECT_ME_TO call.
1288 */
1289 res = EOK;
1290 answer_count = 0;
1291
1292 socket_cores_initialize(&local_sockets);
1293 fibril_rwlock_initialize(&lock);
1294
1295 while (keep_on_going) {
1296
1297 /* Answer the call */
1298 answer_call(callid, res, &answer, answer_count);
1299 /* Refresh data */
1300 refresh_answer(&answer, &answer_count);
1301 /* Get the next call */
1302 callid = async_get_call(&call);
1303
1304 /* Process the call */
1305 switch (IPC_GET_IMETHOD(call)) {
1306 case IPC_M_PHONE_HUNGUP:
1307 keep_on_going = false;
1308 res = EHANGUP;
1309 break;
1310
1311 case NET_SOCKET:
1312 socket_data =
1313 (tcp_socket_data_t *) malloc(sizeof(*socket_data));
1314 if (!socket_data) {
1315 res = ENOMEM;
1316 break;
1317 }
1318
1319 tcp_initialize_socket_data(socket_data);
1320 socket_data->local_lock = &lock;
1321 socket_data->local_sockets = &local_sockets;
1322 fibril_rwlock_write_lock(&lock);
1323 socket_id = SOCKET_GET_SOCKET_ID(call);
1324 res = socket_create(&local_sockets, app_phone,
1325 socket_data, &socket_id);
1326 SOCKET_SET_SOCKET_ID(answer, socket_id);
1327 fibril_rwlock_write_unlock(&lock);
1328 if (res != EOK) {
1329 free(socket_data);
1330 break;
1331 }
1332 if (tl_get_ip_packet_dimension(tcp_globals.ip_phone,
1333 &tcp_globals.dimensions, DEVICE_INVALID_ID,
1334 &packet_dimension) == EOK) {
1335 SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
1336 ((packet_dimension->content <
1337 socket_data->data_fragment_size) ?
1338 packet_dimension->content :
1339 socket_data->data_fragment_size));
1340 }
1341// SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);
1342 SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);
1343 answer_count = 3;
1344 break;
1345
1346 case NET_SOCKET_BIND:
1347 res = async_data_write_accept((void **) &addr, false,
1348 0, 0, 0, &addrlen);
1349 if (res != EOK)
1350 break;
1351 fibril_rwlock_write_lock(&tcp_globals.lock);
1352 fibril_rwlock_write_lock(&lock);
1353 res = socket_bind(&local_sockets, &tcp_globals.sockets,
1354 SOCKET_GET_SOCKET_ID(call), addr, addrlen,
1355 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
1356 tcp_globals.last_used_port);
1357 if (res == EOK) {
1358 socket = socket_cores_find(&local_sockets,
1359 SOCKET_GET_SOCKET_ID(call));
1360 if (socket) {
1361 socket_data = (tcp_socket_data_t *)
1362 socket->specific_data;
1363 assert(socket_data);
1364 socket_data->state = TCP_SOCKET_LISTEN;
1365 }
1366 }
1367 fibril_rwlock_write_unlock(&lock);
1368 fibril_rwlock_write_unlock(&tcp_globals.lock);
1369 free(addr);
1370 break;
1371
1372 case NET_SOCKET_LISTEN:
1373 fibril_rwlock_read_lock(&tcp_globals.lock);
1374// fibril_rwlock_write_lock(&tcp_globals.lock);
1375 fibril_rwlock_write_lock(&lock);
1376 res = tcp_listen_message(&local_sockets,
1377 SOCKET_GET_SOCKET_ID(call),
1378 SOCKET_GET_BACKLOG(call));
1379 fibril_rwlock_write_unlock(&lock);
1380// fibril_rwlock_write_unlock(&tcp_globals.lock);
1381 fibril_rwlock_read_unlock(&tcp_globals.lock);
1382 break;
1383
1384 case NET_SOCKET_CONNECT:
1385 res = async_data_write_accept((void **) &addr, false,
1386 0, 0, 0, &addrlen);
1387 if (res != EOK)
1388 break;
1389 /*
1390 * The global lock may be released in the
1391 * tcp_connect_message() function.
1392 */
1393 fibril_rwlock_write_lock(&tcp_globals.lock);
1394 fibril_rwlock_write_lock(&lock);
1395 res = tcp_connect_message(&local_sockets,
1396 SOCKET_GET_SOCKET_ID(call), addr, addrlen);
1397 if (res != EOK) {
1398 fibril_rwlock_write_unlock(&lock);
1399 fibril_rwlock_write_unlock(&tcp_globals.lock);
1400 free(addr);
1401 }
1402 break;
1403
1404 case NET_SOCKET_ACCEPT:
1405 fibril_rwlock_read_lock(&tcp_globals.lock);
1406 fibril_rwlock_write_lock(&lock);
1407 res = tcp_accept_message(&local_sockets,
1408 SOCKET_GET_SOCKET_ID(call),
1409 SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen);
1410 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
1411 fibril_rwlock_write_unlock(&lock);
1412 fibril_rwlock_read_unlock(&tcp_globals.lock);
1413 if (res > 0) {
1414 SOCKET_SET_SOCKET_ID(answer, res);
1415 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
1416 answer_count = 3;
1417 }
1418 break;
1419
1420 case NET_SOCKET_SEND:
1421 fibril_rwlock_read_lock(&tcp_globals.lock);
1422 fibril_rwlock_write_lock(&lock);
1423 res = tcp_send_message(&local_sockets,
1424 SOCKET_GET_SOCKET_ID(call),
1425 SOCKET_GET_DATA_FRAGMENTS(call), &size,
1426 SOCKET_GET_FLAGS(call));
1427 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
1428 if (res != EOK) {
1429 fibril_rwlock_write_unlock(&lock);
1430 fibril_rwlock_read_unlock(&tcp_globals.lock);
1431 } else {
1432 answer_count = 2;
1433 }
1434 break;
1435
1436 case NET_SOCKET_SENDTO:
1437 res = async_data_write_accept((void **) &addr, false,
1438 0, 0, 0, &addrlen);
1439 if (res != EOK)
1440 break;
1441 fibril_rwlock_read_lock(&tcp_globals.lock);
1442 fibril_rwlock_write_lock(&lock);
1443 res = tcp_send_message(&local_sockets,
1444 SOCKET_GET_SOCKET_ID(call),
1445 SOCKET_GET_DATA_FRAGMENTS(call), &size,
1446 SOCKET_GET_FLAGS(call));
1447 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
1448 if (res != EOK) {
1449 fibril_rwlock_write_unlock(&lock);
1450 fibril_rwlock_read_unlock(&tcp_globals.lock);
1451 } else {
1452 answer_count = 2;
1453 }
1454 free(addr);
1455 break;
1456
1457 case NET_SOCKET_RECV:
1458 fibril_rwlock_read_lock(&tcp_globals.lock);
1459 fibril_rwlock_write_lock(&lock);
1460 res = tcp_recvfrom_message(&local_sockets,
1461 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
1462 NULL);
1463 fibril_rwlock_write_unlock(&lock);
1464 fibril_rwlock_read_unlock(&tcp_globals.lock);
1465 if (res > 0) {
1466 SOCKET_SET_READ_DATA_LENGTH(answer, res);
1467 answer_count = 1;
1468 res = EOK;
1469 }
1470 break;
1471
1472 case NET_SOCKET_RECVFROM:
1473 fibril_rwlock_read_lock(&tcp_globals.lock);
1474 fibril_rwlock_write_lock(&lock);
1475 res = tcp_recvfrom_message(&local_sockets,
1476 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
1477 &addrlen);
1478 fibril_rwlock_write_unlock(&lock);
1479 fibril_rwlock_read_unlock(&tcp_globals.lock);
1480 if (res > 0) {
1481 SOCKET_SET_READ_DATA_LENGTH(answer, res);
1482 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
1483 answer_count = 3;
1484 res = EOK;
1485 }
1486 break;
1487
1488 case NET_SOCKET_CLOSE:
1489 fibril_rwlock_write_lock(&tcp_globals.lock);
1490 fibril_rwlock_write_lock(&lock);
1491 res = tcp_close_message(&local_sockets,
1492 SOCKET_GET_SOCKET_ID(call));
1493 if (res != EOK) {
1494 fibril_rwlock_write_unlock(&lock);
1495 fibril_rwlock_write_unlock(&tcp_globals.lock);
1496 }
1497 break;
1498
1499 case NET_SOCKET_GETSOCKOPT:
1500 case NET_SOCKET_SETSOCKOPT:
1501 default:
1502 res = ENOTSUP;
1503 break;
1504 }
1505 }
1506
1507 /* Release the application phone */
1508 ipc_hangup(app_phone);
1509
1510 printf("release\n");
1511 /* Release all local sockets */
1512 socket_cores_release(tcp_globals.net_phone, &local_sockets,
1513 &tcp_globals.sockets, tcp_free_socket_data);
1514
1515 return EOK;
1516}
1517
1518int tcp_timeout(void *data)
1519{
1520 tcp_timeout_t *timeout = data;
1521 int keep_write_lock = false;
1522 socket_core_t *socket;
1523 tcp_socket_data_t *socket_data;
1524
1525 assert(timeout);
1526
1527 /* Sleep the given timeout */
1528 async_usleep(timeout->timeout);
1529 /* Lock the globals */
1530 if (timeout->globals_read_only)
1531 fibril_rwlock_read_lock(&tcp_globals.lock);
1532 else
1533 fibril_rwlock_write_lock(&tcp_globals.lock);
1534
1535 /* Find the pending operation socket */
1536 socket = socket_port_find(&tcp_globals.sockets, timeout->port,
1537 timeout->key, timeout->key_length);
1538 if (!socket || (socket->socket_id != timeout->socket_id))
1539 goto out;
1540
1541 socket_data = (tcp_socket_data_t *) socket->specific_data;
1542 assert(socket_data);
1543 if (socket_data->local_sockets != timeout->local_sockets)
1544 goto out;
1545
1546 fibril_rwlock_write_lock(socket_data->local_lock);
1547 if (timeout->sequence_number) {
1548 /* Increase the timeout counter */
1549 socket_data->timeout_count++;
1550 if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
1551 /* TODO release as connection lost */
1552 //tcp_refresh_socket_data(socket_data);
1553 fibril_rwlock_write_unlock(socket_data->local_lock);
1554 } else {
1555 /* Retransmit */
1556// tcp_retransmit_packet(socket,
1557// socket_data, timeout->sequence_number);
1558 fibril_rwlock_write_unlock(socket_data->local_lock);
1559 }
1560 } else {
1561 fibril_mutex_lock(&socket_data->operation.mutex);
1562 /* Set the timeout operation result if state not changed */
1563 if (socket_data->state == timeout->state) {
1564 socket_data->operation.result = ETIMEOUT;
1565
1566 /* Notify the main fibril */
1567 fibril_condvar_signal(&socket_data->operation.condvar);
1568
1569 /* Keep the global write lock */
1570 keep_write_lock = true;
1571 } else {
1572 /*
1573 * Operation is ok, do nothing.
1574 * Unlocking from now on, so the unlocking
1575 * order does not matter.
1576 */
1577 fibril_rwlock_write_unlock(socket_data->local_lock);
1578 }
1579 fibril_mutex_unlock(&socket_data->operation.mutex);
1580 }
1581
1582out:
1583 /* Unlock only if no socket */
1584 if (timeout->globals_read_only)
1585 fibril_rwlock_read_unlock(&tcp_globals.lock);
1586 else if (!keep_write_lock)
1587 /* Release if not desired */
1588 fibril_rwlock_write_unlock(&tcp_globals.lock);
1589
1590 /* Release the timeout structure */
1591 free(timeout);
1592 return EOK;
1593}
1594
1595int tcp_release_after_timeout(void *data)
1596{
1597 tcp_timeout_t *timeout = data;
1598 socket_core_t *socket;
1599 tcp_socket_data_t *socket_data;
1600 fibril_rwlock_t *local_lock;
1601
1602 assert(timeout);
1603
1604 /* Sleep the given timeout */
1605 async_usleep(timeout->timeout);
1606
1607 /* Lock the globals */
1608 fibril_rwlock_write_lock(&tcp_globals.lock);
1609
1610 /* Find the pending operation socket */
1611 socket = socket_port_find(&tcp_globals.sockets, timeout->port,
1612 timeout->key, timeout->key_length);
1613
1614 if (socket && (socket->socket_id == timeout->socket_id)) {
1615 socket_data = (tcp_socket_data_t *) socket->specific_data;
1616 assert(socket_data);
1617 if (socket_data->local_sockets == timeout->local_sockets) {
1618 local_lock = socket_data->local_lock;
1619 fibril_rwlock_write_lock(local_lock);
1620 socket_destroy(tcp_globals.net_phone,
1621 timeout->socket_id, timeout->local_sockets,
1622 &tcp_globals.sockets, tcp_free_socket_data);
1623 fibril_rwlock_write_unlock(local_lock);
1624 }
1625 }
1626
1627 /* Unlock the globals */
1628 fibril_rwlock_write_unlock(&tcp_globals.lock);
1629
1630 /* Release the timeout structure */
1631 free(timeout);
1632
1633 return EOK;
1634}
1635
1636void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t *
1637 socket_data, size_t sequence_number)
1638{
1639 packet_t *packet;
1640 packet_t *copy;
1641 size_t data_length;
1642
1643 assert(socket);
1644 assert(socket_data);
1645 assert(socket->specific_data == socket_data);
1646
1647 /* Sent packet? */
1648 packet = pq_find(socket_data->outgoing, sequence_number);
1649 printf("retransmit %d\n", packet_get_id(packet));
1650 if (packet) {
1651 pq_get_order(packet, NULL, &data_length);
1652 copy = tcp_prepare_copy(socket, socket_data, packet,
1653 data_length, sequence_number);
1654 fibril_rwlock_write_unlock(socket_data->local_lock);
1655// printf("r send %d\n", packet_get_id(packet));
1656 if (copy)
1657 tcp_send_packets(socket_data->device_id, copy);
1658 } else {
1659 fibril_rwlock_write_unlock(socket_data->local_lock);
1660 }
1661}
1662
1663int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,
1664 int backlog)
1665{
1666 socket_core_t *socket;
1667 tcp_socket_data_t *socket_data;
1668
1669 assert(local_sockets);
1670
1671 if (backlog < 0)
1672 return EINVAL;
1673
1674 /* Find the socket */
1675 socket = socket_cores_find(local_sockets, socket_id);
1676 if (!socket)
1677 return ENOTSOCK;
1678
1679 /* Get the socket specific data */
1680 socket_data = (tcp_socket_data_t *) socket->specific_data;
1681 assert(socket_data);
1682
1683 /* Set the backlog */
1684 socket_data->backlog = backlog;
1685
1686 return EOK;
1687}
1688
1689int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
1690 struct sockaddr *addr, socklen_t addrlen)
1691{
1692 socket_core_t *socket;
1693 int rc;
1694
1695 assert(local_sockets);
1696 assert(addr);
1697 assert(addrlen > 0);
1698
1699 /* Find the socket */
1700 socket = socket_cores_find(local_sockets, socket_id);
1701 if (!socket)
1702 return ENOTSOCK;
1703
1704 rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
1705 if (rc != EOK) {
1706 tcp_free_socket_data(socket);
1707 /* Unbind if bound */
1708 if (socket->port > 0) {
1709 socket_ports_exclude(&tcp_globals.sockets,
1710 socket->port);
1711 socket->port = 0;
1712 }
1713 }
1714 return rc;
1715}
1716
1717int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
1718 struct sockaddr *addr, socklen_t addrlen)
1719{
1720 tcp_socket_data_t *socket_data;
1721 packet_t *packet;
1722 int rc;
1723
1724 assert(socket);
1725 assert(addr);
1726 assert(addrlen > 0);
1727
1728 /* Get the socket specific data */
1729 socket_data = (tcp_socket_data_t *) socket->specific_data;
1730 assert(socket_data);
1731 assert(socket->specific_data == socket_data);
1732 if ((socket_data->state != TCP_SOCKET_INITIAL) &&
1733 ((socket_data->state != TCP_SOCKET_LISTEN) ||
1734 (socket->port <= 0)))
1735 return EINVAL;
1736
1737 /* Get the destination port */
1738 rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
1739 if (rc != EOK)
1740 return rc;
1741
1742 if (socket->port <= 0) {
1743 /* Try to find a free port */
1744 rc = socket_bind_free_port(&tcp_globals.sockets, socket,
1745 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END,
1746 tcp_globals.last_used_port);
1747 if (rc != EOK)
1748 return rc;
1749 /* Set the next port as the search starting port number */
1750 tcp_globals.last_used_port = socket->port;
1751 }
1752
1753 rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
1754 addr, addrlen, &socket_data->device_id,
1755 &socket_data->pseudo_header, &socket_data->headerlen);
1756 if (rc != EOK)
1757 return rc;
1758
1759 /* Create the notification packet */
1760 rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
1761 if (rc != EOK)
1762 return rc;
1763
1764 /* Unlock the globals and wait for an operation */
1765 fibril_rwlock_write_unlock(&tcp_globals.lock);
1766
1767 socket_data->addr = addr;
1768 socket_data->addrlen = addrlen;
1769
1770 /* Send the packet */
1771
1772 if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) ||
1773 ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0,
1774 TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) !=
1775 EOK)) {
1776 socket_data->addr = NULL;
1777 socket_data->addrlen = 0;
1778 fibril_rwlock_write_lock(&tcp_globals.lock);
1779 } else {
1780 packet = tcp_get_packets_to_send(socket, socket_data);
1781 if (packet) {
1782 fibril_mutex_lock(&socket_data->operation.mutex);
1783 fibril_rwlock_write_unlock(socket_data->local_lock);
1784
1785 socket_data->state = TCP_SOCKET_SYN_SENT;
1786
1787 /* Send the packet */
1788 printf("connecting %d\n", packet_get_id(packet));
1789 tcp_send_packets(socket_data->device_id, packet);
1790
1791 /* Wait for a reply */
1792 fibril_condvar_wait(&socket_data->operation.condvar,
1793 &socket_data->operation.mutex);
1794 rc = socket_data->operation.result;
1795 if (rc != EOK) {
1796 socket_data->addr = NULL;
1797 socket_data->addrlen = 0;
1798 }
1799 } else {
1800 socket_data->addr = NULL;
1801 socket_data->addrlen = 0;
1802 rc = EINTR;
1803 }
1804 }
1805
1806 fibril_mutex_unlock(&socket_data->operation.mutex);
1807 return rc;
1808}
1809
1810int tcp_queue_prepare_packet(socket_core_t *socket,
1811 tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length)
1812{
1813 tcp_header_t *header;
1814 int rc;
1815
1816 assert(socket);
1817 assert(socket_data);
1818 assert(socket->specific_data == socket_data);
1819
1820 /* Get TCP header */
1821 header = (tcp_header_t *) packet_get_data(packet);
1822 if (!header)
1823 return NO_DATA;
1824
1825 header->destination_port = htons(socket_data->dest_port);
1826 header->source_port = htons(socket->port);
1827 header->sequence_number = htonl(socket_data->next_outgoing);
1828
1829 rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
1830 socket_data->addrlen);
1831 if (rc != EOK)
1832 return tcp_release_and_return(packet, EINVAL);
1833
1834 /* Remember the outgoing FIN */
1835 if (header->finalize)
1836 socket_data->fin_outgoing = socket_data->next_outgoing;
1837
1838 return EOK;
1839}
1840
1841int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data,
1842 packet_t *packet, size_t data_length)
1843{
1844 int rc;
1845
1846 assert(socket);
1847 assert(socket_data);
1848 assert(socket->specific_data == socket_data);
1849
1850 rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
1851 if (rc != EOK)
1852 return rc;
1853
1854 rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing,
1855 data_length);
1856 if (rc != EOK)
1857 return tcp_release_and_return(packet, rc);
1858
1859 socket_data->next_outgoing += data_length;
1860 return EOK;
1861}
1862
1863packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t *
1864 socket_data)
1865{
1866 packet_t *packet;
1867 packet_t *copy;
1868 packet_t *sending = NULL;
1869 packet_t *previous = NULL;
1870 size_t data_length;
1871 int rc;
1872
1873 assert(socket);
1874 assert(socket_data);
1875 assert(socket->specific_data == socket_data);
1876
1877 packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
1878 while (packet) {
1879 pq_get_order(packet, NULL, &data_length);
1880
1881 /*
1882 * Send only if fits into the window, respecting the possible
1883 * overflow.
1884 */
1885 if (!IS_IN_INTERVAL_OVERFLOW(
1886 (uint32_t) socket_data->last_outgoing,
1887 (uint32_t) (socket_data->last_outgoing + data_length),
1888 (uint32_t) (socket_data->expected + socket_data->treshold)))
1889 break;
1890
1891 copy = tcp_prepare_copy(socket, socket_data, packet,
1892 data_length, socket_data->last_outgoing + 1);
1893 if (!copy)
1894 return sending;
1895
1896 if (!sending) {
1897 sending = copy;
1898 } else {
1899 rc = pq_insert_after(previous, copy);
1900 if (rc != EOK) {
1901 pq_release_remote(tcp_globals.net_phone,
1902 packet_get_id(copy));
1903 return sending;
1904 }
1905 }
1906
1907 previous = copy;
1908 packet = pq_next(packet);
1909
1910 /* Overflow occurred? */
1911 if (!packet &&
1912 (socket_data->last_outgoing > socket_data->next_outgoing)) {
1913 printf("gpts overflow\n");
1914 /* Continue from the beginning */
1915 packet = socket_data->outgoing;
1916 }
1917 socket_data->last_outgoing += data_length;
1918 }
1919
1920 return sending;
1921}
1922
1923packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t *
1924 socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
1925{
1926 tcp_header_t *header;
1927 uint32_t checksum;
1928 int rc;
1929
1930 assert(socket);
1931 assert(socket_data);
1932 assert(socket->specific_data == socket_data);
1933
1934 /* Adjust the pseudo header */
1935 rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,
1936 socket_data->headerlen, packet_get_data_length(packet));
1937 if (rc != EOK) {
1938 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
1939 return NULL;
1940 }
1941
1942 /* Get the header */
1943 header = (tcp_header_t *) packet_get_data(packet);
1944 if (!header) {
1945 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
1946 return NULL;
1947 }
1948 assert(ntohl(header->sequence_number) == sequence_number);
1949
1950 /* Adjust the header */
1951 if (socket_data->next_incoming) {
1952 header->acknowledgement_number =
1953 htonl(socket_data->next_incoming);
1954 header->acknowledge = 1;
1955 }
1956 header->window = htons(socket_data->window);
1957
1958 /* Checksum */
1959 header->checksum = 0;
1960 checksum = compute_checksum(0, socket_data->pseudo_header,
1961 socket_data->headerlen);
1962 checksum = compute_checksum(checksum,
1963 (uint8_t *) packet_get_data(packet),
1964 packet_get_data_length(packet));
1965 header->checksum = htons(flip_checksum(compact_checksum(checksum)));
1966
1967 /* Prepare the packet */
1968 rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0);
1969 if (rc != EOK) {
1970 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
1971 return NULL;
1972 }
1973
1974 rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
1975 sequence_number, socket_data->state, socket_data->timeout, true);
1976 if (rc != EOK) {
1977 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
1978 return NULL;
1979 }
1980
1981 return packet;
1982}
1983
1984packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t *
1985 socket_data, packet_t *packet, size_t data_length, size_t sequence_number)
1986{
1987 packet_t *copy;
1988
1989 assert(socket);
1990 assert(socket_data);
1991 assert(socket->specific_data == socket_data);
1992
1993 /* Make a copy of the packet */
1994 copy = packet_get_copy(tcp_globals.net_phone, packet);
1995 if (!copy)
1996 return NULL;
1997
1998 return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
1999 sequence_number);
2000}
2001
2002void tcp_send_packets(device_id_t device_id, packet_t *packet)
2003{
2004 packet_t *next;
2005
2006 while (packet) {
2007 next = pq_detach(packet);
2008 ip_send_msg(tcp_globals.ip_phone, device_id, packet,
2009 SERVICE_TCP, 0);
2010 packet = next;
2011 }
2012}
2013
2014void tcp_prepare_operation_header(socket_core_t *socket,
2015 tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
2016 int finalize)
2017{
2018 assert(socket);
2019 assert(socket_data);
2020 assert(socket->specific_data == socket_data);
2021 assert(header);
2022
2023 bzero(header, sizeof(*header));
2024 header->source_port = htons(socket->port);
2025 header->source_port = htons(socket_data->dest_port);
2026 header->header_length = TCP_COMPUTE_HEADER_LENGTH(sizeof(*header));
2027 header->synchronize = synchronize;
2028 header->finalize = finalize;
2029}
2030
2031int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
2032 socket_core_t *socket, tcp_socket_data_t *socket_data,
2033 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
2034 int globals_read_only)
2035{
2036 tcp_timeout_t *operation_timeout;
2037 fid_t fibril;
2038
2039 assert(socket);
2040 assert(socket_data);
2041 assert(socket->specific_data == socket_data);
2042
2043 /* Prepare the timeout with key bundle structure */
2044 operation_timeout = malloc(sizeof(*operation_timeout) +
2045 socket->key_length + 1);
2046 if (!operation_timeout)
2047 return ENOMEM;
2048
2049 bzero(operation_timeout, sizeof(*operation_timeout));
2050 operation_timeout->globals_read_only = globals_read_only;
2051 operation_timeout->port = socket->port;
2052 operation_timeout->local_sockets = socket_data->local_sockets;
2053 operation_timeout->socket_id = socket->socket_id;
2054 operation_timeout->timeout = timeout;
2055 operation_timeout->sequence_number = sequence_number;
2056 operation_timeout->state = state;
2057
2058 /* Copy the key */
2059 operation_timeout->key = ((uint8_t *) operation_timeout) +
2060 sizeof(*operation_timeout);
2061 operation_timeout->key_length = socket->key_length;
2062 memcpy(operation_timeout->key, socket->key, socket->key_length);
2063 operation_timeout->key[operation_timeout->key_length] = '\0';
2064
2065 /* Prepare the timeouting thread */
2066 fibril = fibril_create(timeout_function, operation_timeout);
2067 if (!fibril) {
2068 free(operation_timeout);
2069 return ENOMEM;
2070 }
2071
2072// fibril_mutex_lock(&socket_data->operation.mutex);
2073 /* Start the timeout fibril */
2074 fibril_add_ready(fibril);
2075 //socket_data->state = state;
2076 return EOK;
2077}
2078
2079int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
2080 int flags, size_t *addrlen)
2081{
2082 socket_core_t *socket;
2083 tcp_socket_data_t *socket_data;
2084 int packet_id;
2085 packet_t *packet;
2086 size_t length;
2087 int rc;
2088
2089 assert(local_sockets);
2090
2091 /* Find the socket */
2092 socket = socket_cores_find(local_sockets, socket_id);
2093 if (!socket)
2094 return ENOTSOCK;
2095
2096 /* Get the socket specific data */
2097 if (!socket->specific_data)
2098 return NO_DATA;
2099
2100 socket_data = (tcp_socket_data_t *) socket->specific_data;
2101
2102 /* Check state */
2103 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
2104 (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
2105 return ENOTCONN;
2106
2107 /* Send the source address if desired */
2108 if (addrlen) {
2109 rc = data_reply(socket_data->addr, socket_data->addrlen);
2110 if (rc != EOK)
2111 return rc;
2112 *addrlen = socket_data->addrlen;
2113 }
2114
2115 /* Get the next received packet */
2116 packet_id = dyn_fifo_value(&socket->received);
2117 if (packet_id < 0)
2118 return NO_DATA;
2119
2120 rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
2121 if (rc != EOK)
2122 return rc;
2123
2124 /* Reply the packets */
2125 rc = socket_reply_packets(packet, &length);
2126 if (rc != EOK)
2127 return rc;
2128
2129 /* Release the packet */
2130 dyn_fifo_pop(&socket->received);
2131 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
2132
2133 /* Return the total length */
2134 return (int) length;
2135}
2136
2137int tcp_send_message(socket_cores_t *local_sockets, int socket_id,
2138 int fragments, size_t *data_fragment_size, int flags)
2139{
2140 socket_core_t *socket;
2141 tcp_socket_data_t *socket_data;
2142 packet_dimension_t *packet_dimension;
2143 packet_t *packet;
2144 size_t total_length;
2145 tcp_header_t *header;
2146 int index;
2147 int result;
2148 int rc;
2149
2150 assert(local_sockets);
2151 assert(data_fragment_size);
2152
2153 /* Find the socket */
2154 socket = socket_cores_find(local_sockets, socket_id);
2155 if (!socket)
2156 return ENOTSOCK;
2157
2158 /* Get the socket specific data */
2159 if (!socket->specific_data)
2160 return NO_DATA;
2161
2162 socket_data = (tcp_socket_data_t *) socket->specific_data;
2163
2164 /* Check state */
2165 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
2166 (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
2167 return ENOTCONN;
2168
2169 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
2170 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
2171 if (rc != EOK)
2172 return rc;
2173
2174 *data_fragment_size =
2175 ((packet_dimension->content < socket_data->data_fragment_size) ?
2176 packet_dimension->content : socket_data->data_fragment_size);
2177
2178 for (index = 0; index < fragments; index++) {
2179 /* Read the data fragment */
2180 result = tl_socket_read_packet_data(tcp_globals.net_phone,
2181 &packet, TCP_HEADER_SIZE, packet_dimension,
2182 socket_data->addr, socket_data->addrlen);
2183 if (result < 0)
2184 return result;
2185
2186 total_length = (size_t) result;
2187
2188 /* Prefix the TCP header */
2189 header = PACKET_PREFIX(packet, tcp_header_t);
2190 if (!header)
2191 return tcp_release_and_return(packet, ENOMEM);
2192
2193 tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
2194 rc = tcp_queue_packet(socket, socket_data, packet, total_length);
2195 if (rc != EOK)
2196 return rc;
2197 }
2198
2199 /* Flush packets */
2200 packet = tcp_get_packets_to_send(socket, socket_data);
2201 fibril_rwlock_write_unlock(socket_data->local_lock);
2202 fibril_rwlock_read_unlock(&tcp_globals.lock);
2203
2204 if (packet) {
2205 /* Send the packet */
2206 tcp_send_packets(socket_data->device_id, packet);
2207 }
2208
2209 return EOK;
2210}
2211
2212int
2213tcp_close_message(socket_cores_t *local_sockets, int socket_id)
2214{
2215 socket_core_t *socket;
2216 tcp_socket_data_t *socket_data;
2217 packet_t *packet;
2218 int rc;
2219
2220 /* Find the socket */
2221 socket = socket_cores_find(local_sockets, socket_id);
2222 if (!socket)
2223 return ENOTSOCK;
2224
2225 /* Get the socket specific data */
2226 socket_data = (tcp_socket_data_t *) socket->specific_data;
2227 assert(socket_data);
2228
2229 /* Check state */
2230 switch (socket_data->state) {
2231 case TCP_SOCKET_ESTABLISHED:
2232 socket_data->state = TCP_SOCKET_FIN_WAIT_1;
2233 break;
2234
2235 case TCP_SOCKET_CLOSE_WAIT:
2236 socket_data->state = TCP_SOCKET_LAST_ACK;
2237 break;
2238
2239// case TCP_SOCKET_LISTEN:
2240
2241 default:
2242 /* Just destroy */
2243 rc = socket_destroy(tcp_globals.net_phone, socket_id,
2244 local_sockets, &tcp_globals.sockets,
2245 tcp_free_socket_data);
2246 if (rc == EOK) {
2247 fibril_rwlock_write_unlock(socket_data->local_lock);
2248 fibril_rwlock_write_unlock(&tcp_globals.lock);
2249 }
2250 return rc;
2251 }
2252
2253 /*
2254 * Send FIN.
2255 * TODO should I wait to complete?
2256 */
2257
2258 /* Create the notification packet */
2259 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
2260 if (rc != EOK)
2261 return rc;
2262
2263 /* Send the packet */
2264 rc = tcp_queue_packet(socket, socket_data, packet, 1);
2265 if (rc != EOK)
2266 return rc;
2267
2268 /* Flush packets */
2269 packet = tcp_get_packets_to_send(socket, socket_data);
2270 fibril_rwlock_write_unlock(socket_data->local_lock);
2271 fibril_rwlock_write_unlock(&tcp_globals.lock);
2272
2273 if (packet) {
2274 /* Send the packet */
2275 tcp_send_packets(socket_data->device_id, packet);
2276 }
2277
2278 return EOK;
2279}
2280
2281int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket,
2282 tcp_socket_data_t *socket_data, int synchronize, int finalize)
2283{
2284 packet_dimension_t *packet_dimension;
2285 tcp_header_t *header;
2286 int rc;
2287
2288 assert(packet);
2289
2290 /* Get the device packet dimension */
2291 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
2292 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension);
2293 if (rc != EOK)
2294 return rc;
2295
2296 /* Get a new packet */
2297 *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE,
2298 packet_dimension->addr_len, packet_dimension->prefix,
2299 packet_dimension->suffix);
2300
2301 if (!*packet)
2302 return ENOMEM;
2303
2304 /* Allocate space in the packet */
2305 header = PACKET_SUFFIX(*packet, tcp_header_t);
2306 if (!header)
2307 tcp_release_and_return(*packet, ENOMEM);
2308
2309 tcp_prepare_operation_header(socket, socket_data, header, synchronize,
2310 finalize);
2311
2312 return EOK;
2313}
2314
2315int tcp_accept_message(socket_cores_t *local_sockets, int socket_id,
2316 int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
2317{
2318 socket_core_t *accepted;
2319 socket_core_t *socket;
2320 tcp_socket_data_t *socket_data;
2321 packet_dimension_t *packet_dimension;
2322 int rc;
2323
2324 assert(local_sockets);
2325 assert(data_fragment_size);
2326 assert(addrlen);
2327
2328 /* Find the socket */
2329 socket = socket_cores_find(local_sockets, socket_id);
2330 if (!socket)
2331 return ENOTSOCK;
2332
2333 /* Get the socket specific data */
2334 socket_data = (tcp_socket_data_t *) socket->specific_data;
2335 assert(socket_data);
2336
2337 /* Check state */
2338 if (socket_data->state != TCP_SOCKET_LISTEN)
2339 return EINVAL;
2340
2341 do {
2342 socket_id = dyn_fifo_value(&socket->accepted);
2343 if (socket_id < 0)
2344 return ENOTSOCK;
2345 socket_id *= -1;
2346
2347 accepted = socket_cores_find(local_sockets, socket_id);
2348 if (!accepted)
2349 return ENOTSOCK;
2350
2351 /* Get the socket specific data */
2352 socket_data = (tcp_socket_data_t *) accepted->specific_data;
2353 assert(socket_data);
2354 /* TODO can it be in another state? */
2355 if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
2356 rc = data_reply(socket_data->addr,
2357 socket_data->addrlen);
2358 if (rc != EOK)
2359 return rc;
2360 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone,
2361 &tcp_globals.dimensions, socket_data->device_id,
2362 &packet_dimension);
2363 if (rc != EOK)
2364 return rc;
2365 *addrlen = socket_data->addrlen;
2366
2367 *data_fragment_size =
2368 ((packet_dimension->content <
2369 socket_data->data_fragment_size) ?
2370 packet_dimension->content :
2371 socket_data->data_fragment_size);
2372
2373 if (new_socket_id > 0) {
2374 rc = socket_cores_update(local_sockets,
2375 accepted->socket_id, new_socket_id);
2376 if (rc != EOK)
2377 return rc;
2378 accepted->socket_id = new_socket_id;
2379 }
2380 }
2381 dyn_fifo_pop(&socket->accepted);
2382 } while (socket_data->state != TCP_SOCKET_ESTABLISHED);
2383
2384 printf("ret accept %d\n", accepted->socket_id);
2385 return accepted->socket_id;
2386}
2387
2388void tcp_free_socket_data(socket_core_t *socket)
2389{
2390 tcp_socket_data_t *socket_data;
2391
2392 assert(socket);
2393
2394 printf("destroy_socket %d\n", socket->socket_id);
2395
2396 /* Get the socket specific data */
2397 socket_data = (tcp_socket_data_t *) socket->specific_data;
2398 assert(socket_data);
2399
2400 /* Free the pseudo header */
2401 if (socket_data->pseudo_header) {
2402 if (socket_data->headerlen) {
2403 printf("d pseudo\n");
2404 free(socket_data->pseudo_header);
2405 socket_data->headerlen = 0;
2406 }
2407 socket_data->pseudo_header = NULL;
2408 }
2409
2410 socket_data->headerlen = 0;
2411
2412 /* Free the address */
2413 if (socket_data->addr) {
2414 if (socket_data->addrlen) {
2415 printf("d addr\n");
2416 free(socket_data->addr);
2417 socket_data->addrlen = 0;
2418 }
2419 socket_data->addr = NULL;
2420 }
2421 socket_data->addrlen = 0;
2422}
2423
2424/** Releases the packet and returns the result.
2425 *
2426 * @param[in] packet The packet queue to be released.
2427 * @param[in] result The result to be returned.
2428 * @return The result parameter.
2429 */
2430int tcp_release_and_return(packet_t *packet, int result)
2431{
2432 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
2433 return result;
2434}
2435
2436/** Process IPC messages from the IP module
2437 *
2438 * @param[in] iid Message identifier.
2439 * @param[in,out] icall Message parameters.
2440 *
2441 */
2442static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall)
2443{
2444 packet_t *packet;
2445 int rc;
2446
2447 while (true) {
2448 switch (IPC_GET_IMETHOD(*icall)) {
2449 case NET_TL_RECEIVED:
2450 rc = packet_translate_remote(tcp_globals.net_phone, &packet,
2451 IPC_GET_PACKET(*icall));
2452 if (rc == EOK)
2453 rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet,
2454 SERVICE_TCP, IPC_GET_ERROR(*icall));
2455
2456 ipc_answer_0(iid, (sysarg_t) rc);
2457 break;
2458 default:
2459 ipc_answer_0(iid, (sysarg_t) ENOTSUP);
2460 }
2461
2462 iid = async_get_call(icall);
2463 }
2464}
2465
2466/** Initialize the TCP module.
2467 *
2468 * @param[in] net_phone Network module phone.
2469 *
2470 * @return EOK on success.
2471 * @return ENOMEM if there is not enough memory left.
2472 *
2473 */
2474int tl_initialize(int net_phone)
2475{
2476 fibril_rwlock_initialize(&tcp_globals.lock);
2477 fibril_rwlock_write_lock(&tcp_globals.lock);
2478
2479 tcp_globals.net_phone = net_phone;
2480
2481 tcp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT);
2482 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP,
2483 SERVICE_TCP, tcp_receiver);
2484 if (tcp_globals.ip_phone < 0) {
2485 fibril_rwlock_write_unlock(&tcp_globals.lock);
2486 return tcp_globals.ip_phone;
2487 }
2488
2489 int rc = socket_ports_initialize(&tcp_globals.sockets);
2490 if (rc != EOK)
2491 goto out;
2492
2493 rc = packet_dimensions_initialize(&tcp_globals.dimensions);
2494 if (rc != EOK) {
2495 socket_ports_destroy(&tcp_globals.sockets);
2496 goto out;
2497 }
2498
2499 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1;
2500
2501out:
2502 fibril_rwlock_write_unlock(&tcp_globals.lock);
2503 return rc;
2504}
2505
2506int main(int argc, char *argv[])
2507{
2508 return tl_module_start(SERVICE_TCP);
2509}
2510
2511/** @}
2512 */
Note: See TracBrowser for help on using the repository browser.