63,6 → 63,7 |
#include "../../include/ip_client.h" |
#include "../../include/ip_interface.h" |
#include "../../include/ip_protocols.h" |
#include "../../include/net_interface.h" |
#include "../../include/socket_codes.h" |
#include "../../include/socket_errno.h" |
|
73,14 → 74,38 |
#include "icmp_messages.h" |
#include "icmp_module.h" |
|
/** Original datagram length in bytes transfered to the error notification message. |
*/ |
#define ICMP_KEEP_LENGTH 8 |
|
/** Computes the ICMP datagram checksum. |
* @param header The ICMP datagram header. Input/output parameter. |
* @param length The total datagram length. Input parameter. |
* @returns The computed checksum. |
*/ |
#define ICMP_CHECKSUM( header, length ) htons( ip_checksum(( uint8_t * ) ( header ), ( length ))) |
|
/** An echo request datagrams pattern. |
*/ |
#define ICMP_ECHO_TEXT "Hello from HelenOS." |
|
#define ICMP_GET_LOCK_KEY( id, sequence ) ((( id ) << 16 ) | ( sequence & 0xFFFF )) |
/** Computes an ICMP reply data key. |
* @param id The message identifier. Input parameter. |
* @param sequence The message sequence number. Input parameter. |
* @returns The computed ICMP reply data key. |
*/ |
#define ICMP_GET_REPLY_KEY( id, sequence ) ((( id ) << 16 ) | ( sequence & 0xFFFF )) |
|
/** Type definition of the ICMP reply timeout. |
* @see icmp_reply_timeout |
*/ |
typedef struct icmp_reply_timeout icmp_reply_timeout_t; |
|
/** Type definition of the ICMP reply timeout pointer. |
* @see icmp_reply_timeout |
*/ |
typedef icmp_reply_timeout_t * icmp_reply_timeout_ref; |
|
/** Processes the received ICMP packet. |
* Is used as an entry point from the underlying IP module. |
* Releases the packet on error. |
109,42 → 134,126 |
int icmp_process_packet( packet_t packet, services_t error ); |
|
/** Processes the client messages. |
* Remenbers the assigned identifier and sequence numbers. |
* Remembers the assigned identifier and sequence numbers. |
* Runs until the client module disconnects. |
* @param callid The message identifier. Input parameter. |
* @param call The message parameters. Input parameter. |
* @returns EOK on success. |
* @returns EOK. |
* @see icmp_interface.h |
* @see icmp_api.h |
*/ |
int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ); |
|
/** Processes the generic client messages. |
* @param call The message parameters. Input parameter. |
* @returns EOK on success. |
* @returns ENOTSUP if the message is not known. |
* @returns Other error codes as defined for the packet_translate() function. |
* @returns Other error codes as defined for the icmp_destination_unreachable_msg() function. |
* @returns Other error codes as defined for the icmp_source_quench_msg() function. |
* @returns Other error codes as defined for the icmp_time_exceeded_msg() function. |
* @returns Other error codes as defined for the icmp_parameter_problem_msg() function. |
* @see icmp_interface.h |
*/ |
int icmp_process_message( ipc_call_t * call ); |
|
/** Releases the packet and returns the result. |
* @param packet The packet queue to be released. Input parameter. |
* @param result The result to be returned. Input parameter. |
* @return The result parameter. |
* @returns The result parameter. |
*/ |
int icmp_release_and_return( packet_t packet, int result ); |
|
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, suseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ); |
/** Requests an echo message. |
* Sends a packet with specified parameters to the target host and waits for the reply upto the given timeout. |
* Blocks the caller until the reply or the timeout occurres. |
* @param id The message identifier. Input parameter. |
* @param sequence The message sequence parameter. Input parameter. |
* @param size The message data length in bytes. Input parameter. |
* @param timeout The timeout in miliseconds. Input parameter. |
* @param ttl The time to live. Input parameter. |
* @param tos The type of service. Input parameter. |
* @param dont_fragment The value indicating whether the datagram must not be fragmented. Is used as a MTU discovery. Input parameter. |
* @param addr The target host address. Input parameter. |
* @param addrlen The torget host address length. Input parameter. |
* @returns ICMP_ECHO on success. |
* @returns ETIMEOUT if the reply has not arrived before the timeout. |
* @returns ICMP type of the received error notification. |
* @returns EINVAL if the addrlen parameter is less or equal to zero (<=0). |
* @returns ENOMEM if there is not enough memory left. |
* @returns EPARTY if there was an internal error. |
*/ |
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ); |
|
/** Prepares the ICMP error packet. |
* Truncates the original packet if longer than ICMP_KEEP_LENGTH bytes. |
* Prefixes and returns the ICMP header. |
* @param packet The original packet. Input/output parameter. |
* @returns The prefixed ICMP header. |
* @returns NULL on errors. |
*/ |
icmp_header_ref icmp_prepare_packet( packet_t packet ); |
|
/** Sends the ICMP message. |
* Sets the message type and code and computes the checksum. |
* Error messages are sent only if allowed in the configuration. |
* Releases the packet on errors. |
* @returns EOK on success. |
* @returns EPERM if the error message is not allowed. |
*/ |
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error ); |
int process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ); |
|
/** Tries to set the pending reply result as the received message type. |
* If the reply data are still present, the reply timeouted and the parent fibril is awaken. |
* The global lock is not released in this case to be reused by the parent fibril. |
* Releases the packet. |
* @param packet The received reply message. Input parameter. |
* @param header The ICMP message header. Input parameter. |
* @param type The received reply message type. Input parameter. |
* @param code The received reply message code. Input parameter. |
* @returns EOK. |
*/ |
int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ); |
|
/** Tries to set the pending reply result as timeouted. |
* Sleeps the timeout period of time and then tries to obtain and set the pending reply result as timeouted and signals the reply result. |
* If the reply data are still present, the reply timeouted and the parent fibril is awaken. |
* The global lock is not released in this case to be reused by the parent fibril. |
* Should run in a searate fibril. |
* @param data The icmp_reply_timeout structure. Input parameter. |
* @returns EOK on success. |
* @returns EINVAL if the data parameter is NULL. |
*/ |
int icmp_timeout_for_reply( void * data ); |
|
/** ICMP reply timeout data. |
* Used as a timeouting fibril argument. |
* @see icmp_timeout_for_reply() |
*/ |
struct icmp_reply_timeout{ |
/** Reply data key. |
*/ |
int reply_key; |
/** Timeout in microseconds. |
*/ |
suseconds_t timeout; |
}; |
|
/** ICMP global data. |
*/ |
icmp_globals_t icmp_globals; |
|
INT_MAP_IMPLEMENT( time_locks, atomic_t ); |
INT_MAP_IMPLEMENT( icmp_replies, icmp_reply_t ); |
|
GENERIC_FIELD_IMPLEMENT( echo_data, icmp_echo_t ); |
GENERIC_FIELD_IMPLEMENT( icmp_echo_data, icmp_echo_t ); |
|
int icmp_echo_msg( int icmp_phone, size_t size, suseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){ |
int icmp_echo_msg( int icmp_phone, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){ |
icmp_echo_ref echo_data; |
int res; |
|
fibril_rwlock_write_lock( & icmp_globals.lock ); |
// use the phone as the echo data index |
echo_data = echo_data_get_index( & icmp_globals.echo_data, icmp_phone ); |
echo_data = icmp_echo_data_get_index( & icmp_globals.echo_data, icmp_phone ); |
if( ! echo_data ){ |
res = ENOENT; |
}else{ |
155,96 → 264,124 |
return res; |
} |
|
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, suseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){ |
int icmp_timeout_for_reply( void * data ){ |
icmp_reply_ref reply; |
icmp_reply_timeout_ref timeout = data; |
|
if( ! timeout ){ |
return EINVAL; |
} |
// sleep the given timeout |
async_usleep( timeout->timeout ); |
// lock the globals |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
// find the pending reply |
reply = icmp_replies_find( & icmp_globals.replies, timeout->reply_key ); |
if( reply ){ |
// set the timeout result |
reply->result = ETIMEOUT; |
// notify the main fibril |
fibril_condvar_signal( & reply->condvar ); |
}else{ |
// unlock only if no reply |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
} |
// release the timeout structure |
free( timeout ); |
return EOK; |
} |
|
int icmp_echo( icmp_param_t id, icmp_param_t sequence, size_t size, mseconds_t timeout, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment, const struct sockaddr * addr, socklen_t addrlen ){ |
ERROR_DECLARE; |
|
icmp_header_ref header; |
packet_t packet; |
size_t offset; |
size_t length; |
uint8_t * data; |
atomic_t * lock; |
icmp_reply_ref reply; |
icmp_reply_timeout_ref reply_timeout; |
int result; |
int index; |
int lock_key; |
struct sockaddr_in * address_in; |
struct timeval time_before; |
struct timeval time_after; |
fid_t fibril; |
|
if( addrlen <= 0 ){ |
return EINVAL; |
} |
length = ( size_t ) addrlen; |
// TODO do not ask all the time |
ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix )); |
packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.addr_len, sizeof( icmp_header_t ) + icmp_globals.prefix, icmp_globals.suffix ); |
if( ! packet ) return ENOMEM; |
|
// prepare the requesting packet |
// set the destination address |
if( addrlen < sizeof( struct sockaddr )){ |
return icmp_release_and_return( packet, EINVAL ); |
if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( const uint8_t * ) addr, length ))){ |
return icmp_release_and_return( packet, ERROR_CODE ); |
} |
switch( addr->sa_family ){ |
case AF_INET: |
if( addrlen != sizeof( struct sockaddr_in )){ |
return icmp_release_and_return( packet, EINVAL ); |
} |
address_in = ( struct sockaddr_in * ) addr; |
if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){ |
return icmp_release_and_return( packet, ERROR_CODE ); |
} |
break; |
default: |
return icmp_release_and_return( packet, EAFNOSUPPORT ); |
} |
// allocate space in the packet |
data = ( uint8_t * ) packet_suffix( packet, size ); |
if( ! data ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
offset = 0; |
while( size > offset + sizeof( ICMP_ECHO_TEXT )){ |
memcpy( data + offset, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT )); |
offset += sizeof( ICMP_ECHO_TEXT ); |
// fill the data |
length = 0; |
while( size > length + sizeof( ICMP_ECHO_TEXT )){ |
memcpy( data + length, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT )); |
length += sizeof( ICMP_ECHO_TEXT ); |
} |
memcpy( data + offset, ICMP_ECHO_TEXT, size - offset ); |
header = icmp_prepare_packet( packet ); |
memcpy( data + length, ICMP_ECHO_TEXT, size - length ); |
// prefix the header |
header = PACKET_PREFIX( packet, icmp_header_t ); |
if( ! header ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
bzero( header, sizeof( * header )); |
header->un.echo.id = id; |
header->un.echo.sequence = sequence; |
lock_key = ICMP_GET_LOCK_KEY( header->un.echo.id, header->un.echo.sequence ); |
// create a locked fuxed |
lock = malloc( sizeof( * lock )); |
if( ! lock ){ |
|
// prepare the reply and the reply timeout structures |
reply_timeout = malloc( sizeof( * reply_timeout )); |
if( ! reply_timeout ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
atomic_set( lock, 0 ); |
index = time_locks_add( & icmp_globals.time_locks, lock_key, lock ); |
reply = malloc( sizeof( * reply )); |
if( ! reply ){ |
free( reply_timeout ); |
return icmp_release_and_return( packet, ENOMEM ); |
} |
reply_timeout->reply_key = ICMP_GET_REPLY_KEY( header->un.echo.id, header->un.echo.sequence ); |
// timeout in microseconds |
reply_timeout->timeout = timeout * 1000; |
fibril_mutex_initialize( & reply->mutex ); |
fibril_mutex_lock( & reply->mutex ); |
fibril_condvar_initialize( & reply->condvar ); |
index = icmp_replies_add( & icmp_globals.replies, reply_timeout->reply_key, reply ); |
if( index < 0 ){ |
free( lock ); |
free( reply ); |
free( reply_timeout ); |
return icmp_release_and_return( packet, index ); |
} |
if( ERROR_OCCURRED( icmp_send_packet( ICMP_ECHO, 0, packet, header, 0 ))){ |
free( lock ); |
return icmp_release_and_return( packet, ERROR_CODE ); |
// start the timeouting thread |
fibril = fibril_create( icmp_timeout_for_reply, reply_timeout ); |
if( ! fibril ){ |
return EPARTY; |
} |
// unlock the global to allow unlocking and other fibrils to work |
// try to lock again - may be unlocked by the reply |
ERROR_PROPAGATE( gettimeofday( & time_before, NULL )); |
do{ |
result = atomic_get( lock ); |
if( result ){ |
break; |
}else{ |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
// TODO does not yield? |
//printf( "y %d\n", fibril_yield()); |
fibril_yield(); |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
ERROR_PROPAGATE( gettimeofday( & time_after, NULL )); |
} |
}while( tv_sub( & time_after, & time_before ) <= timeout ); |
if( ! result ){ |
result = ELIMIT; |
} |
// destroy the lock |
time_locks_exclude_index( & icmp_globals.time_locks, index ); |
fibril_add_ready( fibril ); |
|
// unlock the globals and wait for a reply |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
|
// send the request |
icmp_send_packet( ICMP_ECHO, 0, packet, header, 0 ); |
|
// wait for a reply |
fibril_condvar_wait( & reply->condvar, & reply->mutex ); |
// read the result |
result = reply->result; |
|
// destroy the reply structure |
fibril_mutex_unlock( & reply->mutex ); |
icmp_replies_exclude_index( & icmp_globals.replies, index ); |
return result; |
} |
|
252,7 → 389,9 |
icmp_header_ref header; |
|
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
if( ! header ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
if( mtu ){ |
header->un.frag.mtu = mtu; |
} |
263,7 → 402,9 |
icmp_header_ref header; |
|
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
if( ! header ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
return icmp_send_packet( ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP ); |
} |
|
271,7 → 412,9 |
icmp_header_ref header; |
|
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
if( ! header ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP ); |
} |
|
279,7 → 422,9 |
icmp_header_ref header; |
|
header = icmp_prepare_packet( packet ); |
if( ! header ) return ENOMEM; |
if( ! header ){ |
return icmp_release_and_return( packet, ENOMEM ); |
} |
header->un.param.pointer = pointer; |
return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP ); |
} |
294,14 → 439,12 |
header_length = ip_client_header_length( packet ); |
if( header_length <= 0 ) return NULL; |
// truncate if longer than 64 bits (without the IP header) |
if( total_length - header_length > ICMP_KEEP_LENGTH ){ |
if( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK ) return NULL; |
if(( total_length - header_length > ICMP_KEEP_LENGTH ) |
&& ( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK )){ |
return NULL; |
} |
header = PACKET_PREFIX( packet, icmp_header_t ); |
if( ! header ){ |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
return NULL; |
} |
if( ! header ) return NULL; |
bzero( header, sizeof( * header )); |
return header; |
} |
309,13 → 452,16 |
int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error ){ |
ERROR_DECLARE; |
|
// do not send an error if disabled |
if( error && ( ! icmp_globals.error_reporting )){ |
return icmp_release_and_return( packet, EPERM ); |
} |
header->type = type; |
header->code = code; |
header->checksum = 0; |
header->checksum = ICMP_CHECKSUM( header, packet_get_data_length( packet )); |
if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, 0, 0, 0, 0 ))){ |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
return ERROR_CODE; |
return icmp_release_and_return( packet, ERROR_CODE ); |
} |
return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error ); |
} |
332,7 → 478,7 |
echo_data->id = icmp_globals.last_used_id; |
echo_data->sequence = 0; |
// remember the assigned echo data |
index = echo_data_add( & icmp_globals.echo_data, echo_data ); |
index = icmp_echo_data_add( & icmp_globals.echo_data, echo_data ); |
if( index < 0 ){ |
free( echo_data ); |
} |
344,10 → 490,15 |
int icmp_initialize( async_client_conn_t client_connection ){ |
ERROR_DECLARE; |
|
measured_string_t names[] = {{ "ICMP_ERROR_REPORTING", 20 }, { "ICMP_ECHO_REPLYING", 18 }}; |
measured_string_ref configuration; |
size_t count = sizeof( names ) / sizeof( measured_string_t ); |
char * data; |
|
fibril_rwlock_initialize( & icmp_globals.lock ); |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
time_locks_initialize( & icmp_globals.time_locks ); |
echo_data_initialize( & icmp_globals.echo_data ); |
icmp_replies_initialize( & icmp_globals.replies ); |
icmp_echo_data_initialize( & icmp_globals.echo_data ); |
icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg ); |
if( icmp_globals.ip_phone < 0 ){ |
return icmp_globals.ip_phone; |
355,6 → 506,14 |
ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.addr_len, & icmp_globals.prefix, & icmp_globals.content, & icmp_globals.suffix )); |
icmp_globals.prefix += sizeof( icmp_header_t ); |
icmp_globals.content -= sizeof( icmp_header_t ); |
configuration = & names[ 0 ]; |
// get configuration |
ERROR_PROPAGATE( net_get_conf_req( icmp_globals.net_phone, & configuration, count, & data )); |
if( configuration ){ |
icmp_globals.error_reporting = configuration[ 0 ].value && ( configuration[ 0 ].value[ 0 ] == 'y' ); |
icmp_globals.echo_replying = configuration[ 1 ].value && ( configuration[ 1 ].value[ 0 ] == 'y' ); |
net_free_settings( configuration, data ); |
} |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
return EOK; |
} |
363,8 → 522,7 |
ERROR_DECLARE; |
|
if( ERROR_OCCURRED( icmp_process_packet( packet, error ))){ |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
return ERROR_CODE; |
return icmp_release_and_return( packet, ERROR_CODE ); |
} |
|
return EOK; |
401,7 → 559,7 |
// get rid of the ip header |
result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
if( result < 0 ) return result; |
packet_trim( packet, ( size_t ) result, 0 ); |
ERROR_PROPAGATE( packet_trim( packet, ( size_t ) result, 0 )); |
|
length = packet_get_data_length( packet ); |
if( length <= 0 ) return EINVAL; |
411,25 → 569,58 |
// get icmp header |
header = ( icmp_header_ref ) data; |
// checksum |
if(( header->checksum ) && ( ICMP_CHECKSUM( header, length ))){ |
return EINVAL; |
/* if(( header->checksum ) && ( ICMP_CHECKSUM( header, length ))){ |
// set the original message type on error notification |
// type swap observed in Qemu |
if( error ){ |
switch( header->type ){ |
case ICMP_ECHOREPLY: |
header->type = ICMP_ECHO; |
break; |
} |
} |
if( ICMP_CHECKSUM( header, length )){ |
return EINVAL; |
} |
} |
*/ if( header->checksum ){ |
while( ICMP_CHECKSUM( header, length )){ |
// set the original message type on error notification |
// type swap observed in Qemu |
if( error ){ |
switch( header->type ){ |
case ICMP_ECHOREPLY: |
header->type = ICMP_ECHO; |
continue; |
} |
} |
return EINVAL; |
} |
} |
switch( header->type ){ |
case ICMP_ECHOREPLY: |
return process_echo_reply( packet, header, ICMP_ECHO, 0 ); |
if( error ){ |
return icmp_process_echo_reply( packet, header, type, code ); |
}else{ |
return icmp_process_echo_reply( packet, header, ICMP_ECHO, 0 ); |
} |
case ICMP_ECHO: |
if( error ){ |
return process_echo_reply( packet, header, type, code ); |
}else{ |
return icmp_process_echo_reply( packet, header, type, code ); |
// do not send a reply if disabled |
}else if( icmp_globals.echo_replying ){ |
addrlen = packet_get_addr( packet, & src, NULL ); |
if(( addrlen > 0 ) |
// set both addresses to the source one (avoids the source address deletion before setting the destination one) |
&& ( packet_set_addr( packet, src, src, ( size_t ) addrlen ) == EOK )){ |
// send the reply |
return icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0 ); |
icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0 ); |
return EOK; |
}else{ |
return icmp_release_and_return( packet, EINVAL ); |
return EINVAL; |
} |
}else{ |
return EPERM; |
} |
case ICMP_DEST_UNREACH: |
case ICMP_SOURCE_QUENCH: |
443,28 → 634,33 |
case ICMP_REDIRECT_MOBILE: |
case ICMP_SKIP: |
case ICMP_PHOTURIS: |
fibril_rwlock_read_lock( & icmp_globals.lock ); |
ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP ); |
fibril_rwlock_read_unlock( & icmp_globals.lock ); |
return EOK; |
default: |
return icmp_release_and_return( packet, ENOTSUP ); |
return ENOTSUP; |
} |
} |
|
int process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){ |
int lock_key; |
atomic_t * lock; |
int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){ |
int reply_key; |
icmp_reply_ref reply; |
|
lock_key = ICMP_GET_LOCK_KEY( header->un.echo.id, header->un.echo.sequence ); |
// compute the reply key |
reply_key = ICMP_GET_REPLY_KEY( header->un.echo.id, header->un.echo.sequence ); |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
// lock the globals |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
lock = time_locks_find( & icmp_globals.time_locks, lock_key ); |
if( lock ){ |
// unlock the lock for the waiting fibril |
atomic_set( lock, type ); |
// find the pending reply |
reply = icmp_replies_find( & icmp_globals.replies, reply_key ); |
if( reply ){ |
// set the result |
reply->result = type; |
// notify the main fibril |
fibril_condvar_signal( & reply->condvar ); |
}else{ |
// unlock only if no reply |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
} |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
return EOK; |
} |
|
482,6 → 678,8 |
return ERROR_CODE; |
case NET_ICMP_INIT: |
return icmp_process_client_messages( callid, * call ); |
default: |
return icmp_process_message( call ); |
} |
return ENOTSUP; |
} |
493,8 → 691,7 |
fibril_rwlock_t lock; |
ipc_call_t answer; |
int answer_count; |
packet_t packet; |
size_t addrlen; |
size_t length; |
struct sockaddr * addr; |
icmp_param_t id; |
icmp_param_t sequence = 0; |
526,16 → 723,16 |
break; |
case NET_ICMP_ECHO: |
fibril_rwlock_write_lock( & lock ); |
if( ! ipc_data_write_receive( & data_callid, & addrlen )){ |
if( ! ipc_data_write_receive( & data_callid, & length )){ |
ERROR_CODE = EINVAL; |
}else{ |
addr = malloc( addrlen ); |
addr = malloc( length ); |
if( ! addr ){ |
ERROR_CODE = ENOMEM; |
}else{ |
if( ! ERROR_OCCURRED( ipc_data_write_finalize( data_callid, addr, addrlen ))){ |
if( ! ERROR_OCCURRED( ipc_data_write_finalize( data_callid, addr, length ))){ |
fibril_rwlock_write_lock( & icmp_globals.lock ); |
ERROR_CODE = icmp_echo( id, sequence, ICMP_GET_SIZE( call ), ICMP_GET_TIMEOUT( call ), ICMP_GET_TTL( call ), ICMP_GET_TOS( call ), ICMP_GET_DONT_FRAGMENT( call ), addr, addrlen ); |
ERROR_CODE = icmp_echo( id, sequence, ICMP_GET_SIZE( call ), ICMP_GET_TIMEOUT( call ), ICMP_GET_TTL( call ), ICMP_GET_TOS( call ), ICMP_GET_DONT_FRAGMENT( call ), addr, ( socklen_t ) length ); |
fibril_rwlock_write_unlock( & icmp_globals.lock ); |
free( addr ); |
++ sequence; |
544,26 → 741,8 |
} |
fibril_rwlock_write_unlock( & lock ); |
break; |
case NET_ICMP_DEST_UNREACH: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet ); |
} |
break; |
case NET_ICMP_SOURCE_QUENCH: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_source_quench_msg( 0, packet ); |
} |
case NET_ICMP_TIME_EXCEEDED: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet ); |
} |
break; |
case NET_ICMP_PARAMETERPROB: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( & call )))){ |
ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet ); |
} |
default: |
ERROR_CODE = ENOTSUP; |
ERROR_CODE = icmp_process_message( & call ); |
} |
|
answer_call( callid, ERROR_CODE, & answer, answer_count ); |
572,6 → 751,37 |
return EOK; |
} |
|
int icmp_process_message( ipc_call_t * call ){ |
ERROR_DECLARE; |
|
packet_t packet; |
|
switch( IPC_GET_METHOD( * call )){ |
case NET_ICMP_DEST_UNREACH: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet ); |
} |
return ERROR_CODE; |
case NET_ICMP_SOURCE_QUENCH: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
ERROR_CODE = icmp_source_quench_msg( 0, packet ); |
} |
return ERROR_CODE; |
case NET_ICMP_TIME_EXCEEDED: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet ); |
} |
return ERROR_CODE; |
case NET_ICMP_PARAMETERPROB: |
if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet ); |
} |
return ERROR_CODE; |
default: |
return ENOTSUP; |
} |
} |
|
int icmp_release_and_return( packet_t packet, int result ){ |
pq_release( icmp_globals.net_phone, packet_get_id( packet )); |
return result; |