Subversion Repositories HelenOS

Rev

Rev 3886 | Rev 3912 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3886 Rev 3901
1
/*
1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup arp
29
/** @addtogroup arp
30
 *  @{
30
 *  @{
31
 */
31
 */
32
 
32
 
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <as.h>
36
#include <as.h>
37
#include <async.h>
37
#include <async.h>
38
#include <malloc.h>
38
#include <malloc.h>
39
#include <stdio.h>
39
#include <stdio.h>
40
#include <string.h>
40
#include <string.h>
41
 
41
 
42
#include <ipc/ipc.h>
42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
43
#include <ipc/services.h>
44
 
44
 
45
#include "../../err.h"
45
#include "../../err.h"
46
#include "../../messages.h"
46
#include "../../messages.h"
47
#include "../../modules.h"
47
#include "../../modules.h"
48
 
48
 
49
#include "../../include/protocol_map.h"
49
#include "../../include/protocol_map.h"
50
#include "../../netif/device.h"
50
#include "../../netif/device.h"
51
 
51
 
52
#include "../../structures/measured_strings.h"
52
#include "../../structures/measured_strings.h"
53
#include "../../structures/packet/packet.h"
53
#include "../../structures/packet/packet.h"
-
 
54
#include "../../structures/packet/packet_client.h"
54
 
55
 
55
#include "arp.h"
56
#include "arp.h"
56
#include "arp_header.h"
57
#include "arp_header.h"
57
#include "arp_oc.h"
58
#include "arp_oc.h"
58
//#include "arp_messages.h"
59
//#include "arp_messages.h"
59
#include "arp_module.h"
60
#include "arp_module.h"
60
 
61
 
61
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
62
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
-
 
63
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG2( * call )
62
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
64
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
63
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
65
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
64
 
66
 
65
arp_globals_t   arp_globals;
67
arp_globals_t   arp_globals;
66
 
68
 
67
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
69
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
68
 
70
 
69
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
71
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
70
 
72
 
71
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
73
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
72
 
74
 
73
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
75
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
74
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
76
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
75
measured_string_ref arp_translate_message( device_id_t device, services_t protocol, measured_string_ref target );
77
measured_string_ref arp_translate_message( device_id_t device, services_t protocol, measured_string_ref target );
76
int arp_receive_message( device_id_t device_id, packet_t packet );
78
int arp_receive_message( device_id_t device_id, packet_t packet );
77
int arp_clear_device_message( device_id_t device_id );
79
int arp_clear_device_message( device_id_t device_id );
78
void clear_device( arp_device_ref device );
80
void clear_device( arp_device_ref device );
79
int arp_clean_cache_message( void );
81
int arp_clean_cache_message( void );
80
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
82
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
81
 
83
 
82
/** Initializes the ARP module.
84
/** Initializes the ARP module.
83
 */
85
 */
84
int arp_initialize( void ){
86
int arp_initialize( void ){
85
    arp_cache_initialize( & arp_globals.cache );
87
    arp_cache_initialize( & arp_globals.cache );
86
    return EOK;
88
    return EOK;
87
}
89
}
88
 
90
 
89
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
91
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
90
    ERROR_DECLARE;
92
    ERROR_DECLARE;
91
 
93
 
92
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
94
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
93
    if( !( * proto )) return ENOMEM;
95
    if( !( * proto )) return ENOMEM;
94
    ( ** proto ).service = service;
96
    ( ** proto ).service = service;
95
    ( ** proto ).addr = address;
97
    ( ** proto ).addr = address;
96
    ( ** proto ).addr_data = address->value;
98
    ( ** proto ).addr_data = address->value;
97
    if( ERROR_OCCURED( arp_addr_initialize( &( ** proto).addresses ))){
99
    if( ERROR_OCCURED( arp_addr_initialize( &( ** proto).addresses ))){
98
        free( * proto );
100
        free( * proto );
99
        return ERROR_CODE;
101
        return ERROR_CODE;
100
    }
102
    }
