Subversion Repositories HelenOS

Rev

Rev 3666 | 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 <thread.h>
43
#include <unistd.h>
3466 mejdrech 44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
46
//#include <sys/mman.h>
47
 
3666 mejdrech 48
#include "../char_map.h"
49
#include "../err.h"
50
#include "../generic_char_map.h"
51
#include "../measured_strings.h"
52
#include "../messages.h"
3466 mejdrech 53
#include "../modules.h"
3666 mejdrech 54
//#include "../self_test.h"
3466 mejdrech 55
 
3666 mejdrech 56
#include "../netif/netif_device_id_type.h"
57
 
3466 mejdrech 58
#ifdef NETWORKING_module
59
 
60
    #include "../ip/ip.h"
61
    #include "../tcp/tcp.h"
62
 
63
#endif
64
 
3666 mejdrech 65
#define LO_NAME             "lo"
66
#define LO_FILENAME         "/sbin/lo"
67
#define DP8390_ISA_NAME         "dp8390_isa"
68
#define DP8390_ISA_FILENAME     "/sbin/dp8380_isa"
69
#define ETHERNET_NAME           "ethernet"
70
#define ETHERNET_FILENAME       "/sbin/ethernet"
71
#define IP_NAME             "ip"
72
#define IP_FILENAME         "/sbin/ip"
3466 mejdrech 73
 
3666 mejdrech 74
typedef struct module_struct    module_t;
75
typedef module_t *      module_ref;
3466 mejdrech 76
 
3666 mejdrech 77
typedef struct netif        netif_t;
78
typedef netif_t *       netif_ref;
3466 mejdrech 79
 
3666 mejdrech 80
typedef struct networking_globals   networking_globals_t;
3466 mejdrech 81
 
3666 mejdrech 82
DEVICE_MAP_DECLARE( netifs, netif_t )
3466 mejdrech 83
 
3666 mejdrech 84
GENERIC_CHAR_MAP_DECLARE( measured_strings, measured_string_t )
3466 mejdrech 85
 
3666 mejdrech 86
GENERIC_CHAR_MAP_DECLARE( modules, module_t )
3466 mejdrech 87
 
3666 mejdrech 88
struct module_struct{
89
    task_id_t   task_id;
90
    services_t  service;
91
    int     phone;
92
    int     usage;
93
    const char *    name;
94
    const char *    filename;
95
};
3466 mejdrech 96
 
3666 mejdrech 97
/** A present network interface device.
98
 */
99
struct netif{
100
    /** A system-unique network interface identifier.
101
     */
102
    netif_device_id_t   id;
103
    /** A serving network interface driver module index.
104
     */
105
    module_ref      driver_module;
106
    /** A serving link layer module index.
107
     */
108
    module_ref      link_layer_module;
109
    /** A serving internet layer module index.
110
     */
111
    module_ref      internet_layer_module;
112
    /** A system-unique network interface name.
113
     */
114
    char *          name;
115
    /** Configuration.
116
     */
117
    measured_strings_t  configuration;
118
};
3466 mejdrech 119
 
3666 mejdrech 120
/** A networking module global variables.
121
 */
122
struct networking_globals{
123
    /** Present network interfaces.
124
     */
125
    netifs_t        netifs;
126
    /** Network interface structure indices by names.
127
     */
128
    char_map_t      netif_names;
129
    /** Available modules.
130
     */
131
    modules_t       modules;
132
    /** Global configuration.
133
     */
134
    measured_strings_t  configuration;
135
};
3466 mejdrech 136
 
3666 mejdrech 137
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 );
138
static void client_connection( ipc_callid_t iid, ipc_call_t * icall );
139
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
140
int     main( int argc, char * argv[] );
141
int     networking_call( ipc_callid_t callid );
142
int     networking_initialize( void );
143
int     networking_message( ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 );
144
//int       parse_line( measured_strings_ref configuration, char * line );
145
int     add_configuration( measured_strings_ref configuration, const char * name, const char * value );
146
int     read_configuration( void );
147
task_id_t   spawn( const char * fname );
148
int     startup( void );
149
netif_device_id_t   generate_new_device_id( void );
150
 
