Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
3466 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3466 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
3912 mejdrech 30
 *  @{
3466 mejdrech 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"
3901 mejdrech 55
#include "../structures/packet/packet.h"
56
#include "../structures/packet/packet_server.h"
3886 mejdrech 57
 
58
#include "../il/ip/ip_messages.h"
3846 mejdrech 59
#include "../netif/device.h"
3666 mejdrech 60
 
3846 mejdrech 61
#if IP_BUNDLE
3466 mejdrech 62
 
3846 mejdrech 63
    #include "../ip/ip_module.h"
3466 mejdrech 64
 
65
#endif
66
 
3846 mejdrech 67
#if TCP_BUNDLE
68
 
69
    #include "../tcp/tcp_module.h"
70
 
71
#endif
72
 
3901 mejdrech 73
#define NAME    "Networking"
74
 
3666 mejdrech 75
#define LO_NAME             "lo"
76
#define LO_FILENAME         "/sbin/lo"
77
#define DP8390_ISA_NAME         "dp8390_isa"
78
#define DP8390_ISA_FILENAME     "/sbin/dp8380_isa"
79
#define ETHERNET_NAME           "ethernet"
80
#define ETHERNET_FILENAME       "/sbin/ethernet"
81
#define IP_NAME             "ip"
82
#define IP_FILENAME         "/sbin/ip"
3466 mejdrech 83
 
3901 mejdrech 84
#define IPC_GET_DEVICE( call )  ( device_id_t ) IPC_GET_ARG1( * call )
85
#define IPC_GET_COUNT( call )   ( int ) IPC_GET_ARG2( * call )
86
 
3666 mejdrech 87
typedef struct module_struct    module_t;
88
typedef module_t *      module_ref;
3466 mejdrech 89
 
3666 mejdrech 90
typedef struct netif        netif_t;
91
typedef netif_t *       netif_ref;
3466 mejdrech 92
 
3666 mejdrech 93
typedef struct networking_globals   networking_globals_t;
3466 mejdrech 94
 
3666 mejdrech 95
DEVICE_MAP_DECLARE( netifs, netif_t )
3466 mejdrech 96
 
3666 mejdrech 97
GENERIC_CHAR_MAP_DECLARE( measured_strings, measured_string_t )
3466 mejdrech 98
 
3666 mejdrech 99
GENERIC_CHAR_MAP_DECLARE( modules, module_t )
3466 mejdrech 100
 
3666 mejdrech 101
struct module_struct{
102
    task_id_t   task_id;
103
    services_t  service;
104
    int     phone;
105
    int     usage;
106
    const char *    name;
107
    const char *    filename;
108
};
3466 mejdrech 109
 
3666 mejdrech 110
/** A present network interface device.
111
 */
112
struct netif{
113
    /** A system-unique network interface identifier.
114
     */
3846 mejdrech 115
    device_id_t     id;
3666 mejdrech 116
    /** A serving network interface driver module index.
117
     */
3846 mejdrech 118
    module_ref      driver;
3666 mejdrech 119
    /** A serving link layer module index.
120
     */
3846 mejdrech 121
    module_ref      nil;
3666 mejdrech 122
    /** A serving internet layer module index.
123
     */
3846 mejdrech 124
    module_ref      il;
3666 mejdrech 125
    /** A system-unique network interface name.
126
     */
127
    char *          name;
128
    /** Configuration.
129
     */
130
    measured_strings_t  configuration;
131
};
3466 mejdrech 132
 
3666 mejdrech 133
/** A networking module global variables.
134
 */
135
struct networking_globals{
136
    /** Present network interfaces.
137
     */
138
    netifs_t        netifs;
139
    /** Network interface structure indices by names.
140
     */
141
    char_map_t      netif_names;
142
    /** Available modules.
143
     */
144
    modules_t       modules;
145
    /** Global configuration.
146
     */
147
    measured_strings_t  configuration;
148
};
3466 mejdrech 149
 
3901 mejdrech 150
void    networking_print_name( void );
3666 mejdrech 151
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 );
152
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
3901 mejdrech 153
int networking_start_module( async_client_conn_t client_connection );
3666 mejdrech 154
int     networking_initialize( void );
3901 mejdrech 155
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
156
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
3666 mejdrech 157
//int       parse_line( measured_strings_ref configuration, char * line );
158
int     add_configuration( measured_strings_ref configuration, const char * name, const char * value );
159
int     read_configuration( void );
160
task_id_t   spawn( const char * fname );
161
int     startup( void );
3846 mejdrech 162
device_id_t generate_new_device_id( void );
3666 mejdrech 163
 
