0,0 → 1,575 |
/* |
* Copyright (c) 2009 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
/** @addtogroup net |
* @{ |
*/ |
|
/** @file |
*/ |
|
#include <async.h> |
#include <ctype.h> |
#include <ddi.h> |
#include <errno.h> |
#include <malloc.h> |
#include <stdio.h> |
|
#include <ipc/ipc.h> |
#include <ipc/services.h> |
|
#include "../err.h" |
#include "../messages.h" |
#include "../modules.h" |
//#include "../self_test.h" |
|
#include "../structures/char_map.h" |
#include "../structures/generic_char_map.h" |
#include "../structures/measured_strings.h" |
#include "../structures/module_map.h" |
#include "../structures/packet/packet.h" |
#include "../structures/packet/packet_server.h" |
|
#include "../il/ip/ip_messages.h" |
#include "../include/device.h" |
#include "../include/netif_messages.h" |
#include "../include/nil_messages.h" |
/* |
#if IP_BUNDLE |
|
#include "../ip/ip_module.h" |
|
#endif |
|
#if TCP_BUNDLE |
|
#include "../tcp/tcp_module.h" |
|
#endif |
*/ |
#define NAME "Networking" |
|
#define LO_NAME "lo" |
#define LO_FILENAME "/srv/lo" |
#define DP8390_NAME "dp8390" |
#define DP8390_FILENAME "/srv/dp8390" |
#define ETHERNET_NAME "ethernet" |
#define ETHERNET_FILENAME "/srv/eth" |
#define IP_NAME "ip" |
#define IP_FILENAME "/srv/ip" |
|
#define CONF_NAME "NAME" |
#define CONF_NETIF "NETIF" |
#define CONF_NIL "NIL" |
#define CONF_IL "IL" |
#define CONF_IRQ "IRQ" |
#define CONF_IO "IO" |
#define CONF_MTU "MTU" |
|
#define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call ) |
#define IPC_GET_COUNT( call ) ( int ) IPC_GET_ARG2( * call ) |
|
typedef struct netif netif_t; |
typedef netif_t * netif_ref; |
|
typedef struct networking_globals networking_globals_t; |
|
DEVICE_MAP_DECLARE( netifs, netif_t ) |
|
GENERIC_CHAR_MAP_DECLARE( measured_strings, measured_string_t ) |
|
/** A present network interface device. |
*/ |
struct netif{ |
/** A system-unique network interface identifier. |
*/ |
device_id_t id; |
/** A serving network interface driver module index. |
*/ |
module_ref driver; |
/** A serving link layer module index. |
*/ |
module_ref nil; |
/** A serving internet layer module index. |
*/ |
module_ref il; |
/** A system-unique network interface name. |
*/ |
char * name; |
/** Configuration. |
*/ |
measured_strings_t configuration; |
}; |
|
/** A networking module global variables. |
*/ |
struct networking_globals{ |
/** Present network interfaces. |
*/ |
netifs_t netifs; |
/** Network interface structure indices by names. |
*/ |
char_map_t netif_names; |
/** Available modules. |
*/ |
modules_t modules; |
/** Global configuration. |
*/ |
measured_strings_t configuration; |
}; |
|
void networking_print_name( void ); |
measured_string_ref configuration_find( measured_strings_ref configuration, const char * name ); |
int networking_start_module( async_client_conn_t client_connection ); |
int networking_initialize( void ); |
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ); |
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ); |
//int parse_line( measured_strings_ref configuration, char * line ); |
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ); |
int read_configuration( void ); |
int read_netif_configuration( char * name, netif_ref netif ); |
int start_device( netif_ref netif ); |
int startup( void ); |
device_id_t generate_new_device_id( void ); |
|
static networking_globals_t networking_globals; |
|
DEVICE_MAP_IMPLEMENT( netifs, netif_t ) |
|
GENERIC_CHAR_MAP_IMPLEMENT( measured_strings, measured_string_t ) |
|
void networking_print_name( void ){ |
printf( "%s", NAME ); |
} |
|
int networking_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
/*#ifdef NETWORKING_module |
//TODO map to *_message |
if( IS_NET_IL_MESSAGE( call ) || IS_NET_IP_MESSAGE( call )){ |
return ip_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_ARP_MESSAGE( call )){ |
return arp_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_RARP_MESSAGE( call )){ |
return rarp_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_ICMP_MESSAGE( call )){ |
return icmp_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_UDP_MESSAGE( call )){ |
return udp_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_TCP_MESSAGE( call )){ |
return tcp_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_SOCKET_MESSAGE( call )){ |
return socket_message( callid, call, answer, answer_count ); |
*//* }else if( IS_NET_NIL_MESSAGE( call ) || IS_NET_ETHERNET_MESSAGE( call )){ |
return ethernet_message( callid, call, answer, answer_count ); |
*//* }else{ |
#endif |
*/ if( IS_NET_PACKET_MESSAGE( call )){ |
return packet_server_message( callid, call, answer, answer_count ); |
}else{ |
return net_message( callid, call, answer, answer_count ); |
} |
/*#ifdef NETWORKING_module |
} |
#endif |
*/ |
} |
|
int networking_start_module( async_client_conn_t client_connection ){ |
ERROR_DECLARE; |
|
ipcarg_t phonehash; |
|
async_set_client_connection( client_connection ); |
ERROR_PROPAGATE( pm_init()); |
if( ERROR_OCCURRED( networking_initialize()) |
|| ERROR_OCCURRED( REGISTER_ME( SERVICE_NETWORKING, & phonehash ))){ |
pm_destroy(); |
return ERROR_CODE; |
} |
|
async_manager(); |
|
pm_destroy(); |
return EOK; |
} |
|
int networking_initialize( void ){ |
ERROR_DECLARE; |
|
task_id_t task_id; |
|
netifs_initialize( & networking_globals.netifs ); |
char_map_initialize( & networking_globals.netif_names ); |
modules_initialize( & networking_globals.modules ); |
measured_strings_initialize( & networking_globals.configuration ); |
|
// run self tests |
// ERROR_PROPAGATE( self_test()); |
|
ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, LO_NAME, LO_FILENAME, SERVICE_LO, 0 )); |
ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0 )); |
ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0 )); |
|
//#ifdef NETWORKING_modular |
task_id = spawn( "/srv/ip" ); |
if( ! task_id ) return EINVAL; |
ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_id )); |
// if( ! spawn( "/srv/udp" )) return EINVAL; |
// if( ! spawn( "/srv/tcp" )) return EINVAL; |
// if( ! spawn( "/srv/socket" )) return EINVAL; |
// not always necesssary |
// if( ! spawn( "/srv/arp" )) return EINVAL; |
// if( ! spawn( "/srv/rarp" )) return EINVAL; |
// if( ! spawn( "/srv/icmp" )) return EINVAL; |
/* |
#else |
#ifdef NETWORKING_module |
ipcarg_t phonehash; |
|
ERROR_PROPAGATE( REGISTER_ME( SERVICE_IP, & phonehash )); |
ERROR_PROPAGATE( add_module( NULL, & networking_globals.modules, IP_NAME, IP_FILENAME, SERVICE_IP, task_get_id())); |
ERROR_PROPAGATE( ip_initialize()); |
// ERROR_PROPAGATE( REGISTER_ME( SERVICE_ARP, & phonehash )); |
// ERROR_PROPAGATE( arp_initialize()); |
// ERROR_PROPAGATE( REGISTER_ME( SERVICE_RARP, & phonehash )); |
// ERROR_PROPAGATE( rarp_initialize()); |
// ERROR_PROPAGATE( REGISTER_ME( SERVICE_ICMP, & phonehash )); |
// ERROR_PROPAGATE( icmp_initialize()); |
// ERROR_PROPAGATE( REGISTER_ME( SERVICE_UDP, & phonehash )); |
// ERROR_PROPAGATE( udp_initialize()); |
ERROR_PROPAGATE( REGISTER_ME( SERVICE_TCP, & phonehash )); |
ERROR_PROPAGATE( tcp_initialize()); |
// ERROR_PROPAGATE( REGISTER_ME( SERVICE_SOCKET, & phonehash )); |
// ERROR_PROPAGATE( socket_initialize()); |
// ERROR_PROPAGATE( REGISTER_ME( SERVICE_ETHERNET, & phonehash )); |
// ERROR_PROPAGATE( ethernet_initialize()); |
#endif |
#endif |
*/ |
return EOK; |
} |
|
//TODO as ip.c |
int net_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
ERROR_DECLARE; |
|
measured_string_ref strings; |
char * data; |
int index; |
measured_string_ref setting; |
measured_strings_ref configuration; |
netif_ref netif; |
|
* answer_count = 0; |
switch( IPC_GET_METHOD( * call )){ |
case IPC_M_PHONE_HUNGUP: |
return EOK; |
case NET_NET_DEVICE: |
// TODO configure, register |
printf( "\nNetworking: new netif %d", IPC_GET_DEVICE( call )); |
return EOK; |
case NET_NET_GET_DEVICE_CONF: |
ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call ))); |
netif = netifs_find( & networking_globals.netifs, IPC_GET_DEVICE( call )); |
if( netif ){ |
configuration = & netif->configuration; |
}else{ |
configuration = NULL; |
} |
for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){ |
setting = measured_strings_find( configuration, strings[ index ].value, 0 ); |
if( ! setting ){ |
setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 ); |
} |
if( setting ){ |
strings[ index ].length = setting->length; |
strings[ index ].value = setting->value; |
}else{ |
strings[ index ].length = 0; |
strings[ index ].value = NULL; |
} |
} |
// strings should not contain received data anymore |
free( data ); |
ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call )); |
free( strings ); |
return ERROR_CODE; |
case NET_NET_GET_CONF: |
// arg1 = count |
ERROR_PROPAGATE( measured_strings_receive( & strings, & data, IPC_GET_COUNT( call ))); |
for( index = 0; index < IPC_GET_COUNT( call ); ++ index ){ |
setting = measured_strings_find( & networking_globals.configuration, strings[ index ].value, 0 ); |
if( setting ){ |
strings[ index ].length = setting->length; |
strings[ index ].value = setting->value; |
}else{ |
strings[ index ].length = 0; |
strings[ index ].value = NULL; |
} |
} |
// strings should not contain received data anymore |
free( data ); |
ERROR_CODE = measured_strings_reply( strings, IPC_GET_COUNT( call )); |
free( strings ); |
return ERROR_CODE; |
case NET_NET_STARTUP: |
return startup(); |
} |
return ENOTSUP; |
} |
|
/* |
int parse_line( measured_strings_ref configuration, char * line ){ |
ERROR_DECLARE; |
|
measured_string_ref setting; |
char * name; |
char * value; |
|
// from the beginning |
name = line; |
// skip spaces |
while( isspace( * name )) ++ name; |
// remember the name start |
value = name; |
// skip the name |
while( isalnum( * value ) || ( * value == '_' )){ |
// make uppercase |
// * value = toupper( * value ); |
++ value; |
} |
if( * value == '=' ){ |
// terminate the name |
* value = '\0'; |
}else{ |
// terminate the name |
* value = '\0'; |
// skip until '=' |
++ value; |
while(( * value ) && ( * value != '=' )) ++ value; |
// not found? |
if( * value != '=' ) return EINVAL; |
} |
++ value; |
// skip spaces |
while( isspace( * value )) ++ value; |
// create a bulk measured string till the end |
setting = measured_string_create_bulk( value, -1 ); |
if( ! setting ) return ENOMEM; |
// add the configuration setting |
if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){ |
free( setting ); |
return ERROR_CODE; |
} |
return EOK; |
} |
*/ |
|
int add_configuration( measured_strings_ref configuration, const char * name, const char * value ){ |
ERROR_DECLARE; |
|
measured_string_ref setting; |
|
setting = measured_string_create_bulk( value, 0 ); |
if( ! setting ) return ENOMEM; |
// add the configuration setting |
if( ERROR_OCCURRED( measured_strings_add( configuration, name, 0, setting ))){ |
free( setting ); |
return ERROR_CODE; |
} |
return EOK; |
} |
|
device_id_t generate_new_device_id( void ){ |
return device_assign_devno(); |
} |
|
int read_configuration( void ){ |
ERROR_DECLARE; |
|
// read general configuration |
ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "IPV", "4" )); |
ERROR_PROPAGATE( add_configuration( & networking_globals.configuration, "MTU", "1500" )); |
return EOK; |
} |
|
int read_netif_configuration( char * name, netif_ref netif ){ |
ERROR_DECLARE; |
|
if( strncmp( name, "lo", 2 ) == 0 ){ |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NAME", LO_NAME )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETIF", LO_NAME )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IL", IP_NAME )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_CONFIG", "static" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_ADDR", "127.0.0.1" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETMASK", "255.0.0.0" )); |
}else if( strncmp( name, "ne2k", 4 ) == 0 ){ |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NAME", "eth0" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETIF", DP8390_NAME )); |
#ifdef NETWORKING_module |
// ethernet bundled in dp8390 |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NIL", DP8390_NAME )); |
#else |
// standalone ethernet |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NIL", ETHERNET_NAME )); |
#endif |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IL", IP_NAME )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IRQ", "9" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IO", "300" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "MTU", "1492" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_CONFIG", "static" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "IP_ADDR", "10.0.2.15" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "NETMASK", "255.255.255.0" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "BROADCAST", "10.0.2.255" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "GATEWAY", "10.0.2.2" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "DNS1", "10.0.2.2" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "DNS2", "10.0.2.2" )); |
ERROR_PROPAGATE( add_configuration( & netif->configuration, "ARP", "arp" )); |
} |
return EOK; |
} |
|
int start_device( netif_ref netif ){ |
ERROR_DECLARE; |
|
measured_string_ref setting; |
services_t internet_service; |
int irq; |
int io; |
int mtu; |
|
// mandatory netif |
setting = measured_strings_find( & netif->configuration, CONF_NETIF, 0 ); |
netif->driver = get_running_module( & networking_globals.modules, setting->value ); |
if( ! netif->driver ){ |
printf( "\nFailed to start the network interface driver %s", setting->value ); |
return EINVAL; |
} |
// optional network interface layer |
setting = measured_strings_find( & netif->configuration, CONF_NIL, 0 ); |
if( setting ){ |
netif->nil = get_running_module( & networking_globals.modules, setting->value ); |
if( ! netif->nil ){ |
printf( "\nFailed to start the network interface layer %s", setting->value ); |
return EINVAL; |
} |
}else{ |
netif->nil = NULL; |
} |
// mandatory internet layer |
setting = measured_strings_find( & netif->configuration, CONF_IL, 0 ); |
netif->il = get_running_module( & networking_globals.modules, setting->value ); |
if( ! netif->il ){ |
printf( "\nFailed to start the internet layer %s", setting->value ); |
return EINVAL; |
} |
// end of the static loopback initialization |
// startup the loopback interface |
setting = measured_strings_find( & netif->configuration, CONF_IRQ, 0 ); |
irq = setting ? strtol( setting->value, NULL, 10 ) : 0; |
setting = measured_strings_find( & netif->configuration, CONF_IO, 0 ); |
io = setting ? strtol( setting->value, NULL, 16 ) : 0; |
ERROR_PROPAGATE( netif_probe_req( netif->driver->phone, netif->id, irq, io )); |
if( netif->nil ){ |
setting = measured_strings_find( & netif->configuration, CONF_MTU, 0 ); |
if( ! setting ){ |
setting = measured_strings_find( & networking_globals.configuration, CONF_MTU, 0 ); |
} |
mtu = setting ? strtol( setting->value, NULL, 10 ) : 0; |
ERROR_PROPAGATE( nil_device_req( netif->nil->phone, netif->id, mtu, netif->driver->service )); |
internet_service = netif->nil->service; |
}else{ |
internet_service = netif->driver->service; |
} |
// TODO IL_BUNDLE |
ERROR_PROPAGATE( async_req_3_0( netif->il->phone, NET_IL_DEVICE, netif->id, 0, internet_service )); |
ERROR_PROPAGATE( netif_start_req( netif->driver->phone, netif->id )); |
return EOK; |
} |
|
int startup( void ){ |
ERROR_DECLARE; |
|
char * conf_files[] = { "lo", "ne2k" }; |
int count = sizeof( conf_files ) / sizeof( char * ); |
int index; |
netif_ref netif; |
int i; |
measured_string_ref setting; |
|
// TODO dynamic configuration |
ERROR_PROPAGATE( read_configuration()); |
|
for( i = 0; i < count; ++ i ){ |
netif = ( netif_ref ) malloc( sizeof( netif_t )); |
if( ! netif ) return ENOMEM; |
|
netif->id = generate_new_device_id(); |
if( ! netif->id ) return EXDEV; |
ERROR_PROPAGATE( measured_strings_initialize( & netif->configuration )); |
// read configuration files |
if( ERROR_OCCURRED( read_netif_configuration( conf_files[ i ], netif ))){ |
measured_strings_destroy( & netif->configuration ); |
free( netif ); |
return ERROR_CODE; |
} |
// mandatory name |
setting = measured_strings_find( & netif->configuration, CONF_NAME, 0 ); |
if( ! setting ){ |
printf( "\nThe name is missing" ); |
measured_strings_destroy( & netif->configuration ); |
free( netif ); |
return EINVAL; |
} |
netif->name = setting->value; |
// add to the netifs map |
index = netifs_add( & networking_globals.netifs, netif->id, netif ); |
if( index < 0 ){ |
measured_strings_destroy( & netif->configuration ); |
free( netif ); |
return index; |
} |
// add to the netif names map |
if( ERROR_OCCURRED( char_map_add( & networking_globals.netif_names, netif->name, 0, index )) |
// start network interfaces and needed modules |
|| ERROR_OCCURRED( start_device( netif ))){ |
measured_strings_destroy( & netif->configuration ); |
netifs_exclude_index( & networking_globals.netifs, index ); |
return ERROR_CODE; |
} |
// increment modules' usage |
++ netif->driver->usage; |
if( netif->nil ) ++ netif->nil->usage; |
++ netif->il->usage; |
printf( "\nNew network interface started:\n\tname\t= %s\n\tid\t= %d\n\tdriver\t= %s\n\tnil\t= %s\n\til\t= %s", netif->name, netif->id, netif->driver->name, netif->nil ? netif->nil->name : NULL, netif->il->name ); |
} |
return EOK; |
} |
|
/** @} |
*/ |
Property changes: |
Added: svn:eol-style |
+native |
\ No newline at end of property |