Subversion Repositories HelenOS

Rev

Rev 3846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3466 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup net
30
 * @{
31
 */
32
 
33
/** @file
34
 */
35
 
36
#include <async.h>
3666 mejdrech 37
#include <ctype.h>
3466 mejdrech 38
#include <errno.h>
3666 mejdrech 39
#include <malloc.h>
3466 mejdrech 40
#include <stdio.h>
41
#include <task.h>
3666 mejdrech 42
#include <unistd.h>
3846 mejdrech 43
 
3466 mejdrech 44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
46
 
3666 mejdrech 47
#include "../err.h"
48
#include "../messages.h"
3466 mejdrech 49
#include "../modules.h"
3666 mejdrech 50
//#include "../self_test.h"
3466 mejdrech 51
 
3886 mejdrech 52
#include "../structures/char_map.h"
53
#include "../structures/generic_char_map.h"
54
#include "../structures/measured_strings.h"
55
 
56
#include "../il/ip/ip_messages.h"
3846 mejdrech 57
#include "../netif/device.h"
3666 mejdrech 58
 
3846 mejdrech 59
#if IP_BUNDLE
3466 mejdrech 60
 
3846 mejdrech 61
    #include "../ip/ip_module.h"
3466 mejdrech 62
 
63
#endif
64
 
3846 mejdrech 65
#if TCP_BUNDLE
66
 
67
    #include "../tcp/tcp_module.h"
68
 
69
#endif
70
 
3666 mejdrech 71
#define LO_NAME             "lo"
72
#define LO_FILENAME         "/sbin/lo"
73
#define DP8390_ISA_NAME         "dp8390_isa"
74
#define DP8390_ISA_FILENAME     "/sbin/dp8380_isa"
75
#define ETHERNET_NAME           "ethernet"
76
#define ETHERNET_FILENAME       "/sbin/ethernet"
77
#define IP_NAME             "ip"
78
#define IP_FILENAME         "/sbin/ip"
3466 mejdrech 79
 
3666 mejdrech 80
typedef struct module_struct    module_t;
81
typedef module_t *      module_ref;
3466 mejdrech 82
 
3666 mejdrech 83
typedef struct netif        netif_t;
84
typedef netif_t *       netif_ref;
3466 mejdrech 85
 
3666 mejdrech 86
typedef struct networking_globals   networking_globals_t;
3466 mejdrech 87
 
3666 mejdrech 88
DEVICE_MAP_DECLARE( netifs, netif_t )
3466 mejdrech 89
 
3666 mejdrech 90
GENERIC_CHAR_MAP_DECLARE( measured_strings, measured_string_t )
3466 mejdrech 91
 
3666 mejdrech 92
GENERIC_CHAR_MAP_DECLARE( modules, module_t )
3466 mejdrech 93
 
3666 mejdrech 94
struct module_struct{
95
    task_id_t   task_id;
96
    services_t  service;
97
    int     phone;
98
    int     usage;
99
    const char *    name;
100
    const char *    filename;
101
};
3466 mejdrech 102
 
3666 mejdrech 103
/** A present network interface device.
104
 */
105
struct netif{
106
    /** A system-unique network interface identifier.
107
     */
3846 mejdrech 108
    device_id_t     id;
3666 mejdrech 109
    /** A serving network interface driver module index.
110
     */
3846 mejdrech 111
    module_ref      driver;
3666 mejdrech 112
    /** A serving link layer module index.
113
     */
3846 mejdrech 114
    module_ref      nil;
3666 mejdrech 115
    /** A serving internet layer module index.
116
     */
3846 mejdrech 117
    module_ref      il;
3666 mejdrech 118
    /** A system-unique network interface name.
119
     */
120
    char *          name;
121
    /** Configuration.
122
     */
123
    measured_strings_t  configuration;
124
};
3466 mejdrech 125
 
3666 mejdrech 126
/** A networking module global variables.
127
 */
128
struct networking_globals{
129
    /** Present network interfaces.
130
     */
131
    netifs_t        netifs;
132
    /** Network interface structure indices by names.
133
     */
134
    char_map_t      netif_names;
135
    /** Available modules.
136
     */
137
    modules_t       modules;
138
    /** Global configuration.
139
     */
140
    measured_strings_t  configuration;
141
};
3466 mejdrech 142
 
3666 mejdrech 143
int     add_module( module_ref * module, modules_ref modules, const char const * name, const char const * filename, services_t service, task_id_t task_id );
144
static void client_connection( ipc_callid_t iid, ipc_call_t * icall );
145
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
146
int     main( int argc, char * argv[] );
147
int     networking_initialize( void );
148
int     networking_message( ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
149
//int       parse_line( measured_strings_ref configuration, char * line );
150
int     add_configuration( measured_strings_ref configuration, const char * name, const char * value );
151
int     read_configuration( void );
152
task_id_t   spawn( const char * fname );
153
int     startup( void );
3846 mejdrech 154
device_id_t generate_new_device_id( void );
3666 mejdrech 155
 
156
static networking_globals_t networking_globals;
157
 
158
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
159
 
160
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
161
 
162
GENERIC_CHAR_MAP_IMPLEMENT( modules, module_t )
163
 
164
int add_module( module_ref * module, modules_ref modules, const char * name, const char * filename, services_t service, task_id_t task_id ){
3466 mejdrech 165
    ERROR_DECLARE;
166
 
3666 mejdrech 167
    module_ref  tmp_module;
3466 mejdrech 168
 
3666 mejdrech 169
    tmp_module = ( module_ref ) malloc( sizeof( module_t ));
170
    if( ! tmp_module ) return ENOMEM;
171
    tmp_module->task_id = task_id;
172
    tmp_module->phone = 0;
173
    tmp_module->usage = 0;
174
    tmp_module->name = name;
175
    tmp_module->filename = filename;
176
    tmp_module->service = service;
3846 mejdrech 177
    if( ERROR_OCCURED( modules_add( modules, tmp_module->name, 0, tmp_module ))){
3666 mejdrech 178
        free( tmp_module );
179
        return ERROR_CODE;
180
    }
181
    if( module ) * module = tmp_module;
3466 mejdrech 182
    return EOK;
183
}
184
 
185
static void client_connection( ipc_callid_t iid, ipc_call_t * icall ){
3666 mejdrech 186
    ipc_callid_t    callid;
187
    ipc_call_t  call;
188
    ipcarg_t    arg1, arg2, arg3;
189
    int     res;
3466 mejdrech 190
 
3846 mejdrech 191
    /*
192
     * Accept the connection
193
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
194
     */
3685 mejdrech 195
//  printf( "\nNET-%d got %d on %x from %x", fibril_get_id(), IPC_GET_METHOD( * icall ), icall->in_phone_hash, iid );
3466 mejdrech 196
    ipc_answer_0( iid, EOK );
197
 
198
    while( true ){
199
        callid = async_get_call( & call );
200
        arg1 = 0;
201
        arg2 = 0;
202
        arg3 = 0;
3685 mejdrech 203
//      printf( "\nNET-%d got %d on %x from %x", fibril_get_id(), IPC_GET_METHOD( call ), call.in_phone_hash, callid );
3466 mejdrech 204
#ifdef NETWORKING_module
3666 mejdrech 205
        if( IS_NET_IL_MESSAGE( call ) || IS_NET_IP_MESSAGE( call )){
3846 mejdrech 206
            res = ip_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 207
/*      }else if( IS_NET_ARP_MESSAGE( call )){
3846 mejdrech 208
            res = arp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 209
*//*        }else if( IS_NET_RARP_MESSAGE( call )){
3846 mejdrech 210
            res = rarp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 211
*//*        }else if( IS_NET_ICMP_MESSAGE( call )){
3846 mejdrech 212
            res = icmp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 213
*//*        }else if( IS_NET_UDP_MESSAGE( call )){
3846 mejdrech 214
            res = udp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 215
*/      }else if( IS_NET_TCP_MESSAGE( call )){
3846 mejdrech 216
            res = tcp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 217
/*      }else if( IS_NET_SOCKET_MESSAGE( call )){
3846 mejdrech 218
            res = socket_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
219
*//*        }else if( IS_NET_NIL_MESSAGE( call ) || IS_NET_ETHERNET_MESSAGE( call )){
220
            res = ethernet_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 221
*/      }else{
222
#endif
3846 mejdrech 223
            res = networking_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
3466 mejdrech 224
#ifdef NETWORKING_module
225
        }
226
#endif
3666 mejdrech 227
        ipc_answer_2( callid, res, arg1, arg2 );
3466 mejdrech 228
    }
229
}
230
 
231
int main( int argc, char * argv[] ){
3846 mejdrech 232
    ERROR_DECLARE;
3466 mejdrech 233
 
3846 mejdrech 234
    ipcarg_t    phonehash;
235
 
3685 mejdrech 236
    printf("\nTask %d - HelenOS Networking subsystem", task_get_id());
3466 mejdrech 237
 
3846 mejdrech 238
    async_set_client_connection( client_connection );
239
    ERROR_PROPAGATE( networking_initialize());
240
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_NETWORKING, & phonehash ));
3466 mejdrech 241
 
3846 mejdrech 242
    async_manager();
243
 
3666 mejdrech 244
    return EOK;
245
}
246
 
