/*
* 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 ip
* @{
*/
/** @file
*/
#include <async.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"
#include "../../include/net_interface.h"
#include "../../include/sockaddr.h"
#include "../../include/socket.h"
#include "../../include/device.h"
#include "../../include/arp_interface.h"
#include "../../include/nil_interface.h"
#include "../../include/il_interface.h"
#include "../../include/ip_interface.h"
#include "../../structures/measured_strings.h"
#include "../../structures/module_map.h"
#include "../../structures/packet/packet_client.h"
#include "../../nil/nil_messages.h"
#include "../il_messages.h"
#include "ip.h"
#include "ip_module.h"
#define DEFAULT_IPV 4
#define ARP_NAME "arp"
#define ARP_FILENAME "/srv/arp"
ip_globals_t ip_globals;
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state );
int ip_register( int il_phone, int protocol, int phone );
/** Initializes the module.
*/
int ip_initialize( async_client_conn_t client_connection ){
ERROR_DECLARE;
ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
ip_globals.client_connection = client_connection;
ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module ));
return EOK;
}
int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){
ERROR_DECLARE;
ip_netif_ref ip_netif;
measured_string_t names[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }};
measured_string_ref configuration;
int count = 9;
char * data;
int index;
configuration = & names[ 0 ];
ip_netif
= ( ip_netif_ref
) malloc( sizeof( ip_netif_t
));
if( ! ip_netif ) return ENOMEM;
ip_netif->device_id = device_id;
// get configuration
ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data ));
if( configuration ){
if( configuration[ 0 ].value ){
ip_netif
->ipv
= strtol( configuration
[ 0 ].
value, NULL
, 0 );
}else{
ip_netif->ipv = DEFAULT_IPV;
}
ip_netif->dhcp = ! str_lcmp( configuration[ 1 ].value, "dhcp", configuration[ 1 ].length );
if( ip_netif->dhcp ){
// TODO dhcp
net_free_settings( configuration, data );
return ENOTSUP;
}else if( ip_netif->ipv == 4 ){
if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
|| ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
|| ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
|| ( inet_pton( AF_INET, configuration[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
net_free_settings( configuration, data );
return EINVAL;
}
}else{
// TODO ipv6
net_free_settings( configuration, data );
return ENOTSUP;
}
if( configuration[ 8 ].value ){
ip_netif->arp = get_running_module( & ip_globals.modules, configuration[ 8 ].value );
if( ! ip_netif->arp ){
printf( "Failed to start the arp %s\n", configuration
[ 8 ].
value );
net_free_settings( configuration, data );
return EINVAL;
}
}else{
ip_netif->arp = NULL;
}
net_free_settings( configuration, data );
}
ip_netif->phone = bind_service( netif, ip_netif->device_id, SERVICE_IP, 0, ip_globals.client_connection );
if( ip_netif->phone < 0 ){
printf( "Failed to contact the nil service %d\n", netif
);
return ip_netif->phone;
}
if( ip_netif->arp ){
configuration[ 0 ].value = ( char * ) & ip_netif->address;
configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
if( ERROR_OCCURRED( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, netif, & configuration[ 0 ] ))){
return ERROR_CODE;
}
}
index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
if( index < 0 ){
return index;
}
if( ip_netif->arp ) ++ ip_netif->arp->usage;
// print the settings
printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif
->device_id
, ip_netif
->phone
, ip_netif
->ipv
);
printf( "\tconfiguration\t= %s\n", ip_netif
->dhcp
? "dhcp" : "static" );
// TODO ipv6 addresses
data
= malloc( INET_ADDRSTRLEN
);
if( data ){
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
printf( "\taddress\t= %s\n", data
);
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
printf( "\tnetmask\t= %s\n", data
);
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->gateway, data, INET_ADDRSTRLEN );
printf( "\tgateway\t= %s\n", data
);
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast, data, INET_ADDRSTRLEN );
printf( "\tbroadcast\t= %s\n", data
);
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN );
printf( "\tdns1\t= %s\n", data
);
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
printf( "\tdns2\t= %s\n", data
);
}
return EOK;
}
int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state ){
// ERROR_DECLARE;
ip_netif_ref netif;
/* measured_string_t address;
measured_string_ref translation;
char * data;
*/
netif = ip_netifs_find( & ip_globals.netifs, device_id );
if( ! netif ) return ENOENT;
// TODO state
printf( "ip - device %d changed state to %d\n\n", device_id
, state
);
/* if( netif->arp ){
address.value = ( char * ) & netif->gateway;
address.length = CONVERT_SIZE( in_addr_t, char, 1 );
if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ))){
sleep( 2 );
ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ));
}
printf( "\tgateway translated to\t= %X:%X:%X:%X:%X:%X\n", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] );
free( translation );
free( data );
}
*/ return EOK;
}
int ip_bind_service( services_t service, services_t me, async_client_conn_t receiver ){
//TODO receive function
return EOK;
}
int ip_connect_module( services_t service ){
return EOK;
}
int ip_register( int il_phone, int protocol, int phone ){
ip_proto_ref proto;
int index;
proto
= ( ip_proto_ref
) malloc( sizeof( ip_protos_t
));
if( ! proto ) return ENOMEM;
proto->protocol = protocol;
proto->phone = phone;
index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
if( index < 0 ){
return index;
}
printf( "New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto
->protocol
, proto
->phone
);
return EOK;
}
int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t target ){
// TODO send packet
printf( "Packet to send via %d: %s\n", device_id
, packet_get_data
( packet
));
pq_release( ip_globals.net_phone, packet_get_id( packet ));
return EOK;
}
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
ERROR_DECLARE;
packet_t packet;
* answer_count = 0;
switch( IPC_GET_METHOD( * call )){
case IPC_M_PHONE_HUNGUP:
return EOK;
case NET_IL_DEVICE:
return ip_device_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
case IPC_M_CONNECT_TO_ME:
return ip_register( 0, IL_GET_PROTO( call ), IPC_GET_PHONE( call ));
case NET_IL_SEND:
ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
return ip_send_msg( 0, IPC_GET_DEVICE( call ), packet, 0 );
case NET_IL_DEVICE_STATE:
case NET_NIL_DEVICE_STATE:
return ip_device_state_msg( 0, IPC_GET_DEVICE( call ), IPC_GET_STATE( call ));
// TODO packet received
case NET_IL_RECEIVED:
case NET_NIL_RECEIVED:
ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call )));
//return il_receive_msg( 0, IPC_GET_DEVICE( call ), packet );
}
return ENOTSUP;
}
/** @}
*/