Ignore:
File:
1 edited

Legend:

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

    ra63ff7d r065d2d5  
    5151#include <adt/dynamic_fifo.h>
    5252#include <errno.h>
     53#include <err.h>
    5354
    5455#include <net/socket_codes.h>
     
    102103int udp_initialize(async_client_conn_t client_connection)
    103104{
     105        ERROR_DECLARE;
     106
    104107        measured_string_t names[] = {
    105108                {
     
    115118        size_t count = sizeof(names) / sizeof(measured_string_t);
    116119        char *data;
    117         int rc;
    118120
    119121        fibril_rwlock_initialize(&udp_globals.lock);
     
    122124        udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
    123125            ICMP_CONNECT_TIMEOUT);
    124        
    125126        udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
    126127            SERVICE_UDP, client_connection);
    127         if (udp_globals.ip_phone < 0) {
    128                 fibril_rwlock_write_unlock(&udp_globals.lock);
     128        if (udp_globals.ip_phone < 0)
    129129                return udp_globals.ip_phone;
    130         }
    131130
    132131        // read default packet dimensions
    133         rc = ip_packet_size_req(udp_globals.ip_phone, -1,
    134             &udp_globals.packet_dimension);
    135         if (rc != EOK) {
    136                 fibril_rwlock_write_unlock(&udp_globals.lock);
    137                 return rc;
    138         }
    139        
    140         rc = socket_ports_initialize(&udp_globals.sockets);
    141         if (rc != EOK) {
    142                 fibril_rwlock_write_unlock(&udp_globals.lock);
    143                 return rc;
    144         }
    145        
    146         rc = packet_dimensions_initialize(&udp_globals.dimensions);
    147         if (rc != EOK) {
     132        ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
     133            &udp_globals.packet_dimension));
     134        ERROR_PROPAGATE(socket_ports_initialize(&udp_globals.sockets));
     135        if (ERROR_OCCURRED(packet_dimensions_initialize(
     136            &udp_globals.dimensions))) {
    148137                socket_ports_destroy(&udp_globals.sockets);
    149                 fibril_rwlock_write_unlock(&udp_globals.lock);
    150                 return rc;
    151         }
    152        
     138                return ERROR_CODE;
     139        }
    153140        udp_globals.packet_dimension.prefix += sizeof(udp_header_t);
    154141        udp_globals.packet_dimension.content -= sizeof(udp_header_t);
     
    160147        // get configuration
    161148        configuration = &names[0];
    162         rc = net_get_conf_req(udp_globals.net_phone, &configuration, count,
    163             &data);
    164         if (rc != EOK) {
    165                 socket_ports_destroy(&udp_globals.sockets);
    166                 fibril_rwlock_write_unlock(&udp_globals.lock);
    167                 return rc;
    168         }
    169        
     149        ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration,
     150            count, &data));
    170151        if (configuration) {
    171152                if (configuration[0].value)
     
    220201udp_process_packet(device_id_t device_id, packet_t packet, services_t error)
    221202{
     203        ERROR_DECLARE;
     204
    222205        size_t length;
    223206        size_t offset;
     
    236219        struct sockaddr *dest;
    237220        packet_dimension_ref packet_dimension;
    238         int rc;
    239221
    240222        switch (error) {
     
    250232                        return udp_release_and_return(packet, result);
    251233                length = (size_t) result;
    252                 rc = packet_trim(packet, length, 0);
    253                 if (rc != EOK)
    254                         return udp_release_and_return(packet, rc);
     234                if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
     235                        return udp_release_and_return(packet,
     236                            ERROR_CODE);
    255237                break;
    256238        default:
     
    271253
    272254        // trim all but UDP header
    273         rc = packet_trim(packet, offset, 0);
    274         if (rc != EOK)
    275                 return udp_release_and_return(packet, rc);
     255        if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
     256                return udp_release_and_return(packet, ERROR_CODE);
    276257
    277258        // get udp header
     
    303284                if (result <= 0)
    304285                        return udp_release_and_return(packet, result);
    305                
    306                 rc = ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest,
    307                     result, total_length, &ip_header, &length);
    308                 if (rc != EOK) {
    309                         return udp_release_and_return(packet, rc);
     286
     287                if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP,
     288                    src, result, dest, result, total_length, &ip_header,
     289                    &length))) {
     290                        return udp_release_and_return(packet, ERROR_CODE);
    310291                } else {
    311292                        checksum = compute_checksum(0, ip_header, length);
     
    326307
    327308                if (total_length < length) {
    328                         rc = packet_trim(next_packet, 0, length - total_length);
    329                         if (rc != EOK)
    330                                 return udp_release_and_return(packet, rc);
     309                        if (ERROR_OCCURRED(packet_trim(next_packet, 0,
     310                            length - total_length))) {
     311                                return udp_release_and_return(packet,
     312                                    ERROR_CODE);
     313                        }
    331314
    332315                        // add partial checksum if set
     
    377360
    378361        // queue the received packet
    379         rc = dyn_fifo_push(&socket->received, packet_get_id(packet),
    380             SOCKET_MAX_RECEIVED_SIZE);
    381         if (rc != EOK)
    382                 return udp_release_and_return(packet, rc);
    383                
    384         rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
    385             &udp_globals.dimensions, device_id, &packet_dimension);
    386         if (rc != EOK)
    387                 return udp_release_and_return(packet, rc);
     362        if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
     363            packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
     364            ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone,
     365            &udp_globals.dimensions, device_id, &packet_dimension))) {
     366                return udp_release_and_return(packet, ERROR_CODE);
     367        }
    388368
    389369        // notify the destination socket
     
    456436    size_t *data_fragment_size, int flags)
    457437{
     438        ERROR_DECLARE;
     439
    458440        socket_core_ref socket;
    459441        packet_t packet;
     
    469451        device_id_t device_id;
    470452        packet_dimension_ref packet_dimension;
    471         int rc;
    472        
    473         rc = tl_get_address_port(addr, addrlen, &dest_port);
    474         if (rc != EOK)
    475                 return rc;
     453
     454        ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &dest_port));
    476455
    477456        socket = socket_cores_find(local_sockets, socket_id);
     
    481460        if ((socket->port <= 0) && udp_globals.autobinding) {
    482461                // bind the socket to a random free port if not bound
    483                 rc = socket_bind_free_port(&udp_globals.sockets, socket,
    484                     UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
    485                     udp_globals.last_used_port);
    486                 if (rc != EOK)
    487                         return rc;
    488                 // set the next port as the search starting port number
    489                 udp_globals.last_used_port = socket->port;
     462//              do {
     463                        // try to find a free port
     464//                      fibril_rwlock_read_unlock(&udp_globals.lock);
     465//                      fibril_rwlock_write_lock(&udp_globals.lock);
     466                        // might be changed in the meantime
     467//                      if (socket->port <= 0) {
     468                                if (ERROR_OCCURRED(socket_bind_free_port(
     469                                    &udp_globals.sockets, socket,
     470                                    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
     471                                    udp_globals.last_used_port))) {
     472//                                      fibril_rwlock_write_unlock(
     473//                                          &udp_globals.lock);
     474//                                      fibril_rwlock_read_lock(
     475//                                          &udp_globals.lock);
     476                                        return ERROR_CODE;
     477                                }
     478                                // set the next port as the search starting port
     479                                // number
     480                                udp_globals.last_used_port = socket->port;
     481//                      }
     482//                      fibril_rwlock_write_unlock(&udp_globals.lock);
     483//                      fibril_rwlock_read_lock(&udp_globals.lock);
     484                        // might be changed in the meantime
     485//              } while (socket->port <= 0);
    490486        }
    491487
    492488        if (udp_globals.checksum_computing) {
    493                 rc = ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr,
    494                     addrlen, &device_id, &ip_header, &headerlen);
    495                 if (rc != EOK)
    496                         return rc;
     489                if (ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone,
     490                    IPPROTO_UDP, addr, addrlen, &device_id, &ip_header,
     491                    &headerlen))) {
     492                        return udp_release_and_return(packet, ERROR_CODE);
     493                }
    497494                // get the device packet dimension
    498 //              rc = tl_get_ip_packet_dimension(udp_globals.ip_phone,
    499 //                  &udp_globals.dimensions, device_id, &packet_dimension);
    500 //              if (rc != EOK)
    501 //                      return rc;
     495//              ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone,
     496//                  &udp_globals.dimensions, device_id, &packet_dimension));
    502497        }
    503498//      } else {
    504499                // do not ask all the time
    505                 rc = ip_packet_size_req(udp_globals.ip_phone, -1,
    506                     &udp_globals.packet_dimension);
    507                 if (rc != EOK)
    508                         return rc;
     500                ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
     501                    &udp_globals.packet_dimension));
    509502                packet_dimension = &udp_globals.packet_dimension;
    510503//      }
     
    536529                        return udp_release_and_return(packet, result);
    537530
    538                 rc = pq_add(&packet, next_packet, index, 0);
    539                 if (rc != EOK)
    540                         return udp_release_and_return(packet, rc);
     531                if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0)))
     532                        return udp_release_and_return(packet, ERROR_CODE);
    541533
    542534                total_length += (size_t) result;
     
    555547        if (udp_globals.checksum_computing) {
    556548                // update the pseudo header
    557                 rc = ip_client_set_pseudo_header_data_length(ip_header,
    558                     headerlen, total_length + UDP_HEADER_SIZE);
    559                 if (rc != EOK) {
     549                if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
     550                    ip_header, headerlen, total_length + UDP_HEADER_SIZE))) {
    560551                        free(ip_header);
    561                         return udp_release_and_return(packet, rc);
     552                        return udp_release_and_return(packet, ERROR_CODE);
    562553                }
    563554
     
    574565
    575566        // prepare the first packet fragment
    576         rc = ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0);
    577         if (rc != EOK)
    578                 return udp_release_and_return(packet, rc);
    579 
    580         /* Release the UDP global lock on success. */
     567        if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0,
     568            0, 0))) {
     569                return udp_release_and_return(packet, ERROR_CODE);
     570        }
     571
     572        // send the packet
    581573        fibril_rwlock_write_unlock(&udp_globals.lock);
    582 
    583         // send the packet
    584574        ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
    585575
     
    610600    size_t *addrlen)
    611601{
     602        ERROR_DECLARE;
     603
    612604        socket_core_ref socket;
    613605        int packet_id;
     
    618610        uint8_t *data;
    619611        int result;
    620         int rc;
    621612
    622613        // find the socket
     
    626617
    627618        // get the next received packet
    628         packet_id = dyn_fifo_pop(&socket->received);
     619        packet_id = dyn_fifo_value(&socket->received);
    629620        if (packet_id < 0)
    630621                return NO_DATA;
    631        
    632         rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
    633         if (rc != EOK)
    634                 return rc;
     622
     623        ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet,
     624            packet_id));
    635625
    636626        // get udp header
    637627        data = packet_get_data(packet);
    638         if (!data)
    639                 return udp_release_and_return(packet, NO_DATA);
     628        if (!data) {
     629                pq_release_remote(udp_globals.net_phone, packet_id);
     630                return NO_DATA;
     631        }
    640632        header = (udp_header_ref) data;
    641633
    642634        // set the source address port
    643635        result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
    644         rc = tl_set_address_port(addr, result, ntohs(header->source_port));
    645         if (rc != EOK)
    646                 return udp_release_and_return(packet, rc);
     636        if (ERROR_OCCURRED(tl_set_address_port(addr, result,
     637            ntohs(header->source_port)))) {
     638                pq_release_remote(udp_globals.net_phone, packet_id);
     639                return ERROR_CODE;
     640        }
    647641        *addrlen = (size_t) result;
    648642
    649643        // send the source address
    650         rc = data_reply(addr, *addrlen);
    651         if (rc != EOK)
    652                 return udp_release_and_return(packet, rc);
     644        ERROR_PROPAGATE(data_reply(addr, *addrlen));
    653645
    654646        // trim the header
    655         rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
    656         if (rc != EOK)
    657                 return udp_release_and_return(packet, rc);
     647        ERROR_PROPAGATE(packet_trim(packet, UDP_HEADER_SIZE, 0));
    658648
    659649        // reply the packets
    660         rc = socket_reply_packets(packet, &length);
    661         if (rc != EOK)
    662                 return udp_release_and_return(packet, rc);
    663 
    664         // release the packet and return the total length
    665         return udp_release_and_return(packet, (int) length);
     650        ERROR_PROPAGATE(socket_reply_packets(packet, &length));
     651
     652        // release the packet
     653        dyn_fifo_pop(&socket->received);
     654        pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
     655
     656        // return the total length
     657        return (int) length;
    666658}
    667659
     
    834826    ipc_call_t *answer, int *answer_count)
    835827{
     828        ERROR_DECLARE;
     829
    836830        packet_t packet;
    837         int rc;
    838831
    839832        *answer_count = 0;
     
    841834        switch (IPC_GET_METHOD(*call)) {
    842835        case NET_TL_RECEIVED:
    843                 rc = packet_translate_remote(udp_globals.net_phone, &packet,
    844                     IPC_GET_PACKET(call));
    845                 if (rc != EOK)
    846                         return rc;
    847                 return udp_received_msg(IPC_GET_DEVICE(call), packet,
    848                     SERVICE_UDP, IPC_GET_ERROR(call));
     836                if (ERROR_NONE(packet_translate_remote(udp_globals.net_phone,
     837                    &packet, IPC_GET_PACKET(call)))) {
     838                        ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call),
     839                            packet, SERVICE_UDP, IPC_GET_ERROR(call));
     840                }
     841                return ERROR_CODE;
     842       
    849843        case IPC_M_CONNECT_TO_ME:
    850844                return udp_process_client_messages(callid, * call);
     
    856850/** Default thread for new connections.
    857851 *
    858  * @param[in] iid       The initial message identifier.
    859  * @param[in] icall     The initial message call structure.
     852 *  @param[in] iid      The initial message identifier.
     853 *  @param[in] icall    The initial message call structure.
     854 *
    860855 */
    861856static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)
     
    903898int main(int argc, char *argv[])
    904899{
    905         int rc;
     900        ERROR_DECLARE;
    906901       
    907902        /* Start the module */
    908         rc = tl_module_start_standalone(tl_client_connection);
    909         return rc;
     903        if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
     904                return ERROR_CODE;
     905       
     906        return EOK;
    910907}
    911908
Note: See TracChangeset for help on using the changeset viewer.