Subversion Repositories HelenOS

Rev

Rev 4307 | Rev 4351 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4307 Rev 4350
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup net
29
/** @addtogroup net
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
-
 
34
 *  Networking subsystem central module implementation.
34
 */
35
 */
35
 
36
 
36
#include <async.h>
37
#include <async.h>
37
#include <ctype.h>
38
#include <ctype.h>
38
#include <ddi.h>
39
#include <ddi.h>
39
#include <errno.h>
40
#include <errno.h>
40
#include <malloc.h>
41
#include <malloc.h>
41
#include <stdio.h>
42
#include <stdio.h>
42
 
43
 
43
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
44
#include <ipc/services.h>
45
#include <ipc/services.h>
45
 
46
 
46
#include "../err.h"
47
#include "../err.h"
47
#include "../messages.h"
48
#include "../messages.h"
48
#include "../modules.h"
49
#include "../modules.h"
49
//#include "../self_test.h"
50
//#include "../self_test.h"
50
 
51
 
51
#include "../structures/char_map.h"
52
#include "../structures/char_map.h"
52
#include "../structures/generic_char_map.h"
53
#include "../structures/generic_char_map.h"
53
#include "../structures/measured_strings.h"
54
#include "../structures/measured_strings.h"
54
#include "../structures/module_map.h"
55
#include "../structures/module_map.h"
55
#include "../structures/packet/packet.h"
56
#include "../structures/packet/packet.h"
56
 
57
 
57
#include "../il/il_messages.h"
58
#include "../il/il_messages.h"
58
#include "../include/device.h"
59
#include "../include/device.h"
59
#include "../include/netif_interface.h"
60
#include "../include/netif_interface.h"
60
#include "../include/nil_interface.h"
61
#include "../include/nil_interface.h"
61
#include "../include/net_interface.h"
62
#include "../include/net_interface.h"
62
#include "../include/ip_interface.h"
63
#include "../include/ip_interface.h"
63
 
64
 
64
#include "net.h"
65
#include "net.h"
65
 
66
 
-
 
67
/** Networking module name.
-
 
68
 */
66
#define NAME    "Networking"
69
#define NAME    "Networking"
67
 
70
 
-
 
71
/** Prints the module name.
-
 
72
 *  @see NAME
-
 
73
 */
68
void    module_print_name( void );
74
void    module_print_name( void );
-
 
75
 
-
 
76
/** Starts the networking module.
-
 
77
 *  Initializes the client connection serving function, initializes the module, registers the module service and starts the async manager, processing IPC messages in an infinite loop.
-
 
78
 *  @param client_connection The client connection processing function. The module skeleton propagates its own one. Input parameter.
-
 
79
 *  @returns EOK on successful module termination.
69
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name );
80
 *  @returns Other error codes as defined for the net_initialize() function.
-
 
81
 *  @returns Other error codes as defined for the REGISTER_ME() macro function.
-
 
82
 */
70
int module_start( async_client_conn_t client_connection );
83
int module_start( async_client_conn_t client_connection );
-
 
84
 
71
//int       parse_line( measured_strings_ref configuration, char * line );
85
//int       parse_line( measured_strings_ref configuration, char * line );
-
 
86
 
-
 
87
/** Reads the networking subsystem global configuration.
-
 
88
 *  @returns EOK on success.
-
 
89
 *  @returns Other error codes as defined for the add_configuration() function.
-
 
90
 */
72
int     read_configuration( void );
91
int     read_configuration( void );
-
 
92
 
-
 
93
/** Starts the network interface according to its configuration.
-
 
94
 *  Registers the network interface with the subsystem modules.
-
 
95
 *  Starts the needed subsystem modules.
-
 
96
 *  @param netif The network interface specific data.
-
 
97
 *  @returns EOK on success.
-
 
98
 *  @returns EINVAL if there are some settings missing.
-
 
99
 *  @returns ENOENT if the internet protocol module is not known.
-
 
100
 *  @returns Other error codes as defined for the netif_probe_req() function.
-
 
101
 *  @returns Other error codes as defined for the nil_device_req() function.
-
 
102
 *  @returns Other error codes as defined for the needed internet layer registering function.
-
 
103
 */
73
int     start_device( netif_ref netif );
104
int     start_device( netif_ref netif );
-
 
105
 
-
 
