Ignore:
File:
1 edited

Legend:

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

    r19f857a r14f1db0  
    4141#include <stdio.h>
    4242#include <str.h>
    43 
    4443#include <ipc/ipc.h>
    4544#include <ipc/services.h>
    46 
    4745#include <sys/types.h>
    4846
    49 #include "../../err.h"
    50 #include "../../messages.h"
    51 #include "../../modules.h"
    52 
    53 #include "../../include/arp_interface.h"
    54 #include "../../include/byteorder.h"
    55 #include "../../include/checksum.h"
    56 #include "../../include/device.h"
    57 #include "../../include/icmp_client.h"
    58 #include "../../include/icmp_codes.h"
    59 #include "../../include/icmp_interface.h"
    60 #include "../../include/il_interface.h"
    61 #include "../../include/in.h"
    62 #include "../../include/in6.h"
    63 #include "../../include/inet.h"
    64 #include "../../include/ip_client.h"
    65 #include "../../include/ip_interface.h"
    66 #include "../../include/net_interface.h"
    67 #include "../../include/nil_interface.h"
    68 #include "../../include/tl_interface.h"
    69 #include "../../include/socket_codes.h"
    70 #include "../../include/socket_errno.h"
    71 #include "../../structures/measured_strings.h"
    72 #include "../../structures/module_map.h"
    73 #include "../../structures/packet/packet_client.h"
    74 
    75 #include "../../nil/nil_messages.h"
    76 
    77 #include "../il_messages.h"
     47#include <net_err.h>
     48#include <net_messages.h>
     49#include <net_modules.h>
     50#include <arp_interface.h>
     51#include <net_byteorder.h>
     52#include <net_checksum.h>
     53#include <net_device.h>
     54#include <icmp_client.h>
     55#include <icmp_codes.h>
     56#include <icmp_interface.h>
     57#include <il_interface.h>
     58#include <in.h>
     59#include <in6.h>
     60#include <inet.h>
     61#include <ip_client.h>
     62#include <ip_interface.h>
     63#include <net_interface.h>
     64#include <nil_interface.h>
     65#include <tl_interface.h>
     66#include <socket_codes.h>
     67#include <socket_errno.h>
     68#include <adt/measured_strings.h>
     69#include <adt/module_map.h>
     70#include <packet/packet_client.h>
     71#include <packet_remote.h>
     72#include <nil_messages.h>
     73#include <il_messages.h>
     74#include <il_local.h>
     75#include <ip_local.h>
    7876
    7977#include "ip.h"
     
    8179#include "ip_messages.h"
    8280#include "ip_module.h"
     81
     82/** IP module name.
     83 */
     84#define NAME  "ip"
    8385
    8486/** IP version 4.
     
    425427}
    426428
    427 int ip_device_req(int il_phone, device_id_t device_id, services_t netif){
     429int ip_device_req_local(int il_phone, device_id_t device_id, services_t netif){
    428430        ERROR_DECLARE;
    429431
     
    431433        ip_route_ref route;
    432434        int index;
    433         char * data;
    434435
    435436        ip_netif = (ip_netif_ref) malloc(sizeof(ip_netif_t));
     
    455456        }
    456457        // print the settings
    457         printf("New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv);
    458         printf("\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static");
     458        printf("%s: Device registered (id: %d, phone: %d, ipv: %d, conf: %s)\n",
     459            NAME, ip_netif->device_id, ip_netif->phone, ip_netif->ipv,
     460            ip_netif->dhcp ? "dhcp" : "static");
     461       
    459462        // TODO ipv6 addresses
    460         data = (char *) malloc(INET_ADDRSTRLEN);
    461         if(data){
    462                 for(index = 0; index < ip_routes_count(&ip_netif->routes); ++ index){
    463                         route = ip_routes_get_index(&ip_netif->routes, index);
    464                         if(route){
    465                                 printf("\tRouting %d:\n", index);
    466                                 inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, data, INET_ADDRSTRLEN);
    467                                 printf("\t\taddress\t= %s\n", data);
    468                                 inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, data, INET_ADDRSTRLEN);
    469                                 printf("\t\tnetmask\t= %s\n", data);
    470                                 inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, data, INET_ADDRSTRLEN);
    471                                 printf("\t\tgateway\t= %s\n", data);
    472                         }
    473                 }
    474                 inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, data, INET_ADDRSTRLEN);
    475                 printf("\t\tbroadcast\t= %s\n", data);
    476                 free(data);
    477         }
     463       
     464        char address[INET_ADDRSTRLEN];
     465        char netmask[INET_ADDRSTRLEN];
     466        char gateway[INET_ADDRSTRLEN];
     467       
     468        for (index = 0; index < ip_routes_count(&ip_netif->routes); ++ index){
     469                route = ip_routes_get_index(&ip_netif->routes, index);
     470                if (route) {
     471                        inet_ntop(AF_INET, (uint8_t *) &route->address.s_addr, address, INET_ADDRSTRLEN);
     472                        inet_ntop(AF_INET, (uint8_t *) &route->netmask.s_addr, netmask, INET_ADDRSTRLEN);
     473                        inet_ntop(AF_INET, (uint8_t *) &route->gateway.s_addr, gateway, INET_ADDRSTRLEN);
     474                        printf("%s: Route %d (address: %s, netmask: %s, gateway: %s)\n",
     475                            NAME, index, address, netmask, gateway);
     476                }
     477        }
     478       
     479        inet_ntop(AF_INET, (uint8_t *) &ip_netif->broadcast.s_addr, address, INET_ADDRSTRLEN);
     480        printf("%s: Broadcast (%s)\n", NAME, address);
     481       
    478482        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    479483        return EOK;
     
    596600        }
    597601        netif->packet_dimension.content = mtu;
    598         printf("ip - device %d changed mtu to %d\n\n", device_id, mtu);
     602        printf("%s: Device %d changed MTU to %d\n", NAME, device_id, mtu);
    599603        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    600604        return EOK;
     
    612616        }
    613617        netif->state = state;
    614         printf("ip - device %d changed state to %d\n\n", device_id, state);
     618        printf("%s: Device %d changed state to %d\n", NAME, device_id, state);
    615619        fibril_rwlock_write_unlock(&ip_globals.netifs_lock);
    616620        return EOK;
     
    647651                return index;
    648652        }
    649         printf("New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone);
     653       
     654        printf("%s: Protocol registered (protocol: %d, phone: %d)\n",
     655            NAME, proto->protocol, proto->phone);
     656       
    650657        fibril_rwlock_write_unlock(&ip_globals.protos_lock);
    651658        return EOK;
    652659}
    653660
    654 int ip_send_msg(int il_phone, device_id_t device_id, packet_t packet, services_t sender, services_t error){
     661int ip_send_msg_local(int il_phone, device_id_t device_id, packet_t packet, services_t sender, services_t error){
    655662        ERROR_DECLARE;
    656663
     
    778785//                      sleep(1);
    779786//                      ERROR_PROPAGATE(arp_translate_req(netif->arp->phone, netif->device_id, SERVICE_IP, &destination, &translation, &data));
    780                         pq_release(ip_globals.net_phone, packet_get_id(packet));
     787                        pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
    781788                        return ERROR_CODE;
    782789                }
     
    795802        }else translation = NULL;
    796803        if(ERROR_OCCURRED(ip_prepare_packet(src, dest, packet, translation))){
    797                 pq_release(ip_globals.net_phone, packet_get_id(packet));
     804                pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
    798805        }else{
    799806                packet = ip_split_packet(packet, netif->packet_dimension.prefix, netif->packet_dimension.content, netif->packet_dimension.suffix, netif->packet_dimension.addr_len, error);
     
    887894}
    888895
    889 int ip_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     896int ip_message_standalone(ipc_callid_t callid, ipc_call_t *call,
     897    ipc_call_t *answer, int * answer_count)
     898{
    890899        ERROR_DECLARE;
    891 
     900       
    892901        packet_t packet;
    893         struct sockaddr * addr;
     902        struct sockaddr *addr;
    894903        size_t addrlen;
    895         ip_pseudo_header_ref header;
     904        size_t prefix;
     905        size_t suffix;
     906        size_t content;
     907        void *header;
    896908        size_t headerlen;
    897909        device_id_t device_id;
    898 
     910       
    899911        *answer_count = 0;
    900         switch(IPC_GET_METHOD(*call)){
     912        switch (IPC_GET_METHOD(*call)) {
    901913                case IPC_M_PHONE_HUNGUP:
    902914                        return EOK;
    903915                case NET_IL_DEVICE:
    904                         return ip_device_req(0, IPC_GET_DEVICE(call), IPC_GET_SERVICE(call));
     916                        return ip_device_req_local(0, IPC_GET_DEVICE(call),
     917                            IPC_GET_SERVICE(call));
    905918                case IPC_M_CONNECT_TO_ME:
    906                         return ip_register(IL_GET_PROTO(call), IL_GET_SERVICE(call), IPC_GET_PHONE(call), NULL);
     919                        return ip_register(IL_GET_PROTO(call), IL_GET_SERVICE(call),
     920                            IPC_GET_PHONE(call), NULL);
    907921                case NET_IL_SEND:
    908                         ERROR_PROPAGATE(packet_translate(ip_globals.net_phone, &packet, IPC_GET_PACKET(call)));
    909                         return ip_send_msg(0, IPC_GET_DEVICE(call), packet, 0, IPC_GET_ERROR(call));
     922                        ERROR_PROPAGATE(packet_translate_remote(ip_globals.net_phone, &packet,
     923                            IPC_GET_PACKET(call)));
     924                        return ip_send_msg_local(0, IPC_GET_DEVICE(call), packet, 0,
     925                            IPC_GET_ERROR(call));
    910926                case NET_IL_DEVICE_STATE:
    911                         return ip_device_state_message(IPC_GET_DEVICE(call), IPC_GET_STATE(call));
     927                        return ip_device_state_message(IPC_GET_DEVICE(call),
     928                            IPC_GET_STATE(call));
    912929                case NET_IL_RECEIVED:
    913                         ERROR_PROPAGATE(packet_translate(ip_globals.net_phone, &packet, IPC_GET_PACKET(call)));
     930                        ERROR_PROPAGATE(packet_translate_remote(ip_globals.net_phone, &packet,
     931                            IPC_GET_PACKET(call)));
    914932                        return ip_receive_message(IPC_GET_DEVICE(call), packet);
    915933                case NET_IP_RECEIVED_ERROR:
    916                         ERROR_PROPAGATE(packet_translate(ip_globals.net_phone, &packet, IPC_GET_PACKET(call)));
    917                         return ip_received_error_msg(0, IPC_GET_DEVICE(call), packet, IPC_GET_TARGET(call), IPC_GET_ERROR(call));
     934                        ERROR_PROPAGATE(packet_translate_remote(ip_globals.net_phone, &packet,
     935                            IPC_GET_PACKET(call)));
     936                        return ip_received_error_msg_local(0, IPC_GET_DEVICE(call), packet,
     937                            IPC_GET_TARGET(call), IPC_GET_ERROR(call));
    918938                case NET_IP_ADD_ROUTE:
    919                         return ip_add_route_req(0, IPC_GET_DEVICE(call), IP_GET_ADDRESS(call), IP_GET_NETMASK(call), IP_GET_GATEWAY(call));
     939                        return ip_add_route_req_local(0, IPC_GET_DEVICE(call),
     940                            IP_GET_ADDRESS(call), IP_GET_NETMASK(call), IP_GET_GATEWAY(call));
    920941                case NET_IP_SET_GATEWAY:
    921                         return ip_set_gateway_req(0, IPC_GET_DEVICE(call), IP_GET_GATEWAY(call));
     942                        return ip_set_gateway_req_local(0, IPC_GET_DEVICE(call),
     943                            IP_GET_GATEWAY(call));
    922944                case NET_IP_GET_ROUTE:
    923945                        ERROR_PROPAGATE(data_receive((void **) &addr, &addrlen));
    924                         ERROR_PROPAGATE(ip_get_route_req(0, IP_GET_PROTOCOL(call), addr, (socklen_t) addrlen,
    925                             &device_id, &header, &headerlen));
    926                         *IPC_SET_DEVICE(answer) = device_id;
    927                         *IP_SET_HEADERLEN(answer) = headerlen;
     946                        ERROR_PROPAGATE(ip_get_route_req_local(0, IP_GET_PROTOCOL(call),
     947                            addr, (socklen_t) addrlen, &device_id, &header, &headerlen));
     948                        IPC_SET_DEVICE(answer, device_id);
     949                        IP_SET_HEADERLEN(answer, headerlen);
     950                       
    928951                        *answer_count = 2;
    929                         if(! ERROR_OCCURRED(data_reply(&headerlen, sizeof(headerlen)))){
     952                       
     953                        if (!ERROR_OCCURRED(data_reply(&headerlen, sizeof(headerlen))))
    930954                                ERROR_CODE = data_reply(header, headerlen);
    931                         }
     955                       
    932956                        free(header);
    933957                        return ERROR_CODE;
    934958                case NET_IL_PACKET_SPACE:
    935                         ERROR_PROPAGATE(ip_packet_size_message(IPC_GET_DEVICE(call), IPC_SET_ADDR(answer), IPC_SET_PREFIX(answer), IPC_SET_CONTENT(answer), IPC_SET_SUFFIX(answer)));
    936                         *answer_count = 3;
     959                        ERROR_PROPAGATE(ip_packet_size_message(IPC_GET_DEVICE(call),
     960                            &addrlen, &prefix, &content, &suffix));
     961                        IPC_SET_ADDR(answer, addrlen);
     962                        IPC_SET_PREFIX(answer, prefix);
     963                        IPC_SET_CONTENT(answer, content);
     964                        IPC_SET_SUFFIX(answer, suffix);
     965                        *answer_count = 4;
    937966                        return EOK;
    938967                case NET_IL_MTU_CHANGED:
    939                         return ip_mtu_changed_message(IPC_GET_DEVICE(call), IPC_GET_MTU(call));
    940         }
     968                        return ip_mtu_changed_message(IPC_GET_DEVICE(call),
     969                            IPC_GET_MTU(call));
     970        }
     971       
    941972        return ENOTSUP;
    942973}
    943974
    944 int ip_packet_size_req(int ip_phone, device_id_t device_id, packet_dimension_ref packet_dimension){
    945         if(! packet_dimension){
     975int ip_packet_size_req_local(int ip_phone, device_id_t device_id,
     976    packet_dimension_ref packet_dimension)
     977{
     978        if (!packet_dimension)
    946979                return EBADMEM;
    947         }
    948         return ip_packet_size_message(device_id, &packet_dimension->addr_len, &packet_dimension->prefix, &packet_dimension->content, &packet_dimension->suffix);
     980       
     981        return ip_packet_size_message(device_id, &packet_dimension->addr_len,
     982            &packet_dimension->prefix, &packet_dimension->content,
     983            &packet_dimension->suffix);
    949984}
    950985
     
    9921027}
    9931028
    994 int ip_add_route_req(int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway){
     1029int ip_add_route_req_local(int ip_phone, device_id_t device_id, in_addr_t address, in_addr_t netmask, in_addr_t gateway){
    9951030        ip_route_ref route;
    9961031        ip_netif_ref netif;
     
    10561091}
    10571092
    1058 int ip_set_gateway_req(int ip_phone, device_id_t device_id, in_addr_t gateway){
     1093int ip_set_gateway_req_local(int ip_phone, device_id_t device_id, in_addr_t gateway)
     1094{
    10591095        ip_netif_ref netif;
    10601096
     
    11011137                                        }
    11021138                                }else{
    1103                                         pq_release(ip_globals.net_phone, packet_get_id(next));
     1139                                        pq_release_remote(ip_globals.net_phone, packet_get_id(next));
    11041140                                }
    11051141                                next = new_packet;
     
    11421178        }
    11431179        // create the last fragment
    1144         new_packet = packet_get_4(ip_globals.net_phone, prefix, length, suffix, ((addrlen > addr_len) ? addrlen : addr_len));
     1180        new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length, suffix, ((addrlen > addr_len) ? addrlen : addr_len));
    11451181        if(! new_packet){
    11461182                return ENOMEM;
     
    11661202        // create middle framgents
    11671203        while(IP_TOTAL_LENGTH(header) > length){
    1168                 new_packet = packet_get_4(ip_globals.net_phone, prefix, length, suffix, ((addrlen >= addr_len) ? addrlen : addr_len));
     1204                new_packet = packet_get_4_remote(ip_globals.net_phone, prefix, length, suffix, ((addrlen >= addr_len) ? addrlen : addr_len));
    11691205                if(! new_packet){
    11701206                        return ENOMEM;
     
    13461382}
    13471383
    1348 int ip_received_error_msg(int ip_phone, device_id_t device_id, packet_t packet, services_t target, services_t error){
     1384/** Notify the IP module about the received error notification packet.
     1385 *
     1386 * @param[in] ip_phone  The IP module phone used for (semi)remote calls.
     1387 * @param[in] device_id The device identifier.
     1388 * @param[in] packet    The received packet or the received packet queue.
     1389 * @param[in] target    The target internetwork module service to be
     1390 *                      delivered to.
     1391 * @param[in] error     The packet error reporting service. Prefixes the
     1392 *                      received packet.
     1393 *
     1394 * @return EOK on success.
     1395 *
     1396 */
     1397int ip_received_error_msg_local(int ip_phone, device_id_t device_id, packet_t packet, services_t target, services_t error){
    13491398        uint8_t * data;
    13501399        int offset;
     
    14811530        next = pq_detach(packet);
    14821531        if(next){
    1483                 pq_release(ip_globals.net_phone, packet_get_id(next));
     1532                pq_release_remote(ip_globals.net_phone, packet_get_id(next));
    14841533        }
    14851534        if(! header){
     
    15451594
    15461595int ip_release_and_return(packet_t packet, int result){
    1547         pq_release(ip_globals.net_phone, packet_get_id(packet));
     1596        pq_release_remote(ip_globals.net_phone, packet_get_id(packet));
    15481597        return result;
    15491598}
    15501599
    1551 int ip_get_route_req(int ip_phone, ip_protocol_t protocol, const struct sockaddr * destination, socklen_t addrlen, device_id_t * device_id, ip_pseudo_header_ref * header, size_t * headerlen){
     1600int ip_get_route_req_local(int ip_phone, ip_protocol_t protocol, const struct sockaddr * destination, socklen_t addrlen, device_id_t * device_id, void **header, size_t * headerlen){
    15521601        struct sockaddr_in * address_in;
    15531602//      struct sockaddr_in6 *   address_in6;
     
    16131662        header_in->protocol = protocol;
    16141663        header_in->data_length = 0;
    1615         *header = (ip_pseudo_header_ref) header_in;
     1664        *header = header_in;
    16161665        return EOK;
    16171666}
    16181667
     1668/** Default thread for new connections.
     1669 *
     1670 *  @param[in] iid The initial message identifier.
     1671 *  @param[in] icall The initial message call structure.
     1672 *
     1673 */
     1674static void il_client_connection(ipc_callid_t iid, ipc_call_t * icall)
     1675{
     1676        /*
     1677         * Accept the connection
     1678         *  - Answer the first IPC_M_CONNECT_ME_TO call.
     1679         */
     1680        ipc_answer_0(iid, EOK);
     1681       
     1682        while(true) {
     1683                ipc_call_t answer;
     1684                int answer_count;
     1685               
     1686                /* Clear the answer structure */
     1687                refresh_answer(&answer, &answer_count);
     1688               
     1689                /* Fetch the next message */
     1690                ipc_call_t call;
     1691                ipc_callid_t callid = async_get_call(&call);
     1692               
     1693                /* Process the message */
     1694                int res = il_module_message_standalone(callid, &call, &answer,
     1695                    &answer_count);
     1696               
     1697                /* End if said to either by the message or the processing result */
     1698                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
     1699                        return;
     1700               
     1701                /* Answer the message */
     1702                answer_call(callid, res, &answer, answer_count);
     1703        }
     1704}
     1705
     1706/** Starts the module.
     1707 *
     1708 *  @param argc The count of the command line arguments. Ignored parameter.
     1709 *  @param argv The command line parameters. Ignored parameter.
     1710 *
     1711 *  @returns EOK on success.
     1712 *  @returns Other error codes as defined for each specific module start function.
     1713 *
     1714 */
     1715int main(int argc, char *argv[])
     1716{
     1717        ERROR_DECLARE;
     1718       
     1719        /* Start the module */
     1720        if (ERROR_OCCURRED(il_module_start_standalone(il_client_connection)))
     1721                return ERROR_CODE;
     1722       
     1723        return EOK;
     1724}
     1725
    16191726/** @}
    16201727 */
Note: See TracChangeset for help on using the changeset viewer.