Subversion Repositories HelenOS

Rev

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

Rev 4261 Rev 4271
1
/*
1
/*
2
 * Copyright (c) 2009 Lukas Mejdrech
2
 * Copyright (c) 2009 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
 *  ARP module implementation.
34
 *  ARP module implementation.
35
 *  @see arp.h
35
 *  @see arp.h
36
 */
36
 */
37
 
37
 
38
#include <async.h>
38
#include <async.h>
39
#include <malloc.h>
39
#include <malloc.h>
40
#include <mem.h>
40
#include <mem.h>
41
#include <rwlock.h>
41
#include <rwlock.h>
42
#include <stdio.h>
42
#include <stdio.h>
43
 
43
 
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
 
46
 
47
#include "../../err.h"
47
#include "../../err.h"
48
#include "../../messages.h"
48
#include "../../messages.h"
49
#include "../../modules.h"
49
#include "../../modules.h"
50
 
50
 
51
#include "../../include/byteorder.h"
51
#include "../../include/byteorder.h"
52
#include "../../include/device.h"
52
#include "../../include/device.h"
-
 
53
#include "../../include/arp_messages.h"
53
#include "../../include/nil_messages.h"
54
#include "../../include/nil_messages.h"
54
#include "../../include/protocol_map.h"
55
#include "../../include/protocol_map.h"
55
 
56
 
56
#include "../../structures/measured_strings.h"
57
#include "../../structures/measured_strings.h"
57
#include "../../structures/packet/packet.h"
58
#include "../../structures/packet/packet.h"
58
#include "../../structures/packet/packet_client.h"
59
#include "../../structures/packet/packet_client.h"
59
 
60
 
60
#include "arp.h"
61
#include "arp.h"
61
#include "arp_header.h"
62
#include "arp_header.h"
62
#include "arp_oc.h"
63
#include "arp_oc.h"
63
//#include "arp_messages.h"
-
 
64
#include "arp_module.h"
64
#include "arp_module.h"
65
 
-
 
66
/** Returns the protocol service message parameter.
65
#include "arp_wrappers.h"
67
 */
-
 
68
#define ARP_GET_PROTO( call )       ( services_t ) IPC_GET_ARG2( * call )
-
 
69
 
66
 
70
/** ARP global data.
67
/** ARP global data.
71
 */
68
 */
72
arp_globals_t   arp_globals;
69
arp_globals_t   arp_globals;
73
 
70
 
74
/** Creates new protocol specific data.
71
/** Creates new protocol specific data.
75
 *  @param proto Protocol specific data. Output parameter.
72
 *  @param proto Protocol specific data. Output parameter.
76
 *  @param service Protocol module service. Input parameter.
73
 *  @param service Protocol module service. Input parameter.
77
 *  @param address Actual protocol device address. Input parameter.
74
 *  @param address Actual protocol device address. Input parameter.
78
 *  @returns EOK on success.
75
 *  @returns EOK on success.
79
 *  @returns ENOMEM if there is not enough memory left.
76
 *  @returns ENOMEM if there is not enough memory left.
80
 */
77
 */
81
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
78
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address );
82
 
79
 
83
/** Registers the device.
80
/** Registers the device.
84
 *  Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists.
81
 *  Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists.
85
 *  @param device_id The device identifier. Input parameter.
82
 *  @param device_id The device identifier. Input parameter.
86
 *  @param service The device driver service. Input parameter.
83
 *  @param service The device driver service. Input parameter.
87
 *  @param protocol The protocol service. Input parameter.
84
 *  @param protocol The protocol service. Input parameter.
88
 *  @param address The actual device protocol address.
85
 *  @param address The actual device protocol address.
89
 *  @returns EOK on success.
86
 *  @returns EOK on success.
90
 *  @returns EEXIST if another device with the same device identifier and different driver service exists.
87
 *  @returns EEXIST if another device with the same device identifier and different driver service exists.
91
 *  @returns ENOMEM if there is not enough memory left.
88
 *  @returns ENOMEM if there is not enough memory left.
92
 *  @returns Other error codes as defined for the measured_strings_return() function.
89
 *  @returns Other error codes as defined for the measured_strings_return() function.
93
 */
90
 */
94
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
91
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address );
95
 
92
 
