Ignore:
File:
1 edited

Legend:

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

    r2e236901 rf5a3479  
    2828
    2929/** @addtogroup lo
    30  * @{
     30 *  @{
    3131 */
    3232
    3333/** @file
    34  * Loopback network interface implementation.
     34 *  Loopback network interface implementation.
    3535 */
    3636
    3737#include <async.h>
    3838#include <errno.h>
     39#include <err.h>
    3940#include <stdio.h>
    4041#include <str.h>
     
    5253#include <netif_local.h>
    5354
    54 /** Default hardware address. */
     55/** Default hardware address.
     56 */
    5557#define DEFAULT_ADDR            "\0\0\0\0\0\0"
    5658
    57 /** Default address length. */
     59/** Default address length.
     60 */
    5861#define DEFAULT_ADDR_LEN        (sizeof(DEFAULT_ADDR) / sizeof(char))
    5962
    60 /** Loopback module name. */
     63/** Loopback module name.
     64 */
    6165#define NAME  "lo"
    6266
    63 /** Network interface global data. */
     67/** Network interface global data.
     68 */
    6469netif_globals_t netif_globals;
    6570
    66 int
    67 netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
    68     ipc_call_t *answer, int *answer_count)
    69 {
     71/** Changes the loopback state.
     72 *  @param[in] device The device structure.
     73 *  @param[in] state The new device state.
     74 *  @returns The new state if changed.
     75 *  @returns EOK otherwise.
     76 */
     77int change_state_message(netif_device_t * device, device_state_t state);
     78
     79/** Creates and returns the loopback network interface structure.
     80 *  @param[in] device_id The new devce identifier.
     81 *  @param[out] device The device structure.
     82 *  @returns EOK on success.
     83 *  @returns EXDEV if one loopback network interface already exists.
     84 *  @returns ENOMEM if there is not enough memory left.
     85 */
     86int create(device_id_t device_id, netif_device_t * * device);
     87
     88int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
    7089        return ENOTSUP;
    7190}
    7291
    73 int netif_get_addr_message(device_id_t device_id, measured_string_ref address)
    74 {
    75         if (!address)
     92int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
     93        if(! address){
    7694                return EBADMEM;
     95        }
    7796        address->value = str_dup(DEFAULT_ADDR);
    7897        address->length = DEFAULT_ADDR_LEN;
     
    8099}
    81100
    82 int netif_get_device_stats(device_id_t device_id, device_stats_ref stats)
    83 {
    84         netif_device_t *device;
    85         int rc;
    86 
    87         if (!stats)
     101int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
     102        ERROR_DECLARE;
     103
     104        netif_device_t * device;
     105
     106        if(! stats){
    88107                return EBADMEM;
    89         rc = find_device(device_id, &device);
    90         if (rc != EOK)
    91                 return rc;
    92         memcpy(stats, (device_stats_ref) device->specific,
    93             sizeof(device_stats_t));
    94         return EOK;
    95 }
    96 
    97 /** Changes the loopback state.
    98  *
    99  * @param[in] device    The device structure.
    100  * @param[in] state     The new device state.
    101  * @returns             The new state if changed.
    102  * @returns             EOK otherwise.
    103  */
    104 static int change_state_message(netif_device_t *device, device_state_t state)
     108        }
     109        ERROR_PROPAGATE(find_device(device_id, &device));
     110        memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
     111        return EOK;
     112}
     113
     114int change_state_message(netif_device_t * device, device_state_t state)
    105115{
    106116        if (device->state != state) {
     
    116126}
    117127
    118 /** Creates and returns the loopback network interface structure.
    119  *
    120  * @param[in] device_id The new devce identifier.
    121  * @param[out] device   The device structure.
    122  * @returns             EOK on success.
    123  * @returns             EXDEV if one loopback network interface already exists.
    124  * @returns             ENOMEM if there is not enough memory left.
    125  */
    126 static int create(device_id_t device_id, netif_device_t **device)
    127 {
     128int create(device_id_t device_id, netif_device_t * * device){
    128129        int index;
    129130
    130         if (netif_device_map_count(&netif_globals.device_map) > 0)
     131        if(netif_device_map_count(&netif_globals.device_map) > 0){
    131132                return EXDEV;
    132 
    133         *device = (netif_device_t *) malloc(sizeof(netif_device_t));
    134         if (!*device)
    135                 return ENOMEM;
    136         (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
    137         if (!(*device)->specific) {
    138                 free(*device);
    139                 return ENOMEM;
    140         }
    141         null_device_stats((device_stats_ref) (*device)->specific);
    142         (*device)->device_id = device_id;
    143         (*device)->nil_phone = -1;
    144         (*device)->state = NETIF_STOPPED;
    145         index = netif_device_map_add(&netif_globals.device_map,
    146             (*device)->device_id, *device);
    147         if (index < 0) {
    148                 free(*device);
    149                 free((*device)->specific);
    150                 *device = NULL;
    151                 return index;
    152         }
    153        
    154         return EOK;
    155 }
    156 
    157 int netif_initialize(void)
    158 {
     133        }else{
     134                *device = (netif_device_t *) malloc(sizeof(netif_device_t));
     135                if(!(*device)){
     136                        return ENOMEM;
     137                }
     138                (** device).specific = malloc(sizeof(device_stats_t));
     139                if(! (** device).specific){
     140                        free(*device);
     141                        return ENOMEM;
     142                }
     143                null_device_stats((device_stats_ref)(** device).specific);
     144                (** device).device_id = device_id;
     145                (** device).nil_phone = -1;
     146                (** device).state = NETIF_STOPPED;
     147                index = netif_device_map_add(&netif_globals.device_map, (** device).device_id, * device);
     148                if(index < 0){
     149                        free(*device);
     150                        free((** device).specific);
     151                        *device = NULL;
     152                        return index;
     153                }
     154        }
     155        return EOK;
     156}
     157
     158int netif_initialize(void){
    159159        ipcarg_t phonehash;
    160160
     
    162162}
    163163
    164 int netif_probe_message(device_id_t device_id, int irq, uintptr_t io)
    165 {
    166         netif_device_t *device;
    167         int rc;
     164int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
     165        ERROR_DECLARE;
     166
     167        netif_device_t * device;
    168168
    169169        // create a new device
    170         rc = create(device_id, &device);
    171         if (rc != EOK)
    172                 return rc;
     170        ERROR_PROPAGATE(create(device_id, &device));
    173171        // print the settings
    174172        printf("%s: Device created (id: %d)\n", NAME, device->device_id);
     
    176174}
    177175
    178 int netif_send_message(device_id_t device_id, packet_t packet, services_t sender)
    179 {
    180         netif_device_t *device;
     176int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
     177        ERROR_DECLARE;
     178
     179        netif_device_t * device;
    181180        size_t length;
    182181        packet_t next;
    183182        int phone;
    184         int rc;
    185 
    186         rc = find_device(device_id, &device);
    187         if (rc != EOK)
    188                 return EOK;
    189         if (device->state != NETIF_ACTIVE) {
     183
     184        ERROR_PROPAGATE(find_device(device_id, &device));
     185        if(device->state != NETIF_ACTIVE){
    190186                netif_pq_release(packet_get_id(packet));
    191187                return EFORWARD;
    192188        }
    193189        next = packet;
    194         do {
    195                 ((device_stats_ref) device->specific)->send_packets++;
    196                 ((device_stats_ref) device->specific)->receive_packets++;
     190        do{
     191                ++ ((device_stats_ref) device->specific)->send_packets;
     192                ++ ((device_stats_ref) device->specific)->receive_packets;
    197193                length = packet_get_data_length(next);
    198194                ((device_stats_ref) device->specific)->send_bytes += length;
    199195                ((device_stats_ref) device->specific)->receive_bytes += length;
    200196                next = pq_next(next);
    201         } while(next);
     197        }while(next);
    202198        phone = device->nil_phone;
    203199        fibril_rwlock_write_unlock(&netif_globals.lock);
    204200        nil_received_msg(phone, device_id, packet, sender);
    205201        fibril_rwlock_write_lock(&netif_globals.lock);
    206        
    207         return EOK;
    208 }
    209 
    210 int netif_start_message(netif_device_t *device)
    211 {
     202        return EOK;
     203}
     204
     205int netif_start_message(netif_device_t * device){
    212206        return change_state_message(device, NETIF_ACTIVE);
    213207}
    214208
    215 int netif_stop_message(netif_device_t *device)
    216 {
     209int netif_stop_message(netif_device_t * device){
    217210        return change_state_message(device, NETIF_STOPPED);
    218211}
     
    220213/** Default thread for new connections.
    221214 *
    222  * @param[in] iid       The initial message identifier.
    223  * @param[in] icall     The initial message call structure.
     215 * @param[in] iid The initial message identifier.
     216 * @param[in] icall The initial message call structure.
     217 *
    224218 */
    225219static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
     
    231225        ipc_answer_0(iid, EOK);
    232226       
    233         while (true) {
     227        while(true) {
    234228                ipc_call_t answer;
    235229                int answer_count;
     
    246240                    &answer_count);
    247241               
    248                 /*
    249                  * End if told to either by the message or the processing
    250                  * result.
    251                  */
    252                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
    253                     (res == EHANGUP))
     242                /* End if said to either by the message or the processing result */
     243                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
    254244                        return;
    255245               
     
    261251int main(int argc, char *argv[])
    262252{
    263         int rc;
     253        ERROR_DECLARE;
    264254       
    265255        /* Start the module */
    266         rc = netif_module_start(netif_client_connection);
    267         return rc;
     256        if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
     257                return ERROR_CODE;
     258       
     259        return EOK;
    268260}
    269261
Note: See TracChangeset for help on using the changeset viewer.