106
/** Reads the configuration and starts all network interfaces.
-
 
107
 *  @returns EOK on success.
-
 
108
 *  @returns EXDEV if there is no available system-unique device identifier.
-
 
109
 *  @returns EINVAL if any of the network interface names are not configured.
-
 
110
 *  @returns ENOMEM if there is not enough memory left.
-
 
111
 *  @returns Other error codes as defined for the read_configuration() function.
-
 
112
 *  @returns Other error codes as defined for the read_netif_configuration() function.
-
 
113
 *  @returns Other error codes as defined for the start_device() function.
-
 
114
 */
74
int     startup( void );
115
int     startup( void );
-
 
116
 
-
 
117
/** Generates new system-unique device identifier.
-
 
118
 *  @returns The system-unique devic identifier.
-
 
119
 */
75
device_id_t generate_new_device_id( void );
120
device_id_t generate_new_device_id( void );
76
int net_get_conf( measured_strings_ref device_conf, measured_string_ref configuration, int count, char ** data );
-
 
77
 
121
 
-
 
122
/** Returns the configured values.
-
 
123
 *  The network interface configuration is searched first.
-
 
124
 *  @param netif_conf The network interface configuration setting. Input parameter.
-
 
125
 *  @param configuration The found configured values. Output parameter.
-
 
126
 *  @param count The desired settings count. Input parameter.
-
 
127
 *  @param data The found configuration settings data. Output parameter.
-
 
128
 *  @returns EOK.
-
 
129
 */
-
 
130
int net_get_conf( measured_strings_ref netif_conf, measured_string_ref configuration, int count, char ** data );
-
 
131
 
-
 
132
/** Networking module global data.
-
 
133
 */
78
net_globals_t   net_globals;
134
net_globals_t   net_globals;
79
 
135
 
80
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
136
DEVICE_MAP_IMPLEMENT( netifs, netif_t )
81
 
137
 
82
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
138
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t )
83
 
139
 
84
void module_print_name( void ){
140
void module_print_name( void ){
85
    printf( "%s", NAME );
141
    printf( "%s", NAME );
86
}
142
}
87
 
143
 
88
int module_start( async_client_conn_t client_connection ){
144
int module_start( async_client_conn_t client_connection ){
89
    ERROR_DECLARE;
145
    ERROR_DECLARE;
90
 
146
 
91
    ipcarg_t    phonehash;
147
    ipcarg_t    phonehash;
92
 
148
 
93
    async_set_client_connection( client_connection );
149
    async_set_client_connection( client_connection );
94
    ERROR_PROPAGATE( pm_init());
150
    ERROR_PROPAGATE( pm_init());
95
    if( ERROR_OCCURRED( net_initialize())
151
    if( ERROR_OCCURRED( net_initialize())
96
    || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
152
    || ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){
97
        pm_destroy();
153
        pm_destroy();
98
        return ERROR_CODE;
154
        return ERROR_CODE;
99
    }
155
    }
100
 
156
 
101
    async_manager();
157
    async_manager();
102
 
158
 
103
    pm_destroy();
159
    pm_destroy();
104
    return EOK;
160
    return EOK;
105
}
161
}
106
 
162
 
107
int net_get_device_conf_req( int net_phone, device_id_t device_id, measured_string_ref * configuration, int count, char ** data ){
163
int net_get_device_conf_req( int net_phone, device_id_t device_id, measured_string_ref * configuration, int count, char ** data ){
108
    netif_ref   netif;
164
    netif_ref   netif;
109
 
165
 
110
    if( !( configuration && ( count > 0 ))) return EINVAL;
166
    if( !( configuration && ( count > 0 ))) return EINVAL;
111
    netif = netifs_find( & net_globals.netifs, device_id );
167
    netif = netifs_find( & net_globals.netifs, device_id );
112
    if( netif ){
168
    if( netif ){
113
        return net_get_conf( & netif->configuration, * configuration, count, data );
169
        return net_get_conf( & netif->configuration, * configuration, count, data );
114
    }else{
170
    }else{
115
        return net_get_conf( NULL, * configuration, count, data );
171
        return net_get_conf( NULL, * configuration, count, data );
116
    }
172
    }
117
}
173
}
118
 
174
 
119
int net_get_conf_req( int net_phone, measured_string_ref * configuration, int count, char ** data ){
175
int net_get_conf_req( int net_phone, measured_string_ref * configuration, int count, char ** data ){
120
    if( !( configuration && ( count > 0 ))) return EINVAL;
176
    if( !( configuration && ( count > 0 ))) return EINVAL;
121
    return net_get_conf( NULL, * configuration, count, data );
177
    return net_get_conf( NULL, * configuration, count, data );
122
}
178
}
123
 
