Subversion Repositories HelenOS

Rev

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