Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/il/arp/arp.c

    re9caf47 r14f1db0  
    2828
    2929/** @addtogroup arp
    30  * @{
     30 *  @{
    3131 */
    3232
    3333/** @file
    34  * ARP module implementation.
    35  * @see arp.h
    36  */
    37 
    38 #include "arp.h"
    39 #include "arp_header.h"
    40 #include "arp_oc.h"
    41 #include "arp_module.h"
     34 *  ARP module implementation.
     35 *  @see arp.h
     36 */
    4237
    4338#include <async.h>
     
    4843#include <str.h>
    4944#include <task.h>
    50 #include <adt/measured_strings.h>
    5145#include <ipc/ipc.h>
    5246#include <ipc/services.h>
    53 #include <ipc/net.h>
    54 #include <ipc/arp.h>
    55 #include <ipc/il.h>
    56 #include <byteorder.h>
    57 #include <err.h>
    58 
    59 #include <net/modules.h>
    60 #include <net/device.h>
    61 #include <net/packet.h>
    62 
     47
     48#include <net_err.h>
     49#include <net_messages.h>
     50#include <net_modules.h>
     51#include <net_byteorder.h>
     52#include <net_device.h>
     53#include <arp_interface.h>
    6354#include <nil_interface.h>
    6455#include <protocol_map.h>
    65 #include <packet_client.h>
     56#include <adt/measured_strings.h>
     57#include <packet/packet.h>
     58#include <packet/packet_client.h>
    6659#include <packet_remote.h>
     60#include <il_messages.h>
    6761#include <il_interface.h>
    6862#include <il_local.h>
    69 
    70 
    71 /** ARP module name. */
     63#include <arp_messages.h>
     64
     65#include "arp.h"
     66#include "arp_header.h"
     67#include "arp_oc.h"
     68#include "arp_module.h"
     69
     70
     71/** ARP module name.
     72 */
    7273#define NAME  "arp"
    7374
    74 /** ARP global data. */
    75 arp_globals_t arp_globals;
    76 
    77 DEVICE_MAP_IMPLEMENT(arp_cache, arp_device_t);
    78 INT_MAP_IMPLEMENT(arp_protos, arp_proto_t);
    79 GENERIC_CHAR_MAP_IMPLEMENT(arp_addr, measured_string_t);
     75/** ARP global data.
     76 */
     77arp_globals_t   arp_globals;
    8078
    8179/** Clears the device specific data.
    82  *
    83  * @param[in] device    The device specific data.
    84  */
    85 static void arp_clear_device(arp_device_ref device)
    86 {
    87         int count;
    88         arp_proto_ref proto;
    89 
    90         for (count = arp_protos_count(&device->protos) - 1; count >= 0;
    91             count--) {
    92                 proto = arp_protos_get_index(&device->protos, count);
    93                 if (proto) {
    94                         if (proto->addr)
    95                                 free(proto->addr);
    96                         if (proto->addr_data)
    97                                 free(proto->addr_data);
    98                         arp_addr_destroy(&proto->addresses);
    99                 }
    100         }
    101         arp_protos_clear(&device->protos);
    102 }
    103 
    104 static int arp_clean_cache_req(int arp_phone)
    105 {
     80 *  @param[in] device The device specific data.
     81 */
     82void arp_clear_device(arp_device_ref device);
     83
     84/** Creates new protocol specific data.
     85 *  Allocates and returns the needed memory block as the proto parameter.
     86 *  @param[out] proto The allocated protocol specific data.
     87 *  @param[in] service The protocol module service.
     88 *  @param[in] address The actual protocol device address.
     89 *  @returns EOK on success.
     90 *  @returns ENOMEM if there is not enough memory left.
     91 */
     92int arp_proto_create(arp_proto_ref * proto, services_t service, measured_string_ref address);
     93
     94/** @name Message processing functions
     95 */
     96/*@{*/
     97
     98/** Registers the device.
     99 *  Creates new device entry in the cache or updates the protocol address if the device with the device identifier and the driver service exists.
     100 *  @param[in] device_id The device identifier.
     101 *  @param[in] service The device driver service.
     102 *  @param[in] protocol The protocol service.
     103 *  @param[in] address The actual device protocol address.
     104 *  @returns EOK on success.
     105 *  @returns EEXIST if another device with the same device identifier and different driver service exists.
     106 *  @returns ENOMEM if there is not enough memory left.
     107 *  @returns Other error codes as defined for the measured_strings_return() function.
     108 */
     109int arp_device_message(device_id_t device_id, services_t service, services_t protocol, measured_string_ref address);
     110
     111/** Updates the device content length according to the new MTU value.
     112 *  @param[in] device_id The device identifier.
     113 *  @param[in] mtu The new mtu value.
     114 *  @returns ENOENT if device is not found.
     115 *  @returns EOK on success.
     116 */
     117int arp_mtu_changed_message(device_id_t device_id, size_t mtu);
     118
     119/** Processes the received ARP packet.
     120 *  Updates the source hardware address if the source entry exists or the packet is targeted to my protocol address.
     121 *  Responses to the ARP request if the packet is the ARP request and is targeted to my address.
     122 *  @param[in] device_id The source device identifier.
     123 *  @param[in,out] packet The received packet.
     124 *  @returns EOK on success and the packet is no longer needed.
     125 *  @returns 1 on success and the packet has been reused.
     126 *  @returns EINVAL if the packet is too small to carry an ARP packet.
     127 *  @returns EINVAL if the received address lengths differs from the registered values.
     128 *  @returns ENOENT if the device is not found in the cache.
     129 *  @returns ENOENT if the protocol for the device is not found in the cache.
     130 *  @returns ENOMEM if there is not enough memory left.
     131 */
     132int arp_receive_message(device_id_t device_id, packet_t packet);
     133
     134/** Returns the hardware address for the given protocol address.
     135 *  Sends the ARP request packet if the hardware address is not found in the cache.
     136 *  @param[in] device_id The device identifier.
     137 *  @param[in] protocol The protocol service.
     138 *  @param[in] target The target protocol address.
     139 *  @returns The hardware address of the target.
     140 *  @returns NULL if the target parameter is NULL.
     141 *  @returns NULL if the device is not found.
     142 *  @returns NULL if the device packet is too small to send a&nbsp;request.
     143 *  @returns NULL if the hardware address is not found in the cache.
     144 */
     145measured_string_ref arp_translate_message(device_id_t device_id, services_t protocol, measured_string_ref target);
     146
     147/*@}*/
     148
     149DEVICE_MAP_IMPLEMENT(arp_cache, arp_device_t)
     150
     151INT_MAP_IMPLEMENT(arp_protos, arp_proto_t)
     152
     153GENERIC_CHAR_MAP_IMPLEMENT(arp_addr, measured_string_t)
     154
     155int arp_clean_cache_req(int arp_phone){
    106156        int count;
    107157        arp_device_ref device;
    108158
    109159        fibril_rwlock_write_lock(&arp_globals.lock);
    110         for (count = arp_cache_count(&arp_globals.cache) - 1; count >= 0;
    111             count--) {
     160        for(count = arp_cache_count(&arp_globals.cache) - 1; count >= 0; -- count){
    112161                device = arp_cache_get_index(&arp_globals.cache, count);
    113                 if (device) {
     162                if(device){
    114163                        arp_clear_device(device);
    115                         if (device->addr_data)
     164                        if(device->addr_data){
    116165                                free(device->addr_data);
    117                         if (device->broadcast_data)
     166                        }
     167                        if(device->broadcast_data){
    118168                                free(device->broadcast_data);
     169                        }
    119170                }
    120171        }
     
    125176}
    126177
    127 static int
    128 arp_clear_address_req(int arp_phone, device_id_t device_id, services_t protocol,
    129     measured_string_ref address)
    130 {
     178int arp_clear_address_req(int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address){
    131179        arp_device_ref device;
    132180        arp_proto_ref proto;
     
    134182        fibril_rwlock_write_lock(&arp_globals.lock);
    135183        device = arp_cache_find(&arp_globals.cache, device_id);
    136         if (!device) {
     184        if(! device){
    137185                fibril_rwlock_write_unlock(&arp_globals.lock);
    138186                return ENOENT;
    139187        }
    140188        proto = arp_protos_find(&device->protos, protocol);
    141         if (!proto) {
     189        if(! proto){
    142190                fibril_rwlock_write_unlock(&arp_globals.lock);
    143191                return ENOENT;
     
    148196}
    149197
    150 
    151 static int arp_clear_device_req(int arp_phone, device_id_t device_id)
    152 {
     198void arp_clear_device(arp_device_ref device){
     199        int count;
     200        arp_proto_ref proto;
     201
     202        for(count = arp_protos_count(&device->protos) - 1; count >= 0; -- count){
     203                proto = arp_protos_get_index(&device->protos, count);
     204                if(proto){
     205                        if(proto->addr){
     206                                free(proto->addr);
     207                        }
     208                        if(proto->addr_data){
     209                                free(proto->addr_data);
     210                        }
     211                        arp_addr_destroy(&proto->addresses);
     212                }
     213        }
     214        arp_protos_clear(&device->protos);
     215}
     216
     217int arp_clear_device_req(int arp_phone, device_id_t device_id){
    153218        arp_device_ref device;
    154219
    155220        fibril_rwlock_write_lock(&arp_globals.lock);
    156221        device = arp_cache_find(&arp_globals.cache, device_id);
    157         if (!device) {
     222        if(! device){
    158223                fibril_rwlock_write_unlock(&arp_globals.lock);
    159224                return ENOENT;
     
    165230}
    166231
    167 /** Creates new protocol specific data.
    168  *
    169  * Allocates and returns the needed memory block as the proto parameter.
    170  *
    171  * @param[out] proto    The allocated protocol specific data.
    172  * @param[in] service   The protocol module service.
    173  * @param[in] address   The actual protocol device address.
    174  * @returns             EOK on success.
    175  * @returns             ENOMEM if there is not enough memory left.
    176  */
    177 static int
    178 arp_proto_create(arp_proto_ref *proto, services_t service,
    179     measured_string_ref address)
    180 {
    181         ERROR_DECLARE;
    182 
    183         *proto = (arp_proto_ref) malloc(sizeof(arp_proto_t));
    184         if (!*proto)
    185                 return ENOMEM;
    186         (*proto)->service = service;
    187         (*proto)->addr = address;
    188         (*proto)->addr_data = address->value;
    189         if (ERROR_OCCURRED(arp_addr_initialize(&(*proto)->addresses))) {
    190                 free(*proto);
    191                 return ERROR_CODE;
    192         }
    193         return EOK;
    194 }
    195 
    196 /** Registers the device.
    197  *
    198  * Creates new device entry in the cache or updates the protocol address if the
    199  * device with the device identifier and the driver service exists.
    200  *
    201  * @param[in] device_id The device identifier.
    202  * @param[in] service   The device driver service.
    203  * @param[in] protocol  The protocol service.
    204  * @param[in] address   The actual device protocol address.
    205  * @returns             EOK on success.
    206  * @returns             EEXIST if another device with the same device identifier
    207  *                      and different driver service exists.
    208  * @returns             ENOMEM if there is not enough memory left.
    209  * @returns             Other error codes as defined for the
    210  *                      measured_strings_return() function.
    211  */
    212 static int
    213 arp_device_message(device_id_t device_id, services_t service,
    214     services_t protocol, measured_string_ref address)
    215 {
     232int arp_connect_module(services_t service){
     233        if(service != SERVICE_ARP){
     234                return EINVAL;
     235        }
     236        return EOK;
     237}
     238
     239int arp_device_message(device_id_t device_id, services_t service, services_t protocol, measured_string_ref address){
    216240        ERROR_DECLARE;
    217241
     
    224248        // an existing device?
    225249        device = arp_cache_find(&arp_globals.cache, device_id);
    226         if (device) {
    227                 if (device->service != service) {
     250        if(device){
     251                if(device->service != service){
    228252                        printf("Device %d already exists\n", device->device_id);
    229253                        fibril_rwlock_write_unlock(&arp_globals.lock);
     
    231255                }
    232256                proto = arp_protos_find(&device->protos, protocol);
    233                 if (proto) {
     257                if(proto){
    234258                        free(proto->addr);
    235259                        free(proto->addr_data);
    236260                        proto->addr = address;
    237261                        proto->addr_data = address->value;
    238                 } else {
    239                         if (ERROR_OCCURRED(arp_proto_create(&proto, protocol,
    240                             address))) {
     262                }else{
     263                        if(ERROR_OCCURRED(arp_proto_create(&proto, protocol, address))){
    241264                                fibril_rwlock_write_unlock(&arp_globals.lock);
    242265                                return ERROR_CODE;
    243266                        }
    244                         index = arp_protos_add(&device->protos, proto->service,
    245                             proto);
    246                         if (index < 0) {
     267                        index = arp_protos_add(&device->protos, proto->service, proto);
     268                        if(index < 0){
    247269                                fibril_rwlock_write_unlock(&arp_globals.lock);
    248270                                free(proto);
    249271                                return index;
    250272                        }
    251                         printf("New protocol added:\n\tdevice id\t= "
    252                             "%d\n\tproto\t= %d", device_id, protocol);
    253                 }
    254         } else {
     273                        printf("New protocol added:\n\tdevice id\t= %d\n\tproto\t= %d", device_id, protocol);
     274                }
     275        }else{
    255276                hardware = hardware_map(service);
    256                 if (!hardware)
     277                if(! hardware){
    257278                        return ENOENT;
    258                
     279                }
    259280                // create a new device
    260281                device = (arp_device_ref) malloc(sizeof(arp_device_t));
    261                 if (!device) {
     282                if(! device){
    262283                        fibril_rwlock_write_unlock(&arp_globals.lock);
    263284                        return ENOMEM;
     
    265286                device->hardware = hardware;
    266287                device->device_id = device_id;
    267                 if (ERROR_OCCURRED(arp_protos_initialize(&device->protos)) ||
    268                     ERROR_OCCURRED(arp_proto_create(&proto, protocol,
    269                     address))) {
     288                if(ERROR_OCCURRED(arp_protos_initialize(&device->protos))
     289                        || ERROR_OCCURRED(arp_proto_create(&proto, protocol, address))){
    270290                        fibril_rwlock_write_unlock(&arp_globals.lock);
    271291                        free(device);
     
    273293                }
    274294                index = arp_protos_add(&device->protos, proto->service, proto);
    275                 if (index < 0) {
     295                if(index < 0){
    276296                        fibril_rwlock_write_unlock(&arp_globals.lock);
    277297                        arp_protos_destroy(&device->protos);
     
    280300                }
    281301                device->service = service;
    282                
    283302                // bind the new one
    284                 device->phone = nil_bind_service(device->service,
    285                     (ipcarg_t) device->device_id, SERVICE_ARP,
    286                     arp_globals.client_connection);
    287                 if (device->phone < 0) {
     303                device->phone = nil_bind_service(device->service, (ipcarg_t) device->device_id, SERVICE_ARP, arp_globals.client_connection);
     304                if(device->phone < 0){
    288305                        fibril_rwlock_write_unlock(&arp_globals.lock);
    289306                        arp_protos_destroy(&device->protos);
     
    291308                        return EREFUSED;
    292309                }
    293                
    294310                // get packet dimensions
    295                 if (ERROR_OCCURRED(nil_packet_size_req(device->phone, device_id,
    296                     &device->packet_dimension))) {
     311                if(ERROR_OCCURRED(nil_packet_size_req(device->phone, device_id, &device->packet_dimension))){
    297312                        fibril_rwlock_write_unlock(&arp_globals.lock);
    298313                        arp_protos_destroy(&device->protos);
     
    300315                        return ERROR_CODE;
    301316                }
    302                
    303317                // get hardware address
    304                 if (ERROR_OCCURRED(nil_get_addr_req(device->phone, device_id,
    305                     &device->addr, &device->addr_data))) {
     318                if(ERROR_OCCURRED(nil_get_addr_req(device->phone, device_id, &device->addr, &device->addr_data))){
    306319                        fibril_rwlock_write_unlock(&arp_globals.lock);
    307320                        arp_protos_destroy(&device->protos);
     
    309322                        return ERROR_CODE;
    310323                }
    311                
    312324                // get broadcast address
    313                 if (ERROR_OCCURRED(nil_get_broadcast_addr_req(device->phone,
    314                     device_id, &device->broadcast_addr,
    315                     &device->broadcast_data))) {
     325                if(ERROR_OCCURRED(nil_get_broadcast_addr_req(device->phone, device_id, &device->broadcast_addr, &device->broadcast_data))){
    316326                        fibril_rwlock_write_unlock(&arp_globals.lock);
    317327                        free(device->addr);
     
    321331                        return ERROR_CODE;
    322332                }
    323                
    324                 if (ERROR_OCCURRED(arp_cache_add(&arp_globals.cache,
    325                     device->device_id, device))) {
     333                if(ERROR_OCCURRED(arp_cache_add(&arp_globals.cache, device->device_id, device))){
    326334                        fibril_rwlock_write_unlock(&arp_globals.lock);
    327335                        free(device->addr);
     
    333341                        return ERROR_CODE;
    334342                }
    335                 printf("%s: Device registered (id: %d, type: 0x%x, service: %d,"
    336                     " proto: %d)\n", NAME, device->device_id, device->hardware,
    337                     device->service, protocol);
     343                printf("%s: Device registered (id: %d, type: 0x%x, service: %d, proto: %d)\n",
     344                    NAME, device->device_id, device->hardware, device->service, protocol);
    338345        }
    339346        fibril_rwlock_write_unlock(&arp_globals.lock);
    340        
    341         return EOK;
    342 }
    343 
    344 /** Initializes the ARP module.
    345  *
    346  *  @param[in] client_connection The client connection processing function.
    347  *                      The module skeleton propagates its own one.
    348  *  @returns            EOK on success.
    349  *  @returns            ENOMEM if there is not enough memory left.
    350  */
    351 int arp_initialize(async_client_conn_t client_connection)
    352 {
     347        return EOK;
     348}
     349
     350int arp_device_req(int arp_phone, device_id_t device_id, services_t protocol, services_t netif, measured_string_ref address){
     351        ERROR_DECLARE;
     352
     353        measured_string_ref tmp;
     354
     355        // copy the given address for exclusive use
     356        tmp = measured_string_copy(address);
     357        if(ERROR_OCCURRED(arp_device_message(device_id, netif, protocol, tmp))){
     358                free(tmp->value);
     359                free(tmp);
     360        }
     361        return ERROR_CODE;
     362}
     363
     364int arp_initialize(async_client_conn_t client_connection){
    353365        ERROR_DECLARE;
    354366
     
    361373}
    362374
    363 /** Updates the device content length according to the new MTU value.
    364  *
    365  * @param[in] device_id The device identifier.
    366  * @param[in] mtu       The new mtu value.
    367  * @returns             ENOENT if device is not found.
    368  * @returns             EOK on success.
    369  */
    370 static int arp_mtu_changed_message(device_id_t device_id, size_t mtu)
     375int arp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
     376    ipc_call_t *answer, int *answer_count)
    371377{
     378        ERROR_DECLARE;
     379       
     380        measured_string_ref address;
     381        measured_string_ref translation;
     382        char * data;
     383        packet_t packet;
     384        packet_t next;
     385       
     386        *answer_count = 0;
     387        switch (IPC_GET_METHOD(*call)) {
     388                case IPC_M_PHONE_HUNGUP:
     389                        return EOK;
     390                case NET_ARP_DEVICE:
     391                        ERROR_PROPAGATE(measured_strings_receive(&address, &data, 1));
     392                        if(ERROR_OCCURRED(arp_device_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), ARP_GET_NETIF(call), address))){
     393                                free(address);
     394                                free(data);
     395                        }
     396                        return ERROR_CODE;
     397                case NET_ARP_TRANSLATE:
     398                        ERROR_PROPAGATE(measured_strings_receive(&address, &data, 1));
     399                        fibril_rwlock_read_lock(&arp_globals.lock);
     400                        translation = arp_translate_message(IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), address);
     401                        free(address);
     402                        free(data);
     403                        if(! translation){
     404                                fibril_rwlock_read_unlock(&arp_globals.lock);
     405                                return ENOENT;
     406                        }
     407                        ERROR_CODE = measured_strings_reply(translation, 1);
     408                        fibril_rwlock_read_unlock(&arp_globals.lock);
     409                        return ERROR_CODE;
     410                case NET_ARP_CLEAR_DEVICE:
     411                        return arp_clear_device_req(0, IPC_GET_DEVICE(call));
     412                case NET_ARP_CLEAR_ADDRESS:
     413                        ERROR_PROPAGATE(measured_strings_receive(&address, &data, 1));
     414                        arp_clear_address_req(0, IPC_GET_DEVICE(call), IPC_GET_SERVICE(call), address);
     415                        free(address);
     416                        free(data);
     417                        return EOK;
     418                case NET_ARP_CLEAN_CACHE:
     419                        return arp_clean_cache_req(0);
     420                case NET_IL_DEVICE_STATE:
     421                        // do nothing - keep the cache
     422                        return EOK;
     423                case NET_IL_RECEIVED:
     424                        if(! ERROR_OCCURRED(packet_translate_remote(arp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
     425                                fibril_rwlock_read_lock(&arp_globals.lock);
     426                                do{
     427                                        next = pq_detach(packet);
     428                                        ERROR_CODE = arp_receive_message(IPC_GET_DEVICE(call), packet);
     429                                        if(ERROR_CODE != 1){
     430                                                pq_release_remote(arp_globals.net_phone, packet_get_id(packet));
     431                                        }
     432                                        packet = next;
     433                                }while(packet);
     434                                fibril_rwlock_read_unlock(&arp_globals.lock);
     435                        }
     436                        return ERROR_CODE;
     437                case NET_IL_MTU_CHANGED:
     438                        return arp_mtu_changed_message(IPC_GET_DEVICE(call), IPC_GET_MTU(call));
     439        }
     440       
     441        return ENOTSUP;
     442}
     443
     444int arp_mtu_changed_message(device_id_t device_id, size_t mtu){
    372445        arp_device_ref device;
    373446
    374447        fibril_rwlock_write_lock(&arp_globals.lock);
    375448        device = arp_cache_find(&arp_globals.cache, device_id);
    376         if (!device) {
     449        if(! device){
    377450                fibril_rwlock_write_unlock(&arp_globals.lock);
    378451                return ENOENT;
    379452        }
    380453        device->packet_dimension.content = mtu;
     454        printf("arp - device %d changed mtu to %d\n\n", device_id, mtu);
    381455        fibril_rwlock_write_unlock(&arp_globals.lock);
    382         printf("arp - device %d changed mtu to %d\n\n", device_id, mtu);
    383         return EOK;
    384 }
    385 
    386 /** Processes the received ARP packet.
    387  *
    388  * Updates the source hardware address if the source entry exists or the packet
    389  * is targeted to my protocol address.
    390  * Responses to the ARP request if the packet is the ARP request and is
    391  * targeted to my address.
    392  *
    393  * @param[in] device_id The source device identifier.
    394  * @param[in,out] packet The received packet.
    395  * @returns             EOK on success and the packet is no longer needed.
    396  * @returns             One on success and the packet has been reused.
    397  * @returns             EINVAL if the packet is too small to carry an ARP
    398  *                      packet.
    399  * @returns             EINVAL if the received address lengths differs from
    400  *                      the registered values.
    401  * @returns             ENOENT if the device is not found in the cache.
    402  * @returns             ENOENT if the protocol for the device is not found in
    403  *                      the cache.
    404  * @returns             ENOMEM if there is not enough memory left.
    405  */
    406 static int arp_receive_message(device_id_t device_id, packet_t packet)
    407 {
     456        return EOK;
     457}
     458
     459int arp_proto_create(arp_proto_ref * proto, services_t service, measured_string_ref address){
     460        ERROR_DECLARE;
     461
     462        *proto = (arp_proto_ref) malloc(sizeof(arp_proto_t));
     463        if(!(*proto)){
     464                return ENOMEM;
     465        }
     466        (** proto).service = service;
     467        (** proto).addr = address;
     468        (** proto).addr_data = address->value;
     469        if(ERROR_OCCURRED(arp_addr_initialize(&(** proto).addresses))){
     470                free(*proto);
     471                return ERROR_CODE;
     472        }
     473        return EOK;
     474}
     475
     476int arp_receive_message(device_id_t device_id, packet_t packet){
    408477        ERROR_DECLARE;
    409478
     
    413482        arp_proto_ref proto;
    414483        measured_string_ref hw_source;
    415         uint8_t *src_hw;
    416         uint8_t *src_proto;
    417         uint8_t *des_hw;
    418         uint8_t *des_proto;
     484        uint8_t * src_hw;
     485        uint8_t * src_proto;
     486        uint8_t * des_hw;
     487        uint8_t * des_proto;
    419488
    420489        length = packet_get_data_length(packet);
    421         if (length <= sizeof(arp_header_t))
     490        if(length <= sizeof(arp_header_t)){
    422491                return EINVAL;
    423 
     492        }
    424493        device = arp_cache_find(&arp_globals.cache, device_id);
    425         if (!device)
     494        if(! device){
    426495                return ENOENT;
    427 
     496        }
    428497        header = (arp_header_ref) packet_get_data(packet);
    429         if ((ntohs(header->hardware) != device->hardware) ||
    430             (length < sizeof(arp_header_t) + header->hardware_length * 2U +
    431             header->protocol_length * 2U)) {
     498        if((ntohs(header->hardware) != device->hardware)
     499                || (length < sizeof(arp_header_t) + header->hardware_length * 2u + header->protocol_length * 2u)){
    432500                return EINVAL;
    433501        }
    434 
    435         proto = arp_protos_find(&device->protos,
    436             protocol_unmap(device->service, ntohs(header->protocol)));
    437         if (!proto)
     502        proto = arp_protos_find(&device->protos, protocol_unmap(device->service, ntohs(header->protocol)));
     503        if(! proto){
    438504                return ENOENT;
    439 
     505        }
    440506        src_hw = ((uint8_t *) header) + sizeof(arp_header_t);
    441507        src_proto = src_hw + header->hardware_length;
    442508        des_hw = src_proto + header->protocol_length;
    443509        des_proto = des_hw + header->hardware_length;
    444         hw_source = arp_addr_find(&proto->addresses, (char *) src_proto,
    445             CONVERT_SIZE(uint8_t, char, header->protocol_length));
     510        hw_source = arp_addr_find(&proto->addresses, (char *) src_proto, CONVERT_SIZE(uint8_t, char, header->protocol_length));
    446511        // exists?
    447         if (hw_source) {
    448                 if (hw_source->length != CONVERT_SIZE(uint8_t, char,
    449                     header->hardware_length)) {
     512        if(hw_source){
     513                if(hw_source->length != CONVERT_SIZE(uint8_t, char, header->hardware_length)){
    450514                        return EINVAL;
    451515                }
     
    453517        }
    454518        // is my protocol address?
    455         if (proto->addr->length != CONVERT_SIZE(uint8_t, char,
    456             header->protocol_length)) {
     519        if(proto->addr->length != CONVERT_SIZE(uint8_t, char, header->protocol_length)){
    457520                return EINVAL;
    458521        }
    459         if (!str_lcmp(proto->addr->value, (char *) des_proto,
    460             proto->addr->length)) {
     522        if(! str_lcmp(proto->addr->value, (char *) des_proto, proto->addr->length)){
    461523                // not already upadted?
    462                 if (!hw_source) {
    463                         hw_source = measured_string_create_bulk((char *) src_hw,
    464                             CONVERT_SIZE(uint8_t, char,
    465                             header->hardware_length));
    466                         if (!hw_source)
     524                if(! hw_source){
     525                        hw_source = measured_string_create_bulk((char *) src_hw, CONVERT_SIZE(uint8_t, char, header->hardware_length));
     526                        if(! hw_source){
    467527                                return ENOMEM;
    468 
    469                         ERROR_PROPAGATE(arp_addr_add(&proto->addresses,
    470                             (char *) src_proto, CONVERT_SIZE(uint8_t, char,
    471                             header->protocol_length), hw_source));
    472                 }
    473                 if (ntohs(header->operation) == ARPOP_REQUEST) {
     528                        }
     529                        ERROR_PROPAGATE(arp_addr_add(&proto->addresses, (char *) src_proto, CONVERT_SIZE(uint8_t, char, header->protocol_length), hw_source));
     530                }
     531                if(ntohs(header->operation) == ARPOP_REQUEST){
    474532                        header->operation = htons(ARPOP_REPLY);
    475533                        memcpy(des_proto, src_proto, header->protocol_length);
    476                         memcpy(src_proto, proto->addr->value,
    477                             header->protocol_length);
    478                         memcpy(src_hw, device->addr->value,
    479                             device->packet_dimension.addr_len);
    480                         memcpy(des_hw, hw_source->value,
    481                             header->hardware_length);
    482                         ERROR_PROPAGATE(packet_set_addr(packet, src_hw, des_hw,
    483                             header->hardware_length));
    484                         nil_send_msg(device->phone, device_id, packet,
    485                             SERVICE_ARP);
     534                        memcpy(src_proto, proto->addr->value, header->protocol_length);
     535                        memcpy(src_hw, device->addr->value, device->packet_dimension.addr_len);
     536                        memcpy(des_hw, hw_source->value, header->hardware_length);
     537                        ERROR_PROPAGATE(packet_set_addr(packet, src_hw, des_hw, header->hardware_length));
     538                        nil_send_msg(device->phone, device_id, packet, SERVICE_ARP);
    486539                        return 1;
    487540                }
    488541        }
    489 
    490         return EOK;
    491 }
    492 
    493 
    494 /** Returns the hardware address for the given protocol address.
    495  *
    496  * Sends the ARP request packet if the hardware address is not found in the
    497  * cache.
    498  *
    499  * @param[in] device_id The device identifier.
    500  * @param[in] protocol  The protocol service.
    501  * @param[in] target    The target protocol address.
    502  * @returns             The hardware address of the target.
    503  * @returns             NULL if the target parameter is NULL.
    504  * @returns             NULL if the device is not found.
    505  * @returns             NULL if the device packet is too small to send a
    506  *                      request.
    507  * @returns             NULL if the hardware address is not found in the cache.
    508  */
    509 static measured_string_ref
    510 arp_translate_message(device_id_t device_id, services_t protocol,
    511     measured_string_ref target)
    512 {
     542        return EOK;
     543}
     544
     545task_id_t arp_task_get_id(void){
     546        return task_get_id();
     547}
     548
     549measured_string_ref arp_translate_message(device_id_t device_id, services_t protocol, measured_string_ref target){
    513550        arp_device_ref device;
    514551        arp_proto_ref proto;
     
    518555        arp_header_ref header;
    519556
    520         if (!target)
     557        if(! target){
    521558                return NULL;
    522 
     559        }
    523560        device = arp_cache_find(&arp_globals.cache, device_id);
    524         if (!device)
     561        if(! device){
    525562                return NULL;
    526 
     563        }
    527564        proto = arp_protos_find(&device->protos, protocol);
    528         if (!proto || (proto->addr->length != target->length))
     565        if((! proto) || (proto->addr->length != target->length)){
    529566                return NULL;
    530 
     567        }
    531568        addr = arp_addr_find(&proto->addresses, target->value, target->length);
    532         if (addr)
     569        if(addr){
    533570                return addr;
    534 
     571        }
    535572        // ARP packet content size = header + (address + translation) * 2
    536         length = 8 + 2 * (CONVERT_SIZE(char, uint8_t, proto->addr->length) +
    537             CONVERT_SIZE(char, uint8_t, device->addr->length));
    538         if (length > device->packet_dimension.content)
     573        length = 8 + (CONVERT_SIZE(char, uint8_t, proto->addr->length) + CONVERT_SIZE(char, uint8_t, device->addr->length)) * 2;
     574        if(length > device->packet_dimension.content){
    539575                return NULL;
    540 
    541         packet = packet_get_4_remote(arp_globals.net_phone,
    542             device->packet_dimension.addr_len, device->packet_dimension.prefix,
    543             length, device->packet_dimension.suffix);
    544         if (!packet)
     576        }
     577        packet = packet_get_4_remote(arp_globals.net_phone, device->packet_dimension.addr_len, device->packet_dimension.prefix, length, device->packet_dimension.suffix);
     578        if(! packet){
    545579                return NULL;
    546 
     580        }
    547581        header = (arp_header_ref) packet_suffix(packet, length);
    548         if (!header) {
     582        if(! header){
    549583                pq_release_remote(arp_globals.net_phone, packet_get_id(packet));
    550584                return NULL;
    551585        }
    552 
    553586        header->hardware = htons(device->hardware);
    554587        header->hardware_length = (uint8_t) device->addr->length;
     
    557590        header->operation = htons(ARPOP_REQUEST);
    558591        length = sizeof(arp_header_t);
    559         memcpy(((uint8_t *) header) + length, device->addr->value,
    560             device->addr->length);
     592        memcpy(((uint8_t *) header) + length, device->addr->value, device->addr->length);
    561593        length += device->addr->length;
    562         memcpy(((uint8_t *) header) + length, proto->addr->value,
    563             proto->addr->length);
     594        memcpy(((uint8_t *) header) + length, proto->addr->value, proto->addr->length);
    564595        length += proto->addr->length;
    565596        bzero(((uint8_t *) header) + length, device->addr->length);
    566597        length += device->addr->length;
    567598        memcpy(((uint8_t *) header) + length, target->value, target->length);
    568 
    569         if (packet_set_addr(packet, (uint8_t *) device->addr->value,
    570             (uint8_t *) device->broadcast_addr->value,
    571             CONVERT_SIZE(char, uint8_t, device->addr->length)) != EOK) {
     599        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){
    572600                pq_release_remote(arp_globals.net_phone, packet_get_id(packet));
    573601                return NULL;
    574602        }
    575 
    576603        nil_send_msg(device->phone, device_id, packet, SERVICE_ARP);
    577604        return NULL;
    578605}
    579606
    580 
    581 /** Processes the ARP message.
    582  *
    583  * @param[in] callid    The message identifier.
    584  * @param[in] call      The message parameters.
    585  * @param[out] answer   The message answer parameters.
    586  * @param[out] answer_count The last parameter for the actual answer in the
    587  *                      answer parameter.
    588  * @returns             EOK on success.
    589  * @returns             ENOTSUP if the message is not known.
    590  *
    591  * @see arp_interface.h
    592  * @see IS_NET_ARP_MESSAGE()
    593  */
    594 int
    595 arp_message_standalone(ipc_callid_t callid, ipc_call_t *call,
    596     ipc_call_t *answer, int *answer_count)
    597 {
    598         ERROR_DECLARE;
    599        
    600         measured_string_ref address;
    601         measured_string_ref translation;
    602         char *data;
    603         packet_t packet;
    604         packet_t next;
    605        
    606         *answer_count = 0;
    607         switch (IPC_GET_METHOD(*call)) {
    608         case IPC_M_PHONE_HUNGUP:
    609                 return EOK;
    610        
    611         case NET_ARP_DEVICE:
    612                 ERROR_PROPAGATE(measured_strings_receive(&address, &data, 1));
    613                 if (ERROR_OCCURRED(arp_device_message(IPC_GET_DEVICE(call),
    614                     IPC_GET_SERVICE(call), ARP_GET_NETIF(call), address))) {
    615                         free(address);
    616                         free(data);
    617                 }
    618                 return ERROR_CODE;
    619        
    620         case NET_ARP_TRANSLATE:
    621                 ERROR_PROPAGATE(measured_strings_receive(&address, &data, 1));
    622                 fibril_rwlock_read_lock(&arp_globals.lock);
    623                 translation = arp_translate_message(IPC_GET_DEVICE(call),
    624                     IPC_GET_SERVICE(call), address);
    625                 free(address);
    626                 free(data);
    627                 if (!translation) {
    628                         fibril_rwlock_read_unlock(&arp_globals.lock);
    629                         return ENOENT;
    630                 }
    631                 ERROR_CODE = measured_strings_reply(translation, 1);
     607int arp_translate_req(int arp_phone, device_id_t device_id, services_t protocol, measured_string_ref address, measured_string_ref * translation, char ** data){
     608        measured_string_ref tmp;
     609
     610        fibril_rwlock_read_lock(&arp_globals.lock);
     611        tmp = arp_translate_message(device_id, protocol, address);
     612        if(tmp){
     613                *translation = measured_string_copy(tmp);
    632614                fibril_rwlock_read_unlock(&arp_globals.lock);
    633                 return ERROR_CODE;
    634 
    635         case NET_ARP_CLEAR_DEVICE:
    636                 return arp_clear_device_req(0, IPC_GET_DEVICE(call));
    637 
    638         case NET_ARP_CLEAR_ADDRESS:
    639                 ERROR_PROPAGATE(measured_strings_receive(&address, &data, 1));
    640                 arp_clear_address_req(0, IPC_GET_DEVICE(call),
    641                     IPC_GET_SERVICE(call), address);
    642                 free(address);
    643                 free(data);
    644                 return EOK;
    645        
    646         case NET_ARP_CLEAN_CACHE:
    647                 return arp_clean_cache_req(0);
    648        
    649         case NET_IL_DEVICE_STATE:
    650                 // do nothing - keep the cache
    651                 return EOK;
    652        
    653         case NET_IL_RECEIVED:
    654                 if (ERROR_NONE(packet_translate_remote(arp_globals.net_phone,
    655                     &packet, IPC_GET_PACKET(call)))) {
    656                         fibril_rwlock_read_lock(&arp_globals.lock);
    657                         do {
    658                                 next = pq_detach(packet);
    659                                 ERROR_CODE =
    660                                     arp_receive_message(IPC_GET_DEVICE(call),
    661                                     packet);
    662                                 if (ERROR_CODE != 1) {
    663                                         pq_release_remote(arp_globals.net_phone,
    664                                             packet_get_id(packet));
    665                                 }
    666                                 packet = next;
    667                         } while (packet);
    668                         fibril_rwlock_read_unlock(&arp_globals.lock);
    669                 }
    670                 return ERROR_CODE;
    671        
    672         case NET_IL_MTU_CHANGED:
    673                 return arp_mtu_changed_message(IPC_GET_DEVICE(call),
    674                     IPC_GET_MTU(call));
    675         }
    676        
    677         return ENOTSUP;
     615                if(*translation){
     616                        *data = (** translation).value;
     617                        return EOK;
     618                }else{
     619                        return ENOMEM;
     620                }
     621        }else{
     622                fibril_rwlock_read_unlock(&arp_globals.lock);
     623                return ENOENT;
     624        }
    678625}
    679626
    680627/** Default thread for new connections.
    681628 *
    682  * @param[in] iid       The initial message identifier.
    683  * @param[in] icall     The initial message call structure.
    684  */
    685 static void il_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     629 *  @param[in] iid The initial message identifier.
     630 *  @param[in] icall The initial message call structure.
     631 *
     632 */
     633static void il_client_connection(ipc_callid_t iid, ipc_call_t * icall)
    686634{
    687635        /*
     
    691639        ipc_answer_0(iid, EOK);
    692640       
    693         while (true) {
     641        while(true) {
    694642                ipc_call_t answer;
    695643                int answer_count;
     
    706654                    &answer_count);
    707655               
    708                 /*
    709                  * End if told to either by the message or the processing
    710                  * result.
    711                  */
    712                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    713                     (res == EHANGUP))
     656                /* End if said to either by the message or the processing result */
     657                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
    714658                        return;
    715659               
     
    721665/** Starts the module.
    722666 *
    723  * @returns             EOK on success.
    724  * @returns             Other error codes as defined for each specific module
    725  *                      start function.
     667 *  @param argc The count of the command line arguments. Ignored parameter.
     668 *  @param argv The command line parameters. Ignored parameter.
     669 *
     670 *  @returns EOK on success.
     671 *  @returns Other error codes as defined for each specific module start function.
     672 *
    726673 */
    727674int main(int argc, char *argv[])
     
    730677       
    731678        /* Start the module */
    732         ERROR_PROPAGATE(il_module_start_standalone(il_client_connection));
     679        if (ERROR_OCCURRED(il_module_start_standalone(il_client_connection)))
     680                return ERROR_CODE;
     681       
    733682        return EOK;
    734683}
     
    736685/** @}
    737686 */
    738 
Note: See TracChangeset for help on using the changeset viewer.