101
    return EOK;
103
    return EOK;
102
}
104
}
103
 
105
 
104
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
106
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
105
    ERROR_DECLARE;
107
    ERROR_DECLARE;
106
 
108
 
107
    arp_device_ref  device;
109
    arp_device_ref  device;
108
    aid_t           message;
110
    aid_t           message;
109
    ipc_call_t      answer;
111
    ipc_call_t      answer;
110
    ipcarg_t        result;
112
    ipcarg_t        result;
111
    arp_proto_ref   proto;
113
    arp_proto_ref   proto;
112
 
114
 
113
    // an existing device?
115
    // an existing device?
114
    device = arp_cache_find( & arp_globals.cache, device_id );
116
    device = arp_cache_find( & arp_globals.cache, device_id );
115
    if( device ){
117
    if( device ){
116
        if( device->service != service ) return EEXIST;
118
        if( device->service != service ) return EEXIST;
117
        proto = arp_protos_find( & device->protos, protocol );
119
        proto = arp_protos_find( & device->protos, protocol );
118
        if( proto ){
120
        if( proto ){
119
            free( proto->addr );
121
            free( proto->addr );
120
            free( proto->addr_data );
122
            free( proto->addr_data );
121
            proto->addr = address;
123
            proto->addr = address;
122
            proto->addr_data = address->value;
124
            proto->addr_data = address->value;
123
        }else{
125
        }else{
124
            ERROR_PROPAGATE( arp_proto_create( & proto, protocol, address ));
126
            ERROR_PROPAGATE( arp_proto_create( & proto, protocol, address ));
125
            if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
127
            if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
126
                free( proto );
128
                free( proto );
127
                return ERROR_CODE;
129
                return ERROR_CODE;
128
            }
130
            }
129
        }
131
        }
130
        return EOK;
132
        return EOK;
131
    }else{
133
    }else{
132
        // create a new device
134
        // create a new device
133
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
135
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
134
        if( ! device ) return ENOMEM;
136
        if( ! device ) return ENOMEM;
135
        device->device_id = device_id;
137
        device->device_id = device_id;
136
        if( ERROR_OCCURED( arp_protos_initialize( & device->protos ))
138
        if( ERROR_OCCURED( arp_protos_initialize( & device->protos ))
137
        || ERROR_OCCURED( arp_proto_create( & proto, protocol, address ))){
139
        || ERROR_OCCURED( arp_proto_create( & proto, protocol, address ))){
138
            free( device );
140
            free( device );
139
            return ERROR_CODE;
141
            return ERROR_CODE;
140
        }
142
        }
141
        if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
143
        if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
142
            arp_protos_destroy( & device->protos );
144
            arp_protos_destroy( & device->protos );
143
            free( device );
145
            free( device );
144
            return ERROR_CODE;
146
            return ERROR_CODE;
145
        }
147
        }
146
        device->service = service;
148
        device->service = service;
147
        // bind the new one
149
        // bind the new one
148
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
150
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
149
        // get packet dimensions
151
        // get packet dimensions
150
        if( ERROR_OCCURED( async_req_1_3( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->prefix, & device->content, & device->sufix ))){
152
        if( ERROR_OCCURED( async_req_1_4( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->addr_len, & device->prefix, & device->content, & device->sufix ))){
151
            arp_protos_destroy( & device->protos );
153
            arp_protos_destroy( & device->protos );
152
            free( device );
154
            free( device );
153
            return ERROR_CODE;
155
            return ERROR_CODE;
154
        }
156
        }
155
        // get hardware address
157
        // get hardware address
156
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
158
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
157
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
159
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
158
            arp_protos_destroy( & device->protos );
160
            arp_protos_destroy( & device->protos );
159
            free( device );
161
            free( device );
160
            async_wait_for( message, NULL );
162
            async_wait_for( message, NULL );
161
            return ERROR_CODE;
163
            return ERROR_CODE;
162
        }
164
        }
163
        async_wait_for( message, & result );
165
        async_wait_for( message, & result );
