Subversion Repositories HelenOS

Rev

Rev 4707 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.
  35.  */
  36.  
  37. #ifndef __NET_TCP_H__
  38. #define __NET_TCP_H__
  39.  
  40. #include <fibril_sync.h>
  41.  
  42. #include "../../structures/packet/packet.h"
  43.  
  44. #include "../../include/device.h"
  45.  
  46. #include "../../socket/socket_core.h"
  47.  
  48. #include "../tl_common.h"
  49.  
  50. /** Type definition of the TCP global data.
  51.  *  @see tcp_globals
  52.  */
  53. typedef struct tcp_globals  tcp_globals_t;
  54.  
  55. /** Type definition of the TCP socket specific data.
  56.  *  @see tcp_socket_data
  57.  */
  58. typedef struct tcp_socket_data  tcp_socket_data_t;
  59.  
  60. /** Type definition of the TCP socket specific data pointer.
  61.  *  @see tcp_socket_data
  62.  */
  63. typedef tcp_socket_data_t * tcp_socket_data_ref;
  64.  
  65. /** Type definition of the TCP operation data.
  66.  *  @see tcp_operation
  67.  */
  68. typedef struct tcp_operation    tcp_operation_t;
  69.  
  70. /** Type definition of the TCP operation data pointer.
  71.  *  @see tcp_operation
  72.  */
  73. typedef tcp_operation_t *   tcp_operation_ref;
  74.  
  75. /** TCP socket state type definition.
  76.  *  @see tcp_socket_state
  77.  */
  78. typedef enum tcp_socket_state   tcp_socket_state_t;
  79.  
  80. /** Device packet dimensions.
  81.  *  Maps devices to the packet dimensions.
  82.  *  @see device.h
  83.  */
  84. DEVICE_MAP_DECLARE( packet_dimensions, packet_dimension_t );
  85.  
  86. /** TCP socket state.
  87.  */
  88. enum tcp_socket_state{
  89.     /** Initial.
  90.      *  Not connected or bound.
  91.      */
  92.     TCP_SOCKET_INITIAL,
  93.     /** Listening.
  94.      *  Awaiting a connection request from another TCP layer.
  95.      *  When SYN is received a new bound socket in the TCP_SOCKET_SYN_RECEIVED state should be created.
  96.      */
  97.     TCP_SOCKET_LISTEN,
  98.     /** Connecting issued.
  99.      *  A~SYN has been sent, and TCP is awaiting the response SYN.
  100.      *  Should continue to the TCP_SOCKET_ESTABLISHED state.
  101.      */
  102.     TCP_SOCKET_SYN_SENT,
  103.     /** Connecting received.
  104.      *  A~SYN has been received, a~SYN has been sent, and TCP is awaiting an ACK.
  105.      *  Should continue to the TCP_SOCKET_ESTABLISHED state.
  106.      */
  107.     TCP_SOCKET_SYN_RECEIVED,
  108.     /** Connected.
  109.      *  The three-way handshake has been completed.
  110.      */
  111.     TCP_SOCKET_ESTABLISHED,
  112.     /** Closing started.
  113.      *  The local application has issued a~CLOSE.
  114.      *  TCP has sent a~FIN, and is awaiting an ACK or a~FIN.
  115.      *  Should continue to the TCP_SOCKET_FIN_WAIT_2 state when an ACK is received.
  116.      *  Should continue to the TCP_SOCKET_CLOSING state when a~FIN is received.
  117.      */
  118.     TCP_SOCKET_FIN_WAIT_1,
  119.     /** Closing confirmed.
  120.      *  A~FIN has been sent, and an ACK received.
  121.      *  TCP is awaiting a~FIN from the remote TCP layer.
  122.      *  Should continue to the TCP_SOCKET_CLOSING state.
  123.      */
  124.     TCP_SOCKET_FIN_WAIT_2,
  125.     /** Closing.
  126.      *  A FIN has been sent, a FIN has been received, and an ACK has been sent.
  127.      *  TCP is awaiting an ACK for the FIN that was sent.
  128.      *  Should continue to the TCP_SOCKET_TIME_WAIT state.
  129.      */
  130.     TCP_SOCKET_CLOSING,
  131.     /** Closing received.
  132.      *  TCP has received a~FIN, and has sent an ACK.
  133.      *  It is awaiting a~close request from the local application before sending a~FIN.
  134.      *  Should continue to the TCP_SOCKET_SOCKET_LAST_ACK state.
  135.      */
  136.     TCP_SOCKET_CLOSE_WAIT,
  137.     /**
  138.      *  A~FIN has been received, and an ACK and a~FIN have been sent.
  139.      *  TCP is awaiting an ACK.
  140.      *  Should continue to the TCP_SOCKET_TIME_WAIT state.
  141.      */
  142.     TCP_SOCKET_LAST_ACK,
  143.     /** Closing finished.
  144.      *  FINs have been received and ACK’d, and TCP is waiting two MSLs to remove the connection from the table.
  145.      */
  146.     TCP_SOCKET_TIME_WAIT,
  147.     /** Closed.
  148.      *  Imaginary, this indicates that a~connection has been removed from the connection table.
  149.      */
  150.     TCP_SOCKET_CLOSED
  151. };
  152.  
  153. /** TCP operation data.
  154.  */
  155. struct tcp_operation{
  156.     /** Operation result.
  157.      */
  158.     int                 result;
  159.     /** Safety lock.
  160.      */
  161.     fibril_mutex_t      mutex;
  162.     /** Operation result signaling.
  163.      */
  164.     fibril_condvar_t    condvar;
  165. };
  166.  
  167. /** TCP socket specific data.
  168.  */
  169. struct tcp_socket_data{
  170.     /** TCP socket state.
  171.      */
  172.     tcp_socket_state_t  state;
  173.     /** Data fragment size.
  174.      *  Sending optimalization.
  175.      */
  176.     size_t          data_fragment_size;
  177.     /** Device identifier.
  178.      */
  179.     device_id_t     device_id;
  180.     /** Listening backlog.
  181.      *  The maximal number of connected but not yet accepted sockets.
  182.      */
  183.     int             backlog;
  184. //  /** Segment size.
  185. //   */
  186. //  size_t          segment_size;
  187.     /** Parent listening socket identifier.
  188.      *  Set if this socket is an accepted one.
  189.      */
  190.     int             listening_socket_id;
  191.     /** Treshold size in bytes.
  192.      */
  193.     size_t          treshold;
  194.     /** Window size in bytes.
  195.      */
  196.     size_t          window;
  197.     /** Acknowledgement timeout.
  198.      */
  199.     suseconds_t     timeout;
  200.     /** Last acknowledged byte.
  201.      */
  202.     uint32_t        acknowledged;
  203.     /** Next incoming sequence number.
  204.      */
  205.     uint32_t        next_incoming;
  206.     /** Incoming FIN.
  207.      */
  208.     uint32_t        fin_incoming;
  209.     /** Next outgoing sequence number.
  210.      */
  211.     uint32_t        next_outgoing;
  212.     /** Last outgoing sequence number.
  213.      */
  214.     uint32_t        last_outgoing;
  215.     /** Outgoing FIN.
  216.      */
  217.     uint32_t        fin_outgoing;
  218.     /** Expected sequence number by the remote host.
  219.      *  The sequence number the other host expects.
  220.      *  The notification is sent only upon a packet reecival.
  221.      */
  222.     uint32_t        expected;
  223.     /** Expected sequence number counter.
  224.      *  Counts the number of received notifications for the same sequence number.
  225.      */
  226.     int             expected_count;
  227.     /** Incoming packet queue.
  228.      *  Packets are buffered until received in the right order.
  229.      *  The packets are excluded after successfully read.
  230.      *  Packets are sorted by their starting byte.
  231.      *  Packets metric is set as their data length.
  232.      */
  233.     packet_t        incoming;
  234.     /** Outgoing packet queue.
  235.      *  Packets are buffered until acknowledged by the remote host in the right order.
  236.      *  The packets are excluded after acknowledged.
  237.      *  Packets are sorted by their starting byte.
  238.      *  Packets metric is set as their data length.
  239.      */
  240.     packet_t        outgoing;
  241.     /** IP pseudo header.
  242.      */
  243.     ip_pseudo_header_ref    pseudo_header;
  244.     /** IP pseudo header length.
  245.      */
  246.     size_t          headerlen;
  247.     /** Remote host address.
  248.      */
  249.     struct sockaddr *   addr;
  250.     /** Remote host address length.
  251.      */
  252.     socklen_t           addrlen;
  253.     /** Remote host port.
  254.      */
  255.     uint16_t            dest_port;
  256.     /** Parent local sockets.
  257.      */
  258.     socket_cores_ref    local_sockets;
  259.     /** Local sockets safety lock.
  260.      *  May be locked for writing while holding the global lock for reading when changing the local sockets only.
  261.      *  The global lock may to be locked only before locking the local lock.
  262.      *  The global lock may be locked more weakly than the local lock.
  263.      *  The global lock may be released before releasing the local lock.
  264.      *  @see tcp_globals:lock
  265.      */
  266.     fibril_rwlock_t *   local_lock;
  267.     /** Pending operation data.
  268.      */
  269.     tcp_operation_t     operation;
  270.     /** Timeouts in a row counter.
  271.      *  If TCP_MAX_TIMEOUTS is reached, the connection is lost.
  272.      */
  273.     int                 timeout_count;
  274. };
  275.  
  276. /** TCP global data.
  277.  */
  278. struct  tcp_globals{
  279.     /** Networking module phone.
  280.      */
  281.     int             net_phone;
  282.     /** IP module phone.
  283.      */
  284.     int             ip_phone;
  285.     /** ICMP module phone.
  286.      */
  287.     int             icmp_phone;
  288.     /** Last used free port.
  289.      */
  290.     int             last_used_port;
  291.     /** Active sockets.
  292.      */
  293.     socket_ports_t  sockets;
  294.     /** Device packet dimensions.
  295.      */
  296.     packet_dimensions_t dimensions;
  297.     /** Safety lock.
  298.      *  Write lock is used only for adding or removing socket ports.
  299.      */
  300.     fibril_rwlock_t lock;
  301. };
  302.  
  303. #endif
  304.  
  305. /** @}
  306.  */
  307.  
  308.