151
static networking_globals_t networking_globals;
152
 
153
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
154
 
155
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
156
 
157
GENERIC_CHAR_MAP_IMPLEMENT( modules, module_t )
158
 
159
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 160
    ERROR_DECLARE;
161
 
3666 mejdrech 162
    module_ref  tmp_module;
3466 mejdrech 163
 
3666 mejdrech 164
    tmp_module = ( module_ref ) malloc( sizeof( module_t ));
165
    if( ! tmp_module ) return ENOMEM;
166
    tmp_module->task_id = task_id;
167
    tmp_module->phone = 0;
168
    tmp_module->usage = 0;
169
    tmp_module->name = name;
170
    tmp_module->filename = filename;
171
    tmp_module->service = service;
172
    if( ERROR_OCCURED( modules_add( modules, tmp_module->name, tmp_module ))){
173
        free( tmp_module );
174
        return ERROR_CODE;
175
    }
176
    if( module ) * module = tmp_module;
3466 mejdrech 177
    return EOK;
178
}
179
 
180
static void client_connection( ipc_callid_t iid, ipc_call_t * icall ){
3666 mejdrech 181
    ipc_callid_t    callid;
182
    ipc_call_t  call;
183
    ipcarg_t    arg1, arg2, arg3;
184
    int     res;
3466 mejdrech 185
 
186
    /* Accept the connection */
3685 mejdrech 187
//  printf( "\nNET-%d got %d on %x from %x", fibril_get_id(), IPC_GET_METHOD( * icall ), icall->in_phone_hash, iid );
3466 mejdrech 188
    ipc_answer_0( iid, EOK );
189
 
190
    while( true ){
191
        callid = async_get_call( & call );
192
        arg1 = 0;
193
        arg2 = 0;
194
        arg3 = 0;
3685 mejdrech 195
//      printf( "\nNET-%d got %d on %x from %x", fibril_get_id(), IPC_GET_METHOD( call ), call.in_phone_hash, callid );
3466 mejdrech 196
#ifdef NETWORKING_module
3666 mejdrech 197
        if( IS_NET_IL_MESSAGE( call ) || IS_NET_IP_MESSAGE( call )){
3466 mejdrech 198
            res = ip_call( callid );
199
            if( res == EOK ){
200
                res = ip_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
201
            }
202
/*      }else if( IS_NET_ARP_MESSAGE( call )){
203
            res = arp_call( callid );
204
            if( res == EOK ){
205
                res = arp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
206
            }
207
*//*        }else if( IS_NET_RARP_MESSAGE( call )){
208
            res = rarp_call( callid );
209
            if( res == EOK ){
210
                res = rarp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
211
            }
212
*//*        }else if( IS_NET_ICMP_MESSAGE( call )){
213
            res = icmp_call( callid );
214
            if( res == EOK ){
215
                res = icmp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
216
            }
217
*//*        }else if( IS_NET_UDP_MESSAGE( call )){
218
            res = udp_call( callid );
219
            if( res == EOK ){
220
                res = udp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
221
            }
222
*/      }else if( IS_NET_TCP_MESSAGE( call )){
223
            res = tcp_call( callid );
224
            if( res == EOK ){
225
                res = tcp_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
226
            }
227
/*      }else if( IS_NET_SOCKET_MESSAGE( call )){
228
            res = socket_call( callid );
229
            if( res == EOK ){
230
                res = socket_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
231
            }
3666 mejdrech 232
*//*        }else if( IS_NET_LL_MESSAGE( call ) || IS_NET_ETHERNET_MESSAGE( call )){
3466 mejdrech 233
            res = ethernet_call( callid );
234
            if( res == EOK ){
235
                res = ethernet_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
236
            }
237
*/      }else{
238
#endif
239
            res = networking_call( callid );
240
            if( res == EOK ){
241
                res = networking_message( IPC_GET_METHOD( call ), IPC_GET_ARG1( call ), IPC_GET_ARG2( call ), IPC_GET_ARG3( call ), & arg1, & arg2, & arg3 );
242
            }
243
#ifdef NETWORKING_module
244
        }
245
#endif
3666 mejdrech 246
        ipc_answer_2( callid, res, arg1, arg2 );
3466 mejdrech 247
    }
248
}
249
 
