0,0 → 1,152 |
/* |
* Copyright (c) 2009 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
/** @addtogroup socket |
* @{ |
*/ |
|
/** @file |
*/ |
|
#include "../err.h" |
|
#include "../include/in.h" |
#include "../include/inet.h" |
|
#include "../include/socket.h" |
#include "../include/socket_errno.h" |
|
#include "../structures/int_map.h" |
#include "../structures/packet/packet.h" |
#include "../structures/packet/packet_client.h" |
|
#include "socket_core.h" |
|
INT_MAP_IMPLEMENT( socket_cores, socket_core_t ); |
|
INT_MAP_IMPLEMENT( socket_ports, socket_core_ref ); |
|
int socket_bind( socket_cores_ref local_sockets, socket_ports_ref global_sockets, int socket_id, void * addr, size_t addrlen ){ |
ERROR_DECLARE; |
|
socket_core_ref socket; |
socket_core_ref * socket_pointer; |
struct sockaddr * address; |
struct sockaddr_in * address_in; |
|
if( addrlen < sizeof( struct sockaddr )) return EINVAL; |
address = ( struct sockaddr * ) addr; |
switch( address->sa_family ){ |
case AF_INET: |
if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; |
address_in = ( struct sockaddr_in * ) addr; |
socket = socket_cores_find( local_sockets, socket_id ); |
if( ! socket ) return ENOTSOCK; |
socket_pointer = socket_ports_find( global_sockets, address_in->sin_port ); |
if( * socket_pointer ){ |
return EADDRINUSE; |
} |
socket_ports_exclude( global_sockets, socket->port ); |
socket_pointer = ( socket_core_ref * ) malloc( sizeof( * socket_pointer )); |
if( ! socket_pointer ) return ENOMEM; |
* socket_pointer = socket; |
ERROR_PROPAGATE( socket_ports_add( global_sockets, address_in->sin_port, socket_pointer )); |
socket->port = address_in->sin_port; |
break; |
// TODO IPv6 |
default: |
return EAFNOSUPPORT; |
} |
return EOK; |
} |
|
int socket_create( socket_cores_ref local_sockets, int app_phone ){ |
socket_core_ref socket; |
int res; |
|
socket = ( socket_core_ref ) malloc( sizeof( * socket )); |
if( ! socket ) return ENOMEM; |
socket->phone = app_phone; |
socket->port = -1; |
socket->device_id = -1; |
socket->peer_addr = NULL; |
socket->connect_size = 2; |
socket->receive_size = 8; |
socket->connected = ( int * ) malloc( sizeof( int ) * socket->connect_size ); |
if( ! socket->connected ){ |
free( socket ); |
return ENOMEM; |
} |
bzero( socket->connected, sizeof( int ) * socket->connect_size ); |
socket->received = ( int * ) malloc( sizeof( int ) * socket->receive_size ); |
if( ! socket->received ){ |
free( socket->connected ); |
free( socket ); |
return ENOMEM; |
} |
bzero( socket->received, sizeof( int ) * socket->receive_size ); |
socket->socket_id = socket_cores_count( local_sockets ) + 1; |
res = socket_cores_add( local_sockets, socket->socket_id, socket ); |
if( res < 0 ){ |
free( socket->received ); |
free( socket->connected ); |
free( socket ); |
return res; |
} |
return socket->socket_id; |
} |
|
int socket_destroy( int packet_phone, int socket_id, socket_cores_ref local_sockets, socket_ports_ref global_sockets ){ |
socket_core_ref socket; |
size_t i; |
|
socket = socket_cores_find( local_sockets, socket_id ); |
if( ! socket ) return ENOTSOCK; |
socket_ports_exclude( global_sockets, socket->port ); |
if( socket->connected ){ |
for( i = 0; i < socket->connect_size; ++ i ){ |
if( socket->connected[ i ] ){ |
socket_destroy( packet_phone, socket->connected[ i ], local_sockets, global_sockets ); |
} |
} |
} |
if( socket->received ){ |
for( i = 0; i < socket->receive_size; ++ i ){ |
if( socket->received[ i ] ){ |
pq_release( packet_phone, socket->received[ i ] ); |
} |
} |
} |
if( socket->connected ) free( socket->connected ); |
if( socket->received ) free( socket->received ); |
socket_cores_exclude( local_sockets, socket_id ); |
return EOK; |
} |
|
/** @} |
*/ |
Property changes: |
Added: svn:eol-style |
+native |
\ No newline at end of property |