164
static networking_globals_t networking_globals;
165
 
166
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
167
 
168
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
169
 
170
GENERIC_CHAR_MAP_IMPLEMENT( modules, module_t )
171
 
3901 mejdrech 172
void networking_print_name( void ){
173
    printf( NAME );
174
}
175
 
3666 mejdrech 176
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 177
    ERROR_DECLARE;
178
 
3666 mejdrech 179
    module_ref  tmp_module;
3466 mejdrech 180
 
3666 mejdrech 181
    tmp_module = ( module_ref ) malloc( sizeof( module_t ));
182
    if( ! tmp_module ) return ENOMEM;
183
    tmp_module->task_id = task_id;
184
    tmp_module->phone = 0;
185
    tmp_module->usage = 0;
186
    tmp_module->name = name;
187
    tmp_module->filename = filename;
188
    tmp_module->service = service;
3912 mejdrech 189
    if( ERROR_OCCURRED( modules_add( modules, tmp_module->name, 0, tmp_module ))){
3666 mejdrech 190
        free( tmp_module );
191
        return ERROR_CODE;
192
    }
193
    if( module ) * module = tmp_module;
3466 mejdrech 194
    return EOK;
195
}
196
 
3901 mejdrech 197
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3466 mejdrech 198
#ifdef NETWORKING_module
3901 mejdrech 199
        //TODO map to *_message
3666 mejdrech 200
        if( IS_NET_IL_MESSAGE( call ) || IS_NET_IP_MESSAGE( call )){
3901 mejdrech 201
            return ip_message( callid, call, answer, answer_count );
3466 mejdrech 202
/*      }else if( IS_NET_ARP_MESSAGE( call )){
3901 mejdrech 203
            return arp_message( callid, call, answer, answer_count );
3466 mejdrech 204
*//*        }else if( IS_NET_RARP_MESSAGE( call )){
3901 mejdrech 205
            return rarp_message( callid, call, answer, answer_count );
3466 mejdrech 206
*//*        }else if( IS_NET_ICMP_MESSAGE( call )){
3901 mejdrech 207
            return icmp_message( callid, call, answer, answer_count );
3466 mejdrech 208
*//*        }else if( IS_NET_UDP_MESSAGE( call )){
3901 mejdrech 209
            return udp_message( callid, call, answer, answer_count );
3466 mejdrech 210
*/      }else if( IS_NET_TCP_MESSAGE( call )){
3901 mejdrech 211
            return tcp_message( callid, call, answer, answer_count );
3466 mejdrech 212
/*      }else if( IS_NET_SOCKET_MESSAGE( call )){
3901 mejdrech 213
            return socket_message( callid, call, answer, answer_count );
3846 mejdrech 214
*//*        }else if( IS_NET_NIL_MESSAGE( call ) || IS_NET_ETHERNET_MESSAGE( call )){
3901 mejdrech 215
            return ethernet_message( callid, call, answer, answer_count );
3466 mejdrech 216
*/      }else{
217
#endif
3901 mejdrech 218
            if( IS_NET_PACKET_MESSAGE( call )){
219
                return packet_server_message( callid, call, answer, answer_count );
220
            }else{
221
                return net_message( callid, call, answer, answer_count );
222
            }
3466 mejdrech 223
#ifdef NETWORKING_module
224
        }
225
#endif
226
}
227
 
