Subversion Repositories HelenOS

Rev

Rev 3846 | Rev 3901 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3846 Rev 3886
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 "../measured_strings.h"
-
 
47
#include "../messages.h"
46
#include "../../messages.h"
48
#include "../modules.h"
47
#include "../../modules.h"
49
#include "../packet.h"
-
 
50
 
48
 
51
#include "../include/protocol_map.h"
49
#include "../../include/protocol_map.h"
52
#include "../netif/device.h"
50
#include "../../netif/device.h"
-
 
51
 
-
 
52
#include "../../structures/measured_strings.h"
-
 
53
#include "../../structures/packet/packet.h"
53
 
54
 
54
#include "arp.h"
55
#include "arp.h"
55
#include "arp_header.h"
56
#include "arp_header.h"
56
#include "arp_oc.h"
57
#include "arp_oc.h"
57
//#include "arp_messages.h"
58
//#include "arp_messages.h"
58
#include "arp_module.h"
59
#include "arp_module.h"
59
 
60
 
60
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
61
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
61
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
62
#define IPC_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
62
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
63
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG3( * call )
63
 
64
 
64
arp_globals_t   arp_globals;
65
arp_globals_t   arp_globals;
65
 
66
 
66
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
67
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
67
 
68
 
68
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
69
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
69
 
70
 
70
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
71
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
71
 
72
 
72
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
73
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
73
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
74
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
74
measured_string_ref arp_translate_message( device_id_t device, services_t protocol, measured_string_ref target );
75
measured_string_ref arp_translate_message( device_id_t device, services_t protocol, measured_string_ref target );
75
int arp_receive_message( device_id_t device_id, packet_t packet );
76
int arp_receive_message( device_id_t device_id, packet_t packet );
76
int arp_clear_device_message( device_id_t device_id );
77
int arp_clear_device_message( device_id_t device_id );
77
void clear_device( arp_device_ref device );
78
void clear_device( arp_device_ref device );
78
int arp_clean_cache_message( void );
79
int arp_clean_cache_message( void );
79
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
80
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
80
 
81
 
81
/** Initializes the ARP module.
82
/** Initializes the ARP module.
82
 */
83
 */
83
int arp_initialize( void ){
84
int arp_initialize( void ){
84
    arp_cache_initialize( & arp_globals.cache );
85
    arp_cache_initialize( & arp_globals.cache );
85
    return EOK;
86
    return EOK;
86
}
87
}
87
 
88
 
88
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
89
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
89
    ERROR_DECLARE;
90
    ERROR_DECLARE;
90
 
91
 
91
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
92
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
92
    if( !( * proto )) return ENOMEM;
93
    if( !( * proto )) return ENOMEM;
93
    ( ** proto ).service = service;
94
    ( ** proto ).service = service;
94
    ( ** proto ).addr = address;
95
    ( ** proto ).addr = address;
95
    ( ** proto ).addr_data = address->value;
96
    ( ** proto ).addr_data = address->value;
96
    if( ERROR_OCCURED( arp_addr_initialize( &( ** proto).addresses ))){
97
    if( ERROR_OCCURED( arp_addr_initialize( &( ** proto).addresses ))){
97
        free( * proto );
98
        free( * proto );
98
        return ERROR_CODE;
99
        return ERROR_CODE;
99
    }
100
    }
100
    return EOK;
101
    return EOK;
101
}
102
}
102
 
103
 
103
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
104
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
104
    ERROR_DECLARE;
105
    ERROR_DECLARE;
105
 
106
 
106
    arp_device_ref  device;
107
    arp_device_ref  device;
107
    aid_t           message;
108
    aid_t           message;
108
    ipc_call_t      answer;
109
    ipc_call_t      answer;
109
    ipcarg_t        result;
110
    ipcarg_t        result;
110
    arp_proto_ref   proto;
111
    arp_proto_ref   proto;
111
 
112
 
112
    // an existing device?
113
    // an existing device?
