Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4700 → Rev 4701

/branches/network/uspace/srv/net/tl/udp/udp.c
31,6 → 31,8
*/
 
/** @file
* UDP module implementation.
* @see udp.h
*/
 
#include <async.h>
65,24 → 67,130
#include "udp_header.h"
#include "udp_module.h"
 
/** Maximum UDP fragment size.
*/
#define MAX_UDP_FRAGMENT_SIZE 65535
 
/** Free ports pool start.
*/
#define UDP_FREE_PORTS_START 1025
 
/** Free ports pool end.
*/
#define UDP_FREE_PORTS_END 65535
 
/** Processes the received UDP packet.
* Is used as an entry point from the underlying IP module.
* Releases the packet on error.
* @param device_id The device identifier. Ignored parameter.
* @param packet The received packet. Input/output parameter.
* @param receiver The target service. Ignored parameter.
* @returns EOK on success.
* @returns Other error codes as defined for the udp_process_packet() function.
*/
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
 
/** Processes the received UDP packet.
* Notifies the destination socket application.
* @param packet The received packet. Input/output parameter.
* @returns EOK on success.
* @returns EINVAL if the packet is not valid.
* @returns EINVAL if the stored packet address is not the an_addr_t.
* @returns EINVAL if the packet does not contain any data.
* @returns NO_DATA if the packet content is shorter than the user datagram header.
* @returns ENOMEM if there is not enough memory left.
* @returns EADDRNOTAVAIL if the destination socket does not exist.
* @returns Other error codes as defined for the ip_client_process_packet() function.
*/
int udp_process_packet( packet_t packet );
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
 
/** @name Socket messages processing functions
*/
/*@{*/
 
/** Processes the socket client messages.
* Runs until the client module disconnects.
* @param callid The message identifier. Input parameter.
* @param call The message parameters. Input parameter.
* @returns EOK on success.
* @see socket.h
*/
int process_client_messages( ipc_callid_t callid, ipc_call_t call );
 
/** Sends data from the socket to the remote address.
* Binds the socket to a free port if not already connected/bound.
* Handles the NET_SOCKET_SENDTO message.
* @param local_sockets The application local sockets. Input/output parameter.
* @param socket_id Socket identifier. Input parameter.
* @param addr The destination address. Input parameter.
* @param addrlen The address length. Input parameter.
* @param fragments The number of data fragments. Input parameter.
* @param flags Various send flags. Input parameter.
* @returns EOK on success.
* @returns EAFNOTSUPPORT if the address family is not supported.
* @returns ENOTSOCK if the socket is not found.
* @returns EINVAL if the address is invalid.
* @returns ENOTCONN if the sending socket is not and cannot be bound.
* @returns ENOMEM if there is not enough memory left.
* @returns Other error codes as defined for the socket_read_packet_data() function.
* @returns Other error codes as defined for the ip_client_prepare_packet() function.
* @returns Other error codes as defined for the ip_send_msg() function.
*/
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags );
 
/** Receives data to the socket.
* Handles the NET_SOCKET_RECVFROM message.
* @param local_sockets The application local sockets. Input parameter.
* @param socket_id Socket identifier. Input parameter.
* @param flags Various receive flags. Input parameter.
* @returns The number of bytes received.
* @returns ENOTSOCK if the socket is not found.
* @returns NO_DATA if there are no received packets or data.
* @returns ENOMEM if there is not enough memory left.
* @returns EINVAL if the received address is not an IP address.
* @returns Other error codes as defined for the packet_translate() function.
* @returns Other error codes as defined for the socket_write_data() function.
*/
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags );
 
/*@}*/
 
/** Receives data from the socket.
* The received data buffer is allocated and returned.
* @param data The data buffer to be filled. Output parameter.
* @param length The buffer length. Output parameter.
* @returns EOK on success.
* @returns EBADMEM if the data or the length parameter is NULL.
* @returns EINVAL if the client does not send data.
* @returns ENOMEM if there is not enough memory left.
* @returns Other error codes as defined for the ipc_data_write_finalize() function.
*/
int socket_read_data( void ** data, size_t * length );
 
/** Receives data from the socket into a packet.
* @param packet The new created packet. Output parameter.
* @param prefix Reserved packet data prefix length. Input parameter.
* @param address_in The destination address to be set. Input parameter.
* @returns Number of bytes received.
* @returns EINVAL if the client does not send data.
* @returns ENOMEM if there is not enough memory left.
* @returns Other error codes as defined for the ipc_data_read_finalize() function.
*/
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in );
 
/** Replies the data to the socket.
* @param data The data buffer to be sent. Input parameter.
* @param data_length The buffer length. Input parameter.
* @returns EOK on success.
* @returns EINVAL if the client does not expect all the data.
* @returns Other error codes as defined for the ipc_data_read_finalize() function.
*/
int socket_write_data( void * data, size_t data_length );
 
/** UDP global data.
*/
udp_globals_t udp_globals;
 
/** Initializes the module.
*/
int udp_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
 
129,18 → 237,12
 
// get packet data
length = packet_get_addr( packet, & src, & dest );
if( length < 0 ) return length;
if( length != sizeof( in_addr_t )) return EINVAL;
// TODO received ipopts?
// TODO process received ipopts?
ERROR_PROPAGATE( ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ));
 