96
/** Returns the hardware address for the given protocol address.
93
/** Returns the hardware address for the given protocol address.
97
 *  Sends the ARP request packet if the hardware address is not found in the cache.
94
 *  Sends the ARP request packet if the hardware address is not found in the cache.
98
 *  @param device_id The device identifier. Input parameter.
95
 *  @param device_id The device identifier. Input parameter.
99
 *  @param protocol The protocol service. Input parameter.
96
 *  @param protocol The protocol service. Input parameter.
100
 *  @param target The target protocol address. Input parameter.
97
 *  @param target The target protocol address. Input parameter.
101
 *  @returns The hardware address of the target.
98
 *  @returns The hardware address of the target.
102
 *  @returns NULL if the target parameter is NULL.
99
 *  @returns NULL if the target parameter is NULL.
103
 *  @returns NULL if the device is not found.
100
 *  @returns NULL if the device is not found.
104
 *  @returns NULL if the device packet is too small to send a&nbsp;request.
101
 *  @returns NULL if the device packet is too small to send a&nbsp;request.
105
 *  @returns NULL if the hardware address is not found in the cache.
102
 *  @returns NULL if the hardware address is not found in the cache.
106
 */
103
 */
107
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target );
104
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target );
108
 
105
 
109
/** Processes the received ARP packet.
106
/** Processes the received ARP packet.
110
 *  Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address.
107
 *  Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address.
111
 *  Responses to the ARP request if the packet is the ARP request and is targeted to my address.
108
 *  Responses to the ARP request if the packet is the ARP request and is targeted to my address.
112
 *  @param device_id The source device identifier. Input parameter.
109
 *  @param device_id The source device identifier. Input parameter.
113
 *  @param packet The received packet. Input/output parameter.
110
 *  @param packet The received packet. Input/output parameter.
114
 *  @returns EOK on success.
111
 *  @returns EOK on success.
115
 *  @returns EINVAL if the packet is too small to carry the ARP packet.
112
 *  @returns EINVAL if the packet is too small to carry the ARP packet.
116
 *  @returns EINVAL if the received address lengths differs from the registered values.
113
 *  @returns EINVAL if the received address lengths differs from the registered values.
117
 *  @returns ENOENT if the device is not found in the cache.
114
 *  @returns ENOENT if the device is not found in the cache.
118
 *  @returns ENOENT if the protocol for the device is not found in the cache.
115
 *  @returns ENOENT if the protocol for the device is not found in the cache.
119
 *  @returns ENOMEM if there is not enough memory left.
116
 *  @returns ENOMEM if there is not enough memory left.
120
 */
117
 */
121
int arp_receive_message( device_id_t device_id, packet_t packet );
118
int arp_receive_message( device_id_t device_id, packet_t packet );
122
 
119
 
123
/** Clears the device specific data from the cache.
-
 
124
 *  @param device_id The device identifier. Input parameter.
-
 
125
 *  @returns EOK on success.
-
 
126
 *  @returns ENOENT  if the device is not found in the cache.
-
 
127
 */
-
 
128
int arp_clear_device_message( device_id_t device_id );
-
 
129
 
-
 
130
/** Clears the device specific data.
120
/** Clears the device specific data.
131
 *  @param device The device specific data.
121
 *  @param device The device specific data.
132
 */
122
 */
133
void    clear_device( arp_device_ref device );
123
void    clear_device( arp_device_ref device );
134
 
124
 
135
/** Clears the whole cache.
-
 
136
 *  @returns EOK on success.
-
 
137
 */
-
 
138
int arp_clean_cache_message( void );
-
 
139
 
-
 
140
/** Processes IPC messages from the registered device driver modules in an infinite loop.
125
/** Processes IPC messages from the registered device driver modules in an infinite loop.
141
 *  @param iid The message identifier. Input parameter.
126
 *  @param iid The message identifier. Input parameter.
142
 *  @param icall The message parameters. Input/output parameter.
127
 *  @param icall The message parameters. Input/output parameter.
143
 */
128
 */
144
void    arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
129
void    arp_receiver( ipc_callid_t iid, ipc_call_t * icall );
145
 
130
 
146
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
131
DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t )
147
 
132
 
148
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
133
INT_MAP_IMPLEMENT( arp_protos, arp_proto_t )
149
 
134
 
150
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
135
GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t )
151
 
136
 
-
 