113
    device = arp_cache_find( & arp_globals.cache, device_id );
114
    device = arp_cache_find( & arp_globals.cache, device_id );
114
    if( device ){
115
    if( device ){
115
        if( device->service != service ) return EEXIST;
116
        if( device->service != service ) return EEXIST;
116
        proto = arp_protos_find( & device->protos, protocol );
117
        proto = arp_protos_find( & device->protos, protocol );
117
        if( proto ){
118
        if( proto ){
118
            free( proto->addr );
119
            free( proto->addr );
119
            free( proto->addr_data );
120
            free( proto->addr_data );
120
            proto->addr = address;
121
            proto->addr = address;
121
            proto->addr_data = address->value;
122
            proto->addr_data = address->value;
122
        }else{
123
        }else{
123
            ERROR_PROPAGATE( arp_proto_create( & proto, protocol, address ));
124
            ERROR_PROPAGATE( arp_proto_create( & proto, protocol, address ));
124
            if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
125
            if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
125
                free( proto );
126
                free( proto );
126
                return ERROR_CODE;
127
                return ERROR_CODE;
127
            }
128
            }
128
        }
129
        }
129
        return EOK;
130
        return EOK;
130
    }else{
131
    }else{
131
        // create a new device
132
        // create a new device
132
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
133
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
133
        if( ! device ) return ENOMEM;
134
        if( ! device ) return ENOMEM;
134
        device->device_id = device_id;
135
        device->device_id = device_id;
135
        if( ERROR_OCCURED( arp_protos_initialize( & device->protos ))
136
        if( ERROR_OCCURED( arp_protos_initialize( & device->protos ))
136
        || ERROR_OCCURED( arp_proto_create( & proto, protocol, address ))){
137
        || ERROR_OCCURED( arp_proto_create( & proto, protocol, address ))){
137
            free( device );
138
            free( device );
138
            return ERROR_CODE;
139
            return ERROR_CODE;
139
        }
140
        }
140
        if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
141
        if( ERROR_OCCURED( arp_protos_add( & device->protos, proto->service, proto ))){
141
            arp_protos_destroy( & device->protos );
142
            arp_protos_destroy( & device->protos );
142
            free( device );
143
            free( device );
143
            return ERROR_CODE;
144
            return ERROR_CODE;
144
        }
145
        }
145
        device->service = service;
146
        device->service = service;
146
        // bind the new one
147
        // bind the new one
147
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
148
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
148
        // get packet dimensions
149
        // get packet dimensions
149
        if( ERROR_OCCURED( async_req_1_3( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->prefix, & device->content, & device->sufix ))){
150
        if( ERROR_OCCURED( async_req_1_3( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->prefix, & device->content, & device->sufix ))){
150
            arp_protos_destroy( & device->protos );
151
            arp_protos_destroy( & device->protos );
151
            free( device );
152
            free( device );
152
            return ERROR_CODE;
153
            return ERROR_CODE;
153
        }
154
        }
154
        // get hardware address
155
        // get hardware address
155
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
156
        message = async_send_1( device->phone, NET_NIL_ADDR, device->device_id, & answer );
156
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
157
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->addr, & device->addr_data, 1 ))){
157
            arp_protos_destroy( & device->protos );
158
            arp_protos_destroy( & device->protos );
158
            free( device );
159
            free( device );
159
            async_wait_for( message, NULL );
160
            async_wait_for( message, NULL );
160
            return ERROR_CODE;
161
            return ERROR_CODE;
161
        }
162
        }
162
        async_wait_for( message, & result );
163
        async_wait_for( message, & result );
163
        if( ERROR_OCCURED( result )){
164
        if( ERROR_OCCURED( result )){
164
            free( device->addr );
165
            free( device->addr );
165
            free( device->addr_data );
166
            free( device->addr_data );
166
            arp_protos_destroy( & device->protos );
167
            arp_protos_destroy( & device->protos );
167
            free( device );
168
            free( device );
168
            return ERROR_CODE;
169
            return ERROR_CODE;
169
        }
170
        }
