Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
3466 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3466 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
 
3912 mejdrech 29
/** @addtogroup ip
30
 *  @{
3466 mejdrech 31
 */
32
 
33
/** @file
34
 */
3666 mejdrech 35
 
3846 mejdrech 36
#include <as.h>
3466 mejdrech 37
#include <async.h>
38
#include <errno.h>
39
#include <stdio.h>
3846 mejdrech 40
 
3466 mejdrech 41
#include <ipc/ipc.h>
42
#include <ipc/services.h>
43
 
3886 mejdrech 44
#include "../../err.h"
45
#include "../../messages.h"
46
#include "../../modules.h"
3466 mejdrech 47
 
3886 mejdrech 48
#include "../../include/sockaddr.h"
49
#include "../../include/socket.h"
50
#include "../../netif/device.h"
51
#include "../../structures/measured_strings.h"
3901 mejdrech 52
#include "../../structures/packet/packet_client.h"
3846 mejdrech 53
 
3466 mejdrech 54
#include "ip.h"
3846 mejdrech 55
#include "ip_messages.h"
56
#include "ip_module.h"
3466 mejdrech 57
 
3846 mejdrech 58
#define DEFAULT_IPV     4
3685 mejdrech 59
 
3846 mejdrech 60
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
3901 mejdrech 61
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG1( * call )
3846 mejdrech 62
#define IPC_GET_PROTO( call )       ( int ) IPC_GET_ARG1( * call )
63
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG2( * call )
64
#define IPC_GET_STATE( call )       ( device_state_t ) IPC_GET_ARG2( * call )
65
#define IPC_GET_PHONE( call )       ( int ) IPC_GET_ARG5( * call )
66
 
3666 mejdrech 67
ip_globals_t    ip_globals;
3466 mejdrech 68
 
3666 mejdrech 69
DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t )
70
 
3846 mejdrech 71
INT_MAP_IMPLEMENT( ip_protos, ip_proto_t )
3685 mejdrech 72
 
3846 mejdrech 73
int ip_register_message( int protocol, int phone );
74
int ip_state_message( device_id_t device_id, device_state_t state );
75
void    ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall );
76
 
3466 mejdrech 77
/** Initializes the module.
78
 */
79
int ip_initialize( void ){
3666 mejdrech 80
    ip_netifs_initialize( & ip_globals.netifs );
3846 mejdrech 81
    ip_protos_initialize( & ip_globals.protos );
3466 mejdrech 82
    return EOK;
83
}
84
 
3846 mejdrech 85
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 ){
86
    if( answer1 ) * answer1 = arg1;
87
    if( answer2 ) * answer2 = arg2;
88
    if( answer3 ) * answer3 = arg3;
89
    if( answer4 ) * answer4 = arg4;
90
    if( answer5 ) * answer5 = arg5;
3466 mejdrech 91
    return EOK;
92
}
93
 
3846 mejdrech 94
int ip_device_message( device_id_t device_id, services_t service ){
3666 mejdrech 95
    ERROR_DECLARE;
96
 
3685 mejdrech 97
    ip_netif_ref        ip_netif;
98
    aid_t           message;
99
    ipc_call_t      answer;
100
    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 }};
101
    int         count = 9;
102
    measured_string_ref settings;
103
    char *          data;
3666 mejdrech 104
 
3846 mejdrech 105
    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
106
    if( ! ip_netif ) return ENOMEM;
107
    ip_netif->device_id = device_id;
108
    // get configuration
109
    // TODO mapping
110
    message = async_send_2( ip_globals.networking_phone, NET_NET_GET_DEVICE_CONF, ip_netif->device_id, count, & answer );
111
    // send names and get settings
3912 mejdrech 112
    if( ERROR_OCCURRED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
113
    || ERROR_OCCURRED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
3846 mejdrech 114
        async_wait_for( message, NULL );
115
        return ERROR_CODE;
116
    }