137
int arp_clear_device_wrapper( device_id_t device_id ){
-
 
138
    arp_device_ref  device;
-
 
139
 
-
 
140
    rwlock_write_lock( & arp_globals.lock );
-
 
141
    device = arp_cache_find( & arp_globals.cache, device_id );
-
 
142
    if( ! device ){
-
 
143
        rwlock_write_unlock( & arp_globals.lock );
-
 
144
        return ENOENT;
-
 
145
    }
-
 
146
    clear_device( device );
-
 
147
    printf( "\nDevice %d cleared", device_id );
-
 
148
    rwlock_write_unlock( & arp_globals.lock );
-
 
149
    return EOK;
-
 
150
}
-
 
151
 
-
 
152
int arp_clean_cache_wrapper( void ){
-
 
153
    int             count;
-
 
154
    arp_device_ref  device;
-
 
155
 
-
 
156
    rwlock_write_lock( & arp_globals.lock );
-
 
157
    for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){
-
 
158
        device = arp_cache_get_index( & arp_globals.cache, count );
-
 
159
        if( device ){
-
 
160
            clear_device( device );
-
 
161
            if( device->addr_data ) free( device->addr_data );
-
 
162
            if( device->broadcast_data ) free( device->broadcast_data );
-
 
163
        }
-
 
164
    }
-
 
165
    arp_cache_clear( & arp_globals.cache );
-
 
166
    rwlock_write_unlock( & arp_globals.lock );
-
 
167
    printf( "\nCache cleaned" );
-
 
168
    return EOK;
-
 
169
}
-
 
170
 
-
 
171
int arp_device_wrapper( device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){
-
 
172
    ERROR_DECLARE;
-
 
173
 
-
 
174
    measured_string_ref tmp;
-
 
175
 
-
 
176
    tmp = measured_string_copy( address );
-
 
177
    if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){
-
 
178
        free( tmp->value );
-
 
179
        free( tmp );
-
 
180
    }
-
 
181
    return ERROR_CODE;
-
 
182
}
-
 
183
 
-
 
184
int arp_translate_wrapper( device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ){
-
 
185
    measured_string_ref tmp;
-
 
186
 
-
 
187
    rwlock_read_lock( & arp_globals.lock );
-
 
188
    tmp = arp_translate_message( device_id, protocol, address );
-
 
189
    if( tmp ){
-
 
190
        * translation = measured_string_copy( tmp );
-
 
191
        rwlock_read_unlock( & arp_globals.lock );
-
 
192
        if( * translation ){
-
 
193
            * data = ( ** translation ).value;
-
 
194
            return EOK;
-
 
195
        }else{
-
 
196
            return ENOMEM;
-
 
197
        }
-
 
198
    }else{
-
 
199
        rwlock_read_unlock( & arp_globals.lock );
-
 
200
        return ENOENT;
-
 
201
    }
-
 
202
}
-
 
203
 
152
int arp_initialize( void ){
204
int arp_initialize( void ){
153
    ERROR_DECLARE;
205
    ERROR_DECLARE;
154
 
206
 
155
    rwlock_initialize( & arp_globals.lock );
207
    rwlock_initialize( & arp_globals.lock );
156
    rwlock_write_lock( & arp_globals.lock );
208
    rwlock_write_lock( & arp_globals.lock );
157
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
209
    ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache ));
158
    rwlock_write_unlock( & arp_globals.lock );
210
    rwlock_write_unlock( & arp_globals.lock );
159
    return EOK;
211
    return EOK;
160
}
212
}
161
 
213
 
162
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
214
int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){
163
    ERROR_DECLARE;
215
    ERROR_DECLARE;
164
 
216
 
165
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
217
    * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t ));
166
    if( !( * proto )) return ENOMEM;
218
    if( !( * proto )) return ENOMEM;
167
    ( ** proto ).service = service;
219
    ( ** proto ).service = service;
168
    ( ** proto ).addr = address;
220
    ( ** proto ).addr = address;
169
    ( ** proto ).addr_data = address->value;
221
    ( ** proto ).addr_data = address->value;
170
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
222
    if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){
171
        free( * proto );
223
        free( * proto );
172
        return ERROR_CODE;
224
        return ERROR_CODE;
173
    }
225
    }
174
    return EOK;
226
    return EOK;
175
}
227
}
176
 
228
 
