Subversion Repositories HelenOS

Rev

Rev 4192 | Rev 4261 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 * 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 <ipc/ipc.h>
#include <ipc/services.h>

#include "../../err.h"
#include "../../messages.h"
#include "../../modules.h"

#include "../../include/sockaddr.h"
#include "../../include/socket.h"
#include "../../include/device.h"
#include "../../structures/measured_strings.h"
#include "../../structures/module_map.h"
#include "../../structures/packet/packet_client.h"

#include "ip.h"
#include "ip_messages.h"
#include "ip_module.h"

#define DEFAULT_IPV     4

#define ARP_NAME                "arp"
#define ARP_FILENAME            "/srv/arp"

#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG1( * call )
#define IPC_GET_PROTO( call )       ( int ) IPC_GET_ARG1( * call )
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG2( * call )
#define IPC_GET_STATE( call )       ( device_state_t ) IPC_GET_ARG2( * call )
#define IPC_GET_PHONE( call )       ( int ) IPC_GET_ARG5( * call )

ip_globals_t    ip_globals;

DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )

INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )

int ip_register_message( int protocol, int phone );
int ip_state_message( device_id_t device_id, device_state_t state );
void    ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall );

/** Initializes the module.
 */
int ip_initialize( void ){
    ERROR_DECLARE;

    ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
    ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
    ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
    ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, 0 ));
    return EOK;
}

int ip_echo_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t * answer1, ipcarg_t * answer2, ipcarg_t * answer3, ipcarg_t * answer4, ipcarg_t * answer5 ){
    if( answer1 ) * answer1 = arg1;
    if( answer2 ) * answer2 = arg2;
    if( answer3 ) * answer3 = arg3;
    if( answer4 ) * answer4 = arg4;
    if( answer5 ) * answer5 = arg5;
    return EOK;
}