247
int networking_initialize( void ){
248
    ERROR_DECLARE;
249
 
250
    task_id_t   task_id;
251
 
252
    netifs_initialize( & networking_globals.netifs );
253
    char_map_initialize( & networking_globals.netif_names );
254
    modules_initialize( & networking_globals.modules );
255
    measured_strings_initialize( & networking_globals.configuration );
256
 
257
    // run self tests
258
//  ERROR_PROPAGATE( self_test());
259
 
260
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0 ));
261
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, DP8390_ISA_NAME, DP8390_ISA_FILENAME, SERVICE_DP8390_ISA, 0 ));
262
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0 ));
263
 
264
#ifdef NETWORKING_modular
265
    task_id = spawn( "/sbin/ip" );
266
    if( ! task_id ) return EINVAL;
267
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_id ));
268
//  if( ! spawn( "/sbin/udp" )) return EINVAL;
269
    if( ! spawn( "/sbin/tcp" )) return EINVAL;
270
//  if( ! spawn( "/sbin/socket" )) return EINVAL;
271
//  not always necesssary
272
//  if( ! spawn( "/sbin/arp" )) return EINVAL;
273
//  if( ! spawn( "/sbin/rarp" )) return EINVAL;
274
//  if( ! spawn( "/sbin/icmp" )) return EINVAL;
275
 