164
        if( ERROR_OCCURED( result )){
166
        if( ERROR_OCCURED( result )){
165
            free( device->addr );
167
            free( device->addr );
166
            free( device->addr_data );
168
            free( device->addr_data );
167
            arp_protos_destroy( & device->protos );
169
            arp_protos_destroy( & device->protos );
168
            free( device );
170
            free( device );
169
            return ERROR_CODE;
171
            return ERROR_CODE;
170
        }
172
        }
171
        // get broadcast address
173
        // get broadcast address
172
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
174
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
173
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
175
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
174
            free( device->addr );
176
            free( device->addr );
175
            free( device->addr_data );
177
            free( device->addr_data );
176
            arp_protos_destroy( & device->protos );
178
            arp_protos_destroy( & device->protos );
177
            free( device );
179
            free( device );
178
            async_wait_for( message, NULL );
180
            async_wait_for( message, NULL );
179
            return ERROR_CODE;
181
            return ERROR_CODE;
180
        }
182
        }
181
        async_wait_for( message, & result );
183
        async_wait_for( message, & result );
182
        // add to the cache
184
        // add to the cache
183
        if( ERROR_OCCURED( result )
185
        if( ERROR_OCCURED( result )
184
        || ERROR_OCCURED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
186
        || ERROR_OCCURED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
185
            free( device->addr );
187
            free( device->addr );
186
            free( device->addr_data );
188
            free( device->addr_data );
187
            free( device->broadcast_addr );
189
            free( device->broadcast_addr );
188
            free( device->broadcast_data );
190
            free( device->broadcast_data );
189
            arp_protos_destroy( & device->protos );
191
            arp_protos_destroy( & device->protos );
190
            free( device );
192
            free( device );
191
            return ERROR_CODE;
193
            return ERROR_CODE;
192
        }
194
        }
193
    }
195
    }
194
    return EOK;
196
    return EOK;
195
}
197
}
196
 
198
 
197
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
199
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
198
//  ERROR_DECLARE;
200
//  ERROR_DECLARE;
199
 
201
 
200
    arp_device_ref      device;
202
    arp_device_ref      device;
201
    arp_proto_ref       proto;
203
    arp_proto_ref       proto;
202
    measured_string_ref addr;
204
    measured_string_ref addr;
203
    size_t              length;
205
    size_t              length;
204
    packet_t            packet;
206
    packet_t            packet;
205
    arp_header_ref      header;
207
    arp_header_ref      header;
206
 
208
 
207
    if( ! target ) return NULL;
209
    if( ! target ) return NULL;
208
    device = arp_cache_find( & arp_globals.cache, device_id );
210
    device = arp_cache_find( & arp_globals.cache, device_id );
209
    if( ! device ) return NULL;
211
    if( ! device ) return NULL;
210
    proto = arp_protos_find( & device->protos, protocol );
212
    proto = arp_protos_find( & device->protos, protocol );
211
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
213
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
212
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
214
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
213
    if( addr ) return addr;
215
    if( addr ) return addr;
214
    // ARP packet content size = header + ( address + translation ) * 2
216
    // ARP packet content size = header + ( address + translation ) * 2
215
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
217
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
216
    if( length > device->content ){
218
    if( length > device->content ){
217
        return NULL;
219
        return NULL;
218
    }
220
    }
219
    packet = packet_create( device->prefix, length, device->sufix );
221
    packet = packet_get_5( arp_globals.networking_phone, SERVICE_ARP, device->addr_len, device->prefix, length, device->sufix );
220
    if( ! packet ) return NULL;
222
    if( ! packet ) return NULL;
221
    header = ( arp_header_ref ) packet_append( packet, length );
223
    header = ( arp_header_ref ) packet_append( packet, length );
222
    header->hardware = device->hardware;
224
    header->hardware = device->hardware;
223
    header->hardware_length = device->addr->length;
225
    header->hardware_length = device->addr->length;
224
    header->protocol = protocol_map( device->service, protocol );