3901 mejdrech 228
int networking_start_module( async_client_conn_t client_connection ){
3846 mejdrech 229
    ERROR_DECLARE;
3466 mejdrech 230
 
3846 mejdrech 231
    ipcarg_t    phonehash;
232
 
233
    async_set_client_connection( client_connection );
3901 mejdrech 234
    ERROR_PROPAGATE( pm_init());
3914 mejdrech 235
    if( ERROR_OCCURRED( networking_initialize())
236
    || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
237
        pm_destroy();
238
        return ERROR_CODE;
239
    }
240
 
3846 mejdrech 241
    async_manager();
242
 
3914 mejdrech 243
    pm_destroy();
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
 
3901 mejdrech 303
//TODO as ip.c
304
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
3666 mejdrech 305
    ERROR_DECLARE;
306
 
307
    measured_string_ref strings;
308
    char *          data;
309
    int         index;
310
    measured_string_ref setting;
311
    measured_strings_ref    configuration;
312
    netif_ref       netif;
313
 
3901 mejdrech 314
    * answer_count = 0;
315
    switch( IPC_GET_METHOD( * call )){
3666 mejdrech 316
        case IPC_M_PHONE_HUNGUP:
317
            return EOK;
3846 mejdrech 318
        case NET_NET_DEVICE:
3666 mejdrech 319
            // TODO configure, register
3901 mejdrech 320
            printf( "\nNetworking: new netif %d", IPC_GET_DEVICE( call ));
3666 mejdrech 321
            return EOK;
3846 mejdrech 322
        case NET_NET_GET_DEVICE_CONF:
3901 mejdrech 323
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
324
            netif = netifs_find( & networking_globals.netifs, IPC_GET_DEVICE( call ));
3666 mejdrech 325
            if( netif ){
326
                configuration = & netif->configuration;
327
            }else{
328
                configuration = NULL;
329
            }
3901 mejdrech 330
            for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){
3846 mejdrech 331
                setting = measured_strings_find( configuration, strings[ index ].value, 0 );
3666 mejdrech 332
                if( ! setting ){
3846 mejdrech 333
                    setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 334
                }
335
                if( setting ){
336
                    strings[ index ].length = setting->length;
337
                    strings[ index ].value = setting->value;
338
                }else{
339
                    strings[ index ].length = 0;
340
                    strings[ index ].value = NULL;
341
                }
342
            }
343
            // strings should not contain received data anymore
344
            free( data );
3901 mejdrech 345
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 346
            free( strings );
347
            return ERROR_CODE;
3846 mejdrech 348
        case NET_NET_GET_CONF:
3666 mejdrech 349
            // arg1 = count
3901 mejdrech 350
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
351
            for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){
3846 mejdrech 352
                setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 );
3666 mejdrech 353
                if( setting ){
354
                    strings[ index ].length = setting->length;
355
                    strings[ index ].value = setting->value;
356
                }else{
357
                    strings[ index ].length = 0;
358
                    strings[ index ].value = NULL;
359
                }
360
            }
361
            // strings should not contain received data anymore
362
            free( data );
3901 mejdrech 363
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
3666 mejdrech 364
            free( strings );
365
            return ERROR_CODE;
3846 mejdrech 366
        case NET_NET_STARTUP:
3666 mejdrech 367
            return startup();
368
    }
369
    return ENOTSUP;
370
}
371
 
372
/*
373
int parse_line( measured_strings_ref configuration, char * line ){
374
    ERROR_DECLARE;
375
 
376
    measured_string_ref setting;
377
    char *          name;
378
    char *          value;
379
 
380
    // from the beginning
381
    name = line;
382
    // skip spaces
383
    while( isspace( * name )) ++ name;
384
    // remember the name start
385
    value = name;
386
    // skip the name
387
    while( isalnum( * value ) || ( * value == '_' )){
388
        // make uppercase
389
//      * value = toupper( * value );
390
        ++ value;
391
    }
392
    if( * value == '=' ){
393
        // terminate the name
394
        * value = '\0';
395
    }else{
396
        // terminate the name
397
        * value = '\0';
398
        // skip until '='
399
        ++ value;
400
        while(( * value ) && ( * value != '=' )) ++ value;
401
        // not found?
402
        if( * value != '=' ) return EINVAL;
403
    }
404
    ++ value;
405
    // skip spaces
406
    while( isspace( * value )) ++ value;
407
    // create a bulk measured string till the end
408
    setting = measured_string_create_bulk( value, -1 );
409
    if( ! setting ) return ENOMEM;
410
    // add the configuration setting
3912 mejdrech 411
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 412
        free( setting );
413
        return ERROR_CODE;
414
    }
415
    return EOK;
416
}
417
*/
418
 
419
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
420
    ERROR_DECLARE;
421
 
422
    measured_string_ref setting;
423
 
424
    setting = measured_string_create_bulk( value, 0 );
425
    if( ! setting ) return ENOMEM;
426
    // add the configuration setting
3912 mejdrech 427
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
3666 mejdrech 428
        free( setting );
429
        return ERROR_CODE;
430
    }
431
    return EOK;
432
}
433
 
3846 mejdrech 434
device_id_t generate_new_device_id( void ){
3666 mejdrech 435
    return netifs_count( & networking_globals.netifs ) + 1;
436
}
437
 
438
int read_configuration( void ){
439
    ERROR_DECLARE;
440
 
441
    netif_ref       netif;
442
    measured_string_ref setting;
443
    services_t      internet_service;
3685 mejdrech 444
    int         index;
3666 mejdrech 445
 
446
    // read general configuration
447
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "IPV", "4" ));
448
 
