Subversion Repositories HelenOS

Rev

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

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