int ip_device_message( device_id_t device_id, services_t service ){
    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;
    ipcarg_t        result;
    int             index;

    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
    if( ! ip_netif ) return ENOMEM;
    ip_netif->device_id = device_id;
    // get configuration
    // TODO mapping
    message = async_send_2( ip_globals.networking_phone, NET_NET_GET_DEVICE_CONF, ip_netif->device_id, count, & answer );
    // send names and get settings
    if( ERROR_OCCURRED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
    || ERROR_OCCURRED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
        async_wait_for( message, NULL );
        free( ip_netif );
        return ERROR_CODE;
    }
    async_wait_for( message, & result );
    if( ERROR_OCCURRED( result )){
        if( settings ){
            free( settings );
            free( data );
        };
        free( ip_netif );
        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 = ! strncmp( settings[ 1 ].value, "dhcp", 4 );
        if( ip_netif->dhcp ){
            // TODO dhcp
            free( settings );
            free( data );
            free( ip_netif );
            return ENOTSUP;
        }else if( ip_netif->ipv == 4 ){
            if( ERROR_OCCURRED( inet_pton( AF_INET, settings[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
            || ERROR_OCCURRED( inet_pton( AF_INET, settings[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
            || ( inet_pton( AF_INET, settings[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
            || ( inet_pton( AF_INET, settings[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
            || ( inet_pton( AF_INET, settings[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
            || ( inet_pton( AF_INET, settings[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
                free( settings );
                free( data );
                free( ip_netif );
                return EINVAL;
            }
        }else{
            // TODO ipv6
            free( settings );
            free( data );
            free( ip_netif );
            return ENOTSUP;
        }
        if( settings[ 8 ].value ){
            ip_netif->arp = get_running_module( & ip_globals.modules, settings[ 8 ].value );
            if( ! ip_netif->arp ){
                printf( "\nFailed to start the arp %s", settings[ 8 ].value );
                free( settings );
                free( data );
                free( ip_netif );
                return EINVAL;
            }
        }else{
            ip_netif->arp = NULL;
        }
        free( settings );
        free( data );
    }
    // TODO mapping
    ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
    if( ip_netif->phone < 0 ){
        printf( "\nFailed to contact the nil service %d", service );
        free( ip_netif );
        return ip_netif->phone;
    }
    if( ip_netif->arp ){
        message = async_send_3( ip_netif->arp->phone, NET_ARP_DEVICE, ip_netif->device_id, SERVICE_IP, service, & answer );
        configuration[ 0 ].value = ( char * ) & ip_netif->address;
        configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
        if( ERROR_OCCURRED( measured_strings_send( ip_netif->arp->phone, & configuration[ 0 ], 1 ))){
            free( ip_netif );
            return ERROR_CODE;
        }
        async_wait_for( message, & result );
        if( ERROR_OCCURRED( result )){
            free( ip_netif );
            return ERROR_CODE;
        }
    }
    index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
    if( index < 0 ){
        free( ip_netif );
        return index;
    }
    if( ip_netif->arp ) ++ ip_netif->arp->usage;
    // print the settings
    printf( "\nNew device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
    printf( "\n\tconfiguration\t= %s", 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( "\n\taddress\t= %s", data );
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
        printf( "\n\tnetmask\t= %s", data );
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->gateway, data, INET_ADDRSTRLEN );
        printf( "\n\tgateway\t= %s", data );
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast, data, INET_ADDRSTRLEN );
        printf( "\n\tbroadcast\t= %s", data );
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN );
        printf( "\n\tdns1\t= %s", data );
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
        printf( "\n\tdns2\t= %s", data );
        free( data );
    }
    return EOK;
}

void ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall ){
    ERROR_DECLARE;

    ipc_callid_t    callid;
    ipc_call_t  call;
//  ipc_call_t  answer;
//  int     count;
    int     result;
    packet_t    packet;

    /*
     * Accept the connection
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
     */
    ipc_answer_0( iid, EOK );

    while( true ){
/*      // refresh data
        count = 0;
        IPC_SET_RETVAL( answer, 0 );
        // just to be precize
        IPC_SET_RETVAL( answer, 0 );
        IPC_SET_ARG1( answer, 0 );
        IPC_SET_ARG2( answer, 0 );
        IPC_SET_ARG3( answer, 0 );
        IPC_SET_ARG4( answer, 0 );
        IPC_SET_ARG5( answer, 0 );
*/
        callid = async_get_call( & call );
        switch( IPC_GET_METHOD( call )){
            case NET_IL_DEVICE_STATE:
            case NET_NIL_DEVICE_STATE:
                result = ip_state_message( IPC_GET_DEVICE( & call ), IPC_GET_STATE( & call ));
                ipc_answer_0( callid, result );
            // TODO packer received
            case NET_IL_RECEIVED:
            case NET_NIL_RECEIVED:
                if( ! ERROR_OCCURRED( result = packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( & call )))){
                    //result = ip_receive_message( IPC_GET_DEVICE( call ), packet );
                }
                ipc_answer_0( callid, result );
        }
    }
}

int ip_state_message( device_id_t device_id, device_state_t state ){
    ERROR_DECLARE;

    ip_netif_ref    netif;

    aid_t           message;
    ipc_call_t      answer;
    measured_string_t address;
    measured_string_ref translation;
    char *          data;
    ipcarg_t        result;

    netif = ip_netifs_find( & ip_globals.netifs, device_id );
    if( ! netif ) return ENOENT;
    // TODO state
    printf( "\nip - device %d changed state to %d\n", device_id, state );
    if( netif->arp ){
        message = async_send_2( netif->arp->phone, NET_ARP_TRANSLATE, netif->device_id, SERVICE_IP, & answer );
        address.value = ( char * ) & netif->gateway;
        address.length = CONVERT_SIZE( in_addr_t, char, 1 );
        if( ERROR_OCCURRED( measured_strings_send( netif->arp->phone, & address, 1 ))
        || ERROR_OCCURRED( measured_strings_return( netif->arp->phone, & translation, & data, 1 ))){
            async_wait_for( message, & result );
            return result;
        }
        async_wait_for( message, & result );
        if( ! ERROR_OCCURRED( result )){
            printf( "\n\tgateway translated to\t= %X:%X:%X:%X:%X:%X", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] );
        }
        free( translation );
        free( data );
        return result;
    }
    return EOK;
}

int ip_register_message( 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 ){
        free( proto );
        return index;
    }
    printf( "\nNew protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d", proto->protocol, proto->phone );
    return EOK;
}

int ip_send_message( device_id_t device_id, packet_t packet ){
    // TODO send packet
    printf( "Packet to send via %d: %s", device_id, packet_get_data( packet ));
    pq_release( ip_globals.networking_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_IP_ECHO:
            * answer_count = 5;
            return ip_echo_message( IPC_GET_ARG1( * call ), IPC_GET_ARG2( * call ), IPC_GET_ARG3( * call ), IPC_GET_ARG4( * call ), IPC_GET_ARG5( * call ), & IPC_GET_ARG1( * answer ), & IPC_GET_ARG2( * answer ), & IPC_GET_ARG3( * answer ), & IPC_GET_ARG4( * answer ), & IPC_GET_ARG5( * answer ) );
        case NET_IL_DEVICE:
            return ip_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
        case IPC_M_CONNECT_TO_ME:
            return ip_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
        case NET_IP_SEND:
            ERROR_PROPAGATE( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
            return ip_send_message( IPC_GET_DEVICE( call ), packet );
    }
    return ENOTSUP;
}

/** @}
 */