177
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
229
int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){
178
    ERROR_DECLARE;
230
    ERROR_DECLARE;
179
 
231
 
180
    arp_device_ref  device;
232
    arp_device_ref  device;
181
    arp_proto_ref   proto;
233
    arp_proto_ref   proto;
182
    int             index;
234
    int             index;
183
 
235
 
184
    rwlock_write_lock( & arp_globals.lock );
236
    rwlock_write_lock( & arp_globals.lock );
185
    // an existing device?
237
    // an existing device?
186
    device = arp_cache_find( & arp_globals.cache, device_id );
238
    device = arp_cache_find( & arp_globals.cache, device_id );
187
    if( device ){
239
    if( device ){
188
        if( device->service != service ){
240
        if( device->service != service ){
189
            printf( "\nDevice %d already exists", device->device_id );
241
            printf( "\nDevice %d already exists", device->device_id );
190
            rwlock_write_unlock( & arp_globals.lock );
242
            rwlock_write_unlock( & arp_globals.lock );
191
            return EEXIST;
243
            return EEXIST;
192
        }
244
        }
193
        proto = arp_protos_find( & device->protos, protocol );
245
        proto = arp_protos_find( & device->protos, protocol );
194
        if( proto ){
246
        if( proto ){
195
            free( proto->addr );
247
            free( proto->addr );
196
            free( proto->addr_data );
248
            free( proto->addr_data );
197
            proto->addr = address;
249
            proto->addr = address;
198
            proto->addr_data = address->value;
250
            proto->addr_data = address->value;
199
        }else{
251
        }else{
200
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
252
            if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
201
                rwlock_write_unlock( & arp_globals.lock );
253
                rwlock_write_unlock( & arp_globals.lock );
202
                return ERROR_CODE;
254
                return ERROR_CODE;
203
            }
255
            }
204
            index = arp_protos_add( & device->protos, proto->service, proto );
256
            index = arp_protos_add( & device->protos, proto->service, proto );
205
            if( index < 0 ){
257
            if( index < 0 ){
206
                rwlock_write_unlock( & arp_globals.lock );
258
                rwlock_write_unlock( & arp_globals.lock );
207
                free( proto );
259
                free( proto );
208
                return index;
260
                return index;
209
            }
261
            }
210
        }
262
        }
211
        printf( "\nCache of the existing device %d cleaned", device->device_id );
263
        printf( "\nCache of the existing device %d cleaned", device->device_id );
212
    }else{
264
    }else{
213
        index = hardware_map( service );
265
        index = hardware_map( service );
214
        if( ! index ) return ENOENT;
266
        if( ! index ) return ENOENT;
215
        // create a new device
267
        // create a new device
216
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
268
        device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
217
        if( ! device ){
269
        if( ! device ){
218
            rwlock_write_unlock( & arp_globals.lock );
270
            rwlock_write_unlock( & arp_globals.lock );
219
            return ENOMEM;
271
            return ENOMEM;
220
        }
272
        }
221
        device->hardware = index;
273
        device->hardware = index;
222
        device->device_id = device_id;
274
        device->device_id = device_id;
223
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
275
        if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
224
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
276
        || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
225
            rwlock_write_unlock( & arp_globals.lock );
277
            rwlock_write_unlock( & arp_globals.lock );
226
            free( device );
278
            free( device );
227
            return ERROR_CODE;
279
            return ERROR_CODE;
228
        }
280
        }
229
        index = arp_protos_add( & device->protos, proto->service, proto );
281
        index = arp_protos_add( & device->protos, proto->service, proto );
230
        if( index < 0 ){
282
        if( index < 0 ){
231
            rwlock_write_unlock( & arp_globals.lock );
283
            rwlock_write_unlock( & arp_globals.lock );
232
            arp_protos_destroy( & device->protos );
284
            arp_protos_destroy( & device->protos );
233
            free( device );
285
            free( device );
234
            return index;
286
            return index;
235
        }
287
        }
236
        device->service = service;
288
        device->service = service;
237
        // bind the new one
289
        // bind the new one
238
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
290
        device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
239
        if( device->phone < 0 ){
291
        if( device->phone < 0 ){
240
            rwlock_write_unlock( & arp_globals.lock );
292
            rwlock_write_unlock( & arp_globals.lock );
241
            arp_protos_destroy( & device->protos );
293
            arp_protos_destroy( & device->protos );
242
            free( device );
294
            free( device );
243
            return EREFUSED;
295
            return EREFUSED;
244
        }
296
        }
