Changeset 2a786f9 in mainline


Ignore:
Timestamp:
2010-09-22T19:07:02Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ec1bdc8
Parents:
dc94cb2 (diff), 921a860 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jakub/helenos/net.

Location:
uspace
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/net/generic/net_remote.c

    rdc94cb2 r2a786f9  
    4747#include <net_net_messages.h>
    4848
    49 int net_connect_module(services_t service){
     49int net_connect_module(services_t service)
     50{
    5051        return connect_to_service(SERVICE_NETWORKING);
    5152}
    5253
    53 void net_free_settings(measured_string_ref settings, char * data){
    54         if(settings){
     54void net_free_settings(measured_string_ref settings, char * data)
     55{
     56        if (settings)
    5557                free(settings);
    56         }
    57         if(data){
     58
     59        if (data)
    5860                free(data);
    59         }
    6061}
    6162
    62 int net_get_conf_req(int net_phone, measured_string_ref * configuration, size_t count, char ** data){
    63         return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 0, 0, * configuration, count, configuration, data);
     63int
     64net_get_conf_req(int net_phone, measured_string_ref * configuration,
     65    size_t count, char ** data)
     66{
     67        return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, 0, 0,
     68            *configuration, count, configuration, data);
    6469}
    6570
    66 int net_get_device_conf_req(int net_phone, device_id_t device_id, measured_string_ref * configuration, size_t count, char ** data){
    67         return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF, device_id, 0, * configuration, count, configuration, data);
     71int
     72net_get_device_conf_req(int net_phone, device_id_t device_id,
     73    measured_string_ref * configuration, size_t count, char ** data)
     74{
     75        return generic_translate_req(net_phone, NET_NET_GET_DEVICE_CONF,
     76            device_id, 0, *configuration, count, configuration, data);
    6877}
    6978
  • uspace/lib/net/generic/packet_remote.c

    rdc94cb2 r2a786f9  
    6464 *
    6565 */
    66 static int packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size){
     66static int
     67packet_return(int phone, packet_ref packet, packet_id_t packet_id, size_t size)
     68{
    6769        ERROR_DECLARE;
    6870       
    6971        ipc_call_t answer;
    7072        aid_t message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer);
     73
    7174        *packet = (packet_t) as_get_mappable_page(size);
    7275        if (ERROR_OCCURRED(async_share_in_start_0_0(phone, *packet, size)) ||
     
    9194       
    9295        *packet = pm_find(packet_id);
    93         if (!(*packet)) {
     96        if (!*packet) {
    9497                ipcarg_t size;
    9598               
    96                 ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id, &size));
     99                ERROR_PROPAGATE(async_req_1_1(phone, NET_PACKET_GET_SIZE,
     100                    packet_id, &size));
    97101                ERROR_PROPAGATE(packet_return(phone, packet, packet_id, size));
    98102        }
    99         if ((** packet).next) {
     103        if ((*packet)->next) {
    100104                packet_t next;
    101105               
    102                 return packet_translate_remote(phone, &next, (** packet).next);
     106                return packet_translate_remote(phone, &next, (*packet)->next);
    103107        }
    104108       
     
    114118        ipcarg_t size;
    115119       
    116         if (ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4, max_content,
    117             addr_len, max_prefix, max_suffix, &packet_id, &size)))
     120        if (ERROR_OCCURRED(async_req_4_2(phone, NET_PACKET_CREATE_4,
     121            max_content, addr_len, max_prefix, max_suffix, &packet_id, &size)))
    118122                return NULL;
    119123       
     
    121125        packet_t packet = pm_find(packet_id);
    122126        if (!packet) {
    123                 if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size)))
     127                if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id,
     128                    size)))
    124129                        return NULL;
    125130        }
     
    141146        packet_t packet = pm_find(packet_id);
    142147        if (!packet) {
    143                 if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id, size)))
     148                if (ERROR_OCCURRED(packet_return(phone, &packet, packet_id,
     149                    size)))
    144150                        return NULL;
    145151        }
  • uspace/lib/net/il/ip_remote.c

    rdc94cb2 r2a786f9  
    127127                return EINVAL;
    128128       
    129         if ((!device_id) || (header) || (headerlen))
     129        if ((!device_id) || (!header) || (!headerlen))
    130130                return EBADMEM;
    131131       
  • uspace/lib/net/tl/tl_common.c

    rdc94cb2 r2a786f9  
    5858DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t);
    5959
    60 int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){
    61         const struct sockaddr_in * address_in;
    62         const struct sockaddr_in6 * address_in6;
    63 
    64         if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){
    65                 return EINVAL;
    66         }
    67         switch(addr->sa_family){
    68                 case AF_INET:
    69                         if(addrlen != sizeof(struct sockaddr_in)){
     60int
     61tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port)
     62{
     63        const struct sockaddr_in *address_in;
     64        const struct sockaddr_in6 *address_in6;
     65
     66        if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr)))
     67                return EINVAL;
     68
     69        switch (addr->sa_family) {
     70        case AF_INET:
     71                if (addrlen != sizeof(struct sockaddr_in))
     72                        return EINVAL;
     73
     74                address_in = (struct sockaddr_in *) addr;
     75                *port = ntohs(address_in->sin_port);
     76                break;
     77        case AF_INET6:
     78                if (addrlen != sizeof(struct sockaddr_in6))
    7079                                return EINVAL;
    71                         }
    72                         address_in = (struct sockaddr_in *) addr;
    73                         *port = ntohs(address_in->sin_port);
    74                         break;
    75                 case AF_INET6:
    76                         if(addrlen != sizeof(struct sockaddr_in6)){
    77                                 return EINVAL;
    78                         }
    79                         address_in6 = (struct sockaddr_in6 *) addr;
    80                         *port = ntohs(address_in6->sin6_port);
    81                         break;
    82                 default:
    83                         return EAFNOSUPPORT;
    84         }
     80
     81                address_in6 = (struct sockaddr_in6 *) addr;
     82                *port = ntohs(address_in6->sin6_port);
     83                break;
     84        default:
     85                return EAFNOSUPPORT;
     86        }
     87
    8588        return EOK;
    8689}
     
    112115                return EBADMEM;
    113116       
    114         *packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
     117        *packet_dimension = packet_dimensions_find(packet_dimensions,
     118            device_id);
    115119        if (!*packet_dimension) {
    116120                /* Ask for and remember them if not found */
     
    136140}
    137141
    138 int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){
     142int
     143tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions,
     144    device_id_t device_id, size_t content)
     145{
    139146        packet_dimension_ref packet_dimension;
    140147
    141148        packet_dimension = packet_dimensions_find(packet_dimensions, device_id);
    142         if(! packet_dimension){
     149        if (!packet_dimension)
    143150                return ENOENT;
    144         }
    145151        packet_dimension->content = content;
    146         if(device_id != DEVICE_INVALID_ID){
    147                 packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID);
    148                 if(packet_dimension){
    149                         if(packet_dimension->content >= content){
     152
     153        if (device_id != DEVICE_INVALID_ID) {
     154                packet_dimension = packet_dimensions_find(packet_dimensions,
     155                    DEVICE_INVALID_ID);
     156
     157                if (packet_dimension) {
     158                        if (packet_dimension->content >= content)
    150159                                packet_dimension->content = content;
    151                         }else{
    152                                 packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID);
    153                         }
     160                        else
     161                                packet_dimensions_exclude(packet_dimensions,
     162                                    DEVICE_INVALID_ID);
     163
    154164                }
    155165        }
     166
    156167        return EOK;
    157168}
     
    159170int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port)
    160171{
    161         struct sockaddr_in * address_in;
    162         struct sockaddr_in6 * address_in6;
     172        struct sockaddr_in *address_in;
     173        struct sockaddr_in6 *address_in6;
    163174        size_t length;
    164175
     
    188199}
    189200
    190 int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){
     201int
     202tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet,
     203    services_t error)
     204{
    191205        packet_t next;
    192         uint8_t * src;
     206        uint8_t *src;
    193207        int length;
    194208
     
    199213       
    200214        length = packet_get_addr(packet, &src, NULL);
    201         if((length > 0)
    202                 && (! error)
    203                 && (icmp_phone >= 0)
    204         // set both addresses to the source one (avoids the source address deletion before setting the destination one)
    205                 && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){
     215        if ((length > 0) && (!error) && (icmp_phone >= 0) &&
     216            // set both addresses to the source one (avoids the source address
     217            // deletion before setting the destination one)
     218            (packet_set_addr(packet, src, src, (size_t) length) == EOK)) {
    206219                return EOK;
    207         }else{
     220        } else
    208221                pq_release_remote(packet_phone, packet_get_id(packet));
    209         }
     222
    210223        return ENOENT;
    211224}
    212225
    213 int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){
     226int
     227tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix,
     228    const packet_dimension_ref dimension, const struct sockaddr *addr,
     229    socklen_t addrlen)
     230{
    214231        ERROR_DECLARE;
    215232
     
    218235        void * data;
    219236
    220         if(! dimension){
    221                 return EINVAL;
    222         }
     237        if (!dimension)
     238                return EINVAL;
     239
    223240        // get the data length
    224         if(! async_data_write_receive(&callid, &length)){
    225                 return EINVAL;
    226         }
     241        if (!async_data_write_receive(&callid, &length))
     242                return EINVAL;
     243
    227244        // get a new packet
    228         *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix);
    229         if(! packet){
     245        *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len,
     246            prefix + dimension->prefix, dimension->suffix);
     247        if (!packet)
    230248                return ENOMEM;
    231         }
     249
    232250        // allocate space in the packet
    233251        data = packet_suffix(*packet, length);
    234         if(! data){
     252        if (!data) {
    235253                pq_release_remote(packet_phone, packet_get_id(*packet));
    236254                return ENOMEM;
    237255        }
     256
    238257        // read the data into the packet
    239         if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length))
    240         // set the packet destination address
    241                 || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){
     258        if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) ||
     259            // set the packet destination address
     260            ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr,
     261            addrlen))) {
    242262                pq_release_remote(packet_phone, packet_get_id(*packet));
    243263                return ERROR_CODE;
    244264        }
     265
    245266        return (int) length;
    246267}
  • uspace/srv/net/tl/udp/udp.c

    rdc94cb2 r2a786f9  
    7272#include "udp_module.h"
    7373
    74 /** UDP module name.
    75  */
     74/** UDP module name. */
    7675#define NAME    "UDP protocol"
    7776
    78 /** Default UDP checksum computing.
    79  */
     77/** Default UDP checksum computing. */
    8078#define NET_DEFAULT_UDP_CHECKSUM_COMPUTING      true
    8179
    82 /** Default UDP autobind when sending via unbound sockets.
    83  */
     80/** Default UDP autobind when sending via unbound sockets. */
    8481#define NET_DEFAULT_UDP_AUTOBINDING     true
    8582
    86 /** Maximum UDP fragment size.
    87  */
    88 #define MAX_UDP_FRAGMENT_SIZE   65535
    89 
    90 /** Free ports pool start.
    91  */
    92 #define UDP_FREE_PORTS_START    1025
    93 
    94 /** Free ports pool end.
    95  */
     83/** Maximum UDP fragment size. */
     84#define MAX_UDP_FRAGMENT_SIZE           65535
     85
     86/** Free ports pool start. */
     87#define UDP_FREE_PORTS_START            1025
     88
     89/** Free ports pool end. */
    9690#define UDP_FREE_PORTS_END              65535
    9791
    9892/** Processes the received UDP packet queue.
     93 *
    9994 *  Is used as an entry point from the underlying IP module.
    10095 *  Locks the global lock and calls udp_process_packet() function.
     96 *
    10197 *  @param[in] device_id The receiving device identifier.
    10298 *  @param[in,out] packet The received packet queue.
    103  *  @param receiver The target service. Ignored parameter.
    104  *  @param[in] error The packet error reporting service. Prefixes the received packet.
    105  *  @returns EOK on success.
    106  *  @returns Other error codes as defined for the udp_process_packet() function.
    107  */
    108 int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error);
     99 *  @param receiver     The target service. Ignored parameter.
     100 *  @param[in] error    The packet error reporting service. Prefixes the
     101 *                      received packet.
     102 *  @returns            EOK on success.
     103 *  @returns            Other error codes as defined for the
     104 *                      udp_process_packet() function.
     105 */
     106int
     107udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
     108    services_t error);
    109109
    110110/** Processes the received UDP packet queue.
     111 *
    111112 *  Notifies the destination socket application.
    112  *  Releases the packet on error or sends an ICMP error notification..
     113 *  Releases the packet on error or sends an ICMP error notification.
     114 *
    113115 *  @param[in] device_id The receiving device identifier.
    114116 *  @param[in,out] packet The received packet queue.
    115  *  @param[in] error The packet error reporting service. Prefixes the received packet.
    116  *  @returns EOK on success.
    117  *  @returns EINVAL if the packet is not valid.
    118  *  @returns EINVAL if the stored packet address is not the an_addr_t.
    119  *  @returns EINVAL if the packet does not contain any data.
    120  *  @returns NO_DATA if the packet content is shorter than the user datagram header.
    121  *  @returns ENOMEM if there is not enough memory left.
    122  *  @returns EADDRNOTAVAIL if the destination socket does not exist.
    123  *  @returns Other error codes as defined for the ip_client_process_packet() function.
    124  */
    125 int udp_process_packet(device_id_t device_id, packet_t packet, services_t error);
     117 *  @param[in] error    The packet error reporting service. Prefixes the
     118 *                      received packet.
     119 *  @returns            EOK on success.
     120 *  @returns            EINVAL if the packet is not valid.
     121 *  @returns            EINVAL if the stored packet address is not the
     122 *                      an_addr_t.
     123 *  @returns            EINVAL if the packet does not contain any data.
     124 *  @returns            NO_DATA if the packet content is shorter than the user
     125 *                      datagram header.
     126 *  @returns            ENOMEM if there is not enough memory left.
     127 *  @returns            EADDRNOTAVAIL if the destination socket does not exist.
     128 *  @returns            Other error codes as defined for the
     129 *                      ip_client_process_packet() function.
     130 */
     131int
     132udp_process_packet(device_id_t device_id, packet_t packet, services_t error);
    126133
    127134/** Releases the packet and returns the result.
    128  *  @param[in] packet The packet queue to be released.
    129  *  @param[in] result The result to be returned.
    130  *  @return The result parameter.
     135 *
     136 *  @param[in] packet   The packet queue to be released.
     137 *  @param[in] result   The result to be returned.
     138 *  @return             The result parameter.
    131139 */
    132140int udp_release_and_return(packet_t packet, int result);
     
    137145
    138146/** Processes the socket client messages.
     147 *
    139148 *  Runs until the client module disconnects.
    140  *  @param[in] callid The message identifier.
    141  *  @param[in] call The message parameters.
    142  *  @returns EOK on success.
    143  *  @see socket.h
     149 *
     150 *  @param[in] callid   The message identifier.
     151 *  @param[in] call     The message parameters.
     152 *  @returns            EOK on success.
     153 *  @see                socket.h
    144154 */
    145155int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call);
    146156
    147157/** Sends data from the socket to the remote address.
     158 *
    148159 *  Binds the socket to a free port if not already connected/bound.
    149160 *  Handles the NET_SOCKET_SENDTO message.
    150161 *  Supports AF_INET and AF_INET6 address families.
     162 *
    151163 *  @param[in,out] local_sockets The application local sockets.
    152164 *  @param[in] socket_id Socket identifier.
    153  *  @param[in] addr The destination address.
    154  *  @param[in] addrlen The address length.
     165 *  @param[in] addr     The destination address.
     166 *  @param[in] addrlen  The address length.
    155167 *  @param[in] fragments The number of data fragments.
    156168 *  @param[out] data_fragment_size The data fragment size in bytes.
    157  *  @param[in] flags Various send flags.
    158  *  @returns EOK on success.
    159  *  @returns EAFNOTSUPPORT if the address family is not supported.
    160  *  @returns ENOTSOCK if the socket is not found.
    161  *  @returns EINVAL if the address is invalid.
    162  *  @returns ENOTCONN if the sending socket is not and cannot be bound.
    163  *  @returns ENOMEM if there is not enough memory left.
    164  *  @returns Other error codes as defined for the socket_read_packet_data() function.
    165  *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
    166  *  @returns Other error codes as defined for the ip_send_msg() function.
    167  */
    168 int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags);
     169 *  @param[in] flags    Various send flags.
     170 *  @returns            EOK on success.
     171 *  @returns            EAFNOTSUPPORT if the address family is not supported.
     172 *  @returns            ENOTSOCK if the socket is not found.
     173 *  @returns            EINVAL if the address is invalid.
     174 *  @returns            ENOTCONN if the sending socket is not and cannot be
     175 *                      bound.
     176 *  @returns            ENOMEM if there is not enough memory left.
     177 *  @returns            Other error codes as defined for the
     178 *                      socket_read_packet_data() function.
     179 *  @returns            Other error codes as defined for the
     180 *                      ip_client_prepare_packet() function.
     181 *  @returns            Other error codes as defined for the ip_send_msg()
     182 *                      function.
     183 */
     184int
     185udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
     186    const struct sockaddr * addr, socklen_t addrlen, int fragments,
     187    size_t * data_fragment_size, int flags);
    169188
    170189/** Receives data to the socket.
     190 *
    171191 *  Handles the NET_SOCKET_RECVFROM message.
    172192 *  Replies the source address as well.
     193 *
    173194 *  @param[in] local_sockets The application local sockets.
    174195 *  @param[in] socket_id Socket identifier.
    175  *  @param[in] flags Various receive flags.
    176  *  @param[out] addrlen The source address length.
    177  *  @returns The number of bytes received.
    178  *  @returns ENOTSOCK if the socket is not found.
    179  *  @returns NO_DATA if there are no received packets or data.
    180  *  @returns ENOMEM if there is not enough memory left.
    181  *  @returns EINVAL if the received address is not an IP address.
    182  *  @returns Other error codes as defined for the packet_translate() function.
    183  *  @returns Other error codes as defined for the data_reply() function.
    184  */
    185 int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen);
     196 *  @param[in] flags    Various receive flags.
     197 *  @param[out] addrlen The source address length.
     198 *  @returns            The number of bytes received.
     199 *  @returns            ENOTSOCK if the socket is not found.
     200 *  @returns            NO_DATA if there are no received packets or data.
     201 *  @returns            ENOMEM if there is not enough memory left.
     202 *  @returns            EINVAL if the received address is not an IP address.
     203 *  @returns            Other error codes as defined for the packet_translate()
     204 *                      function.
     205 *  @returns            Other error codes as defined for the data_reply()
     206 *                      function.
     207 */
     208int
     209udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
     210    size_t * addrlen);
    186211
    187212/*@}*/
     
    189214/** UDP global data.
    190215 */
    191 udp_globals_t   udp_globals;
    192 
    193 int udp_initialize(async_client_conn_t client_connection){
     216udp_globals_t udp_globals;
     217
     218int udp_initialize(async_client_conn_t client_connection)
     219{
    194220        ERROR_DECLARE;
    195221
    196         measured_string_t names[] = {{str_dup("UDP_CHECKSUM_COMPUTING"), 22}, {str_dup("UDP_AUTOBINDING"), 15}};
     222        measured_string_t names[] = {
     223                {
     224                        str_dup("UDP_CHECKSUM_COMPUTING"),
     225                        22
     226                },
     227                {
     228                        str_dup("UDP_AUTOBINDING"),
     229                        15
     230                }
     231        };
    197232        measured_string_ref configuration;
    198233        size_t count = sizeof(names) / sizeof(measured_string_t);
     
    201236        fibril_rwlock_initialize(&udp_globals.lock);
    202237        fibril_rwlock_write_lock(&udp_globals.lock);
    203         udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT);
    204         udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg);
    205         if(udp_globals.ip_phone < 0){
     238
     239        udp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP,
     240            ICMP_CONNECT_TIMEOUT);
     241        udp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_UDP,
     242            SERVICE_UDP, client_connection, udp_received_msg);
     243        if (udp_globals.ip_phone < 0)
    206244                return udp_globals.ip_phone;
    207         }
     245
    208246        // read default packet dimensions
    209         ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension));
     247        ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
     248            &udp_globals.packet_dimension));
    210249        ERROR_PROPAGATE(socket_ports_initialize(&udp_globals.sockets));
    211         if(ERROR_OCCURRED(packet_dimensions_initialize(&udp_globals.dimensions))){
     250        if (ERROR_OCCURRED(packet_dimensions_initialize(
     251            &udp_globals.dimensions))) {
    212252                socket_ports_destroy(&udp_globals.sockets);
    213253                return ERROR_CODE;
     
    216256        udp_globals.packet_dimension.content -= sizeof(udp_header_t);
    217257        udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
     258
    218259        // get configuration
    219260        udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
    220261        udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
    221262        configuration = &names[0];
    222         ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration, count, &data));
    223         if(configuration){
    224                 if(configuration[0].value){
    225                         udp_globals.checksum_computing = (configuration[0].value[0] == 'y');
    226                 }
    227                 if(configuration[1].value){
    228                         udp_globals.autobinding = (configuration[1].value[0] == 'y');
    229                 }
     263        ERROR_PROPAGATE(net_get_conf_req(udp_globals.net_phone, &configuration,
     264            count, &data));
     265        if (configuration) {
     266                if (configuration[0].value)
     267                        udp_globals.checksum_computing =
     268                            (configuration[0].value[0] == 'y');
     269               
     270                if (configuration[1].value)
     271                        udp_globals.autobinding =
     272                            (configuration[1].value[0] == 'y');
     273
    230274                net_free_settings(configuration, data);
    231275        }
     276
    232277        fibril_rwlock_write_unlock(&udp_globals.lock);
    233278        return EOK;
    234279}
    235280
    236 int udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){
     281int
     282udp_received_msg(device_id_t device_id, packet_t packet, services_t receiver,
     283    services_t error)
     284{
    237285        int result;
    238286
    239287        fibril_rwlock_write_lock(&udp_globals.lock);
    240288        result = udp_process_packet(device_id, packet, error);
    241         if(result != EOK){
     289        if (result != EOK)
    242290                fibril_rwlock_write_unlock(&udp_globals.lock);
    243         }
    244291
    245292        return result;
    246293}
    247294
    248 int udp_process_packet(device_id_t device_id, packet_t packet, services_t error){
     295int udp_process_packet(device_id_t device_id, packet_t packet, services_t error)
     296{
    249297        ERROR_DECLARE;
    250298
     
    262310        icmp_code_t code;
    263311        void *ip_header;
    264         struct sockaddr * src;
    265         struct sockaddr * dest;
     312        struct sockaddr *src;
     313        struct sockaddr *dest;
    266314        packet_dimension_ref packet_dimension;
    267315
    268         if(error){
    269                 switch(error){
    270                         case SERVICE_ICMP:
    271                                 // ignore error
    272                                 // length = icmp_client_header_length(packet);
    273                                 // process error
    274                                 result = icmp_client_process_packet(packet, &type, &code, NULL, NULL);
    275                                 if(result < 0){
    276                                         return udp_release_and_return(packet, result);
    277                                 }
    278                                 length = (size_t) result;
    279                                 if(ERROR_OCCURRED(packet_trim(packet, length, 0))){
    280                                         return udp_release_and_return(packet, ERROR_CODE);
    281                                 }
    282                                 break;
    283                         default:
    284                                 return udp_release_and_return(packet, ENOTSUP);
    285                 }
    286         }
     316        if (error) {
     317                switch (error) {
     318                case SERVICE_ICMP:
     319                        // ignore error
     320                        // length = icmp_client_header_length(packet);
     321                        // process error
     322                        result = icmp_client_process_packet(packet, &type,
     323                            &code, NULL, NULL);
     324                        if (result < 0)
     325                                return udp_release_and_return(packet, result);
     326                        length = (size_t) result;
     327                        if (ERROR_OCCURRED(packet_trim(packet, length, 0)))
     328                                return udp_release_and_return(packet,
     329                                    ERROR_CODE);
     330                        break;
     331                default:
     332                        return udp_release_and_return(packet, ENOTSUP);
     333                }
     334        }
     335
    287336        // TODO process received ipopts?
    288337        result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL);
    289         if(result < 0){
     338        if (result < 0)
    290339                return udp_release_and_return(packet, result);
    291         }
    292340        offset = (size_t) result;
    293341
    294342        length = packet_get_data_length(packet);
    295         if(length <= 0){
     343        if (length <= 0)
    296344                return udp_release_and_return(packet, EINVAL);
    297         }
    298         if(length < UDP_HEADER_SIZE + offset){
     345        if (length < UDP_HEADER_SIZE + offset)
    299346                return udp_release_and_return(packet, NO_DATA);
    300         }
    301347
    302348        // trim all but UDP header
    303         if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){
     349        if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))
    304350                return udp_release_and_return(packet, ERROR_CODE);
    305         }
    306351
    307352        // get udp header
    308353        header = (udp_header_ref) packet_get_data(packet);
    309         if(! header){
     354        if (!header)
    310355                return udp_release_and_return(packet, NO_DATA);
    311         }
     356
    312357        // find the destination socket
    313         socket = socket_port_find(&udp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
    314         if(! socket){
    315                 if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){
    316                         icmp_destination_unreachable_msg(udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet);
     358        socket = socket_port_find(&udp_globals.sockets,
     359        ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0);
     360        if (!socket) {
     361                if (tl_prepare_icmp_packet(udp_globals.net_phone,
     362                    udp_globals.icmp_phone, packet, error) == EOK) {
     363                        icmp_destination_unreachable_msg(udp_globals.icmp_phone,
     364                            ICMP_PORT_UNREACH, 0, packet);
    317365                }
    318366                return EADDRNOTAVAIL;
     
    323371        fragments = 0;
    324372        total_length = ntohs(header->total_length);
     373
    325374        // compute header checksum if set
    326         if(header->checksum && (! error)){
    327                 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest);
    328                 if(result <= 0){
     375        if (header->checksum && (!error)) {
     376                result = packet_get_addr(packet, (uint8_t **) &src,
     377                    (uint8_t **) &dest);
     378                if( result <= 0)
    329379                        return udp_release_and_return(packet, result);
    330                 }
    331                 if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP, src, result, dest, result, total_length, &ip_header, &length))){
     380
     381                if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_UDP,
     382                    src, result, dest, result, total_length, &ip_header,
     383                    &length))) {
    332384                        return udp_release_and_return(packet, ERROR_CODE);
    333                 }else{
     385                } else {
    334386                        checksum = compute_checksum(0, ip_header, length);
    335                         // the udp header checksum will be added with the first fragment later
     387                        // the udp header checksum will be added with the first
     388                        // fragment later
    336389                        free(ip_header);
    337390                }
    338         }else{
     391        } else {
    339392                header->checksum = 0;
    340393                checksum = 0;
    341394        }
    342395
    343         do{
     396        do {
    344397                ++ fragments;
    345398                length = packet_get_data_length(next_packet);
    346                 if(length <= 0){
     399                if (length <= 0)
    347400                        return udp_release_and_return(packet, NO_DATA);
    348                 }
    349                 if(total_length < length){
    350                         if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - total_length))){
    351                                 return udp_release_and_return(packet, ERROR_CODE);
     401
     402                if (total_length < length) {
     403                        if (ERROR_OCCURRED(packet_trim(next_packet, 0,
     404                            length - total_length))) {
     405                                return udp_release_and_return(packet,
     406                                    ERROR_CODE);
    352407                        }
     408
    353409                        // add partial checksum if set
    354                         if(header->checksum){
    355                                 checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
     410                        if (header->checksum) {
     411                                checksum = compute_checksum(checksum,
     412                                    packet_get_data(packet),
     413                                    packet_get_data_length(packet));
    356414                        }
     415
    357416                        // relese the rest of the packet fragments
    358417                        tmp_packet = pq_next(next_packet);
    359                         while(tmp_packet){
     418                        while (tmp_packet) {
    360419                                next_packet = pq_detach(tmp_packet);
    361                                 pq_release_remote(udp_globals.net_phone, packet_get_id(tmp_packet));
     420                                pq_release_remote(udp_globals.net_phone,
     421                                    packet_get_id(tmp_packet));
    362422                                tmp_packet = next_packet;
    363423                        }
     424
    364425                        // exit the loop
    365426                        break;
    366427                }
    367428                total_length -= length;
     429
    368430                // add partial checksum if set
    369                 if(header->checksum){
    370                         checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet));
    371                 }
    372         }while((next_packet = pq_next(next_packet)) && (total_length > 0));
     431                if (header->checksum) {
     432                        checksum = compute_checksum(checksum,
     433                            packet_get_data(packet),
     434                            packet_get_data_length(packet));
     435                }
     436
     437        } while ((next_packet = pq_next(next_packet)) && (total_length > 0));
    373438
    374439        // check checksum
    375         if(header->checksum){
    376                 if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){
    377                         if(tl_prepare_icmp_packet(udp_globals.net_phone, udp_globals.icmp_phone, packet, error) == EOK){
     440        if (header->checksum) {
     441                if (flip_checksum(compact_checksum(checksum)) !=
     442                    IP_CHECKSUM_ZERO) {
     443                        if (tl_prepare_icmp_packet(udp_globals.net_phone,
     444                            udp_globals.icmp_phone, packet, error) == EOK) {
    378445                                // checksum error ICMP
    379                                 icmp_parameter_problem_msg(udp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet);
     446                                icmp_parameter_problem_msg(
     447                                    udp_globals.icmp_phone, ICMP_PARAM_POINTER,
     448                                    ((size_t) ((void *) &header->checksum)) -
     449                                    ((size_t) ((void *) header)), packet);
    380450                        }
    381451                        return EINVAL;
     
    384454
    385455        // queue the received packet
    386         if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE))
    387             || ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension))){
     456        if (ERROR_OCCURRED(dyn_fifo_push(&socket->received,
     457            packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) ||
     458            ERROR_OCCURRED(tl_get_ip_packet_dimension(udp_globals.ip_phone,
     459            &udp_globals.dimensions, device_id, &packet_dimension))) {
    388460                return udp_release_and_return(packet, ERROR_CODE);
    389461        }
     
    391463        // notify the destination socket
    392464        fibril_rwlock_write_unlock(&udp_globals.lock);
    393         async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0, (ipcarg_t) fragments);
     465        async_msg_5(socket->phone, NET_SOCKET_RECEIVED,
     466            (ipcarg_t) socket->socket_id, packet_dimension->content, 0, 0,
     467            (ipcarg_t) fragments);
     468
    394469        return EOK;
    395470}
    396471
    397 int udp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
     472int
     473udp_message_standalone(ipc_callid_t callid, ipc_call_t * call,
     474    ipc_call_t * answer, int * answer_count)
     475{
    398476        ERROR_DECLARE;
    399477
     
    401479
    402480        *answer_count = 0;
    403         switch(IPC_GET_METHOD(*call)){
    404                 case NET_TL_RECEIVED:
    405                         if(! ERROR_OCCURRED(packet_translate_remote(udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){
    406                                 ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_UDP, IPC_GET_ERROR(call));
    407                         }
    408                         return ERROR_CODE;
    409                 case IPC_M_CONNECT_TO_ME:
    410                         return udp_process_client_messages(callid, * call);
    411         }
     481
     482        switch (IPC_GET_METHOD(*call)) {
     483        case NET_TL_RECEIVED:
     484                if (!ERROR_OCCURRED(packet_translate_remote(
     485                    udp_globals.net_phone, &packet, IPC_GET_PACKET(call)))) {
     486                        ERROR_CODE = udp_received_msg(IPC_GET_DEVICE(call),
     487                            packet, SERVICE_UDP, IPC_GET_ERROR(call));
     488                }
     489                return ERROR_CODE;
     490       
     491        case IPC_M_CONNECT_TO_ME:
     492                return udp_process_client_messages(callid, * call);
     493        }
     494
    412495        return ENOTSUP;
    413496}
    414497
    415 int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call){
     498int udp_process_client_messages(ipc_callid_t callid, ipc_call_t call)
     499{
    416500        int res;
    417501        bool keep_on_going = true;
    418502        socket_cores_t local_sockets;
    419503        int app_phone = IPC_GET_PHONE(&call);
    420         struct sockaddr * addr;
     504        struct sockaddr *addr;
    421505        int socket_id;
    422506        size_t addrlen;
     
    433517        answer_count = 0;
    434518
    435         // The client connection is only in one fibril and therefore no additional locks are needed.
     519        // The client connection is only in one fibril and therefore no
     520        // additional locks are needed.
    436521
    437522        socket_cores_initialize(&local_sockets);
    438523
    439         while(keep_on_going){
     524        while (keep_on_going) {
    440525
    441526                // answer the call
     
    449534
    450535                // process the call
    451                 switch(IPC_GET_METHOD(call)){
    452                         case IPC_M_PHONE_HUNGUP:
    453                                 keep_on_going = false;
    454                                 res = EHANGUP;
     536                switch (IPC_GET_METHOD(call)) {
     537                case IPC_M_PHONE_HUNGUP:
     538                        keep_on_going = false;
     539                        res = EHANGUP;
     540                        break;
     541
     542                case NET_SOCKET:
     543                        socket_id = SOCKET_GET_SOCKET_ID(call);
     544                        res = socket_create(&local_sockets, app_phone, NULL,
     545                            &socket_id);
     546                        SOCKET_SET_SOCKET_ID(answer, socket_id);
     547
     548                        if (res != EOK)
    455549                                break;
    456                         case NET_SOCKET:
    457                                 socket_id = SOCKET_GET_SOCKET_ID(call);
    458                                 res = socket_create(&local_sockets, app_phone, NULL, &socket_id);
    459                                 SOCKET_SET_SOCKET_ID(answer, socket_id);
    460 
    461                                 if(res == EOK){
    462                                         if (tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){
    463                                                 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, packet_dimension->content);
    464                                         }
    465 //                                      SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_UDP_FRAGMENT_SIZE);
    466                                         SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
    467                                         answer_count = 3;
    468                                 }
     550                       
     551                        if (tl_get_ip_packet_dimension(udp_globals.ip_phone,
     552                            &udp_globals.dimensions, DEVICE_INVALID_ID,
     553                            &packet_dimension) == EOK) {
     554                                SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
     555                                    packet_dimension->content);
     556                        }
     557
     558//                      SOCKET_SET_DATA_FRAGMENT_SIZE(answer,
     559//                          MAX_UDP_FRAGMENT_SIZE);
     560                        SOCKET_SET_HEADER_SIZE(answer, UDP_HEADER_SIZE);
     561                        answer_count = 3;
     562                        break;
     563
     564                case NET_SOCKET_BIND:
     565                        res = data_receive((void **) &addr, &addrlen);
     566                        if (res != EOK)
    469567                                break;
    470                         case NET_SOCKET_BIND:
    471                                 res = data_receive((void **) &addr, &addrlen);
    472                                 if(res == EOK){
    473                                         fibril_rwlock_write_lock(&udp_globals.lock);
    474                                         res = socket_bind(&local_sockets, &udp_globals.sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port);
    475                                         fibril_rwlock_write_unlock(&udp_globals.lock);
    476                                         free(addr);
    477                                 }
     568                        fibril_rwlock_write_lock(&udp_globals.lock);
     569                        res = socket_bind(&local_sockets, &udp_globals.sockets,
     570                            SOCKET_GET_SOCKET_ID(call), addr, addrlen,
     571                            UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
     572                            udp_globals.last_used_port);
     573                        fibril_rwlock_write_unlock(&udp_globals.lock);
     574                        free(addr);
     575                        break;
     576
     577                case NET_SOCKET_SENDTO:
     578                        res = data_receive((void **) &addr, &addrlen);
     579                        if (res != EOK)
    478580                                break;
    479                         case NET_SOCKET_SENDTO:
    480                                 res = data_receive((void **) &addr, &addrlen);
    481                                 if(res == EOK){
    482                                         fibril_rwlock_write_lock(&udp_globals.lock);
    483                                         res = udp_sendto_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call));
    484                                         SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
    485                                         if(res != EOK){
    486                                                 fibril_rwlock_write_unlock(&udp_globals.lock);
    487                                         }else{
    488                                                 answer_count = 2;
    489                                         }
    490                                         free(addr);
    491                                 }
     581
     582                        fibril_rwlock_write_lock(&udp_globals.lock);
     583                        res = udp_sendto_message(&local_sockets,
     584                            SOCKET_GET_SOCKET_ID(call), addr, addrlen,
     585                            SOCKET_GET_DATA_FRAGMENTS(call), &size,
     586                            SOCKET_GET_FLAGS(call));
     587                        SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size);
     588
     589                        if (res != EOK)
     590                                fibril_rwlock_write_unlock(&udp_globals.lock);
     591                        else
     592                                answer_count = 2;
     593                       
     594                        free(addr);
     595                        break;
     596
     597                case NET_SOCKET_RECVFROM:
     598                        fibril_rwlock_write_lock(&udp_globals.lock);
     599                        res = udp_recvfrom_message(&local_sockets,
     600                             SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call),
     601                             &addrlen);
     602                        fibril_rwlock_write_unlock(&udp_globals.lock);
     603
     604                        if (res <= 0)
    492605                                break;
    493                         case NET_SOCKET_RECVFROM:
    494                                 fibril_rwlock_write_lock(&udp_globals.lock);
    495                                 res = udp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen);
    496                                 fibril_rwlock_write_unlock(&udp_globals.lock);
    497                                 if(res > 0){
    498                                         SOCKET_SET_READ_DATA_LENGTH(answer, res);
    499                                         SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
    500                                         answer_count = 3;
    501                                         res = EOK;
    502                                 }
    503                                 break;
    504                         case NET_SOCKET_CLOSE:
    505                                 fibril_rwlock_write_lock(&udp_globals.lock);
    506                                 res = socket_destroy(udp_globals.net_phone, SOCKET_GET_SOCKET_ID(call), &local_sockets, &udp_globals.sockets, NULL);
    507                                 fibril_rwlock_write_unlock(&udp_globals.lock);
    508                                 break;
    509                         case NET_SOCKET_GETSOCKOPT:
    510                         case NET_SOCKET_SETSOCKOPT:
    511                         default:
    512                                 res = ENOTSUP;
    513                                 break;
     606
     607                        SOCKET_SET_READ_DATA_LENGTH(answer, res);
     608                        SOCKET_SET_ADDRESS_LENGTH(answer, addrlen);
     609                        answer_count = 3;
     610                        res = EOK;
     611                        break;
     612                       
     613                case NET_SOCKET_CLOSE:
     614                        fibril_rwlock_write_lock(&udp_globals.lock);
     615                        res = socket_destroy(udp_globals.net_phone,
     616                            SOCKET_GET_SOCKET_ID(call), &local_sockets,
     617                            &udp_globals.sockets, NULL);
     618                        fibril_rwlock_write_unlock(&udp_globals.lock);
     619                        break;
     620
     621                case NET_SOCKET_GETSOCKOPT:
     622                case NET_SOCKET_SETSOCKOPT:
     623                default:
     624                        res = ENOTSUP;
     625                        break;
    514626                }
    515627        }
     
    519631
    520632        // release all local sockets
    521         socket_cores_release(udp_globals.net_phone, &local_sockets, &udp_globals.sockets, NULL);
     633        socket_cores_release(udp_globals.net_phone, &local_sockets,
     634            &udp_globals.sockets, NULL);
    522635
    523636        return res;
    524637}
    525638
    526 int udp_sendto_message(socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, size_t * data_fragment_size, int flags){
     639int
     640udp_sendto_message(socket_cores_ref local_sockets, int socket_id,
     641    const struct sockaddr *addr, socklen_t addrlen, int fragments,
     642    size_t *data_fragment_size, int flags)
     643{
    527644        ERROR_DECLARE;
    528645
     
    544661
    545662        socket = socket_cores_find(local_sockets, socket_id);
    546         if(! socket){
     663        if (!socket)
    547664                return ENOTSOCK;
    548         }
    549 
    550         if((socket->port <= 0) && udp_globals.autobinding){
     665
     666        if ((socket->port <= 0) && udp_globals.autobinding) {
    551667                // bind the socket to a random free port if not bound
    552 //              do{
     668//              do {
    553669                        // try to find a free port
    554670//                      fibril_rwlock_read_unlock(&udp_globals.lock);
    555671//                      fibril_rwlock_write_lock(&udp_globals.lock);
    556672                        // might be changed in the meantime
    557 //                      if(socket->port <= 0){
    558                                 if(ERROR_OCCURRED(socket_bind_free_port(&udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port))){
    559 //                                      fibril_rwlock_write_unlock(&udp_globals.lock);
    560 //                                      fibril_rwlock_read_lock(&udp_globals.lock);
     673//                      if (socket->port <= 0) {
     674                                if (ERROR_OCCURRED(socket_bind_free_port(
     675                                    &udp_globals.sockets, socket,
     676                                    UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,
     677                                    udp_globals.last_used_port))) {
     678//                                      fibril_rwlock_write_unlock(
     679//                                          &udp_globals.lock);
     680//                                      fibril_rwlock_read_lock(
     681//                                          &udp_globals.lock);
    561682                                        return ERROR_CODE;
    562683                                }
    563                                 // set the next port as the search starting port number
     684                                // set the next port as the search starting port
     685                                // number
    564686                                udp_globals.last_used_port = socket->port;
    565687//                      }
     
    567689//                      fibril_rwlock_read_lock(&udp_globals.lock);
    568690                        // might be changed in the meantime
    569 //              }while(socket->port <= 0);
    570         }
    571 
    572         if(udp_globals.checksum_computing){
    573                 if(ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, &device_id, &ip_header, &headerlen))){
     691//              } while (socket->port <= 0);
     692        }
     693
     694        if (udp_globals.checksum_computing) {
     695                if (ERROR_OCCURRED(ip_get_route_req(udp_globals.ip_phone,
     696                    IPPROTO_UDP, addr, addrlen, &device_id, &ip_header,
     697                    &headerlen))) {
    574698                        return udp_release_and_return(packet, ERROR_CODE);
    575699                }
    576700                // get the device packet dimension
    577 //              ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone, &udp_globals.dimensions, device_id, &packet_dimension));
    578         }
    579 //      }else{
     701//              ERROR_PROPAGATE(tl_get_ip_packet_dimension(udp_globals.ip_phone,
     702//                  &udp_globals.dimensions, device_id, &packet_dimension));
     703        }
     704//      } else {
    580705                // do not ask all the time
    581                 ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1, &udp_globals.packet_dimension));
     706                ERROR_PROPAGATE(ip_packet_size_req(udp_globals.ip_phone, -1,
     707                    &udp_globals.packet_dimension));
    582708                packet_dimension = &udp_globals.packet_dimension;
    583709//      }
    584710
    585711        // read the first packet fragment
    586         result = tl_socket_read_packet_data(udp_globals.net_phone, &packet, UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
    587         if(result < 0){
     712        result = tl_socket_read_packet_data(udp_globals.net_phone, &packet,
     713            UDP_HEADER_SIZE, packet_dimension, addr, addrlen);
     714        if (result < 0)
    588715                return result;
    589         }
     716
    590717        total_length = (size_t) result;
    591         if(udp_globals.checksum_computing){
    592                 checksum = compute_checksum(0, packet_get_data(packet), packet_get_data_length(packet));
    593         }else{
     718        if (udp_globals.checksum_computing)
     719                checksum = compute_checksum(0, packet_get_data(packet),
     720                    packet_get_data_length(packet));
     721        else
    594722                checksum = 0;
    595         }
     723
    596724        // prefix the udp header
    597725        header = PACKET_PREFIX(packet, udp_header_t);
    598         if(! header){
     726        if(! header)
    599727                return udp_release_and_return(packet, ENOMEM);
    600         }
     728
    601729        bzero(header, sizeof(*header));
    602730        // read the rest of the packet fragments
    603         for(index = 1; index < fragments; ++ index){
    604                 result = tl_socket_read_packet_data(udp_globals.net_phone, &next_packet, 0, packet_dimension, addr, addrlen);
    605                 if(result < 0){
     731        for (index = 1; index < fragments; ++ index) {
     732                result = tl_socket_read_packet_data(udp_globals.net_phone,
     733                    &next_packet, 0, packet_dimension, addr, addrlen);
     734                if (result < 0)
    606735                        return udp_release_and_return(packet, result);
    607                 }
    608                 if(ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0))){
     736
     737                if (ERROR_OCCURRED(pq_add(&packet, next_packet, index, 0)))
    609738                        return udp_release_and_return(packet, ERROR_CODE);
    610                 }
     739
    611740                total_length += (size_t) result;
    612                 if(udp_globals.checksum_computing){
    613                         checksum = compute_checksum(checksum, packet_get_data(next_packet), packet_get_data_length(next_packet));
    614                 }
    615         }
     741                if (udp_globals.checksum_computing) {
     742                        checksum = compute_checksum(checksum,
     743                            packet_get_data(next_packet),
     744                            packet_get_data_length(next_packet));
     745                }
     746        }
     747
    616748        // set the udp header
    617749        header->source_port = htons((socket->port > 0) ? socket->port : 0);
     
    619751        header->total_length = htons(total_length + sizeof(*header));
    620752        header->checksum = 0;
    621         if(udp_globals.checksum_computing){
     753        if (udp_globals.checksum_computing) {
    622754                // update the pseudo header
    623                 if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(ip_header, headerlen, total_length + UDP_HEADER_SIZE))){
     755                if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(
     756                    ip_header, headerlen, total_length + UDP_HEADER_SIZE))) {
    624757                        free(ip_header);
    625758                        return udp_release_and_return(packet, ERROR_CODE);
    626759                }
     760
    627761                // finish the checksum computation
    628762                checksum = compute_checksum(checksum, ip_header, headerlen);
    629                 checksum = compute_checksum(checksum, (uint8_t *) header, sizeof(*header));
    630                 header->checksum = htons(flip_checksum(compact_checksum(checksum)));
     763                checksum = compute_checksum(checksum, (uint8_t *) header,
     764                    sizeof(*header));
     765                header->checksum =
     766                    htons(flip_checksum(compact_checksum(checksum)));
    631767                free(ip_header);
    632         }else{
     768        } else {
    633769                device_id = DEVICE_INVALID_ID;
    634770        }
     771
    635772        // prepare the first packet fragment
    636         if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0, 0, 0))){
     773        if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_UDP, 0, 0,
     774            0, 0))) {
    637775                return udp_release_and_return(packet, ERROR_CODE);
    638776        }
     777
    639778        // send the packet
    640779        fibril_rwlock_write_unlock(&udp_globals.lock);
    641780        ip_send_msg(udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0);
     781
    642782        return EOK;
    643783}
    644784
    645 int udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){
     785int
     786udp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags,
     787    size_t *addrlen)
     788{
    646789        ERROR_DECLARE;
    647790
     
    650793        packet_t packet;
    651794        udp_header_ref header;
    652         struct sockaddr * addr;
     795        struct sockaddr *addr;
    653796        size_t length;
    654         uint8_t * data;
     797        uint8_t *data;
    655798        int result;
    656799
    657800        // find the socket
    658801        socket = socket_cores_find(local_sockets, socket_id);
    659         if(! socket){
     802        if (!socket)
    660803                return ENOTSOCK;
    661         }
     804
    662805        // get the next received packet
    663806        packet_id = dyn_fifo_value(&socket->received);
    664         if(packet_id < 0){
     807        if (packet_id < 0)
    665808                return NO_DATA;
    666         }
    667         ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet, packet_id));
     809
     810        ERROR_PROPAGATE(packet_translate_remote(udp_globals.net_phone, &packet,
     811            packet_id));
     812
    668813        // get udp header
    669814        data = packet_get_data(packet);
    670         if(! data){
     815        if (!data) {
    671816                pq_release_remote(udp_globals.net_phone, packet_id);
    672817                return NO_DATA;
     
    676821        // set the source address port
    677822        result = packet_get_addr(packet, (uint8_t **) &addr, NULL);
    678         if(ERROR_OCCURRED(tl_set_address_port(addr, result, ntohs(header->source_port)))){
     823        if (ERROR_OCCURRED(tl_set_address_port(addr, result,
     824            ntohs(header->source_port)))) {
    679825                pq_release_remote(udp_globals.net_phone, packet_id);
    680826                return ERROR_CODE;
    681827        }
    682828        *addrlen = (size_t) result;
     829
    683830        // send the source address
    684831        ERROR_PROPAGATE(data_reply(addr, * addrlen));
     
    693840        dyn_fifo_pop(&socket->received);
    694841        pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
     842
    695843        // return the total length
    696844        return (int) length;
    697845}
    698846
    699 int udp_release_and_return(packet_t packet, int result){
     847int udp_release_and_return(packet_t packet, int result)
     848{
    700849        pq_release_remote(udp_globals.net_phone, packet_get_id(packet));
    701850        return result;
     
    704853/** Default thread for new connections.
    705854 *
    706  *  @param[in] iid The initial message identifier.
    707  *  @param[in] icall The initial message call structure.
     855 *  @param[in] iid      The initial message identifier.
     856 *  @param[in] icall    The initial message call structure.
    708857 *
    709858 */
     
    716865        ipc_answer_0(iid, EOK);
    717866       
    718         while(true) {
     867        while (true) {
    719868                ipc_call_t answer;
    720869                int answer_count;
     
    731880                    &answer_count);
    732881               
    733                 /* End if said to either by the message or the processing result */
    734                 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
     882                /*
     883                 * End if said to either by the message or the processing result
     884                 */
     885                if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) ||
     886                    (res == EHANGUP))
    735887                        return;
    736888               
     
    742894/** Starts the module.
    743895 *
    744  *  @param argc The count of the command line arguments. Ignored parameter.
    745  *  @param argv The command line parameters. Ignored parameter.
    746  *
    747  *  @returns EOK on success.
    748  *  @returns Other error codes as defined for each specific module start function.
    749  *
     896 *  @param argc         The count of the command line arguments. Ignored
     897 *                      parameter.
     898 *  @param argv         The command line parameters. Ignored parameter.
     899 *
     900 *  @returns            EOK on success.
     901 *  @returns            Other error codes as defined for each specific module
     902 *                      start function.
    750903 */
    751904int main(int argc, char *argv[])
Note: See TracChangeset for help on using the changeset viewer.