170
        // get broadcast address
171
        // get broadcast address
171
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
172
        message = async_send_1( device->phone, NET_NIL_BROADCAST_ADDR, device->device_id, & answer );
172
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
173
        if( ERROR_OCCURED( measured_strings_return( device->phone, & device->broadcast_addr, & device->broadcast_data, 1 ))){
173
            free( device->addr );
174
            free( device->addr );
174
            free( device->addr_data );
175
            free( device->addr_data );
175
            arp_protos_destroy( & device->protos );
176
            arp_protos_destroy( & device->protos );
176
            free( device );
177
            free( device );
177
            async_wait_for( message, NULL );
178
            async_wait_for( message, NULL );
178
            return ERROR_CODE;
179
            return ERROR_CODE;
179
        }
180
        }
180
        async_wait_for( message, & result );
181
        async_wait_for( message, & result );
181
        // add to the cache
182
        // add to the cache
182
        if( ERROR_OCCURED( result )
183
        if( ERROR_OCCURED( result )
183
        || ERROR_OCCURED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
184
        || ERROR_OCCURED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
184
            free( device->addr );
185
            free( device->addr );
185
            free( device->addr_data );
186
            free( device->addr_data );
186
            free( device->broadcast_addr );
187
            free( device->broadcast_addr );
187
            free( device->broadcast_data );
188
            free( device->broadcast_data );
188
            arp_protos_destroy( & device->protos );
189
            arp_protos_destroy( & device->protos );
189
            free( device );
190
            free( device );
190
            return ERROR_CODE;
191
            return ERROR_CODE;
191
        }
192
        }
192
    }
193
    }
193
    return EOK;
194
    return EOK;
194
}
195
}
195
 
196
 
196
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
197
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
197
//  ERROR_DECLARE;
198
//  ERROR_DECLARE;
198
 
199
 
199
    arp_device_ref      device;
200
    arp_device_ref      device;
200
    arp_proto_ref       proto;
201
    arp_proto_ref       proto;
201
    measured_string_ref addr;
202
    measured_string_ref addr;
202
    size_t              length;
203
    size_t              length;
203
    packet_t            packet;
204
    packet_t            packet;
204
    arp_header_ref      header;
205
    arp_header_ref      header;
205
 
206
 
206
    if( ! target ) return NULL;
207
    if( ! target ) return NULL;
207
    device = arp_cache_find( & arp_globals.cache, device_id );
208
    device = arp_cache_find( & arp_globals.cache, device_id );
208
    if( ! device ) return NULL;
209
    if( ! device ) return NULL;
209
    proto = arp_protos_find( & device->protos, protocol );
210
    proto = arp_protos_find( & device->protos, protocol );
210
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
211
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
211
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
212
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
212
    if( addr ) return addr;
213
    if( addr ) return addr;
213
    // ARP packet content size = header + ( address + translation ) * 2
214
    // ARP packet content size = header + ( address + translation ) * 2
214
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
215
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
215
    if( length > device->content ){
216
    if( length > device->content ){
216
        return NULL;
217
        return NULL;
217
    }
218
    }
218
    packet = packet_create( device->prefix, length, device->sufix );
219
    packet = packet_create( device->prefix, length, device->sufix );
219
    if( ! packet ) return NULL;
220
    if( ! packet ) return NULL;
220
    header = ( arp_header_ref ) packet_append( packet, length );
221
    header = ( arp_header_ref ) packet_append( packet, length );
221
    header->hardware = device->hardware;
222
    header->hardware = device->hardware;
222
    header->hardware_length = device->addr->length;
223
    header->hardware_length = device->addr->length;
223
    header->protocol = protocol_map( device->service, protocol );
224
    header->protocol = protocol_map( device->service, protocol );
224
    header->protocol_length = proto->addr->length;
225
    header->protocol_length = proto->addr->length;
225
    header->operation = ARPOP_REQUEST;
