Changeset 858fc90 in mainline for uspace/srv/net/net/net.c


Ignore:
Timestamp:
2010-03-15T19:35:25Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6092b56e
Parents:
92307f1 (diff), 4684368 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from bzr://bzr.helenos.org/head.

File:
1 edited

Legend:

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

    r92307f1 r858fc90  
    6666#include "net_messages.h"
    6767
     68/** File read buffer size.
     69 */
     70#define BUFFER_SIZE     256
     71
    6872/** Networking module name.
    6973 */
    7074#define NAME    "Networking"
    7175
    72 /** File read buffer size.
    73  */
    74 #define BUFFER_SIZE     256
     76/** Networking module global data.
     77 */
     78net_globals_t   net_globals;
     79
     80/** Generates new system-unique device identifier.
     81 *  @returns The system-unique devic identifier.
     82 */
     83device_id_t generate_new_device_id(void);
    7584
    7685/** Prints the module name.
    7786 *  @see NAME
    7887 */
    79 void    module_print_name( void );
     88void module_print_name(void);
    8089
    8190/** Starts the networking module.
     
    8695 *  @returns Other error codes as defined for the REGISTER_ME() macro function.
    8796 */
    88 int module_start( async_client_conn_t client_connection );
     97int module_start(async_client_conn_t client_connection);
     98
     99/** Returns the configured values.
     100 *  The network interface configuration is searched first.
     101 *  @param[in] netif_conf The network interface configuration setting.
     102 *  @param[out] configuration The found configured values.
     103 *  @param[in] count The desired settings count.
     104 *  @param[out] data The found configuration settings data.
     105 *  @returns EOK.
     106 */
     107int net_get_conf(measured_strings_ref netif_conf, measured_string_ref configuration, size_t count, char ** data);
     108
     109/** Initializes the networking module.
     110 *  @param[in] client_connection The client connection processing function. The module skeleton propagates its own one.
     111 *  @returns EOK on success.
     112 *  @returns ENOMEM if there is not enough memory left.
     113 */
     114int net_initialize(async_client_conn_t client_connection);
    89115
    90116/** \todo
    91117 */
    92 int     read_configuration_file( const char * directory, const char * filename, measured_strings_ref configuration );
    93 
    94 /** \todo
    95  */
    96 int     parse_line( measured_strings_ref configuration, char * line );
     118int parse_line(measured_strings_ref configuration, char * line);
    97119
    98120/** Reads the networking subsystem global configuration.
     
    100122 *  @returns Other error codes as defined for the add_configuration() function.
    101123 */
    102 int             read_configuration( void );
     124int read_configuration(void);
     125
     126/** \todo
     127 */
     128int read_configuration_file(const char * directory, const char * filename, measured_strings_ref configuration);
     129
     130/** Reads the network interface specific configuration.
     131 *  @param[in] name The network interface name.
     132 *  @param[in,out] netif The network interface structure.
     133 *  @returns EOK on success.
     134 *  @returns Other error codes as defined for the add_configuration() function.
     135 */
     136int read_netif_configuration(const char * name, netif_ref netif);
    103137
    104138/** Starts the network interface according to its configuration.
     
    113147 *  @returns Other error codes as defined for the needed internet layer registering function.
    114148 */
    115 int             start_device( netif_ref netif );
     149int start_device(netif_ref netif);
    116150
    117151/** Reads the configuration and starts all network interfaces.
     
    124158 *  @returns Other error codes as defined for the start_device() function.
    125159 */
    126 int             startup( void );
    127 
    128 /** Generates new system-unique device identifier.
    129  *  @returns The system-unique devic identifier.
    130  */
    131 device_id_t     generate_new_device_id( void );
    132 
    133 /** Returns the configured values.
    134  *  The network interface configuration is searched first.
    135  *  @param[in] netif_conf The network interface configuration setting.
    136  *  @param[out] configuration The found configured values.
    137  *  @param[in] count The desired settings count.
    138  *  @param[out] data The found configuration settings data.
    139  *  @returns EOK.
    140  */
    141 int     net_get_conf( measured_strings_ref netif_conf, measured_string_ref configuration, size_t count, char ** data );
    142 
    143 /** Initializes the networking module.
    144  *  @param[in] client_connection The client connection processing function. The module skeleton propagates its own one.
    145  *  @returns EOK on success.
    146  *  @returns ENOMEM if there is not enough memory left.
    147  */
    148 int     net_initialize( async_client_conn_t client_connection );
    149 
    150 /** Reads the network interface specific configuration.
    151  *  @param[in] name The network interface name.
    152  *  @param[in,out] netif The network interface structure.
    153  *  @returns EOK on success.
    154  *  @returns Other error codes as defined for the add_configuration() function.
    155  */
    156 int     read_netif_configuration( const char * name, netif_ref netif );
    157 
    158 /** Networking module global data.
    159  */
    160 net_globals_t   net_globals;
    161 
    162 DEVICE_MAP_IMPLEMENT( netifs, netif_t )
    163 
    164 GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
    165 
    166 void module_print_name( void ){
    167         printf( "%s", NAME );
    168 }
    169 
    170 int module_start( async_client_conn_t client_connection ){
    171         ERROR_DECLARE;
    172 
    173         ipcarg_t        phonehash;
    174 
    175         async_set_client_connection( client_connection );
    176         ERROR_PROPAGATE( pm_init());
    177         if( ERROR_OCCURRED( net_initialize( client_connection ))
    178         || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
     160int startup(void);
     161
     162GENERIC_CHAR_MAP_IMPLEMENT(measured_strings, measured_string_t)
     163
     164DEVICE_MAP_IMPLEMENT(netifs, netif_t)
     165
     166int add_configuration(measured_strings_ref configuration, const char * name, const char * value){
     167        ERROR_DECLARE;
     168
     169        measured_string_ref setting;
     170
     171        setting = measured_string_create_bulk(value, 0);
     172        if(! setting){
     173                return ENOMEM;
     174        }
     175        // add the configuration setting
     176        if(ERROR_OCCURRED(measured_strings_add(configuration, name, 0, setting))){
     177                free(setting);
     178                return ERROR_CODE;
     179        }
     180        return EOK;
     181}
     182
     183device_id_t generate_new_device_id(void){
     184        return device_assign_devno();
     185}
     186
     187void module_print_name(void){
     188        printf("%s", NAME);
     189}
     190
     191int module_start(async_client_conn_t client_connection){
     192        ERROR_DECLARE;
     193
     194        ipcarg_t phonehash;
     195
     196        async_set_client_connection(client_connection);
     197        ERROR_PROPAGATE(pm_init());
     198        if(ERROR_OCCURRED(net_initialize(client_connection))
     199                || ERROR_OCCURRED(REGISTER_ME(SERVICE_NETWORKING, &phonehash))){
    179200                pm_destroy();
    180201                return ERROR_CODE;
     
    187208}
    188209
    189 int net_initialize( async_client_conn_t client_connection ){
    190         ERROR_DECLARE;
    191 
    192         netifs_initialize( & net_globals.netifs );
    193         char_map_initialize( & net_globals.netif_names );
    194         modules_initialize( & net_globals.modules );
    195         measured_strings_initialize( & net_globals.configuration );
     210int net_connect_module(services_t service){
     211        return EOK;
     212}
     213
     214void net_free_settings(measured_string_ref settings, char * data){
     215}
     216
     217int net_get_conf(measured_strings_ref netif_conf, measured_string_ref configuration, size_t count, char ** data){
     218        measured_string_ref setting;
     219        size_t index;
     220
     221        if(data){
     222                *data = NULL;
     223        }
     224
     225        for(index = 0; index < count; ++ index){
     226                setting = measured_strings_find(netif_conf, configuration[index].value, 0);
     227                if(! setting){
     228                        setting = measured_strings_find(&net_globals.configuration, configuration[index].value, 0);
     229                }
     230                if(setting){
     231                        configuration[index].length = setting->length;
     232                        configuration[index].value = setting->value;
     233                }else{
     234                        configuration[index].length = 0;
     235                        configuration[index].value = NULL;
     236                }
     237        }
     238        return EOK;
     239}
     240
     241int net_get_conf_req(int net_phone, measured_string_ref * configuration, size_t count, char ** data){
     242        if(!(configuration && (count > 0))){
     243                return EINVAL;
     244        }
     245
     246        return net_get_conf(NULL, * configuration, count, data);
     247}
     248
     249int net_get_device_conf_req(int net_phone, device_id_t device_id, measured_string_ref * configuration, size_t count, char ** data){
     250        netif_ref netif;
     251
     252        if(!(configuration && (count > 0))){
     253                return EINVAL;
     254        }
     255
     256        netif = netifs_find(&net_globals.netifs, device_id);
     257        if(netif){
     258                return net_get_conf(&netif->configuration, * configuration, count, data);
     259        }else{
     260                return net_get_conf(NULL, * configuration, count, data);
     261        }
     262}
     263
     264int net_initialize(async_client_conn_t client_connection){
     265        ERROR_DECLARE;
     266
     267        netifs_initialize(&net_globals.netifs);
     268        char_map_initialize(&net_globals.netif_names);
     269        modules_initialize(&net_globals.modules);
     270        measured_strings_initialize(&net_globals.configuration);
    196271
    197272        // TODO dynamic configuration
    198         ERROR_PROPAGATE( read_configuration());
    199 
    200         ERROR_PROPAGATE( add_module( NULL, & net_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0, connect_to_service ));
    201         ERROR_PROPAGATE( add_module( NULL, & net_globals.modules, DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service ));
    202         ERROR_PROPAGATE( add_module( NULL, & net_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service ));
    203         ERROR_PROPAGATE( add_module( NULL, & net_globals.modules, NILDUMMY_NAME, NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service ));
     273        ERROR_PROPAGATE(read_configuration());
     274
     275        ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0, connect_to_service));
     276        ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service));
     277        ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service));
     278        ERROR_PROPAGATE(add_module(NULL, &net_globals.modules, NILDUMMY_NAME, NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service));
    204279
    205280        // build specific initialization
    206         return net_initialize_build( client_connection );
    207 }
    208 
    209 int     net_get_device_conf_req( int net_phone, device_id_t device_id, measured_string_ref * configuration, size_t count, char ** data ){
    210         netif_ref       netif;
    211 
    212         if( !( configuration && ( count > 0 ))) return EINVAL;
    213         netif = netifs_find( & net_globals.netifs, device_id );
    214         if( netif ){
    215                 return net_get_conf( & netif->configuration, * configuration, count, data );
    216         }else{
    217                 return net_get_conf( NULL, * configuration, count, data );
    218         }
    219 }
    220 
    221 int     net_get_conf_req( int net_phone, measured_string_ref * configuration, size_t count, char ** data ){
    222         if( !( configuration && ( count > 0 ))) return EINVAL;
    223         return net_get_conf( NULL, * configuration, count, data );
    224 }
    225 
    226 int     net_get_conf( measured_strings_ref netif_conf, measured_string_ref configuration, size_t count, char ** data ){
    227         measured_string_ref     setting;
    228         size_t                          index;
    229 
    230         if( data ) * data = NULL;
    231         for( index = 0; index < count; ++ index ){
    232                 setting = measured_strings_find( netif_conf, configuration[ index ].value, 0 );
    233                 if( ! setting ){
    234                         setting = measured_strings_find( & net_globals.configuration, configuration[ index ].value, 0 );
    235                 }
    236                 if( setting ){
    237                         configuration[ index ].length = setting->length;
    238                         configuration[ index ].value = setting->value;
    239                 }else{
    240                         configuration[ index ].length = 0;
    241                         configuration[ index ].value = NULL;
    242                 }
    243         }
    244         return EOK;
    245 }
    246 
    247 void net_free_settings( measured_string_ref settings, char * data ){
    248 }
    249 
    250 int net_connect_module( services_t service ){
    251         return EOK;
    252 }
    253 
    254 int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
    255         ERROR_DECLARE;
    256 
    257         measured_string_ref     strings;
    258         char *                          data;
    259 
    260         * answer_count = 0;
    261         switch( IPC_GET_METHOD( * call )){
     281        return net_initialize_build(client_connection);
     282}
     283
     284int net_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     285        ERROR_DECLARE;
     286
     287        measured_string_ref strings;
     288        char * data;
     289
     290        *answer_count = 0;
     291        switch(IPC_GET_METHOD(*call)){
    262292                case IPC_M_PHONE_HUNGUP:
    263293                        return EOK;
    264294                case NET_NET_GET_DEVICE_CONF:
    265                         ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
    266                         net_get_device_conf_req( 0, IPC_GET_DEVICE( call ), & strings, IPC_GET_COUNT( call ), NULL );
     295                        ERROR_PROPAGATE(measured_strings_receive(&strings, &data, IPC_GET_COUNT(call)));
     296                        net_get_device_conf_req(0, IPC_GET_DEVICE(call), &strings, IPC_GET_COUNT(call), NULL);
    267297                        // strings should not contain received data anymore
    268                         free( data );
    269                         ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
    270                         free( strings );
     298                        free(data);
     299                        ERROR_CODE = measured_strings_reply(strings, IPC_GET_COUNT(call));
     300                        free(strings);
    271301                        return ERROR_CODE;
    272302                case NET_NET_GET_CONF:
    273                         ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
    274                         net_get_conf_req( 0, & strings, IPC_GET_COUNT( call ), NULL );
     303                        ERROR_PROPAGATE(measured_strings_receive(&strings, &data, IPC_GET_COUNT(call)));
     304                        net_get_conf_req(0, &strings, IPC_GET_COUNT(call), NULL);
    275305                        // strings should not contain received data anymore
    276                         free( data );
    277                         ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
    278                         free( strings );
     306                        free(data);
     307                        ERROR_CODE = measured_strings_reply(strings, IPC_GET_COUNT(call));
     308                        free(strings);
    279309                        return ERROR_CODE;
    280310                case NET_NET_STARTUP:
     
    284314}
    285315
    286 int read_configuration_file( const char * directory, const char * filename, measured_strings_ref configuration ){
    287         ERROR_DECLARE;
    288 
    289         size_t  index = 0;
    290         char    line[ BUFFER_SIZE ];
    291         FILE *  cfg;
    292         int             read;
    293         int             line_number = 0;
     316int parse_line(measured_strings_ref configuration, char * line){
     317        ERROR_DECLARE;
     318
     319        measured_string_ref setting;
     320        char * name;
     321        char * value;
     322
     323        // from the beginning
     324        name = line;
     325
     326        // skip comments and blank lines
     327        if((*name == '#') || (*name == '\0')){
     328                return EOK;
     329        }
     330        // skip spaces
     331        while(isspace(*name)){
     332                ++ name;
     333        }
     334
     335        // remember the name start
     336        value = name;
     337        // skip the name
     338        while(isalnum(*value) || (*value == '_')){
     339                // make uppercase
     340//              *value = toupper(*value);
     341                ++ value;
     342        }
     343
     344        if(*value == '='){
     345                // terminate the name
     346                *value = '\0';
     347        }else{
     348                // terminate the name
     349                *value = '\0';
     350                // skip until '='
     351                ++ value;
     352                while((*value) && (*value != '=')){
     353                        ++ value;
     354                }
     355                // not found?
     356                if(*value != '='){
     357                        return EINVAL;
     358                }
     359        }
     360
     361        ++ value;
     362        // skip spaces
     363        while(isspace(*value)){
     364                ++ value;
     365        }
     366        // create a bulk measured string till the end
     367        setting = measured_string_create_bulk(value, 0);
     368        if(! setting){
     369                return ENOMEM;
     370        }
     371
     372        // add the configuration setting
     373        if(ERROR_OCCURRED(measured_strings_add(configuration, name, 0, setting))){
     374                free(setting);
     375                return ERROR_CODE;
     376        }
     377        return EOK;
     378}
     379
     380int read_configuration(void){
     381        // read the general configuration file
     382        return read_configuration_file(CONF_DIR, CONF_GENERAL_FILE, &net_globals.configuration);
     383}
     384
     385int read_configuration_file(const char * directory, const char * filename, measured_strings_ref configuration){
     386        ERROR_DECLARE;
     387
     388        size_t index = 0;
     389        char line[BUFFER_SIZE];
     390        FILE * cfg;
     391        int read;
     392        int line_number = 0;
    294393
    295394        // construct the full filename
    296         printf( "Reading file %s/%s\n", directory, filename );
    297         if( snprintf( line, BUFFER_SIZE, "%s/%s", directory, filename ) > BUFFER_SIZE ){
     395        printf("Reading file %s/%s\n", directory, filename);
     396        if(snprintf(line, BUFFER_SIZE, "%s/%s", directory, filename) > BUFFER_SIZE){
    298397                return EOVERFLOW;
    299398        }
    300399        // open the file
    301         cfg = fopen( line, "r" );
    302         if( ! cfg ){
     400        cfg = fopen(line, "r");
     401        if(! cfg){
    303402                return ENOENT;
    304403        }
     
    306405        // read the configuration line by line
    307406        // until an error or the end of file
    308         while(( ! ferror( cfg )) && ( ! feof( cfg ))){
    309                 read = fgetc( cfg );
    310                 if(( read > 0 ) && ( read != '\n' ) && ( read != '\r' )){
    311                         if( index >= BUFFER_SIZE ){
    312                                 line[ BUFFER_SIZE - 1 ] = '\0';
    313                                 printf( "line %d too long: %s\n", line_number, line );
     407        while((! ferror(cfg)) && (! feof(cfg))){
     408                read = fgetc(cfg);
     409                if((read > 0) && (read != '\n') && (read != '\r')){
     410                        if(index >= BUFFER_SIZE){
     411                                line[BUFFER_SIZE - 1] = '\0';
     412                                printf("line %d too long: %s\n", line_number, line);
    314413                                // no space left in the line buffer
    315414                                return EOVERFLOW;
    316415                        }else{
    317416                                // append the character
    318                                 line[ index ] = (char) read;
     417                                line[index] = (char) read;
    319418                                ++ index;
    320419                        }
    321420                }else{
    322421                        // on error or new line
    323                         line[ index ] = '\0';
     422                        line[index] = '\0';
    324423                        ++ line_number;
    325                         if( ERROR_OCCURRED( parse_line( configuration, line ))){
    326                                 printf( "error on line %d: %s\n", line_number, line );
    327                                 //fclose( cfg );
     424                        if(ERROR_OCCURRED(parse_line(configuration, line))){
     425                                printf("error on line %d: %s\n", line_number, line);
     426                                //fclose(cfg);
    328427                                //return ERROR_CODE;
    329428                        }
     
    331430                }
    332431        }
    333         fclose( cfg );
    334         return EOK;
    335 }
    336 
    337 int parse_line( measured_strings_ref configuration, char * line ){
    338         ERROR_DECLARE;
    339 
    340         measured_string_ref     setting;
    341         char *                          name;
    342         char *                          value;
    343 
    344         // from the beginning
    345         name = line;
    346 
    347         // skip comments and blank lines
    348         if(( * name == '#' ) || ( * name == '\0' )){
    349                 return EOK;
    350         }
    351         // skip spaces
    352         while( isspace( * name )) ++ name;
    353 
    354         // remember the name start
    355         value = name;
    356         // skip the name
    357         while( isalnum( * value ) || ( * value == '_' )){
    358                 // make uppercase
    359 //              * value = toupper( * value );
    360                 ++ value;
    361         }
    362 
    363         if( * value == '=' ){
    364                 // terminate the name
    365                 * value = '\0';
    366         }else{
    367                 // terminate the name
    368                 * value = '\0';
    369                 // skip until '='
    370                 ++ value;
    371                 while(( * value ) && ( * value != '=' )) ++ value;
    372                 // not found?
    373                 if( * value != '=' ) return EINVAL;
    374         }
    375 
    376         ++ value;
    377         // skip spaces
    378         while( isspace( * value )) ++ value;
    379         // create a bulk measured string till the end
    380         setting = measured_string_create_bulk( value, 0 );
    381         if( ! setting ) return ENOMEM;
    382 
    383         // add the configuration setting
    384         if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
    385                 free( setting );
    386                 return ERROR_CODE;
    387         }
    388         return EOK;
    389 }
    390 
    391 int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
    392         ERROR_DECLARE;
    393 
    394         measured_string_ref     setting;
    395 
    396         setting = measured_string_create_bulk( value, 0 );
    397         if( ! setting ) return ENOMEM;
    398         // add the configuration setting
    399         if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
    400                 free( setting );
    401                 return ERROR_CODE;
    402         }
    403         return EOK;
    404 }
    405 
    406 device_id_t generate_new_device_id( void ){
    407         return device_assign_devno();
    408 }
    409 
    410 int read_configuration( void ){
    411         // read the general configuration file
    412         return read_configuration_file( CONF_DIR, CONF_GENERAL_FILE, & net_globals.configuration );
    413 }
    414 
    415 int read_netif_configuration( const char * name, netif_ref netif ){
     432        fclose(cfg);
     433        return EOK;
     434}
     435
     436int read_netif_configuration(const char * name, netif_ref netif){
    416437        // read the netif configuration file
    417         return read_configuration_file( CONF_DIR, name, & netif->configuration );
    418 }
    419 
    420 int start_device( netif_ref netif ){
    421         ERROR_DECLARE;
    422 
    423         measured_string_ref     setting;
    424         services_t                      internet_service;
    425         int                                     irq;
    426         int                                     io;
    427         int                                     mtu;
     438        return read_configuration_file(CONF_DIR, name, &netif->configuration);
     439}
     440
     441int start_device(netif_ref netif){
     442        ERROR_DECLARE;
     443
     444        measured_string_ref setting;
     445        services_t internet_service;
     446        int irq;
     447        int io;
     448        int mtu;
    428449
    429450        // mandatory netif
    430         setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
    431         netif->driver = get_running_module( & net_globals.modules, setting->value );
    432         if( ! netif->driver ){
    433                 printf( "Failed to start the network interface driver %s\n", setting->value );
     451        setting = measured_strings_find(&netif->configuration, CONF_NETIF, 0);
     452        netif->driver = get_running_module(&net_globals.modules, setting->value);
     453        if(! netif->driver){
     454                printf("Failed to start the network interface driver %s\n", setting->value);
    434455                return EINVAL;
    435456        }
     457
    436458        // optional network interface layer
    437         setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
    438         if( setting ){
    439                 netif->nil = get_running_module( & net_globals.modules, setting->value );
    440                 if( ! netif->nil ){
    441                         printf( "Failed to start the network interface layer %s\n", setting->value );
     459        setting = measured_strings_find(&netif->configuration, CONF_NIL, 0);
     460        if(setting){
     461                netif->nil = get_running_module(&net_globals.modules, setting->value);
     462                if(! netif->nil){
     463                        printf("Failed to start the network interface layer %s\n", setting->value);
    442464                        return EINVAL;
    443465                }
     
    445467                netif->nil = NULL;
    446468        }
     469
    447470        // mandatory internet layer
    448         setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
    449         netif->il = get_running_module( & net_globals.modules, setting->value );
    450         if( ! netif->il ){
    451                 printf( "Failed to start the internet layer %s\n", setting->value );
     471        setting = measured_strings_find(&netif->configuration, CONF_IL, 0);
     472        netif->il = get_running_module(&net_globals.modules, setting->value);
     473        if(! netif->il){
     474                printf("Failed to start the internet layer %s\n", setting->value);
    452475                return EINVAL;
    453476        }
    454         // end of the static loopback initialization
    455         // startup the loopback interface
    456         setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
    457         irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
    458         setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
    459         io = setting ? strtol( setting->value, NULL, 16 ) : 0;
    460         ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io ));
    461         if( netif->nil ){
    462                 setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
    463                 if( ! setting ){
    464                         setting = measured_strings_find( & net_globals.configuration, CONF_MTU, 0 );
    465                 }
    466                 mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
    467                 ERROR_PROPAGATE( nil_device_req( netif->nil->phone, netif->id, mtu, netif->driver->service ));
     477
     478        // hardware configuration
     479        setting = measured_strings_find(&netif->configuration, CONF_IRQ, 0);
     480        irq = setting ? strtol(setting->value, NULL, 10) : 0;
     481        setting = measured_strings_find(&netif->configuration, CONF_IO, 0);
     482        io = setting ? strtol(setting->value, NULL, 16) : 0;
     483        ERROR_PROPAGATE(netif_probe_req(netif->driver->phone, netif->id, irq, io));
     484
     485        // network interface layer startup
     486        if(netif->nil){
     487                setting = measured_strings_find(&netif->configuration, CONF_MTU, 0);
     488                if(! setting){
     489                        setting = measured_strings_find(&net_globals.configuration, CONF_MTU, 0);
     490                }
     491                mtu = setting ? strtol(setting->value, NULL, 10) : 0;
     492                ERROR_PROPAGATE(nil_device_req(netif->nil->phone, netif->id, mtu, netif->driver->service));
    468493                internet_service = netif->nil->service;
    469494        }else{
    470495                internet_service = netif->driver->service;
    471496        }
    472         switch( netif->il->service ){
     497
     498        // inter-network layer startup
     499        switch(netif->il->service){
    473500                case SERVICE_IP:
    474                         ERROR_PROPAGATE( ip_device_req( netif->il->phone, netif->id, internet_service ));
     501                        ERROR_PROPAGATE(ip_device_req(netif->il->phone, netif->id, internet_service));
    475502                        break;
    476503                default:
    477504                        return ENOENT;
    478505        }
    479         ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id ));
    480         return EOK;
    481 }
    482 
    483 int startup( void ){
     506        ERROR_PROPAGATE(netif_start_req(netif->driver->phone, netif->id));
     507        return EOK;
     508}
     509
     510int startup(void){
    484511        ERROR_DECLARE;
    485512
    486513#ifdef CONFIG_NETIF_DP8390
    487         const char *            conf_files[] = { "lo", "ne2k" };
     514        const char * conf_files[] = {"lo", "ne2k"};
    488515#else
    489         const char *            conf_files[] = { "lo" };
     516        const char * conf_files[] = {"lo"};
    490517#endif
    491518
    492         int                     count = sizeof( conf_files ) / sizeof( char * );
    493         int                     index;
    494         netif_ref       netif;
    495         int                     i;
    496         measured_string_ref     setting;
    497 
    498         for( i = 0; i < count; ++ i ){
    499                 netif = ( netif_ref ) malloc( sizeof( netif_t ));
    500                 if( ! netif ) return ENOMEM;
     519        int count = sizeof(conf_files) / sizeof(char *);
     520        int index;
     521        netif_ref netif;
     522        int i;
     523        measured_string_ref setting;
     524
     525        for(i = 0; i < count; ++ i){
     526                netif = (netif_ref) malloc(sizeof(netif_t));
     527                if(! netif){
     528                        return ENOMEM;
     529                }
    501530
    502531                netif->id = generate_new_device_id();
    503                 if( ! netif->id ) return EXDEV;
    504                 ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
     532                if(! netif->id){
     533                        return EXDEV;
     534                }
     535                ERROR_PROPAGATE(measured_strings_initialize(&netif->configuration));
     536
    505537                // read configuration files
    506                 if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){
    507                         measured_strings_destroy( & netif->configuration );
    508                         free( netif );
     538                if(ERROR_OCCURRED(read_netif_configuration(conf_files[i], netif))){
     539                        measured_strings_destroy(&netif->configuration);
     540                        free(netif);
    509541                        return ERROR_CODE;
    510542                }
     543
    511544                // mandatory name
    512                 setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
    513                 if( ! setting ){
    514                         printf( "The name is missing\n" );
    515                         measured_strings_destroy( & netif->configuration );
    516                         free( netif );
     545                setting = measured_strings_find(&netif->configuration, CONF_NAME, 0);
     546                if(! setting){
     547                        printf("The name is missing\n");
     548                        measured_strings_destroy(&netif->configuration);
     549                        free(netif);
    517550                        return EINVAL;
    518551                }
    519552                netif->name = setting->value;
     553
    520554                // add to the netifs map
    521                 index = netifs_add( & net_globals.netifs, netif->id, netif );
    522                 if( index < 0 ){
    523                         measured_strings_destroy( & netif->configuration );
    524                         free( netif );
     555                index = netifs_add(&net_globals.netifs, netif->id, netif);
     556                if(index < 0){
     557                        measured_strings_destroy(&netif->configuration);
     558                        free(netif);
    525559                        return index;
    526560                }
     561
    527562                // add to the netif names map
    528                 if( ERROR_OCCURRED( char_map_add( & net_globals.netif_names, netif->name, 0, index ))
     563                if(ERROR_OCCURRED(char_map_add(&net_globals.netif_names, netif->name, 0, index))
    529564                // start network interfaces and needed modules
    530                 || ERROR_OCCURRED( start_device( netif ))){
    531                         measured_strings_destroy( & netif->configuration );
    532                         netifs_exclude_index( & net_globals.netifs, index );
     565                        || ERROR_OCCURRED(start_device(netif))){
     566                        measured_strings_destroy(&netif->configuration);
     567                        netifs_exclude_index(&net_globals.netifs, index);
    533568                        return ERROR_CODE;
    534569                }
     570
    535571                // increment modules' usage
    536572                ++ netif->driver->usage;
    537                 if( netif->nil ) ++ netif->nil->usage;
     573                if(netif->nil){
     574                        ++ netif->nil->usage;
     575                }
    538576                ++ netif->il->usage;
    539                 printf( "New network interface started:\n\tname\t= %s\n\tid\t= %d\n\tdriver\t= %s\n\tnil\t= %s\n\til\t= %s\n", netif->name, netif->id, netif->driver->name, netif->nil ? netif->nil->name : NULL, netif->il->name );
     577                printf("New network interface started:\n\tname\t= %s\n\tid\t= %d\n\tdriver\t= %s\n\tnil\t= %s\n\til\t= %s\n", netif->name, netif->id, netif->driver->name, netif->nil ? netif->nil->name : NULL, netif->il->name);
    540578        }
    541579        return EOK;
Note: See TracChangeset for help on using the changeset viewer.