/*
* Copyright (c) 2008 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 udp
* @{
*/
/** @file
*/
#include <async.h>
#include <malloc.h>
#include <stdio.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../structures/packet/packet_client.h"
#include "../../include/in.h"
#include "../../include/inet.h"
#include "../../include/ip_client.h"
#include "../../include/ip_interface.h"
#include "../../include/ip_protocols.h"
#include "../../include/socket.h"
#include "../../include/socket_errno.h"
//#include "../../include/udp_interface.h"
#include "../../socket/socket_core.h"
#include "../../socket/socket_messages.h"
#include "../tl_messages.h"
#include "udp.h"
#include "udp_header.h"
#include "udp_module.h"
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, void * data, size_t length, int flags );
int socket_get_data( void ** data, size_t * length );
udp_globals_t udp_globals;
/** Initializes the module.
*/
int udp_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
udp_globals.port_search_start = 1025;
udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
if( udp_globals.ip_phone < 0 ){
return udp_globals.ip_phone;
}
ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
udp_globals.prefix += sizeof( udp_header_t );
udp_globals.content -= sizeof( udp_header_t );
return EOK;
}
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver ){
// TODO received
// TODO remove debug dump:
uint8_t * data;
data = packet_get_data( packet );
printf( "Receiving packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id
( packet
), packet_get_data_length
( packet
), data
[ 0 ], data
[ 1 ], data
[ 2 ], data
[ 3 ], data
[ 4 ], data
[ 5 ], data
[ 6 ], data
[ 7 ], data
[ 8 ], data
[ 9 ], data
[ 10 ], data
[ 11 ], data
[ 12 ], data
[ 13 ], data
[ 14 ], data
[ 15 ], data
[ 16 ], data
[ 17 ], data
[ 18 ], data
[ 19 ], data
[ 20 ], data
[ 21 ], data
[ 22 ], data
[ 23 ], data
[ 24 ], data
[ 25 ], data
[ 26 ], data
[ 27 ], data
[ 28 ], data
[ 29 ], data
[ 30 ], data
[ 31 ], data
[ 32 ], data
[ 33 ], data
[ 34 ], data
[ 35 ], data
[ 36 ], data
[ 37 ], data
[ 38 ], data
[ 39 ], data
[ 40 ], data
[ 41 ], data
[ 42 ], data
[ 43 ], data
[ 44 ], data
[ 45 ], data
[ 46 ], data
[ 47 ], data
[ 48 ], data
[ 49 ], data
[ 50 ], data
[ 51 ], data
[ 52 ], data
[ 53 ], data
[ 54 ], data
[ 55 ], data
[ 56 ], data
[ 57 ], data
[ 58 ], data
[ 59 ] );
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return EOK;
}
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
packet_t packet;
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case NET_TL_RECEIVED:
ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return udp_received_msg( IPC_GET_DEVICE( call ), packet, 0 );
case IPC_M_CONNECT_TO_ME:
return process_client_messages( callid, call, answer, answer_count );
}
return ENOTSUP;
}
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
int res;
bool keep_on_going = true;
socket_cores_t local_sockets;
int app_phone = IPC_GET_PHONE( call );
void * addr;
size_t addrlen;
void * data;
size_t length;
/*
* Accept the connection
* - Answer the first IPC_M_CONNECT_ME_TO call.
*/
ipc_answer_0( callid, EOK );
socket_cores_initialize( & local_sockets );
while( keep_on_going ){
// refresh data
* answer_count = 0;
IPC_SET_RETVAL( * answer, 0 );
// just to be precize
IPC_SET_METHOD( * answer, 0 );
IPC_SET_ARG1( * answer, 0 );
IPC_SET_ARG2( * answer, 0 );
IPC_SET_ARG3( * answer, 0 );
IPC_SET_ARG4( * answer, 0 );
IPC_SET_ARG5( * answer, 0 );
callid = async_get_call( call );
printf( "message %d\n", IPC_GET_METHOD
( * call
));
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
keep_on_going = false;
res = EOK;
break;
case NET_SOCKET:
res = socket_create( & local_sockets, app_phone );
break;
case NET_SOCKET_BIND:
if( ERROR_OCCURRED( socket_get_data( & addr, & addrlen ))){
res = ERROR_CODE;
break;
}
res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
break;
case NET_SOCKET_SENDTO:
if( ERROR_OCCURRED( socket_get_data( & addr, & addrlen ))){
res = ERROR_CODE;
break;
}
if( ERROR_OCCURRED( socket_get_data( & data, & length ))){
res = ERROR_CODE;
break;
}
res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, data, length, SOCKET_GET_FLAGS( call ));
break;
case NET_SOCKET_RECVFROM:
// TODO read first received packet queue data continuesly
res = ENOTSUP;
break;
case NET_SOCKET_CLOSE:
res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
break;
case NET_SOCKET_GETSOCKOPT:
case NET_SOCKET_SETSOCKOPT:
default:
res = ENOTSUP;
break;
}
// TODO debug
switch( * answer_count ){
case 0: ipc_answer_0( callid, res );
continue;
case 1: ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
continue;
case 2: ipc_answer_2( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ));
continue;
case 3: ipc_answer_3( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ));
continue;
case 4: ipc_answer_4( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ), IPC_GET_ARG4( * answer ));
continue;
default: ipc_answer_5( callid, res, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ), IPC_GET_ARG4( * answer ), IPC_GET_ARG5( * answer ));
continue;
}
}
socket_cores_destroy( & local_sockets );
return EOK;
}
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, void * data, size_t length, int flags ){
ERROR_DECLARE;
socket_core_ref socket;
struct sockaddr * address;
struct sockaddr_in * address_in;
packet_t packet;
udp_header_ref header;
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;
/* if( socket->port < 0 ){
return ENOTCONN;
socket->port = udp_globals.port_search_start;
while( socket_ports_find( global_sockets, socket->port ) >= 0 ){
socket_port = ( socket->port + 1 ) % ( 65535 - 1025 ) + 1025;
}
ERROR_PROPAGATE( socket_ports_add( global_sockets, socket->port );
udp_globals.port_search_start = socket->port + 1;
}
*/ // TODO create and send fragmented packets
// TODO do not ask all the time
ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, udp_globals.prefix, udp_globals.suffix );
if( ! packet ) return ENOMEM;
if( ERROR_OCCURRED( packet_copy_data( packet, data, length ))
|| ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return ERROR_CODE;
}
header = PACKET_PREFIX( packet, udp_header_t );
if( ! header ){
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return ENOMEM;
}
header->source = ( socket->port < 0 ) ? 0 : htons( socket->port );
header->dest = htons( address_in->sin_port );
header->len = htons( length );
// TODO my ip address for the pseudo header checksum
header->check = 0;
if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
pq_release( udp_globals.net_phone, packet_get_id( packet ));
return ERROR_CODE;
}
return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP );
break;
// TODO IPv6
default:
return EAFNOSUPPORT;
}
return EOK;
}
int socket_get_data( void ** data, size_t * length ){
ERROR_DECLARE;
ipc_callid_t callid;
if( !( data && length )) return EBADMEM;
if( ! ipc_data_write_receive( & callid, length )) return EINVAL;
if( ! data ) return ENOMEM;
if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){
return ERROR_CODE;
}
return EOK;
}
/** @}
*/