// TODO remove debug dump:
/* uint8_t * rdata;
rdata = packet_get_data( packet );
printf( "Receiving udp 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 ), rdata[ 0 ], rdata[ 1 ], rdata[ 2 ], rdata[ 3 ], rdata[ 4 ], rdata[ 5 ], rdata[ 6 ], rdata[ 7 ], rdata[ 8 ], rdata[ 9 ], rdata[ 10 ], rdata[ 11 ], rdata[ 12 ], rdata[ 13 ], rdata[ 14 ], rdata[ 15 ], rdata[ 16 ], rdata[ 17 ], rdata[ 18 ], rdata[ 19 ], rdata[ 20 ], rdata[ 21 ], rdata[ 22 ], rdata[ 23 ], rdata[ 24 ], rdata[ 25 ], rdata[ 26 ], rdata[ 27 ], rdata[ 28 ], rdata[ 29 ], rdata[ 30 ], rdata[ 31 ], rdata[ 32 ], rdata[ 33 ], rdata[ 34 ], rdata[ 35 ], rdata[ 36 ], rdata[ 37 ], rdata[ 38 ], rdata[ 39 ], rdata[ 40 ], rdata[ 41 ], rdata[ 42 ], rdata[ 43 ], rdata[ 44 ], rdata[ 45 ], rdata[ 46 ], rdata[ 47 ], rdata[ 48 ], rdata[ 49 ], rdata[ 50 ], rdata[ 51 ], rdata[ 52 ], rdata[ 53 ], rdata[ 54 ], rdata[ 55 ], rdata[ 56 ], rdata[ 57 ], rdata[ 58 ], rdata[ 59 ] );
*/
length = packet_get_data_length( packet );
if( length < 0 ) return length;
if( length <= 0 ) return EINVAL;
if( length < sizeof( udp_header_t )) return NO_DATA;
data = packet_get_data( packet );
if( ! data ) return NO_DATA;
156,7 → 258,6
do{
++ fragments;
length = packet_get_data_length( packet );
if( length < 0 ) return length;
if( ! length ) return NO_DATA;
if( total_length < length ){
// cut of the suffix if too long
193,26 → 294,28
case NET_TL_RECEIVED:
fibril_rwlock_read_lock( & udp_globals.lock );
if( ! ERROR_OCCURRED( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, 0 );
ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_UDP );
}
fibril_rwlock_read_unlock( & udp_globals.lock );
return ERROR_CODE;
case IPC_M_CONNECT_TO_ME:
return process_client_messages( callid, call, answer, answer_count );
return process_client_messages( callid, * call );
}
return ENOTSUP;
}
 
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
int process_client_messages( ipc_callid_t callid, ipc_call_t call ){
ERROR_DECLARE;
 
int res;
bool keep_on_going = true;
socket_cores_t local_sockets;
int app_phone = IPC_GET_PHONE( call );
int app_phone = IPC_GET_PHONE( & call );
void * addr;
size_t addrlen;
fibril_rwlock_t lock;
ipc_call_t answer;
int answer_count;
 
/*
* Accept the connection
225,20 → 328,20
 
while( keep_on_going ){
// refresh data
* answer_count = 0;
IPC_SET_RETVAL( * answer, 0 );
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 );
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 );
callid = async_get_call( & call );
// printf( "message %d\n", IPC_GET_METHOD( * call ));
 
switch( IPC_GET_METHOD( * call )){
switch( IPC_GET_METHOD( call )){
case IPC_M_PHONE_HUNGUP:
keep_on_going = false;
res = EOK;
249,7 → 352,7
fibril_rwlock_write_unlock( & lock );
* SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
* SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
* answer_count = 3;
answer_count = 3;
break;
case NET_SOCKET_BIND:
if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
262,7 → 365,7
fibril_rwlock_write_unlock( & udp_globals.lock );
fibril_rwlock_write_unlock( & lock );
free( addr );
break;
break;
case NET_SOCKET_SENDTO:
if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
res = ERROR_CODE;
284,7 → 387,7
if( res > 0 ){
* SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
* SOCKET_SET_ADDRESS_LENGTH( answer ) = sizeof( struct sockaddr_in );
* answer_count = 2;
answer_count = 2;
res = EOK;
}
break;
304,18 → 407,18
 
// printf( "res = %d\n", res );
 
switch( * answer_count ){
switch( answer_count ){
case 0: ipc_answer_0( callid, res );
continue;
case 1: ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
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 ));
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 ));
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 ));
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 ));
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;
}
}
421,7 → 524,7
if( ! socket ) return ENOTSOCK;
// get the next received packet
packet_id = dyn_fifo_value( & socket->received );
if( packet_id < 0 ) return packet_id;
if( packet_id < 0 ) return NO_DATA;
ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
// get udp header
data = packet_get_data( packet );
506,7 → 609,7
if( !( data && length )) return EBADMEM;
if( ! ipc_data_write_receive( & callid, length )) return EINVAL;
* data = malloc( * length );
if( ! data ) return ENOMEM;
if( !( * data )) return ENOMEM;
if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){
free( data );
return ERROR_CODE;