226
    header->protocol = protocol_map( device->service, protocol );
225
    header->protocol_length = proto->addr->length;
227
    header->protocol_length = proto->addr->length;
226
    header->operation = ARPOP_REQUEST;
228
    header->operation = ARPOP_REQUEST;
227
    length = sizeof( arp_header_t );
229
    length = sizeof( arp_header_t );
228
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
230
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
229
    length += device->addr->length;
231
    length += device->addr->length;
230
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
232
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
231
    length += proto->addr->length;
233
    length += proto->addr->length;
232
    memset((( uint8_t * ) header ) + length, 0, device->addr->length );
234
    memset((( uint8_t * ) header ) + length, 0, device->addr->length );
233
    length += device->addr->length;
235
    length += device->addr->length;
234
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
236
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
235
    // TODO send to the device->broadcast_addr as arp protocol
237
    // TODO send to the device->broadcast_addr as arp protocol
236
    packet_send( packet, device->phone );
238
    async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
237
    return NULL;
239
    return NULL;
238
}
240
}
239
 
241
 
240
int arp_receive_message( device_id_t device_id, packet_t packet ){
242
int arp_receive_message( device_id_t device_id, packet_t packet ){
241
    ERROR_DECLARE;
243
    ERROR_DECLARE;
242
 
244
 
243
    size_t              length;
245
    size_t              length;
244
    arp_header_ref      header;
246
    arp_header_ref      header;
245
    arp_device_ref      device;
247
    arp_device_ref      device;
246
    arp_proto_ref       proto;
248
    arp_proto_ref       proto;
247
//  arp_addr_ref        addr;
249
//  arp_addr_ref        addr;
248
    measured_string_ref hw_source;
250
    measured_string_ref hw_source;
249
/*  measured_string_t   proto_target;
251
/*  measured_string_t   proto_target;
250
    aid_t               message;
252
    aid_t               message;
251
    ipcarg_t            result;
253
    ipcarg_t            result;
252
    int                 index;
254
    int                 index;
253
    ipc_call_t          answer;
255
    ipc_call_t          answer;
254
*/  int8_t *            src_hw;
256
*/  int8_t *            src_hw;
255
    int8_t *            src_proto;
257
    int8_t *            src_proto;
256
    int8_t *            des_hw;
258
    int8_t *            des_hw;
257
    int8_t *            des_proto;
259
    int8_t *            des_proto;
258
 
260
 
259
    length = packet_get_data_length( packet );
261
    length = packet_get_data_length( packet );
260
    if( length <= sizeof( arp_header_t )) return EINVAL;
262
    if( length <= sizeof( arp_header_t )) return EINVAL;
261
    device = arp_cache_find( & arp_globals.cache, device_id );
263
    device = arp_cache_find( & arp_globals.cache, device_id );
262
    if( ! device ) return ENOENT;
264
    if( ! device ) return ENOENT;
263
    header = ( arp_header_ref ) packet_get_data( packet );
265
    header = ( arp_header_ref ) packet_get_data( packet );
264
    if( header->hardware != device->hardware ) return EINVAL;
266
    if( header->hardware != device->hardware ) return EINVAL;
265
    if( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 ) return EINVAL;
267
    if( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 ) return EINVAL;
266
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
268
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
267
    if( ! proto ) return ENOENT;
269
    if( ! proto ) return ENOENT;
268
    src_hw = (( int8_t * ) header ) + sizeof( arp_header_t );
270
    src_hw = (( int8_t * ) header ) + sizeof( arp_header_t );
269
    src_proto = src_hw + header->hardware_length;
271
    src_proto = src_hw + header->hardware_length;
270
    des_hw = src_proto + header->protocol_length;
272
    des_hw = src_proto + header->protocol_length;
271
    des_proto = des_hw + header->hardware_length;
273
    des_proto = des_hw + header->hardware_length;
272
    hw_source = arp_addr_find( & proto->addresses, src_proto, header->protocol_length );
274
    hw_source = arp_addr_find( & proto->addresses, src_proto, header->protocol_length );
273
    // exists?
275
    // exists?
274
    if( hw_source ){
276
    if( hw_source ){
275
        if( hw_source->length != header->hardware_length ) return EINVAL;
277
        if( hw_source->length != header->hardware_length ) return EINVAL;
276
        memcpy( hw_source->value, src_hw, header->hardware_length );
278
        memcpy( hw_source->value, src_hw, header->hardware_length );
277
    }
279
    }
278
    // is my protocol address?
280
    // is my protocol address?
279
/*  proto_target.value = des_proto;
281
/*  proto_target.value = des_proto;
280
    proto_target.length = header->protocol_length;
282
    proto_target.length = header->protocol_length;
281
    // TODO send necessary?
283
    // TODO send necessary?
282
    message = async_send_0( proto->phone, NET_IL_MY_ADDR, & answer );
284
    message = async_send_0( proto->phone, NET_IL_MY_ADDR, & answer );
283
    if( ERROR_OCCURED( measured_strings_send( device->phone, & proto_target, 1 ))){
285
    if( ERROR_OCCURED( measured_strings_send( device->phone, & proto_target, 1 ))){
284
        async_wait_for( message, NULL );
286
        async_wait_for( message, NULL );
285
        return ERROR_CODE;
287
        return ERROR_CODE;
286
    }
288
    }
287
    async_wait_for( message, & result );
289
    async_wait_for( message, & result );
288
    if( result == EOK ){
290
    if( result == EOK ){
289
*/  if( proto->addr->length != header->hardware_length ) return EINVAL;
291
*/  if( proto->addr->length != header->hardware_length ) return EINVAL;
290
    if( ! strncmp( proto->addr->value, des_proto, proto->addr->length )){
292
    if( ! strncmp( proto->addr->value, des_proto, proto->addr->length )){
291
        // not already upadted?
293
        // not already upadted?
292
        if( ! hw_source ){
294
        if( ! hw_source ){
293
            hw_source = measured_string_create_bulk( src_hw, header->hardware_length );
295
            hw_source = measured_string_create_bulk( src_hw, header->hardware_length );
294
            if( ! hw_source ) return ENOMEM;
296
            if( ! hw_source ) return ENOMEM;
295
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, src_proto, header->protocol_length, hw_source ));
297
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, src_proto, header->protocol_length, hw_source ));
296
        }
298
        }
