/*
* Copyright (c) 2008 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 <errno.h>
#include <stdio.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
//#include <sys/mman.h>
#include "../err.h"
#include "../measured_strings.h"
#include "../messages.h"
#include "../modules.h"
#include "../netif/netif_device_id_type.h"
#include "ip.h"
#define DEFAULT_IPV 4
ip_globals_t ip_globals;
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
int parse_address( char * value, address_ref address );
/** Initializes the module.
*/
int ip_initialize( void ){
ip_netifs_initialize( & ip_globals.netifs );
return EOK;
}
int ip_call( ipc_callid_t callid ){
return EOK;
}
int parse_address( char * value, address_ref address ){
char * next;
int index;
if( ! value ){
( * address )[ 0 ] = ( * address )[ 1 ] = ( * address )[ 2 ] = ( * address )[ 3 ] = 0;
return ENOENT;
}
next = value;
for( index = 0; index < 4; ++ index ){
if(( ! next ) || ( ! * next )) return EINVAL;
if( index ) ++ next;
( * address
)[ index
] = strtoul( next
, & next
, 0 );
}
return EOK;
}
int ip_message( ipc_callid_t callid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t * result1, ipcarg_t * result2, ipcarg_t * result3 ){
ERROR_DECLARE;
ip_netif_ref ip_netif;
aid_t message;
ipc_call_t answer;
measured_string_t configuration[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }};
int count = 9;
measured_string_ref settings;
char * data;
switch( method ){
case IPC_M_PHONE_HUNGUP:
return EOK;
case NET_IP_ECHO:
if( result1 ) * result1 = arg1;
if( result2 ) * result2 = arg2;
if( result3 ) * result3 = arg3;
return EOK;
case NET_IL_DEVICE:
ip_netif
= ( ip_netif_ref
) malloc( sizeof( ip_netif_t
));
if( ! ip_netif ) return ENOMEM;
ip_netif->device_id = arg1;
// get configuration
message = async_send_2( ip_globals.networking_phone, NET_NETWORKING_GET_DEVICE_CONFIGURATION, ip_netif->device_id, count, & answer );
// send names and get settings
if( ERROR_OCCURED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
|| ERROR_OCCURED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
async_wait_for( message, NULL );
return ERROR_CODE;
}
if( settings ){
if( settings[ 0 ].value ){
ip_netif
->ipv
= strtol( settings
[ 0 ].
value, NULL
, 0 );
}else{
ip_netif->ipv = DEFAULT_IPV;
}
ip_netif
->dhcp
= ! strcmp( settings
[ 1 ].
value, "DHCP" );
if( ip_netif->dhcp ){
// TODO dhcp
return ENOTSUP;
}else if( ip_netif->ipv == 4 ){
if( ERROR_OCCURED( parse_address( settings[ 2 ].value, & ip_netif->address ))
|| ERROR_OCCURED( parse_address( settings[ 3 ].value, & ip_netif->netmask ))
|| ( parse_address( settings[ 4 ].value, & ip_netif->gateway ) == EINVAL )
|| ( parse_address( settings[ 5 ].value, & ip_netif->broadcast ) == EINVAL )
|| ( parse_address( settings[ 6 ].value, & ip_netif->dns1 ) == EINVAL )
|| ( parse_address( settings[ 7 ].value, & ip_netif->dns2 ) == EINVAL )){
return EINVAL;
}
}else{
// TODO ipv6
return ENOTSUP;
}
// TODO ARP module
}
// print the settings
printf( "\n -IPV=%d", ip_netif
->ipv
);
printf( "\n -configuration=%s", ip_netif
->dhcp
? "dhcp" : "static" );
// TODO ipv6
printf( "\n -address=%d.%d.%d.%d", ip_netif
->address
[ 0 ], ip_netif
->address
[ 1 ], ip_netif
->address
[ 2 ], ip_netif
->address
[ 3 ] );
printf( "\n -netmask=%d.%d.%d.%d", ip_netif
->netmask
[ 0 ], ip_netif
->netmask
[ 1 ], ip_netif
->netmask
[ 2 ], ip_netif
->netmask
[ 3 ] );
printf( "\n -gateway=%d.%d.%d.%d", ip_netif
->gateway
[ 0 ], ip_netif
->gateway
[ 1 ], ip_netif
->gateway
[ 2 ], ip_netif
->gateway
[ 3 ] );
printf( "\n -broadcast=%d.%d.%d.%d", ip_netif
->broadcast
[ 0 ], ip_netif
->broadcast
[ 1 ], ip_netif
->broadcast
[ 2 ], ip_netif
->broadcast
[ 3 ] );
printf( "\n -dns1=%d.%d.%d.%d", ip_netif
->dns1
[ 0 ], ip_netif
->dns1
[ 1 ], ip_netif
->dns1
[ 2 ], ip_netif
->dns1
[ 3 ] );
printf( "\n -dns2=%d.%d.%d.%d", ip_netif
->dns2
[ 0 ], ip_netif
->dns2
[ 1 ], ip_netif
->dns2
[ 2 ], ip_netif
->dns2
[ 3 ] );
// TODO arp module
// end request
async_wait_for( message, NULL );
ip_netif->phone = connect_to_service( arg2 );
if( ERROR_OCCURED( async_req_2_0( ip_netif->phone, NET_LL_REGISTER, arg1, SERVICE_IP ))
|| ERROR_OCCURED( ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif ))){
return ERROR_CODE;
}
return EOK;
case NET_IL_DEVICE_STATE_CHANGED:
case NET_LL_DEVICE_STATE_CHANGED:
// arg1 device id
// arg2 state
// TODO state
printf( "\nip - device %d changed state to %d\n", arg1
, arg2
);
case NET_IP_TCP_REGISTER:
ip_globals.tcp_phone = connect_to_service( arg1 );
return EOK;
}
return ENOTSUP;
}
/** @}
*/