226
    header->operation = ARPOP_REQUEST;
226
    length = sizeof( arp_header_t );
227
    length = sizeof( arp_header_t );
227
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
228
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
228
    length += device->addr->length;
229
    length += device->addr->length;
229
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
230
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
230
    length += proto->addr->length;
231
    length += proto->addr->length;
231
    memset((( uint8_t * ) header ) + length, 0, device->addr->length );
232
    memset((( uint8_t * ) header ) + length, 0, device->addr->length );
232
    length += device->addr->length;
233
    length += device->addr->length;
233
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
234
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
234
    // TODO send to the device->broadcast_addr as arp protocol
235
    // TODO send to the device->broadcast_addr as arp protocol
235
    packet_send( packet, device->phone );
236
    packet_send( packet, device->phone );
236
    return NULL;
237
    return NULL;
237
}
238
}
238
 
239
 
239
int arp_receive_message( device_id_t device_id, packet_t packet ){
240
int arp_receive_message( device_id_t device_id, packet_t packet ){
240
    ERROR_DECLARE;
241
    ERROR_DECLARE;
241
 
242
 
242
    size_t              length;
243
    size_t              length;
243
    arp_header_ref      header;
244
    arp_header_ref      header;
244
    arp_device_ref      device;
245
    arp_device_ref      device;
245
    arp_proto_ref       proto;
246
    arp_proto_ref       proto;
246
//  arp_addr_ref        addr;
247
//  arp_addr_ref        addr;
247
    measured_string_ref hw_source;
248
    measured_string_ref hw_source;
248
/*  measured_string_t   proto_target;
249
/*  measured_string_t   proto_target;
249
    aid_t               message;
250
    aid_t               message;
250
    ipcarg_t            result;
251
    ipcarg_t            result;
251
    int                 index;
252
    int                 index;
252
    ipc_call_t          answer;
253
    ipc_call_t          answer;
253
*/  int8_t *            src_hw;
254
*/  int8_t *            src_hw;
254
    int8_t *            src_proto;
255
    int8_t *            src_proto;
255
    int8_t *            des_hw;
256
    int8_t *            des_hw;
256
    int8_t *            des_proto;
257
    int8_t *            des_proto;
257
 
258
 
258
    length = packet_get_data_length( packet );
259
    length = packet_get_data_length( packet );
259
    if( length <= sizeof( arp_header_t )) return EINVAL;
260
    if( length <= sizeof( arp_header_t )) return EINVAL;
260
    device = arp_cache_find( & arp_globals.cache, device_id );
261
    device = arp_cache_find( & arp_globals.cache, device_id );
261
    if( ! device ) return ENOENT;
262
    if( ! device ) return ENOENT;
262
    header = ( arp_header_ref ) packet_get_data( packet );
263
    header = ( arp_header_ref ) packet_get_data( packet );
263
    if( header->hardware != device->hardware ) return EINVAL;
264
    if( header->hardware != device->hardware ) return EINVAL;
264
    if( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 ) return EINVAL;
265
    if( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 ) return EINVAL;
265
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
266
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, header->protocol ));
266
    if( ! proto ) return ENOENT;
267
    if( ! proto ) return ENOENT;
267
    src_hw = (( int8_t * ) header ) + sizeof( arp_header_t );
268
    src_hw = (( int8_t * ) header ) + sizeof( arp_header_t );
268
    src_proto = src_hw + header->hardware_length;
269
    src_proto = src_hw + header->hardware_length;
269
    des_hw = src_proto + header->protocol_length;
270
    des_hw = src_proto + header->protocol_length;
270
    des_proto = des_hw + header->hardware_length;
271
    des_proto = des_hw + header->hardware_length;
271
    hw_source = arp_addr_find( & proto->addresses, src_proto, header->protocol_length );
272
    hw_source = arp_addr_find( & proto->addresses, src_proto, header->protocol_length );
272
    // exists?
273
    // exists?