179
 
124
int net_get_conf( measured_strings_ref device_conf, measured_string_ref configuration, int count, char ** data ){
180
int net_get_conf( measured_strings_ref netif_conf, measured_string_ref configuration, int count, char ** data ){
125
    measured_string_ref setting;
181
    measured_string_ref setting;
126
    int                 index;
182
    int                 index;
127
 
183
 
128
    if( data ) * data = NULL;
184
    if( data ) * data = NULL;
129
    for( index = 0; index < count; ++ index ){
185
    for( index = 0; index < count; ++ index ){
130
        setting = measured_strings_find( device_conf, configuration[ index ].value, 0 );
186
        setting = measured_strings_find( netif_conf, configuration[ index ].value, 0 );
131
        if( ! setting ){
187
        if( ! setting ){
132
            setting = measured_strings_find( & net_globals.configuration, configuration[ index ].value, 0 );
188
            setting = measured_strings_find( & net_globals.configuration, configuration[ index ].value, 0 );
133
        }
189
        }
134
        if( setting ){
190
        if( setting ){
135
            configuration[ index ].length = setting->length;
191
            configuration[ index ].length = setting->length;
136
            configuration[ index ].value = setting->value;
192
            configuration[ index ].value = setting->value;
137
        }else{
193
        }else{
138
            configuration[ index ].length = 0;
194
            configuration[ index ].length = 0;
139
            configuration[ index ].value = NULL;
195
            configuration[ index ].value = NULL;
140
        }
196
        }
141
    }
197
    }
142
    return EOK;
198
    return EOK;
143
}
199
}
144
 
200
 
145
void net_free_settings( measured_string_ref settings, char * data ){
201
void net_free_settings( measured_string_ref settings, char * data ){
146
}
202
}
147
 
203
 
148
int net_connect_module( services_t service ){
204
int net_connect_module( services_t service ){
149
    return EOK;
205
    return EOK;
150
}
206
}
151
 
207
 
152
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
208
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
153
    ERROR_DECLARE;
209
    ERROR_DECLARE;
154
 
210
 
155
    measured_string_ref strings;
211
    measured_string_ref strings;
156
    char *              data;
212
    char *              data;
157
 
213
 
158
    * answer_count = 0;
214
    * answer_count = 0;
159
    switch( IPC_GET_METHOD( * call )){
215
    switch( IPC_GET_METHOD( * call )){
160
        case IPC_M_PHONE_HUNGUP:
216
        case IPC_M_PHONE_HUNGUP:
161
            return EOK;
217
            return EOK;
162
        case NET_NET_DEVICE:
218
        case NET_NET_DEVICE:
163
            // TODO configure, register
219
            // TODO configure, register
164
            printf( "Networking: new netif %d\n", IPC_GET_DEVICE( call ));
220
            printf( "Networking: new netif %d\n", IPC_GET_DEVICE( call ));
165
            return EOK;
221
            return EOK;
166
        case NET_NET_GET_DEVICE_CONF:
222
        case NET_NET_GET_DEVICE_CONF:
167
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
223
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
168
            net_get_device_conf_req( 0, IPC_GET_DEVICE( call ), & strings, IPC_GET_COUNT( call ), NULL );
224
            net_get_device_conf_req( 0, IPC_GET_DEVICE( call ), & strings, IPC_GET_COUNT( call ), NULL );
169
            // strings should not contain received data anymore
225
            // strings should not contain received data anymore
170
            free( data );
226
            free( data );
171
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
227
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
172
            free( strings );
228
            free( strings );
173
            return ERROR_CODE;
229
            return ERROR_CODE;
174
        case NET_NET_GET_CONF:
230
        case NET_NET_GET_CONF:
175
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
231
            ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call )));
176
            net_get_conf_req( 0, & strings, IPC_GET_COUNT( call ), NULL );
232
            net_get_conf_req( 0, & strings, IPC_GET_COUNT( call ), NULL );
177
            // strings should not contain received data anymore
233
            // strings should not contain received data anymore
178
            free( data );
234
            free( data );
179
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
235
            ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call ));
180
            free( strings );
236
            free( strings );
181
            return ERROR_CODE;
237
            return ERROR_CODE;