245
        // get packet dimensions
297
        // get packet dimensions
246
        if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
298
        if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
247
            rwlock_write_unlock( & arp_globals.lock );
299
            rwlock_write_unlock( & arp_globals.lock );
248
            arp_protos_destroy( & device->protos );
300
            arp_protos_destroy( & device->protos );
249
            free( device );
301
            free( device );
250
            return ERROR_CODE;
302
            return ERROR_CODE;
251
        }
303
        }
252
        // get hardware address
304
        // get hardware address
253
        if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){
305
        if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){
254
            rwlock_write_unlock( & arp_globals.lock );
306
            rwlock_write_unlock( & arp_globals.lock );
255
            arp_protos_destroy( & device->protos );
307
            arp_protos_destroy( & device->protos );
256
            free( device );
308
            free( device );
257
            return ERROR_CODE;
309
            return ERROR_CODE;
258
        }
310
        }
259
        // get broadcast address
311
        // get broadcast address
260
        if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){
312
        if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){
261
            rwlock_write_unlock( & arp_globals.lock );
313
            rwlock_write_unlock( & arp_globals.lock );
262
            free( device->addr );
314
            free( device->addr );
263
            free( device->addr_data );
315
            free( device->addr_data );
264
            arp_protos_destroy( & device->protos );
316
            arp_protos_destroy( & device->protos );
265
            free( device );
317
            free( device );
266
            return ERROR_CODE;
318
            return ERROR_CODE;
267
        }
319
        }
268
        if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
320
        if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){
269
            rwlock_write_unlock( & arp_globals.lock );
321
            rwlock_write_unlock( & arp_globals.lock );
270
            free( device->addr );
322
            free( device->addr );
271
            free( device->addr_data );
323
            free( device->addr_data );
272
            free( device->broadcast_addr );
324
            free( device->broadcast_addr );
273
            free( device->broadcast_data );
325
            free( device->broadcast_data );
274
            arp_protos_destroy( & device->protos );
326
            arp_protos_destroy( & device->protos );
275
            free( device );
327
            free( device );
276
            return ERROR_CODE;
328
            return ERROR_CODE;
277
        }
329
        }
278
        printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d", device->device_id, device->hardware, device->service );
330
        printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d", device->device_id, device->hardware, device->service );
279
    }
331
    }
280
    rwlock_write_unlock( & arp_globals.lock );
332
    rwlock_write_unlock( & arp_globals.lock );
281
    return EOK;
333
    return EOK;
282
}
334
}
283
 
335
 
284
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
336
measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){
285
    arp_device_ref      device;
337
    arp_device_ref      device;
286
    arp_proto_ref       proto;
338
    arp_proto_ref       proto;
287
    measured_string_ref addr;
339
    measured_string_ref addr;
288
    size_t              length;
340
    size_t              length;
289
    packet_t            packet;
341
    packet_t            packet;
290
    arp_header_ref      header;
342
    arp_header_ref      header;
291
 
343
 
292
    if( ! target ) return NULL;
344
    if( ! target ) return NULL;
293
    device = arp_cache_find( & arp_globals.cache, device_id );
345
    device = arp_cache_find( & arp_globals.cache, device_id );
294
    if( ! device ) return NULL;
346
    if( ! device ) return NULL;
295
    proto = arp_protos_find( & device->protos, protocol );
347
    proto = arp_protos_find( & device->protos, protocol );
296
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
348
    if(( ! proto ) || ( proto->addr->length != target->length )) return NULL;
297
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
349
    addr = arp_addr_find( & proto->addresses, target->value, target->length );
298
    if( addr ) return addr;
350
    if( addr ) return addr;
299
    // ARP packet content size = header + ( address + translation ) * 2
351
    // ARP packet content size = header + ( address + translation ) * 2
300
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
352
    length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2;
301
    if( length > device->content ) return NULL;
353
    if( length > device->content ) return NULL;
302
    packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix );
354
    packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix );
303
    if( ! packet ) return NULL;
355
    if( ! packet ) return NULL;
304
    header = ( arp_header_ref ) packet_suffix( packet, length );
356
    header = ( arp_header_ref ) packet_suffix( packet, length );
305
    header->hardware = htons( device->hardware );