297
        if( header->operation == ARPOP_REQUEST ){
299
        if( header->operation == ARPOP_REQUEST ){
298
            header->operation = ARPOP_REPLY;
300
            header->operation = ARPOP_REPLY;
299
/*          for( index = 0; index + header->hardware_length < header->protocol_length; index += header->hardware_length ){
301
/*          for( index = 0; index + header->hardware_length < header->protocol_length; index += header->hardware_length ){
300
                memcpy( src_hw, src_proto + index, header->hardware_length );
302
                memcpy( src_hw, src_proto + index, header->hardware_length );
301
                memcpy( src_proto + index, des_proto + index, header->hardware_length );
303
                memcpy( src_proto + index, des_proto + index, header->hardware_length );
302
                memcpy( des_proto + index, src_hw, header->hardware_length );
304
                memcpy( des_proto + index, src_hw, header->hardware_length );
303
            }
305
            }
304
            memcpy( src_hw, src_proto + index, header->hardware_length - header->protocol_length );
306
            memcpy( src_hw, src_proto + index, header->hardware_length - header->protocol_length );
305
            memcpy( src_proto + index, des_proto + index, header->hardware_length - header->protocol_length );
307
            memcpy( src_proto + index, des_proto + index, header->hardware_length - header->protocol_length );
306
            memcpy( des_proto + index, src_hw, header->hardware_length - header->protocol_length );
308
            memcpy( des_proto + index, src_hw, header->hardware_length - header->protocol_length );
307
            memcpy( src_hw, des_hw, header->hardware_length );
309
            memcpy( src_hw, des_hw, header->hardware_length );
308
            memcpy( des_hw, hw_source->value, hw_source->length );
310
            memcpy( des_hw, hw_source->value, hw_source->length );
309
*/          memcpy( des_proto, src_proto, header->protocol_length );
311
*/          memcpy( des_proto, src_proto, header->protocol_length );
310
            memcpy( src_proto, proto->addr->value, header->protocol_length );
312
            memcpy( src_proto, proto->addr->value, header->protocol_length );
311
            memcpy( src_hw, des_hw, header->hardware_length );
313
            memcpy( src_hw, des_hw, header->hardware_length );
312
            memcpy( des_hw, hw_source->value, hw_source->length );
314
            memcpy( des_hw, hw_source->value, hw_source->length );
313
            // TODO send to the hw_source as arp protocol
315
            // TODO send to the hw_source as arp protocol
-
 
316
            async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet ));