250
int main( int argc, char * argv[] ){
251
 
3685 mejdrech 252
    printf("\nTask %d - HelenOS Networking subsystem", task_get_id());
3466 mejdrech 253
 
254
    return start_service( SERVICE_NETWORKING, NULL, NULL, client_connection, networking_initialize );
255
}
256
 
3666 mejdrech 257
int networking_call( ipc_callid_t callid ){
258
    return EOK;
259
}
260
 
261
int networking_initialize( void ){
262
    ERROR_DECLARE;
263
 
264
    task_id_t   task_id;
265
 
266
    netifs_initialize( & networking_globals.netifs );
267
    char_map_initialize( & networking_globals.netif_names );
268
    modules_initialize( & networking_globals.modules );
269
    measured_strings_initialize( & networking_globals.configuration );
270
 
271
    // run self tests
272
//  ERROR_PROPAGATE( self_test());
273
 
274
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0 ));
275
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, DP8390_ISA_NAME, DP8390_ISA_FILENAME, SERVICE_DP8390_ISA, 0 ));
276
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0 ));
277
 
278
#ifdef NETWORKING_modular
279
    task_id = spawn( "/sbin/ip" );
280
    if( ! task_id ) return EINVAL;
281
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_id ));
282
//  if( ! spawn( "/sbin/udp" )) return EINVAL;
283
    if( ! spawn( "/sbin/tcp" )) return EINVAL;
284
//  if( ! spawn( "/sbin/socket" )) return EINVAL;
285
//  not always necesssary
286
//  if( ! spawn( "/sbin/arp" )) return EINVAL;
287
//  if( ! spawn( "/sbin/rarp" )) return EINVAL;
288
//  if( ! spawn( "/sbin/icmp" )) return EINVAL;
289
 
290
#else
291
#ifdef NETWORKING_module
292
    ipcarg_t    phonehash;
293
 
294
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_IP, & phonehash ));
295
    ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_get_id()));
296
    ERROR_PROPAGATE( ip_initialize());
297
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ARP, & phonehash ));
298
//  ERROR_PROPAGATE( arp_initialize());
299
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_RARP, & phonehash ));
300
//  ERROR_PROPAGATE( rarp_initialize());
301
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ICMP, & phonehash ));
302
//  ERROR_PROPAGATE( icmp_initialize());
303
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_UDP, & phonehash ));
304
//  ERROR_PROPAGATE( udp_initialize());
305
    ERROR_PROPAGATE( REGISTER_ME( SERVICE_TCP, & phonehash ));
306
    ERROR_PROPAGATE( tcp_initialize());
307
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_SOCKET, & phonehash ));
308
//  ERROR_PROPAGATE( socket_initialize());
309
//  ERROR_PROPAGATE( REGISTER_ME( SERVICE_ETHERNET, & phonehash ));
310
//  ERROR_PROPAGATE( ethernet_initialize());
311
#endif
312
#endif
313
 
314
    return EOK;
315
}
316
 
317
int networking_message( ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
318
    ERROR_DECLARE;
319
 
320
    measured_string_ref strings;
321
    char *          data;
322
    int         index;
323
    measured_string_ref setting;
324
    measured_strings_ref    configuration;
325
    netif_ref       netif;
326
 
327
    switch( method ){
328
        case IPC_M_PHONE_HUNGUP:
329
            return EOK;
330
        case NET_NETWORKING_DEVICE:
331
            // TODO configure, register
332
            // arg1 = netif id
333
            printf( "\nNetworking: new netif %d", arg1 );
334
            return EOK;
335
        case NET_NETWORKING_GET_DEVICE_CONFIGURATION:
336
            // arg1 = netif id
337
            // arg2 = count
338
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, arg2 ));
339
            netif = netifs_find( & networking_globals.netifs, arg1 );
340
            if( netif ){
341
                configuration = & netif->configuration;
342
            }else{
343
                configuration = NULL;
344
            }
345
            for( index = 0; index < arg2; ++ index ){
346
                setting = measured_strings_find( configuration, strings[ index ].value );
347
                if( ! setting ){
348
                    setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value );
349
                }