182
        case NET_NET_STARTUP:
238
        case NET_NET_STARTUP:
183
            return startup();
239
            return startup();
184
    }
240
    }
185
    return ENOTSUP;
241
    return ENOTSUP;
186
}
242
}
187
 
243
 
188
/*
244
/*
189
int parse_line( measured_strings_ref configuration, char * line ){
245
int parse_line( measured_strings_ref configuration, char * line ){
190
    ERROR_DECLARE;
246
    ERROR_DECLARE;
191
 
247
 
192
    measured_string_ref setting;
248
    measured_string_ref setting;
193
    char *          name;
249
    char *          name;
194
    char *          value;
250
    char *          value;
195
 
251
 
196
    // from the beginning
252
    // from the beginning
197
    name = line;
253
    name = line;
198
    // skip spaces
254
    // skip spaces
199
    while( isspace( * name )) ++ name;
255
    while( isspace( * name )) ++ name;
200
    // remember the name start
256
    // remember the name start
201
    value = name;
257
    value = name;
202
    // skip the name
258
    // skip the name
203
    while( isalnum( * value ) || ( * value == '_' )){
259
    while( isalnum( * value ) || ( * value == '_' )){
204
        // make uppercase
260
        // make uppercase
205
//      * value = toupper( * value );
261
//      * value = toupper( * value );
206
        ++ value;
262
        ++ value;
207
    }
263
    }
208
    if( * value == '=' ){
264
    if( * value == '=' ){
209
        // terminate the name
265
        // terminate the name
210
        * value = '\0';
266
        * value = '\0';
211
    }else{
267
    }else{
212
        // terminate the name
268
        // terminate the name
213
        * value = '\0';
269
        * value = '\0';
214
        // skip until '='
270
        // skip until '='
215
        ++ value;
271
        ++ value;
216
        while(( * value ) && ( * value != '=' )) ++ value;
272
        while(( * value ) && ( * value != '=' )) ++ value;
217
        // not found?
273
        // not found?
218
        if( * value != '=' ) return EINVAL;
274
        if( * value != '=' ) return EINVAL;
219
    }
275
    }
220
    ++ value;
276
    ++ value;
221
    // skip spaces
277
    // skip spaces
222
    while( isspace( * value )) ++ value;
278
    while( isspace( * value )) ++ value;
223
    // create a bulk measured string till the end
279
    // create a bulk measured string till the end
224
    setting = measured_string_create_bulk( value, -1 );
280
    setting = measured_string_create_bulk( value, -1 );
225
    if( ! setting ) return ENOMEM;
281
    if( ! setting ) return ENOMEM;
226
    // add the configuration setting
282
    // add the configuration setting
227
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
283
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
228
        free( setting );
284
        free( setting );
229
        return ERROR_CODE;
285
        return ERROR_CODE;
230
    }
286
    }
231
    return EOK;
287
    return EOK;
232
}
288
}
233
*/
289
*/
234
 
290
 
235
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
291
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){
236
    ERROR_DECLARE;
292
    ERROR_DECLARE;
237
 
293
 
238
    measured_string_ref setting;
294
    measured_string_ref setting;
239
 
295
 
240
    setting = measured_string_create_bulk( value, 0 );
296
    setting = measured_string_create_bulk( value, 0 );
241
    if( ! setting ) return ENOMEM;
297
    if( ! setting ) return ENOMEM;
242
    // add the configuration setting
298
    // add the configuration setting
243
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
299
    if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){
244
        free( setting );
300
        free( setting );
245
        return ERROR_CODE;
301
        return ERROR_CODE;
246
    }
302
    }
247
    return EOK;
303
    return EOK;
248
}
304
}
249
 
305
 
250
device_id_t generate_new_device_id( void ){
306
device_id_t generate_new_device_id( void ){
251
    return device_assign_devno();
307
    return device_assign_devno();
252
}
308
}
253
 
309
 
254
int read_configuration( void ){
310
int read_configuration( void ){
255
    ERROR_DECLARE;
311
    ERROR_DECLARE;
256
 
312
 
257
    // read general configuration
313
    // read general configuration
258
    ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "IPV", "4" ));
314
    ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "IPV", "4" ));
259
    ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "MTU", "1500" ));
315
    ERROR_PROPAGATE( add_configuration( & net_globals.configuration, "MTU", "1500" ));
260
    return EOK;
316
    return EOK;
261
}
317
}
262
 