-
 
317
        }else{
314
            packet_send( packet, device->phone );
318
            packet_release( arp_globals.networking_phone, packet_get_id( packet ));
315
        }
319
        }
316
    }
320
    }
317
    return EOK;
321
    return EOK;
318
}
322
}
319
 
323
 
320
int arp_clear_device_message( device_id_t device_id ){
324
int arp_clear_device_message( device_id_t device_id ){
321
    arp_device_ref  device;
325
    arp_device_ref  device;
322
 
326
 
323
    device = arp_cache_find( & arp_globals.cache, device_id );
327
    device = arp_cache_find( & arp_globals.cache, device_id );
324
    if( ! device ) return ENOENT;
328
    if( ! device ) return ENOENT;
325
    clear_device( device );
329
    clear_device( device );
326
    return EOK;
330
    return EOK;
327
}
331
}
328
 
332
 
329
void clear_device( arp_device_ref device ){
333
void clear_device( arp_device_ref device ){
330
    int             count;
334
    int             count;
331
    arp_proto_ref   proto;
335
    arp_proto_ref   proto;
332
 
336
 
333
    count = arp_protos_count( & device->protos );
337
    count = arp_protos_count( & device->protos );
334
    while( count > 0 ){
338
    while( count > 0 ){
335
        proto = arp_protos_get_index( & device->protos, count );
339
        proto = arp_protos_get_index( & device->protos, count );
336
        if( proto->addr ) free( proto->addr );
340
        if( proto->addr ) free( proto->addr );
337
        if( proto->addr_data ) free( proto->addr_data );
341
        if( proto->addr_data ) free( proto->addr_data );
338
        arp_addr_destroy( & proto->addresses );
342
        arp_addr_destroy( & proto->addresses );
339
        -- count;
343
        -- count;
340
    }
344
    }
341
    arp_protos_clear( & device->protos );
345
    arp_protos_clear( & device->protos );
342
}
346
}
343
 
347
 
344
int arp_clean_cache_message( void ){
348
int arp_clean_cache_message( void ){
345
    int             count;
349
    int             count;
346
    arp_device_ref  device;
350
    arp_device_ref  device;
347
 
351
 
348
    count = arp_cache_count( & arp_globals.cache );
352
    count = arp_cache_count( & arp_globals.cache );
349
    while( count > 0 ){
353
    while( count > 0 ){
350
        device = arp_cache_get_index( & arp_globals.cache, count );
354
        device = arp_cache_get_index( & arp_globals.cache, count );
351
        if( device ){
355
        if( device ){
352
            clear_device( device );
356
            clear_device( device );
353
            if( device->broadcast_addr ) free( device->broadcast_addr );
357
            if( device->broadcast_addr ) free( device->broadcast_addr );
354
            if( device->broadcast_data ) free( device->broadcast_data );
358
            if( device->broadcast_data ) free( device->broadcast_data );
355
        }
359
        }
356
    }
360
    }
357
    arp_cache_clear( & arp_globals.cache );
361
    arp_cache_clear( & arp_globals.cache );
358
    return EOK;
362
    return EOK;
359
}
363
}
360
 
