Subversion Repositories HelenOS

Rev

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

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