276
#else
277
#ifdef NETWORKING_module
278
    ipcarg_t    phonehash;
279
 
280
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_IP, & phonehash ));
281
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_get_id()));
282
    ERROR_PROPAGATE( ip_initialize());
283
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ARP, & phonehash ));
284
//  ERROR_PROPAGATE( arp_initialize());
285
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_RARP, & phonehash ));
286
//  ERROR_PROPAGATE( rarp_initialize());
287
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ICMP, & phonehash ));
288
//  ERROR_PROPAGATE( icmp_initialize());
289
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_UDP, & phonehash ));
290
//  ERROR_PROPAGATE( udp_initialize());
291
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_TCP, & phonehash ));
292
    ERROR_PROPAGATE( tcp_initialize());
293
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_SOCKET, & phonehash ));
294
//  ERROR_PROPAGATE( socket_initialize());
295
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ETHERNET, & phonehash ));
296
//  ERROR_PROPAGATE( ethernet_initialize());
297
#endif
298
#endif
299
 
300
    return EOK;
301
}
302
 
303
int networking_message( ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
304
    ERROR_DECLARE;
305
 
306
    measured_string_ref strings;
307
    char *          data;
308
    int         index;
309
    measured_string_ref setting;
310
    measured_strings_ref    configuration;
311
    netif_ref       netif;
312
 
313
    switch( method ){
314
        case IPC_M_PHONE_HUNGUP:
315
            return EOK;
3846 mejdrech 316
        case NET_NET_DEVICE:
3666 mejdrech 317
            // TODO configure, register
318
            // arg1 = netif id
319
            printf( "\nNetworking: new netif %d", arg1 );
320
            return EOK;
3846 mejdrech 321
        case NET_NET_GET_DEVICE_CONF:
3666 mejdrech 322
            // arg1 = netif id
323
            // arg2 = count
324
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, arg2 ));
325
            netif = netifs_find( & networking_globals.netifs, arg1 );
326
            if( netif ){
327
                configuration = & netif->configuration;
328
            }else{
329
                configuration = NULL;
330
            }
