Changeset aadf01e in mainline for uspace/srv/net/nil


Ignore:
Timestamp:
2010-03-07T15:13:28Z (15 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)

Location:
uspace/srv/net/nil
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/nil/eth/eth.c

    raa85487 raadf01e  
    7070/** Reserved packet prefix length.
    7171 */
    72 #define ETH_PREFIX              ( sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ))
     72#define ETH_PREFIX              (sizeof(eth_header_t) + sizeof(eth_header_lsap_t) + sizeof(eth_header_snap_t))
    7373
    7474/** Reserved packet suffix length.
    7575 */
    76 #define ETH_SUFFIX              sizeof( eth_fcs_t )
     76#define ETH_SUFFIX              sizeof(eth_fcs_t)
    7777
    7878/** Maximum packet content length.
     
    8686/** Maximum tagged packet content length.
    8787 */
    88 #define ETH_MAX_TAGGED_CONTENT( flags ) ( ETH_MAX_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 ))
     88#define ETH_MAX_TAGGED_CONTENT(flags)   (ETH_MAX_CONTENT - ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? sizeof(eth_header_lsap_t) : 0) - (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))
    8989
    9090/** Minimum tagged packet content length.
    9191 */
    92 #define ETH_MIN_TAGGED_CONTENT( flags ) ( ETH_MIN_CONTENT - (( IS_8023_2_LSAP( flags ) || IS_8023_2_SNAP( flags )) ? sizeof( eth_header_lsap_t ) : 0 ) - ( IS_8023_2_SNAP( flags ) ? sizeof( eth_header_snap_t ) : 0 ))
     92#define ETH_MIN_TAGGED_CONTENT(flags)   (ETH_MIN_CONTENT - ((IS_8023_2_LSAP(flags) || IS_8023_2_SNAP(flags)) ? sizeof(eth_header_lsap_t) : 0) - (IS_8023_2_SNAP(flags) ? sizeof(eth_header_snap_t) : 0))
    9393
    9494/** Dummy flag shift value.
     
    103103 *  Preamble and FCS are mandatory part of the packets.
    104104 */
    105 #define ETH_DUMMY                               ( 1 << ETH_DUMMY_SHIFT )
     105#define ETH_DUMMY                               (1 << ETH_DUMMY_SHIFT)
    106106
    107107/** Returns the dummy flag.
    108108 *  @see ETH_DUMMY
    109109 */
    110 #define IS_DUMMY( flags )               (( flags ) & ETH_DUMMY )
     110#define IS_DUMMY(flags)         ((flags) &ETH_DUMMY)
    111111
    112112/** Device mode flags.
     
    115115 *  @see ETH_8023_2_SNAP
    116116 */
    117 #define ETH_MODE_MASK                   ( 3 << ETH_MODE_SHIFT )
     117#define ETH_MODE_MASK                   (3 << ETH_MODE_SHIFT)
    118118
    119119/** DIX Ethernet mode flag.
    120120 */
    121 #define ETH_DIX                                 ( 1 << ETH_MODE_SHIFT )
     121#define ETH_DIX                                 (1 << ETH_MODE_SHIFT)
    122122
    123123/** Returns whether the DIX Ethernet mode flag is set.
     
    125125 *  @see ETH_DIX
    126126 */
    127 #define IS_DIX( flags )                 ((( flags ) & ETH_MODE_MASK ) == ETH_DIX )
     127#define IS_DIX(flags)                   (((flags) &ETH_MODE_MASK) == ETH_DIX)
    128128
    129129/** 802.3 + 802.2 + LSAP mode flag.
    130130 */
    131 #define ETH_8023_2_LSAP                 ( 2 << ETH_MODE_SHIFT )
     131#define ETH_8023_2_LSAP                 (2 << ETH_MODE_SHIFT)
    132132
    133133/** Returns whether the 802.3 + 802.2 + LSAP mode flag is set.
     
    135135 *  @see ETH_8023_2_LSAP
    136136 */
    137 #define IS_8023_2_LSAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_LSAP )
     137#define IS_8023_2_LSAP(flags)   (((flags) &ETH_MODE_MASK) == ETH_8023_2_LSAP)
    138138
    139139/** 802.3 + 802.2 + LSAP + SNAP mode flag.
    140140 */
    141 #define ETH_8023_2_SNAP                 ( 3 << ETH_MODE_SHIFT )
     141#define ETH_8023_2_SNAP                 (3 << ETH_MODE_SHIFT)
    142142
    143143/** Returns whether the 802.3 + 802.2 + LSAP + SNAP mode flag is set.
     
    145145 *  @see ETH_8023_2_SNAP
    146146 */
    147 #define IS_8023_2_SNAP( flags ) ((( flags ) & ETH_MODE_MASK ) == ETH_8023_2_SNAP )
     147#define IS_8023_2_SNAP(flags)   (((flags) &ETH_MODE_MASK) == ETH_8023_2_SNAP)
    148148
    149149/** Type definition of the ethernet address type.
     
    180180 *  @param[in,out] icall The message parameters.
    181181 */
    182 void    eth_receiver( ipc_callid_t iid, ipc_call_t * icall );
     182void eth_receiver(ipc_callid_t iid, ipc_call_t * icall);
    183183
    184184/** Registers new device or updates the MTU of an existing one.
     
    194194 *  @returns Other error codes as defined for the netif_get_addr_req() function.
    195195 */
    196 int     eth_device_message( device_id_t device_id, services_t service, size_t mtu );
     196int eth_device_message(device_id_t device_id, services_t service, size_t mtu);
    197197
    198198/** Registers receiving module service.
     
    204204 *  @returns ENOMEM if there is not enough memory left.
    205205 */
    206 int     eth_register_message( services_t service, int phone );
     206int eth_register_message(services_t service, int phone);
    207207
    208208/** Returns the device packet dimensions for sending.
     
    216216 *  @returns ENOENT if there is no such device.
    217217 */
    218 int     eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
     218int eth_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);
    219219
    220220/** Returns the device hardware address.
     
    226226 *  @returns ENOENT if there no such device.
    227227 */
    228 int     eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address );
     228int eth_addr_message(device_id_t device_id, eth_addr_type_t type, measured_string_ref * address);
    229229
    230230/** Sends the packet queue.
     
    237237 *  @returns EINVAL if the service parameter is not known.
    238238 */
    239 int     eth_send_message( device_id_t device_id, packet_t packet, services_t sender );
     239int eth_send_message(device_id_t device_id, packet_t packet, services_t sender);
    240240
    241241/*@}*/
     
    251251 *  @returns NULL if the packet address length is not big enough.
    252252 */
    253 eth_proto_ref   eth_process_packet( int flags, packet_t packet );
     253eth_proto_ref eth_process_packet(int flags, packet_t packet);
    254254
    255255/** Prepares the packet for sending.
     
    264264 *  @returns ENOMEM if there is not enough memory in the packet.
    265265 */
    266 int     eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu );
    267 
    268 DEVICE_MAP_IMPLEMENT( eth_devices, eth_device_t )
    269 
    270 INT_MAP_IMPLEMENT( eth_protos, eth_proto_t )
    271 
    272 int     nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
    273         int                             index;
    274         eth_proto_ref   proto;
    275 
    276         fibril_rwlock_read_lock( & eth_globals.protos_lock );
    277         for( index = eth_protos_count( & eth_globals.protos ) - 1; index >= 0; -- index ){
    278                 proto = eth_protos_get_index( & eth_globals.protos, index );
    279                 if( proto && proto->phone ) il_device_state_msg( proto->phone, device_id, state, proto->service );
    280         }
    281         fibril_rwlock_read_unlock( & eth_globals.protos_lock );
     266int eth_prepare_packet(int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu);
     267
     268DEVICE_MAP_IMPLEMENT(eth_devices, eth_device_t)
     269
     270INT_MAP_IMPLEMENT(eth_protos, eth_proto_t)
     271
     272int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){
     273        int index;
     274        eth_proto_ref proto;
     275
     276        fibril_rwlock_read_lock(&eth_globals.protos_lock);
     277        for(index = eth_protos_count(&eth_globals.protos) - 1; index >= 0; -- index){
     278                proto = eth_protos_get_index(&eth_globals.protos, index);
     279                if(proto && proto->phone){
     280                        il_device_state_msg(proto->phone, device_id, state, proto->service);
     281                }
     282        }
     283        fibril_rwlock_read_unlock(&eth_globals.protos_lock);
    282284        return EOK;
    283285}
    284286
    285 int nil_initialize( int net_phone ){
     287int nil_initialize(int net_phone){
    286288        ERROR_DECLARE;
    287289
    288         fibril_rwlock_initialize( & eth_globals.devices_lock );
    289         fibril_rwlock_initialize( & eth_globals.protos_lock );
    290         fibril_rwlock_write_lock( & eth_globals.devices_lock );
    291         fibril_rwlock_write_lock( & eth_globals.protos_lock );
     290        fibril_rwlock_initialize(&eth_globals.devices_lock);
     291        fibril_rwlock_initialize(&eth_globals.protos_lock);
     292        fibril_rwlock_write_lock(&eth_globals.devices_lock);
     293        fibril_rwlock_write_lock(&eth_globals.protos_lock);
    292294        eth_globals.net_phone = net_phone;
    293         eth_globals.broadcast_addr = measured_string_create_bulk( "\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE( uint8_t, char, ETH_ADDR ));
    294         if( ! eth_globals.broadcast_addr ) return ENOMEM;
    295         ERROR_PROPAGATE( eth_devices_initialize( & eth_globals.devices ));
    296         if( ERROR_OCCURRED( eth_protos_initialize( & eth_globals.protos ))){
    297                 eth_devices_destroy( & eth_globals.devices );
     295        eth_globals.broadcast_addr = measured_string_create_bulk("\xFF\xFF\xFF\xFF\xFF\xFF", CONVERT_SIZE(uint8_t, char, ETH_ADDR));
     296        if(! eth_globals.broadcast_addr){
     297                return ENOMEM;
     298        }
     299        ERROR_PROPAGATE(eth_devices_initialize(&eth_globals.devices));
     300        if(ERROR_OCCURRED(eth_protos_initialize(&eth_globals.protos))){
     301                eth_devices_destroy(&eth_globals.devices);
    298302                return ERROR_CODE;
    299303        }
    300         fibril_rwlock_write_unlock( & eth_globals.protos_lock );
    301         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
     304        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
     305        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    302306        return EOK;
    303307}
    304308
    305 int eth_device_message( device_id_t device_id, services_t service, size_t mtu ){
     309int eth_device_message(device_id_t device_id, services_t service, size_t mtu){
    306310        ERROR_DECLARE;
    307311
    308         eth_device_ref  device;
    309         int                             index;
    310         measured_string_t       names[ 2 ] = {{ str_dup("ETH_MODE"), 8 }, { str_dup("ETH_DUMMY"), 9 }};
    311         measured_string_ref     configuration;
    312         size_t                          count = sizeof( names ) / sizeof( measured_string_t );
    313         char *                          data;
    314         eth_proto_ref           proto;
    315 
    316         fibril_rwlock_write_lock( & eth_globals.devices_lock );
     312        eth_device_ref device;
     313        int index;
     314        measured_string_t names[2] = {{str_dup("ETH_MODE"), 8}, {str_dup("ETH_DUMMY"), 9}};
     315        measured_string_ref configuration;
     316        size_t count = sizeof(names) / sizeof(measured_string_t);
     317        char * data;
     318        eth_proto_ref proto;
     319
     320        fibril_rwlock_write_lock(&eth_globals.devices_lock);
    317321        // an existing device?
    318         device = eth_devices_find( & eth_globals.devices, device_id );
    319         if( device ){
    320                 if( device->service != service ){
    321                         printf( "Device %d already exists\n", device->device_id );
    322                         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
     322        device = eth_devices_find(&eth_globals.devices, device_id);
     323        if(device){
     324                if(device->service != service){
     325                        printf("Device %d already exists\n", device->device_id);
     326                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    323327                        return EEXIST;
    324328                }else{
    325329                        // update mtu
    326                         if(( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))){
     330                        if((mtu > 0) && (mtu <= ETH_MAX_TAGGED_CONTENT(device->flags))){
    327331                                device->mtu = mtu;
    328332                        }else{
    329                                  device->mtu = ETH_MAX_TAGGED_CONTENT( device->flags );
     333                                 device->mtu = ETH_MAX_TAGGED_CONTENT(device->flags);
    330334                        }
    331                         printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
    332                         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
     335                        printf("Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu);
     336                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    333337                        // notify all upper layer modules
    334                         fibril_rwlock_read_lock( & eth_globals.protos_lock );
    335                         for( index = 0; index < eth_protos_count( & eth_globals.protos ); ++ index ){
    336                                 proto = eth_protos_get_index( & eth_globals.protos, index );
    337                                 if ( proto->phone ){
    338                                         il_mtu_changed_msg( proto->phone, device->device_id, device->mtu, proto->service );
     338                        fibril_rwlock_read_lock(&eth_globals.protos_lock);
     339                        for(index = 0; index < eth_protos_count(&eth_globals.protos); ++ index){
     340                                proto = eth_protos_get_index(&eth_globals.protos, index);
     341                                if (proto->phone){
     342                                        il_mtu_changed_msg(proto->phone, device->device_id, device->mtu, proto->service);
    339343                                }
    340344                        }
    341                         fibril_rwlock_read_unlock( & eth_globals.protos_lock );
     345                        fibril_rwlock_read_unlock(&eth_globals.protos_lock);
    342346                        return EOK;
    343347                }
    344348        }else{
    345349                // create a new device
    346                 device = ( eth_device_ref ) malloc( sizeof( eth_device_t ));
    347                 if( ! device ) return ENOMEM;
     350                device = (eth_device_ref) malloc(sizeof(eth_device_t));
     351                if(! device){
     352                        return ENOMEM;
     353                }
    348354                device->device_id = device_id;
    349355                device->service = service;
    350356                device->flags = 0;
    351                 if(( mtu > 0 ) && ( mtu <= ETH_MAX_TAGGED_CONTENT( device->flags ))){
     357                if((mtu > 0) && (mtu <= ETH_MAX_TAGGED_CONTENT(device->flags))){
    352358                        device->mtu = mtu;
    353359                }else{
    354                          device->mtu = ETH_MAX_TAGGED_CONTENT( device->flags );
    355                 }
    356                 configuration = & names[ 0 ];
    357                 if( ERROR_OCCURRED( net_get_device_conf_req( eth_globals.net_phone, device->device_id, & configuration, count, & data ))){
    358                         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
    359                         free( device );
     360                         device->mtu = ETH_MAX_TAGGED_CONTENT(device->flags);
     361                }
     362                configuration = &names[0];
     363                if(ERROR_OCCURRED(net_get_device_conf_req(eth_globals.net_phone, device->device_id, &configuration, count, &data))){
     364                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     365                        free(device);
    360366                        return ERROR_CODE;
    361367                }
    362                 if( configuration ){
    363                         if( ! str_lcmp( configuration[ 0 ].value, "DIX", configuration[ 0 ].length )){
     368                if(configuration){
     369                        if(! str_lcmp(configuration[0].value, "DIX", configuration[0].length)){
    364370                                device->flags |= ETH_DIX;
    365                         }else if( ! str_lcmp( configuration[ 0 ].value, "8023_2_LSAP", configuration[ 0 ].length )){
     371                        }else if(! str_lcmp(configuration[0].value, "8023_2_LSAP", configuration[0].length)){
    366372                                device->flags |= ETH_8023_2_LSAP;
    367373                        }else device->flags |= ETH_8023_2_SNAP;
    368                         if(( configuration[ 1 ].value ) && ( configuration[ 1 ].value[ 0 ] == 'y' )){
     374                        if((configuration[1].value) && (configuration[1].value[0] == 'y')){
    369375                                device->flags |= ETH_DUMMY;
    370376                        }
    371                         net_free_settings( configuration, data );
     377                        net_free_settings(configuration, data);
    372378                }else{
    373379                        device->flags |= ETH_8023_2_SNAP;
    374380                }
    375381                // bind the device driver
    376                 device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, eth_receiver );
    377                 if( device->phone < 0 ){
    378                         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
    379                         free( device );
     382                device->phone = netif_bind_service(device->service, device->device_id, SERVICE_ETHERNET, eth_receiver);
     383                if(device->phone < 0){
     384                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     385                        free(device);
    380386                        return device->phone;
    381387                }
    382388                // get hardware address
    383                 if( ERROR_OCCURRED( netif_get_addr_req( device->phone, device->device_id, & device->addr, & device->addr_data ))){
    384                         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
    385                         free( device );
     389                if(ERROR_OCCURRED(netif_get_addr_req(device->phone, device->device_id, &device->addr, &device->addr_data))){
     390                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     391                        free(device);
    386392                        return ERROR_CODE;
    387393                }
    388394                // add to the cache
    389                 index = eth_devices_add( & eth_globals.devices, device->device_id, device );
    390                 if( index < 0 ){
    391                         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
    392                         free( device->addr );
    393                         free( device->addr_data );
    394                         free( device );
     395                index = eth_devices_add(&eth_globals.devices, device->device_id, device);
     396                if(index < 0){
     397                        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
     398                        free(device->addr);
     399                        free(device->addr_data);
     400                        free(device);
    395401                        return index;
    396402                }
    397                 printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", device->device_id, device->service, device->mtu, device->addr_data[ 0 ], device->addr_data[ 1 ], device->addr_data[ 2 ], device->addr_data[ 3 ], device->addr_data[ 4 ], device->addr_data[ 5 ], device->flags );
    398         }
    399         fibril_rwlock_write_unlock( & eth_globals.devices_lock );
     403                printf("New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n\taddress\t= %X:%X:%X:%X:%X:%X\n\tflags\t= 0x%x\n", device->device_id, device->service, device->mtu, device->addr_data[0], device->addr_data[1], device->addr_data[2], device->addr_data[3], device->addr_data[4], device->addr_data[5], device->flags);
     404        }
     405        fibril_rwlock_write_unlock(&eth_globals.devices_lock);
    400406        return EOK;
    401407}
    402408
    403 eth_proto_ref eth_process_packet( int flags, packet_t packet ){
     409eth_proto_ref eth_process_packet(int flags, packet_t packet){
    404410        ERROR_DECLARE;
    405411
    406         eth_header_snap_ref     header;
    407         size_t                          length;
    408         eth_type_t                      type;
    409         size_t                          prefix;
    410         size_t                          suffix;
    411         eth_fcs_ref                     fcs;
    412         uint8_t *                       data;
    413 
    414         length = packet_get_data_length( packet );
    415         if( IS_DUMMY( flags )){
    416                 packet_trim( packet, sizeof( eth_preamble_t ), 0 );
    417         }
    418         if( length < sizeof( eth_header_t ) + ETH_MIN_CONTENT + ( IS_DUMMY( flags ) ? ETH_SUFFIX : 0 )) return NULL;
    419         data = packet_get_data( packet );
    420         header = ( eth_header_snap_ref ) data;
    421         type = ntohs( header->header.ethertype );
    422         if( type >= ETH_MIN_PROTO ){
     412        eth_header_snap_ref header;
     413        size_t length;
     414        eth_type_t type;
     415        size_t prefix;
     416        size_t suffix;
     417        eth_fcs_ref fcs;
     418        uint8_t * data;
     419
     420        length = packet_get_data_length(packet);
     421        if(IS_DUMMY(flags)){
     422                packet_trim(packet, sizeof(eth_preamble_t), 0);
     423        }
     424        if(length < sizeof(eth_header_t) + ETH_MIN_CONTENT + (IS_DUMMY(flags) ? ETH_SUFFIX : 0)) return NULL;
     425        data = packet_get_data(packet);
     426        header = (eth_header_snap_ref) data;
     427        type = ntohs(header->header.ethertype);
     428        if(type >= ETH_MIN_PROTO){
    423429                // DIX Ethernet
    424                 prefix = sizeof( eth_header_t );
     430                prefix = sizeof(eth_header_t);
    425431                suffix = 0;
    426                 fcs = ( eth_fcs_ref ) data + length - sizeof( eth_fcs_t );
    427                 length -= sizeof( eth_fcs_t );
    428         }else if( type <= ETH_MAX_CONTENT ){
     432                fcs = (eth_fcs_ref) data + length - sizeof(eth_fcs_t);
     433                length -= sizeof(eth_fcs_t);
     434        }else if(type <= ETH_MAX_CONTENT){
    429435                // translate "LSAP" values
    430                 if(( header->lsap.dsap == ETH_LSAP_GLSAP ) && ( header->lsap.ssap == ETH_LSAP_GLSAP )){
     436                if((header->lsap.dsap == ETH_LSAP_GLSAP) && (header->lsap.ssap == ETH_LSAP_GLSAP)){
    431437                        // raw packet
    432438                        // discard
    433439                        return NULL;
    434                 }else if(( header->lsap.dsap == ETH_LSAP_SNAP ) && ( header->lsap.ssap == ETH_LSAP_SNAP )){
     440                }else if((header->lsap.dsap == ETH_LSAP_SNAP) && (header->lsap.ssap == ETH_LSAP_SNAP)){
    435441                        // IEEE 802.3 + 802.2 + LSAP + SNAP
    436442                        // organization code not supported
    437                         type = ntohs( header->snap.ethertype );
    438                         prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t );
     443                        type = ntohs(header->snap.ethertype);
     444                        prefix = sizeof(eth_header_t) + sizeof(eth_header_lsap_t) + sizeof(eth_header_snap_t);
    439445                }else{
    440446                        // IEEE 802.3 + 802.2 LSAP
    441                         type = lsap_map( header->lsap.dsap );
    442                         prefix = sizeof( eth_header_t ) + sizeof( eth_header_lsap_t);
    443                 }
    444                 suffix = ( type < ETH_MIN_CONTENT ) ? ETH_MIN_CONTENT - type : 0u;
    445                 fcs = ( eth_fcs_ref ) data + prefix + type + suffix;
     447                        type = lsap_map(header->lsap.dsap);
     448                        prefix = sizeof(eth_header_t) + sizeof(eth_header_lsap_t);
     449                }
     450                suffix = (type < ETH_MIN_CONTENT) ? ETH_MIN_CONTENT - type : 0u;
     451                fcs = (eth_fcs_ref) data + prefix + type + suffix;
    446452                suffix += length - prefix - type;
    447453                length = prefix + type + suffix;
     
    450456                return NULL;
    451457        }
    452         if( IS_DUMMY( flags )){
    453                 if(( ~ compute_crc32( ~ 0u, data, length * 8 )) != ntohl( * fcs )){
     458        if(IS_DUMMY(flags)){
     459                if((~ compute_crc32(~ 0u, data, length * 8)) != ntohl(*fcs)){
    454460                        return NULL;
    455461                }
    456                 suffix += sizeof( eth_fcs_t );
    457         }
    458         if( ERROR_OCCURRED( packet_set_addr( packet, header->header.source_address, header->header.destination_address, ETH_ADDR ))
    459         || ERROR_OCCURRED( packet_trim( packet, prefix, suffix ))){
     462                suffix += sizeof(eth_fcs_t);
     463        }
     464        if(ERROR_OCCURRED(packet_set_addr(packet, header->header.source_address, header->header.destination_address, ETH_ADDR))
     465                || ERROR_OCCURRED(packet_trim(packet, prefix, suffix))){
    460466                return NULL;
    461467        }
    462         return eth_protos_find( & eth_globals.protos, type );
    463 }
    464 
    465 int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
    466         eth_proto_ref   proto;
    467         packet_t                next;
    468         eth_device_ref  device;
    469         int                             flags;
    470 
    471         fibril_rwlock_read_lock( & eth_globals.devices_lock );
    472         device = eth_devices_find( & eth_globals.devices, device_id );
    473         if( ! device ){
    474                 fibril_rwlock_read_unlock( & eth_globals.devices_lock );
     468        return eth_protos_find(&eth_globals.protos, type);
     469}
     470
     471int nil_received_msg(int nil_phone, device_id_t device_id, packet_t packet, services_t target){
     472        eth_proto_ref proto;
     473        packet_t next;
     474        eth_device_ref device;
     475        int flags;
     476
     477        fibril_rwlock_read_lock(&eth_globals.devices_lock);
     478        device = eth_devices_find(&eth_globals.devices, device_id);
     479        if(! device){
     480                fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    475481                return ENOENT;
    476482        }
    477483        flags = device->flags;
    478         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
    479         fibril_rwlock_read_lock( & eth_globals.protos_lock );
     484        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
     485        fibril_rwlock_read_lock(&eth_globals.protos_lock);
    480486        do{
    481                 next = pq_detach( packet );
    482                 proto = eth_process_packet( flags, packet );
    483                 if( proto ){
    484                         il_received_msg( proto->phone, device_id, packet, proto->service );
     487                next = pq_detach(packet);
     488                proto = eth_process_packet(flags, packet);
     489                if(proto){
     490                        il_received_msg(proto->phone, device_id, packet, proto->service);
    485491                }else{
    486492                        // drop invalid/unknown
    487                         pq_release( eth_globals.net_phone, packet_get_id( packet ));
     493                        pq_release(eth_globals.net_phone, packet_get_id(packet));
    488494                }
    489495                packet = next;
    490         }while( packet );
    491         fibril_rwlock_read_unlock( & eth_globals.protos_lock );
     496        }while(packet);
     497        fibril_rwlock_read_unlock(&eth_globals.protos_lock);
    492498        return EOK;
    493499}
    494500
    495 int eth_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
    496         eth_device_ref  device;
    497 
    498         if( !( addr_len && prefix && content && suffix )) return EBADMEM;
    499         fibril_rwlock_read_lock( & eth_globals.devices_lock );
    500         device = eth_devices_find( & eth_globals.devices, device_id );
    501         if( ! device ){
    502                 fibril_rwlock_read_unlock( & eth_globals.devices_lock );
     501int eth_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){
     502        eth_device_ref device;
     503
     504        if(!(addr_len && prefix && content && suffix)){
     505                return EBADMEM;
     506        }
     507        fibril_rwlock_read_lock(&eth_globals.devices_lock);
     508        device = eth_devices_find(&eth_globals.devices, device_id);
     509        if(! device){
     510                fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    503511                return ENOENT;
    504512        }
    505         * content = device->mtu;
    506         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
    507         * addr_len = ETH_ADDR;
    508         * prefix = ETH_PREFIX;
    509         * suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
     513        *content = device->mtu;
     514        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
     515        *addr_len = ETH_ADDR;
     516        *prefix = ETH_PREFIX;
     517        *suffix = ETH_MIN_CONTENT + ETH_SUFFIX;
    510518        return EOK;
    511519}
    512520
    513 int eth_addr_message( device_id_t device_id, eth_addr_type_t type, measured_string_ref * address ){
    514         eth_device_ref  device;
    515 
    516         if( ! address ) return EBADMEM;
    517         if( type == ETH_BROADCAST_ADDR ){
    518                 * address = eth_globals.broadcast_addr;
     521int eth_addr_message(device_id_t device_id, eth_addr_type_t type, measured_string_ref * address){
     522        eth_device_ref device;
     523
     524        if(! address){
     525                return EBADMEM;
     526        }
     527        if(type == ETH_BROADCAST_ADDR){
     528                *address = eth_globals.broadcast_addr;
    519529        }else{
    520                 fibril_rwlock_read_lock( & eth_globals.devices_lock );
    521                 device = eth_devices_find( & eth_globals.devices, device_id );
    522                 if( ! device ){
    523                         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
     530                fibril_rwlock_read_lock(&eth_globals.devices_lock);
     531                device = eth_devices_find(&eth_globals.devices, device_id);
     532                if(! device){
     533                        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    524534                        return ENOENT;
    525535                }
    526                 * address = device->addr;
    527                 fibril_rwlock_read_unlock( & eth_globals.devices_lock );
    528         }
    529         return ( * address ) ? EOK : ENOENT;
    530 }
    531 
    532 int eth_register_message( services_t service, int phone ){
    533         eth_proto_ref   proto;
    534         int                             protocol;
    535         int                             index;
    536 
    537         protocol = protocol_map( SERVICE_ETHERNET, service );
    538         if( ! protocol ) return ENOENT;
    539         fibril_rwlock_write_lock( & eth_globals.protos_lock );
    540         proto = eth_protos_find( & eth_globals.protos, protocol );
    541         if( proto ){
     536                *address = device->addr;
     537                fibril_rwlock_read_unlock(&eth_globals.devices_lock);
     538        }
     539        return (*address) ? EOK : ENOENT;
     540}
     541
     542int eth_register_message(services_t service, int phone){
     543        eth_proto_ref proto;
     544        int protocol;
     545        int index;
     546
     547        protocol = protocol_map(SERVICE_ETHERNET, service);
     548        if(! protocol){
     549                return ENOENT;
     550        }
     551        fibril_rwlock_write_lock(&eth_globals.protos_lock);
     552        proto = eth_protos_find(&eth_globals.protos, protocol);
     553        if(proto){
    542554                proto->phone = phone;
    543                 fibril_rwlock_write_unlock( & eth_globals.protos_lock );
     555                fibril_rwlock_write_unlock(&eth_globals.protos_lock);
    544556                return EOK;
    545557        }else{
    546                 proto = ( eth_proto_ref ) malloc( sizeof( eth_proto_t ));
    547                 if( ! proto ){
    548                         fibril_rwlock_write_unlock( & eth_globals.protos_lock );
     558                proto = (eth_proto_ref) malloc(sizeof(eth_proto_t));
     559                if(! proto){
     560                        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
    549561                        return ENOMEM;
    550562                }
     
    552564                proto->protocol = protocol;
    553565                proto->phone = phone;
    554                 index = eth_protos_add( & eth_globals.protos, protocol, proto );
    555                 if( index < 0 ){
    556                         fibril_rwlock_write_unlock( & eth_globals.protos_lock );
    557                         free( proto );
     566                index = eth_protos_add(&eth_globals.protos, protocol, proto);
     567                if(index < 0){
     568                        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
     569                        free(proto);
    558570                        return index;
    559571                }
    560572        }
    561         printf( "New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone );
    562         fibril_rwlock_write_unlock( & eth_globals.protos_lock );
     573        printf("New protocol registered:\n\tprotocol\t= 0x%x\n\tservice\t= %d\n\tphone\t= %d\n", proto->protocol, proto->service, proto->phone);
     574        fibril_rwlock_write_unlock(&eth_globals.protos_lock);
    563575        return EOK;
    564576}
    565577
    566 int eth_prepare_packet( int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu ){
    567         eth_header_snap_ref     header;
    568         eth_header_lsap_ref     header_lsap;
    569         eth_header_ref          header_dix;
    570         eth_fcs_ref                     fcs;
    571         uint8_t *                       src;
    572         uint8_t *                       dest;
    573         size_t                          length;
    574         int                                     i;
    575         void *                          padding;
    576         eth_preamble_ref        preamble;
    577 
    578         i = packet_get_addr( packet, & src, & dest );
    579         if( i < 0 ) return i;
    580         if( i != ETH_ADDR ) return EINVAL;
    581         length = packet_get_data_length( packet );
    582         if( length > mtu ) return EINVAL;
    583         if( length < ETH_MIN_TAGGED_CONTENT( flags )){
    584                 padding = packet_suffix( packet, ETH_MIN_TAGGED_CONTENT( flags ) - length );
    585                 if( ! padding ) return ENOMEM;
    586                 bzero( padding, ETH_MIN_TAGGED_CONTENT( flags ) - length );
    587         }
    588         if( IS_DIX( flags )){
    589                 header_dix = PACKET_PREFIX( packet, eth_header_t );
    590                 if( ! header_dix ) return ENOMEM;
    591                 header_dix->ethertype = ( uint16_t ) ethertype;
    592                 memcpy( header_dix->source_address, src_addr, ETH_ADDR );
    593                 memcpy( header_dix->destination_address, dest, ETH_ADDR );
    594                 src = & header_dix->destination_address[ 0 ];
    595         }else if( IS_8023_2_LSAP( flags )){
    596                 header_lsap = PACKET_PREFIX( packet, eth_header_lsap_t );
    597                 if( ! header_lsap ) return ENOMEM;
    598                 header_lsap->header.ethertype = htons( length + sizeof( eth_header_lsap_t ));
    599                 header_lsap->lsap.dsap = lsap_unmap( ntohs( ethertype ));
     578int eth_prepare_packet(int flags, packet_t packet, uint8_t * src_addr, int ethertype, size_t mtu){
     579        eth_header_snap_ref header;
     580        eth_header_lsap_ref header_lsap;
     581        eth_header_ref header_dix;
     582        eth_fcs_ref fcs;
     583        uint8_t * src;
     584        uint8_t * dest;
     585        size_t length;
     586        int i;
     587        void * padding;
     588        eth_preamble_ref preamble;
     589
     590        i = packet_get_addr(packet, &src, &dest);
     591        if(i < 0){
     592                return i;
     593        }
     594        if(i != ETH_ADDR){
     595                return EINVAL;
     596        }
     597        length = packet_get_data_length(packet);
     598        if(length > mtu){
     599                return EINVAL;
     600        }
     601        if(length < ETH_MIN_TAGGED_CONTENT(flags)){
     602                padding = packet_suffix(packet, ETH_MIN_TAGGED_CONTENT(flags) - length);
     603                if(! padding){
     604                        return ENOMEM;
     605                }
     606                bzero(padding, ETH_MIN_TAGGED_CONTENT(flags) - length);
     607        }
     608        if(IS_DIX(flags)){
     609                header_dix = PACKET_PREFIX(packet, eth_header_t);
     610                if(! header_dix){
     611                        return ENOMEM;
     612                }
     613                header_dix->ethertype = (uint16_t) ethertype;
     614                memcpy(header_dix->source_address, src_addr, ETH_ADDR);
     615                memcpy(header_dix->destination_address, dest, ETH_ADDR);
     616                src = &header_dix->destination_address[0];
     617        }else if(IS_8023_2_LSAP(flags)){
     618                header_lsap = PACKET_PREFIX(packet, eth_header_lsap_t);
     619                if(! header_lsap){
     620                        return ENOMEM;
     621                }
     622                header_lsap->header.ethertype = htons(length + sizeof(eth_header_lsap_t));
     623                header_lsap->lsap.dsap = lsap_unmap(ntohs(ethertype));
    600624                header_lsap->lsap.ssap = header_lsap->lsap.dsap;
    601625                header_lsap->lsap.ctrl = IEEE_8023_2_UI;
    602                 memcpy( header_lsap->header.source_address, src_addr, ETH_ADDR );
    603                 memcpy( header_lsap->header.destination_address, dest, ETH_ADDR );
    604                 src = & header_lsap->header.destination_address[ 0 ];
    605         }else if( IS_8023_2_SNAP( flags )){
    606                 header = PACKET_PREFIX( packet, eth_header_snap_t );
    607                 if( ! header ) return ENOMEM;
    608                 header->header.ethertype = htons( length + sizeof( eth_header_lsap_t ) + sizeof( eth_header_snap_t ));
    609                 header->lsap.dsap = ( uint16_t ) ETH_LSAP_SNAP;
     626                memcpy(header_lsap->header.source_address, src_addr, ETH_ADDR);
     627                memcpy(header_lsap->header.destination_address, dest, ETH_ADDR);
     628                src = &header_lsap->header.destination_address[0];
     629        }else if(IS_8023_2_SNAP(flags)){
     630                header = PACKET_PREFIX(packet, eth_header_snap_t);
     631                if(! header){
     632                        return ENOMEM;
     633                }
     634                header->header.ethertype = htons(length + sizeof(eth_header_lsap_t) + sizeof(eth_header_snap_t));
     635                header->lsap.dsap = (uint16_t) ETH_LSAP_SNAP;
    610636                header->lsap.ssap = header->lsap.dsap;
    611637                header->lsap.ctrl = IEEE_8023_2_UI;
    612                 for( i = 0; i < 3; ++ i ) header->snap.protocol[ i ] = 0;
    613                 header->snap.ethertype = ( uint16_t ) ethertype;
    614                 memcpy( header->header.source_address, src_addr, ETH_ADDR );
    615                 memcpy( header->header.destination_address, dest, ETH_ADDR );
    616                 src = & header->header.destination_address[ 0 ];
    617         }
    618         if( IS_DUMMY( flags )){
    619                 preamble = PACKET_PREFIX( packet, eth_preamble_t );
    620                 if( ! preamble ) return ENOMEM;
    621                 for( i = 0; i < 7; ++ i ) preamble->preamble[ i ] = ETH_PREAMBLE;
     638                for(i = 0; i < 3; ++ i){
     639                        header->snap.protocol[i] = 0;
     640                }
     641                header->snap.ethertype = (uint16_t) ethertype;
     642                memcpy(header->header.source_address, src_addr, ETH_ADDR);
     643                memcpy(header->header.destination_address, dest, ETH_ADDR);
     644                src = &header->header.destination_address[0];
     645        }
     646        if(IS_DUMMY(flags)){
     647                preamble = PACKET_PREFIX(packet, eth_preamble_t);
     648                if(! preamble){
     649                        return ENOMEM;
     650                }
     651                for(i = 0; i < 7; ++ i){
     652                        preamble->preamble[i] = ETH_PREAMBLE;
     653                }
    622654                preamble->sfd = ETH_SFD;
    623                 fcs = PACKET_SUFFIX( packet, eth_fcs_t );
    624                 if( ! fcs ) return ENOMEM;
    625                 * fcs = htonl( ~ compute_crc32( ~ 0u, src, length * 8 ));
     655                fcs = PACKET_SUFFIX(packet, eth_fcs_t);
     656                if(! fcs){
     657                        return ENOMEM;
     658                }
     659                *fcs = htonl(~ compute_crc32(~ 0u, src, length * 8));
    626660        }
    627661        return EOK;
    628662}
    629663
    630 int eth_send_message( device_id_t device_id, packet_t packet, services_t sender ){
     664int eth_send_message(device_id_t device_id, packet_t packet, services_t sender){
    631665        ERROR_DECLARE;
    632666
    633         eth_device_ref          device;
    634         packet_t                        next;
    635         packet_t                        tmp;
    636         int                                     ethertype;
    637 
    638         ethertype = htons( protocol_map( SERVICE_ETHERNET, sender ));
    639         if( ! ethertype ){
    640                 pq_release( eth_globals.net_phone, packet_get_id( packet ));
     667        eth_device_ref device;
     668        packet_t next;
     669        packet_t tmp;
     670        int ethertype;
     671
     672        ethertype = htons(protocol_map(SERVICE_ETHERNET, sender));
     673        if(! ethertype){
     674                pq_release(eth_globals.net_phone, packet_get_id(packet));
    641675                return EINVAL;
    642676        }
    643         fibril_rwlock_read_lock( & eth_globals.devices_lock );
    644         device = eth_devices_find( & eth_globals.devices, device_id );
    645         if( ! device ){
    646                 fibril_rwlock_read_unlock( & eth_globals.devices_lock );
     677        fibril_rwlock_read_lock(&eth_globals.devices_lock);
     678        device = eth_devices_find(&eth_globals.devices, device_id);
     679        if(! device){
     680                fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    647681                return ENOENT;
    648682        }
     
    650684        next = packet;
    651685        do{
    652                 if( ERROR_OCCURRED( eth_prepare_packet( device->flags, next, ( uint8_t * ) device->addr->value, ethertype, device->mtu ))){
     686                if(ERROR_OCCURRED(eth_prepare_packet(device->flags, next, (uint8_t *) device->addr->value, ethertype, device->mtu))){
    653687                        // release invalid packet
    654                         tmp = pq_detach( next );
    655                         if( next == packet ) packet = tmp;
    656                         pq_release( eth_globals.net_phone, packet_get_id( next ));
     688                        tmp = pq_detach(next);
     689                        if(next == packet){
     690                                packet = tmp;
     691                        }
     692                        pq_release(eth_globals.net_phone, packet_get_id(next));
    657693                        next = tmp;
    658694                }else{
    659                         next = pq_next( next );
    660                 }
    661         }while( next );
     695                        next = pq_next(next);
     696                }
     697        }while(next);
    662698        // send packet queue
    663         if( packet ){
    664                 netif_send_msg( device->phone, device_id, packet, SERVICE_ETHERNET );
    665         }
    666         fibril_rwlock_read_unlock( & eth_globals.devices_lock );
     699        if(packet){
     700                netif_send_msg(device->phone, device_id, packet, SERVICE_ETHERNET);
     701        }
     702        fibril_rwlock_read_unlock(&eth_globals.devices_lock);
    667703        return EOK;
    668704}
    669705
    670 int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
     706int nil_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    671707        ERROR_DECLARE;
    672708
    673         measured_string_ref     address;
    674         packet_t                        packet;
    675 
    676 //      printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
    677         * answer_count = 0;
    678         switch( IPC_GET_METHOD( * call )){
     709        measured_string_ref address;
     710        packet_t packet;
     711
     712//      printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST);
     713        *answer_count = 0;
     714        switch(IPC_GET_METHOD(*call)){
    679715                case IPC_M_PHONE_HUNGUP:
    680716                        return EOK;
    681717                case NET_NIL_DEVICE:
    682                         return eth_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
     718                        return eth_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call));
    683719                case NET_NIL_SEND:
    684                         ERROR_PROPAGATE( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( call )));
    685                         return eth_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
     720                        ERROR_PROPAGATE(packet_translate(eth_globals.net_phone, &packet, IPC_GET_PACKET(call)));
     721                        return eth_send_message(IPC_GET_DEVICE(call), packet, IPC_GET_SERVICE(call));
    686722                case NET_NIL_PACKET_SPACE:
    687                         ERROR_PROPAGATE( eth_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
    688                         * answer_count = 4;
     723                        ERROR_PROPAGATE(eth_packet_space_message(IPC_GET_DEVICE(call), IPC_SET_ADDR(answer), IPC_SET_PREFIX(answer), IPC_SET_CONTENT(answer), IPC_SET_SUFFIX(answer)));
     724                        *answer_count = 4;
    689725                        return EOK;
    690726                case NET_NIL_ADDR:
    691                         ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_LOCAL_ADDR, & address ));
    692                         return measured_strings_reply( address, 1 );
     727                        ERROR_PROPAGATE(eth_addr_message(IPC_GET_DEVICE(call), ETH_LOCAL_ADDR, &address));
     728                        return measured_strings_reply(address, 1);
    693729                case NET_NIL_BROADCAST_ADDR:
    694                         ERROR_PROPAGATE( eth_addr_message( IPC_GET_DEVICE( call ), ETH_BROADCAST_ADDR, & address ));
    695                         return measured_strings_reply( address, 1 );
     730                        ERROR_PROPAGATE(eth_addr_message(IPC_GET_DEVICE(call), ETH_BROADCAST_ADDR, &address));
     731                        return measured_strings_reply(address, 1);
    696732                case IPC_M_CONNECT_TO_ME:
    697                         return eth_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
     733                        return eth_register_message(NIL_GET_PROTO(call), IPC_GET_PHONE(call));
    698734        }
    699735        return ENOTSUP;
    700736}
    701737
    702 void eth_receiver( ipc_callid_t iid, ipc_call_t * icall ){
     738void eth_receiver(ipc_callid_t iid, ipc_call_t * icall){
    703739        ERROR_DECLARE;
    704740
    705         packet_t                packet;
    706 
    707         while( true ){
    708 //              printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
    709                 switch( IPC_GET_METHOD( * icall )){
     741        packet_t packet;
     742
     743        while(true){
     744//              printf("message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST);
     745                switch(IPC_GET_METHOD(*icall)){
    710746                        case NET_NIL_DEVICE_STATE:
    711                                 nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
    712                                 ipc_answer_0( iid, EOK );
     747                                nil_device_state_msg(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
     748                                ipc_answer_0(iid, EOK);
    713749                                break;
    714750                        case NET_NIL_RECEIVED:
    715                                 if( ! ERROR_OCCURRED( packet_translate( eth_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
    716                                         ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
     751                                if(! ERROR_OCCURRED(packet_translate(eth_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){
     752                                        ERROR_CODE = nil_received_msg(0, IPC_GET_DEVICE(icall), packet, 0);
    717753                                }
    718                                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
     754                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    719755                                break;
    720756                        default:
    721                                 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP );
    722                 }
    723                 iid = async_get_call( icall );
     757                                ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
     758                }
     759                iid = async_get_call(icall);
    724760        }
    725761}
  • uspace/srv/net/nil/eth/eth.h

    raa85487 raadf01e  
    7373 *  @see device.h
    7474 */
    75 DEVICE_MAP_DECLARE( eth_devices, eth_device_t )
     75DEVICE_MAP_DECLARE(eth_devices, eth_device_t)
    7676
    7777/** Ethernet protocol map.
     
    7979 *  @see int_map.h
    8080 */
    81 INT_MAP_DECLARE( eth_protos, eth_proto_t )
     81INT_MAP_DECLARE(eth_protos, eth_proto_t)
    8282
    8383/** Ethernet device specific data.
     
    8686        /** Device identifier.
    8787         */
    88         device_id_t                     device_id;
     88        device_id_t device_id;
    8989        /** Device driver service.
    9090         */
    91         services_t                      service;
     91        services_t service;
    9292        /** Driver phone.
    9393         */
    94         int                                     phone;
     94        int phone;
    9595        /** Maximal transmission unit.
    9696         */
    97         size_t                          mtu;
     97        size_t mtu;
    9898        /** Various device flags.
    9999         *  @see ETH_DUMMY
    100100         *  @see ETH_MODE_MASK
    101101         */
    102         int                                     flags;
     102        int flags;
    103103        /** Actual device hardware address.
    104104         */
    105         measured_string_ref     addr;
     105        measured_string_ref addr;
    106106        /** Actual device hardware address data.
    107107         */
    108         char *                          addr_data;
     108        char * addr_data;
    109109};
    110110
     
    114114        /** Protocol service.
    115115         */
    116         services_t      service;
     116        services_t service;
    117117        /** Protocol identifier.
    118118         */
    119         int                     protocol;
     119        int protocol;
    120120        /** Protocol module phone.
    121121         */
    122         int                     phone;
     122        int phone;
    123123};
    124124
     
    128128        /** Networking module phone.
    129129         */
    130         int                             net_phone;
     130        int net_phone;
    131131        /** Safety lock for devices.
    132132         */
    133         fibril_rwlock_t         devices_lock;
     133        fibril_rwlock_t devices_lock;
    134134        /** All known Ethernet devices.
    135135         */
    136         eth_devices_t   devices;
     136        eth_devices_t devices;
    137137        /** Safety lock for protocols.
    138138         */
    139         fibril_rwlock_t         protos_lock;
     139        fibril_rwlock_t protos_lock;
    140140        /** Protocol map.
    141141         *  Service phone map for each protocol.
    142142         */
    143         eth_protos_t    protos;
     143        eth_protos_t protos;
    144144        /** Broadcast device hardware address.
    145145         */
    146         measured_string_ref     broadcast_addr;
     146        measured_string_ref broadcast_addr;
    147147};
    148148
  • uspace/srv/net/nil/eth/eth_header.h

    raa85487 raadf01e  
    121121struct eth_ieee_lsap{
    122122        /** Destination Service Access Point identifier.
    123          *      The possible values are assigned by an IEEE committee.
    124          */
    125         uint8_t         dsap;
     123         * The possible values are assigned by an IEEE committee.
     124         */
     125        uint8_t dsap;
    126126        /** Source Service Access Point identifier.
    127          *      The possible values are assigned by an IEEE committee.
    128          */
    129         uint8_t         ssap;
     127         * The possible values are assigned by an IEEE committee.
     128         */
     129        uint8_t ssap;
    130130        /** Control parameter.
    131          *      The possible values are assigned by an IEEE committee.
    132          */
    133         uint8_t         ctrl;
     131         * The possible values are assigned by an IEEE committee.
     132         */
     133        uint8_t ctrl;
    134134} __attribute__ ((packed));
    135135
     
    139139        /** Protocol identifier or organization code.
    140140         */
    141         uint8_t         protocol[ 3 ];
     141        uint8_t protocol[3];
    142142        /** Ethernet protocol identifier in the network byte order (big endian).
    143143         *  @see ethernet_protocols.h
    144144         */
    145         uint16_t        ethertype;
     145        uint16_t ethertype;
    146146} __attribute__ ((packed));
    147147
     
    153153         *  All should be set to ETH_PREAMBLE.
    154154         */
    155         uint8_t         preamble[ 7 ];
     155        uint8_t preamble[7];
    156156        /** Start of Frame Delimiter used for the frame transmission synchronization.
    157157         *  Should be set to ETH_SFD.
    158158         */
    159         uint8_t         sfd;
     159        uint8_t sfd;
    160160} __attribute__ ((packed));
    161161
     
    165165        /** Destination host Ethernet address (MAC address).
    166166         */
    167         uint8_t         destination_address[ ETH_ADDR ];
     167        uint8_t destination_address[ETH_ADDR];
    168168        /** Source host Ethernet address (MAC address).
    169169         */
    170         uint8_t         source_address[ ETH_ADDR ];
     170        uint8_t source_address[ETH_ADDR];
    171171        /** Ethernet protocol identifier in the network byte order (big endian).
    172172         *  @see ethernet_protocols.h
    173173         */
    174         uint16_t        ethertype;
     174        uint16_t ethertype;
    175175} __attribute__ ((packed));
    176176
     
    180180        /** Ethernet header.
    181181         */
    182         eth_header_t            header;
     182        eth_header_t header;
    183183        /** LSAP extension.
    184184         *  If DSAP and SSAP are set to ETH_LSAP_SNAP the SNAP extension is being used.
    185185         *  If DSAP and SSAP fields are equal to ETH_RAW the raw Ethernet packet without any extensions is being used and the frame content starts rigth after the two fields.
    186186         */
    187         eth_ieee_lsap_t         lsap;
     187        eth_ieee_lsap_t lsap;
    188188} __attribute__ ((packed));
    189189
     
    193193        /** Ethernet header.
    194194         */
    195         eth_header_t            header;
     195        eth_header_t header;
    196196        /** LSAP extension.
    197197         *  If DSAP and SSAP are set to ETH_LSAP_SNAP the SNAP extension is being used.
    198198         *  If DSAP and SSAP fields are equal to ETH_RAW the raw Ethernet packet without any extensions is being used and the frame content starts rigth after the two fields.
    199199         */
    200         eth_ieee_lsap_t         lsap;
     200        eth_ieee_lsap_t lsap;
    201201        /** SNAP extension.
    202202         */
    203         eth_snap_t                      snap;
     203        eth_snap_t snap;
    204204} __attribute__ ((packed));
    205205
  • uspace/srv/net/nil/eth/eth_module.c

    raa85487 raadf01e  
    5959/** Prints the module name.
    6060 */
    61 void    module_print_name( void );
     61void module_print_name(void);
    6262
    6363/** Starts the Ethernet module.
     
    6969 *  @returns Other error codes as defined for the REGISTER_ME() macro function.
    7070 */
    71 int     module_start( async_client_conn_t client_connection );
     71int module_start(async_client_conn_t client_connection);
    7272
    7373/** Passes the parameters to the module specific nil_message() function.
     
    8080 *  @returns Other error codes as defined for each specific module message function.
    8181 */
    82 int     module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
     82int module_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count);
    8383
    84 void module_print_name( void ){
    85         printf( "%s", NAME );
     84void module_print_name(void){
     85        printf("%s", NAME);
    8686}
    8787
    88 int module_start( async_client_conn_t client_connection ){
     88int module_start(async_client_conn_t client_connection){
    8989        ERROR_DECLARE;
    9090
    91         ipcarg_t        phonehash;
    92         int                     net_phone;
     91        ipcarg_t phonehash;
     92        int net_phone;
    9393
    94         async_set_client_connection( client_connection );
    95         net_phone = net_connect_module( SERVICE_NETWORKING );
    96         ERROR_PROPAGATE( pm_init());
    97         if( ERROR_OCCURRED( nil_initialize( net_phone ))
    98         || ERROR_OCCURRED( REGISTER_ME( SERVICE_ETHERNET, & phonehash ))){
     94        async_set_client_connection(client_connection);
     95        net_phone = net_connect_module(SERVICE_NETWORKING);
     96        ERROR_PROPAGATE(pm_init());
     97        if(ERROR_OCCURRED(nil_initialize(net_phone))
     98                || ERROR_OCCURRED(REGISTER_ME(SERVICE_ETHERNET, &phonehash))){
    9999                pm_destroy();
    100100                return ERROR_CODE;
     
    107107}
    108108
    109 int     module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
    110         return nil_message( callid, call, answer, answer_count );
     109int module_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     110        return nil_message(callid, call, answer, answer_count);
    111111}
    112112
  • uspace/srv/net/nil/nil_messages.h

    raa85487 raadf01e  
    8282/** Returns the protocol service message parameter.
    8383 */
    84 #define NIL_GET_PROTO( call )           ( services_t ) IPC_GET_ARG2( * call )
     84#define NIL_GET_PROTO(call)             (services_t) IPC_GET_ARG2(*call)
    8585
    8686/*@}*/
  • uspace/srv/net/nil/nil_module.h

    raa85487 raadf01e  
    4747 *  @returns Other error codes as defined for each specific module initialize function.
    4848 */
    49 int     nil_initialize( int net_phone );
     49int nil_initialize(int net_phone);
    5050
    5151/** Message processing function.
     
    6060 *  @see IS_NET_NIL_MESSAGE()
    6161 */
    62 int     nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
     62int nil_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count);
    6363
    6464#endif
  • uspace/srv/net/nil/nil_remote.c

    raa85487 raadf01e  
    4646#include "nil_messages.h"
    4747
    48 int nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
    49         return generic_device_state_msg( nil_phone, NET_NIL_DEVICE_STATE, device_id, state, 0 );
     48int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){
     49        return generic_device_state_msg(nil_phone, NET_NIL_DEVICE_STATE, device_id, state, 0);
    5050}
    5151
    52 int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
    53         return generic_received_msg( nil_phone, NET_NIL_RECEIVED, device_id, packet_get_id( packet ), target, 0 );
     52int nil_received_msg(int nil_phone, device_id_t device_id, packet_t packet, services_t target){
     53        return generic_received_msg(nil_phone, NET_NIL_RECEIVED, device_id, packet_get_id(packet), target, 0);
    5454}
    5555
  • uspace/srv/net/nil/nildummy/nildummy.c

    raa85487 raadf01e  
    7777 *  @param[in,out] icall The message parameters.
    7878 */
    79 void    nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall );
     79void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall);
    8080
    8181/** Registers new device or updates the MTU of an existing one.
     
    9090 *  @returns Other error codes as defined for the netif_get_addr_req() function.
    9191 */
    92 int     nildummy_device_message( device_id_t device_id, services_t service, size_t mtu );
     92int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu);
    9393
    9494/** Returns the device packet dimensions for sending.
     
    102102 *  @returns ENOENT if there is no such device.
    103103 */
    104 int     nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix );
     104int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix);
    105105
    106106/** Registers receiving module service.
     
    112112 *  @returns ENOMEM if there is not enough memory left.
    113113 */
    114 int     nildummy_register_message( services_t service, int phone );
     114int nildummy_register_message(services_t service, int phone);
    115115
    116116/** Sends the packet queue.
     
    122122 *  @returns EINVAL if the service parameter is not known.
    123123 */
    124 int     nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender );
     124int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender);
    125125
    126126/** Returns the device hardware address.
     
    131131 *  @returns ENOENT if there no such device.
    132132 */
    133 int     nildummy_addr_message( device_id_t device_id, measured_string_ref * address );
     133int nildummy_addr_message(device_id_t device_id, measured_string_ref * address);
    134134
    135135/*@}*/
    136136
    137 DEVICE_MAP_IMPLEMENT( nildummy_devices, nildummy_device_t )
    138 
    139 int     nil_device_state_msg( int nil_phone, device_id_t device_id, int state ){
    140         fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
    141         if( nildummy_globals.proto.phone ) il_device_state_msg( nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service );
    142         fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
    143         return EOK;
    144 }
    145 
    146 int nil_initialize( int net_phone ){
     137DEVICE_MAP_IMPLEMENT(nildummy_devices, nildummy_device_t)
     138
     139int nil_device_state_msg(int nil_phone, device_id_t device_id, int state){
     140        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     141        if(nildummy_globals.proto.phone){
     142                il_device_state_msg(nildummy_globals.proto.phone, device_id, state, nildummy_globals.proto.service);
     143        }
     144        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
     145        return EOK;
     146}
     147
     148int nil_initialize(int net_phone){
    147149        ERROR_DECLARE;
    148150
    149         fibril_rwlock_initialize( & nildummy_globals.devices_lock );
    150         fibril_rwlock_initialize( & nildummy_globals.protos_lock );
    151         fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
    152         fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
     151        fibril_rwlock_initialize(&nildummy_globals.devices_lock);
     152        fibril_rwlock_initialize(&nildummy_globals.protos_lock);
     153        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
     154        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    153155        nildummy_globals.net_phone = net_phone;
    154156        nildummy_globals.proto.phone = 0;
    155         ERROR_PROPAGATE( nildummy_devices_initialize( & nildummy_globals.devices ));
    156         fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
    157         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    158         return EOK;
    159 }
    160 
    161 int nildummy_device_message( device_id_t device_id, services_t service, size_t mtu ){
     157        ERROR_PROPAGATE(nildummy_devices_initialize(&nildummy_globals.devices));
     158        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
     159        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     160        return EOK;
     161}
     162
     163int nildummy_device_message(device_id_t device_id, services_t service, size_t mtu){
    162164        ERROR_DECLARE;
    163165
    164         nildummy_device_ref     device;
    165         int                                     index;
    166 
    167         fibril_rwlock_write_lock( & nildummy_globals.devices_lock );
     166        nildummy_device_ref device;
     167        int index;
     168
     169        fibril_rwlock_write_lock(&nildummy_globals.devices_lock);
    168170        // an existing device?
    169         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    170         if( device ){
    171                 if( device->service != service ){
    172                         printf( "Device %d already exists\n", device->device_id );
    173                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
     171        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     172        if(device){
     173                if(device->service != service){
     174                        printf("Device %d already exists\n", device->device_id);
     175                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    174176                        return EEXIST;
    175177                }else{
    176178                        // update mtu
    177                         if( mtu > 0 ){
     179                        if(mtu > 0){
    178180                                device->mtu = mtu;
    179181                        }else{
    180182                                device->mtu = NET_DEFAULT_MTU;
    181183                        }
    182                         printf( "Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu );
    183                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
     184                        printf("Device %d already exists:\tMTU\t= %d\n", device->device_id, device->mtu);
     185                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
    184186                        // notify the upper layer module
    185                         fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
    186                         if( nildummy_globals.proto.phone ){
    187                                 il_mtu_changed_msg( nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service );
     187                        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     188                        if(nildummy_globals.proto.phone){
     189                                il_mtu_changed_msg(nildummy_globals.proto.phone, device->device_id, device->mtu, nildummy_globals.proto.service);
    188190                        }
    189                         fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
     191                        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
    190192                        return EOK;
    191193                }
    192194        }else{
    193195                // create a new device
    194                 device = ( nildummy_device_ref ) malloc( sizeof( nildummy_device_t ));
    195                 if( ! device ) return ENOMEM;
     196                device = (nildummy_device_ref) malloc(sizeof(nildummy_device_t));
     197                if(! device){
     198                        return ENOMEM;
     199                }
    196200                device->device_id = device_id;
    197201                device->service = service;
    198                 if( mtu > 0 ){
     202                if(mtu > 0){
    199203                        device->mtu = mtu;
    200204                }else{
     
    202206                }
    203207                // bind the device driver
    204                 device->phone = netif_bind_service( device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver );
    205                 if( device->phone < 0 ){
    206                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    207                         free( device );
     208                device->phone = netif_bind_service(device->service, device->device_id, SERVICE_ETHERNET, nildummy_receiver);
     209                if(device->phone < 0){
     210                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     211                        free(device);
    208212                        return device->phone;
    209213                }
    210214                // get hardware address
    211                 if( ERROR_OCCURRED( netif_get_addr_req( device->phone, device->device_id, & device->addr, & device->addr_data ))){
    212                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    213                         free( device );
     215                if(ERROR_OCCURRED(netif_get_addr_req(device->phone, device->device_id, &device->addr, &device->addr_data))){
     216                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     217                        free(device);
    214218                        return ERROR_CODE;
    215219                }
    216220                // add to the cache
    217                 index = nildummy_devices_add( & nildummy_globals.devices, device->device_id, device );
    218                 if( index < 0 ){
    219                         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    220                         free( device->addr );
    221                         free( device->addr_data );
    222                         free( device );
     221                index = nildummy_devices_add(&nildummy_globals.devices, device->device_id, device);
     222                if(index < 0){
     223                        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     224                        free(device->addr);
     225                        free(device->addr_data);
     226                        free(device);
    223227                        return index;
    224228                }
    225                 printf( "New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu );
    226         }
    227         fibril_rwlock_write_unlock( & nildummy_globals.devices_lock );
    228         return EOK;
    229 }
    230 
    231 int nildummy_addr_message( device_id_t device_id, measured_string_ref * address ){
    232         nildummy_device_ref     device;
    233 
    234         if( ! address ) return EBADMEM;
    235         fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
    236         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    237         if( ! device ){
    238                 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
     229                printf("New device registered:\n\tid\t= %d\n\tservice\t= %d\n\tMTU\t= %d\n", device->device_id, device->service, device->mtu);
     230        }
     231        fibril_rwlock_write_unlock(&nildummy_globals.devices_lock);
     232        return EOK;
     233}
     234
     235int nildummy_addr_message(device_id_t device_id, measured_string_ref * address){
     236        nildummy_device_ref device;
     237
     238        if(! address){
     239                return EBADMEM;
     240        }
     241        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
     242        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     243        if(! device){
     244                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    239245                return ENOENT;
    240246        }
    241         * address = device->addr;
    242         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
    243         return ( * address ) ? EOK : ENOENT;
    244 }
    245 
    246 int nildummy_packet_space_message( device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix ){
    247         nildummy_device_ref     device;
    248 
    249         if( !( addr_len && prefix && content && suffix )) return EBADMEM;
    250         fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
    251         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    252         if( ! device ){
    253                 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
     247        *address = device->addr;
     248        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     249        return (*address) ? EOK : ENOENT;
     250}
     251
     252int nildummy_packet_space_message(device_id_t device_id, size_t * addr_len, size_t * prefix, size_t * content, size_t * suffix){
     253        nildummy_device_ref device;
     254
     255        if(!(addr_len && prefix && content && suffix)){
     256                return EBADMEM;
     257        }
     258        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
     259        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     260        if(! device){
     261                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    254262                return ENOENT;
    255263        }
    256         * content = device->mtu;
    257         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
    258         * addr_len = 0;
    259         * prefix = 0;
    260         * suffix = 0;
    261         return EOK;
    262 }
    263 
    264 int nil_received_msg( int nil_phone, device_id_t device_id, packet_t packet, services_t target ){
    265         packet_t                next;
    266 
    267         fibril_rwlock_read_lock( & nildummy_globals.protos_lock );
    268         if( nildummy_globals.proto.phone ){
     264        *content = device->mtu;
     265        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     266        *addr_len = 0;
     267        *prefix = 0;
     268        *suffix = 0;
     269        return EOK;
     270}
     271
     272int nil_received_msg(int nil_phone, device_id_t device_id, packet_t packet, services_t target){
     273        packet_t next;
     274
     275        fibril_rwlock_read_lock(&nildummy_globals.protos_lock);
     276        if(nildummy_globals.proto.phone){
    269277                do{
    270                         next = pq_detach( packet );
    271                         il_received_msg( nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service );
     278                        next = pq_detach(packet);
     279                        il_received_msg(nildummy_globals.proto.phone, device_id, packet, nildummy_globals.proto.service);
    272280                        packet = next;
    273                 }while( packet );
    274         }
    275         fibril_rwlock_read_unlock( & nildummy_globals.protos_lock );
    276         return EOK;
    277 }
    278 
    279 int nildummy_register_message( services_t service, int phone ){
    280         fibril_rwlock_write_lock( & nildummy_globals.protos_lock );
     281                }while(packet);
     282        }
     283        fibril_rwlock_read_unlock(&nildummy_globals.protos_lock);
     284        return EOK;
     285}
     286
     287int nildummy_register_message(services_t service, int phone){
     288        fibril_rwlock_write_lock(&nildummy_globals.protos_lock);
    281289        nildummy_globals.proto.service = service;
    282290        nildummy_globals.proto.phone = phone;
    283         printf( "New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone );
    284         fibril_rwlock_write_unlock( & nildummy_globals.protos_lock );
    285         return EOK;
    286 }
    287 
    288 int nildummy_send_message( device_id_t device_id, packet_t packet, services_t sender ){
    289         nildummy_device_ref             device;
    290 
    291         fibril_rwlock_read_lock( & nildummy_globals.devices_lock );
    292         device = nildummy_devices_find( & nildummy_globals.devices, device_id );
    293         if( ! device ){
    294                 fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
     291        printf("New protocol registered:\n\tservice\t= %d\n\tphone\t= %d\n", nildummy_globals.proto.service, nildummy_globals.proto.phone);
     292        fibril_rwlock_write_unlock(&nildummy_globals.protos_lock);
     293        return EOK;
     294}
     295
     296int nildummy_send_message(device_id_t device_id, packet_t packet, services_t sender){
     297        nildummy_device_ref device;
     298
     299        fibril_rwlock_read_lock(&nildummy_globals.devices_lock);
     300        device = nildummy_devices_find(&nildummy_globals.devices, device_id);
     301        if(! device){
     302                fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
    295303                return ENOENT;
    296304        }
    297305        // send packet queue
    298         if( packet ){
    299                 netif_send_msg( device->phone, device_id, packet, SERVICE_NILDUMMY );
    300         }
    301         fibril_rwlock_read_unlock( & nildummy_globals.devices_lock );
    302         return EOK;
    303 }
    304 
    305 int nil_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
     306        if(packet){
     307                netif_send_msg(device->phone, device_id, packet, SERVICE_NILDUMMY);
     308        }
     309        fibril_rwlock_read_unlock(&nildummy_globals.devices_lock);
     310        return EOK;
     311}
     312
     313int nil_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    306314        ERROR_DECLARE;
    307315
    308         measured_string_ref     address;
    309         packet_t                        packet;
    310 
    311 //      printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_NIL_FIRST );
    312         * answer_count = 0;
    313         switch( IPC_GET_METHOD( * call )){
     316        measured_string_ref address;
     317        packet_t packet;
     318
     319//      printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NIL_FIRST);
     320        *answer_count = 0;
     321        switch(IPC_GET_METHOD(*call)){
    314322                case IPC_M_PHONE_HUNGUP:
    315323                        return EOK;
    316324                case NET_NIL_DEVICE:
    317                         return nildummy_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_MTU( call ));
     325                        return nildummy_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), IPC_GET_MTU(call));
    318326                case NET_NIL_SEND:
    319                         ERROR_PROPAGATE( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( call )));
    320                         return nildummy_send_message( IPC_GET_DEVICE( call ), packet, IPC_GET_SERVICE( call ));
     327                        ERROR_PROPAGATE(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(call)));
     328                        return nildummy_send_message(IPC_GET_DEVICE(call), packet, IPC_GET_SERVICE(call));
    321329                case NET_NIL_PACKET_SPACE:
    322                         ERROR_PROPAGATE( nildummy_packet_space_message( IPC_GET_DEVICE( call ), IPC_SET_ADDR( answer ), IPC_SET_PREFIX( answer ), IPC_SET_CONTENT( answer ), IPC_SET_SUFFIX( answer )));
    323                         * answer_count = 4;
     330                        ERROR_PROPAGATE(nildummy_packet_space_message(IPC_GET_DEVICE(call), IPC_SET_ADDR(answer), IPC_SET_PREFIX(answer), IPC_SET_CONTENT(answer), IPC_SET_SUFFIX(answer)));
     331                        *answer_count = 4;
    324332                        return EOK;
    325333                case NET_NIL_ADDR:
    326                         ERROR_PROPAGATE( nildummy_addr_message( IPC_GET_DEVICE( call ), & address ));
    327                         return measured_strings_reply( address, 1 );
     334                        ERROR_PROPAGATE(nildummy_addr_message(IPC_GET_DEVICE(call), &address));
     335                        return measured_strings_reply(address, 1);
    328336                case IPC_M_CONNECT_TO_ME:
    329                         return nildummy_register_message( NIL_GET_PROTO( call ), IPC_GET_PHONE( call ));
     337                        return nildummy_register_message(NIL_GET_PROTO(call), IPC_GET_PHONE(call));
    330338        }
    331339        return ENOTSUP;
    332340}
    333341
    334 void nildummy_receiver( ipc_callid_t iid, ipc_call_t * icall ){
     342void nildummy_receiver(ipc_callid_t iid, ipc_call_t * icall){
    335343        ERROR_DECLARE;
    336344
    337         packet_t                packet;
    338 
    339         while( true ){
    340 //              printf( "message %d - %d\n", IPC_GET_METHOD( * icall ), NET_NIL_FIRST );
    341                 switch( IPC_GET_METHOD( * icall )){
     345        packet_t packet;
     346
     347        while(true){
     348//              printf("message %d - %d\n", IPC_GET_METHOD(*icall), NET_NIL_FIRST);
     349                switch(IPC_GET_METHOD(*icall)){
    342350                        case NET_NIL_DEVICE_STATE:
    343                                 ERROR_CODE = nil_device_state_msg( 0, IPC_GET_DEVICE( icall ), IPC_GET_STATE( icall ));
    344                                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
     351                                ERROR_CODE = nil_device_state_msg(0, IPC_GET_DEVICE(icall), IPC_GET_STATE(icall));
     352                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    345353                                break;
    346354                        case NET_NIL_RECEIVED:
    347                                 if( ! ERROR_OCCURRED( packet_translate( nildummy_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){
    348                                         ERROR_CODE = nil_received_msg( 0, IPC_GET_DEVICE( icall ), packet, 0 );
     355                                if(! ERROR_OCCURRED(packet_translate(nildummy_globals.net_phone, &packet, IPC_GET_PACKET(icall)))){
     356                                        ERROR_CODE = nil_received_msg(0, IPC_GET_DEVICE(icall), packet, 0);
    349357                                }
    350                                 ipc_answer_0( iid, ( ipcarg_t ) ERROR_CODE );
     358                                ipc_answer_0(iid, (ipcarg_t) ERROR_CODE);
    351359                                break;
    352360                        default:
    353                                 ipc_answer_0( iid, ( ipcarg_t ) ENOTSUP );
    354                 }
    355                 iid = async_get_call( icall );
     361                                ipc_answer_0(iid, (ipcarg_t) ENOTSUP);
     362                }
     363                iid = async_get_call(icall);
    356364        }
    357365}
  • uspace/srv/net/nil/nildummy/nildummy.h

    raa85487 raadf01e  
    7373 *  @see device.h
    7474 */
    75 DEVICE_MAP_DECLARE( nildummy_devices, nildummy_device_t )
     75DEVICE_MAP_DECLARE(nildummy_devices, nildummy_device_t)
    7676
    7777/** Dummy nil device specific data.
     
    8080        /** Device identifier.
    8181         */
    82         device_id_t                     device_id;
     82        device_id_t device_id;
    8383        /** Device driver service.
    8484         */
    85         services_t                      service;
     85        services_t service;
    8686        /** Driver phone.
    8787         */
    88         int                                     phone;
     88        int phone;
    8989        /** Maximal transmission unit.
    9090         */
    91         size_t                          mtu;
     91        size_t mtu;
    9292        /** Actual device hardware address.
    9393         */
    94         measured_string_ref     addr;
     94        measured_string_ref addr;
    9595        /** Actual device hardware address data.
    9696         */
    97         char *                          addr_data;
     97        char * addr_data;
    9898};
    9999
     
    103103        /** Protocol service.
    104104         */
    105         services_t      service;
     105        services_t service;
    106106        /** Protocol module phone.
    107107         */
    108         int                     phone;
     108        int phone;
    109109};
    110110
     
    114114        /** Networking module phone.
    115115         */
    116         int                             net_phone;
     116        int net_phone;
    117117        /** Safety lock for devices.
    118118         */
    119         fibril_rwlock_t         devices_lock;
     119        fibril_rwlock_t devices_lock;
    120120        /** All known Ethernet devices.
    121121         */
    122         nildummy_devices_t      devices;
     122        nildummy_devices_t devices;
    123123        /** Safety lock for protocols.
    124124         */
    125         fibril_rwlock_t         protos_lock;
     125        fibril_rwlock_t protos_lock;
    126126        /** Default protocol.
    127127         */
    128         nildummy_proto_t        proto;
     128        nildummy_proto_t proto;
    129129};
    130130
  • uspace/srv/net/nil/nildummy/nildummy_module.c

    raa85487 raadf01e  
    5959/** Prints the module name.
    6060 */
    61 void    module_print_name( void );
     61void module_print_name(void);
    6262
    6363/** Starts the dummy nil module.
     
    6969 *  @returns Other error codes as defined for the REGISTER_ME() macro function.
    7070 */
    71 int     module_start( async_client_conn_t client_connection );
     71int module_start(async_client_conn_t client_connection);
    7272
    7373/** Passes the parameters to the module specific nil_message() function.
     
    8080 *  @returns Other error codes as defined for each specific module message function.
    8181 */
    82 int     module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
     82int module_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count);
    8383
    84 void module_print_name( void ){
    85         printf( "%s", NAME );
     84void module_print_name(void){
     85        printf("%s", NAME);
    8686}
    8787
    88 int module_start( async_client_conn_t client_connection ){
     88int module_start(async_client_conn_t client_connection){
    8989        ERROR_DECLARE;
    9090
    91         ipcarg_t        phonehash;
    92         int                     net_phone;
     91        ipcarg_t phonehash;
     92        int net_phone;
    9393
    94         async_set_client_connection( client_connection );
    95         net_phone = net_connect_module( SERVICE_NETWORKING );
    96         ERROR_PROPAGATE( pm_init());
    97         if( ERROR_OCCURRED( nil_initialize( net_phone ))
    98         || ERROR_OCCURRED( REGISTER_ME( SERVICE_NILDUMMY, & phonehash ))){
     94        async_set_client_connection(client_connection);
     95        net_phone = net_connect_module(SERVICE_NETWORKING);
     96        ERROR_PROPAGATE(pm_init());
     97        if(ERROR_OCCURRED(nil_initialize(net_phone))
     98                || ERROR_OCCURRED(REGISTER_ME(SERVICE_NILDUMMY, &phonehash))){
    9999                pm_destroy();
    100100                return ERROR_CODE;
     
    107107}
    108108
    109 int     module_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
    110         return nil_message( callid, call, answer, answer_count );
     109int module_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     110        return nil_message(callid, call, answer, answer_count);
    111111}
    112112
Note: See TracChangeset for help on using the changeset viewer.