364
 
361
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
365
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
362
    ERROR_DECLARE;
366
    ERROR_DECLARE;
363
 
367
 
364
//  packet_t            packet;
368
//  packet_t            packet;
365
    measured_string_ref address;
369
    measured_string_ref address;
366
    measured_string_ref translation;
370
    measured_string_ref translation;
367
    char *              data;
371
    char *              data;
368
 
372
 
369
    * answer_count = 0;
373
    * answer_count = 0;
370
    switch( IPC_GET_METHOD( * call )){
374
    switch( IPC_GET_METHOD( * call )){
371
        case IPC_M_PHONE_HUNGUP:
375
        case IPC_M_PHONE_HUNGUP:
372
            return EOK;
376
            return EOK;
373
        case NET_ARP_DEVICE:
377
        case NET_ARP_DEVICE:
374
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
378
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
375
            if( ERROR_OCCURED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
379
            if( ERROR_OCCURED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
376
                free( address );
380
                free( address );
377
                free( data );
381
                free( data );
378
            }
382
            }
379
            return ERROR_CODE;
383
            return ERROR_CODE;
380
        case NET_ARP_TRANSLATE:
384
        case NET_ARP_TRANSLATE:
381
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
385
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
382
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
386
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
383
            free( address );
387
            free( address );
384
            free( data );
388
            free( data );
385
            if( ! translation ) return ENOENT;
389
            if( ! translation ) return ENOENT;
386
            return measured_strings_reply( translation, 1 );
390
            return measured_strings_reply( translation, 1 );
387
        case NET_ARP_CLEAR_DEVICE:
391
        case NET_ARP_CLEAR_DEVICE:
388
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
392
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
389
        case NET_ARP_CLEAN_CACHE:
393
        case NET_ARP_CLEAN_CACHE:
390
            return arp_clean_cache_message();
394
            return arp_clean_cache_message();
391
    }
395
    }
392
    return ENOTSUP;
396
    return ENOTSUP;
393
}
397
}
394
 
398
 
395
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
399
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
396
    ERROR_DECLARE;
400
    ERROR_DECLARE;
397
 
401
 
398
    ipc_callid_t    callid;
402
    ipc_callid_t    callid;
399
    ipc_call_t      call;
403
    ipc_call_t      call;
400
//  int             result;
404
//  int             result;
401
    packet_t        packet;
405
    packet_t        packet;
402
 
406
 
403
    /*
407
    /*
404
     * Accept the connection
408
     * Accept the connection
405
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
409
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
406
     */
410
     */
407
    //TODO auto accept?
411
    //TODO auto accept?
408
    //ipc_answer_0( iid, EOK );
412
    //ipc_answer_0( iid, EOK );
409
 
413
 
410
    while( true ){
414
    while( true ){
411
        callid = async_get_call( & call );
415
        callid = async_get_call( & call );
412
        switch( IPC_GET_METHOD( call )){
416
        switch( IPC_GET_METHOD( call )){
413
            case NET_IL_DEVICE_STATE:
417
            case NET_IL_DEVICE_STATE:
414
                //TODO clear device if off?
418
                //TODO clear device if off?
415
                break;
419
                break;
416
            case NET_IL_RECEIVED:
420
            case NET_IL_RECEIVED:
417
                if( ! ERROR_OCCURED( packet_receive( & packet ))){
421
                if( ! ERROR_OCCURED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( & call )))){
418
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( & call ), packet );
422
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( & call ), packet );
419
                }
423
                }
420
                ipc_answer_0( callid, ERROR_CODE );
424
                ipc_answer_0( callid, ERROR_CODE );
421
                break;
425
                break;
422
            default:
426
            default:
423
                ipc_answer_0( callid, ENOTSUP );
427
                ipc_answer_0( callid, ENOTSUP );
424
        }
428
        }
425
    }
429
    }
426
}
430
}
427
 
431
 
428
/** @}
432
/** @}
429
 */
433
 */
430
 
434