331
            for( index = 0; index < arg2; ++ index ){
3846 mejdrech 332
                setting = measured_strings_find( configuration, strings[ index ].value, 0 );
3666 mejdrech 333
                if( ! setting ){
3846 mejdrech 334
                    setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 335
                }
336
                if( setting ){
337
                    strings[ index ].length = setting->length;
338
                    strings[ index ].value = setting->value;
339
                }else{
340
                    strings[ index ].length = 0;
341
                    strings[ index ].value = NULL;
342
                }
343
            }
344
            // strings should not contain received data anymore
345
            free( data );
346
            ERROR_CODE = measured_strings_reply( strings, arg2 );
347
            free( strings );
348
            return ERROR_CODE;
3846 mejdrech 349
        case NET_NET_GET_CONF:
3666 mejdrech 350
            // arg1 = count
351
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, arg1 ));
352
            for( index = 0; index < arg1; ++ index ){
3846 mejdrech 353
                setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 354
                if( setting ){
355
                    strings[ index ].length = setting->length;
356
                    strings[ index ].value = setting->value;
357
                }else{
358
                    strings[ index ].length = 0;
359
                    strings[ index ].value = NULL;
360
                }
361
            }
362
            // strings should not contain received data anymore
363
            free( data );
364
            ERROR_CODE = measured_strings_reply( strings, arg1 );
365
            free( strings );
366
            return ERROR_CODE;
3846 mejdrech 367
        case NET_NET_STARTUP:
3666 mejdrech 368
            return startup();
369
    }
370
    return ENOTSUP;
371
}
372
 
373
/*
374
int parse_line( measured_strings_ref configuration, char * line ){
375
    ERROR_DECLARE;
376
 
377
    measured_string_ref setting;
378
    char *          name;
379
    char *          value;
380
 
381
    // from the beginning
382
    name = line;
383
    // skip spaces
384
    while( isspace( * name )) ++ name;
385
    // remember the name start
386
    value = name;
387
    // skip the name
388
    while( isalnum( * value ) || ( * value == '_' )){
389
        // make uppercase
390
//      * value = toupper( * value );
391
        ++ value;
392
    }
393
    if( * value == '=' ){
394
        // terminate the name
395
        * value = '\0';
396
    }else{
397
        // terminate the name
398
        * value = '\0';
399
        // skip until '='
400
        ++ value;
401
        while(( * value ) && ( * value != '=' )) ++ value;
402
        // not found?
403
        if( * value != '=' ) return EINVAL;
404
    }
405
    ++ value;
406
    // skip spaces
407
    while( isspace( * value )) ++ value;
408
    // create a bulk measured string till the end
409
    setting = measured_string_create_bulk( value, -1 );
410
    if( ! setting ) return ENOMEM;
411
    // add the configuration setting
3846 mejdrech 412
    if( ERROR_OCCURED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 413
        free( setting );
414
        return ERROR_CODE;
415
    }
416
    return EOK;
417
}
418
*/
419
 
420
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
421
    ERROR_DECLARE;
422
 
423
    measured_string_ref setting;
424
 
425
    setting = measured_string_create_bulk( value, 0 );
426
    if( ! setting ) return ENOMEM;
427
    // add the configuration setting
3846 mejdrech 428
    if( ERROR_OCCURED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 429
        free( setting );
430
        return ERROR_CODE;
431
    }
432
    return EOK;
433
}
434
 
3846 mejdrech 435
device_id_t generate_new_device_id( void ){
3666 mejdrech 436
    return netifs_count( & networking_globals.netifs ) + 1;
437
}
438
 
439
int read_configuration( void ){
440
    ERROR_DECLARE;
441
 
442
    netif_ref       netif;
443
    measured_string_ref setting;
444
    services_t      internet_service;
3685 mejdrech 445
    int         index;
3666 mejdrech 446
 
447
    // read general configuration
448
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "IPV", "4" ));
449
 
450
    // read network interfaces configuration
451
 
452
    // static loopback initialization
3685 mejdrech 453
//  printf( "\nloopback initialization" );
3666 mejdrech 454
    netif = ( netif_ref ) malloc( sizeof( netif_t ));
455
    if( ! netif ) return ENOMEM;
456
    netif->id = generate_new_device_id();
457
    ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
