Subversion Repositories HelenOS

Rev

Rev 4747 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 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 socket
  30.  *  @{
  31.  */
  32.  
  33. /** @file
  34.  *  Socket common core.
  35.  */
  36.  
  37. #ifndef __NET_SOCKET_CORE_H__
  38. #define __NET_SOCKET_CORE_H__
  39.  
  40. #include <sys/types.h>
  41.  
  42. #include "../include/in.h"
  43. #include "../include/device.h"
  44.  
  45. #include "../structures/generic_char_map.h"
  46. #include "../structures/dynamic_fifo.h"
  47. #include "../structures/int_map.h"
  48. #include "../structures/packet/packet.h"
  49.  
  50. /** Initial size of the received packet queue.
  51.  */
  52. #define SOCKET_INITIAL_RECEIVED_SIZE    4
  53.  
  54. /** Maximum size of the received packet queue.
  55.  */
  56. #define SOCKET_MAX_RECEIVED_SIZE        0
  57.  
  58. /** Initial size of the sockets for acceptance queue.
  59.  */
  60. #define SOCKET_INITIAL_ACCEPTED_SIZE    1
  61.  
  62. /** Maximum size of the sockets for acceptance queue.
  63.  */
  64. #define SOCKET_MAX_ACCEPTEDED_SIZE      0
  65.  
  66. /** Listening sockets' port map key.
  67.  */
  68. #define SOCKET_MAP_KEY_LISTENING    "L"
  69.  
  70. /** Type definition of the socket core.
  71.  *  @see socket_core
  72.  */
  73. typedef struct socket_core  socket_core_t;
  74.  
  75. /** Type definition of the socket core pointer.
  76.  *  @see socket_core
  77.  */
  78. typedef socket_core_t * socket_core_ref;
  79.  
  80. /** Type definition of the socket port.
  81.  *  @see socket_port
  82.  */
  83. typedef struct socket_port  socket_port_t;
  84.  
  85. /** Type definition of the socket port pointer.
  86.  *  @see socket_port
  87.  */
  88. typedef socket_port_t * socket_port_ref;
  89.  
  90. /** Socket core.
  91.  */
  92. struct socket_core{
  93.     /** Socket identifier.
  94.      */
  95.     int             socket_id;
  96.     /** Client application phone.
  97.      */
  98.     int             phone;
  99.     /** Bound port.
  100.      */
  101.     int             port;
  102.     /** Received packets queue.
  103.      */
  104.     dyn_fifo_t      received;
  105.     /** Sockets for acceptance queue.
  106.      */
  107.     dyn_fifo_t      accepted;
  108.     /** Protocol specific data.
  109.      */
  110.     void *          specific_data;
  111.     /** Socket ports map key.
  112.      */
  113.     const char *    key;
  114.     /** Length of the Socket ports map key.
  115.      */
  116.     size_t          key_length;
  117. };
  118.  
  119. /** Sockets map.
  120.  *  The key is the socket identifier.
  121.  */
  122. INT_MAP_DECLARE( socket_cores, socket_core_t );
  123.  
  124. /** Bount port sockets map.
  125.  *  The listening socket has the SOCKET_MAP_KEY_LISTENING key identifier whereas the other use the remote addresses.
  126.  */
  127. GENERIC_CHAR_MAP_DECLARE( socket_port_map, socket_core_ref );
  128.  
  129. /** Ports map.
  130.  *  The key is the port number.
  131.  */
  132. INT_MAP_DECLARE( socket_ports, socket_port_t );
  133.  
  134. /** Destroys local sockets.
  135.  *  Releases all buffered packets and calls the release function for each of the sockets.
  136.  *  @param[in] packet_phone The packet server phone to release buffered packets.
  137.  *  @param[in] local_sockets The local sockets to be destroyed.
  138.  *  @param[in,out] global_sockets The global sockets to be updated.
  139.  *  @param[in] socket_release The client release callback function.
  140.  */
  141. void    socket_cores_release( int packet_phone, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void ( * socket_release )( socket_core_ref socket ));
  142.  
  143. /** Binds the socket to the port.
  144.  *  The address port is used if set, a free port is used if not.
  145.  *  @param[in] local_sockets The local sockets to be searched.
  146.  *  @param[in,out] global_sockets The global sockets to be updated.
  147.  *  @param[in] socket_id The new socket identifier.
  148.  *  @param[in] addr The address to be bound to.
  149.  *  @param[in] addrlen The address length.
  150.  *  @param[in] free_ports_start The minimum free port.
  151.  *  @param[in] free_ports_end The maximum free port.
  152.  *  @param[in] last_used_port The last used free port.
  153.  *  @returns EOK on success.
  154.  *  @returns ENOTSOCK if the socket was not found.
  155.  *  @returns EAFNOSUPPORT if the address family is not supported.
  156.  *  @returns EADDRINUSE if the port is already in use.
  157.  *  @returns Other error codes as defined for the socket_bind_free_port() function.
  158.  *  @returns Other error codes as defined for the socket_bind_insert() function.
  159.  */
  160. int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen, int free_ports_start, int free_ports_end, int last_used_port );
  161.  
  162. /** Binds the socket to a free port.
  163.  *  The first free port is used.
  164.  *  @param[in,out] global_sockets The global sockets to be updated.
  165.  *  @param[in,out] socket The socket to be bound.
  166.  *  @param[in] free_ports_start The minimum free port.
  167.  *  @param[in] free_ports_end The maximum free port.
  168.  *  @param[in] last_used_port The last used free port.
  169.  *  @returns EOK on success.
  170.  *  @returns ENOTCONN if no free port was found.
  171.  *  @returns Other error codes as defined for the socket_bind_insert() function.
  172.  */
  173. int socket_bind_free_port( socket_ports_ref global_sockets, socket_core_ref socket, int free_ports_start, int free_ports_end, int last_used_port );
  174.  
  175. /** Creates a new socket.
  176.  *  @param[in,out] local_sockets The local sockets to be updated.
  177.  *  @param[in] app_phone The application phone.
  178.  *  @param[in] specific_data The socket specific data.
  179.  *  @param[out] socket_id The new socket identifier.
  180.  *  @returns EOK on success.
  181.  *  @returns EBADMEM if the socket_id parameter is NULL.
  182.  *  @returns ENOMEM if there is not enough memory left.
  183.  */
  184. int socket_create( socket_cores_ref local_sockets, int app_phone, void * specific_data, int * socket_id );
  185.  
  186. /** Destroys the socket.
  187.  *  If the socket is bound, the port is released.
  188.  *  Releases all buffered packets, calls the release function and removes the socket from the local sockets.
  189.  *  @param[in] packet_phone The packet server phone to release buffered packets.
  190.  *  @param[in] socket_id The socket identifier.
  191.  *  @param[in,out] local_sockets The local sockets to be updated.
  192.  *  @param[in,out] global_sockets The global sockets to be updated.
  193.  *  @param[in] socket_release The client release callback function.
  194.  *  @returns EOK on success.
  195.  *  @returns ENOTSOCK if the socket is not found.
  196.  */
  197. int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets, void ( * socket_release )( socket_core_ref socket ));
  198.  
  199. /** Replies the packet or the packet queue data to the application via the socket.
  200.  *  Uses the current message processing fibril.
  201.  *  @param[in] packet The packet to be transfered.
  202.  *  @param[out] length The total data length.
  203.  *  @returns EOK on success.
  204.  *  @returns EBADMEM if the length parameter is NULL.
  205.  *  @returns ENOMEM if there is not enough memory left.
  206.  *  @returns Other error codes as defined for the data_reply() function.
  207.  */
  208. int socket_reply_packets( packet_t packet, size_t * length );
  209.  
  210. /** Finds the bound port socket.
  211.  *  @param[in] global_sockets The global sockets to be searched.
  212.  *  @param[in] port The port number.
  213.  *  @param[in] key The socket key identifier.
  214.  *  @param[in] key_length The socket key length.
  215.  *  @returns The found socket.
  216.  *  @returns NULL if no socket was found.
  217.  */
  218. socket_core_ref socket_port_find( socket_ports_ref global_sockets, int port, const char * key, size_t key_length );
  219.  
  220. /** Releases the socket port.
  221.  *  If the socket is bound the port entry is released.
  222.  *  If there are no more port entries the port is release.
  223.  *  @param[in] global_sockets The global sockets to be updated.
  224.  *  @param[in] socket The socket to be unbound.
  225.  */
  226. void    socket_port_release( socket_ports_ref global_sockets, socket_core_ref socket );
  227.  
  228. /** Adds the socket to an already bound port.
  229.  *  @param[in] global_sockets The global sockets to be updated.
  230.  *  @param[in] port The port number to be bound to.
  231.  *  @param[in] socket The socket to be added.
  232.  *  @param[in] key The socket key identifier.
  233.  *  @param[in] key_length The socket key length.
  234.  *  @returns EOK on success.
  235.  *  @returns ENOENT if the port is not already used.
  236.  *  @returns Other error codes as defined for the socket_port_add_core() function.
  237.  */
  238. int socket_port_add( socket_ports_ref global_sockets, int port, socket_core_ref socket, const char * key, size_t key_length );
  239.  
  240. #endif
  241.  
  242. /** @}
  243.  */
  244.