117
    if( settings ){
118
        if( settings[ 0 ].value ){
119
            ip_netif->ipv = strtol( settings[ 0 ].value, NULL, 0 );
120
        }else{
121
            ip_netif->ipv = DEFAULT_IPV;
122
        }
123
        ip_netif->dhcp = ! strcmp( settings[ 1 ].value, "DHCP" );
124
        if( ip_netif->dhcp ){
125
            // TODO dhcp
126
            free( ip_netif );
127
            return ENOTSUP;
128
        }else if( ip_netif->ipv == 4 ){
3912 mejdrech 129
            if( ERROR_OCCURRED( inet_pton( AF_INET, settings[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
130
            || ERROR_OCCURRED( inet_pton( AF_INET, settings[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
3846 mejdrech 131
            || ( inet_pton( AF_INET, settings[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
132
            || ( inet_pton( AF_INET, settings[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
133
            || ( inet_pton( AF_INET, settings[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
134
            || ( inet_pton( AF_INET, settings[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
135
                free( ip_netif );
136
                return EINVAL;
137
            }
138
        }else{
139
            // TODO ipv6
140
            free( ip_netif );
141
            return ENOTSUP;
142
        }
143
        // TODO ARP module
144
    }
145
    // TODO arp module
146
    free( settings );
147
    free( data );
148
    // end request
149
    // TODO mapping
150
    async_wait_for( message, NULL );
151
    // print the settings
152
    printf( "\n -IPV =\t%d", ip_netif->ipv );
153
    printf( "\n -configuration =\t%s", ip_netif->dhcp ? "dhcp" : "static" );
154
    // TODO ipv6
155
    data = malloc( INET_ADDRSTRLEN );
156
    if( data ){
157
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
158
        printf( "\n -address =\t%s", data );
159
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
160
        printf( "\n -netmask =\t%s", data );
161
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->gateway, data, INET_ADDRSTRLEN );
162
        printf( "\n -gateway =\t%s", data );
163
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast, data, INET_ADDRSTRLEN );
164
        printf( "\n -broadcast =\t%s", data );
165
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN );
166
        printf( "\n -dns1 =\t%s", data );
167
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
168
        printf( "\n -dns2 =\t%s", data );
169
        free( data );
170
    }
171
    // TODO mapping
172
    ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
3912 mejdrech 173
    if( ERROR_OCCURRED( ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif ))){
3846 mejdrech 174
        free( ip_netif );
175
        return ERROR_CODE;
176
    }
177
    return EOK;
178
}
179
 
180
void ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall ){
181
    ERROR_DECLARE;
182
 
183
    ipc_callid_t    callid;
184
    ipc_call_t  call;
185
//  ipc_call_t  answer;
186
//  int     count;
187
    int     result;
188
    packet_t    packet;
189
 
190
    /*
191
     * Accept the connection
192
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
193
     */
194
    ipc_answer_0( iid, EOK );
195
 
196
    while( true ){
197
/*      // refresh data
198
        count = 0;
199
        IPC_SET_RETVAL( answer, 0 );
200
        // just to be precize
201
        IPC_SET_RETVAL( answer, 0 );
202
        IPC_SET_ARG1( answer, 0 );
203
        IPC_SET_ARG2( answer, 0 );
204
        IPC_SET_ARG3( answer, 0 );
205
        IPC_SET_ARG4( answer, 0 );
206
        IPC_SET_ARG5( answer, 0 );
207
*/
208
        callid = async_get_call( & call );
209
        switch( IPC_GET_METHOD( call )){
210
            case NET_IL_DEVICE_STATE:
211
            case NET_NIL_DEVICE_STATE:
212
                result = ip_state_message( IPC_GET_DEVICE( & call ), IPC_GET_STATE( & call ));
213
                ipc_answer_0( callid, result );
214
            // TODO packer received
215
            case NET_IL_RECEIVED:
216
            case NET_NIL_RECEIVED:
3912 mejdrech 217
                if( ! ERROR_OCCURRED( result = packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( & call )))){
3901 mejdrech 218
                    //result = ip_receive_message( IPC_GET_DEVICE( call ), packet );
3846 mejdrech 219
                }
220
                ipc_answer_0( callid, result );
221
        }
222
    }
223
}
224
 
225
int ip_state_message( device_id_t device_id, device_state_t state ){
226
    // TODO state
227
    printf( "\nip - device %d changed state to %d\n", device_id, state );
228
    return EOK;
229
}
230
 
231
int ip_register_message( int protocol, int phone ){
232
    ERROR_DECLARE;
233
 
234
    ip_proto_ref    proto;
235
 
236
    proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
237
    if( ! proto ) return ENOMEM;
238
    proto->protocol = protocol;
239
    proto->phone = phone;
3912 mejdrech 240
    if( ERROR_OCCURRED( ip_protos_add( & ip_globals.protos, proto->protocol, proto ))){
3846 mejdrech 241
        free( proto );
242
        return ERROR_CODE;
243
    }
244
    return EOK;
245
}
246
 
247
int ip_send_message( device_id_t device_id, packet_t packet ){
248
    // TODO send packet
249
    printf( "Packet to send via %d: %s", device_id, packet_get_data( packet ));
3901 mejdrech 250
    packet_release( ip_globals.networking_phone, packet_get_id( packet ));
3846 mejdrech 251
    return EOK;
252
}
253
 
254
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
255
    ERROR_DECLARE;
256
 
257
    packet_t    packet;
258
 
259
    * answer_count = 0;
260
    switch( IPC_GET_METHOD( * call )){
3466 mejdrech 261
        case IPC_M_PHONE_HUNGUP:
262
            return EOK;
263
        case NET_IP_ECHO:
3846 mejdrech 264
            * answer_count = 5;
265
            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 ) );
3666 mejdrech 266
        case NET_IL_DEVICE:
3846 mejdrech 267
            return ip_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
268
        case IPC_M_CONNECT_TO_ME:
269
            return ip_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
270
        case NET_IP_SEND:
3912 mejdrech 271
            if( ERROR_OCCURRED( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )))){
3901 mejdrech 272
                printf( "\nIP send E %d", ERROR_CODE );
273
                return ERROR_CODE;
274
            }
3846 mejdrech 275
            return ip_send_message( IPC_GET_DEVICE( call ), packet );
3466 mejdrech 276
    }
277
    return ENOTSUP;
278
}
279
 
280
/** @}
281
 */