273
    if( hw_source ){
274
    if( hw_source ){
274
        if( hw_source->length != header->hardware_length ) return EINVAL;
275
        if( hw_source->length != header->hardware_length ) return EINVAL;
275
        memcpy( hw_source->value, src_hw, header->hardware_length );
276
        memcpy( hw_source->value, src_hw, header->hardware_length );
276
    }
277
    }
277
    // is my protocol address?
278
    // is my protocol address?
278
/*  proto_target.value = des_proto;
279
/*  proto_target.value = des_proto;
279
    proto_target.length = header->protocol_length;
280
    proto_target.length = header->protocol_length;
280
    // TODO send necessary?
281
    // TODO send necessary?
281
    message = async_send_0( proto->phone, NET_IL_MY_ADDR, & answer );
282
    message = async_send_0( proto->phone, NET_IL_MY_ADDR, & answer );
282
    if( ERROR_OCCURED( measured_strings_send( device->phone, & proto_target, 1 ))){
283
    if( ERROR_OCCURED( measured_strings_send( device->phone, & proto_target, 1 ))){
283
        async_wait_for( message, NULL );
284
        async_wait_for( message, NULL );
284
        return ERROR_CODE;
285
        return ERROR_CODE;
285
    }
286
    }
286
    async_wait_for( message, & result );
287
    async_wait_for( message, & result );
287
    if( result == EOK ){
288
    if( result == EOK ){
288
*/  if( proto->addr->length != header->hardware_length ) return EINVAL;
289
*/  if( proto->addr->length != header->hardware_length ) return EINVAL;
289
    if( ! strncmp( proto->addr->value, des_proto, proto->addr->length )){
290
    if( ! strncmp( proto->addr->value, des_proto, proto->addr->length )){
290
        // not already upadted?
291
        // not already upadted?
291
        if( ! hw_source ){
292
        if( ! hw_source ){
292
            hw_source = measured_string_create_bulk( src_hw, header->hardware_length );
293
            hw_source = measured_string_create_bulk( src_hw, header->hardware_length );
293
            if( ! hw_source ) return ENOMEM;
294
            if( ! hw_source ) return ENOMEM;
294
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, src_proto, header->protocol_length, hw_source ));
295
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, src_proto, header->protocol_length, hw_source ));
295
        }
296
        }
296
        if( header->operation == ARPOP_REQUEST ){
297
        if( header->operation == ARPOP_REQUEST ){
297
            header->operation = ARPOP_REPLY;
298
            header->operation = ARPOP_REPLY;
298
/*          for( index = 0; index + header->hardware_length < header->protocol_length; index += header->hardware_length ){
299
/*          for( index = 0; index + header->hardware_length < header->protocol_length; index += header->hardware_length ){
299
                memcpy( src_hw, src_proto + index, header->hardware_length );
300
                memcpy( src_hw, src_proto + index, header->hardware_length );
300
                memcpy( src_proto + index, des_proto + index, header->hardware_length );
301
                memcpy( src_proto + index, des_proto + index, header->hardware_length );
301
                memcpy( des_proto + index, src_hw, header->hardware_length );
302
                memcpy( des_proto + index, src_hw, header->hardware_length );
302
            }
303
            }
303
            memcpy( src_hw, src_proto + index, header->hardware_length - header->protocol_length );
304
            memcpy( src_hw, src_proto + index, header->hardware_length - header->protocol_length );
304
            memcpy( src_proto + index, des_proto + index, header->hardware_length - header->protocol_length );
305
            memcpy( src_proto + index, des_proto + index, header->hardware_length - header->protocol_length );
305
            memcpy( des_proto + index, src_hw, header->hardware_length - header->protocol_length );
306
            memcpy( des_proto + index, src_hw, header->hardware_length - header->protocol_length );
306
            memcpy( src_hw, des_hw, header->hardware_length );
307
            memcpy( src_hw, des_hw, header->hardware_length );
307
            memcpy( des_hw, hw_source->value, hw_source->length );
308
            memcpy( des_hw, hw_source->value, hw_source->length );
308
*/          memcpy( des_proto, src_proto, header->protocol_length );
309
*/          memcpy( des_proto, src_proto, header->protocol_length );
309
            memcpy( src_proto, proto->addr->value, header->protocol_length );
310
            memcpy( src_proto, proto->addr->value, header->protocol_length );
310
            memcpy( src_hw, des_hw, header->hardware_length );
311
            memcpy( src_hw, des_hw, header->hardware_length );
311
            memcpy( des_hw, hw_source->value, hw_source->length );
312
            memcpy( des_hw, hw_source->value, hw_source->length );
312
            // TODO send to the hw_source as arp protocol
313
            // TODO send to the hw_source as arp protocol
313
            packet_send( packet, device->phone );
314
            packet_send( packet, device->phone );
314
        }
315
        }