357
    header->hardware = htons( device->hardware );
306
    header->hardware_length = device->addr->length;
358
    header->hardware_length = device->addr->length;
307
    header->protocol = htons( protocol_map( device->service, protocol ));
359
    header->protocol = htons( protocol_map( device->service, protocol ));
308
    header->protocol_length = proto->addr->length;
360
    header->protocol_length = proto->addr->length;
309
    header->operation = htons( ARPOP_REQUEST );
361
    header->operation = htons( ARPOP_REQUEST );
310
    length = sizeof( arp_header_t );
362
    length = sizeof( arp_header_t );
311
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
363
    memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length );
312
    length += device->addr->length;
364
    length += device->addr->length;
313
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
365
    memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
314
    length += proto->addr->length;
366
    length += proto->addr->length;
315
    bzero((( uint8_t * ) header ) + length, device->addr->length );
367
    bzero((( uint8_t * ) header ) + length, device->addr->length );
316
    length += device->addr->length;
368
    length += device->addr->length;
317
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
369
    memcpy((( uint8_t * ) header ) + length, target->value, target->length );
318
    packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
370
    packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
319
    nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
371
    nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
320
    return NULL;
372
    return NULL;
321
}
373
}
322
 
374
 
323
int arp_receive_message( device_id_t device_id, packet_t packet ){
375
int arp_receive_message( device_id_t device_id, packet_t packet ){
324
    ERROR_DECLARE;
376
    ERROR_DECLARE;
325
 
377
 
326
    size_t              length;
378
    size_t              length;
327
    arp_header_ref      header;
379
    arp_header_ref      header;
328
    arp_device_ref      device;
380
    arp_device_ref      device;
329
    arp_proto_ref       proto;
381
    arp_proto_ref       proto;
330
    measured_string_ref hw_source;
382
    measured_string_ref hw_source;
331
    uint8_t *           src_hw;
383
    uint8_t *           src_hw;
332
    uint8_t *           src_proto;
384
    uint8_t *           src_proto;
333
    uint8_t *           des_hw;
385
    uint8_t *           des_hw;
334
    uint8_t *           des_proto;
386
    uint8_t *           des_proto;
335
 
387
 
336
    length = packet_get_data_length( packet );
388
    length = packet_get_data_length( packet );
337
    if( length <= sizeof( arp_header_t )) return EINVAL;
389
    if( length <= sizeof( arp_header_t )) return EINVAL;
338
    device = arp_cache_find( & arp_globals.cache, device_id );
390
    device = arp_cache_find( & arp_globals.cache, device_id );
339
    if( ! device ) return ENOENT;
391
    if( ! device ) return ENOENT;
340
    header = ( arp_header_ref ) packet_get_data( packet );
392
    header = ( arp_header_ref ) packet_get_data( packet );
341
    if(( ntohs( header->hardware ) != device->hardware )
393
    if(( ntohs( header->hardware ) != device->hardware )
342
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
394
    || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){
343
        return EINVAL;
395
        return EINVAL;
344
    }
396
    }
345
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
397
    proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol )));
346
    if( ! proto ) return ENOENT;
398
    if( ! proto ) return ENOENT;
347
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
399
    src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t );
348
    src_proto = src_hw + header->hardware_length;
400
    src_proto = src_hw + header->hardware_length;
349
    des_hw = src_proto + header->protocol_length;
401
    des_hw = src_proto + header->protocol_length;
350
    des_proto = des_hw + header->hardware_length;
402
    des_proto = des_hw + header->hardware_length;
351
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
403
    hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ));
352
    // exists?
404
    // exists?
353
    if( hw_source ){
405
    if( hw_source ){
354
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
406
        if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
355
            return EINVAL;
407
            return EINVAL;
356
        }
408
        }
357
        memcpy( hw_source->value, src_hw, hw_source->length );
409
        memcpy( hw_source->value, src_hw, hw_source->length );
358
    }
410
    }
359
    // is my protocol address?
411
    // is my protocol address?
360
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
412
    if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){
361
        return EINVAL;
413
        return EINVAL;
362
    }
414
    }
363
    if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
415
    if( ! strncmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){
364
        // not already upadted?
416
        // not already upadted?
365
        if( ! hw_source ){
417
        if( ! hw_source ){
366
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
418
            hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length ));
367
            if( ! hw_source ) return ENOMEM;
419
            if( ! hw_source ) return ENOMEM;
368
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
420
            ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source ));
