Rev 4396 | Rev 4558 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3846 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3846 | mejdrech | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup arp |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** @file |
||
3912 | mejdrech | 34 | * ARP module implementation. |
35 | * @see arp.h |
||
3846 | mejdrech | 36 | */ |
37 | |||
38 | #include <async.h> |
||
39 | #include <malloc.h> |
||
4192 | mejdrech | 40 | #include <mem.h> |
3991 | mejdrech | 41 | #include <rwlock.h> |
3846 | mejdrech | 42 | #include <stdio.h> |
4327 | mejdrech | 43 | #include <string.h> |
4307 | mejdrech | 44 | #include <task.h> |
3846 | mejdrech | 45 | |
46 | #include <ipc/ipc.h> |
||
47 | #include <ipc/services.h> |
||
48 | |||
3886 | mejdrech | 49 | #include "../../err.h" |
50 | #include "../../messages.h" |
||
51 | #include "../../modules.h" |
||
3846 | mejdrech | 52 | |
4243 | mejdrech | 53 | #include "../../include/byteorder.h" |
54 | #include "../../include/device.h" |
||
4307 | mejdrech | 55 | #include "../../include/arp_interface.h" |
56 | #include "../../include/nil_interface.h" |
||
3886 | mejdrech | 57 | #include "../../include/protocol_map.h" |
3846 | mejdrech | 58 | |
3886 | mejdrech | 59 | #include "../../structures/measured_strings.h" |
60 | #include "../../structures/packet/packet.h" |
||
3901 | mejdrech | 61 | #include "../../structures/packet/packet_client.h" |
3886 | mejdrech | 62 | |
4307 | mejdrech | 63 | #include "../il_messages.h" |
64 | |||
3846 | mejdrech | 65 | #include "arp.h" |
66 | #include "arp_header.h" |
||
67 | #include "arp_oc.h" |
||
68 | #include "arp_module.h" |
||
4307 | mejdrech | 69 | #include "arp_messages.h" |
3846 | mejdrech | 70 | |
3912 | mejdrech | 71 | /** ARP global data. |
72 | */ |
||
3846 | mejdrech | 73 | arp_globals_t arp_globals; |
74 | |||
3912 | mejdrech | 75 | /** Creates new protocol specific data. |
76 | * @param proto Protocol specific data. Output parameter. |
||
77 | * @param service Protocol module service. Input parameter. |
||
78 | * @param address Actual protocol device address. Input parameter. |
||
79 | * @returns EOK on success. |
||
80 | * @returns ENOMEM if there is not enough memory left. |
||
81 | */ |
||
82 | int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ); |
||
83 | |||
84 | /** Registers the device. |
||
85 | * Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists. |
||
86 | * @param device_id The device identifier. Input parameter. |
||
87 | * @param service The device driver service. Input parameter. |
||
88 | * @param protocol The protocol service. Input parameter. |
||
89 | * @param address The actual device protocol address. |
||
90 | * @returns EOK on success. |
||
91 | * @returns EEXIST if another device with the same device identifier and different driver service exists. |
||
92 | * @returns ENOMEM if there is not enough memory left. |
||
93 | * @returns Other error codes as defined for the measured_strings_return() function. |
||
94 | */ |
||
95 | int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ); |
||
96 | |||
97 | /** Returns the hardware address for the given protocol address. |
||
98 | * Sends the ARP request packet if the hardware address is not found in the cache. |
||
99 | * @param device_id The device identifier. Input parameter. |
||
100 | * @param protocol The protocol service. Input parameter. |
||
101 | * @param target The target protocol address. Input parameter. |
||
102 | * @returns The hardware address of the target. |
||
103 | * @returns NULL if the target parameter is NULL. |
||
104 | * @returns NULL if the device is not found. |
||
105 | * @returns NULL if the device packet is too small to send a request. |
||
106 | * @returns NULL if the hardware address is not found in the cache. |
||
107 | */ |
||
108 | measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ); |
||
109 | |||
110 | /** Processes the received ARP packet. |
||
111 | * Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address. |
||
112 | * Responses to the ARP request if the packet is the ARP request and is targeted to my address. |
||
113 | * @param device_id The source device identifier. Input parameter. |
||
114 | * @param packet The received packet. Input/output parameter. |
||
4394 | mejdrech | 115 | * @returns EOK on success and the packet is no longer needed. |
116 | * @returns 1 on success and the packet has been reused. |
||
3912 | mejdrech | 117 | * @returns EINVAL if the packet is too small to carry the ARP packet. |
118 | * @returns EINVAL if the received address lengths differs from the registered values. |
||
119 | * @returns ENOENT if the device is not found in the cache. |
||
120 | * @returns ENOENT if the protocol for the device is not found in the cache. |
||
121 | * @returns ENOMEM if there is not enough memory left. |
||
122 | */ |
||
123 | int arp_receive_message( device_id_t device_id, packet_t packet ); |
||
124 | |||
125 | /** Clears the device specific data. |
||
126 | * @param device The device specific data. |
||
127 | */ |
||
128 | void clear_device( arp_device_ref device ); |
||
129 | |||
3846 | mejdrech | 130 | DEVICE_MAP_IMPLEMENT( arp_cache, arp_device_t ) |
131 | |||
132 | INT_MAP_IMPLEMENT( arp_protos, arp_proto_t ) |
||
133 | |||
134 | GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t ) |
||
135 | |||
4501 | mejdrech | 136 | task_id_t arp_task_get_id( void ){ |
4307 | mejdrech | 137 | return task_get_id(); |
138 | } |
||
139 | |||
140 | int arp_clear_device_req( int arp_phone, device_id_t device_id ){ |
||
4271 | mejdrech | 141 | arp_device_ref device; |
142 | |||
143 | rwlock_write_lock( & arp_globals.lock ); |
||
144 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
145 | if( ! device ){ |
||
146 | rwlock_write_unlock( & arp_globals.lock ); |
||
147 | return ENOENT; |
||
148 | } |
||
149 | clear_device( device ); |
||
4307 | mejdrech | 150 | printf( "Device %d cleared\n", device_id ); |
4271 | mejdrech | 151 | rwlock_write_unlock( & arp_globals.lock ); |
152 | return EOK; |
||
153 | } |
||
154 | |||
4307 | mejdrech | 155 | int arp_clean_cache_req( int arp_phone ){ |
4271 | mejdrech | 156 | int count; |
157 | arp_device_ref device; |
||
158 | |||
159 | rwlock_write_lock( & arp_globals.lock ); |
||
160 | for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){ |
||
161 | device = arp_cache_get_index( & arp_globals.cache, count ); |
||
162 | if( device ){ |
||
163 | clear_device( device ); |
||
164 | if( device->addr_data ) free( device->addr_data ); |
||
165 | if( device->broadcast_data ) free( device->broadcast_data ); |
||
166 | } |
||
167 | } |
||
168 | arp_cache_clear( & arp_globals.cache ); |
||
169 | rwlock_write_unlock( & arp_globals.lock ); |
||
4307 | mejdrech | 170 | printf( "Cache cleaned\n" ); |
4271 | mejdrech | 171 | return EOK; |
172 | } |
||
173 | |||
4307 | mejdrech | 174 | int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){ |
4271 | mejdrech | 175 | ERROR_DECLARE; |
176 | |||
177 | measured_string_ref tmp; |
||
178 | |||
179 | tmp = measured_string_copy( address ); |
||
180 | if( ERROR_OCCURRED( arp_device_message( device_id, netif, protocol, tmp ))){ |
||
181 | free( tmp->value ); |
||
182 | free( tmp ); |
||
183 | } |
||
184 | return ERROR_CODE; |
||
185 | } |
||
186 | |||
4307 | mejdrech | 187 | int arp_translate_req( int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ){ |
4271 | mejdrech | 188 | measured_string_ref tmp; |
189 | |||
190 | rwlock_read_lock( & arp_globals.lock ); |
||
191 | tmp = arp_translate_message( device_id, protocol, address ); |
||
192 | if( tmp ){ |
||
193 | * translation = measured_string_copy( tmp ); |
||
194 | rwlock_read_unlock( & arp_globals.lock ); |
||
195 | if( * translation ){ |
||
196 | * data = ( ** translation ).value; |
||
197 | return EOK; |
||
198 | }else{ |
||
199 | return ENOMEM; |
||
200 | } |
||
201 | }else{ |
||
202 | rwlock_read_unlock( & arp_globals.lock ); |
||
203 | return ENOENT; |
||
204 | } |
||
205 | } |
||
206 | |||
4351 | mejdrech | 207 | int arp_initialize( async_client_conn_t client_connection ){ |
3991 | mejdrech | 208 | ERROR_DECLARE; |
209 | |||
210 | rwlock_initialize( & arp_globals.lock ); |
||
211 | rwlock_write_lock( & arp_globals.lock ); |
||
4351 | mejdrech | 212 | arp_globals.client_connection = client_connection; |
3991 | mejdrech | 213 | ERROR_PROPAGATE( arp_cache_initialize( & arp_globals.cache )); |
214 | rwlock_write_unlock( & arp_globals.lock ); |
||
215 | return EOK; |
||
3846 | mejdrech | 216 | } |
217 | |||
218 | int arp_proto_create( arp_proto_ref * proto, services_t service, measured_string_ref address ){ |
||
219 | ERROR_DECLARE; |
||
220 | |||
221 | * proto = ( arp_proto_ref ) malloc( sizeof( arp_proto_t )); |
||
222 | if( !( * proto )) return ENOMEM; |
||
223 | ( ** proto ).service = service; |
||
224 | ( ** proto ).addr = address; |
||
225 | ( ** proto ).addr_data = address->value; |
||
3912 | mejdrech | 226 | if( ERROR_OCCURRED( arp_addr_initialize( &( ** proto).addresses ))){ |
3846 | mejdrech | 227 | free( * proto ); |
228 | return ERROR_CODE; |
||
229 | } |
||
230 | return EOK; |
||
231 | } |
||
232 | |||
233 | int arp_device_message( device_id_t device_id, services_t service, services_t protocol, measured_string_ref address ){ |
||
234 | ERROR_DECLARE; |
||
235 | |||
236 | arp_device_ref device; |
||
237 | arp_proto_ref proto; |
||
4192 | mejdrech | 238 | int index; |
3846 | mejdrech | 239 | |
3991 | mejdrech | 240 | rwlock_write_lock( & arp_globals.lock ); |
3846 | mejdrech | 241 | // an existing device? |
242 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
243 | if( device ){ |
||
3991 | mejdrech | 244 | if( device->service != service ){ |
4307 | mejdrech | 245 | printf( "Device %d already exists\n", device->device_id ); |
3991 | mejdrech | 246 | rwlock_write_unlock( & arp_globals.lock ); |
247 | return EEXIST; |
||
248 | } |
||
3846 | mejdrech | 249 | proto = arp_protos_find( & device->protos, protocol ); |
250 | if( proto ){ |
||
251 | free( proto->addr ); |
||
252 | free( proto->addr_data ); |
||
253 | proto->addr = address; |
||
254 | proto->addr_data = address->value; |
||
255 | }else{ |
||
3991 | mejdrech | 256 | if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
257 | rwlock_write_unlock( & arp_globals.lock ); |
||
258 | return ERROR_CODE; |
||
259 | } |
||
4192 | mejdrech | 260 | index = arp_protos_add( & device->protos, proto->service, proto ); |
261 | if( index < 0 ){ |
||
3991 | mejdrech | 262 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 263 | free( proto ); |
4261 | mejdrech | 264 | return index; |
3846 | mejdrech | 265 | } |
4332 | mejdrech | 266 | printf( "New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol ); |
3846 | mejdrech | 267 | } |
268 | }else{ |
||
4243 | mejdrech | 269 | index = hardware_map( service ); |
4192 | mejdrech | 270 | if( ! index ) return ENOENT; |
3846 | mejdrech | 271 | // create a new device |
272 | device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); |
||
3991 | mejdrech | 273 | if( ! device ){ |
274 | rwlock_write_unlock( & arp_globals.lock ); |
||
275 | return ENOMEM; |
||
276 | } |
||
4192 | mejdrech | 277 | device->hardware = index; |
3846 | mejdrech | 278 | device->device_id = device_id; |
3912 | mejdrech | 279 | if( ERROR_OCCURRED( arp_protos_initialize( & device->protos )) |
280 | || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
||
3991 | mejdrech | 281 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 282 | free( device ); |
283 | return ERROR_CODE; |
||
284 | } |
||
4192 | mejdrech | 285 | index = arp_protos_add( & device->protos, proto->service, proto ); |
286 | if( index < 0 ){ |
||
3991 | mejdrech | 287 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 288 | arp_protos_destroy( & device->protos ); |
289 | free( device ); |
||
4192 | mejdrech | 290 | return index; |
3846 | mejdrech | 291 | } |
292 | device->service = service; |
||
293 | // bind the new one |
||
4351 | mejdrech | 294 | device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_globals.client_connection ); |
4192 | mejdrech | 295 | if( device->phone < 0 ){ |
296 | rwlock_write_unlock( & arp_globals.lock ); |
||
297 | arp_protos_destroy( & device->protos ); |
||
298 | free( device ); |
||
299 | return EREFUSED; |
||
300 | } |
||
3846 | mejdrech | 301 | // get packet dimensions |
4261 | mejdrech | 302 | if( ERROR_OCCURRED( nil_packet_size_req( device->phone, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){ |
3991 | mejdrech | 303 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 304 | arp_protos_destroy( & device->protos ); |
305 | free( device ); |
||
306 | return ERROR_CODE; |
||
307 | } |
||
308 | // get hardware address |
||
4261 | mejdrech | 309 | if( ERROR_OCCURRED( nil_get_addr( device->phone, device_id, & device->addr, & device->addr_data ))){ |
3991 | mejdrech | 310 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 311 | arp_protos_destroy( & device->protos ); |
312 | free( device ); |
||
313 | return ERROR_CODE; |
||
314 | } |
||
315 | // get broadcast address |
||
4261 | mejdrech | 316 | if( ERROR_OCCURRED( nil_get_broadcast_addr( device->phone, device_id, & device->broadcast_addr, & device->broadcast_data ))){ |
3991 | mejdrech | 317 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 318 | free( device->addr ); |
319 | free( device->addr_data ); |
||
320 | arp_protos_destroy( & device->protos ); |
||
321 | free( device ); |
||
322 | return ERROR_CODE; |
||
323 | } |
||
4261 | mejdrech | 324 | if( ERROR_OCCURRED( arp_cache_add( & arp_globals.cache, device->device_id, device ))){ |
3991 | mejdrech | 325 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 326 | free( device->addr ); |
327 | free( device->addr_data ); |
||
328 | free( device->broadcast_addr ); |
||
329 | free( device->broadcast_data ); |
||
330 | arp_protos_destroy( & device->protos ); |
||
331 | free( device ); |
||
332 | return ERROR_CODE; |
||
333 | } |
||
4332 | mejdrech | 334 | printf( "New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n\tproto\t= %d\n", device->device_id, device->hardware, device->service, protocol ); |
3846 | mejdrech | 335 | } |
3991 | mejdrech | 336 | rwlock_write_unlock( & arp_globals.lock ); |
3846 | mejdrech | 337 | return EOK; |
338 | } |
||
339 | |||
340 | measured_string_ref arp_translate_message( device_id_t device_id, services_t protocol, measured_string_ref target ){ |
||
341 | arp_device_ref device; |
||
342 | arp_proto_ref proto; |
||
343 | measured_string_ref addr; |
||
344 | size_t length; |
||
345 | packet_t packet; |
||
346 | arp_header_ref header; |
||
347 | |||
348 | if( ! target ) return NULL; |
||
349 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
4243 | mejdrech | 350 | if( ! device ) return NULL; |
3846 | mejdrech | 351 | proto = arp_protos_find( & device->protos, protocol ); |
4243 | mejdrech | 352 | if(( ! proto ) || ( proto->addr->length != target->length )) return NULL; |
3846 | mejdrech | 353 | addr = arp_addr_find( & proto->addresses, target->value, target->length ); |
4243 | mejdrech | 354 | if( addr ) return addr; |
3846 | mejdrech | 355 | // ARP packet content size = header + ( address + translation ) * 2 |
356 | length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2; |
||
4243 | mejdrech | 357 | if( length > device->content ) return NULL; |
4307 | mejdrech | 358 | packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix ); |
4243 | mejdrech | 359 | if( ! packet ) return NULL; |
3912 | mejdrech | 360 | header = ( arp_header_ref ) packet_suffix( packet, length ); |
4394 | mejdrech | 361 | if( ! header ){ |
4396 | mejdrech | 362 | pq_release( arp_globals.net_phone, packet_get_id( packet )); |
4394 | mejdrech | 363 | return NULL; |
364 | } |
||
4243 | mejdrech | 365 | header->hardware = htons( device->hardware ); |
3846 | mejdrech | 366 | header->hardware_length = device->addr->length; |
4243 | mejdrech | 367 | header->protocol = htons( protocol_map( device->service, protocol )); |
3846 | mejdrech | 368 | header->protocol_length = proto->addr->length; |
4243 | mejdrech | 369 | header->operation = htons( ARPOP_REQUEST ); |
3846 | mejdrech | 370 | length = sizeof( arp_header_t ); |
371 | memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length ); |
||
372 | length += device->addr->length; |
||
373 | memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length ); |
||
374 | length += proto->addr->length; |
||
4192 | mejdrech | 375 | bzero((( uint8_t * ) header ) + length, device->addr->length ); |
3846 | mejdrech | 376 | length += device->addr->length; |
377 | memcpy((( uint8_t * ) header ) + length, target->value, target->length ); |
||
4394 | mejdrech | 378 | if( packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )) != EOK ){ |
4396 | mejdrech | 379 | pq_release( arp_globals.net_phone, packet_get_id( packet )); |
4394 | mejdrech | 380 | return NULL; |
381 | } |
||
4261 | mejdrech | 382 | nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); |
3846 | mejdrech | 383 | return NULL; |
384 | } |
||
385 | |||
386 | int arp_receive_message( device_id_t device_id, packet_t packet ){ |
||
387 | ERROR_DECLARE; |
||
388 | |||
389 | size_t length; |
||
390 | arp_header_ref header; |
||
391 | arp_device_ref device; |
||
392 | arp_proto_ref proto; |
||
393 | measured_string_ref hw_source; |
||
3991 | mejdrech | 394 | uint8_t * src_hw; |
3912 | mejdrech | 395 | uint8_t * src_proto; |
396 | uint8_t * des_hw; |
||
397 | uint8_t * des_proto; |
||
3846 | mejdrech | 398 | |
399 | length = packet_get_data_length( packet ); |
||
400 | if( length <= sizeof( arp_header_t )) return EINVAL; |
||
401 | device = arp_cache_find( & arp_globals.cache, device_id ); |
||
4243 | mejdrech | 402 | if( ! device ) return ENOENT; |
3846 | mejdrech | 403 | header = ( arp_header_ref ) packet_get_data( packet ); |
4243 | mejdrech | 404 | if(( ntohs( header->hardware ) != device->hardware ) |
3991 | mejdrech | 405 | || ( length < sizeof( arp_header_t ) + ( header->hardware_length + header->protocol_length ) * 2 )){ |
406 | return EINVAL; |
||
407 | } |
||
4243 | mejdrech | 408 | proto = arp_protos_find( & device->protos, protocol_unmap( device->service, ntohs( header->protocol ))); |
409 | if( ! proto ) return ENOENT; |
||
3912 | mejdrech | 410 | src_hw = (( uint8_t * ) header ) + sizeof( arp_header_t ); |
3846 | mejdrech | 411 | src_proto = src_hw + header->hardware_length; |
412 | des_hw = src_proto + header->protocol_length; |
||
413 | des_proto = des_hw + header->hardware_length; |
||
3912 | mejdrech | 414 | hw_source = arp_addr_find( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length )); |
3846 | mejdrech | 415 | // exists? |
416 | if( hw_source ){ |
||
3991 | mejdrech | 417 | if( hw_source->length != CONVERT_SIZE( uint8_t, char, header->hardware_length )){ |
418 | return EINVAL; |
||
419 | } |
||
3912 | mejdrech | 420 | memcpy( hw_source->value, src_hw, hw_source->length ); |
3846 | mejdrech | 421 | } |
422 | // is my protocol address? |
||
4332 | mejdrech | 423 | if( proto->addr->length != CONVERT_SIZE( uint8_t, char, header->protocol_length )){ |
3991 | mejdrech | 424 | return EINVAL; |
3846 | mejdrech | 425 | } |
4327 | mejdrech | 426 | if( ! str_lcmp( proto->addr->value, ( char * ) des_proto, proto->addr->length )){ |
3846 | mejdrech | 427 | // not already upadted? |
428 | if( ! hw_source ){ |
||
3912 | mejdrech | 429 | hw_source = measured_string_create_bulk(( char * ) src_hw, CONVERT_SIZE( uint8_t, char, header->hardware_length )); |
4243 | mejdrech | 430 | if( ! hw_source ) return ENOMEM; |
431 | ERROR_PROPAGATE( arp_addr_add( & proto->addresses, ( char * ) src_proto, CONVERT_SIZE( uint8_t, char, header->protocol_length ), hw_source )); |
||
3846 | mejdrech | 432 | } |
4243 | mejdrech | 433 | if( ntohs( header->operation ) == ARPOP_REQUEST ){ |
434 | header->operation = htons( ARPOP_REPLY ); |
||
3991 | mejdrech | 435 | memcpy( des_proto, src_proto, header->protocol_length ); |
3846 | mejdrech | 436 | memcpy( src_proto, proto->addr->value, header->protocol_length ); |
4332 | mejdrech | 437 | memcpy( src_hw, device->addr->value, device->addr_len ); |
3912 | mejdrech | 438 | memcpy( des_hw, hw_source->value, header->hardware_length ); |
4394 | mejdrech | 439 | ERROR_PROPAGATE( packet_set_addr( packet, src_hw, des_hw, header->hardware_length )); |
4261 | mejdrech | 440 | nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); |
4394 | mejdrech | 441 | return 1; |
3846 | mejdrech | 442 | } |
443 | } |
||
444 | return EOK; |
||
445 | } |
||
446 | |||
447 | void clear_device( arp_device_ref device ){ |
||
448 | int count; |
||
449 | arp_proto_ref proto; |
||
450 | |||
4192 | mejdrech | 451 | for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){ |
3846 | mejdrech | 452 | proto = arp_protos_get_index( & device->protos, count ); |
4192 | mejdrech | 453 | if( proto ){ |
454 | if( proto->addr ) free( proto->addr ); |
||
455 | if( proto->addr_data ) free( proto->addr_data ); |
||
456 | arp_addr_destroy( & proto->addresses ); |
||
457 | } |
||
3846 | mejdrech | 458 | } |
459 | arp_protos_clear( & device->protos ); |
||
460 | } |
||
461 | |||
4307 | mejdrech | 462 | int arp_connect_module( services_t service ){ |
463 | return EOK; |
||
464 | } |
||
465 | |||
3846 | mejdrech | 466 | int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
467 | ERROR_DECLARE; |
||
468 | |||
469 | measured_string_ref address; |
||
470 | measured_string_ref translation; |
||
471 | char * data; |
||
4351 | mejdrech | 472 | packet_t packet; |
4394 | mejdrech | 473 | packet_t next; |
3846 | mejdrech | 474 | |
4307 | mejdrech | 475 | // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST ); |
3846 | mejdrech | 476 | * answer_count = 0; |
477 | switch( IPC_GET_METHOD( * call )){ |
||
478 | case IPC_M_PHONE_HUNGUP: |
||
479 | return EOK; |
||
480 | case NET_ARP_DEVICE: |
||
481 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
||
4307 | mejdrech | 482 | if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){ |
3846 | mejdrech | 483 | free( address ); |
484 | free( data ); |
||
485 | } |
||
486 | return ERROR_CODE; |
||
487 | case NET_ARP_TRANSLATE: |
||
488 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
||
4243 | mejdrech | 489 | rwlock_read_lock( & arp_globals.lock ); |
4307 | mejdrech | 490 | translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address ); |
3846 | mejdrech | 491 | free( address ); |
492 | free( data ); |
||
4243 | mejdrech | 493 | if( ! translation ){ |
494 | rwlock_read_unlock( & arp_globals.lock ); |
||
495 | return ENOENT; |
||
496 | } |
||
497 | ERROR_CODE = measured_strings_reply( translation, 1 ); |
||
498 | rwlock_read_unlock( & arp_globals.lock ); |
||
499 | return ERROR_CODE; |
||
3846 | mejdrech | 500 | case NET_ARP_CLEAR_DEVICE: |
4307 | mejdrech | 501 | return arp_clear_device_req( 0, IPC_GET_DEVICE( call )); |
3846 | mejdrech | 502 | case NET_ARP_CLEAN_CACHE: |
4307 | mejdrech | 503 | return arp_clean_cache_req( 0 ); |
4351 | mejdrech | 504 | case NET_IL_DEVICE_STATE: |
505 | // do nothing - keep the cache |
||
506 | return EOK; |
||
507 | case NET_IL_RECEIVED: |
||
508 | if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
||
509 | rwlock_read_lock( & arp_globals.lock ); |
||
4394 | mejdrech | 510 | do{ |
4501 | mejdrech | 511 | next = pq_detach( packet ); |
4394 | mejdrech | 512 | ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( call ), packet ); |
513 | if( ERROR_CODE != 1 ) pq_release( arp_globals.net_phone, packet_get_id( packet )); |
||
514 | packet = next; |
||
515 | }while( packet ); |
||
4351 | mejdrech | 516 | rwlock_read_unlock( & arp_globals.lock ); |
517 | } |
||
518 | return ERROR_CODE; |
||
3846 | mejdrech | 519 | } |
520 | return ENOTSUP; |
||
521 | } |
||
522 | |||
523 | /** @} |
||
524 | */ |