350
                if( setting ){
351
                    strings[ index ].length = setting->length;
352
                    strings[ index ].value = setting->value;
353
                }else{
354
                    strings[ index ].length = 0;
355
                    strings[ index ].value = NULL;
356
                }
357
            }
358
            // strings should not contain received data anymore
359
            free( data );
360
            ERROR_CODE = measured_strings_reply( strings, arg2 );
361
            free( strings );
362
            return ERROR_CODE;
363
        case NET_NETWORKING_GET_CONFIGURATION:
364
            // arg1 = count
365
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, arg1 ));
366
            for( index = 0; index < arg1; ++ index ){
367
                setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value );
368
                if( setting ){
369
                    strings[ index ].length = setting->length;
370
                    strings[ index ].value = setting->value;
371
                }else{
372
                    strings[ index ].length = 0;
373
                    strings[ index ].value = NULL;
374
                }
375
            }
376
            // strings should not contain received data anymore
377
            free( data );
378
            ERROR_CODE = measured_strings_reply( strings, arg1 );
379
            free( strings );
380
            return ERROR_CODE;
381
        case NET_NETWORKING_STARTUP:
382
            return startup();
383
    }
384
    return ENOTSUP;
385
}
386
 
387
/*
388
int parse_line( measured_strings_ref configuration, char * line ){
389
    ERROR_DECLARE;
390
 
391
    measured_string_ref setting;
392
    char *          name;
393
    char *          value;
394
 
395
    // from the beginning
396
    name = line;
397
    // skip spaces
398
    while( isspace( * name )) ++ name;
399
    // remember the name start
400
    value = name;
401
    // skip the name
402
    while( isalnum( * value ) || ( * value == '_' )){
403
        // make uppercase
404
//      * value = toupper( * value );
405
        ++ value;
406
    }
407
    if( * value == '=' ){
408
        // terminate the name
409
        * value = '\0';
410
    }else{
411
        // terminate the name
412
        * value = '\0';
413
        // skip until '='
414
        ++ value;
415
        while(( * value ) && ( * value != '=' )) ++ value;
416
        // not found?
417
        if( * value != '=' ) return EINVAL;
418
    }
419
    ++ value;
420
    // skip spaces
421
    while( isspace( * value )) ++ value;
422
    // create a bulk measured string till the end
423
    setting = measured_string_create_bulk( value, -1 );
424
    if( ! setting ) return ENOMEM;
425
    // add the configuration setting
426
    if( ERROR_OCCURED( measured_strings_add( configuration, name, setting ))){
427
        free( setting );
428
        return ERROR_CODE;
429
    }
430
    return EOK;
431
}
432
*/
433
 
434
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
435
    ERROR_DECLARE;
436
 
437
    measured_string_ref setting;
438
 
439
    setting = measured_string_create_bulk( value, 0 );
440
    if( ! setting ) return ENOMEM;
441
    // add the configuration setting
442
    if( ERROR_OCCURED( measured_strings_add( configuration, name, setting ))){
443
        free( setting );
444
        return ERROR_CODE;
445
    }
446
    return EOK;
447
}
448
 
449
netif_device_id_t generate_new_device_id( void ){
450
    return netifs_count( & networking_globals.netifs ) + 1;
451
}
452
 
453
int read_configuration( void ){
454
    ERROR_DECLARE;
455
 
456
    netif_ref       netif;
457
    measured_string_ref setting;
458
    services_t      internet_service;
3685 mejdrech 459
    int         index;
3666 mejdrech 460
 
461
    // read general configuration
462
    ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "IPV", "4" ));
463
 
464
    // read network interfaces configuration
465
 
466
    // static loopback initialization
3685 mejdrech 467
//  printf( "\nloopback initialization" );
3666 mejdrech 468
    netif = ( netif_ref ) malloc( sizeof( netif_t ));
469
    if( ! netif ) return ENOMEM;
470
    netif->id = generate_new_device_id();
471
    ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