458
    if( ERROR_OCCURED( add_configuration( & netif->configuration, "NAME", LO_NAME ))
3685 mejdrech 459
    || ERROR_OCCURED( add_configuration( & netif->configuration, "NETIF", LO_NAME ))
460
    || ERROR_OCCURED( add_configuration( & netif->configuration, "IL", IP_NAME ))
461
    || ERROR_OCCURED( add_configuration( & netif->configuration, "MTU", "1500" ))
462
    || ERROR_OCCURED( add_configuration( & netif->configuration, "IP_CONFIG", "STATIC" ))
463
    || ERROR_OCCURED( add_configuration( & netif->configuration, "IP_ADDR", "127.0.0.1" ))
464
    || ERROR_OCCURED( add_configuration( & netif->configuration, "NETMASK", "255.0.0.0" ))){
3666 mejdrech 465
        measured_strings_destroy( & netif->configuration );
466
        free( netif );
467
        return ERROR_CODE;
468
    }
469
    // mandatory name
3685 mejdrech 470
//  printf( "\n\tname" );
3846 mejdrech 471
    setting = measured_strings_find( & netif->configuration, "NAME", 0 );
3666 mejdrech 472
    if( ! setting ){
473
        measured_strings_destroy( & netif->configuration );
474
        free( netif );
475
        return EINVAL;
476
    }
477
    netif->name = setting->value;
3685 mejdrech 478
//  printf( " %s OK", netif->name );
3666 mejdrech 479
    // mandatory netif
3685 mejdrech 480
//  printf( "\n\tnetif" );
3846 mejdrech 481
    setting = measured_strings_find( & netif->configuration, "NETIF", 0 );
3666 mejdrech 482
    if( ! setting ){
3685 mejdrech 483
//      printf( " unknown" );
3666 mejdrech 484
        measured_strings_destroy( & netif->configuration );
485
        free( netif );
486
        return EINVAL;
487
    }
488
//  printf( " find %s in %d?", setting->value, modules_count( & networking_globals.modules ));
3846 mejdrech 489
    netif->driver = modules_find( & networking_globals.modules, setting->value, 0 );
490
    if( ! netif->driver ){
3685 mejdrech 491
//      printf( " not found" );
3666 mejdrech 492
        // TODO register the unknown one
493
        measured_strings_destroy( & netif->configuration );
494
        free( netif );
495
        return EINVAL;
496
    }
3685 mejdrech 497
//  printf( " found" );
3846 mejdrech 498
    if( ! netif->driver->task_id ){
499
        netif->driver->task_id = spawn( netif->driver->filename );
500
        if( ! netif->driver->task_id ){
3666 mejdrech 501
            measured_strings_destroy( & netif->configuration );
502
            free( netif );
503
            return EINVAL;
504
        }
505
    }
3685 mejdrech 506
//  printf( " OK" );
3666 mejdrech 507
    // optional link layer
3685 mejdrech 508
//  printf( "\n\tlink layer" );
3846 mejdrech 509
    setting = measured_strings_find( & netif->configuration, "NIL", 0 );
3666 mejdrech 510
    if( setting ){
3846 mejdrech 511
        netif->nil = modules_find( & networking_globals.modules, setting->value, 0 );
512
        if( ! netif->nil ){
3666 mejdrech 513
            // TODO register the unknown one
3846 mejdrech 514
            -- netif->driver->usage;
3666 mejdrech 515
            measured_strings_destroy( & netif->configuration );
516
            free( netif );
517
            return EINVAL;
518
        }
3846 mejdrech 519
        if( ! netif->nil->task_id ){
520
            netif->nil->task_id = spawn( netif->nil->filename );
521
            if( ! netif->nil->task_id ){
522
                -- netif->driver->usage;
3666 mejdrech 523
                measured_strings_destroy( & netif->configuration );
524
                free( netif );
525
                return EINVAL;
526
            }
527
        }
528
    }else{
3846 mejdrech 529
        netif->nil = NULL;
3666 mejdrech 530
    }
531
    // mandatory internet layer
3685 mejdrech 532
//  printf( "\n\tinternet layer" );
3846 mejdrech 533
    setting = measured_strings_find( & netif->configuration, "IL", 0 );
3666 mejdrech 534
    if( ! setting ){
3846 mejdrech 535
        -- netif->driver->usage;
536
        -- netif->nil->usage;
3666 mejdrech 537
        measured_strings_destroy( & netif->configuration );
538
        free( netif );
539
        return EINVAL;
540
    }
3685 mejdrech 541
//  printf( " set %s", setting->value );
3846 mejdrech 542
    netif->il = modules_find( & networking_globals.modules, setting->value, 0 );
