Subversion Repositories HelenOS

Rev

Rev 3666 | Rev 3846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3466 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup net
30
 * @{
31
 */
32
 
33
/** @file
34
 */
3666 mejdrech 35
 
3466 mejdrech 36
#include <async.h>
37
#include <errno.h>
38
#include <stdio.h>
39
#include <ipc/ipc.h>
40
#include <ipc/services.h>
41
//#include <sys/mman.h>
42
 
3666 mejdrech 43
#include "../err.h"
3685 mejdrech 44
#include "../measured_strings.h"
3466 mejdrech 45
#include "../messages.h"
46
#include "../modules.h"
3666 mejdrech 47
#include "../netif/netif_device_id_type.h"
3466 mejdrech 48
 
49
#include "ip.h"
50
 
3685 mejdrech 51
#define DEFAULT_IPV 4
52
 
3666 mejdrech 53
ip_globals_t    ip_globals;
3466 mejdrech 54
 
3666 mejdrech 55
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
56
 
3685 mejdrech 57
int parse_address( char * value, address_ref address );
58
 
3466 mejdrech 59
/** Initializes the module.
60
 */
61
int ip_initialize( void ){
3666 mejdrech 62
    ip_netifs_initialize( & ip_globals.netifs );
3466 mejdrech 63
    return EOK;
64
}
65
 
66
int ip_call( ipc_callid_t callid ){
67
    return EOK;
68
}
69
 
3685 mejdrech 70
int parse_address( char * value, address_ref address ){
71
    char *  next;
72
    int index;
73
 
74
    if( ! value ){
75
        ( * address )[ 0 ] = ( * address )[ 1 ] = ( * address )[ 2 ] = ( * address )[ 3 ] = 0;
76
        return ENOENT;
77
    }
78
    next = value;
79
    for( index = 0; index < 4; ++ index ){
80
        if(( ! next ) || ( ! * next )) return EINVAL;
81
        if( index ) ++ next;
82
        ( * address )[ index ] = strtoul( next, & next, 0 );
83
    }
84
    return EOK;
85
}
86
 
3666 mejdrech 87
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 ){
88
    ERROR_DECLARE;
89
 
3685 mejdrech 90
    ip_netif_ref        ip_netif;
91
    aid_t           message;
92
    ipc_call_t      answer;
93
    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 }};
94
    int         count = 9;
95
    measured_string_ref settings;
96
    char *          data;
3666 mejdrech 97
 
3466 mejdrech 98
    switch( method ){
99
        case IPC_M_PHONE_HUNGUP:
100
            return EOK;
101
        case NET_IP_ECHO:
102
            if( result1 ) * result1 = arg1;
103
            if( result2 ) * result2 = arg2;
104
            if( result3 ) * result3 = arg3;
105
            return EOK;
3666 mejdrech 106
        case NET_IL_DEVICE:
107
            ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
108
            if( ! ip_netif ) return ENOMEM;
109
            ip_netif->device_id = arg1;
3685 mejdrech 110
            // get configuration
111
            message = async_send_2( ip_globals.networking_phone, NET_NETWORKING_GET_DEVICE_CONFIGURATION, ip_netif->device_id, count, & answer );
112
            // send names and get settings
113
            if( ERROR_OCCURED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
114
            || ERROR_OCCURED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
115
                async_wait_for( message, NULL );
3666 mejdrech 116
                return ERROR_CODE;
117
            }
3685 mejdrech 118
            if( settings ){
119
                if( settings[ 0 ].value ){
120
                    ip_netif->ipv = strtol( settings[ 0 ].value, NULL, 0 );
121
                }else{
122
                    ip_netif->ipv = DEFAULT_IPV;
123
                }
124
                ip_netif->dhcp = ! strcmp( settings[ 1 ].value, "DHCP" );
125
                if( ip_netif->dhcp ){
126
                    // TODO dhcp
127
                    free( ip_netif );
128
                    return ENOTSUP;
129
                }else if( ip_netif->ipv == 4 ){
130
                    if( ERROR_OCCURED( parse_address( settings[ 2 ].value, & ip_netif->address ))
131
                    || ERROR_OCCURED( parse_address( settings[ 3 ].value, & ip_netif->netmask ))
132
                    || ( parse_address( settings[ 4 ].value, & ip_netif->gateway ) == EINVAL )
133
                    || ( parse_address( settings[ 5 ].value, & ip_netif->broadcast ) == EINVAL )
134
                    || ( parse_address( settings[ 6 ].value, & ip_netif->dns1 ) == EINVAL )
135
                    || ( parse_address( settings[ 7 ].value, & ip_netif->dns2 ) == EINVAL )){
136
                        free( ip_netif );
137
                        return EINVAL;
138
                    }
139
                }else{
140
                    // TODO ipv6
141
                    free( ip_netif );
142
                    return ENOTSUP;
143
                }
144
                // TODO ARP module
145
            }
146
            // print the settings
147
            printf( "\n -IPV=%d", ip_netif->ipv );
148
            printf( "\n -configuration=%s", ip_netif->dhcp ? "dhcp" : "static" );
149
            // TODO ipv6
150
            printf( "\n -address=%d.%d.%d.%d", ip_netif->address[ 0 ], ip_netif->address[ 1 ], ip_netif->address[ 2 ], ip_netif->address[ 3 ] );
151
            printf( "\n -netmask=%d.%d.%d.%d", ip_netif->netmask[ 0 ], ip_netif->netmask[ 1 ], ip_netif->netmask[ 2 ], ip_netif->netmask[ 3 ] );
152
            printf( "\n -gateway=%d.%d.%d.%d", ip_netif->gateway[ 0 ], ip_netif->gateway[ 1 ], ip_netif->gateway[ 2 ], ip_netif->gateway[ 3 ] );
153
            printf( "\n -broadcast=%d.%d.%d.%d", ip_netif->broadcast[ 0 ], ip_netif->broadcast[ 1 ], ip_netif->broadcast[ 2 ], ip_netif->broadcast[ 3 ] );
154
            printf( "\n -dns1=%d.%d.%d.%d", ip_netif->dns1[ 0 ], ip_netif->dns1[ 1 ], ip_netif->dns1[ 2 ], ip_netif->dns1[ 3 ] );
155
            printf( "\n -dns2=%d.%d.%d.%d", ip_netif->dns2[ 0 ], ip_netif->dns2[ 1 ], ip_netif->dns2[ 2 ], ip_netif->dns2[ 3 ] );
156
            // TODO arp module
157
            free( settings );
158
            free( data );
159
            // end request
160
            async_wait_for( message, NULL );
161
            ip_netif->phone = connect_to_service( arg2 );
162
            if( ERROR_OCCURED( async_req_2_0( ip_netif->phone, NET_LL_REGISTER, arg1, SERVICE_IP ))
163
            || ERROR_OCCURED( ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif ))){
3666 mejdrech 164
                free( ip_netif );
165
                return ERROR_CODE;
166
            }
167
            return EOK;
3685 mejdrech 168
        case NET_IL_DEVICE_STATE_CHANGED:
169
        case NET_LL_DEVICE_STATE_CHANGED:
170
            // arg1 device id
171
            // arg2 state
172
            // TODO state
173
            printf( "\nip - device %d changed state to %d\n", arg1, arg2 );
3666 mejdrech 174
        case NET_IP_TCP_REGISTER:
3685 mejdrech 175
            ip_globals.tcp_phone = connect_to_service( arg1 );
3666 mejdrech 176
            return EOK;
3466 mejdrech 177
    }
178
    return ENOTSUP;
179
}
180
 
181
/** @}
182
 */