315
    }
316
    }
316
    return EOK;
317
    return EOK;
317
}
318
}
318
 
319
 
319
int arp_clear_device_message( device_id_t device_id ){
320
int arp_clear_device_message( device_id_t device_id ){
320
    arp_device_ref  device;
321
    arp_device_ref  device;
321
 
322
 
322
    device = arp_cache_find( & arp_globals.cache, device_id );
323
    device = arp_cache_find( & arp_globals.cache, device_id );
323
    if( ! device ) return ENOENT;
324
    if( ! device ) return ENOENT;
324
    clear_device( device );
325
    clear_device( device );
325
    return EOK;
326
    return EOK;
326
}
327
}
327
 
328
 
328
void clear_device( arp_device_ref device ){
329
void clear_device( arp_device_ref device ){
329
    int             count;
330
    int             count;
330
    arp_proto_ref   proto;
331
    arp_proto_ref   proto;
331
 
332
 
332
    count = arp_protos_count( & device->protos );
333
    count = arp_protos_count( & device->protos );
333
    while( count > 0 ){
334
    while( count > 0 ){
334
        proto = arp_protos_get_index( & device->protos, count );
335
        proto = arp_protos_get_index( & device->protos, count );
335
        if( proto->addr ) free( proto->addr );
336
        if( proto->addr ) free( proto->addr );
336
        if( proto->addr_data ) free( proto->addr_data );
337
        if( proto->addr_data ) free( proto->addr_data );
337
        arp_addr_destroy( & proto->addresses );
338
        arp_addr_destroy( & proto->addresses );
338
        -- count;
339
        -- count;
339
    }
340
    }
340
    arp_protos_clear( & device->protos );
341
    arp_protos_clear( & device->protos );
341
}
342
}
342
 
343
 
343
int arp_clean_cache_message( void ){
344
int arp_clean_cache_message( void ){
344
    int             count;
345
    int             count;
345
    arp_device_ref  device;
346
    arp_device_ref  device;
346
 
347
 
347
    count = arp_cache_count( & arp_globals.cache );
348
    count = arp_cache_count( & arp_globals.cache );
348
    while( count > 0 ){
349
    while( count > 0 ){
349
        device = arp_cache_get_index( & arp_globals.cache, count );
350
        device = arp_cache_get_index( & arp_globals.cache, count );
350
        if( device ){
351
        if( device ){
351
            clear_device( device );
352
            clear_device( device );
352
            if( device->broadcast_addr ) free( device->broadcast_addr );
353
            if( device->broadcast_addr ) free( device->broadcast_addr );
353
            if( device->broadcast_data ) free( device->broadcast_data );
354
            if( device->broadcast_data ) free( device->broadcast_data );
354
        }
355
        }
355
    }
356
    }
356
    arp_cache_clear( & arp_globals.cache );
357
    arp_cache_clear( & arp_globals.cache );
357
    return EOK;
358
    return EOK;
358
}
359
}
359
 
360
 
360
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
361
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
361
    ERROR_DECLARE;
362
    ERROR_DECLARE;