449
    // read network interfaces configuration
450
 
451
    // static loopback initialization
3685 mejdrech 452
//  printf( "\nloopback initialization" );
3666 mejdrech 453
    netif = ( netif_ref ) malloc( sizeof( netif_t ));
454
    if( ! netif ) return ENOMEM;
455
    netif->id = generate_new_device_id();
456
    ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
3912 mejdrech 457
    if( ERROR_OCCURRED( add_configuration( & netif->configuration, "NAME", LO_NAME ))
458
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NETIF", LO_NAME ))
459
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IL", IP_NAME ))
460
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "MTU", "1500" ))
461
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IP_CONFIG", "STATIC" ))
462
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "IP_ADDR", "127.0.0.1" ))
463
    || ERROR_OCCURRED( add_configuration( & netif->configuration, "NETMASK", "255.0.0.0" ))){
3666 mejdrech 464
        measured_strings_destroy( & netif->configuration );
465
        free( netif );
466
        return ERROR_CODE;
467
    }
468
    // mandatory name
3685 mejdrech 469
//  printf( "\n\tname" );
3846 mejdrech 470
    setting = measured_strings_find( & netif->configuration, "NAME", 0 );
3666 mejdrech 471
    if( ! setting ){
472
        measured_strings_destroy( & netif->configuration );
473
        free( netif );
474
        return EINVAL;
475
    }
476
    netif->name = setting->value;
3685 mejdrech 477
//  printf( " %s OK", netif->name );
3666 mejdrech 478
    // mandatory netif
3685 mejdrech 479
//  printf( "\n\tnetif" );
3846 mejdrech 480
    setting = measured_strings_find( & netif->configuration, "NETIF", 0 );
3666 mejdrech 481
    if( ! setting ){
3685 mejdrech 482
//      printf( " unknown" );
3666 mejdrech 483
        measured_strings_destroy( & netif->configuration );
484
        free( netif );
485
        return EINVAL;
486
    }
487
//  printf( " find %s in %d?", setting->value, modules_count( & networking_globals.modules ));
3846 mejdrech 488
    netif->driver = modules_find( & networking_globals.modules, setting->value, 0 );
489
    if( ! netif->driver ){
3685 mejdrech 490
//      printf( " not found" );
3666 mejdrech 491
        // TODO register the unknown one
492
        measured_strings_destroy( & netif->configuration );
493
        free( netif );
494
        return EINVAL;
495
    }
3685 mejdrech 496
//  printf( " found" );
3846 mejdrech 497
    if( ! netif->driver->task_id ){
498
        netif->driver->task_id = spawn( netif->driver->filename );
499
        if( ! netif->driver->task_id ){
3666 mejdrech 500
            measured_strings_destroy( & netif->configuration );
501
            free( netif );
502
            return EINVAL;
503
        }
504
    }
3685 mejdrech 505
//  printf( " OK" );
3666 mejdrech 506
    // optional link layer
3685 mejdrech 507
//  printf( "\n\tlink layer" );
3846 mejdrech 508
    setting = measured_strings_find( & netif->configuration, "NIL", 0 );
3666 mejdrech 509
    if( setting ){
3846 mejdrech 510
        netif->nil = modules_find( & networking_globals.modules, setting->value, 0 );
511
        if( ! netif->nil ){
3666 mejdrech 512
            // TODO register the unknown one
3846 mejdrech 513
            -- netif->driver->usage;
3666 mejdrech 514
            measured_strings_destroy( & netif->configuration );
515
            free( netif );
516
            return EINVAL;
517
        }
3846 mejdrech 518
        if( ! netif->nil->task_id ){
519
            netif->nil->task_id = spawn( netif->nil->filename );
520
            if( ! netif->nil->task_id ){
521
                -- netif->driver->usage;
3666 mejdrech 522
                measured_strings_destroy( & netif->configuration );
523
                free( netif );
524
                return EINVAL;
525
            }
526
        }
527
    }else{
3846 mejdrech 528
        netif->nil = NULL;
3666 mejdrech 529
    }
530
    // mandatory internet layer
3685 mejdrech 531
//  printf( "\n\tinternet layer" );
3846 mejdrech 532
    setting = measured_strings_find( & netif->configuration, "IL", 0 );
3666 mejdrech 533
    if( ! setting ){
3846 mejdrech 534
        -- netif->driver->usage;
535
        -- netif->nil->usage;
3666 mejdrech 536
        measured_strings_destroy( & netif->configuration );
537
        free( netif );
538
        return EINVAL;
539
    }
