Rev 4271 | Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4271 | Rev 4307 | ||
---|---|---|---|
Line 38... | Line 38... | ||
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 | #include <task.h> |
|
43 | 44 | ||
44 | #include <ipc/ipc.h> |
45 | #include <ipc/ipc.h> |
45 | #include <ipc/services.h> |
46 | #include <ipc/services.h> |
46 | 47 | ||
47 | #include "../../err.h" |
48 | #include "../../err.h" |
48 | #include "../../messages.h" |
49 | #include "../../messages.h" |
49 | #include "../../modules.h" |
50 | #include "../../modules.h" |
50 | 51 | ||
51 | #include "../../include/byteorder.h" |
52 | #include "../../include/byteorder.h" |
52 | #include "../../include/device.h" |
53 | #include "../../include/device.h" |
53 | #include "../../include/arp_messages.h" |
54 | #include "../../include/arp_interface.h" |
54 | #include "../../include/nil_messages.h" |
55 | #include "../../include/nil_interface.h" |
55 | #include "../../include/protocol_map.h" |
56 | #include "../../include/protocol_map.h" |
56 | 57 | ||
57 | #include "../../structures/measured_strings.h" |
58 | #include "../../structures/measured_strings.h" |
58 | #include "../../structures/packet/packet.h" |
59 | #include "../../structures/packet/packet.h" |
59 | #include "../../structures/packet/packet_client.h" |
60 | #include "../../structures/packet/packet_client.h" |
60 | 61 | ||
- | 62 | #include "../il_messages.h" |
|
- | 63 | ||
61 | #include "arp.h" |
64 | #include "arp.h" |
62 | #include "arp_header.h" |
65 | #include "arp_header.h" |
63 | #include "arp_oc.h" |
66 | #include "arp_oc.h" |
64 | #include "arp_module.h" |
67 | #include "arp_module.h" |
65 | #include "arp_wrappers.h" |
68 | #include "arp_messages.h" |
66 | 69 | ||
67 | /** ARP global data. |
70 | /** ARP global data. |
68 | */ |
71 | */ |
69 | arp_globals_t arp_globals; |
72 | arp_globals_t arp_globals; |
70 | 73 | ||
- | 74 | int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ); |
|
- | 75 | 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 ); |
|
- | 76 | ||
- | 77 | /** Clears the whole cache. |
|
- | 78 | * @returns EOK on success. |
|
- | 79 | */ |
|
- | 80 | int arp_clean_cache_req( int arp_phone ); |
|
- | 81 | ||
- | 82 | /** Clears the device specific data from the cache. |
|
- | 83 | * @param device_id The device identifier. Input parameter. |
|
- | 84 | * @returns EOK on success. |
|
- | 85 | * @returns ENOENT if the device is not found in the cache. |
|
- | 86 | */ |
|
- | 87 | int arp_clear_device_req( int arp_phone, device_id_t device_id ); |
|
- | 88 | ||
71 | /** Creates new protocol specific data. |
89 | /** Creates new protocol specific data. |
72 | * @param proto Protocol specific data. Output parameter. |
90 | * @param proto Protocol specific data. Output parameter. |
73 | * @param service Protocol module service. Input parameter. |
91 | * @param service Protocol module service. Input parameter. |
74 | * @param address Actual protocol device address. Input parameter. |
92 | * @param address Actual protocol device address. Input parameter. |
75 | * @returns EOK on success. |
93 | * @returns EOK on success. |
Line 132... | Line 150... | ||
132 | 150 | ||
133 | INT_MAP_IMPLEMENT( arp_protos, arp_proto_t ) |
151 | INT_MAP_IMPLEMENT( arp_protos, arp_proto_t ) |
134 | 152 | ||
135 | GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t ) |
153 | GENERIC_CHAR_MAP_IMPLEMENT( arp_addr, measured_string_t ) |
136 | 154 | ||
- | 155 | int arp_task_get_id( void ){ |
|
- | 156 | return task_get_id(); |
|
- | 157 | } |
|
- | 158 | ||
137 | int arp_clear_device_wrapper( device_id_t device_id ){ |
159 | int arp_clear_device_req( int arp_phone, device_id_t device_id ){ |
138 | arp_device_ref device; |
160 | arp_device_ref device; |
139 | 161 | ||
140 | rwlock_write_lock( & arp_globals.lock ); |
162 | rwlock_write_lock( & arp_globals.lock ); |
141 | device = arp_cache_find( & arp_globals.cache, device_id ); |
163 | device = arp_cache_find( & arp_globals.cache, device_id ); |
142 | if( ! device ){ |
164 | if( ! device ){ |
143 | rwlock_write_unlock( & arp_globals.lock ); |
165 | rwlock_write_unlock( & arp_globals.lock ); |
144 | return ENOENT; |
166 | return ENOENT; |
145 | } |
167 | } |
146 | clear_device( device ); |
168 | clear_device( device ); |
147 | printf( "\nDevice %d cleared", device_id ); |
169 | printf( "Device %d cleared\n", device_id ); |
148 | rwlock_write_unlock( & arp_globals.lock ); |
170 | rwlock_write_unlock( & arp_globals.lock ); |
149 | return EOK; |
171 | return EOK; |
150 | } |
172 | } |
151 | 173 | ||
152 | int arp_clean_cache_wrapper( void ){ |
174 | int arp_clean_cache_req( int arp_phone ){ |
153 | int count; |
175 | int count; |
154 | arp_device_ref device; |
176 | arp_device_ref device; |
155 | 177 | ||
156 | rwlock_write_lock( & arp_globals.lock ); |
178 | rwlock_write_lock( & arp_globals.lock ); |
157 | for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){ |
179 | for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){ |
Line 162... | Line 184... | ||
162 | if( device->broadcast_data ) free( device->broadcast_data ); |
184 | if( device->broadcast_data ) free( device->broadcast_data ); |
163 | } |
185 | } |
164 | } |
186 | } |
165 | arp_cache_clear( & arp_globals.cache ); |
187 | arp_cache_clear( & arp_globals.cache ); |
166 | rwlock_write_unlock( & arp_globals.lock ); |
188 | rwlock_write_unlock( & arp_globals.lock ); |
167 | printf( "\nCache cleaned" ); |
189 | printf( "Cache cleaned\n" ); |
168 | return EOK; |
190 | return EOK; |
169 | } |
191 | } |
170 | 192 | ||
171 | int arp_device_wrapper( device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){ |
193 | int arp_device_req( int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address ){ |
172 | ERROR_DECLARE; |
194 | ERROR_DECLARE; |
173 | 195 | ||
174 | measured_string_ref tmp; |
196 | measured_string_ref tmp; |
175 | 197 | ||
176 | tmp = measured_string_copy( address ); |
198 | tmp = measured_string_copy( address ); |
Line 179... | Line 201... | ||
179 | free( tmp ); |
201 | free( tmp ); |
180 | } |
202 | } |
181 | return ERROR_CODE; |
203 | return ERROR_CODE; |
182 | } |
204 | } |
183 | 205 | ||
184 | int arp_translate_wrapper( device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data ){ |
206 | 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 ){ |
185 | measured_string_ref tmp; |
207 | measured_string_ref tmp; |
186 | 208 | ||
187 | rwlock_read_lock( & arp_globals.lock ); |
209 | rwlock_read_lock( & arp_globals.lock ); |
188 | tmp = arp_translate_message( device_id, protocol, address ); |
210 | tmp = arp_translate_message( device_id, protocol, address ); |
189 | if( tmp ){ |
211 | if( tmp ){ |
Line 236... | Line 258... | ||
236 | rwlock_write_lock( & arp_globals.lock ); |
258 | rwlock_write_lock( & arp_globals.lock ); |
237 | // an existing device? |
259 | // an existing device? |
238 | device = arp_cache_find( & arp_globals.cache, device_id ); |
260 | device = arp_cache_find( & arp_globals.cache, device_id ); |
239 | if( device ){ |
261 | if( device ){ |
240 | if( device->service != service ){ |
262 | if( device->service != service ){ |
241 | printf( "\nDevice %d already exists", device->device_id ); |
263 | printf( "Device %d already exists\n", device->device_id ); |
242 | rwlock_write_unlock( & arp_globals.lock ); |
264 | rwlock_write_unlock( & arp_globals.lock ); |
243 | return EEXIST; |
265 | return EEXIST; |
244 | } |
266 | } |
245 | proto = arp_protos_find( & device->protos, protocol ); |
267 | proto = arp_protos_find( & device->protos, protocol ); |
246 | if( proto ){ |
268 | if( proto ){ |
Line 258... | Line 280... | ||
258 | rwlock_write_unlock( & arp_globals.lock ); |
280 | rwlock_write_unlock( & arp_globals.lock ); |
259 | free( proto ); |
281 | free( proto ); |
260 | return index; |
282 | return index; |
261 | } |
283 | } |
262 | } |
284 | } |
263 | printf( "\nCache of the existing device %d cleaned", device->device_id ); |
285 | printf( "Cache of the existing device %d cleaned\n", device->device_id ); |
264 | }else{ |
286 | }else{ |
265 | index = hardware_map( service ); |
287 | index = hardware_map( service ); |
266 | if( ! index ) return ENOENT; |
288 | if( ! index ) return ENOENT; |
267 | // create a new device |
289 | // create a new device |
268 | device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); |
290 | device = ( arp_device_ref ) malloc( sizeof( arp_device_t )); |
Line 325... | Line 347... | ||
325 | free( device->broadcast_data ); |
347 | free( device->broadcast_data ); |
326 | arp_protos_destroy( & device->protos ); |
348 | arp_protos_destroy( & device->protos ); |
327 | free( device ); |
349 | free( device ); |
328 | return ERROR_CODE; |
350 | return ERROR_CODE; |
329 | } |
351 | } |
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 ); |
352 | printf( "New device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d\n", device->device_id, device->hardware, device->service ); |
331 | } |
353 | } |
332 | rwlock_write_unlock( & arp_globals.lock ); |
354 | rwlock_write_unlock( & arp_globals.lock ); |
333 | return EOK; |
355 | return EOK; |
334 | } |
356 | } |
335 | 357 | ||
Line 349... | Line 371... | ||
349 | addr = arp_addr_find( & proto->addresses, target->value, target->length ); |
371 | addr = arp_addr_find( & proto->addresses, target->value, target->length ); |
350 | if( addr ) return addr; |
372 | if( addr ) return addr; |
351 | // ARP packet content size = header + ( address + translation ) * 2 |
373 | // ARP packet content size = header + ( address + translation ) * 2 |
352 | length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2; |
374 | length = 8 + ( CONVERT_SIZE( char, uint8_t, proto->addr->length ) + CONVERT_SIZE( char, uint8_t, device->addr->length )) * 2; |
353 | if( length > device->content ) return NULL; |
375 | if( length > device->content ) return NULL; |
354 | packet = packet_get_4( arp_globals.networking_phone, device->addr_len, device->prefix, length, device->suffix ); |
376 | packet = packet_get_4( arp_globals.net_phone, device->addr_len, device->prefix, length, device->suffix ); |
355 | if( ! packet ) return NULL; |
377 | if( ! packet ) return NULL; |
356 | header = ( arp_header_ref ) packet_suffix( packet, length ); |
378 | header = ( arp_header_ref ) packet_suffix( packet, length ); |
357 | header->hardware = htons( device->hardware ); |
379 | header->hardware = htons( device->hardware ); |
358 | header->hardware_length = device->addr->length; |
380 | header->hardware_length = device->addr->length; |
359 | header->protocol = htons( protocol_map( device->service, protocol )); |
381 | header->protocol = htons( protocol_map( device->service, protocol )); |
Line 426... | Line 448... | ||
426 | memcpy( src_hw, des_hw, header->hardware_length ); |
448 | memcpy( src_hw, des_hw, header->hardware_length ); |
427 | memcpy( des_hw, hw_source->value, header->hardware_length ); |
449 | memcpy( des_hw, hw_source->value, header->hardware_length ); |
428 | packet_set_addr( packet, src_hw, des_hw, header->hardware_length ); |
450 | packet_set_addr( packet, src_hw, des_hw, header->hardware_length ); |
429 | nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); |
451 | nil_send_msg( device->phone, device_id, packet, SERVICE_ARP ); |
430 | }else{ |
452 | }else{ |
431 | pq_release( arp_globals.networking_phone, packet_get_id( packet )); |
453 | pq_release( arp_globals.net_phone, packet_get_id( packet )); |
432 | } |
454 | } |
433 | } |
455 | } |
434 | return EOK; |
456 | return EOK; |
435 | } |
457 | } |
436 | 458 | ||
Line 447... | Line 469... | ||
447 | } |
469 | } |
448 | } |
470 | } |
449 | arp_protos_clear( & device->protos ); |
471 | arp_protos_clear( & device->protos ); |
450 | } |
472 | } |
451 | 473 | ||
- | 474 | int arp_connect_module( services_t service ){ |
|
- | 475 | return EOK; |
|
- | 476 | } |
|
- | 477 | ||
452 | int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
478 | int arp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
453 | ERROR_DECLARE; |
479 | ERROR_DECLARE; |
454 | 480 | ||
455 | measured_string_ref address; |
481 | measured_string_ref address; |
456 | measured_string_ref translation; |
482 | measured_string_ref translation; |
457 | char * data; |
483 | char * data; |
458 | 484 | ||
459 | // printf( "\nmessage %d - %d", IPC_GET_METHOD( * call ), NET_ARP_FIRST ); |
485 | // printf( "message %d - %d\n", IPC_GET_METHOD( * call ), NET_ARP_FIRST ); |
460 | * answer_count = 0; |
486 | * answer_count = 0; |
461 | switch( IPC_GET_METHOD( * call )){ |
487 | switch( IPC_GET_METHOD( * call )){ |
462 | case IPC_M_PHONE_HUNGUP: |
488 | case IPC_M_PHONE_HUNGUP: |
463 | return EOK; |
489 | return EOK; |
464 | case NET_ARP_DEVICE: |
490 | case NET_ARP_DEVICE: |
465 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
491 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
466 | if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_PROTO( call ), address ))){ |
492 | if( ERROR_OCCURRED( arp_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), ARP_GET_NETIF( call ), address ))){ |
467 | free( address ); |
493 | free( address ); |
468 | free( data ); |
494 | free( data ); |
469 | } |
495 | } |
470 | return ERROR_CODE; |
496 | return ERROR_CODE; |
471 | case NET_ARP_TRANSLATE: |
497 | case NET_ARP_TRANSLATE: |
472 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
498 | ERROR_PROPAGATE( measured_strings_receive( & address, & data, 1 )); |
473 | rwlock_read_lock( & arp_globals.lock ); |
499 | rwlock_read_lock( & arp_globals.lock ); |
474 | translation = arp_translate_message( IPC_GET_DEVICE( call ), ARP_GET_PROTO( call ), address ); |
500 | translation = arp_translate_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ), address ); |
475 | free( address ); |
501 | free( address ); |
476 | free( data ); |
502 | free( data ); |
477 | if( ! translation ){ |
503 | if( ! translation ){ |
478 | rwlock_read_unlock( & arp_globals.lock ); |
504 | rwlock_read_unlock( & arp_globals.lock ); |
479 | return ENOENT; |
505 | return ENOENT; |
480 | } |
506 | } |
481 | ERROR_CODE = measured_strings_reply( translation, 1 ); |
507 | ERROR_CODE = measured_strings_reply( translation, 1 ); |
482 | rwlock_read_unlock( & arp_globals.lock ); |
508 | rwlock_read_unlock( & arp_globals.lock ); |
483 | return ERROR_CODE; |
509 | return ERROR_CODE; |
484 | case NET_ARP_CLEAR_DEVICE: |
510 | case NET_ARP_CLEAR_DEVICE: |
485 | return arp_clear_device_wrapper( IPC_GET_DEVICE( call )); |
511 | return arp_clear_device_req( 0, IPC_GET_DEVICE( call )); |
486 | case NET_ARP_CLEAN_CACHE: |
512 | case NET_ARP_CLEAN_CACHE: |
487 | return arp_clean_cache_wrapper(); |
513 | return arp_clean_cache_req( 0 ); |
488 | } |
514 | } |
489 | return ENOTSUP; |
515 | return ENOTSUP; |
490 | } |
516 | } |
491 | 517 | ||
492 | void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
518 | void arp_receiver( ipc_callid_t iid, ipc_call_t * icall ){ |
Line 499... | Line 525... | ||
499 | case NET_IL_DEVICE_STATE: |
525 | case NET_IL_DEVICE_STATE: |
500 | // do nothing - keep the cache |
526 | // do nothing - keep the cache |
501 | ipc_answer_0( iid, EOK ); |
527 | ipc_answer_0( iid, EOK ); |
502 | break; |
528 | break; |
503 | case NET_IL_RECEIVED: |
529 | case NET_IL_RECEIVED: |
504 | if( ! ERROR_OCCURRED( packet_translate( arp_globals.networking_phone, & packet, IPC_GET_PACKET( icall )))){ |
530 | if( ! ERROR_OCCURRED( packet_translate( arp_globals.net_phone, & packet, IPC_GET_PACKET( icall )))){ |
505 | rwlock_read_lock( & arp_globals.lock ); |
531 | rwlock_read_lock( & arp_globals.lock ); |
506 | ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet ); |
532 | ERROR_CODE = arp_receive_message( IPC_GET_DEVICE( icall ), packet ); |
507 | rwlock_read_unlock( & arp_globals.lock ); |
533 | rwlock_read_unlock( & arp_globals.lock ); |
508 | } |
534 | } |
509 | ipc_answer_0( iid, ERROR_CODE ); |
535 | ipc_answer_0( iid, ERROR_CODE ); |