362
 
363
 
363
//  packet_t            packet;
364
//  packet_t            packet;
364
    measured_string_ref address;
365
    measured_string_ref address;
365
    measured_string_ref translation;
366
    measured_string_ref translation;
366
    char *              data;
367
    char *              data;
367
 
368
 
368
    * answer_count = 0;
369
    * answer_count = 0;
369
    switch( IPC_GET_METHOD( * call )){
370
    switch( IPC_GET_METHOD( * call )){
370
        case IPC_M_PHONE_HUNGUP:
371
        case IPC_M_PHONE_HUNGUP:
371
            return EOK;
372
            return EOK;
372
        case NET_ARP_DEVICE:
373
        case NET_ARP_DEVICE:
373
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
374
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
374
            if( ERROR_OCCURED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
375
            if( ERROR_OCCURED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), IPC_GET_PROTO( call ), address ))){
375
                free( address );
376
                free( address );
376
                free( data );
377
                free( data );
377
            }
378
            }
378
            return ERROR_CODE;
379
            return ERROR_CODE;
379
        case NET_ARP_TRANSLATE:
380
        case NET_ARP_TRANSLATE:
380
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
381
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
381
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
382
            translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_PROTO( call ), address );
382
            free( address );
383
            free( address );
383
            free( data );
384
            free( data );
384
            if( ! translation ) return ENOENT;
385
            if( ! translation ) return ENOENT;
385
            return measured_strings_reply( translation, 1 );
386
            return measured_strings_reply( translation, 1 );
386
        case NET_ARP_CLEAR_DEVICE:
387
        case NET_ARP_CLEAR_DEVICE:
387
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
388
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
388
        case NET_ARP_CLEAN_CACHE:
389
        case NET_ARP_CLEAN_CACHE:
389
            return arp_clean_cache_message();
390
            return arp_clean_cache_message();
390
    }
391
    }
391
    return ENOTSUP;
392
    return ENOTSUP;
392
}
393
}
393
 
394
 
394
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
395
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
395
    ERROR_DECLARE;
396
    ERROR_DECLARE;
396
 
397
 
397
    ipc_callid_t    callid;
398
    ipc_callid_t    callid;
398
    ipc_call_t      call;
399
    ipc_call_t      call;
399
//  int             result;
400
//  int             result;
400
    packet_t        packet;
401
    packet_t        packet;
401
 
402
 
402
    /*
403
    /*
403
     * Accept the connection
404
     * Accept the connection
404
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
405
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
405
     */
406
     */
406
    //TODO auto accept?
407
    //TODO auto accept?
407
    //ipc_answer_0( iid, EOK );
408
    //ipc_answer_0( iid, EOK );
408
 
409
 
409
    while( true ){
410
    while( true ){
410
        callid = async_get_call( & call );
411
        callid = async_get_call( & call );
411
        switch( IPC_GET_METHOD( call )){
412
        switch( IPC_GET_METHOD( call )){
412
            case NET_IL_DEVICE_STATE:
413
            case NET_IL_DEVICE_STATE:
413
                //TODO clear device if off?
414
                //TODO clear device if off?
414
                break;
415
                break;
415
            case NET_IL_RECEIVED:
416
            case NET_IL_RECEIVED:
416
                if( ! ERROR_OCCURED( packet_receive( & packet ))){
417
                if( ! ERROR_OCCURED( packet_receive( & packet ))){
417
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( & call ), packet );
418
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( & call ), packet );
418
                }
419
                }
419
                ipc_answer_0( callid, ERROR_CODE );
420
                ipc_answer_0( callid, ERROR_CODE );
420
                break;
421
                break;
421
            default:
422
            default:
422
                ipc_answer_0( callid, ENOTSUP );
423
                ipc_answer_0( callid, ENOTSUP );
423
        }
424
        }
424
    }
425
    }
425
}
426
}
426
 
427
 
427
/** @}
428
/** @}
428
 */
429
 */
429
 
430