318
 
263
int start_device( netif_ref netif ){
319
int start_device( netif_ref netif ){
264
    ERROR_DECLARE;
320
    ERROR_DECLARE;
265
 
321
 
266
    measured_string_ref setting;
322
    measured_string_ref setting;
267
    services_t          internet_service;
323
    services_t          internet_service;
268
    int                 irq;
324
    int                 irq;
269
    int                 io;
325
    int                 io;
270
    int                 mtu;
326
    int                 mtu;
271
 
327
 
272
    // mandatory netif
328
    // mandatory netif
273
    setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
329
    setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 );
274
    netif->driver = get_running_module( & net_globals.modules, setting->value );
330
    netif->driver = get_running_module( & net_globals.modules, setting->value );
275
    if( ! netif->driver ){
331
    if( ! netif->driver ){
276
        printf( "Failed to start the network interface driver %s\n", setting->value );
332
        printf( "Failed to start the network interface driver %s\n", setting->value );
277
        return EINVAL;
333
        return EINVAL;
278
    }
334
    }
279
    // optional network interface layer
335
    // optional network interface layer
280
    setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
336
    setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 );
281
    if( setting ){
337
    if( setting ){
282
        netif->nil = get_running_module( & net_globals.modules, setting->value );
338
        netif->nil = get_running_module( & net_globals.modules, setting->value );
283
        if( ! netif->nil ){
339
        if( ! netif->nil ){
284
            printf( "Failed to start the network interface layer %s\n", setting->value );
340
            printf( "Failed to start the network interface layer %s\n", setting->value );
285
            return EINVAL;
341
            return EINVAL;
286
        }
342
        }
287
    }else{
343
    }else{
288
        netif->nil = NULL;
344
        netif->nil = NULL;
289
    }
345
    }
290
    // mandatory internet layer
346
    // mandatory internet layer
291
    setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
347
    setting = measured_strings_find( & netif->configuration, CONF_IL, 0 );
292
    netif->il = get_running_module( & net_globals.modules, setting->value );
348
    netif->il = get_running_module( & net_globals.modules, setting->value );
293
    if( ! netif->il ){
349
    if( ! netif->il ){
294
        printf( "Failed to start the internet layer %s\n", setting->value );
350
        printf( "Failed to start the internet layer %s\n", setting->value );
295
        return EINVAL;
351
        return EINVAL;
296
    }
352
    }
297
    // end of the static loopback initialization
353
    // end of the static loopback initialization
298
    // startup the loopback interface
354
    // startup the loopback interface
299
    setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
355
    setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 );
300
    irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
356
    irq = setting ? strtol( setting->value, NULL, 10 ) : 0;
301
    setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
357
    setting = measured_strings_find( & netif->configuration, CONF_IO, 0 );
302
    io = setting ? strtol( setting->value, NULL, 16 ) : 0;
358
    io = setting ? strtol( setting->value, NULL, 16 ) : 0;
303
    ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io ));
359
    ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io ));
304
    if( netif->nil ){
360
    if( netif->nil ){
305
        setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
361
        setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 );
306
        if( ! setting ){
362
        if( ! setting ){
307
            setting = measured_strings_find( & net_globals.configuration, CONF_MTU, 0 );
363
            setting = measured_strings_find( & net_globals.configuration, CONF_MTU, 0 );
308
        }
364
        }
309
        mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
365
        mtu = setting ? strtol( setting->value, NULL, 10 ) : 0;
310
        ERROR_PROPAGATE( nil_device_req( netif->nil->phone, netif->id, mtu, netif->driver->service ));
366
        ERROR_PROPAGATE( nil_device_req( netif->nil->phone, netif->id, mtu, netif->driver->service ));
311
        internet_service = netif->nil->service;
367
        internet_service = netif->nil->service;
312
    }else{
368
    }else{
313
        internet_service = netif->driver->service;
369
        internet_service = netif->driver->service;
314
    }
370
    }
315
    switch( netif->il->service ){
371
    switch( netif->il->service ){
316
        case SERVICE_IP:
372
        case SERVICE_IP:
317
            ERROR_PROPAGATE( ip_device_req( netif->il->phone, netif->id, internet_service ));
373
            ERROR_PROPAGATE( ip_device_req( netif->il->phone, netif->id, internet_service ));
318
            break;
374
            break;
319
        default:
375
        default:
320
            return ENOENT;
376
            return ENOENT;
321
    }
377
    }
