Ignore:
File:
1 edited

Legend:

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

    r065d2d5 r08042bd  
    5151#include <adt/dynamic_fifo.h>
    5252#include <errno.h>
    53 #include <err.h>
    5453
    5554#include <net/socket_codes.h>
     
    103102int udp_initialize(async_client_conn_t client_connection)
    104103{
    105         ERROR_DECLARE;
    106 
    107104        measured_string_t names[] = {
    108105                {
     
    118115        size_t count = sizeof(names) / sizeof(measured_string_t);
    119116        char *data;
     117        int rc;
    120118
    121119        fibril_rwlock_initialize(&udp_globals.lock);
     
    124122        udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
    125123            ICMP_CONNECT_TIMEOUT);
     124       
    126125        udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
    127126            SERVICE_UDP, client_connection);
    128         if (udp_globals.ip_phone < 0)
     127        if (udp_globals.ip_phone < 0) {
     128                fibril_rwlock_write_unlock(&udp_globals.lock);
    129129                return udp_globals.ip_phone;
     130        }
    130131
    131132        // read default packet dimensions
    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))) {
     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) {
    137148                socket_ports_destroy(&udp_globals.sockets);
    138                 return ERROR_CODE;
    139         }
     149                fibril_rwlock_write_unlock(&udp_globals.lock);
     150                return rc;
     151        }
     152       
    140153        udp_globals.packet_dimension.prefix += sizeof(udp_header_t);
    141154        udp_globals.packet_dimension.content -= sizeof(udp_header_t);
     
    147160        // get configuration
    148161        configuration = &names[0];
    149         ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration,
    150             count, &data));
     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       
    151170        if (configuration) {
    152171                if (configuration[0].value)
     
    201220udp_process_packet(device_id_t device_id, packet_t packet, services_t error)
    202221{
    203         ERROR_DECLARE;
    204 
    205222        size_t length;
    206223        size_t offset;
     
    219236        struct sockaddr *dest;
    220237        packet_dimension_ref packet_dimension;
     238        int rc;
    221239
    222240        switch (error) {
     
    232250                        return udp_release_and_return(packet, result);
    233251                length = (size_t) result;
    234                 if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
    235                         return udp_release_and_return(packet,
    236                             ERROR_CODE);
     252                rc = packet_trim(packet, length, 0);
     253                if (rc != EOK)
     254                        return udp_release_and_return(packet, rc);
    237255                break;
    238256        default:
     
    253271
    254272        // trim all but UDP header
    255         if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
    256                 return udp_release_and_return(packet, ERROR_CODE);
     273        rc = packet_trim(packet, offset, 0);
     274        if (rc != EOK)
     275                return udp_release_and_return(packet, rc);
    257276
    258277        // get udp header
     
    284303                if (result <= 0)
    285304                        return udp_release_and_return(packet, result);
    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);
     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);
    291310                } else {
    292311                        checksum = compute_checksum(0, ip_header, length);
     
    307326
    308327                if (total_length < length) {
    309                         if (ERROR_OCCURRED(packet_trim(next_packet, 0,
    310                             length - total_length))) {
    311                                 return udp_release_and_return(packet,
    312                                     ERROR_CODE);
    313                         }
     328                        rc = packet_trim(next_packet, 0, length - total_length);
     329                        if (rc != EOK)
     330                                return udp_release_and_return(packet, rc);
    314331
    315332                        // add partial checksum if set
     
    360377
    361378        // queue the received packet
    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         }
     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);
    368388
    369389        // notify the destination socket
     
    436456    size_t *data_fragment_size, int flags)
    437457{
    438         ERROR_DECLARE;
    439 
    440458        socket_core_ref socket;
    441459        packet_t packet;
     
    451469        device_id_t device_id;
    452470        packet_dimension_ref packet_dimension;
    453 
    454         ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &dest_port));
     471        int rc;
     472       
     473        rc = tl_get_address_port(addr, addrlen, &dest_port);
     474        if (rc != EOK)
     475                return rc;
    455476
    456477        socket = socket_cores_find(local_sockets, socket_id);
     
    460481        if ((socket->port <= 0) && udp_globals.autobinding) {
    461482                // bind the socket to a random free port if not bound
    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);
     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;
    486490        }
    487491
    488492        if (udp_globals.checksum_computing) {
    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                 }
     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;
    494497                // get the device packet dimension
    495 //              ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone,
    496 //                  &udp_globals.dimensions, device_id, &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;
    497502        }
    498503//      } else {
    499504                // do not ask all the time
    500                 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
    501                     &udp_globals.packet_dimension));
     505                rc = ip_packet_size_req(udp_globals.ip_phone, -1,
     506                    &udp_globals.packet_dimension);
     507                if (rc != EOK)
     508                        return rc;
    502509                packet_dimension = &udp_globals.packet_dimension;
    503510//      }
     
    529536                        return udp_release_and_return(packet, result);
    530537
    531                 if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0)))
    532                         return udp_release_and_return(packet, ERROR_CODE);
     538                rc = pq_add(&packet, next_packet, index, 0);
     539                if (rc != EOK)
     540                        return udp_release_and_return(packet, rc);
    533541
    534542                total_length += (size_t) result;
     
    547555        if (udp_globals.checksum_computing) {
    548556                // update the pseudo header
    549                 if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
    550                     ip_header, headerlen, total_length + UDP_HEADER_SIZE))) {
     557                rc = ip_client_set_pseudo_header_data_length(ip_header,
     558                    headerlen, total_length + UDP_HEADER_SIZE);
     559                if (rc != EOK) {
    551560                        free(ip_header);
    552                         return udp_release_and_return(packet, ERROR_CODE);
     561                        return udp_release_and_return(packet, rc);
    553562                }
    554563
     
    565574
    566575        // prepare the first packet fragment
    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         }
     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. */
     581        fibril_rwlock_write_unlock(&udp_globals.lock);
    571582
    572583        // send the packet
    573         fibril_rwlock_write_unlock(&udp_globals.lock);
    574584        ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
    575585
     
    600610    size_t *addrlen)
    601611{
    602         ERROR_DECLARE;
    603 
    604612        socket_core_ref socket;
    605613        int packet_id;
     
    610618        uint8_t *data;
    611619        int result;
     620        int rc;
    612621
    613622        // find the socket
     
    620629        if (packet_id < 0)
    621630                return NO_DATA;
    622 
    623         ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet,
    624             packet_id));
     631       
     632        rc = packet_translate_remote(udp_globals.net_phone, &packet, packet_id);
     633        if (rc != EOK) {
     634                (void) dyn_fifo_pop(&socket->received);
     635                return rc;
     636        }
    625637
    626638        // get udp header
    627639        data = packet_get_data(packet);
    628640        if (!data) {
    629                 pq_release_remote(udp_globals.net_phone, packet_id);
    630                 return NO_DATA;
     641                (void) dyn_fifo_pop(&socket->received);
     642                return udp_release_and_return(packet, NO_DATA);
    631643        }
    632644        header = (udp_header_ref) data;
     
    634646        // set the source address port
    635647        result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
    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;
     648        rc = tl_set_address_port(addr, result, ntohs(header->source_port));
     649        if (rc != EOK) {
     650                (void) dyn_fifo_pop(&socket->received);
     651                return udp_release_and_return(packet, rc);
    640652        }
    641653        *addrlen = (size_t) result;
    642654
    643655        // send the source address
    644         ERROR_PROPAGATE(data_reply(addr, *addrlen));
     656        rc = data_reply(addr, *addrlen);
     657        switch (rc) {
     658        case EOK:
     659                break;
     660        case EOVERFLOW:
     661                return rc;
     662        default:
     663                (void) dyn_fifo_pop(&socket->received);
     664                return udp_release_and_return(packet, rc);
     665        }
    645666
    646667        // trim the header
    647         ERROR_PROPAGATE(packet_trim(packet, UDP_HEADER_SIZE, 0));
     668        rc = packet_trim(packet, UDP_HEADER_SIZE, 0);
     669        if (rc != EOK) {
     670                (void) dyn_fifo_pop(&socket->received);
     671                return udp_release_and_return(packet, rc);
     672        }
    648673
    649674        // reply the packets
    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;
     675        rc = socket_reply_packets(packet, &length);
     676        switch (rc) {
     677        case EOK:
     678                break;
     679        case EOVERFLOW:
     680                return rc;
     681        default:
     682                (void) dyn_fifo_pop(&socket->received);
     683                return udp_release_and_return(packet, rc);
     684        }
     685
     686        (void) dyn_fifo_pop(&socket->received);
     687
     688        // release the packet and return the total length
     689        return udp_release_and_return(packet, (int) length);
    658690}
    659691
     
    826858    ipc_call_t *answer, int *answer_count)
    827859{
    828         ERROR_DECLARE;
    829 
    830860        packet_t packet;
     861        int rc;
    831862
    832863        *answer_count = 0;
     
    834865        switch (IPC_GET_METHOD(*call)) {
    835866        case NET_TL_RECEIVED:
    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        
     867                rc = packet_translate_remote(udp_globals.net_phone, &packet,
     868                    IPC_GET_PACKET(call));
     869                if (rc != EOK)
     870                        return rc;
     871                return udp_received_msg(IPC_GET_DEVICE(call), packet,
     872                    SERVICE_UDP, IPC_GET_ERROR(call));
    843873        case IPC_M_CONNECT_TO_ME:
    844874                return udp_process_client_messages(callid, * call);
     
    850880/** Default thread for new connections.
    851881 *
    852  *  @param[in] iid      The initial message identifier.
    853  *  @param[in] icall    The initial message call structure.
    854  *
     882 * @param[in] iid       The initial message identifier.
     883 * @param[in] icall     The initial message call structure.
    855884 */
    856885static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall)
     
    898927int main(int argc, char *argv[])
    899928{
    900         ERROR_DECLARE;
     929        int rc;
    901930       
    902931        /* Start the module */
    903         if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection)))
    904                 return ERROR_CODE;
    905        
    906         return EOK;
     932        rc = tl_module_start_standalone(tl_client_connection);
     933        return rc;
    907934}
    908935
Note: See TracChangeset for help on using the changeset viewer.