369
        }
421
        }
370
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
422
        if( ntohs( header->operation ) == ARPOP_REQUEST ){
371
            header->operation = htons( ARPOP_REPLY );
423
            header->operation = htons( ARPOP_REPLY );
372
            memcpy( des_proto, src_proto, header->protocol_length );
424
            memcpy( des_proto, src_proto, header->protocol_length );
373
            memcpy( src_proto, proto->addr->value, header->protocol_length );
425
            memcpy( src_proto, proto->addr->value, header->protocol_length );
374
            memcpy( src_hw, des_hw, header->hardware_length );
426
            memcpy( src_hw, des_hw, header->hardware_length );
375
            memcpy( des_hw, hw_source->value, header->hardware_length );
427
            memcpy( des_hw, hw_source->value, header->hardware_length );
376
            packet_set_addr( packet, src_hw, des_hw, header->hardware_length );
428
            packet_set_addr( packet, src_hw, des_hw, header->hardware_length );
377
            nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
429
            nil_send_msg( device->phone, device_id, packet, SERVICE_ARP );
378
        }else{
430
        }else{
379
            pq_release( arp_globals.networking_phone, packet_get_id( packet ));
431
            pq_release( arp_globals.networking_phone, packet_get_id( packet ));
380
        }
432
        }
381
    }
433
    }
382
    return EOK;
434
    return EOK;
383
}
435
}
384
 
436
 
385
int arp_clear_device_message( device_id_t device_id ){
-
 
386
    arp_device_ref  device;
-
 
387
 
-
 
388
    rwlock_write_lock( & arp_globals.lock );
-
 
389
    device = arp_cache_find( & arp_globals.cache, device_id );
-
 
390
    if( ! device ){
-
 
391
        rwlock_write_unlock( & arp_globals.lock );
-
 
392
        return ENOENT;
-
 
393
    }
-
 
394
    clear_device( device );
-
 
395
    printf( "\nDevice %d cleared", device_id );
-
 
396
    rwlock_write_unlock( & arp_globals.lock );
-
 
397
    return EOK;
-
 
398
}
-
 
399
 
-
 
400
void clear_device( arp_device_ref device ){
437
void clear_device( arp_device_ref device ){
401
    int             count;
438
    int             count;
402
    arp_proto_ref   proto;
439
    arp_proto_ref   proto;
403
 
440
 
404
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
441
    for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
405
        proto = arp_protos_get_index( & device->protos, count );
442
        proto = arp_protos_get_index( & device->protos, count );
406
        if( proto ){
443
        if( proto ){
407
            if( proto->addr ) free( proto->addr );
444
            if( proto->addr ) free( proto->addr );
408
            if( proto->addr_data ) free( proto->addr_data );
445
            if( proto->addr_data ) free( proto->addr_data );
409
            arp_addr_destroy( & proto->addresses );
446
            arp_addr_destroy( & proto->addresses );
410
        }
447
        }
411
    }
448
    }
412
    arp_protos_clear( & device->protos );
449
    arp_protos_clear( & device->protos );
413
}
450
}
414
 
451
 
415
int arp_clean_cache_message( void ){
-
 
416
    int             count;
-
 
417
    arp_device_ref  device;
-
 
418
 
-
 
419
    rwlock_write_lock( & arp_globals.lock );
-
 
420
    for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){
-
 
421
        device = arp_cache_get_index( & arp_globals.cache, count );
-
 
422
        if( device ){
-
 
423
            clear_device( device );
-
 
424
            if( device->addr_data ) free( device->addr_data );
-
 
425
            if( device->broadcast_data ) free( device->broadcast_data );
-
 
426
        }
-
 
427
    }
-
 
428
    arp_cache_clear( & arp_globals.cache );
-
 
429
    rwlock_write_unlock( & arp_globals.lock );
-
 
430
    printf( "\nCache cleaned" );
-
 
431
    return EOK;
-
 
432
}
-
 
433
 
-
 
434
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
452
int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
435
    ERROR_DECLARE;
453
    ERROR_DECLARE;
436
 
454
 
437
    measured_string_ref address;
455
    measured_string_ref address;
438
    measured_string_ref translation;
456
    measured_string_ref translation;