543
    if( ! netif->il ){
3666 mejdrech 544
        // TODO register the unknown one
3846 mejdrech 545
        -- netif->driver->usage;
546
        -- netif->nil->usage;
3666 mejdrech 547
        measured_strings_destroy( & netif->configuration );
548
        free( netif );
549
        return EINVAL;
550
    }
3685 mejdrech 551
//  printf( " found" );
3846 mejdrech 552
    if( ! netif->il->task_id ){
553
        netif->il->task_id = spawn( netif->il->filename );
554
        if( ! netif->il->task_id ){
555
            -- netif->driver->usage;
556
            -- netif->nil->usage;
3666 mejdrech 557
            measured_strings_destroy( & netif->configuration );
558
            free( netif );
559
            return EINVAL;
560
        }
561
    }
3685 mejdrech 562
    index = netifs_add( & networking_globals.netifs, netif->id, netif );
563
    if( index < 0 ){
3666 mejdrech 564
        free( netif );
565
        return ERROR_CODE;
566
    }
3846 mejdrech 567
    if( ERROR_OCCURED( char_map_add( & networking_globals.netif_names, netif->name, 0, index ))){
3685 mejdrech 568
        netifs_exclude_index( & networking_globals.netifs, index );
3666 mejdrech 569
        return ERROR_CODE;
570
    }
3685 mejdrech 571
//  printf( "\nloopback OK" );
572
    // end of the static loopback initialization
573
    // startup the loopback interface
3846 mejdrech 574
    if( ! netif->driver->phone ){
3685 mejdrech 575
//      printf( " connect?" );
3846 mejdrech 576
        netif->driver->phone = connect_to_service( netif->driver->service );
3685 mejdrech 577
    }
578
//  printf( " connected" );
3846 mejdrech 579
    // TODO io, irq
580
    ERROR_PROPAGATE( async_req_3_0( netif->driver->phone, NET_NETIF_PROBE, netif->id, 0, 0 ));
581
    ++ netif->driver->usage;
582
    if( netif->nil ){
583
        if( ! netif->nil->phone ){
584
            netif->nil->phone = connect_to_service( netif->nil->service );
3685 mejdrech 585
        }
3846 mejdrech 586
        ERROR_PROPAGATE( async_req_2_0( netif->nil->phone, NET_NIL_DEVICE, netif->id, netif->driver->service ));
587
        ++ netif->nil->usage;
588
        internet_service = netif->nil->service;
3685 mejdrech 589
//      printf( " OK" );
590
    }else{
3846 mejdrech 591
        internet_service = netif->driver->service;
3685 mejdrech 592
//      printf( " none" );
593
    }
3846 mejdrech 594
    if( ! netif->il->phone ){
3685 mejdrech 595
//      printf( " connect" );
3846 mejdrech 596
        netif->il->phone = connect_to_service( netif->il->service );
3685 mejdrech 597
    }
3846 mejdrech 598
    // TODO IL_BUNDLE
599
    ERROR_PROPAGATE( async_req_2_0( netif->il->phone, NET_IL_DEVICE, netif->id, internet_service ));
600
    ++ netif->il->usage;
601
    // TODO startup?
602
    ERROR_PROPAGATE( async_req_1_0( netif->driver->phone, NET_NETIF_START, netif->id ));
3685 mejdrech 603
//  printf( "\n LO OK" );
3666 mejdrech 604
    return EOK;
605
}
606
 
607
task_id_t spawn( const char * fname ){
608
    const char  * argv[ 2 ];
609
    task_id_t   res;
610
 
611
//  printf( "Spawning %s\n", fname );
612
    argv[ 0 ] = fname;
613
    argv[ 1 ] = NULL;
614
    res = task_spawn( fname, argv );
615
    if( res != 0 ){
616
        /* Success */
617
        usleep( 50000 );
618
    }
619
    return res;
620
}
621
 
622
int startup( void ){
623
    ERROR_DECLARE;
624
 
625
    // read configuration files
626
    if( ERROR_OCCURED( read_configuration())) return ERROR_CODE;
627
 
628
    // start network interfaces and needed modules
629
//  start_device( "/sbin/lo", "/sbin/dummy_link_layer" );
630
    return EOK;
631
}
632
 
3466 mejdrech 633
/** @}
634
 */