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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f1938c6 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
RevLine 
[21580dd]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
[89e57cee]30 * @{
[21580dd]31 */
32
33/** @file
[89e57cee]34 * TCP module implementation.
35 * @see tcp.h
[21580dd]36 */
37
38#include <assert.h>
39#include <async.h>
40#include <fibril_synch.h>
41#include <malloc.h>
[fb04cba8]42/* TODO remove stdio */
[21580dd]43#include <stdio.h>
[e98b1d5]44#include <errno.h>
[21580dd]45
46#include <ipc/ipc.h>
47#include <ipc/services.h>
[514ee46]48#include <ipc/net.h>
[8e3a65c]49#include <ipc/tl.h>
[88e127ee]50#include <ipc/socket.h>
[21580dd]51
[058edb6]52#include <net/socket_codes.h>
[fe5d3c1b]53#include <net/ip_protocols.h>
[e4554d4]54#include <net/in.h>
55#include <net/in6.h>
56#include <net/inet.h>
[c7a8442]57#include <net/modules.h>
[058edb6]58
[849ed54]59#include <adt/dynamic_fifo.h>
[0a866eeb]60#include <packet_client.h>
[14f1db0]61#include <packet_remote.h>
[849ed54]62#include <net_checksum.h>
63#include <ip_client.h>
64#include <ip_interface.h>
65#include <icmp_client.h>
[f1938c6]66#include <icmp_remote.h>
[849ed54]67#include <net_interface.h>
68#include <socket_core.h>
69#include <tl_common.h>
[014dd57b]70#include <tl_remote.h>
71#include <tl_skel.h>
72
73#include "tcp.h"
74#include "tcp_header.h"
[21580dd]75
[7c8267b]76/** TCP module name. */
[014dd57b]77#define NAME "tcp"
[849ed54]78
[7c8267b]79/** The TCP window default value. */
80#define NET_DEFAULT_TCP_WINDOW 10240
[21580dd]81
[7c8267b]82/** Initial timeout for new connections. */
[21580dd]83#define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L
84
[7c8267b]85/** Default timeout for closing. */
86#define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L
[21580dd]87
[7c8267b]88/** The initial outgoing sequence number. */
89#define TCP_INITIAL_SEQUENCE_NUMBER 2999
[21580dd]90
[7c8267b]91/** Maximum TCP fragment size. */
92#define MAX_TCP_FRAGMENT_SIZE 65535
[21580dd]93
[7c8267b]94/** Free ports pool start. */
95#define TCP_FREE_PORTS_START 1025
[21580dd]96
[7c8267b]97/** Free ports pool end. */
[21580dd]98#define TCP_FREE_PORTS_END 65535
99
[7c8267b]100/** Timeout for connection initialization, SYN sent. */
101#define TCP_SYN_SENT_TIMEOUT 1000000L
[21580dd]102
[7c8267b]103/** The maximum number of timeouts in a row before singaling connection lost. */
[21580dd]104#define TCP_MAX_TIMEOUTS 8
105
[7c8267b]106/** The number of acknowledgements before retransmit. */
[21580dd]107#define TCP_FAST_RETRANSMIT_COUNT 3
108
[7c8267b]109/** Returns a value indicating whether the value is in the interval respecting
[89e57cee]110 * the possible overflow.
111 *
112 * The high end and/or the value may overflow, be lower than the low value.
[7c8267b]113 *
[89e57cee]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.
[21580dd]117 */
[7c8267b]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))))
[21580dd]122
123/** Type definition of the TCP timeout.
124 * @see tcp_timeout
125 */
[7c8267b]126typedef struct tcp_timeout tcp_timeout_t;
[21580dd]127
128/** TCP reply timeout data.
129 * Used as a timeouting fibril argument.
130 * @see tcp_timeout()
131 */
[7c8267b]132struct tcp_timeout {
133 /** TCP global data are going to be read only. */
[aadf01e]134 int globals_read_only;
[7c8267b]135
136 /** Socket port. */
[aadf01e]137 int port;
[7c8267b]138
139 /** Local sockets. */
[aaa3f33a]140 socket_cores_t *local_sockets;
[7c8267b]141
142 /** Socket identifier. */
[aadf01e]143 int socket_id;
[7c8267b]144
145 /** Socket state. */
[aadf01e]146 tcp_socket_state_t state;
[7c8267b]147
148 /** Sent packet sequence number. */
[aadf01e]149 int sequence_number;
[7c8267b]150
151 /** Timeout in microseconds. */
[aadf01e]152 suseconds_t timeout;
[7c8267b]153
154 /** Port map key. */
[61bfc370]155 uint8_t *key;
[7c8267b]156
157 /** Port map key length. */
[aadf01e]158 size_t key_length;
[21580dd]159};
160
[46d4d9f]161static int tcp_release_and_return(packet_t *, int);
[88a1bb9]162static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *,
[4e5c7ba]163 tcp_header_t *, int synchronize, int);
[88a1bb9]164static int tcp_prepare_timeout(int (*)(void *), socket_core_t *,
[4e5c7ba]165 tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int);
[88a1bb9]166static void tcp_free_socket_data(socket_core_t *);
[89e57cee]167
168static int tcp_timeout(void *);
169
170static int tcp_release_after_timeout(void *);
171
[46d4d9f]172static int tcp_process_packet(device_id_t, packet_t *, services_t);
[aaa3f33a]173static int tcp_connect_core(socket_core_t *, socket_cores_t *,
[89e57cee]174 struct sockaddr *, socklen_t);
[88a1bb9]175static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]176 packet_t *, size_t);
177static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *,
[89e57cee]178 size_t);
[46d4d9f]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 *);
[89e57cee]181
[88a1bb9]182static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *,
[4e5c7ba]183 tcp_header_t *);
[46d4d9f]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);
[88a1bb9]188/* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *,
[89e57cee]189 size_t);
[46d4d9f]190static int tcp_create_notification_packet(packet_t **, socket_core_t *,
[4e5c7ba]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
[88a1bb9]196static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]197 tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t);
[88a1bb9]198static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]199 tcp_header_t *, packet_t *);
[88a1bb9]200static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]201 tcp_header_t *, packet_t *);
[88a1bb9]202static int tcp_process_established(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]203 tcp_header_t *, packet_t *, int, size_t);
[88a1bb9]204static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *,
[46d4d9f]205 packet_t *, int, size_t);
[d493830e]206static void tcp_queue_received_end_of_data(socket_core_t *socket);
[89e57cee]207
[46d4d9f]208static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t);
[89e57cee]209static int tcp_process_client_messages(ipc_callid_t, ipc_call_t);
210
[aaa3f33a]211static int tcp_listen_message(socket_cores_t *, int, int);
212static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *,
[89e57cee]213 socklen_t);
[aaa3f33a]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);
[21580dd]218
[7c8267b]219/** TCP global data. */
220tcp_globals_t tcp_globals;
[21580dd]221
[46d4d9f]222int tcp_received_msg(device_id_t device_id, packet_t *packet,
[fb04cba8]223 services_t receiver, services_t error)
[7c8267b]224{
[0578271]225 int rc;
[21580dd]226
[7c8267b]227 if (receiver != SERVICE_TCP)
[aadf01e]228 return EREFUSED;
[7c8267b]229
[aadf01e]230 fibril_rwlock_write_lock(&tcp_globals.lock);
[0578271]231 rc = tcp_process_packet(device_id, packet, error);
232 if (rc != EOK)
[aadf01e]233 fibril_rwlock_write_unlock(&tcp_globals.lock);
[7c8267b]234
[0578271]235 printf("receive %d \n", rc);
[21580dd]236
[0578271]237 return rc;
[21580dd]238}
239
[46d4d9f]240int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error)
[7c8267b]241{
[aadf01e]242 size_t length;
243 size_t offset;
244 int result;
[4e5c7ba]245 tcp_header_t *header;
[88a1bb9]246 socket_core_t *socket;
[4e5c7ba]247 tcp_socket_data_t *socket_data;
[46d4d9f]248 packet_t *next_packet;
[aadf01e]249 size_t total_length;
250 uint32_t checksum;
251 int fragments;
252 icmp_type_t type;
253 icmp_code_t code;
[7c8267b]254 struct sockaddr *src;
255 struct sockaddr *dest;
[aadf01e]256 size_t addrlen;
[0578271]257 int rc;
[aadf01e]258
[89e57cee]259 switch (error) {
260 case SERVICE_NONE:
261 break;
262 case SERVICE_ICMP:
[fb04cba8]263 /* Process error */
[89e57cee]264 result = icmp_client_process_packet(packet, &type, &code, NULL,
265 NULL);
266 if (result < 0)
267 return tcp_release_and_return(packet, result);
[7c8267b]268
[89e57cee]269 length = (size_t) result;
[0578271]270 rc = packet_trim(packet, length, 0);
271 if (rc != EOK)
272 return tcp_release_and_return(packet, rc);
[89e57cee]273 break;
274 default:
275 return tcp_release_and_return(packet, ENOTSUP);
[21580dd]276 }
277
[fb04cba8]278 /* TODO process received ipopts? */
[aadf01e]279 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
[7c8267b]280 if (result < 0)
[aadf01e]281 return tcp_release_and_return(packet, result);
[7c8267b]282
[aadf01e]283 offset = (size_t) result;
[21580dd]284
[aadf01e]285 length = packet_get_data_length(packet);
[7c8267b]286 if (length <= 0)
[aadf01e]287 return tcp_release_and_return(packet, EINVAL);
[7c8267b]288
289 if (length < TCP_HEADER_SIZE + offset)
[aadf01e]290 return tcp_release_and_return(packet, NO_DATA);
[21580dd]291
[fb04cba8]292 /* Trim all but TCP header */
[0578271]293 rc = packet_trim(packet, offset, 0);
294 if (rc != EOK)
295 return tcp_release_and_return(packet, rc);
[21580dd]296
[fb04cba8]297 /* Get tcp header */
[4e5c7ba]298 header = (tcp_header_t *) packet_get_data(packet);
[7c8267b]299 if (!header)
[aadf01e]300 return tcp_release_and_return(packet, NO_DATA);
[7c8267b]301
302// printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header),
303// ntohs(header->destination_port));
[21580dd]304
[aadf01e]305 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
[7c8267b]306 if (result <= 0)
[aadf01e]307 return tcp_release_and_return(packet, result);
[7c8267b]308
[aadf01e]309 addrlen = (size_t) result;
[21580dd]310
[0578271]311 rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));
312 if (rc != EOK)
313 return tcp_release_and_return(packet, rc);
[7c8267b]314
[fb04cba8]315 /* Find the destination socket */
[7c8267b]316 socket = socket_port_find(&tcp_globals.sockets,
[61bfc370]317 ntohs(header->destination_port), (uint8_t *) src, addrlen);
[7c8267b]318 if (!socket) {
[fb04cba8]319 /* Find the listening destination socket */
[7c8267b]320 socket = socket_port_find(&tcp_globals.sockets,
[61bfc370]321 ntohs(header->destination_port),
322 (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
[89e57cee]323 }
[fb04cba8]324
[89e57cee]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);
[21580dd]330 }
[89e57cee]331 return EADDRNOTAVAIL;
[21580dd]332 }
[89e57cee]333
[aadf01e]334 printf("socket id %d\n", socket->socket_id);
[4e5c7ba]335 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]336 assert(socket_data);
[21580dd]337
[fb04cba8]338 /* Some data received, clear the timeout counter */
[21580dd]339 socket_data->timeout_count = 0;
340
[fb04cba8]341 /* Count the received packet fragments */
[21580dd]342 next_packet = packet;
343 fragments = 0;
344 checksum = 0;
345 total_length = 0;
[7c8267b]346 do {
[89e57cee]347 fragments++;
[aadf01e]348 length = packet_get_data_length(next_packet);
[7c8267b]349 if (length <= 0)
[aadf01e]350 return tcp_release_and_return(packet, NO_DATA);
[7c8267b]351
[21580dd]352 total_length += length;
[7c8267b]353
[fb04cba8]354 /* Add partial checksum if set */
[7c8267b]355 if (!error) {
356 checksum = compute_checksum(checksum,
357 packet_get_data(packet),
358 packet_get_data_length(packet));
[21580dd]359 }
360
[7c8267b]361 } while ((next_packet = pq_next(next_packet)));
362
[aadf01e]363 fibril_rwlock_write_lock(socket_data->local_lock);
[7c8267b]364
365 if (error)
[89e57cee]366 goto has_error_service;
[7c8267b]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
[0578271]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) {
[aadf01e]379 fibril_rwlock_write_unlock(socket_data->local_lock);
[0578271]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);
[21580dd]389 }
[7c8267b]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
[0578271]399 rc = tl_prepare_icmp_packet(tcp_globals.net_phone,
400 tcp_globals.icmp_phone, packet, error);
401 if (rc == EOK) {
[fb04cba8]402 /* Checksum error ICMP */
[7c8267b]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);
[21580dd]407 }
[7c8267b]408
409 return EINVAL;
[21580dd]410 }
411
[89e57cee]412has_error_service:
[092e4f1]413 fibril_rwlock_write_unlock(&tcp_globals.lock);
[21580dd]414
[fb04cba8]415 /* TODO error reporting/handling */
[7c8267b]416 switch (socket_data->state) {
417 case TCP_SOCKET_LISTEN:
[0578271]418 rc = tcp_process_listen(socket, socket_data, header, packet,
419 src, dest, addrlen);
[7c8267b]420 break;
421 case TCP_SOCKET_SYN_RECEIVED:
[0578271]422 rc = tcp_process_syn_received(socket, socket_data, header,
423 packet);
[7c8267b]424 break;
425 case TCP_SOCKET_SYN_SENT:
[0578271]426 rc = tcp_process_syn_sent(socket, socket_data, header, packet);
[7c8267b]427 break;
428 case TCP_SOCKET_FIN_WAIT_1:
[fb04cba8]429 /* ack changing the state to FIN_WAIT_2 gets processed later */
[7c8267b]430 case TCP_SOCKET_FIN_WAIT_2:
[fb04cba8]431 /* fin changing state to LAST_ACK gets processed later */
[7c8267b]432 case TCP_SOCKET_LAST_ACK:
[fb04cba8]433 /* ack releasing the socket get processed later */
[7c8267b]434 case TCP_SOCKET_CLOSING:
[fb04cba8]435 /* ack releasing the socket gets processed later */
[7c8267b]436 case TCP_SOCKET_ESTABLISHED:
[0578271]437 rc = tcp_process_established(socket, socket_data, header,
438 packet, fragments, total_length);
[7c8267b]439 break;
440 default:
441 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[21580dd]442 }
443
[0578271]444 if (rc != EOK) {
[aadf01e]445 fibril_rwlock_write_unlock(socket_data->local_lock);
[0578271]446 printf("process %d\n", rc);
[21580dd]447 }
[7c8267b]448
[21580dd]449 return EOK;
450}
451
[88a1bb9]452int tcp_process_established(socket_core_t *socket, tcp_socket_data_t *
[46d4d9f]453 socket_data, tcp_header_t *header, packet_t *packet, int fragments,
[fb04cba8]454 size_t total_length)
[7c8267b]455{
[46d4d9f]456 packet_t *next_packet;
457 packet_t *tmp_packet;
[aadf01e]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;
[d493830e]464 bool forced_ack;
[0578271]465 int rc;
[aadf01e]466
467 assert(socket);
468 assert(socket_data);
469 assert(socket->specific_data == socket_data);
470 assert(header);
471 assert(packet);
472
[d493830e]473 forced_ack = false;
474
[aadf01e]475 new_sequence_number = ntohl(header->sequence_number);
[21580dd]476 old_incoming = socket_data->next_incoming;
477
[d493830e]478 if (header->finalize) {
479 socket_data->fin_incoming = new_sequence_number +
480 total_length - TCP_HEADER_LENGTH(header);
481 }
[21580dd]482
[fb04cba8]483 /* Trim begining if containing expected data */
[7c8267b]484 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
485 socket_data->next_incoming, new_sequence_number + total_length)) {
486
[fb04cba8]487 /* Get the acknowledged offset */
[7c8267b]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;
[21580dd]494 }
[7c8267b]495
[21580dd]496 new_sequence_number += offset;
497 total_length -= offset;
[aadf01e]498 length = packet_get_data_length(packet);
[fb04cba8]499
500 /* Trim the acknowledged data */
[7c8267b]501 while (length <= offset) {
[fb04cba8]502 /* Release the acknowledged packets */
[aadf01e]503 next_packet = pq_next(packet);
[7c8267b]504 pq_release_remote(tcp_globals.net_phone,
505 packet_get_id(packet));
[21580dd]506 packet = next_packet;
507 offset -= length;
[aadf01e]508 length = packet_get_data_length(packet);
[21580dd]509 }
[7c8267b]510
[0578271]511 if (offset > 0) {
512 rc = packet_trim(packet, offset, 0);
513 if (rc != EOK)
514 return tcp_release_and_return(packet, rc);
515 }
[7c8267b]516
[aadf01e]517 assert(new_sequence_number == socket_data->next_incoming);
[21580dd]518 }
519
[fb04cba8]520 /* Release if overflowing the window */
[21580dd]521/*
[7c8267b]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
[21580dd]528 // trim end if overflowing the window
[7c8267b]529 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number,
530 socket_data->next_incoming + socket_data->window,
531 new_sequence_number + total_length)) {
[21580dd]532 // get the allowed data length
[7c8267b]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;
[21580dd]540 }
541 next_packet = packet;
542 // trim the overflowing data
[7c8267b]543 while (next_packet && (offset > 0)) {
[aadf01e]544 length = packet_get_data_length(packet);
[7c8267b]545 if (length <= offset)
[aadf01e]546 next_packet = pq_next(next_packet);
[0578271]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 }
[21580dd]554 offset -= length;
555 total_length -= length - offset;
556 }
557 // release the overflowing packets
[aadf01e]558 next_packet = pq_next(next_packet);
[7c8267b]559 if (next_packet) {
[21580dd]560 tmp_packet = next_packet;
[aadf01e]561 next_packet = pq_next(next_packet);
562 pq_insert_after(tmp_packet, next_packet);
[7c8267b]563 pq_release_remote(tcp_globals.net_phone,
564 packet_get_id(tmp_packet));
[21580dd]565 }
[7c8267b]566 assert(new_sequence_number + total_length ==
567 socket_data->next_incoming + socket_data->window);
[21580dd]568 }
569*/
[fb04cba8]570 /* The expected one arrived? */
[7c8267b]571 if (new_sequence_number == socket_data->next_incoming) {
[21580dd]572 printf("expected\n");
[fb04cba8]573 /* Process acknowledgement */
[aadf01e]574 tcp_process_acknowledgement(socket, socket_data, header);
[21580dd]575
[fb04cba8]576 /* Remove the header */
[aadf01e]577 total_length -= TCP_HEADER_LENGTH(header);
[0578271]578 rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
579 if (rc != EOK)
580 return tcp_release_and_return(packet, rc);
[21580dd]581
[7c8267b]582 if (total_length) {
[0578271]583 rc = tcp_queue_received_packet(socket, socket_data,
584 packet, fragments, total_length);
585 if (rc != EOK)
586 return rc;
[7c8267b]587 } else {
[21580dd]588 total_length = 1;
589 }
[7c8267b]590
[21580dd]591 socket_data->next_incoming = old_incoming + total_length;
592 packet = socket_data->incoming;
[7c8267b]593 while (packet) {
[0578271]594 rc = pq_get_order(socket_data->incoming, &order, NULL);
595 if (rc != EOK) {
[fb04cba8]596 /* Remove the corrupted packet */
[aadf01e]597 next_packet = pq_detach(packet);
[7c8267b]598 if (packet == socket_data->incoming)
[21580dd]599 socket_data->incoming = next_packet;
[7c8267b]600 pq_release_remote(tcp_globals.net_phone,
601 packet_get_id(packet));
[21580dd]602 packet = next_packet;
603 continue;
604 }
[7c8267b]605
[aadf01e]606 sequence_number = (uint32_t) order;
[7c8267b]607 if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
608 old_incoming, socket_data->next_incoming)) {
[fb04cba8]609 /* Move to the next */
[aadf01e]610 packet = pq_next(packet);
[fb04cba8]611 /* Coninual data? */
[7c8267b]612 } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming,
613 sequence_number, socket_data->next_incoming)) {
[fb04cba8]614 /* Detach the packet */
[aadf01e]615 next_packet = pq_detach(packet);
[7c8267b]616 if (packet == socket_data->incoming)
[21580dd]617 socket_data->incoming = next_packet;
[fb04cba8]618 /* Get data length */
[aadf01e]619 length = packet_get_data_length(packet);
[21580dd]620 new_sequence_number = sequence_number + length;
[7c8267b]621 if (length <= 0) {
[fb04cba8]622 /* Remove the empty packet */
[7c8267b]623 pq_release_remote(tcp_globals.net_phone,
624 packet_get_id(packet));
[21580dd]625 packet = next_packet;
626 continue;
627 }
[fb04cba8]628 /* Exactly following */
[7c8267b]629 if (sequence_number ==
630 socket_data->next_incoming) {
[fb04cba8]631 /* Queue received data */
[0578271]632 rc = tcp_queue_received_packet(socket,
[7c8267b]633 socket_data, packet, 1,
[0578271]634 packet_get_data_length(packet));
635 if (rc != EOK)
636 return rc;
[7c8267b]637 socket_data->next_incoming =
638 new_sequence_number;
[21580dd]639 packet = next_packet;
640 continue;
[fb04cba8]641 /* At least partly following data? */
[89e57cee]642 }
643 if (IS_IN_INTERVAL_OVERFLOW(sequence_number,
644 socket_data->next_incoming, new_sequence_number)) {
[7c8267b]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;
[21580dd]653 }
[0578271]654 rc = packet_trim(packet,length, 0);
655 if (rc == EOK) {
[fb04cba8]656 /* Queue received data */
[0578271]657 rc = tcp_queue_received_packet(
[7c8267b]658 socket, socket_data, packet,
659 1, packet_get_data_length(
[0578271]660 packet));
661 if (rc != EOK)
662 return rc;
[7c8267b]663 socket_data->next_incoming =
664 new_sequence_number;
[21580dd]665 packet = next_packet;
666 continue;
667 }
668 }
[fb04cba8]669 /* Remove the duplicit or corrupted packet */
[7c8267b]670 pq_release_remote(tcp_globals.net_phone,
671 packet_get_id(packet));
[21580dd]672 packet = next_packet;
673 continue;
[7c8267b]674 } else {
[21580dd]675 break;
676 }
677 }
[7c8267b]678 } else if (IS_IN_INTERVAL(socket_data->next_incoming,
679 new_sequence_number,
680 socket_data->next_incoming + socket_data->window)) {
[21580dd]681 printf("in window\n");
[fb04cba8]682 /* Process acknowledgement */
[aadf01e]683 tcp_process_acknowledgement(socket, socket_data, header);
[21580dd]684
[fb04cba8]685 /* Remove the header */
[aadf01e]686 total_length -= TCP_HEADER_LENGTH(header);
[0578271]687 rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);
688 if (rc != EOK)
689 return tcp_release_and_return(packet, rc);
[21580dd]690
[aadf01e]691 next_packet = pq_detach(packet);
692 length = packet_get_data_length(packet);
[0578271]693 rc = pq_add(&socket_data->incoming, packet, new_sequence_number,
694 length);
695 if (rc != EOK) {
[fb04cba8]696 /* Remove the corrupted packets */
[7c8267b]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) {
[21580dd]703 new_sequence_number += length;
[aadf01e]704 tmp_packet = pq_detach(next_packet);
705 length = packet_get_data_length(next_packet);
[0578271]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) {
[7c8267b]715 pq_release_remote(tcp_globals.net_phone,
716 packet_get_id(next_packet));
[21580dd]717 }
718 next_packet = tmp_packet;
719 }
720 }
[7c8267b]721 } else {
[21580dd]722 printf("unexpected\n");
[fb04cba8]723 /* Release duplicite or restricted */
[14f1db0]724 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[d493830e]725 forced_ack = true;
[21580dd]726 }
727
[d493830e]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 */
[7c8267b]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;
[d493830e]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;
[7c8267b]745 default:
746 socket_data->state = TCP_SOCKET_CLOSE_WAIT;
747 break;
[21580dd]748 }
749 }
750
[aadf01e]751 packet = tcp_get_packets_to_send(socket, socket_data);
[d493830e]752 if (!packet && (socket_data->next_incoming != old_incoming || forced_ack)) {
[fb04cba8]753 /* Create the notification packet */
[0578271]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;
[7c8267b]761 packet = tcp_send_prepare_packet(socket, socket_data, packet, 1,
762 socket_data->last_outgoing + 1);
[21580dd]763 }
[7c8267b]764
[aadf01e]765 fibril_rwlock_write_unlock(socket_data->local_lock);
[7c8267b]766
[fb04cba8]767 /* Send the packet */
[aadf01e]768 tcp_send_packets(socket_data->device_id, packet);
[7c8267b]769
[21580dd]770 return EOK;
771}
772
[88a1bb9]773int tcp_queue_received_packet(socket_core_t *socket,
[46d4d9f]774 tcp_socket_data_t *socket_data, packet_t *packet, int fragments,
[7c8267b]775 size_t total_length)
776{
[f772bc55]777 packet_dimension_t *packet_dimension;
[0578271]778 int rc;
[ede63e4]779
[aadf01e]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);
[21580dd]786
[fb04cba8]787 /* Queue the received packet */
[0578271]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);
[21580dd]796
[fb04cba8]797 /* Decrease the window size */
[21580dd]798 socket_data->window -= total_length;
799
[fb04cba8]800 /* Notify the destination socket */
[7c8267b]801 async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
[96b02eb9]802 (sysarg_t) socket->socket_id,
[7c8267b]803 ((packet_dimension->content < socket_data->data_fragment_size) ?
804 packet_dimension->content : socket_data->data_fragment_size), 0, 0,
[96b02eb9]805 (sysarg_t) fragments);
[7c8267b]806
[21580dd]807 return EOK;
808}
809
[d493830e]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
[88a1bb9]828int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t *
[46d4d9f]829 socket_data, tcp_header_t *header, packet_t *packet)
[7c8267b]830{
[46d4d9f]831 packet_t *next_packet;
[0578271]832 int rc;
[21580dd]833
[aadf01e]834 assert(socket);
835 assert(socket_data);
836 assert(socket->specific_data == socket_data);
837 assert(header);
838 assert(packet);
[21580dd]839
[7c8267b]840 if (!header->synchronize)
841 return tcp_release_and_return(packet, EINVAL);
842
[fb04cba8]843 /* Process acknowledgement */
[7c8267b]844 tcp_process_acknowledgement(socket, socket_data, header);
845
846 socket_data->next_incoming = ntohl(header->sequence_number) + 1;
[fb04cba8]847
848 /* Release additional packets */
[7c8267b]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 }
[fb04cba8]854
855 /* Trim if longer than the header */
[0578271]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);
[7c8267b]861 }
[fb04cba8]862
[7c8267b]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);
[fb04cba8]867
[7c8267b]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);
[fb04cba8]873 /* Send the packet */
[7c8267b]874 tcp_send_packets(socket_data->device_id, packet);
[fb04cba8]875 /* Signal the result */
[7c8267b]876 fibril_condvar_signal( &socket_data->operation.condvar);
877 fibril_mutex_unlock( &socket_data->operation.mutex);
878 return EOK;
[21580dd]879 }
880 }
[fb04cba8]881
[7c8267b]882 fibril_mutex_unlock(&socket_data->operation.mutex);
[aadf01e]883 return tcp_release_and_return(packet, EINVAL);
[21580dd]884}
885
[88a1bb9]886int tcp_process_listen(socket_core_t *listening_socket,
[4e5c7ba]887 tcp_socket_data_t *listening_socket_data, tcp_header_t *header,
[46d4d9f]888 packet_t *packet, struct sockaddr *src, struct sockaddr *dest,
[7c8267b]889 size_t addrlen)
890{
[46d4d9f]891 packet_t *next_packet;
[88a1bb9]892 socket_core_t *socket;
[4e5c7ba]893 tcp_socket_data_t *socket_data;
[aadf01e]894 int socket_id;
895 int listening_socket_id = listening_socket->socket_id;
896 int listening_port = listening_socket->port;
[0578271]897 int rc;
[aadf01e]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
[7c8267b]905 if (!header->synchronize)
906 return tcp_release_and_return(packet, EINVAL);
[21580dd]907
[4e5c7ba]908 socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data));
[7c8267b]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 }
[fb04cba8]925
[7c8267b]926 memcpy(socket_data->addr, src, socket_data->addrlen);
927 socket_data->dest_port = ntohs(header->source_port);
[0578271]928 rc = tl_set_address_port(socket_data->addr, socket_data->addrlen,
929 socket_data->dest_port);
930 if (rc != EOK) {
[7c8267b]931 free(socket_data->addr);
932 free(socket_data);
[0578271]933 return tcp_release_and_return(packet, rc);
[7c8267b]934 }
[21580dd]935
[fb04cba8]936 /* Create a socket */
[7c8267b]937 socket_id = -1;
[0578271]938 rc = socket_create(socket_data->local_sockets, listening_socket->phone,
939 socket_data, &socket_id);
940 if (rc != EOK) {
[7c8267b]941 free(socket_data->addr);
942 free(socket_data);
[0578271]943 return tcp_release_and_return(packet, rc);
[7c8267b]944 }
[21580dd]945
[7c8267b]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;
[21580dd]951
[7c8267b]952 fibril_rwlock_write_unlock(socket_data->local_lock);
953 fibril_rwlock_write_lock(&tcp_globals.lock);
[21580dd]954
[fb04cba8]955 /* Find the destination socket */
[7c8267b]956 listening_socket = socket_port_find(&tcp_globals.sockets,
[61bfc370]957 listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0);
[89e57cee]958 if (!listening_socket ||
[7c8267b]959 (listening_socket->socket_id != listening_socket_id)) {
960 fibril_rwlock_write_unlock(&tcp_globals.lock);
[fb04cba8]961 /* A shadow may remain until app hangs up */
[7c8267b]962 return tcp_release_and_return(packet, EOK /*ENOTSOCK*/);
963 }
964 listening_socket_data =
[4e5c7ba]965 (tcp_socket_data_t *) listening_socket->specific_data;
[7c8267b]966 assert(listening_socket_data);
[21580dd]967
[7c8267b]968 fibril_rwlock_write_lock(listening_socket_data->local_lock);
[21580dd]969
[7c8267b]970 socket = socket_cores_find(listening_socket_data->local_sockets,
971 socket_id);
972 if (!socket) {
[fb04cba8]973 /* Where is the socket?!? */
[7c8267b]974 fibril_rwlock_write_unlock(&tcp_globals.lock);
975 return ENOTSOCK;
976 }
[4e5c7ba]977 socket_data = (tcp_socket_data_t *) socket->specific_data;
[7c8267b]978 assert(socket_data);
[21580dd]979
[0578271]980 rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,
[61bfc370]981 (uint8_t *) socket_data->addr, socket_data->addrlen);
[7c8267b]982 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port,
[61bfc370]983 (uint8_t *) socket_data->addr, socket_data->addrlen));
[21580dd]984
[0578271]985// rc = socket_bind_free_port(&tcp_globals.sockets, socket,
[7c8267b]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);
[0578271]990 if (rc != EOK) {
[7c8267b]991 socket_destroy(tcp_globals.net_phone, socket->socket_id,
992 socket_data->local_sockets, &tcp_globals.sockets,
993 tcp_free_socket_data);
[0578271]994 return tcp_release_and_return(packet, rc);
[7c8267b]995 }
[21580dd]996
[7c8267b]997 socket_data->state = TCP_SOCKET_LISTEN;
998 socket_data->next_incoming = ntohl(header->sequence_number) + 1;
[21580dd]999
[fb04cba8]1000 /* Release additional packets */
[7c8267b]1001 next_packet = pq_detach(packet);
1002 if (next_packet) {
1003 pq_release_remote(tcp_globals.net_phone,
1004 packet_get_id(next_packet));
[21580dd]1005 }
[7c8267b]1006
[fb04cba8]1007 /* Trim if longer than the header */
[0578271]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 }
[7c8267b]1017 }
1018
1019 tcp_prepare_operation_header(socket, socket_data, header, 1, 0);
1020
[0578271]1021 rc = tcp_queue_packet(socket, socket_data, packet, 1);
1022 if (rc != EOK) {
[7c8267b]1023 socket_destroy(tcp_globals.net_phone, socket->socket_id,
1024 socket_data->local_sockets, &tcp_globals.sockets,
1025 tcp_free_socket_data);
[0578271]1026 return rc;
[7c8267b]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
[fb04cba8]1040 /* Send the packet */
[7c8267b]1041 tcp_send_packets(socket_data->device_id, packet);
1042
1043 return EOK;
[21580dd]1044}
1045
[88a1bb9]1046int tcp_process_syn_received(socket_core_t *socket,
[46d4d9f]1047 tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet)
[7c8267b]1048{
[88a1bb9]1049 socket_core_t *listening_socket;
[4e5c7ba]1050 tcp_socket_data_t *listening_socket_data;
[0578271]1051 int rc;
[21580dd]1052
[aadf01e]1053 assert(socket);
1054 assert(socket_data);
1055 assert(socket->specific_data == socket_data);
1056 assert(header);
1057 assert(packet);
[21580dd]1058
[7c8267b]1059 if (!header->acknowledge)
1060 return tcp_release_and_return(packet, EINVAL);
[21580dd]1061
[fb04cba8]1062 /* Process acknowledgement */
[7c8267b]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 =
[4e5c7ba]1072 (tcp_socket_data_t *) listening_socket->specific_data;
[7c8267b]1073 assert(listening_socket_data);
1074
[fb04cba8]1075 /* Queue the received packet */
[0578271]1076 rc = dyn_fifo_push(&listening_socket->accepted,
1077 (-1 * socket->socket_id), listening_socket_data->backlog);
1078 if (rc == EOK) {
[fb04cba8]1079 /* Notify the destination socket */
[7c8267b]1080 async_msg_5(socket->phone, NET_SOCKET_ACCEPTED,
[96b02eb9]1081 (sysarg_t) listening_socket->socket_id,
[7c8267b]1082 socket_data->data_fragment_size, TCP_HEADER_SIZE,
[96b02eb9]1083 0, (sysarg_t) socket->socket_id);
[7c8267b]1084
1085 fibril_rwlock_write_unlock(socket_data->local_lock);
1086 return EOK;
[21580dd]1087 }
[7c8267b]1088 }
[fb04cba8]1089 /* Send FIN */
[7c8267b]1090 socket_data->state = TCP_SOCKET_FIN_WAIT_1;
[21580dd]1091
[fb04cba8]1092 /* Create the notification packet */
[0578271]1093 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
1094 if (rc != EOK)
1095 return rc;
[21580dd]1096
[fb04cba8]1097 /* Send the packet */
[0578271]1098 rc = tcp_queue_packet(socket, socket_data, packet, 1);
1099 if (rc != EOK)
1100 return rc;
[21580dd]1101
[fb04cba8]1102 /* Flush packets */
[7c8267b]1103 packet = tcp_get_packets_to_send(socket, socket_data);
1104 fibril_rwlock_write_unlock(socket_data->local_lock);
1105 if (packet) {
[fb04cba8]1106 /* Send the packet */
[7c8267b]1107 tcp_send_packets(socket_data->device_id, packet);
[21580dd]1108 }
[7c8267b]1109
1110 return EOK;
[21580dd]1111}
1112
[88a1bb9]1113void tcp_process_acknowledgement(socket_core_t *socket,
[4e5c7ba]1114 tcp_socket_data_t *socket_data, tcp_header_t *header)
[7c8267b]1115{
[aadf01e]1116 size_t number;
1117 size_t length;
[46d4d9f]1118 packet_t *packet;
1119 packet_t *next;
1120 packet_t *acknowledged = NULL;
[aadf01e]1121 uint32_t old;
1122
1123 assert(socket);
1124 assert(socket_data);
1125 assert(socket->specific_data == socket_data);
1126 assert(header);
1127
[7c8267b]1128 if (!header->acknowledge)
1129 return;
1130
1131 number = ntohl(header->acknowledgement_number);
[fb04cba8]1132
1133 /* If more data acknowledged */
[7c8267b]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:
[fb04cba8]1144 /*
1145 * FIN acknowledged - release the socket in
1146 * another fibril.
1147 */
[7c8267b]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;
[21580dd]1155 }
[7c8267b]1156 }
[fb04cba8]1157
1158 /* Update the treshold if higher than set */
[7c8267b]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 }
[fb04cba8]1164
1165 /* Set new expected sequence number */
[7c8267b]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
[fb04cba8]1177 /* Add to acknowledged or release */
[7c8267b]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 }
[fb04cba8]1185
1186 /* Release acknowledged */
[7c8267b]1187 if (acknowledged) {
1188 pq_release_remote(tcp_globals.net_phone,
1189 packet_get_id(acknowledged));
1190 }
1191 return;
[fb04cba8]1192 /* If the same as the previous time */
[7c8267b]1193 }
[fb04cba8]1194
[7c8267b]1195 if (number == socket_data->expected) {
[fb04cba8]1196 /* Increase the counter */
[89e57cee]1197 socket_data->expected_count++;
[7c8267b]1198 if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) {
1199 socket_data->expected_count = 1;
[fb04cba8]1200 /* TODO retransmit lock */
[7c8267b]1201 //tcp_retransmit_packet(socket, socket_data, number);
[21580dd]1202 }
1203 }
1204}
1205
[f1938c6]1206/** Per-connection initialization
1207 *
1208 */
1209void tl_connection(void)
1210{
1211}
1212
[89e57cee]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.
[1bfd3d3]1220 * @return EOK on success.
1221 * @return ENOTSUP if the message is not known.
[89e57cee]1222 *
1223 * @see tcp_interface.h
1224 * @see IS_NET_TCP_MESSAGE()
1225 */
[f1938c6]1226int tl_message(ipc_callid_t callid, ipc_call_t *call,
[774e6d1a]1227 ipc_call_t *answer, size_t *answer_count)
[7c8267b]1228{
[aadf01e]1229 assert(call);
1230 assert(answer);
1231 assert(answer_count);
[21580dd]1232
[aadf01e]1233 *answer_count = 0;
[228e490]1234 switch (IPC_GET_IMETHOD(*call)) {
[7c8267b]1235 case IPC_M_CONNECT_TO_ME:
1236 return tcp_process_client_messages(callid, *call);
[21580dd]1237 }
[7c8267b]1238
[21580dd]1239 return ENOTSUP;
1240}
1241
[4e5c7ba]1242void tcp_refresh_socket_data(tcp_socket_data_t *socket_data)
[7c8267b]1243{
[aadf01e]1244 assert(socket_data);
[21580dd]1245
[aadf01e]1246 bzero(socket_data, sizeof(*socket_data));
[21580dd]1247 socket_data->state = TCP_SOCKET_INITIAL;
[ede63e4]1248 socket_data->device_id = DEVICE_INVALID_ID;
[21580dd]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
[4e5c7ba]1258void tcp_initialize_socket_data(tcp_socket_data_t *socket_data)
[7c8267b]1259{
[aadf01e]1260 assert(socket_data);
[21580dd]1261
[aadf01e]1262 tcp_refresh_socket_data(socket_data);
1263 fibril_mutex_initialize(&socket_data->operation.mutex);
1264 fibril_condvar_initialize(&socket_data->operation.condvar);
[ede63e4]1265 socket_data->data_fragment_size = MAX_TCP_FRAGMENT_SIZE;
[21580dd]1266}
1267
[7c8267b]1268int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
1269{
[aadf01e]1270 int res;
1271 bool keep_on_going = true;
1272 socket_cores_t local_sockets;
[774e6d1a]1273 int app_phone = IPC_GET_PHONE(call);
[7c8267b]1274 struct sockaddr *addr;
[6092b56e]1275 int socket_id;
[aadf01e]1276 size_t addrlen;
[e417b96]1277 size_t size;
[aadf01e]1278 fibril_rwlock_t lock;
1279 ipc_call_t answer;
[774e6d1a]1280 size_t answer_count;
[4e5c7ba]1281 tcp_socket_data_t *socket_data;
[88a1bb9]1282 socket_core_t *socket;
[f772bc55]1283 packet_dimension_t *packet_dimension;
[21580dd]1284
1285 /*
1286 * Accept the connection
1287 * - Answer the first IPC_M_CONNECT_ME_TO call.
1288 */
[2e99277]1289 res = EOK;
1290 answer_count = 0;
[21580dd]1291
[aadf01e]1292 socket_cores_initialize(&local_sockets);
1293 fibril_rwlock_initialize(&lock);
[21580dd]1294
[7c8267b]1295 while (keep_on_going) {
[2e99277]1296
[fb04cba8]1297 /* Answer the call */
[aadf01e]1298 answer_call(callid, res, &answer, answer_count);
[fb04cba8]1299 /* Refresh data */
[aadf01e]1300 refresh_answer(&answer, &answer_count);
[fb04cba8]1301 /* Get the next call */
[aadf01e]1302 callid = async_get_call(&call);
[21580dd]1303
[fb04cba8]1304 /* Process the call */
[228e490]1305 switch (IPC_GET_IMETHOD(call)) {
[7c8267b]1306 case IPC_M_PHONE_HUNGUP:
1307 keep_on_going = false;
1308 res = EHANGUP;
1309 break;
1310
1311 case NET_SOCKET:
1312 socket_data =
[4e5c7ba]1313 (tcp_socket_data_t *) malloc(sizeof(*socket_data));
[7c8267b]1314 if (!socket_data) {
1315 res = ENOMEM;
[21580dd]1316 break;
[7c8267b]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);
[21580dd]1330 break;
[7c8267b]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:
[7880d58]1347 res = async_data_write_accept((void **) &addr, false,
1348 0, 0, 0, &addrlen);
[7c8267b]1349 if (res != EOK)
[21580dd]1350 break;
[7c8267b]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) {
[4e5c7ba]1361 socket_data = (tcp_socket_data_t *)
[7c8267b]1362 socket->specific_data;
1363 assert(socket_data);
1364 socket_data->state = TCP_SOCKET_LISTEN;
[21580dd]1365 }
[7c8267b]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:
[7880d58]1385 res = async_data_write_accept((void **) &addr, false,
1386 0, 0, 0, &addrlen);
[7c8267b]1387 if (res != EOK)
[21580dd]1388 break;
[fb04cba8]1389 /*
1390 * The global lock may be released in the
1391 * tcp_connect_message() function.
1392 */
[7c8267b]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) {
[aadf01e]1398 fibril_rwlock_write_unlock(&lock);
[7c8267b]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) {
[aadf01e]1429 fibril_rwlock_write_unlock(&lock);
1430 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]1431 } else {
1432 answer_count = 2;
1433 }
1434 break;
1435
1436 case NET_SOCKET_SENDTO:
[7880d58]1437 res = async_data_write_accept((void **) &addr, false,
1438 0, 0, 0, &addrlen);
[7c8267b]1439 if (res != EOK)
[21580dd]1440 break;
[7c8267b]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) {
[aadf01e]1449 fibril_rwlock_write_unlock(&lock);
1450 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]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;
[21580dd]1504 }
1505 }
1506
[fb04cba8]1507 /* Release the application phone */
[a8a13d0]1508 ipc_hangup(app_phone);
1509
[21580dd]1510 printf("release\n");
[fb04cba8]1511 /* Release all local sockets */
[7c8267b]1512 socket_cores_release(tcp_globals.net_phone, &local_sockets,
1513 &tcp_globals.sockets, tcp_free_socket_data);
[21580dd]1514
1515 return EOK;
1516}
1517
[7c8267b]1518int tcp_timeout(void *data)
1519{
[f772bc55]1520 tcp_timeout_t *timeout = data;
[aadf01e]1521 int keep_write_lock = false;
[88a1bb9]1522 socket_core_t *socket;
[4e5c7ba]1523 tcp_socket_data_t *socket_data;
[21580dd]1524
[aadf01e]1525 assert(timeout);
[21580dd]1526
[fb04cba8]1527 /* Sleep the given timeout */
[aadf01e]1528 async_usleep(timeout->timeout);
[fb04cba8]1529 /* Lock the globals */
[7c8267b]1530 if (timeout->globals_read_only)
[aadf01e]1531 fibril_rwlock_read_lock(&tcp_globals.lock);
[7c8267b]1532 else
[aadf01e]1533 fibril_rwlock_write_lock(&tcp_globals.lock);
[7c8267b]1534
[fb04cba8]1535 /* Find the pending operation socket */
[7c8267b]1536 socket = socket_port_find(&tcp_globals.sockets, timeout->port,
1537 timeout->key, timeout->key_length);
[89e57cee]1538 if (!socket || (socket->socket_id != timeout->socket_id))
[7c8267b]1539 goto out;
1540
[4e5c7ba]1541 socket_data = (tcp_socket_data_t *) socket->specific_data;
[7c8267b]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) {
[fb04cba8]1548 /* Increase the timeout counter */
[89e57cee]1549 socket_data->timeout_count++;
[7c8267b]1550 if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) {
[fb04cba8]1551 /* TODO release as connection lost */
[7c8267b]1552 //tcp_refresh_socket_data(socket_data);
1553 fibril_rwlock_write_unlock(socket_data->local_lock);
1554 } else {
[fb04cba8]1555 /* Retransmit */
[7c8267b]1556// tcp_retransmit_packet(socket,
1557// socket_data, timeout->sequence_number);
1558 fibril_rwlock_write_unlock(socket_data->local_lock);
[21580dd]1559 }
[7c8267b]1560 } else {
1561 fibril_mutex_lock(&socket_data->operation.mutex);
[fb04cba8]1562 /* Set the timeout operation result if state not changed */
[7c8267b]1563 if (socket_data->state == timeout->state) {
1564 socket_data->operation.result = ETIMEOUT;
[fb04cba8]1565
1566 /* Notify the main fibril */
[7c8267b]1567 fibril_condvar_signal(&socket_data->operation.condvar);
[fb04cba8]1568
1569 /* Keep the global write lock */
[7c8267b]1570 keep_write_lock = true;
1571 } else {
[fb04cba8]1572 /*
1573 * Operation is ok, do nothing.
1574 * Unlocking from now on, so the unlocking
1575 * order does not matter.
1576 */
[7c8267b]1577 fibril_rwlock_write_unlock(socket_data->local_lock);
1578 }
1579 fibril_mutex_unlock(&socket_data->operation.mutex);
[21580dd]1580 }
[7c8267b]1581
1582out:
[fb04cba8]1583 /* Unlock only if no socket */
[7c8267b]1584 if (timeout->globals_read_only)
[aadf01e]1585 fibril_rwlock_read_unlock(&tcp_globals.lock);
[7c8267b]1586 else if (!keep_write_lock)
[fb04cba8]1587 /* Release if not desired */
[aadf01e]1588 fibril_rwlock_write_unlock(&tcp_globals.lock);
[7c8267b]1589
[fb04cba8]1590 /* Release the timeout structure */
[aadf01e]1591 free(timeout);
[21580dd]1592 return EOK;
1593}
1594
[7c8267b]1595int tcp_release_after_timeout(void *data)
1596{
[f772bc55]1597 tcp_timeout_t *timeout = data;
[88a1bb9]1598 socket_core_t *socket;
[4e5c7ba]1599 tcp_socket_data_t *socket_data;
[7c8267b]1600 fibril_rwlock_t *local_lock;
[21580dd]1601
[aadf01e]1602 assert(timeout);
[21580dd]1603
[fb04cba8]1604 /* Sleep the given timeout */
[aadf01e]1605 async_usleep(timeout->timeout);
[fb04cba8]1606
1607 /* Lock the globals */
[aadf01e]1608 fibril_rwlock_write_lock(&tcp_globals.lock);
[fb04cba8]1609
1610 /* Find the pending operation socket */
[7c8267b]1611 socket = socket_port_find(&tcp_globals.sockets, timeout->port,
1612 timeout->key, timeout->key_length);
[fb04cba8]1613
[7c8267b]1614 if (socket && (socket->socket_id == timeout->socket_id)) {
[4e5c7ba]1615 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]1616 assert(socket_data);
[7c8267b]1617 if (socket_data->local_sockets == timeout->local_sockets) {
[21580dd]1618 local_lock = socket_data->local_lock;
[aadf01e]1619 fibril_rwlock_write_lock(local_lock);
[7c8267b]1620 socket_destroy(tcp_globals.net_phone,
1621 timeout->socket_id, timeout->local_sockets,
1622 &tcp_globals.sockets, tcp_free_socket_data);
[aadf01e]1623 fibril_rwlock_write_unlock(local_lock);
[21580dd]1624 }
1625 }
[fb04cba8]1626
1627 /* Unlock the globals */
[aadf01e]1628 fibril_rwlock_write_unlock(&tcp_globals.lock);
[fb04cba8]1629
1630 /* Release the timeout structure */
[aadf01e]1631 free(timeout);
[fb04cba8]1632
[21580dd]1633 return EOK;
1634}
1635
[88a1bb9]1636void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t *
[fb04cba8]1637 socket_data, size_t sequence_number)
[7c8267b]1638{
[46d4d9f]1639 packet_t *packet;
1640 packet_t *copy;
[aadf01e]1641 size_t data_length;
[21580dd]1642
[aadf01e]1643 assert(socket);
1644 assert(socket_data);
1645 assert(socket->specific_data == socket_data);
[21580dd]1646
[fb04cba8]1647 /* Sent packet? */
[aadf01e]1648 packet = pq_find(socket_data->outgoing, sequence_number);
1649 printf("retransmit %d\n", packet_get_id(packet));
[7c8267b]1650 if (packet) {
[aadf01e]1651 pq_get_order(packet, NULL, &data_length);
[7c8267b]1652 copy = tcp_prepare_copy(socket, socket_data, packet,
1653 data_length, sequence_number);
[aadf01e]1654 fibril_rwlock_write_unlock(socket_data->local_lock);
[7c8267b]1655// printf("r send %d\n", packet_get_id(packet));
1656 if (copy)
[aadf01e]1657 tcp_send_packets(socket_data->device_id, copy);
[7c8267b]1658 } else {
[aadf01e]1659 fibril_rwlock_write_unlock(socket_data->local_lock);
[21580dd]1660 }
1661}
1662
[aaa3f33a]1663int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]1664 int backlog)
[7c8267b]1665{
[88a1bb9]1666 socket_core_t *socket;
[4e5c7ba]1667 tcp_socket_data_t *socket_data;
[21580dd]1668
[aadf01e]1669 assert(local_sockets);
[21580dd]1670
[7c8267b]1671 if (backlog < 0)
[aadf01e]1672 return EINVAL;
[7c8267b]1673
[fb04cba8]1674 /* Find the socket */
[aadf01e]1675 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]1676 if (!socket)
[aadf01e]1677 return ENOTSOCK;
[7c8267b]1678
[fb04cba8]1679 /* Get the socket specific data */
[4e5c7ba]1680 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]1681 assert(socket_data);
[fb04cba8]1682
1683 /* Set the backlog */
[21580dd]1684 socket_data->backlog = backlog;
[7c8267b]1685
[21580dd]1686 return EOK;
1687}
1688
[aaa3f33a]1689int tcp_connect_message(socket_cores_t *local_sockets, int socket_id,
[7c8267b]1690 struct sockaddr *addr, socklen_t addrlen)
1691{
[88a1bb9]1692 socket_core_t *socket;
[0578271]1693 int rc;
[21580dd]1694
[aadf01e]1695 assert(local_sockets);
1696 assert(addr);
1697 assert(addrlen > 0);
[21580dd]1698
[fb04cba8]1699 /* Find the socket */
[aadf01e]1700 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]1701 if (!socket)
[aadf01e]1702 return ENOTSOCK;
[7c8267b]1703
[0578271]1704 rc = tcp_connect_core(socket, local_sockets, addr, addrlen);
1705 if (rc != EOK) {
[aadf01e]1706 tcp_free_socket_data(socket);
[fb04cba8]1707 /* Unbind if bound */
[7c8267b]1708 if (socket->port > 0) {
1709 socket_ports_exclude(&tcp_globals.sockets,
1710 socket->port);
[21580dd]1711 socket->port = 0;
1712 }
1713 }
[0578271]1714 return rc;
[21580dd]1715}
1716
[aaa3f33a]1717int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets,
[7c8267b]1718 struct sockaddr *addr, socklen_t addrlen)
1719{
[4e5c7ba]1720 tcp_socket_data_t *socket_data;
[46d4d9f]1721 packet_t *packet;
[0578271]1722 int rc;
[21580dd]1723
[aadf01e]1724 assert(socket);
1725 assert(addr);
1726 assert(addrlen > 0);
[21580dd]1727
[fb04cba8]1728 /* Get the socket specific data */
[4e5c7ba]1729 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]1730 assert(socket_data);
1731 assert(socket->specific_data == socket_data);
[7c8267b]1732 if ((socket_data->state != TCP_SOCKET_INITIAL) &&
1733 ((socket_data->state != TCP_SOCKET_LISTEN) ||
1734 (socket->port <= 0)))
[21580dd]1735 return EINVAL;
[7c8267b]1736
[fb04cba8]1737 /* Get the destination port */
[0578271]1738 rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port);
1739 if (rc != EOK)
1740 return rc;
1741
[7c8267b]1742 if (socket->port <= 0) {
[fb04cba8]1743 /* Try to find a free port */
[0578271]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;
[fb04cba8]1749 /* Set the next port as the search starting port number */
[21580dd]1750 tcp_globals.last_used_port = socket->port;
1751 }
[7c8267b]1752
[0578271]1753 rc = ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,
[7c8267b]1754 addr, addrlen, &socket_data->device_id,
[0578271]1755 &socket_data->pseudo_header, &socket_data->headerlen);
1756 if (rc != EOK)
1757 return rc;
[21580dd]1758
[fb04cba8]1759 /* Create the notification packet */
[0578271]1760 rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0);
1761 if (rc != EOK)
1762 return rc;
[21580dd]1763
[fb04cba8]1764 /* Unlock the globals and wait for an operation */
[aadf01e]1765 fibril_rwlock_write_unlock(&tcp_globals.lock);
[21580dd]1766
1767 socket_data->addr = addr;
1768 socket_data->addrlen = addrlen;
[fb04cba8]1769
1770 /* Send the packet */
[0578271]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)) {
[21580dd]1776 socket_data->addr = NULL;
1777 socket_data->addrlen = 0;
[aadf01e]1778 fibril_rwlock_write_lock(&tcp_globals.lock);
[7c8267b]1779 } else {
[aadf01e]1780 packet = tcp_get_packets_to_send(socket, socket_data);
[7c8267b]1781 if (packet) {
[aadf01e]1782 fibril_mutex_lock(&socket_data->operation.mutex);
1783 fibril_rwlock_write_unlock(socket_data->local_lock);
[fb04cba8]1784
[3ac66f69]1785 socket_data->state = TCP_SOCKET_SYN_SENT;
1786
[fb04cba8]1787 /* Send the packet */
[aadf01e]1788 printf("connecting %d\n", packet_get_id(packet));
1789 tcp_send_packets(socket_data->device_id, packet);
[7c8267b]1790
[fb04cba8]1791 /* Wait for a reply */
[7c8267b]1792 fibril_condvar_wait(&socket_data->operation.condvar,
1793 &socket_data->operation.mutex);
[0578271]1794 rc = socket_data->operation.result;
1795 if (rc != EOK) {
[21580dd]1796 socket_data->addr = NULL;
1797 socket_data->addrlen = 0;
1798 }
[7c8267b]1799 } else {
[21580dd]1800 socket_data->addr = NULL;
1801 socket_data->addrlen = 0;
[0578271]1802 rc = EINTR;
[21580dd]1803 }
1804 }
1805
[aadf01e]1806 fibril_mutex_unlock(&socket_data->operation.mutex);
[0578271]1807 return rc;
[21580dd]1808}
1809
[88a1bb9]1810int tcp_queue_prepare_packet(socket_core_t *socket,
[46d4d9f]1811 tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length)
[7c8267b]1812{
[4e5c7ba]1813 tcp_header_t *header;
[0578271]1814 int rc;
[21580dd]1815
[aadf01e]1816 assert(socket);
1817 assert(socket_data);
1818 assert(socket->specific_data == socket_data);
[21580dd]1819
[fb04cba8]1820 /* Get TCP header */
[4e5c7ba]1821 header = (tcp_header_t *) packet_get_data(packet);
[7c8267b]1822 if (!header)
[aadf01e]1823 return NO_DATA;
[7c8267b]1824
[aadf01e]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);
[7c8267b]1828
[0578271]1829 rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr,
1830 socket_data->addrlen);
1831 if (rc != EOK)
[aadf01e]1832 return tcp_release_and_return(packet, EINVAL);
[7c8267b]1833
[fb04cba8]1834 /* Remember the outgoing FIN */
[7c8267b]1835 if (header->finalize)
[21580dd]1836 socket_data->fin_outgoing = socket_data->next_outgoing;
[7c8267b]1837
[21580dd]1838 return EOK;
1839}
1840
[88a1bb9]1841int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data,
[46d4d9f]1842 packet_t *packet, size_t data_length)
[7c8267b]1843{
[0578271]1844 int rc;
[21580dd]1845
[aadf01e]1846 assert(socket);
1847 assert(socket_data);
1848 assert(socket->specific_data == socket_data);
[21580dd]1849
[0578271]1850 rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length);
1851 if (rc != EOK)
1852 return rc;
[21580dd]1853
[0578271]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);
[7c8267b]1858
[21580dd]1859 socket_data->next_outgoing += data_length;
1860 return EOK;
1861}
1862
[46d4d9f]1863packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t *
[fb04cba8]1864 socket_data)
[7c8267b]1865{
[46d4d9f]1866 packet_t *packet;
1867 packet_t *copy;
1868 packet_t *sending = NULL;
1869 packet_t *previous = NULL;
[aadf01e]1870 size_t data_length;
[0578271]1871 int rc;
[21580dd]1872
[aadf01e]1873 assert(socket);
1874 assert(socket_data);
1875 assert(socket->specific_data == socket_data);
[21580dd]1876
[aadf01e]1877 packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1);
[7c8267b]1878 while (packet) {
[aadf01e]1879 pq_get_order(packet, NULL, &data_length);
[7c8267b]1880
[fb04cba8]1881 /*
1882 * Send only if fits into the window, respecting the possible
1883 * overflow.
1884 */
[7c8267b]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)))
[21580dd]1889 break;
[7c8267b]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;
[0578271]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 }
[7c8267b]1905 }
1906
1907 previous = copy;
1908 packet = pq_next(packet);
[fb04cba8]1909
1910 /* Overflow occurred? */
[89e57cee]1911 if (!packet &&
[7c8267b]1912 (socket_data->last_outgoing > socket_data->next_outgoing)) {
1913 printf("gpts overflow\n");
[fb04cba8]1914 /* Continue from the beginning */
[7c8267b]1915 packet = socket_data->outgoing;
[21580dd]1916 }
[7c8267b]1917 socket_data->last_outgoing += data_length;
[21580dd]1918 }
[7c8267b]1919
[21580dd]1920 return sending;
1921}
1922
[46d4d9f]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)
[7c8267b]1925{
[4e5c7ba]1926 tcp_header_t *header;
[aadf01e]1927 uint32_t checksum;
[0578271]1928 int rc;
[21580dd]1929
[aadf01e]1930 assert(socket);
1931 assert(socket_data);
1932 assert(socket->specific_data == socket_data);
[21580dd]1933
[fb04cba8]1934 /* Adjust the pseudo header */
[0578271]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) {
[14f1db0]1938 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[21580dd]1939 return NULL;
1940 }
1941
[fb04cba8]1942 /* Get the header */
[4e5c7ba]1943 header = (tcp_header_t *) packet_get_data(packet);
[7c8267b]1944 if (!header) {
[14f1db0]1945 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[21580dd]1946 return NULL;
1947 }
[aadf01e]1948 assert(ntohl(header->sequence_number) == sequence_number);
[21580dd]1949
[fb04cba8]1950 /* Adjust the header */
[7c8267b]1951 if (socket_data->next_incoming) {
1952 header->acknowledgement_number =
1953 htonl(socket_data->next_incoming);
[21580dd]1954 header->acknowledge = 1;
1955 }
[aadf01e]1956 header->window = htons(socket_data->window);
[21580dd]1957
[fb04cba8]1958 /* Checksum */
[21580dd]1959 header->checksum = 0;
[7c8267b]1960 checksum = compute_checksum(0, socket_data->pseudo_header,
1961 socket_data->headerlen);
[0578271]1962 checksum = compute_checksum(checksum,
1963 (uint8_t *) packet_get_data(packet),
[7c8267b]1964 packet_get_data_length(packet));
[aadf01e]1965 header->checksum = htons(flip_checksum(compact_checksum(checksum)));
[7c8267b]1966
[fb04cba8]1967 /* Prepare the packet */
[0578271]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 }
[fb04cba8]1973
[0578271]1974 rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,
1975 sequence_number, socket_data->state, socket_data->timeout, true);
1976 if (rc != EOK) {
[14f1db0]1977 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[21580dd]1978 return NULL;
1979 }
[7c8267b]1980
[21580dd]1981 return packet;
1982}
1983
[46d4d9f]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)
[7c8267b]1986{
[46d4d9f]1987 packet_t *copy;
[21580dd]1988
[aadf01e]1989 assert(socket);
1990 assert(socket_data);
1991 assert(socket->specific_data == socket_data);
[21580dd]1992
[fb04cba8]1993 /* Make a copy of the packet */
[aadf01e]1994 copy = packet_get_copy(tcp_globals.net_phone, packet);
[7c8267b]1995 if (!copy)
[aadf01e]1996 return NULL;
[21580dd]1997
[7c8267b]1998 return tcp_send_prepare_packet(socket, socket_data, copy, data_length,
1999 sequence_number);
[21580dd]2000}
2001
[46d4d9f]2002void tcp_send_packets(device_id_t device_id, packet_t *packet)
[7c8267b]2003{
[46d4d9f]2004 packet_t *next;
[21580dd]2005
[7c8267b]2006 while (packet) {
[aadf01e]2007 next = pq_detach(packet);
[7c8267b]2008 ip_send_msg(tcp_globals.ip_phone, device_id, packet,
2009 SERVICE_TCP, 0);
[21580dd]2010 packet = next;
2011 }
2012}
2013
[88a1bb9]2014void tcp_prepare_operation_header(socket_core_t *socket,
[4e5c7ba]2015 tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize,
[7c8267b]2016 int finalize)
2017{
[aadf01e]2018 assert(socket);
2019 assert(socket_data);
2020 assert(socket->specific_data == socket_data);
2021 assert(header);
[21580dd]2022
[aadf01e]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));
[21580dd]2027 header->synchronize = synchronize;
2028 header->finalize = finalize;
2029}
2030
[fb04cba8]2031int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t),
[88a1bb9]2032 socket_core_t *socket, tcp_socket_data_t *socket_data,
[7c8267b]2033 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout,
2034 int globals_read_only)
2035{
[f772bc55]2036 tcp_timeout_t *operation_timeout;
[aadf01e]2037 fid_t fibril;
[21580dd]2038
[aadf01e]2039 assert(socket);
2040 assert(socket_data);
2041 assert(socket->specific_data == socket_data);
[21580dd]2042
[fb04cba8]2043 /* Prepare the timeout with key bundle structure */
[7c8267b]2044 operation_timeout = malloc(sizeof(*operation_timeout) +
2045 socket->key_length + 1);
2046 if (!operation_timeout)
[aadf01e]2047 return ENOMEM;
[7c8267b]2048
[aadf01e]2049 bzero(operation_timeout, sizeof(*operation_timeout));
[21580dd]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
[fb04cba8]2058 /* Copy the key */
[61bfc370]2059 operation_timeout->key = ((uint8_t *) operation_timeout) +
[7c8267b]2060 sizeof(*operation_timeout);
[21580dd]2061 operation_timeout->key_length = socket->key_length;
[aadf01e]2062 memcpy(operation_timeout->key, socket->key, socket->key_length);
2063 operation_timeout->key[operation_timeout->key_length] = '\0';
[21580dd]2064
[fb04cba8]2065 /* Prepare the timeouting thread */
[aadf01e]2066 fibril = fibril_create(timeout_function, operation_timeout);
[7c8267b]2067 if (!fibril) {
[aadf01e]2068 free(operation_timeout);
[be942bc]2069 return ENOMEM;
[21580dd]2070 }
[be942bc]2071
[7c8267b]2072// fibril_mutex_lock(&socket_data->operation.mutex);
[fb04cba8]2073 /* Start the timeout fibril */
[aadf01e]2074 fibril_add_ready(fibril);
[21580dd]2075 //socket_data->state = state;
2076 return EOK;
2077}
2078
[aaa3f33a]2079int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]2080 int flags, size_t *addrlen)
[7c8267b]2081{
[88a1bb9]2082 socket_core_t *socket;
[4e5c7ba]2083 tcp_socket_data_t *socket_data;
[aadf01e]2084 int packet_id;
[46d4d9f]2085 packet_t *packet;
[aadf01e]2086 size_t length;
[0578271]2087 int rc;
[21580dd]2088
[aadf01e]2089 assert(local_sockets);
[21580dd]2090
[fb04cba8]2091 /* Find the socket */
[aadf01e]2092 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2093 if (!socket)
[aadf01e]2094 return ENOTSOCK;
[7c8267b]2095
[fb04cba8]2096 /* Get the socket specific data */
[7c8267b]2097 if (!socket->specific_data)
[aadf01e]2098 return NO_DATA;
[7c8267b]2099
[4e5c7ba]2100 socket_data = (tcp_socket_data_t *) socket->specific_data;
[21580dd]2101
[fb04cba8]2102 /* Check state */
[7c8267b]2103 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
2104 (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
[21580dd]2105 return ENOTCONN;
2106
[fb04cba8]2107 /* Send the source address if desired */
[7c8267b]2108 if (addrlen) {
[0578271]2109 rc = data_reply(socket_data->addr, socket_data->addrlen);
2110 if (rc != EOK)
2111 return rc;
[aadf01e]2112 *addrlen = socket_data->addrlen;
[21580dd]2113 }
2114
[fb04cba8]2115 /* Get the next received packet */
[aadf01e]2116 packet_id = dyn_fifo_value(&socket->received);
[7c8267b]2117 if (packet_id < 0)
[aadf01e]2118 return NO_DATA;
[7c8267b]2119
[0578271]2120 rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id);
2121 if (rc != EOK)
2122 return rc;
[21580dd]2123
[fb04cba8]2124 /* Reply the packets */
[0578271]2125 rc = socket_reply_packets(packet, &length);
2126 if (rc != EOK)
2127 return rc;
[21580dd]2128
[fb04cba8]2129 /* Release the packet */
[aadf01e]2130 dyn_fifo_pop(&socket->received);
[14f1db0]2131 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[fb04cba8]2132
2133 /* Return the total length */
[aadf01e]2134 return (int) length;
[21580dd]2135}
2136
[aaa3f33a]2137int tcp_send_message(socket_cores_t *local_sockets, int socket_id,
[fb04cba8]2138 int fragments, size_t *data_fragment_size, int flags)
[7c8267b]2139{
[88a1bb9]2140 socket_core_t *socket;
[4e5c7ba]2141 tcp_socket_data_t *socket_data;
[f772bc55]2142 packet_dimension_t *packet_dimension;
[46d4d9f]2143 packet_t *packet;
[aadf01e]2144 size_t total_length;
[4e5c7ba]2145 tcp_header_t *header;
[aadf01e]2146 int index;
2147 int result;
[0578271]2148 int rc;
[21580dd]2149
[aadf01e]2150 assert(local_sockets);
2151 assert(data_fragment_size);
[21580dd]2152
[fb04cba8]2153 /* Find the socket */
[aadf01e]2154 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2155 if (!socket)
[aadf01e]2156 return ENOTSOCK;
[7c8267b]2157
[fb04cba8]2158 /* Get the socket specific data */
[7c8267b]2159 if (!socket->specific_data)
[aadf01e]2160 return NO_DATA;
[7c8267b]2161
[4e5c7ba]2162 socket_data = (tcp_socket_data_t *) socket->specific_data;
[21580dd]2163
[fb04cba8]2164 /* Check state */
[7c8267b]2165 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) &&
2166 (socket_data->state != TCP_SOCKET_CLOSE_WAIT))
[21580dd]2167 return ENOTCONN;
2168
[0578271]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;
[21580dd]2173
[7c8267b]2174 *data_fragment_size =
2175 ((packet_dimension->content < socket_data->data_fragment_size) ?
2176 packet_dimension->content : socket_data->data_fragment_size);
[21580dd]2177
[89e57cee]2178 for (index = 0; index < fragments; index++) {
[fb04cba8]2179 /* Read the data fragment */
[7c8267b]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)
[aadf01e]2184 return result;
[7c8267b]2185
[aadf01e]2186 total_length = (size_t) result;
[fb04cba8]2187
2188 /* Prefix the TCP header */
[aadf01e]2189 header = PACKET_PREFIX(packet, tcp_header_t);
[7c8267b]2190 if (!header)
[aadf01e]2191 return tcp_release_and_return(packet, ENOMEM);
[7c8267b]2192
[aadf01e]2193 tcp_prepare_operation_header(socket, socket_data, header, 0, 0);
[86f6121]2194 rc = tcp_queue_packet(socket, socket_data, packet, total_length);
[0578271]2195 if (rc != EOK)
2196 return rc;
[21580dd]2197 }
2198
[fb04cba8]2199 /* Flush packets */
[aadf01e]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);
[7c8267b]2203
2204 if (packet) {
[fb04cba8]2205 /* Send the packet */
[aadf01e]2206 tcp_send_packets(socket_data->device_id, packet);
[21580dd]2207 }
2208
2209 return EOK;
2210}
2211
[7c8267b]2212int
[aaa3f33a]2213tcp_close_message(socket_cores_t *local_sockets, int socket_id)
[7c8267b]2214{
[88a1bb9]2215 socket_core_t *socket;
[4e5c7ba]2216 tcp_socket_data_t *socket_data;
[46d4d9f]2217 packet_t *packet;
[0578271]2218 int rc;
[21580dd]2219
[fb04cba8]2220 /* Find the socket */
[aadf01e]2221 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2222 if (!socket)
[aadf01e]2223 return ENOTSOCK;
[7c8267b]2224
[fb04cba8]2225 /* Get the socket specific data */
[4e5c7ba]2226 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]2227 assert(socket_data);
[21580dd]2228
[fb04cba8]2229 /* Check state */
[7c8267b]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:
[fb04cba8]2242 /* Just destroy */
[0578271]2243 rc = socket_destroy(tcp_globals.net_phone, socket_id,
[7c8267b]2244 local_sockets, &tcp_globals.sockets,
[0578271]2245 tcp_free_socket_data);
2246 if (rc == EOK) {
[7c8267b]2247 fibril_rwlock_write_unlock(socket_data->local_lock);
2248 fibril_rwlock_write_unlock(&tcp_globals.lock);
2249 }
[0578271]2250 return rc;
[21580dd]2251 }
[7c8267b]2252
[fb04cba8]2253 /*
2254 * Send FIN.
2255 * TODO should I wait to complete?
2256 */
[21580dd]2257
[fb04cba8]2258 /* Create the notification packet */
[0578271]2259 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1);
2260 if (rc != EOK)
2261 return rc;
[21580dd]2262
[fb04cba8]2263 /* Send the packet */
[0578271]2264 rc = tcp_queue_packet(socket, socket_data, packet, 1);
2265 if (rc != EOK)
2266 return rc;
[21580dd]2267
[fb04cba8]2268 /* Flush packets */
[aadf01e]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);
[7c8267b]2272
2273 if (packet) {
[fb04cba8]2274 /* Send the packet */
[aadf01e]2275 tcp_send_packets(socket_data->device_id, packet);
[21580dd]2276 }
[7c8267b]2277
[21580dd]2278 return EOK;
2279}
2280
[46d4d9f]2281int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket,
[4e5c7ba]2282 tcp_socket_data_t *socket_data, int synchronize, int finalize)
[7c8267b]2283{
[f772bc55]2284 packet_dimension_t *packet_dimension;
[4e5c7ba]2285 tcp_header_t *header;
[0578271]2286 int rc;
[21580dd]2287
[aadf01e]2288 assert(packet);
[21580dd]2289
[fb04cba8]2290 /* Get the device packet dimension */
[0578271]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;
[7c8267b]2295
[fb04cba8]2296 /* Get a new packet */
[7c8267b]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)
[aadf01e]2302 return ENOMEM;
[7c8267b]2303
[fb04cba8]2304 /* Allocate space in the packet */
[aadf01e]2305 header = PACKET_SUFFIX(*packet, tcp_header_t);
[7c8267b]2306 if (!header)
[aadf01e]2307 tcp_release_and_return(*packet, ENOMEM);
[21580dd]2308
[7c8267b]2309 tcp_prepare_operation_header(socket, socket_data, header, synchronize,
2310 finalize);
2311
[21580dd]2312 return EOK;
2313}
2314
[aaa3f33a]2315int tcp_accept_message(socket_cores_t *local_sockets, int socket_id,
[89e57cee]2316 int new_socket_id, size_t *data_fragment_size, size_t *addrlen)
[7c8267b]2317{
[88a1bb9]2318 socket_core_t *accepted;
2319 socket_core_t *socket;
[4e5c7ba]2320 tcp_socket_data_t *socket_data;
[f772bc55]2321 packet_dimension_t *packet_dimension;
[0578271]2322 int rc;
[21580dd]2323
[aadf01e]2324 assert(local_sockets);
2325 assert(data_fragment_size);
2326 assert(addrlen);
[21580dd]2327
[fb04cba8]2328 /* Find the socket */
[aadf01e]2329 socket = socket_cores_find(local_sockets, socket_id);
[7c8267b]2330 if (!socket)
[aadf01e]2331 return ENOTSOCK;
[7c8267b]2332
[fb04cba8]2333 /* Get the socket specific data */
[4e5c7ba]2334 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]2335 assert(socket_data);
[21580dd]2336
[fb04cba8]2337 /* Check state */
[7c8267b]2338 if (socket_data->state != TCP_SOCKET_LISTEN)
[21580dd]2339 return EINVAL;
2340
[7c8267b]2341 do {
[aadf01e]2342 socket_id = dyn_fifo_value(&socket->accepted);
[7c8267b]2343 if (socket_id < 0)
[aadf01e]2344 return ENOTSOCK;
[d510c0fe]2345 socket_id *= -1;
[21580dd]2346
[aadf01e]2347 accepted = socket_cores_find(local_sockets, socket_id);
[7c8267b]2348 if (!accepted)
[aadf01e]2349 return ENOTSOCK;
[7c8267b]2350
[fb04cba8]2351 /* Get the socket specific data */
[4e5c7ba]2352 socket_data = (tcp_socket_data_t *) accepted->specific_data;
[aadf01e]2353 assert(socket_data);
[fb04cba8]2354 /* TODO can it be in another state? */
[7c8267b]2355 if (socket_data->state == TCP_SOCKET_ESTABLISHED) {
[0578271]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;
[aadf01e]2365 *addrlen = socket_data->addrlen;
[7c8267b]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) {
[0578271]2374 rc = socket_cores_update(local_sockets,
2375 accepted->socket_id, new_socket_id);
2376 if (rc != EOK)
2377 return rc;
[ede63e4]2378 accepted->socket_id = new_socket_id;
2379 }
[21580dd]2380 }
[aadf01e]2381 dyn_fifo_pop(&socket->accepted);
[7c8267b]2382 } while (socket_data->state != TCP_SOCKET_ESTABLISHED);
2383
[aadf01e]2384 printf("ret accept %d\n", accepted->socket_id);
[21580dd]2385 return accepted->socket_id;
2386}
2387
[88a1bb9]2388void tcp_free_socket_data(socket_core_t *socket)
[7c8267b]2389{
[4e5c7ba]2390 tcp_socket_data_t *socket_data;
[21580dd]2391
[aadf01e]2392 assert(socket);
[21580dd]2393
[aadf01e]2394 printf("destroy_socket %d\n", socket->socket_id);
[21580dd]2395
[fb04cba8]2396 /* Get the socket specific data */
[4e5c7ba]2397 socket_data = (tcp_socket_data_t *) socket->specific_data;
[aadf01e]2398 assert(socket_data);
[fb04cba8]2399
2400 /* Free the pseudo header */
[7c8267b]2401 if (socket_data->pseudo_header) {
2402 if (socket_data->headerlen) {
[21580dd]2403 printf("d pseudo\n");
[aadf01e]2404 free(socket_data->pseudo_header);
[21580dd]2405 socket_data->headerlen = 0;
2406 }
2407 socket_data->pseudo_header = NULL;
2408 }
[fb04cba8]2409
[21580dd]2410 socket_data->headerlen = 0;
[fb04cba8]2411
2412 /* Free the address */
[7c8267b]2413 if (socket_data->addr) {
2414 if (socket_data->addrlen) {
[21580dd]2415 printf("d addr\n");
[aadf01e]2416 free(socket_data->addr);
[21580dd]2417 socket_data->addrlen = 0;
2418 }
2419 socket_data->addr = NULL;
2420 }
2421 socket_data->addrlen = 0;
2422}
2423
[89e57cee]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 */
[46d4d9f]2430int tcp_release_and_return(packet_t *packet, int result)
[7c8267b]2431{
[14f1db0]2432 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));
[21580dd]2433 return result;
2434}
2435
[014dd57b]2436/** Process IPC messages from the IP module
[849ed54]2437 *
[014dd57b]2438 * @param[in] iid Message identifier.
2439 * @param[in,out] icall Message parameters.
[849ed54]2440 *
2441 */
[014dd57b]2442static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall)
[849ed54]2443{
[014dd57b]2444 packet_t *packet;
2445 int rc;
2446
[7c8267b]2447 while (true) {
[014dd57b]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);
[849ed54]2463 }
2464}
2465
[014dd57b]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.
[849ed54]2472 *
2473 */
[014dd57b]2474int tl_initialize(int net_phone)
[849ed54]2475{
[014dd57b]2476 fibril_rwlock_initialize(&tcp_globals.lock);
2477 fibril_rwlock_write_lock(&tcp_globals.lock);
2478
2479 tcp_globals.net_phone = net_phone;
2480
[f1938c6]2481 tcp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT);
[014dd57b]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;
[7c8267b]2492
[014dd57b]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);
[0578271]2503 return rc;
[849ed54]2504}
2505
[014dd57b]2506int main(int argc, char *argv[])
2507{
2508 return tl_module_start(SERVICE_TCP);
2509}
2510
[21580dd]2511/** @}
2512 */
Note: See TracBrowser for help on using the repository browser.