34,6 → 34,7 |
*/ |
|
#include <async.h> |
#include <malloc.h> |
#include <stdio.h> |
|
#include <ipc/ipc.h> |
44,17 → 45,28 |
#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; |
|
61,25 → 73,18 |
/** Initializes the module. |
*/ |
int udp_initialize( async_client_conn_t client_connection ){ |
// ERROR_DECLARE; |
ERROR_DECLARE; |
|
// ipcarg_t arg1, arg2; |
// packet_t packet; |
|
/* printf( "UDP - testing to send to IP:\n" ); |
ERROR_PROPAGATE( ip_echo( udp_globals.ip_phone, 12, 34, 0, 0, 0, & arg1, & arg2, NULL, NULL, NULL )); |
if(( arg1 != 12 ) || ( arg2 != 34 )) return EINVAL; |
printf( "OK\n" ); |
*/ |
udp_globals.port_search_start = 1025; |
udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg ); |
|
/* printf( "UDP - testing to send packet to IP:\n" ); |
packet = packet_get_4( udp_globals.net_phone, 6, 20, 30, 20 ); |
if( ! packet ) return ENOMEM; |
packet_copy_data( packet, "Hi, this is UDP", 16 ); |
ip_send_msg( udp_globals.ip_phone, -1, packet, SERVICE_UDP ); |
printf( "OK\n" ); |
*/ return EOK; |
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 ){ |
100,14 → 105,192 |
|
* answer_count = 0; |
switch( IPC_GET_METHOD( * call )){ |
case IPC_M_PHONE_HUNGUP: |
return EOK; |
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 ); |
free( addr ); |
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 ))){ |
free( addr ); |
res = ERROR_CODE; |
break; |
} |
res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, data, length, SOCKET_GET_FLAGS( call )); |
free( addr ); |
free( data ); |
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 |
printf( "res = %d\n", res ); |
|
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; |
* data = malloc( * length ); |
if( ! data ) return ENOMEM; |
if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){ |
free( data ); |
return ERROR_CODE; |
} |
return EOK; |
} |
|
/** @} |
*/ |