Changeset aadf01e in mainline for uspace/srv/net/tl/icmp/icmp.c


Ignore:
Timestamp:
2010-03-07T15:13:28Z (14 years ago)
Author:
Lukas Mejdrech <lukasmejdrech@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
936835e
Parents:
aa85487
Message:

Coding style (no functional change)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/tl/icmp/icmp.c

    raa85487 raadf01e  
    102102 *  @returns The computed checksum.
    103103 */
    104 #define ICMP_CHECKSUM( header, length )         htons( ip_checksum(( uint8_t * ) ( header ), ( length )))
     104#define ICMP_CHECKSUM(header, length)           htons(ip_checksum((uint8_t *) (header), (length)))
    105105
    106106/** An echo request datagrams pattern.
     
    113113 *  @returns The computed ICMP reply data key.
    114114 */
    115 #define ICMP_GET_REPLY_KEY( id, sequence )      ((( id ) << 16 ) | ( sequence & 0xFFFF ))
     115#define ICMP_GET_REPLY_KEY(id, sequence)        (((id) << 16) | (sequence &0xFFFF))
    116116
    117117/** Processes the received ICMP packet.
     
    125125 *  @returns Other error codes as defined for the icmp_process_packet() function.
    126126 */
    127 int     icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
     127int icmp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
    128128
    129129/** Processes the received ICMP packet.
     
    140140 *  @returns Other error codes as defined for the ip_client_process_packet() function.
    141141 */
    142 int     icmp_process_packet( packet_t packet, services_t error );
     142int icmp_process_packet(packet_t packet, services_t error);
    143143
    144144/** Processes the client messages.
     
    151151 *  @see icmp_api.h
    152152 */
    153 int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
     153int icmp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
    154154
    155155/** Processes the generic client messages.
     
    164164 *  @see icmp_interface.h
    165165 */
    166 int     icmp_process_message( ipc_call_t * call );
     166int icmp_process_message(ipc_call_t * call);
    167167
    168168/** Releases the packet and returns the result.
     
    171171 *  @returns The result parameter.
    172172 */
    173 int     icmp_release_and_return( packet_t packet, int result );
     173int icmp_release_and_return(packet_t packet, int result);
    174174
    175175/** Requests an echo message.
     
    192192 *  @returns EPARTY if there was an internal error.
    193193 */
    194 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 );
     194int 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);
    195195
    196196/** Prepares the ICMP error packet.
     
    201201 *  @returns NULL on errors.
    202202 */
    203 icmp_header_ref icmp_prepare_packet( packet_t packet );
     203icmp_header_ref icmp_prepare_packet(packet_t packet);
    204204
    205205/** Sends the ICMP message.
     
    218218 *  @returns EPERM if the error message is not allowed.
    219219 */
    220 int     icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment );
     220int icmp_send_packet(icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment);
    221221
    222222/** Tries to set the pending reply result as the received message type.
     
    230230 *  @returns EOK.
    231231 */
    232 int     icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code );
     232int icmp_process_echo_reply(packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code);
    233233
    234234/** Assigns a new identifier for the connection.
     
    239239 *  @returns ENOTCONN if no free identifier have been found.
    240240 */
    241 int     icmp_bind_free_id( icmp_echo_ref echo_data );
     241int icmp_bind_free_id(icmp_echo_ref echo_data);
    242242
    243243/** ICMP global data.
     
    245245icmp_globals_t  icmp_globals;
    246246
    247 INT_MAP_IMPLEMENT( icmp_replies, icmp_reply_t );
    248 
    249 INT_MAP_IMPLEMENT( icmp_echo_data, icmp_echo_t );
    250 
    251 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 ){
    252         icmp_echo_ref   echo_data;
    253         int                             res;
    254 
    255         fibril_rwlock_write_lock( & icmp_globals.lock );
     247INT_MAP_IMPLEMENT(icmp_replies, icmp_reply_t);
     248
     249INT_MAP_IMPLEMENT(icmp_echo_data, icmp_echo_t);
     250
     251int 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){
     252        icmp_echo_ref echo_data;
     253        int res;
     254
     255        fibril_rwlock_write_lock(&icmp_globals.lock);
    256256        // use the phone as the echo data index
    257         echo_data = icmp_echo_data_find( & icmp_globals.echo_data, icmp_phone );
    258         if( ! echo_data ){
     257        echo_data = icmp_echo_data_find(&icmp_globals.echo_data, icmp_phone);
     258        if(! echo_data){
    259259                res = ENOENT;
    260260        }else{
    261                 res = icmp_echo( echo_data->identifier, echo_data->sequence_number, size, timeout, ttl, tos, dont_fragment, addr, addrlen );
    262                 if( echo_data->sequence_number < MAX_UINT16 ){
     261                res = icmp_echo(echo_data->identifier, echo_data->sequence_number, size, timeout, ttl, tos, dont_fragment, addr, addrlen);
     262                if(echo_data->sequence_number < MAX_UINT16){
    263263                        ++ echo_data->sequence_number;
    264264                }else{
     
    266266                }
    267267        }
    268         fibril_rwlock_write_unlock( & icmp_globals.lock );
     268        fibril_rwlock_write_unlock(&icmp_globals.lock);
    269269        return res;
    270270}
    271271
    272 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 ){
     272int 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){
    273273        ERROR_DECLARE;
    274274
    275         icmp_header_ref header;
    276         packet_t                packet;
    277         size_t                  length;
    278         uint8_t *               data;
    279         icmp_reply_ref                  reply;
    280         int                             reply_key;
    281         int                             result;
    282         int                             index;
    283 
    284         if( addrlen <= 0 ){
     275        icmp_header_ref header;
     276        packet_t packet;
     277        size_t length;
     278        uint8_t * data;
     279        icmp_reply_ref reply;
     280        int reply_key;
     281        int result;
     282        int index;
     283
     284        if(addrlen <= 0){
    285285                return EINVAL;
    286286        }
    287         length = ( size_t ) addrlen;
     287        length = (size_t) addrlen;
    288288        // TODO do not ask all the time
    289         ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.packet_dimension ));
    290         packet = packet_get_4( icmp_globals.net_phone, size, icmp_globals.packet_dimension.addr_len, ICMP_HEADER_SIZE + icmp_globals.packet_dimension.prefix, icmp_globals.packet_dimension.suffix );
    291         if( ! packet ) return ENOMEM;
     289        ERROR_PROPAGATE(ip_packet_size_req(icmp_globals.ip_phone, -1, &icmp_globals.packet_dimension));
     290        packet = packet_get_4(icmp_globals.net_phone, size, icmp_globals.packet_dimension.addr_len, ICMP_HEADER_SIZE + icmp_globals.packet_dimension.prefix, icmp_globals.packet_dimension.suffix);
     291        if(! packet){
     292                return ENOMEM;
     293        }
    292294
    293295        // prepare the requesting packet
    294296        // set the destination address
    295         if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( const uint8_t * ) addr, length ))){
    296                 return icmp_release_and_return( packet, ERROR_CODE );
     297        if(ERROR_OCCURRED(packet_set_addr(packet, NULL, (const uint8_t *) addr, length))){
     298                return icmp_release_and_return(packet, ERROR_CODE);
    297299        }
    298300        // allocate space in the packet
    299         data = ( uint8_t * ) packet_suffix( packet, size );
    300         if( ! data ){
    301                 return icmp_release_and_return( packet, ENOMEM );
     301        data = (uint8_t *) packet_suffix(packet, size);
     302        if(! data){
     303                return icmp_release_and_return(packet, ENOMEM);
    302304        }
    303305        // fill the data
    304306        length = 0;
    305         while( size > length + sizeof( ICMP_ECHO_TEXT )){
    306                 memcpy( data + length, ICMP_ECHO_TEXT, sizeof( ICMP_ECHO_TEXT ));
    307                 length += sizeof( ICMP_ECHO_TEXT );
    308         }
    309         memcpy( data + length, ICMP_ECHO_TEXT, size - length );
     307        while(size > length + sizeof(ICMP_ECHO_TEXT)){
     308                memcpy(data + length, ICMP_ECHO_TEXT, sizeof(ICMP_ECHO_TEXT));
     309                length += sizeof(ICMP_ECHO_TEXT);
     310        }
     311        memcpy(data + length, ICMP_ECHO_TEXT, size - length);
    310312        // prefix the header
    311         header = PACKET_PREFIX( packet, icmp_header_t );
    312         if( ! header ){
    313                 return icmp_release_and_return( packet, ENOMEM );
    314         }
    315         bzero( header, sizeof( * header ));
     313        header = PACKET_PREFIX(packet, icmp_header_t);
     314        if(! header){
     315                return icmp_release_and_return(packet, ENOMEM);
     316        }
     317        bzero(header, sizeof(*header));
    316318        header->un.echo.identifier = id;
    317319        header->un.echo.sequence_number = sequence;
    318320
    319321        // prepare the reply structure
    320         reply = malloc( sizeof( * reply ));
    321         if( ! reply ){
    322                 return icmp_release_and_return( packet, ENOMEM );
    323         }
    324         fibril_mutex_initialize( & reply->mutex );
    325         fibril_mutex_lock( & reply->mutex );
    326         fibril_condvar_initialize( & reply->condvar );
    327         reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
    328         index = icmp_replies_add( & icmp_globals.replies, reply_key, reply );
    329         if( index < 0 ){
    330                 free( reply );
    331                 return icmp_release_and_return( packet, index );
     322        reply = malloc(sizeof(*reply));
     323        if(! reply){
     324                return icmp_release_and_return(packet, ENOMEM);
     325        }
     326        fibril_mutex_initialize(&reply->mutex);
     327        fibril_mutex_lock(&reply->mutex);
     328        fibril_condvar_initialize(&reply->condvar);
     329        reply_key = ICMP_GET_REPLY_KEY(header->un.echo.identifier, header->un.echo.sequence_number);
     330        index = icmp_replies_add(&icmp_globals.replies, reply_key, reply);
     331        if(index < 0){
     332                free(reply);
     333                return icmp_release_and_return(packet, index);
    332334        }
    333335
    334336        // unlock the globals and wait for a reply
    335         fibril_rwlock_write_unlock( & icmp_globals.lock );
     337        fibril_rwlock_write_unlock(&icmp_globals.lock);
    336338
    337339        // send the request
    338         icmp_send_packet( ICMP_ECHO, 0, packet, header, 0, ttl, tos, dont_fragment );
     340        icmp_send_packet(ICMP_ECHO, 0, packet, header, 0, ttl, tos, dont_fragment);
    339341
    340342        // wait for a reply
    341343        // timeout in microseconds
    342         if( ERROR_OCCURRED( fibril_condvar_wait_timeout( & reply->condvar, & reply->mutex, timeout * 1000 ))){
     344        if(ERROR_OCCURRED(fibril_condvar_wait_timeout(&reply->condvar, &reply->mutex, timeout * 1000))){
    343345                result = ERROR_CODE;
    344346
    345347                // lock the globals again and clean up
    346                 fibril_rwlock_write_lock( & icmp_globals.lock );
     348                fibril_rwlock_write_lock(&icmp_globals.lock);
    347349        }else{
    348350                // read the result
     
    350352
    351353                // release the reply structure
    352                 fibril_mutex_unlock( & reply->mutex );
     354                fibril_mutex_unlock(&reply->mutex);
    353355        }
    354356
    355357        // destroy the reply structure
    356         icmp_replies_exclude_index( & icmp_globals.replies, index );
     358        icmp_replies_exclude_index(&icmp_globals.replies, index);
    357359        return result;
    358360}
    359361
    360 int icmp_destination_unreachable_msg( int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet ){
    361         icmp_header_ref header;
    362 
    363         header = icmp_prepare_packet( packet );
    364         if( ! header ){
    365                 return icmp_release_and_return( packet, ENOMEM );
    366         }
    367         if( mtu ){
     362int icmp_destination_unreachable_msg(int icmp_phone, icmp_code_t code, icmp_param_t mtu, packet_t packet){
     363        icmp_header_ref header;
     364
     365        header = icmp_prepare_packet(packet);
     366        if(! header){
     367                return icmp_release_and_return(packet, ENOMEM);
     368        }
     369        if(mtu){
    368370                header->un.frag.mtu = mtu;
    369371        }
    370         return icmp_send_packet( ICMP_DEST_UNREACH, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
    371 }
    372 
    373 int icmp_source_quench_msg( int icmp_phone, packet_t packet ){
    374         icmp_header_ref header;
    375 
    376         header = icmp_prepare_packet( packet );
    377         if( ! header ){
    378                 return icmp_release_and_return( packet, ENOMEM );
    379         }
    380         return icmp_send_packet( ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP, 0, 0, 0 );
    381 }
    382 
    383 int icmp_time_exceeded_msg( int icmp_phone, icmp_code_t code, packet_t packet ){
    384         icmp_header_ref header;
    385 
    386         header = icmp_prepare_packet( packet );
    387         if( ! header ){
    388                 return icmp_release_and_return( packet, ENOMEM );
    389         }
    390         return icmp_send_packet( ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
    391 }
    392 
    393 int icmp_parameter_problem_msg( int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet ){
    394         icmp_header_ref header;
    395 
    396         header = icmp_prepare_packet( packet );
    397         if( ! header ){
    398                 return icmp_release_and_return( packet, ENOMEM );
     372        return icmp_send_packet(ICMP_DEST_UNREACH, code, packet, header, SERVICE_ICMP, 0, 0, 0);
     373}
     374
     375int icmp_source_quench_msg(int icmp_phone, packet_t packet){
     376        icmp_header_ref header;
     377
     378        header = icmp_prepare_packet(packet);
     379        if(! header){
     380                return icmp_release_and_return(packet, ENOMEM);
     381        }
     382        return icmp_send_packet(ICMP_SOURCE_QUENCH, 0, packet, header, SERVICE_ICMP, 0, 0, 0);
     383}
     384
     385int icmp_time_exceeded_msg(int icmp_phone, icmp_code_t code, packet_t packet){
     386        icmp_header_ref header;
     387
     388        header = icmp_prepare_packet(packet);
     389        if(! header){
     390                return icmp_release_and_return(packet, ENOMEM);
     391        }
     392        return icmp_send_packet(ICMP_TIME_EXCEEDED, code, packet, header, SERVICE_ICMP, 0, 0, 0);
     393}
     394
     395int icmp_parameter_problem_msg(int icmp_phone, icmp_code_t code, icmp_param_t pointer, packet_t packet){
     396        icmp_header_ref header;
     397
     398        header = icmp_prepare_packet(packet);
     399        if(! header){
     400                return icmp_release_and_return(packet, ENOMEM);
    399401        }
    400402        header->un.param.pointer = pointer;
    401         return icmp_send_packet( ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP, 0, 0, 0 );
    402 }
    403 
    404 icmp_header_ref icmp_prepare_packet( packet_t packet ){
    405         icmp_header_ref header;
    406         size_t                  header_length;
    407         size_t                  total_length;
    408 
    409         total_length = packet_get_data_length( packet );
    410         if( total_length <= 0 ) return NULL;
    411         header_length = ip_client_header_length( packet );
    412         if( header_length <= 0 ) return NULL;
     403        return icmp_send_packet(ICMP_PARAMETERPROB, code, packet, header, SERVICE_ICMP, 0, 0, 0);
     404}
     405
     406icmp_header_ref icmp_prepare_packet(packet_t packet){
     407        icmp_header_ref header;
     408        size_t header_length;
     409        size_t total_length;
     410
     411        total_length = packet_get_data_length(packet);
     412        if(total_length <= 0){
     413                return NULL;
     414        }
     415        header_length = ip_client_header_length(packet);
     416        if(header_length <= 0){
     417                return NULL;
     418        }
    413419        // truncate if longer than 64 bits (without the IP header)
    414         if(( total_length > header_length + ICMP_KEEP_LENGTH )
    415         && ( packet_trim( packet, 0, total_length - header_length - ICMP_KEEP_LENGTH ) != EOK )){
     420        if((total_length > header_length + ICMP_KEEP_LENGTH)
     421                && (packet_trim(packet, 0, total_length - header_length - ICMP_KEEP_LENGTH) != EOK)){
    416422                return NULL;
    417423        }
    418         header = PACKET_PREFIX( packet, icmp_header_t );
    419         if( ! header ) return NULL;
    420         bzero( header, sizeof( * header ));
     424        header = PACKET_PREFIX(packet, icmp_header_t);
     425        if(! header){
     426                return NULL;
     427        }
     428        bzero(header, sizeof(*header));
    421429        return header;
    422430}
    423431
    424 int icmp_send_packet( icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment ){
     432int icmp_send_packet(icmp_type_t type, icmp_code_t code, packet_t packet, icmp_header_ref header, services_t error, ip_ttl_t ttl, ip_tos_t tos, int dont_fragment){
    425433        ERROR_DECLARE;
    426434
    427435        // do not send an error if disabled
    428         if( error && ( ! icmp_globals.error_reporting )){
    429                 return icmp_release_and_return( packet, EPERM );
     436        if(error && (! icmp_globals.error_reporting)){
     437                return icmp_release_and_return(packet, EPERM);
    430438        }
    431439        header->type = type;
    432440        header->code = code;
    433441        header->checksum = 0;
    434         header->checksum = ICMP_CHECKSUM( header, packet_get_data_length( packet ));
    435         if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_ICMP, ttl, tos, dont_fragment, 0 ))){
    436                 return icmp_release_and_return( packet, ERROR_CODE );
    437         }
    438         return ip_send_msg( icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error );
    439 }
    440 
    441 int icmp_connect_module( services_t service, suseconds_t timeout ){
    442         icmp_echo_ref   echo_data;
    443         icmp_param_t    id;
    444         int                             index;
    445 
    446         echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
    447         if( ! echo_data ) return ENOMEM;
     442        header->checksum = ICMP_CHECKSUM(header, packet_get_data_length(packet));
     443        if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_ICMP, ttl, tos, dont_fragment, 0))){
     444                return icmp_release_and_return(packet, ERROR_CODE);
     445        }
     446        return ip_send_msg(icmp_globals.ip_phone, -1, packet, SERVICE_ICMP, error);
     447}
     448
     449int icmp_connect_module(services_t service, suseconds_t timeout){
     450        icmp_echo_ref echo_data;
     451        icmp_param_t id;
     452        int index;
     453
     454        echo_data = (icmp_echo_ref) malloc(sizeof(*echo_data));
     455        if(! echo_data){
     456                return ENOMEM;
     457        }
    448458        // assign a new identifier
    449         fibril_rwlock_write_lock( & icmp_globals.lock );
    450         index = icmp_bind_free_id( echo_data );
    451         if( index < 0 ){
    452                 free( echo_data );
    453                 fibril_rwlock_write_unlock( & icmp_globals.lock );
     459        fibril_rwlock_write_lock(&icmp_globals.lock);
     460        index = icmp_bind_free_id(echo_data);
     461        if(index < 0){
     462                free(echo_data);
     463                fibril_rwlock_write_unlock(&icmp_globals.lock);
    454464                return index;
    455465        }else{
    456466                id = echo_data->identifier;
    457                 fibril_rwlock_write_unlock( & icmp_globals.lock );
     467                fibril_rwlock_write_unlock(&icmp_globals.lock);
    458468                // return the echo data identifier as the ICMP phone
    459469                return id;
     
    461471}
    462472
    463 int icmp_initialize( async_client_conn_t client_connection ){
     473int icmp_initialize(async_client_conn_t client_connection){
    464474        ERROR_DECLARE;
    465475
    466         measured_string_t       names[] = {{ str_dup("ICMP_ERROR_REPORTING"), 20 }, { str_dup("ICMP_ECHO_REPLYING"), 18 }};
    467         measured_string_ref     configuration;
    468         size_t                          count = sizeof( names ) / sizeof( measured_string_t );
    469         char *                          data;
    470 
    471         fibril_rwlock_initialize( & icmp_globals.lock );
    472         fibril_rwlock_write_lock( & icmp_globals.lock );
    473         icmp_replies_initialize( & icmp_globals.replies );
    474         icmp_echo_data_initialize( & icmp_globals.echo_data );
    475         icmp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg );
    476         if( icmp_globals.ip_phone < 0 ){
     476        measured_string_t names[] = {{str_dup("ICMP_ERROR_REPORTING"), 20}, {str_dup("ICMP_ECHO_REPLYING"), 18}};
     477        measured_string_ref configuration;
     478        size_t count = sizeof(names) / sizeof(measured_string_t);
     479        char * data;
     480
     481        fibril_rwlock_initialize(&icmp_globals.lock);
     482        fibril_rwlock_write_lock(&icmp_globals.lock);
     483        icmp_replies_initialize(&icmp_globals.replies);
     484        icmp_echo_data_initialize(&icmp_globals.echo_data);
     485        icmp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_ICMP, SERVICE_ICMP, client_connection, icmp_received_msg);
     486        if(icmp_globals.ip_phone < 0){
    477487                return icmp_globals.ip_phone;
    478488        }
    479         ERROR_PROPAGATE( ip_packet_size_req( icmp_globals.ip_phone, -1, & icmp_globals.packet_dimension ));
     489        ERROR_PROPAGATE(ip_packet_size_req(icmp_globals.ip_phone, -1, &icmp_globals.packet_dimension));
    480490        icmp_globals.packet_dimension.prefix += ICMP_HEADER_SIZE;
    481491        icmp_globals.packet_dimension.content -= ICMP_HEADER_SIZE;
     
    483493        icmp_globals.error_reporting = NET_DEFAULT_ICMP_ERROR_REPORTING;
    484494        icmp_globals.echo_replying = NET_DEFAULT_ICMP_ECHO_REPLYING;
    485         configuration = & names[ 0 ];
    486         ERROR_PROPAGATE( net_get_conf_req( icmp_globals.net_phone, & configuration, count, & data ));
    487         if( configuration ){
    488                 if( configuration[ 0 ].value ){
    489                         icmp_globals.error_reporting = ( configuration[ 0 ].value[ 0 ] == 'y' );
     495        configuration = &names[0];
     496        ERROR_PROPAGATE(net_get_conf_req(icmp_globals.net_phone, &configuration, count, &data));
     497        if(configuration){
     498                if(configuration[0].value){
     499                        icmp_globals.error_reporting = (configuration[0].value[0] == 'y');
    490500                }
    491                 if( configuration[ 1 ].value ){
    492                         icmp_globals.echo_replying = ( configuration[ 1 ].value[ 0 ] == 'y' );
     501                if(configuration[1].value){
     502                        icmp_globals.echo_replying = (configuration[1].value[0] == 'y');
    493503                }
    494                 net_free_settings( configuration, data );
    495         }
    496         fibril_rwlock_write_unlock( & icmp_globals.lock );
     504                net_free_settings(configuration, data);
     505        }
     506        fibril_rwlock_write_unlock(&icmp_globals.lock);
    497507        return EOK;
    498508}
    499509
    500 int     icmp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
     510int icmp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
    501511        ERROR_DECLARE;
    502512
    503         if( ERROR_OCCURRED( icmp_process_packet( packet, error ))){
    504                 return icmp_release_and_return( packet, ERROR_CODE );
     513        if(ERROR_OCCURRED(icmp_process_packet(packet, error))){
     514                return icmp_release_and_return(packet, ERROR_CODE);
    505515        }
    506516
     
    508518}
    509519
    510 int icmp_process_packet( packet_t packet, services_t error ){
     520int icmp_process_packet(packet_t packet, services_t error){
    511521        ERROR_DECLARE;
    512522
    513         size_t                  length;
    514         uint8_t *               src;
    515         int                             addrlen;
    516         int                             result;
    517         void *                  data;
    518         icmp_header_ref header;
    519         icmp_type_t             type;
    520         icmp_code_t             code;
    521 
    522         if( error ){
    523                 switch( error ){
     523        size_t length;
     524        uint8_t * src;
     525        int addrlen;
     526        int result;
     527        void * data;
     528        icmp_header_ref header;
     529        icmp_type_t type;
     530        icmp_code_t code;
     531
     532        if(error){
     533                switch(error){
    524534                        case SERVICE_ICMP:
    525535                                // process error
    526                                 result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
    527                                 if( result < 0 ) return result;
    528                                 length = ( size_t ) result;
     536                                result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
     537                                if(result < 0){
     538                                        return result;
     539                                }
     540                                length = (size_t) result;
    529541                                // remove the error header
    530                                 ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
     542                                ERROR_PROPAGATE(packet_trim(packet, length, 0));
    531543                                break;
    532544                        default:
     
    535547        }
    536548        // get rid of the ip header
    537         length = ip_client_header_length( packet );
    538         ERROR_PROPAGATE( packet_trim( packet, length, 0 ));
    539 
    540         length = packet_get_data_length( packet );
    541         if( length <= 0 ) return EINVAL;
    542         if( length < ICMP_HEADER_SIZE) return EINVAL;
    543         data = packet_get_data( packet );
    544         if( ! data ) return EINVAL;
     549        length = ip_client_header_length(packet);
     550        ERROR_PROPAGATE(packet_trim(packet, length, 0));
     551
     552        length = packet_get_data_length(packet);
     553        if(length <= 0){
     554                return EINVAL;
     555        }
     556        if(length < ICMP_HEADER_SIZE){
     557                return EINVAL;
     558        }
     559        data = packet_get_data(packet);
     560        if(! data){
     561                return EINVAL;
     562        }
    545563        // get icmp header
    546         header = ( icmp_header_ref ) data;
     564        header = (icmp_header_ref) data;
    547565        // checksum
    548         if( header->checksum ){
    549                 while( ICMP_CHECKSUM( header, length ) != IP_CHECKSUM_ZERO ){
     566        if(header->checksum){
     567                while(ICMP_CHECKSUM(header, length) != IP_CHECKSUM_ZERO){
    550568                        // set the original message type on error notification
    551569                        // type swap observed in Qemu
    552                         if( error ){
    553                                 switch( header->type ){
     570                        if(error){
     571                                switch(header->type){
    554572                                        case ICMP_ECHOREPLY:
    555573                                                header->type = ICMP_ECHO;
     
    560578                }
    561579        }
    562         switch( header->type ){
     580        switch(header->type){
    563581                case ICMP_ECHOREPLY:
    564                         if( error ){
    565                                 return icmp_process_echo_reply( packet, header, type, code );
     582                        if(error){
     583                                return icmp_process_echo_reply(packet, header, type, code);
    566584                        }else{
    567                                 return icmp_process_echo_reply( packet, header, ICMP_ECHO, 0 );
     585                                return icmp_process_echo_reply(packet, header, ICMP_ECHO, 0);
    568586                        }
    569587                case ICMP_ECHO:
    570                         if( error ){
    571                                 return icmp_process_echo_reply( packet, header, type, code );
     588                        if(error){
     589                                return icmp_process_echo_reply(packet, header, type, code);
    572590                        // do not send a reply if disabled
    573                         }else if( icmp_globals.echo_replying ){
    574                                 addrlen = packet_get_addr( packet, & src, NULL );
    575                                 if(( addrlen > 0 )
     591                        }else if(icmp_globals.echo_replying){
     592                                addrlen = packet_get_addr(packet, &src, NULL);
     593                                if((addrlen > 0)
    576594                                // set both addresses to the source one (avoids the source address deletion before setting the destination one)
    577                                 && ( packet_set_addr( packet, src, src, ( size_t ) addrlen ) == EOK )){
     595                                        && (packet_set_addr(packet, src, src, (size_t) addrlen) == EOK)){
    578596                                        // send the reply
    579                                         icmp_send_packet( ICMP_ECHOREPLY, 0, packet, header, 0, 0, 0, 0 );
     597                                        icmp_send_packet(ICMP_ECHOREPLY, 0, packet, header, 0, 0, 0, 0);
    580598                                        return EOK;
    581599                                }else{
     
    597615                case ICMP_SKIP:
    598616                case ICMP_PHOTURIS:
    599                         ip_received_error_msg( icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP );
     617                        ip_received_error_msg(icmp_globals.ip_phone, -1, packet, SERVICE_IP, SERVICE_ICMP);
    600618                        return EOK;
    601619                default:
     
    604622}
    605623
    606 int icmp_process_echo_reply( packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code ){
    607         int                             reply_key;
    608         icmp_reply_ref  reply;
     624int icmp_process_echo_reply(packet_t packet, icmp_header_ref header, icmp_type_t type, icmp_code_t code){
     625        int reply_key;
     626        icmp_reply_ref reply;
    609627
    610628        // compute the reply key
    611         reply_key = ICMP_GET_REPLY_KEY( header->un.echo.identifier, header->un.echo.sequence_number );
    612         pq_release( icmp_globals.net_phone, packet_get_id( packet ));
     629        reply_key = ICMP_GET_REPLY_KEY(header->un.echo.identifier, header->un.echo.sequence_number);
     630        pq_release(icmp_globals.net_phone, packet_get_id(packet));
    613631        // lock the globals
    614         fibril_rwlock_write_lock( & icmp_globals.lock );
     632        fibril_rwlock_write_lock(&icmp_globals.lock);
    615633        // find the pending reply
    616         reply = icmp_replies_find( & icmp_globals.replies, reply_key );
    617         if( reply ){
     634        reply = icmp_replies_find(&icmp_globals.replies, reply_key);
     635        if(reply){
    618636                // set the result
    619637                reply->result = type;
    620638                // notify the main fibril
    621                 fibril_condvar_signal( & reply->condvar );
     639                fibril_condvar_signal(&reply->condvar);
    622640        }else{
    623641                // unlock only if no reply
    624                 fibril_rwlock_write_unlock( & icmp_globals.lock );
     642                fibril_rwlock_write_unlock(&icmp_globals.lock);
    625643        }
    626644        return EOK;
    627645}
    628646
    629 int icmp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
     647int icmp_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    630648        ERROR_DECLARE;
    631649
    632         packet_t                        packet;
    633 
    634         * answer_count = 0;
    635         switch( IPC_GET_METHOD( * call )){
     650        packet_t packet;
     651
     652        *answer_count = 0;
     653        switch(IPC_GET_METHOD(*call)){
    636654                case NET_TL_RECEIVED:
    637                         if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
    638                                 ERROR_CODE = icmp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_ICMP, IPC_GET_ERROR( call ));
     655                        if(! ERROR_OCCURRED(packet_translate(icmp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     656                                ERROR_CODE = icmp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_ICMP, IPC_GET_ERROR(call));
    639657                        }
    640658                        return ERROR_CODE;
    641659                case NET_ICMP_INIT:
    642                         return icmp_process_client_messages( callid, * call );
     660                        return icmp_process_client_messages(callid, * call);
    643661                default:
    644                         return icmp_process_message( call );
     662                        return icmp_process_message(call);
    645663        }
    646664        return ENOTSUP;
    647665}
    648666
    649 int icmp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
     667int icmp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
    650668        ERROR_DECLARE;
    651669
    652         bool                                    keep_on_going = true;
     670        bool keep_on_going = true;
    653671//      fibril_rwlock_t                 lock;
    654         ipc_call_t                              answer;
    655         int                                             answer_count;
    656         size_t                                  length;
    657         struct sockaddr *               addr;
    658         ipc_callid_t                    data_callid;
    659         icmp_echo_ref                   echo_data;
    660         int                                             res;
     672        ipc_call_t answer;
     673        int answer_count;
     674        size_t length;
     675        struct sockaddr * addr;
     676        ipc_callid_t data_callid;
     677        icmp_echo_ref echo_data;
     678        int res;
    661679
    662680        /*
     
    667685        answer_count = 0;
    668686
    669 //      fibril_rwlock_initialize( & lock );
    670 
    671         echo_data = ( icmp_echo_ref ) malloc( sizeof( * echo_data ));
    672         if( ! echo_data ) return ENOMEM;
     687//      fibril_rwlock_initialize(&lock);
     688
     689        echo_data = (icmp_echo_ref) malloc(sizeof(*echo_data));
     690        if(! echo_data){
     691                return ENOMEM;
     692        }
    673693
    674694        // assign a new identifier
    675         fibril_rwlock_write_lock( & icmp_globals.lock );
    676         res = icmp_bind_free_id( echo_data );
    677         fibril_rwlock_write_unlock( & icmp_globals.lock );
    678         if( res < 0 ){
    679                 free( echo_data );
     695        fibril_rwlock_write_lock(&icmp_globals.lock);
     696        res = icmp_bind_free_id(echo_data);
     697        fibril_rwlock_write_unlock(&icmp_globals.lock);
     698        if(res < 0){
     699                free(echo_data);
    680700                return res;
    681701        }
    682702
    683         while( keep_on_going ){
     703        while(keep_on_going){
    684704
    685705                // answer the call
    686                 answer_call( callid, res, & answer, answer_count );
     706                answer_call(callid, res, &answer, answer_count);
    687707
    688708                // refresh data
    689                 refresh_answer( & answer, & answer_count );
     709                refresh_answer(&answer, &answer_count);
    690710
    691711                // get the next call
    692                 callid = async_get_call( & call );
     712                callid = async_get_call(&call);
    693713
    694714                // process the call
    695                 switch( IPC_GET_METHOD( call )){
     715                switch(IPC_GET_METHOD(call)){
    696716                        case IPC_M_PHONE_HUNGUP:
    697717                                keep_on_going = false;
     
    699719                                break;
    700720                        case NET_ICMP_ECHO:
    701 //                              fibril_rwlock_write_lock( & lock );
    702                                 if( ! async_data_write_receive( & data_callid, & length )){
     721//                              fibril_rwlock_write_lock(&lock);
     722                                if(! async_data_write_receive(&data_callid, &length)){
    703723                                        res = EINVAL;
    704724                                }else{
    705                                         addr = malloc( length );
    706                                         if( ! addr ){
     725                                        addr = malloc(length);
     726                                        if(! addr){
    707727                                                res = ENOMEM;
    708728                                        }else{
    709                                                 if( ! ERROR_OCCURRED( async_data_write_finalize( data_callid, addr, length ))){
    710                                                         fibril_rwlock_write_lock( & icmp_globals.lock );
    711                                                         res = icmp_echo( echo_data->identifier, echo_data->sequence_number, 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 );
    712                                                         fibril_rwlock_write_unlock( & icmp_globals.lock );
    713                                                         free( addr );
    714                                                         if( echo_data->sequence_number < MAX_UINT16 ){
     729                                                if(! ERROR_OCCURRED(async_data_write_finalize(data_callid, addr, length))){
     730                                                        fibril_rwlock_write_lock(&icmp_globals.lock);
     731                                                        res = icmp_echo(echo_data->identifier, echo_data->sequence_number, 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);
     732                                                        fibril_rwlock_write_unlock(&icmp_globals.lock);
     733                                                        free(addr);
     734                                                        if(echo_data->sequence_number < MAX_UINT16){
    715735                                                                ++ echo_data->sequence_number;
    716736                                                        }else{
     
    722742                                        }
    723743                                }
    724 //                              fibril_rwlock_write_unlock( & lock );
     744//                              fibril_rwlock_write_unlock(&lock);
    725745                                break;
    726746                        default:
    727                                 res = icmp_process_message( & call );
     747                                res = icmp_process_message(&call);
    728748                }
    729749        }
    730750
    731751        // release the identifier
    732         fibril_rwlock_write_lock( & icmp_globals.lock );
    733         icmp_echo_data_exclude( & icmp_globals.echo_data, echo_data->identifier );
    734         fibril_rwlock_write_unlock( & icmp_globals.lock );
     752        fibril_rwlock_write_lock(&icmp_globals.lock);
     753        icmp_echo_data_exclude(&icmp_globals.echo_data, echo_data->identifier);
     754        fibril_rwlock_write_unlock(&icmp_globals.lock);
    735755        return res;
    736756}
    737757
    738 int icmp_process_message( ipc_call_t * call ){
     758int icmp_process_message(ipc_call_t * call){
    739759        ERROR_DECLARE;
    740760
    741         packet_t        packet;
    742 
    743         switch( IPC_GET_METHOD( * call )){
     761        packet_t packet;
     762
     763        switch(IPC_GET_METHOD(*call)){
    744764                case NET_ICMP_DEST_UNREACH:
    745                         if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
    746                                 ERROR_CODE = icmp_destination_unreachable_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_MTU( call ), packet );
     765                        if(! ERROR_OCCURRED(packet_translate(icmp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     766                                ERROR_CODE = icmp_destination_unreachable_msg(0, ICMP_GET_CODE(call), ICMP_GET_MTU(call), packet);
    747767                        }
    748768                        return ERROR_CODE;
    749769                case NET_ICMP_SOURCE_QUENCH:
    750                         if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
    751                                 ERROR_CODE = icmp_source_quench_msg( 0, packet );
     770                        if(! ERROR_OCCURRED(packet_translate(icmp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     771                                ERROR_CODE = icmp_source_quench_msg(0, packet);
    752772                        }
    753773                        return ERROR_CODE;
    754774                case NET_ICMP_TIME_EXCEEDED:
    755                         if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
    756                                 ERROR_CODE = icmp_time_exceeded_msg( 0, ICMP_GET_CODE( call ), packet );
     775                        if(! ERROR_OCCURRED(packet_translate(icmp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     776                                ERROR_CODE = icmp_time_exceeded_msg(0, ICMP_GET_CODE(call), packet);
    757777                        }
    758778                        return ERROR_CODE;
    759779                case NET_ICMP_PARAMETERPROB:
    760                         if( ! ERROR_OCCURRED( packet_translate( icmp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
    761                                 ERROR_CODE = icmp_parameter_problem_msg( 0, ICMP_GET_CODE( call ), ICMP_GET_POINTER( call ), packet );
     780                        if(! ERROR_OCCURRED(packet_translate(icmp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     781                                ERROR_CODE = icmp_parameter_problem_msg(0, ICMP_GET_CODE(call), ICMP_GET_POINTER(call), packet);
    762782                        }
    763783                        return ERROR_CODE;
     
    767787}
    768788
    769 int     icmp_release_and_return( packet_t packet, int result ){
    770         pq_release( icmp_globals.net_phone, packet_get_id( packet ));
     789int icmp_release_and_return(packet_t packet, int result){
     790        pq_release(icmp_globals.net_phone, packet_get_id(packet));
    771791        return result;
    772792}
    773793
    774 int icmp_bind_free_id( icmp_echo_ref echo_data ){
    775         icmp_param_t    index;
    776 
    777         if( ! echo_data ) return EBADMEM;
     794int icmp_bind_free_id(icmp_echo_ref echo_data){
     795        icmp_param_t index;
     796
     797        if(! echo_data){
     798                return EBADMEM;
     799        }
    778800        // from the last used one
    779801        index = icmp_globals.last_used_id;
     
    781803                ++ index;
    782804                // til the range end
    783                 if( index >= ICMP_FREE_IDS_END ){
     805                if(index >= ICMP_FREE_IDS_END){
    784806                        // start from the range beginning
    785807                        index = ICMP_FREE_IDS_START - 1;
     
    787809                                ++ index;
    788810                                // til the last used one
    789                                 if( index >= icmp_globals.last_used_id ){
     811                                if(index >= icmp_globals.last_used_id){
    790812                                        // none found
    791813                                        return ENOTCONN;
    792814                                }
    793                         }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
     815                        }while(icmp_echo_data_find(&icmp_globals.echo_data, index) != NULL);
    794816                        // found, break immediately
    795817                        break;
    796818                }
    797         }while( icmp_echo_data_find( & icmp_globals.echo_data, index ) != NULL );
     819        }while(icmp_echo_data_find(&icmp_globals.echo_data, index) != NULL);
    798820        echo_data->identifier = index;
    799821        echo_data->sequence_number = 0;
    800         return icmp_echo_data_add( & icmp_globals.echo_data, index, echo_data );
     822        return icmp_echo_data_add(&icmp_globals.echo_data, index, echo_data);
    801823}
    802824
Note: See TracChangeset for help on using the changeset viewer.