/*
* 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 lo
* @{
*/
/** @file
*/
#include <async.h>
#include <errno.h>
#include <stdio.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../structures/measured_strings.h"
#include "../../structures/packet/packet_client.h"
#include "../../include/device.h"
#include "../../include/nil_messages.h"
#include "../netif.h"
#include "../netif_interface.h"
#define DEFAULT_MTU 1500
#define DEFAULT_ADDR "\0\0\0\0\0\0"
#define DEFAULT_ADDR_LEN ( sizeof( DEFAULT_ADDR ) / sizeof( char ))
#define NAME "lo - loopback interface"
netif_globals_t netif_globals;
static struct lo_globals{
int mtu;
} lo_globals;
static int change_state_message( device_ref device, device_state_t state );
static int create( device_id_t device_id, device_ref * device );
void netif_print_name( void );
int netif_specific_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
//TODO nil send message
return ENOTSUP;
}
int netif_get_addr_message( device_id_t device_id, measured_string_ref address ){
if( ! address ) return EBADMEM;
address->value = DEFAULT_ADDR;
address->length = DEFAULT_ADDR_LEN;
return EOK;
}
int netif_get_device_stats( device_id_t device_id, device_stats_ref stats ){
ERROR_DECLARE;
device_ref device;
if( ! stats ) return EBADMEM;
ERROR_PROPAGATE( find_device( device_id, & device ));
memcpy( stats
, ( device_stats_ref
) device
->specific
, sizeof( device_stats_t
));
return EOK;
}
static int change_state_message( device_ref device, device_state_t state ){
if( device->state != state ){
device->state = state;
printf( "\nState changed to %s", ( state
== NETIF_ACTIVE
) ? "ACTIVE" : "STOPPED" );
return state;
}
return EOK;
}
static int create( device_id_t device_id, device_ref * device ){
int index;
if( device_map_count( & netif_globals.device_map ) > 0 ){
return EXDEV;
}else{
* device
= ( device_ref
) malloc( sizeof( device_t
));
if( !( * device )) return ENOMEM;
( ** device
).
specific = malloc( sizeof( device_stats_t
));
if( ! ( ** device ).specific ){
return ENOMEM;
}
null_device_stats(( device_stats_ref )( ** device ).specific );
( ** device ).device_id = device_id;
( ** device ).nil_phone = -1;
( ** device ).state = NETIF_STOPPED;
index = device_map_add( & netif_globals.device_map, ( ** device ).device_id, * device );
if( index < 0 ){
free(( ** device
).
specific );
* device = NULL;
return index;
}
}
return EOK;
}
int netif_initialize( void ){
ipcarg_t phonehash;
return REGISTER_ME( SERVICE_LO, & phonehash );
}
void netif_print_name( void ){
}
int netif_probe_auto_message( void ){
/* ERROR_DECLARE;
device_ref device;
ERROR_PROPAGATE( create( arg1, & device ));
ipc_call_sync_3_3( netif_globals.networking_phone, NET_NET_DEVICE, device->device_id, NULL, NULL, NULL, NULL, NULL );
*/ return ENOTSUP;
}
int netif_probe_message( device_id_t device_id, int irq, int io ){
ERROR_DECLARE;
device_ref device;
aid_t message;
ipc_call_t answer;
measured_string_t configuration[ 1 ] = {{ "MTU", 3 }};
int count = 1;
measured_string_ref settings;
char * data;
ipcarg_t result;
// create a new device
ERROR_PROPAGATE( create( device_id, & device ));
// get configuration
message = async_send_2( netif_globals.networking_phone, NET_NET_GET_DEVICE_CONF, device->device_id, count, & answer );
// send names and get settings
if( ERROR_OCCURRED( measured_strings_send( netif_globals.networking_phone, configuration, count ))
|| ERROR_OCCURRED( measured_strings_return( netif_globals.networking_phone, & settings, & data, count ))){
async_wait_for( message, NULL );
return ERROR_CODE;
}
// end request
async_wait_for( message, & result );
if( ERROR_OCCURRED( result )){
if( settings ){
}
return ERROR_CODE;
}
// MTU is the first one
if( settings && ( settings[ 0 ].value )){
lo_globals.
mtu = strtoul( settings
[ 0 ].
value, NULL
, 0 );
}else{
lo_globals.mtu = DEFAULT_MTU;
}
// print the settings
printf("\nNew device registered:\n\tid\t= %d\n\tMTU\t= %d", device
->device_id
, lo_globals.
mtu );
return EOK;
}
int netif_send_message( device_id_t device_id, packet_t packet, services_t sender ){
ERROR_DECLARE;
device_ref device;
size_t length;
packet_t next;
int phone;
ERROR_PROPAGATE( find_device( device_id, & device ));
if( device->state != NETIF_ACTIVE ) return EPERM;
next = packet;
do{
++ (( device_stats_ref ) device->specific )->tx_packets;
++ (( device_stats_ref ) device->specific )->rx_packets;
length = packet_get_data_length( next );
(( device_stats_ref ) device->specific )->tx_bytes += length;
(( device_stats_ref ) device->specific )->rx_bytes += length;
next = pq_next( next );
}while( next );
phone = device->nil_phone;
rwlock_write_unlock( & netif_globals.lock );
nil_received_msg( phone, device_id, packet, sender );
rwlock_write_lock( & netif_globals.lock );
return EOK;
}
int netif_start_message( device_ref device ){
return change_state_message( device, NETIF_ACTIVE );
}
int netif_stop_message( device_ref device ){
return change_state_message( device, NETIF_STOPPED );
}
/** @}
*/