472
    if( ERROR_OCCURED( add_configuration( & netif->configuration, "NAME", LO_NAME ))
3685 mejdrech 473
    || ERROR_OCCURED( add_configuration( & netif->configuration, "NETIF", LO_NAME ))
474
    || ERROR_OCCURED( add_configuration( & netif->configuration, "IL", IP_NAME ))
475
    || ERROR_OCCURED( add_configuration( & netif->configuration, "MTU", "1500" ))
476
    || ERROR_OCCURED( add_configuration( & netif->configuration, "IP_CONFIG", "STATIC" ))
477
    || ERROR_OCCURED( add_configuration( & netif->configuration, "IP_ADDR", "127.0.0.1" ))
478
    || ERROR_OCCURED( add_configuration( & netif->configuration, "NETMASK", "255.0.0.0" ))){
3666 mejdrech 479
        measured_strings_destroy( & netif->configuration );
480
        free( netif );
481
        return ERROR_CODE;
482
    }
483
    // mandatory name
3685 mejdrech 484
//  printf( "\n\tname" );
3666 mejdrech 485
    setting = measured_strings_find( & netif->configuration, "NAME" );
486
    if( ! setting ){
487
        measured_strings_destroy( & netif->configuration );
488
        free( netif );
489
        return EINVAL;
490
    }
491
    netif->name = setting->value;
3685 mejdrech 492
//  printf( " %s OK", netif->name );
3666 mejdrech 493
    // mandatory netif
3685 mejdrech 494
//  printf( "\n\tnetif" );
3666 mejdrech 495
    setting = measured_strings_find( & netif->configuration, "NETIF" );
496
    if( ! setting ){
3685 mejdrech 497
//      printf( " unknown" );
3666 mejdrech 498
        measured_strings_destroy( & netif->configuration );
499
        free( netif );
500
        return EINVAL;
501
    }
502
//  printf( " find %s in %d?", setting->value, modules_count( & networking_globals.modules ));
503
    netif->driver_module = modules_find( & networking_globals.modules, setting->value );
504
    if( ! netif->driver_module ){
3685 mejdrech 505
//      printf( " not found" );
3666 mejdrech 506
        // TODO register the unknown one
507
        measured_strings_destroy( & netif->configuration );
508
        free( netif );
509
        return EINVAL;
510
    }
3685 mejdrech 511
//  printf( " found" );
3666 mejdrech 512
    if( ! netif->driver_module->task_id ){
513
        netif->driver_module->task_id = spawn( netif->driver_module->filename );
514
        if( ! netif->driver_module->task_id ){
515
            measured_strings_destroy( & netif->configuration );
516
            free( netif );
517
            return EINVAL;
518
        }
519
    }
3685 mejdrech 520
//  printf( " OK" );
3666 mejdrech 521
    // optional link layer
3685 mejdrech 522
//  printf( "\n\tlink layer" );
3666 mejdrech 523
    setting = measured_strings_find( & netif->configuration, "LL" );
524
    if( setting ){
525
        netif->link_layer_module = modules_find( & networking_globals.modules, setting->value );
526
        if( ! netif->link_layer_module ){
527
            // TODO register the unknown one
528
            -- netif->driver_module->usage;
529
            measured_strings_destroy( & netif->configuration );
530
            free( netif );
531
            return EINVAL;
532
        }
533
        if( ! netif->link_layer_module->task_id ){
534
            netif->link_layer_module->task_id = spawn( netif->link_layer_module->filename );
535
            if( ! netif->link_layer_module->task_id ){
536
                -- netif->driver_module->usage;
537
                measured_strings_destroy( & netif->configuration );
538
                free( netif );
539
                return EINVAL;
540
            }
541
        }
542
    }else{
3685 mejdrech 543
        netif->link_layer_module = NULL;
3666 mejdrech 544
    }
545
    // mandatory internet layer
3685 mejdrech 546
//  printf( "\n\tinternet layer" );
3666 mejdrech 547
    setting = measured_strings_find( & netif->configuration, "IL" );
548
    if( ! setting ){
549
        -- netif->driver_module->usage;
550
        -- netif->link_layer_module->usage;
551
        measured_strings_destroy( & netif->configuration );
552
        free( netif );
553
        return EINVAL;
554
    }
