/*
* 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
* Socket application program interface (API) implementation.
* @see socket.h for more information.
* This is a part of the network application library.
*/
#include <assert.h>
#include <async.h>
#include <fibril_sync.h>
#include <ipc/services.h>
#include "../err.h"
#include "../modules.h"
#include "../include/in.h"
#include "../include/socket.h"
#include "../include/socket_errno.h"
#include "../structures/dynamic_fifo.h"
#include "../structures/int_map.h"
#include "socket_messages.h"
#define SOCKET_INITIAL_RECEIVED_SIZE 4
#define SOCKET_MAX_RECEIVED_SIZE 64
#define SOCKET_INITIAL_ACCEPTED_SIZE 1
#define SOCKET_MAX_ACCEPTED_SIZE 64
typedef struct socket socket_t;
typedef socket_t * socket_ref;
struct socket{
int socket_id;
int phone;
services_t service;
int header_size;
int data_fragment_size;
dyn_fifo_t received;
fibril_mutex_t receive_lock;
fibril_condvar_t receive_signal;
dyn_fifo_t accepted;
fibril_mutex_t accept_lock;
fibril_condvar_t accept_signal;
};
INT_MAP_DECLARE( sockets, socket_t );
static struct{
int tcp_phone;
int udp_phone;
sockets_ref sockets;
} socket_globals = { -1, -1, NULL };
INT_MAP_IMPLEMENT( sockets, socket_t );
static int socket_get_tcp_phone();
static int socket_get_tcp_phone();
static sockets_ref socket_get_sockets();
void socket_connection( ipc_callid_t iid, ipc_call_t * icall );
int socket_send_data( int socket_id, int message, ipcarg_t arg2, const void * data, size_t datalength );
void socket_initialize( socket_ref socket, int socket_id, int phone, services_t service );
void socket_destroy( socket_ref socket );
int recvfrom_core( int message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen );
int sendto_core( int message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen );
static int socket_get_tcp_phone(){
if( socket_globals.tcp_phone < 0 ){
socket_globals.tcp_phone = bind_service( SERVICE_TCP, 0, 0, SERVICE_TCP, socket_connection );
}
return socket_globals.tcp_phone;
}
static int socket_get_udp_phone(){
if( socket_globals.udp_phone < 0 ){
socket_globals.udp_phone = bind_service( SERVICE_UDP, 0, 0, SERVICE_UDP, socket_connection );
}
return socket_globals.udp_phone;
}
static sockets_ref socket_get_sockets(){
if( ! socket_globals.sockets ){
socket_globals.
sockets = ( sockets_ref
) malloc( sizeof( sockets_t
));
if( ! socket_globals.sockets ) return NULL;
if( sockets_initialize( socket_globals.sockets ) != EOK ){
free( socket_globals.
sockets );
socket_globals.sockets = NULL;
}
}
return socket_globals.sockets;
}
void socket_initialize( socket_ref socket, int socket_id, int phone, services_t service ){
socket->socket_id = socket_id;
socket->phone = phone;
socket->service = service;
dyn_fifo_initialize( & socket->received, SOCKET_INITIAL_RECEIVED_SIZE );
dyn_fifo_initialize( & socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE );
fibril_mutex_initialize( & socket->receive_lock );
fibril_condvar_initialize( & socket->receive_signal );
fibril_mutex_initialize( & socket->accept_lock );
fibril_condvar_initialize( & socket->accept_signal );
}
void socket_connection( ipc_callid_t iid, ipc_call_t * icall ){
ERROR_DECLARE;
ipc_callid_t callid;
ipc_call_t call;
socket_ref socket;
socket_ref new_socket;
while( true ){
callid = async_get_call( & call );
switch( IPC_GET_METHOD( call )){
case NET_SOCKET_RECEIVED:
// find the socket
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
fibril_mutex_lock( & socket->receive_lock );
// push the number of received packet fragments
if( ! ERROR_OCCURRED( dyn_fifo_push( & socket->received, SOCKET_GET_DATA_FRAGMENTS( & call ), SOCKET_MAX_RECEIVED_SIZE ))){
// signal the received packet
fibril_condvar_signal( & socket->receive_signal );
}
fibril_mutex_unlock( & socket->receive_lock );
break;
case NET_SOCKET_ACCEPTED:
// find the socket
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
// create a new scoket
new_socket
= ( socket_ref
) malloc( sizeof( socket_t
));
if( ! new_socket ){
ERROR_CODE = ENOMEM;
break;
}
socket_initialize( new_socket, SOCKET_GET_SOCKET_ID( & call ), socket->phone, socket->service );
ERROR_CODE = sockets_add( socket_get_sockets(), new_socket->socket_id, new_socket );
if( ERROR_CODE < 0 ){
}else{
// push the new socket identifier
fibril_mutex_lock( & socket->accept_lock );
if( ERROR_OCCURRED( dyn_fifo_push( & socket->accepted, new_socket->socket_id, SOCKET_MAX_ACCEPTED_SIZE ))){
sockets_exclude( socket_get_sockets(), new_socket->socket_id );
}else{
// signal the accepted socket
fibril_condvar_signal( & socket->accept_signal );
}
fibril_mutex_unlock( & socket->accept_lock );
ERROR_CODE = EOK;
}
break;
case NET_SOCKET_DATA_FRAGMENT_SIZE:
// find the socket
socket = sockets_find( socket_get_sockets(), SOCKET_GET_SOCKET_ID( & call ));
if( ! socket ){
ERROR_CODE = ENOTSOCK;
break;
}
// set the data fragment size
socket->data_fragment_size = SOCKET_GET_DATA_FRAGMENT_SIZE( & call );
ERROR_CODE = EOK;
break;
default:
ERROR_CODE = ENOTSUP;
}
ipc_answer_0( callid, ERROR_CODE );
}
}
int socket( int domain, int type, int protocol ){
ERROR_DECLARE;
socket_ref socket;
int phone;
int socket_id;
services_t service;
// find the appropriate service
switch( domain ){
case PF_INET:
switch( type ){
case SOCK_STREAM:
if( ! protocol ) protocol = IPPROTO_TCP;
switch( protocol ){
case IPPROTO_TCP:
phone = socket_get_tcp_phone();
service = SERVICE_TCP;
break;
default:
return EPROTONOSUPPORT;
}
break;
case SOCK_DGRAM:
if( ! protocol ) protocol = IPPROTO_UDP;
switch( protocol ){
case IPPROTO_UDP:
phone = socket_get_udp_phone();
service = SERVICE_UDP;
break;
default:
return EPROTONOSUPPORT;
}
break;
case SOCK_RAW:
default:
return ESOCKTNOSUPPORT;
}
break;
// TODO IPv6
default:
return EPFNOSUPPORT;
}
// create a new socket structure
socket
= ( socket_ref
) malloc( sizeof( socket_t
));
if( ! socket ) return ENOMEM;
if( ERROR_OCCURRED( dyn_fifo_initialize( & socket->received, SOCKET_INITIAL_RECEIVED_SIZE ))){
return ERROR_CODE;
}
if( ERROR_OCCURRED( dyn_fifo_initialize( & socket->accepted, SOCKET_INITIAL_ACCEPTED_SIZE ))){
dyn_fifo_destroy( & socket->received );
return ERROR_CODE;
}
// request a new socket
if( ERROR_OCCURRED( async_req_3_3( phone, NET_SOCKET, 0, 0, service, ( ipcarg_t * ) & socket_id, ( ipcarg_t * ) & socket->header_size, ( ipcarg_t * ) & socket->data_fragment_size ))){
dyn_fifo_destroy( & socket->received );
dyn_fifo_destroy( & socket->accepted );
return ERROR_CODE;
}
// finish the new socket initialization
socket_initialize( socket, socket_id, phone, service );
// store the new socket
ERROR_CODE = sockets_add( socket_get_sockets(), socket_id, socket );
if( ERROR_CODE < 0 ){
dyn_fifo_destroy( & socket->received );
dyn_fifo_destroy( & socket->accepted );
async_msg_3( phone, NET_SOCKET_CLOSE, socket_id, 0, service );
return ERROR_CODE;
}
return socket_id;
}
int socket_send_data( int socket_id, int message, ipcarg_t arg2, const void * data, size_t datalength ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// request the message
message_id = async_send_3( socket->phone, message, socket->socket_id, arg2, socket->service, NULL );
// send the address
ipc_data_write_start( socket->phone, data, datalength );
async_wait_for( message_id, & result );
return ( int ) result;
}
int bind( int socket_id, const struct sockaddr * my_addr, socklen_t addrlen ){
// send the address
return socket_send_data( socket_id, NET_SOCKET_BIND, 0, my_addr, addrlen );
}
int listen( int socket_id, int backlog ){
socket_ref socket;
if( backlog <= 0 ) return EINVAL;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// request listen backlog change
return async_req_3_0( socket->phone, NET_SOCKET_LISTEN, socket->socket_id, backlog, socket->service );
}
int accept( int socket_id, struct sockaddr * cliaddr, socklen_t * addrlen ){
socket_ref socket;
aid_t message_id;
int result;
if( ! cliaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
fibril_mutex_lock( & socket->accept_lock );
// wait for an accepted socket
while( dyn_fifo_value( & socket->accepted ) <= 0 ){
fibril_condvar_wait( & socket->accept_signal, & socket->accept_lock );
}
// request accept
message_id = async_send_3( socket->phone, NET_SOCKET_ACCEPT, socket->socket_id, dyn_fifo_value( & socket->accepted ), socket->service, NULL );
// read address
ipc_data_read_start( socket->phone, cliaddr, * addrlen );
async_wait_for( message_id, ( ipcarg_t * ) & result );
if( result > 0 ){
// dequeue the accepted apcket if successful
dyn_fifo_pop( & socket->accepted );
}
fibril_mutex_unlock( & socket->accept_lock );
return result;
}
int connect( int socket_id, const struct sockaddr * serv_addr, socklen_t addrlen ){
// send the address
return socket_send_data( socket_id, NET_SOCKET_CONNECT, 0, serv_addr, addrlen );
}
int closesocket( int socket_id ){
ERROR_DECLARE;
socket_ref socket;
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// request close
ERROR_PROPAGATE( async_req_3_0( socket->phone, NET_SOCKET_CLOSE, socket->socket_id, 0, socket->service ));
// free the socket structure
socket_destroy( socket );
return EOK;
}
void socket_destroy( socket_ref socket ){
int accepted_id;
// destroy all accepted sockets
while(( accepted_id = dyn_fifo_pop( & socket->accepted ))){
socket_destroy( sockets_find( socket_get_sockets(), accepted_id ));
}
dyn_fifo_destroy( & socket->received );
dyn_fifo_destroy( & socket->accepted );
sockets_exclude( socket_get_sockets(), socket->socket_id );
}
int send( int socket_id, void * data, size_t datalength, int flags ){
// without the address
return sendto_core( NET_SOCKET_SEND, socket_id, data, datalength, flags, NULL, 0 );
}
int sendto( int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen ){
if( ! toaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
// with the address
return sendto_core( NET_SOCKET_SENDTO, socket_id, data, datalength, flags, toaddr, addrlen );
}
int sendto_core( int message, int socket_id, const void * data, size_t datalength, int flags, const struct sockaddr * toaddr, socklen_t addrlen ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
int fragments;
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
// find socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// compute data fragment count
fragments = ( datalength + socket->header_size ) / socket->data_fragment_size;
if(( datalength + socket->header_size ) % socket->data_fragment_size ) ++ fragments;
// request send
message_id = async_send_4( socket->phone, message, socket->socket_id, fragments, socket->service, flags, NULL );
// send the address if given
if(( ! toaddr ) || ( ipc_data_write_start( socket->phone, toaddr, addrlen ) == EOK )){
if( fragments == 1 ){
// send all if only one fragment
ipc_data_write_start( socket->phone, data, datalength );
}else{
// send the first fragment
ipc_data_write_start( socket->phone, data, socket->data_fragment_size - socket->header_size );
data += socket->data_fragment_size - socket->header_size;
// send the middle fragments
while(( -- fragments ) > 1 ){
ipc_data_write_start( socket->phone, data, socket->data_fragment_size );
data += socket->data_fragment_size;
}
// send the last fragment
ipc_data_write_start( socket->phone, data, ( datalength + socket->header_size ) % socket->data_fragment_size );
}
}
async_wait_for( message_id, & result );
return ( int ) result;
}
int recv( int socket_id, void * data, size_t datalength, int flags ){
// without the address
return recvfrom_core( NET_SOCKET_RECV, socket_id, data, datalength, flags, NULL, NULL );
}
int recvfrom( int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen ){
if( ! fromaddr ) return EBADMEM;
if( ! addrlen ) return NO_DATA;
// with the address
return recvfrom_core( NET_SOCKET_RECVFROM, socket_id, data, datalength, flags, fromaddr, addrlen );
}
int recvfrom_core( int message, int socket_id, void * data, size_t datalength, int flags, struct sockaddr * fromaddr, socklen_t * addrlen ){
socket_ref socket;
aid_t message_id;
int result;
int fragments;
int * lengths;
int index;
ipc_call_t answer;
if( ! data ) return EBADMEM;
if( ! datalength ) return NO_DATA;
if( fromaddr && (( ! addrlen ) || ( * addrlen < sizeof( struct sockaddr_in )))) return EINVAL;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
fibril_mutex_lock( & socket->receive_lock );
// wait for a received packet
while(( fragments = dyn_fifo_value( & socket->received )) <= 0 ){
fibril_condvar_wait( & socket->receive_signal, & socket->receive_lock );
}
// prepare lengths if more fragments
if( fragments > 1 ){
lengths
= ( int * ) malloc( sizeof( int ) * ( fragments
+ 1 ));
if( ! lengths ){
fibril_mutex_unlock( & socket->receive_lock );
return ENOMEM;
}
}
// request packet data
message_id = async_send_4( socket->phone, message, socket->socket_id, 0, socket->service, flags, & answer );
// read the address if desired
if(( ! fromaddr ) || ( ipc_data_read_start( socket->phone, fromaddr, * addrlen ) == EOK )){
if( fragments == 1 ){
// read all if only one fragment
ipc_data_read_start( socket->phone, data, datalength );
}else{
// read the fragment lengths
if( ipc_data_read_start( socket->phone, lengths, sizeof( int ) * ( fragments + 1 )) == EOK ){
if( lengths[ fragments ] <= datalength ){
// read all fragments if long enough
for( index = 0; index < fragments; ++ index ){
ipc_data_read_start( socket->phone, data, lengths[ index ] );
data += lengths[ index ];
}
}
}
}
}else if( fragments > 1 ){
}
async_wait_for( message_id, ( ipcarg_t * ) & result );
// if successful
if( result == EOK ){
// dequeue the received packet
dyn_fifo_pop( & socket->received );
// return read data length
result = SOCKET_GET_READ_DATA_LENGTH( & answer );
// set address length
if( fromaddr && addrlen ) * addrlen = SOCKET_GET_ADDRESS_LENGTH( & answer );
}
fibril_mutex_unlock( & socket->receive_lock );
return result;
}
int getsockopt( int socket_id, int level, int optname, void * value, size_t * optlen ){
socket_ref socket;
aid_t message_id;
ipcarg_t result;
if( !( value && optlen )) return EBADMEM;
if( !( * optlen )) return NO_DATA;
// find the socket
socket = sockets_find( socket_get_sockets(), socket_id );
if( ! socket ) return ENOTSOCK;
// request option value
message_id = async_send_3( socket->phone, NET_SOCKET_GETSOCKOPT, socket->socket_id, optname, socket->service, NULL );
// read the length
if( ipc_data_read_start( socket->phone, optlen, sizeof( * optlen )) == EOK ){
// read the value
ipc_data_read_start( socket->phone, value, * optlen );
}
async_wait_for( message_id, & result );
return ( int ) result;
}
int setsockopt( int socket_id, int level, int optname, const void * value, size_t optlen ){
// send the value
return socket_send_data( socket_id, NET_SOCKET_SETSOCKOPT, optname, value, optlen );
}
/** @}
*/