Rev 4163 | Rev 4243 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4163 | Rev 4192 | ||
---|---|---|---|
Line 35... | Line 35... | ||
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 <rwlock.h> |
41 | #include <rwlock.h> |
41 | #include <stdio.h> |
42 | #include <stdio.h> |
42 | #include <string.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" |
Line 190... | Line 190... | ||
190 | arp_device_ref device; |
190 | arp_device_ref device; |
191 | aid_t message; |
191 | aid_t message; |
192 | ipc_call_t answer; |
192 | ipc_call_t answer; |
193 | ipcarg_t result; |
193 | ipcarg_t result; |
194 | arp_proto_ref proto; |
194 | arp_proto_ref proto; |
- | 195 | int index; |
|
195 | 196 | ||
196 | rwlock_write_lock( & arp_globals.lock ); |
197 | rwlock_write_lock( & arp_globals.lock ); |
197 | // an existing device? |
198 | // an existing device? |
198 | device = arp_cache_find( & arp_globals.cache, device_id ); |
199 | device = arp_cache_find( & arp_globals.cache, device_id ); |
199 | if( device ){ |
200 | if( device ){ |
Line 211... | Line 212... | ||
211 | }else{ |
212 | }else{ |
212 | if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
213 | if( ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
213 | rwlock_write_unlock( & arp_globals.lock ); |
214 | rwlock_write_unlock( & arp_globals.lock ); |
214 | return ERROR_CODE; |
215 | return ERROR_CODE; |
215 | } |
216 | } |
216 | if( ERROR_OCCURRED( arp_protos_add( & device->protos, proto->service, proto ))){ |
217 | index = arp_protos_add( & device->protos, proto->service, proto ); |
- | 218 | if( index < 0 ){ |
|
217 | rwlock_write_unlock( & arp_globals.lock ); |
219 | rwlock_write_unlock( & arp_globals.lock ); |
218 | free( proto ); |
220 | free( proto ); |
219 | return ERROR_CODE; |
221 | return result; |
220 | } |
222 | } |
221 | } |
223 | } |
222 | printf( "\nCache of the existing device %d cleaned", device->device_id ); |
224 | printf( "\nCache of the existing device %d cleaned", device->device_id ); |
223 | }else{ |
225 | }else{ |
- | 226 | index = protocol_map( service, protocol ); |
|
- | 227 | if( ! index ) return ENOENT; |
|
224 | // create a new device |
228 | // create a new device |
225 | device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); |
229 | device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); |
226 | if( ! device ){ |
230 | if( ! device ){ |
227 | rwlock_write_unlock( & arp_globals.lock ); |
231 | rwlock_write_unlock( & arp_globals.lock ); |
228 | return ENOMEM; |
232 | return ENOMEM; |
229 | } |
233 | } |
- | 234 | device->hardware = index; |
|
230 | device->device_id = device_id; |
235 | device->device_id = device_id; |
231 | if( ERROR_OCCURRED( arp_protos_initialize( & device->protos )) |
236 | if( ERROR_OCCURRED( arp_protos_initialize( & device->protos )) |
232 | || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
237 | || ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){ |
233 | rwlock_write_unlock( & arp_globals.lock ); |
238 | rwlock_write_unlock( & arp_globals.lock ); |
234 | free( device ); |
239 | free( device ); |
235 | return ERROR_CODE; |
240 | return ERROR_CODE; |
236 | } |
241 | } |
237 | if( ERROR_OCCURRED( arp_protos_add( & device->protos, proto->service, proto ))){ |
242 | index = arp_protos_add( & device->protos, proto->service, proto ); |
- | 243 | if( index < 0 ){ |
|
238 | rwlock_write_unlock( & arp_globals.lock ); |
244 | rwlock_write_unlock( & arp_globals.lock ); |
239 | arp_protos_destroy( & device->protos ); |
245 | arp_protos_destroy( & device->protos ); |
240 | free( device ); |
246 | free( device ); |
241 | return ERROR_CODE; |
247 | return index; |
242 | } |
248 | } |
243 | device->service = service; |
249 | device->service = service; |
244 | // bind the new one |
250 | // bind the new one |
245 | device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver ); |
251 | device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver ); |
- | 252 | if( device->phone < 0 ){ |
|
- | 253 | rwlock_write_unlock( & arp_globals.lock ); |
|
- | 254 | arp_protos_destroy( & device->protos ); |
|
- | 255 | free( device ); |
|
- | 256 | return EREFUSED; |
|
- | 257 | } |
|
246 | // get packet dimensions |
258 | // get packet dimensions |
247 | if( ERROR_OCCURRED( async_req_1_4( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){ |
259 | if( ERROR_OCCURRED( async_req_1_4( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){ |
248 | rwlock_write_unlock( & arp_globals.lock ); |
260 | rwlock_write_unlock( & arp_globals.lock ); |
249 | arp_protos_destroy( & device->protos ); |
261 | arp_protos_destroy( & device->protos ); |
250 | free( device ); |
262 | free( device ); |
Line 290... | Line 302... | ||
290 | free( device->broadcast_data ); |
302 | free( device->broadcast_data ); |
291 | arp_protos_destroy( & device->protos ); |
303 | arp_protos_destroy( & device->protos ); |
292 | free( device ); |
304 | free( device ); |
293 | return ERROR_CODE; |
305 | return ERROR_CODE; |
294 | } |
306 | } |
295 | printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= %x\n\tservice\t= %d", device->device_id, device->hardware, device->service ); |
307 | printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d", device->device_id, device->hardware, device->service ); |
296 | } |
308 | } |
297 | rwlock_write_unlock( & arp_globals.lock ); |
309 | rwlock_write_unlock( & arp_globals.lock ); |
298 | return EOK; |
310 | return EOK; |
299 | } |
311 | } |
300 | 312 | ||
Line 343... | Line 355... | ||
343 | length = sizeof( arp_header_t ); |
355 | length = sizeof( arp_header_t ); |
344 | memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length ); |
356 | memcpy((( uint8_t * ) header ) + length, device->addr->value, device->addr->length ); |
345 | length += device->addr->length; |
357 | length += device->addr->length; |
346 | memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length ); |
358 | memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length ); |
347 | length += proto->addr->length; |
359 | length += proto->addr->length; |
348 | memset((( uint8_t * ) header ) + length, 0, device->addr->length ); |
360 | bzero((( uint8_t * ) header ) + length, device->addr->length ); |
349 | length += device->addr->length; |
361 | length += device->addr->length; |
350 | memcpy((( uint8_t * ) header ) + length, target->value, target->length ); |
362 | memcpy((( uint8_t * ) header ) + length, target->value, target->length ); |
351 | packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )); |
363 | packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length )); |
352 | async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet )); |
364 | async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet )); |
353 | rwlock_read_unlock( & arp_globals.lock ); |
365 | rwlock_read_unlock( & arp_globals.lock ); |
Line 426... | Line 438... | ||
426 | packet_set_addr( packet, src_hw, des_hw, header->hardware_length ); |
438 | packet_set_addr( packet, src_hw, des_hw, header->hardware_length ); |
427 | async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet )); |
439 | async_msg_3( device->phone, NET_NETIF_SEND, device_id, SERVICE_ARP, packet_get_id( packet )); |
428 | rwlock_read_unlock( & arp_globals.lock ); |
440 | rwlock_read_unlock( & arp_globals.lock ); |
429 | }else{ |
441 | }else{ |
430 | rwlock_read_unlock( & arp_globals.lock ); |
442 | rwlock_read_unlock( & arp_globals.lock ); |
431 | packet_release( arp_globals.networking_phone, packet_get_id( packet )); |
443 | pq_release( arp_globals.networking_phone, packet_get_id( packet )); |
432 | } |
444 | } |
433 | } |
445 | } |
434 | return EOK; |
446 | return EOK; |
435 | } |
447 | } |
436 | 448 | ||
Line 451... | Line 463... | ||
451 | 463 | ||
452 | void clear_device( arp_device_ref device ){ |
464 | void clear_device( arp_device_ref device ){ |
453 | int count; |
465 | int count; |
454 | arp_proto_ref proto; |
466 | arp_proto_ref proto; |
455 | 467 | ||
456 | count = arp_protos_count( & device->protos ); |
468 | for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){ |
457 | while( count > 0 ){ |
- | |
458 | proto = arp_protos_get_index( & device->protos, count ); |
469 | proto = arp_protos_get_index( & device->protos, count ); |
- | 470 | if( proto ){ |
|
459 | if( proto->addr ) free( proto->addr ); |
471 | if( proto->addr ) free( proto->addr ); |
460 | if( proto->addr_data ) free( proto->addr_data ); |
472 | if( proto->addr_data ) free( proto->addr_data ); |
461 | arp_addr_destroy( & proto->addresses ); |
473 | arp_addr_destroy( & proto->addresses ); |
462 | -- count; |
474 | } |
463 | } |
475 | } |
464 | arp_protos_clear( & device->protos ); |
476 | arp_protos_clear( & device->protos ); |
465 | } |
477 | } |
466 | 478 | ||
467 | int arp_clean_cache_message( void ){ |
479 | int arp_clean_cache_message( void ){ |
468 | int count; |
480 | int count; |
469 | arp_device_ref device; |
481 | arp_device_ref device; |
470 | 482 | ||
471 | rwlock_write_lock( & arp_globals.lock ); |
483 | rwlock_write_lock( & arp_globals.lock ); |
472 | count = arp_cache_count( & arp_globals.cache ); |
484 | for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){ |
473 | while( count > 0 ){ |
- | |
474 | device = arp_cache_get_index( & arp_globals.cache, count ); |
485 | device = arp_cache_get_index( & arp_globals.cache, count ); |
475 | if( device ){ |
486 | if( device ){ |
476 | clear_device( device ); |
487 | clear_device( device ); |
477 | if( device->addr_data ) free( device->addr_data ); |
488 | if( device->addr_data ) free( device->addr_data ); |
478 | if( device->broadcast_data ) free( device->broadcast_data ); |
489 | if( device->broadcast_data ) free( device->broadcast_data ); |