Subversion Repositories HelenOS

Rev

Rev 4589 | Go to most recent revision | 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.  */
  35.  
  36. #include "../err.h"
  37.  
  38. #include "../include/in.h"
  39. #include "../include/inet.h"
  40.  
  41. #include "../include/socket.h"
  42. #include "../include/socket_errno.h"
  43.  
  44. #include "../structures/int_map.h"
  45. #include "../structures/packet/packet.h"
  46. #include "../structures/packet/packet_client.h"
  47.  
  48. #include "socket_core.h"
  49.  
  50. INT_MAP_IMPLEMENT( socket_cores, socket_core_t );
  51.  
  52. INT_MAP_IMPLEMENT( socket_ports, socket_core_ref );
  53.  
  54. int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen ){
  55.     ERROR_DECLARE;
  56.  
  57.     socket_core_ref         socket;
  58.     socket_core_ref *       socket_pointer;
  59.     struct sockaddr *       address;
  60.     struct sockaddr_in *    address_in;
  61.  
  62.     if( addrlen < sizeof( struct sockaddr )) return EINVAL;
  63.     address = ( struct sockaddr * ) addr;
  64.     switch( address->sa_family ){
  65.         case AF_INET:
  66.             if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
  67.             address_in = ( struct sockaddr_in * ) addr;
  68.             socket = socket_cores_find( local_sockets, socket_id );
  69.             if( ! socket ) return ENOTSOCK;
  70.             socket_pointer = socket_ports_find( global_sockets, address_in->sin_port );
  71.             if( * socket_pointer ){
  72.                 return EADDRINUSE;
  73.             }
  74.             socket_ports_exclude( global_sockets, socket->port );
  75.             socket_pointer = ( socket_core_ref * ) malloc( sizeof( * socket_pointer ));
  76.             if( ! socket_pointer ) return ENOMEM;
  77.             * socket_pointer = socket;
  78.             ERROR_PROPAGATE( socket_ports_add( global_sockets, address_in->sin_port, socket_pointer ));
  79.             socket->port = address_in->sin_port;
  80.             break;
  81.         // TODO IPv6
  82.         default:
  83.             return EAFNOSUPPORT;
  84.     }
  85.     return EOK;
  86. }
  87.  
  88. int socket_create( socket_cores_ref local_sockets, int app_phone ){
  89.     socket_core_ref socket;
  90.     int             res;
  91.  
  92.     socket = ( socket_core_ref ) malloc( sizeof( * socket ));
  93.     if( ! socket ) return ENOMEM;
  94.     socket->phone = app_phone;
  95.     socket->port = -1;
  96.     socket->device_id = -1;
  97.     socket->peer_addr = NULL;
  98.     socket->connect_size = 2;
  99.     socket->receive_size = 8;
  100.     socket->connected = ( int * ) malloc( sizeof( int ) * socket->connect_size );
  101.     if( ! socket->connected ){
  102.         free( socket );
  103.         return ENOMEM;
  104.     }
  105.     bzero( socket->connected, sizeof( int ) * socket->connect_size );
  106.     socket->received = ( int * ) malloc( sizeof( int ) * socket->receive_size );
  107.     if( ! socket->received ){
  108.         free( socket->connected );
  109.         free( socket );
  110.         return ENOMEM;
  111.     }
  112.     bzero( socket->received, sizeof( int ) * socket->receive_size );
  113.     socket->socket_id = socket_cores_count( local_sockets ) + 1;
  114.     res = socket_cores_add( local_sockets, socket->socket_id, socket );
  115.     if( res < 0 ){
  116.         free( socket->received );
  117.         free( socket->connected );
  118.         free( socket );
  119.         return res;
  120.     }
  121.     return socket->socket_id;
  122. }
  123.  
  124. int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets ){
  125.     socket_core_ref socket;
  126.     size_t          i;
  127.  
  128.     socket = socket_cores_find( local_sockets, socket_id );
  129.     if( ! socket ) return ENOTSOCK;
  130.     socket_ports_exclude( global_sockets, socket->port );
  131.     if( socket->connected ){
  132.         for( i = 0; i < socket->connect_size; ++ i ){
  133.             if( socket->connected[ i ] ){
  134.                 socket_destroy( packet_phone, socket->connected[ i ], local_sockets, global_sockets );
  135.             }
  136.         }
  137.     }
  138.     if( socket->received ){
  139.         for( i = 0; i < socket->receive_size; ++ i ){
  140.             if( socket->received[ i ] ){
  141.                 pq_release( packet_phone, socket->received[ i ] );
  142.             }
  143.         }
  144.     }
  145.     if( socket->connected ) free( socket->connected );
  146.     if( socket->received ) free( socket->received );
  147.     socket_cores_exclude( local_sockets, socket_id );
  148.     return EOK;
  149. }
  150.  
  151. /** @}
  152.  */
  153.