322
    ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id ));
378
    ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id ));
323
    return EOK;
379
    return EOK;
324
}
380
}
325
 
381
 
326
int startup( void ){
382
int startup( void ){
327
    ERROR_DECLARE;
383
    ERROR_DECLARE;
328
 
384
 
329
    char *      conf_files[] = { "lo", "ne2k" };
385
    char *      conf_files[] = { "lo", "ne2k" };
330
    int         count = sizeof( conf_files ) / sizeof( char * );
386
    int         count = sizeof( conf_files ) / sizeof( char * );
331
    int         index;
387
    int         index;
332
    netif_ref   netif;
388
    netif_ref   netif;
333
    int         i;
389
    int         i;
334
    measured_string_ref setting;
390
    measured_string_ref setting;
335
 
391
 
336
    // TODO dynamic configuration
392
    // TODO dynamic configuration
337
    ERROR_PROPAGATE( read_configuration());
393
    ERROR_PROPAGATE( read_configuration());
338
 
394
 
339
    for( i = 0; i < count; ++ i ){
395
    for( i = 0; i < count; ++ i ){
340
        netif = ( netif_ref ) malloc( sizeof( netif_t ));
396
        netif = ( netif_ref ) malloc( sizeof( netif_t ));
341
        if( ! netif ) return ENOMEM;
397
        if( ! netif ) return ENOMEM;
342
 
398
 
343
        netif->id = generate_new_device_id();
399
        netif->id = generate_new_device_id();
344
        if( ! netif->id ) return EXDEV;
400
        if( ! netif->id ) return EXDEV;
345
        ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
401
        ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration ));
346
        // read configuration files
402
        // read configuration files
347
        if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){
403
        if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){
348
            measured_strings_destroy( & netif->configuration );
404
            measured_strings_destroy( & netif->configuration );
349
            free( netif );
405
            free( netif );
350
            return ERROR_CODE;
406
            return ERROR_CODE;
351
        }
407
        }
352
        // mandatory name
408
        // mandatory name
353
        setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
409
        setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 );
354
        if( ! setting ){
410
        if( ! setting ){
355
            printf( "The name is missing\n" );
411
            printf( "The name is missing\n" );
356
            measured_strings_destroy( & netif->configuration );
412
            measured_strings_destroy( & netif->configuration );
357
            free( netif );
413
            free( netif );
358
            return EINVAL;
414
            return EINVAL;
359
        }
415
        }
360
        netif->name = setting->value;
416
        netif->name = setting->value;
361
        // add to the netifs map
417
        // add to the netifs map
362
        index = netifs_add( & net_globals.netifs, netif->id, netif );
418
        index = netifs_add( & net_globals.netifs, netif->id, netif );
363
        if( index < 0 ){
419
        if( index < 0 ){
364
            measured_strings_destroy( & netif->configuration );
420
            measured_strings_destroy( & netif->configuration );
365
            free( netif );
421
            free( netif );
366
            return index;
422
            return index;
367
        }
423
        }
368
        // add to the netif names map
424
        // add to the netif names map
369
        if( ERROR_OCCURRED( char_map_add( & net_globals.netif_names, netif->name, 0, index ))
425
        if( ERROR_OCCURRED( char_map_add( & net_globals.netif_names, netif->name, 0, index ))
370
        // start network interfaces and needed modules
426
        // start network interfaces and needed modules
371
        || ERROR_OCCURRED( start_device( netif ))){
427
        || ERROR_OCCURRED( start_device( netif ))){
372
            measured_strings_destroy( & netif->configuration );
428
            measured_strings_destroy( & netif->configuration );
373
            netifs_exclude_index( & net_globals.netifs, index );
429
            netifs_exclude_index( & net_globals.netifs, index );
374
            return ERROR_CODE;
430
            return ERROR_CODE;
375
        }
431
        }
376
        // increment modules' usage
432
        // increment modules' usage
377
        ++ netif->driver->usage;
433
        ++ netif->driver->usage;
378
        if( netif->nil ) ++ netif->nil->usage;
434
        if( netif->nil ) ++ netif->nil->usage;
379
        ++ netif->il->usage;
435
        ++ netif->il->usage;
380
        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 );
436
        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 );
381
    }
437
    }
382
    return EOK;
438
    return EOK;
383
}
439
}
384
 
440
 
385
/** @}
441
/** @}
386
 */
442
 */
387
 
443