Ignore:
File:
1 edited

Legend:

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

    r14f1db0 r1bfd3d3  
    3636 */
    3737
     38#include "net.h"
     39
    3840#include <async.h>
    3941#include <ctype.h>
     
    4648#include <ipc/ipc.h>
    4749#include <ipc/services.h>
    48 
    49 #include <net_err.h>
    50 #include <net_messages.h>
    51 #include <net_modules.h>
     50#include <ipc/net.h>
     51#include <ipc/net_net.h>
     52#include <ipc/il.h>
     53
     54#include <net/modules.h>
     55#include <net/packet.h>
     56#include <net/device.h>
     57
    5258#include <adt/char_map.h>
    5359#include <adt/generic_char_map.h>
    5460#include <adt/measured_strings.h>
    5561#include <adt/module_map.h>
    56 #include <packet/packet.h>
    57 #include <il_messages.h>
     62
    5863#include <netif_remote.h>
    59 #include <net_device.h>
    6064#include <nil_interface.h>
    6165#include <net_interface.h>
    6266#include <ip_interface.h>
    63 #include <net_net_messages.h>
    64 
    65 #include "net.h"
    66 
    67 /** Networking module name.
    68  *
    69  */
     67
     68/** Networking module name. */
    7069#define NAME  "net"
    7170
    72 /** File read buffer size.
    73  *
    74  */
     71/** File read buffer size. */
    7572#define BUFFER_SIZE  256
    7673
    77 /** Networking module global data.
    78  *
    79  */
     74/** Networking module global data. */
    8075net_globals_t net_globals;
    8176
    8277GENERIC_CHAR_MAP_IMPLEMENT(measured_strings, measured_string_t);
    8378DEVICE_MAP_IMPLEMENT(netifs, netif_t);
     79
     80static int startup(void);
    8481
    8582/** Add the configured setting to the configuration map.
     
    8986 * @param[in] value         The setting value.
    9087 *
    91  * @returns EOK on success.
    92  * @returns ENOMEM if there is not enough memory left.
    93  *
    94  */
    95 int add_configuration(measured_strings_ref configuration, const char *name,
     88 * @return EOK on success.
     89 * @return ENOMEM if there is not enough memory left.
     90 *
     91 */
     92int add_configuration(measured_strings_t *configuration, const char *name,
    9693    const char *value)
    9794{
    98         ERROR_DECLARE;
    99        
    100         measured_string_ref setting =
     95        int rc;
     96       
     97        measured_string_t *setting =
    10198            measured_string_create_bulk(value, 0);
    102        
    10399        if (!setting)
    104100                return ENOMEM;
    105101       
    106102        /* Add the configuration setting */
    107         if (ERROR_OCCURRED(measured_strings_add(configuration, name, 0, setting))) {
     103        rc = measured_strings_add(configuration, name, 0, setting);
     104        if (rc != EOK) {
    108105                free(setting);
    109                 return ERROR_CODE;
     106                return rc;
    110107        }
    111108       
     
    115112/** Generate new system-unique device identifier.
    116113 *
    117  * @returns The system-unique devic identifier.
    118  *
     114 * @return              The system-unique devic identifier.
    119115 */
    120116static device_id_t generate_new_device_id(void)
     
    123119}
    124120
    125 static int parse_line(measured_strings_ref configuration, char *line)
    126 {
    127         ERROR_DECLARE;
     121static int parse_line(measured_strings_t *configuration, char *line)
     122{
     123        int rc;
    128124       
    129125        /* From the beginning */
     
    169165       
    170166        /* Create a bulk measured string till the end */
    171         measured_string_ref setting =
     167        measured_string_t *setting =
    172168            measured_string_create_bulk(value, 0);
    173169        if (!setting)
     
    175171       
    176172        /* Add the configuration setting */
    177         if (ERROR_OCCURRED(measured_strings_add(configuration, name, 0, setting))) {
     173        rc = measured_strings_add(configuration, name, 0, setting);
     174        if (rc != EOK) {
    178175                free(setting);
    179                 return ERROR_CODE;
     176                return rc;
    180177        }
    181178       
     
    184181
    185182static int read_configuration_file(const char *directory, const char *filename,
    186     measured_strings_ref configuration)
    187 {
    188         ERROR_DECLARE;
    189        
     183    measured_strings_t *configuration)
     184{
    190185        printf("%s: Reading configuration file %s/%s\n", NAME, directory, filename);
    191186       
     
    206201        unsigned int line_number = 0;
    207202        size_t index = 0;
    208         while ((!ferror(cfg)) && (!feof(cfg))) {
     203        while (!ferror(cfg) && !feof(cfg)) {
    209204                int read = fgetc(cfg);
    210205                if ((read > 0) && (read != '\n') && (read != '\r')) {
    211206                        if (index >= BUFFER_SIZE) {
    212207                                line[BUFFER_SIZE - 1] = '\0';
    213                                 fprintf(stderr, "%s: Configuration line %u too long: %s\n",
    214                                     NAME, line_number, line);
     208                                fprintf(stderr, "%s: Configuration line %u too "
     209                                    "long: %s\n", NAME, line_number, line);
    215210                               
    216211                                /* No space left in the line buffer */
    217212                                return EOVERFLOW;
    218                         } else {
    219                                 /* Append the character */
    220                                 line[index] = (char) read;
    221                                 index++;
    222213                        }
     214                        /* Append the character */
     215                        line[index] = (char) read;
     216                        index++;
    223217                } else {
    224218                        /* On error or new line */
    225219                        line[index] = '\0';
    226220                        line_number++;
    227                         if (ERROR_OCCURRED(parse_line(configuration, line)))
    228                                 fprintf(stderr, "%s: Configuration error on line %u: %s\n",
    229                                     NAME, line_number, line);
     221                        if (parse_line(configuration, line) != EOK) {
     222                                fprintf(stderr, "%s: Configuration error on "
     223                                    "line %u: %s\n", NAME, line_number, line);
     224                        }
    230225                       
    231226                        index = 0;
     
    242237 * @param[in,out] netif The network interface structure.
    243238 *
    244  * @returns EOK on success.
    245  * @returns Other error codes as defined for the add_configuration() function.
     239 * @return EOK on success.
     240 * @return Other error codes as defined for the add_configuration() function.
    246241 *
    247242 */
     
    253248/** Read the networking subsystem global configuration.
    254249 *
    255  * @returns EOK on success.
    256  * @returns Other error codes as defined for the add_configuration() function.
     250 * @return EOK on success.
     251 * @return Other error codes as defined for the add_configuration() function.
    257252 *
    258253 */
     
    269264 *                              its own one.
    270265 *
    271  * @returns EOK on success.
    272  * @returns ENOMEM if there is not enough memory left.
     266 * @return EOK on success.
     267 * @return ENOMEM if there is not enough memory left.
    273268 *
    274269 */
    275270static int net_initialize(async_client_conn_t client_connection)
    276271{
    277         ERROR_DECLARE;
     272        int rc;
    278273       
    279274        netifs_initialize(&net_globals.netifs);
     
    282277        measured_strings_initialize(&net_globals.configuration);
    283278       
    284         // TODO: dynamic configuration
    285         ERROR_PROPAGATE(read_configuration());
    286        
    287         ERROR_PROPAGATE(add_module(NULL, &net_globals.modules,
    288             LO_NAME, LO_FILENAME, SERVICE_LO, 0, connect_to_service));
    289         ERROR_PROPAGATE(add_module(NULL, &net_globals.modules,
    290             DP8390_NAME, DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service));
    291         ERROR_PROPAGATE(add_module(NULL, &net_globals.modules,
    292             ETHERNET_NAME, ETHERNET_FILENAME, SERVICE_ETHERNET, 0,
    293             connect_to_service));
    294         ERROR_PROPAGATE(add_module(NULL, &net_globals.modules,
    295             NILDUMMY_NAME, NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0,
    296             connect_to_service));
     279        /* TODO: dynamic configuration */
     280        rc = read_configuration();
     281        if (rc != EOK)
     282                return rc;
     283       
     284        rc = add_module(NULL, &net_globals.modules, LO_NAME, LO_FILENAME,
     285            SERVICE_LO, 0, connect_to_service);
     286        if (rc != EOK)
     287                return rc;
     288        rc = add_module(NULL, &net_globals.modules, DP8390_NAME,
     289            DP8390_FILENAME, SERVICE_DP8390, 0, connect_to_service);
     290        if (rc != EOK)
     291                return rc;
     292        rc = add_module(NULL, &net_globals.modules, ETHERNET_NAME,
     293            ETHERNET_FILENAME, SERVICE_ETHERNET, 0, connect_to_service);
     294        if (rc != EOK)
     295                return rc;
     296        rc = add_module(NULL, &net_globals.modules, NILDUMMY_NAME,
     297            NILDUMMY_FILENAME, SERVICE_NILDUMMY, 0, connect_to_service);
     298        if (rc != EOK)
     299                return rc;
    297300       
    298301        /* Build specific initialization */
     
    312315 *                              its own one.
    313316 *
    314  * @returns EOK on successful module termination.
    315  * @returns Other error codes as defined for the net_initialize() function.
    316  * @returns Other error codes as defined for the REGISTER_ME() macro function.
     317 * @return EOK on successful module termination.
     318 * @return Other error codes as defined for the net_initialize() function.
     319 * @return Other error codes as defined for the REGISTER_ME() macro function.
    317320 *
    318321 */
    319322static int net_module_start(async_client_conn_t client_connection)
    320323{
    321         ERROR_DECLARE;
     324        ipcarg_t phonehash;
     325        int rc;
    322326       
    323327        async_set_client_connection(client_connection);
    324         ERROR_PROPAGATE(pm_init());
    325        
    326         ipcarg_t phonehash;
    327        
    328         if (ERROR_OCCURRED(net_initialize(client_connection))
    329             || ERROR_OCCURRED(REGISTER_ME(SERVICE_NETWORKING, &phonehash))){
    330                 pm_destroy();
    331                 return ERROR_CODE;
    332         }
    333        
     328        rc = pm_init();
     329        if (rc != EOK)
     330                return rc;
     331       
     332       
     333        rc = net_initialize(client_connection);
     334        if (rc != EOK)
     335                goto out;
     336       
     337        rc = REGISTER_ME(SERVICE_NETWORKING, &phonehash);
     338        if (rc != EOK)
     339                goto out;
     340       
     341        rc = startup();
     342        if (rc != EOK)
     343                goto out;
     344       
     345        task_retval(0);
    334346        async_manager();
    335        
     347
     348out:
    336349        pm_destroy();
    337         return EOK;
     350        return rc;
    338351}
    339352
     
    341354 *
    342355 * The network interface configuration is searched first.
    343  &
     356 *
    344357 * @param[in]  netif_conf    The network interface configuration setting.
    345358 * @param[out] configuration The found configured values.
     
    347360 * @param[out] data          The found configuration settings data.
    348361 *
    349  * @returns EOK.
    350  *
    351  */
    352 static int net_get_conf(measured_strings_ref netif_conf,
    353     measured_string_ref configuration, size_t count, char **data)
     362 * @return EOK.
     363 *
     364 */
     365static int net_get_conf(measured_strings_t *netif_conf,
     366    measured_string_t *configuration, size_t count, char **data)
    354367{
    355368        if (data)
     
    358371        size_t index;
    359372        for (index = 0; index < count; index++) {
    360                 measured_string_ref setting =
     373                measured_string_t *setting =
    361374                    measured_strings_find(netif_conf, configuration[index].value, 0);
    362375                if (!setting)
     
    376389}
    377390
    378 int net_get_conf_req(int net_phone, measured_string_ref *configuration,
     391int net_get_conf_req(int net_phone, measured_string_t **configuration,
    379392    size_t count, char **data)
    380393{
    381         if (!(configuration && (count > 0)))
     394        if (!configuration || (count <= 0))
    382395                return EINVAL;
    383396       
     
    386399
    387400int net_get_device_conf_req(int net_phone, device_id_t device_id,
    388     measured_string_ref *configuration, size_t count, char **data)
     401    measured_string_t **configuration, size_t count, char **data)
    389402{
    390403        if ((!configuration) || (count == 0))
     
    398411}
    399412
    400 void net_free_settings(measured_string_ref settings, char *data)
     413void net_free_settings(measured_string_t *settings, char *data)
    401414{
    402415}
     
    409422 * @param[in] netif The network interface specific data.
    410423 *
    411  * @returns EOK on success.
    412  * @returns EINVAL if there are some settings missing.
    413  * @returns ENOENT if the internet protocol module is not known.
    414  * @returns Other error codes as defined for the netif_probe_req() function.
    415  * @returns Other error codes as defined for the nil_device_req() function.
    416  * @returns Other error codes as defined for the needed internet layer
    417  *          registering function.
     424 * @return EOK on success.
     425 * @return EINVAL if there are some settings missing.
     426 * @return ENOENT if the internet protocol module is not known.
     427 * @return Other error codes as defined for the netif_probe_req() function.
     428 * @return Other error codes as defined for the nil_device_req() function.
     429 * @return Other error codes as defined for the needed internet layer
     430 *         registering function.
    418431 *
    419432 */
    420433static int start_device(netif_t *netif)
    421434{
    422         ERROR_DECLARE;
     435        int rc;
    423436       
    424437        /* Mandatory netif */
    425         measured_string_ref setting =
     438        measured_string_t *setting =
    426439            measured_strings_find(&netif->configuration, CONF_NETIF, 0);
    427440       
     
    461474        int io = setting ? strtol(setting->value, NULL, 16) : 0;
    462475       
    463         ERROR_PROPAGATE(netif_probe_req_remote(netif->driver->phone, netif->id, irq, io));
     476        rc = netif_probe_req_remote(netif->driver->phone, netif->id, irq, io);
     477        if (rc != EOK)
     478                return rc;
    464479       
    465480        /* Network interface layer startup */
     
    473488                int mtu = setting ? strtol(setting->value, NULL, 10) : 0;
    474489               
    475                 ERROR_PROPAGATE(nil_device_req(netif->nil->phone, netif->id, mtu,
    476                     netif->driver->service));
     490                rc = nil_device_req(netif->nil->phone, netif->id, mtu,
     491                    netif->driver->service);
     492                if (rc != EOK)
     493                        return rc;
    477494               
    478495                internet_service = netif->nil->service;
     
    482499        /* Inter-network layer startup */
    483500        switch (netif->il->service) {
    484                 case SERVICE_IP:
    485                         ERROR_PROPAGATE(ip_device_req(netif->il->phone, netif->id,
    486                             internet_service));
    487                         break;
    488                 default:
    489                         return ENOENT;
    490         }
    491        
    492         ERROR_PROPAGATE(netif_start_req_remote(netif->driver->phone, netif->id));
    493         return EOK;
     501        case SERVICE_IP:
     502                rc = ip_device_req(netif->il->phone, netif->id,
     503                    internet_service);
     504                if (rc != EOK)
     505                        return rc;
     506                break;
     507        default:
     508                return ENOENT;
     509        }
     510       
     511        return netif_start_req_remote(netif->driver->phone, netif->id);
    494512}
    495513
    496514/** Read the configuration and start all network interfaces.
    497515 *
    498  * @returns EOK on success.
    499  * @returns EXDEV if there is no available system-unique device identifier.
    500  * @returns EINVAL if any of the network interface names are not configured.
    501  * @returns ENOMEM if there is not enough memory left.
    502  * @returns Other error codes as defined for the read_configuration()
    503  *          function.
    504  * @returns Other error codes as defined for the read_netif_configuration()
    505  *          function.
    506  * @returns Other error codes as defined for the start_device() function.
     516 * @return EOK on success.
     517 * @return EXDEV if there is no available system-unique device identifier.
     518 * @return EINVAL if any of the network interface names are not configured.
     519 * @return ENOMEM if there is not enough memory left.
     520 * @return Other error codes as defined for the read_configuration()
     521 *         function.
     522 * @return Other error codes as defined for the read_netif_configuration()
     523 *         function.
     524 * @return Other error codes as defined for the start_device() function.
    507525 *
    508526 */
    509527static int startup(void)
    510528{
    511         ERROR_DECLARE;
    512        
    513         const char *conf_files[] = {"lo", "ne2k"};
     529        const char *conf_files[] = {
     530                "lo",
     531                "ne2k"
     532        };
    514533        size_t count = sizeof(conf_files) / sizeof(char *);
     534        int rc;
    515535       
    516536        size_t i;
     
    524544                        return EXDEV;
    525545               
    526                 ERROR_PROPAGATE(measured_strings_initialize(&netif->configuration));
     546                rc = measured_strings_initialize(&netif->configuration);
     547                if (rc != EOK)
     548                        return rc;
    527549               
    528550                /* Read configuration files */
    529                 if (ERROR_OCCURRED(read_netif_configuration(conf_files[i], netif))) {
     551                rc = read_netif_configuration(conf_files[i], netif);
     552                if (rc != EOK) {
    530553                        measured_strings_destroy(&netif->configuration);
    531554                        free(netif);
    532                         return ERROR_CODE;
     555                        return rc;
    533556                }
    534557               
    535558                /* Mandatory name */
    536                 measured_string_ref setting =
     559                measured_string_t *setting =
    537560                    measured_strings_find(&netif->configuration, CONF_NAME, 0);
    538561                if (!setting) {
     
    556579                 * and needed modules.
    557580                 */
    558                 if ((ERROR_OCCURRED(char_map_add(&net_globals.netif_names,
    559                     netif->name, 0, index))) || (ERROR_OCCURRED(start_device(netif)))) {
     581                rc = char_map_add(&net_globals.netif_names, netif->name, 0,
     582                    index);
     583                if (rc != EOK) {
    560584                        measured_strings_destroy(&netif->configuration);
    561585                        netifs_exclude_index(&net_globals.netifs, index);
    562                         return ERROR_CODE;
     586                        return rc;
     587                }
     588               
     589                rc = start_device(netif);
     590                if (rc != EOK) {
     591                        measured_strings_destroy(&netif->configuration);
     592                        netifs_exclude_index(&net_globals.netifs, index);
     593                        return rc;
    563594                }
    564595               
     
    586617 *                          in the answer parameter.
    587618 *
    588  * @returns EOK on success.
    589  * @returns ENOTSUP if the message is not known.
     619 * @return EOK on success.
     620 * @return ENOTSUP if the message is not known.
    590621 *
    591622 * @see net_interface.h
     
    596627    int *answer_count)
    597628{
    598         ERROR_DECLARE;
    599        
    600         measured_string_ref strings;
     629        measured_string_t *strings;
    601630        char *data;
     631        int rc;
    602632       
    603633        *answer_count = 0;
    604634        switch (IPC_GET_METHOD(*call)) {
    605                 case IPC_M_PHONE_HUNGUP:
    606                         return EOK;
    607                 case NET_NET_GET_DEVICE_CONF:
    608                         ERROR_PROPAGATE(measured_strings_receive(&strings, &data,
    609                             IPC_GET_COUNT(call)));
    610                         net_get_device_conf_req(0, IPC_GET_DEVICE(call), &strings,
    611                             IPC_GET_COUNT(call), NULL);
    612                        
    613                         /* Strings should not contain received data anymore */
    614                         free(data);
    615                        
    616                         ERROR_CODE = measured_strings_reply(strings, IPC_GET_COUNT(call));
    617                         free(strings);
    618                         return ERROR_CODE;
    619                 case NET_NET_GET_CONF:
    620                         ERROR_PROPAGATE(measured_strings_receive(&strings, &data,
    621                             IPC_GET_COUNT(call)));
    622                         net_get_conf_req(0, &strings, IPC_GET_COUNT(call), NULL);
    623                        
    624                         /* Strings should not contain received data anymore */
    625                         free(data);
    626                        
    627                         ERROR_CODE = measured_strings_reply(strings, IPC_GET_COUNT(call));
    628                         free(strings);
    629                         return ERROR_CODE;
    630                 case NET_NET_STARTUP:
    631                         return startup();
    632         }
     635        case IPC_M_PHONE_HUNGUP:
     636                return EOK;
     637        case NET_NET_GET_DEVICE_CONF:
     638                rc = measured_strings_receive(&strings, &data,
     639                    IPC_GET_COUNT(call));
     640                if (rc != EOK)
     641                        return rc;
     642                net_get_device_conf_req(0, IPC_GET_DEVICE(call), &strings,
     643                    IPC_GET_COUNT(call), NULL);
     644               
     645                /* Strings should not contain received data anymore */
     646                free(data);
     647               
     648                rc = measured_strings_reply(strings, IPC_GET_COUNT(call));
     649                free(strings);
     650                return rc;
     651        case NET_NET_GET_CONF:
     652                rc = measured_strings_receive(&strings, &data,
     653                    IPC_GET_COUNT(call));
     654                if (rc != EOK)
     655                        return rc;
     656                net_get_conf_req(0, &strings, IPC_GET_COUNT(call), NULL);
     657               
     658                /* Strings should not contain received data anymore */
     659                free(data);
     660               
     661                rc = measured_strings_reply(strings, IPC_GET_COUNT(call));
     662                free(strings);
     663                return rc;
     664        case NET_NET_STARTUP:
     665                return startup();
     666        }
     667       
    633668        return ENOTSUP;
    634669}
     
    661696                int res = net_module_message(callid, &call, &answer, &answer_count);
    662697               
    663                 /* End if said to either by the message or the processing result */
     698                /* End if told to either by the message or the processing result */
    664699                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
    665700                        return;
     
    672707int main(int argc, char *argv[])
    673708{
    674         ERROR_DECLARE;
    675        
    676         if (ERROR_OCCURRED(net_module_start(net_client_connection))) {
    677                 fprintf(stderr, "%s: net_module_start error %i\n", NAME, ERROR_CODE);
    678                 return ERROR_CODE;
     709        int rc;
     710       
     711        rc = net_module_start(net_client_connection);
     712        if (rc != EOK) {
     713                fprintf(stderr, "%s: net_module_start error %i\n", NAME, rc);
     714                return rc;
    679715        }
    680716       
Note: See TracChangeset for help on using the changeset viewer.