3685 mejdrech 555
//  printf( " set %s", setting->value );
3666 mejdrech 556
    netif->internet_layer_module = modules_find( & networking_globals.modules, setting->value );
557
    if( ! netif->internet_layer_module ){
558
        // TODO register the unknown one
559
        -- netif->driver_module->usage;
560
        -- netif->link_layer_module->usage;
561
        measured_strings_destroy( & netif->configuration );
562
        free( netif );
563
        return EINVAL;
564
    }
3685 mejdrech 565
//  printf( " found" );
3666 mejdrech 566
    if( ! netif->internet_layer_module->task_id ){
567
        netif->internet_layer_module->task_id = spawn( netif->internet_layer_module->filename );
568
        if( ! netif->internet_layer_module->task_id ){
569
            -- netif->driver_module->usage;
570
            -- netif->link_layer_module->usage;
571
            measured_strings_destroy( & netif->configuration );
572
            free( netif );
573
            return EINVAL;
574
        }
575
    }
3685 mejdrech 576
    index = netifs_add( & networking_globals.netifs, netif->id, netif );
577
    if( index < 0 ){
3666 mejdrech 578
        free( netif );
579
        return ERROR_CODE;
580
    }
3685 mejdrech 581
    if( ERROR_OCCURED( char_map_add( & networking_globals.netif_names, netif->name, index ))){
582
        netifs_exclude_index( & networking_globals.netifs, index );
3666 mejdrech 583
        return ERROR_CODE;
584
    }
3685 mejdrech 585
//  printf( "\nloopback OK" );
586
    // end of the static loopback initialization
587
    // startup the loopback interface
588
    if( ! netif->driver_module->phone ){
589
//      printf( " connect?" );
590
        netif->driver_module->phone = connect_to_service( netif->driver_module->service );
591
    }
592
//  printf( " connected" );
593
    ERROR_PROPAGATE( async_req_1_0( netif->driver_module->phone, NET_NETIF_PROBE, netif->id ));
594
    ++ netif->driver_module->usage;
595
    if( netif->link_layer_module ){
596
        if( ! netif->link_layer_module->phone ){
597
            netif->link_layer_module->phone = connect_to_service( netif->link_layer_module->service );
598
        }
599
        ERROR_PROPAGATE( async_req_2_0( netif->link_layer_module->phone, NET_LL_DEVICE, netif->id, netif->driver_module->service ));
600
        ++ netif->link_layer_module->usage;
601
        internet_service = netif->link_layer_module->service;
602
//      printf( " OK" );
603
    }else{
604
        internet_service = netif->driver_module->service;
605
//      printf( " none" );
606
    }
607
    if( ! netif->internet_layer_module->phone ){
608
//      printf( " connect" );
609
        netif->internet_layer_module->phone = connect_to_service( netif->internet_layer_module->service );
610
    }
611
    ERROR_PROPAGATE( async_req_2_0( netif->internet_layer_module->phone, NET_IL_DEVICE, netif->id, internet_service ));
612
    ++ netif->internet_layer_module->usage;
613
    ERROR_PROPAGATE( async_req_1_0( netif->driver_module->phone, NET_NETIF_START, netif->id ));
614
//  printf( "\n LO OK" );
3666 mejdrech 615
    return EOK;
616
}
617
 
618
task_id_t spawn( const char * fname ){
619
    const char  * argv[ 2 ];
620
    task_id_t   res;
621
 
622
//  printf( "Spawning %s\n", fname );
623
    argv[ 0 ] = fname;
624
    argv[ 1 ] = NULL;
625
    res = task_spawn( fname, argv );
626
    if( res != 0 ){
627
        /* Success */
628
        usleep( 50000 );
629
    }
630
    return res;
631
}
632
 
633
int startup( void ){
634
    ERROR_DECLARE;
635
 
636
    // read configuration files
637
    if( ERROR_OCCURED( read_configuration())) return ERROR_CODE;
638
 
639
    // start network interfaces and needed modules
640
//  start_device( "/sbin/lo", "/sbin/dummy_link_layer" );
641
    return EOK;
642
}
643
 
3466 mejdrech 644
/** @}
645
 */