439
    char *              data;
457
    char *              data;
440
 
458
 
441
//  printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
459
//  printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_ARP_FIRST );
442
    * answer_count = 0;
460
    * answer_count = 0;
443
    switch( IPC_GET_METHOD( * call )){
461
    switch( IPC_GET_METHOD( * call )){
444
        case IPC_M_PHONE_HUNGUP:
462
        case IPC_M_PHONE_HUNGUP:
445
            return EOK;
463
            return EOK;
446
        case NET_ARP_DEVICE:
464
        case NET_ARP_DEVICE:
447
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
465
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
448
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_PROTO( call ), address ))){
466
            if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_PROTO( call ), address ))){
449
                free( address );
467
                free( address );
450
                free( data );
468
                free( data );
451
            }
469
            }
452
            return ERROR_CODE;
470
            return ERROR_CODE;
453
        case NET_ARP_TRANSLATE:
471
        case NET_ARP_TRANSLATE:
454
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
472
            ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 ));
455
            rwlock_read_lock( & arp_globals.lock );
473
            rwlock_read_lock( & arp_globals.lock );
456
            translation = arp_translate_message( IPC_GET_DEVICE( call ), ARP_GET_PROTO( call ), address );
474
            translation = arp_translate_message( IPC_GET_DEVICE( call ), ARP_GET_PROTO( call ), address );
457
            free( address );
475
            free( address );
458
            free( data );
476
            free( data );
459
            if( ! translation ){
477
            if( ! translation ){
460
                rwlock_read_unlock( & arp_globals.lock );
478
                rwlock_read_unlock( & arp_globals.lock );
461
                return ENOENT;
479
                return ENOENT;
462
            }
480
            }
463
            ERROR_CODE = measured_strings_reply( translation, 1 );
481
            ERROR_CODE = measured_strings_reply( translation, 1 );
464
            rwlock_read_unlock( & arp_globals.lock );
482
            rwlock_read_unlock( & arp_globals.lock );
465
            return ERROR_CODE;
483
            return ERROR_CODE;
466
        case NET_ARP_CLEAR_DEVICE:
484
        case NET_ARP_CLEAR_DEVICE:
467
            return arp_clear_device_message( IPC_GET_DEVICE( call ));
485
            return arp_clear_device_wrapper( IPC_GET_DEVICE( call ));
468
        case NET_ARP_CLEAN_CACHE:
486
        case NET_ARP_CLEAN_CACHE:
469
            return arp_clean_cache_message();
487
            return arp_clean_cache_wrapper();
470
    }
488
    }
471
    return ENOTSUP;
489
    return ENOTSUP;
472
}
490
}
473
 
491
 
474
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
492
void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){
475
    ERROR_DECLARE;
493
    ERROR_DECLARE;
476
 
494
 
477
    packet_t        packet;
495
    packet_t        packet;
478
 
496
 
479
    while( true ){
497
    while( true ){
480
        switch( IPC_GET_METHOD( * icall )){
498
        switch( IPC_GET_METHOD( * icall )){
481
            case NET_IL_DEVICE_STATE:
499
            case NET_IL_DEVICE_STATE:
482
                // do nothing - keep the cache
500
                // do nothing - keep the cache
483
                ipc_answer_0( iid, EOK );
501
                ipc_answer_0( iid, EOK );
484
                break;
502
                break;
485
            case NET_IL_RECEIVED:
503
            case NET_IL_RECEIVED:
486
                if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
504
                if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){
487
                    rwlock_read_lock( & arp_globals.lock );
505
                    rwlock_read_lock( & arp_globals.lock );
488
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet );
506
                    ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet );
489
                    rwlock_read_unlock( & arp_globals.lock );
507
                    rwlock_read_unlock( & arp_globals.lock );
490
                }
508
                }
491
                ipc_answer_0( iid, ERROR_CODE );
509
                ipc_answer_0( iid, ERROR_CODE );
492
                break;
510
                break;
493
            default:
511
            default:
494
                ipc_answer_0( iid, ENOTSUP );
512
                ipc_answer_0( iid, ENOTSUP );
495
        }
513
        }
496
        iid = async_get_call( icall );
514
        iid = async_get_call( icall );
497
    }
515
    }
498
}
516
}
499
 
517
 
500
/** @}
518
/** @}
501
 */
519
 */
502
 
520