3685 mejdrech 540
//  printf( " set %s", setting->value );
3846 mejdrech 541
    netif->il = modules_find( & networking_globals.modules, setting->value, 0 );
542
    if( ! netif->il ){
3666 mejdrech 543
        // TODO register the unknown one
3846 mejdrech 544
        -- netif->driver->usage;
545
        -- netif->nil->usage;
3666 mejdrech 546
        measured_strings_destroy( & netif->configuration );
547
        free( netif );
548
        return EINVAL;
549
    }
3685 mejdrech 550
//  printf( " found" );
3846 mejdrech 551
    if( ! netif->il->task_id ){
552
        netif->il->task_id = spawn( netif->il->filename );
553
        if( ! netif->il->task_id ){
554
            -- netif->driver->usage;
555
            -- netif->nil->usage;
3666 mejdrech 556
            measured_strings_destroy( & netif->configuration );
557
            free( netif );
558
            return EINVAL;
559
        }
560
    }
3685 mejdrech 561
    index = netifs_add( & networking_globals.netifs, netif->id, netif );
562
    if( index < 0 ){
3666 mejdrech 563
        free( netif );
564
        return ERROR_CODE;
565
    }
3912 mejdrech 566
    if( ERROR_OCCURRED( char_map_add( & networking_globals.netif_names, netif->name, 0, index ))){
3685 mejdrech 567
        netifs_exclude_index( & networking_globals.netifs, index );
3666 mejdrech 568
        return ERROR_CODE;
569
    }
3685 mejdrech 570
//  printf( "\nloopback OK" );
571
    // end of the static loopback initialization
572
    // startup the loopback interface
3846 mejdrech 573
    if( ! netif->driver->phone ){
3685 mejdrech 574
//      printf( " connect?" );
3846 mejdrech 575
        netif->driver->phone = connect_to_service( netif->driver->service );
3685 mejdrech 576
    }
577
//  printf( " connected" );
3846 mejdrech 578
    // TODO io, irq
579
    ERROR_PROPAGATE( async_req_3_0( netif->driver->phone, NET_NETIF_PROBE, netif->id, 0, 0 ));
580
    ++ netif->driver->usage;
581
    if( netif->nil ){
582
        if( ! netif->nil->phone ){
583
            netif->nil->phone = connect_to_service( netif->nil->service );
3685 mejdrech 584
        }
3846 mejdrech 585
        ERROR_PROPAGATE( async_req_2_0( netif->nil->phone, NET_NIL_DEVICE, netif->id, netif->driver->service ));
586
        ++ netif->nil->usage;
587
        internet_service = netif->nil->service;
3685 mejdrech 588
//      printf( " OK" );
589
    }else{
3846 mejdrech 590
        internet_service = netif->driver->service;
3685 mejdrech 591
//      printf( " none" );
592
    }
3846 mejdrech 593
    if( ! netif->il->phone ){
3685 mejdrech 594
//      printf( " connect" );
3846 mejdrech 595
        netif->il->phone = connect_to_service( netif->il->service );
3685 mejdrech 596
    }
3846 mejdrech 597
    // TODO IL_BUNDLE
598
    ERROR_PROPAGATE( async_req_2_0( netif->il->phone, NET_IL_DEVICE, netif->id, internet_service ));
599
    ++ netif->il->usage;
600
    // TODO startup?
601
    ERROR_PROPAGATE( async_req_1_0( netif->driver->phone, NET_NETIF_START, netif->id ));
3685 mejdrech 602
//  printf( "\n LO OK" );
3666 mejdrech 603
    return EOK;
604
}
605
 
606
task_id_t spawn( const char * fname ){
607
    const char  * argv[ 2 ];
608
    task_id_t   res;
609
 
610
//  printf( "Spawning %s\n", fname );
611
    argv[ 0 ] = fname;
612
    argv[ 1 ] = NULL;
613
    res = task_spawn( fname, argv );
614
    if( res != 0 ){
615
        /* Success */
616
        usleep( 50000 );
617
    }
618
    return res;
619
}
620
 
621
int startup( void ){
622
    ERROR_DECLARE;
623
 
624
    // read configuration files
3912 mejdrech 625
    if( ERROR_OCCURRED( read_configuration())) return ERROR_CODE;
3666 mejdrech 626
 
627
    // start network interfaces and needed modules
628
//  start_device( "/sbin/lo", "/sbin/dummy_link_layer" );
629
    return EOK;
630
}
631
 
3466 mejdrech 632
/** @}
633
 */