Subversion Repositories HelenOS

Rev

Rev 4738 | Rev 4750 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4499 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup udp
30
 *  @{
31
 */
32
 
33
/** @file
4701 mejdrech 34
 *  UDP module implementation.
35
 *  @see udp.h
4499 mejdrech 36
 */
37
 
38
#include <async.h>
4700 mejdrech 39
#include <fibril_sync.h>
4579 mejdrech 40
#include <malloc.h>
4743 mejdrech 41
#include <stdio.h>
4499 mejdrech 42
 
43
#include <ipc/ipc.h>
44
#include <ipc/services.h>
45
 
46
#include "../../err.h"
47
#include "../../messages.h"
48
#include "../../modules.h"
4589 mejdrech 49
 
50
#include "../../structures/dynamic_fifo.h"
4499 mejdrech 51
#include "../../structures/packet/packet_client.h"
52
 
4743 mejdrech 53
#include "../../include/checksum.h"
4579 mejdrech 54
#include "../../include/in.h"
4720 mejdrech 55
#include "../../include/in6.h"
4579 mejdrech 56
#include "../../include/inet.h"
4499 mejdrech 57
#include "../../include/ip_client.h"
58
#include "../../include/ip_interface.h"
59
#include "../../include/ip_protocols.h"
4707 mejdrech 60
#include "../../include/icmp_client.h"
61
#include "../../include/icmp_interface.h"
4722 mejdrech 62
#include "../../include/net_interface.h"
4713 mejdrech 63
#include "../../include/socket_codes.h"
4579 mejdrech 64
#include "../../include/socket_errno.h"
4499 mejdrech 65
 
4579 mejdrech 66
#include "../../socket/socket_core.h"
67
#include "../../socket/socket_messages.h"
68
 
4726 mejdrech 69
#include "../tl_common.h"
4499 mejdrech 70
#include "../tl_messages.h"
71
 
72
#include "udp.h"
4579 mejdrech 73
#include "udp_header.h"
4499 mejdrech 74
#include "udp_module.h"
75
 
4722 mejdrech 76
/** Default UDP checksum computing.
77
 */
78
#define NET_DEFAULT_UDP_CHECKSUM_COMPUTING  true
79
 
4730 mejdrech 80
/** Default UDP autobind when sending via unbound sockets.
81
 */
82
#define NET_DEFAULT_UDP_AUTOBINDING true
83
 
4701 mejdrech 84
/** Maximum UDP fragment size.
85
 */
4589 mejdrech 86
#define MAX_UDP_FRAGMENT_SIZE   65535
87
 
4701 mejdrech 88
/** Free ports pool start.
89
 */
4700 mejdrech 90
#define UDP_FREE_PORTS_START    1025
4701 mejdrech 91
 
92
/** Free ports pool end.
93
 */
4700 mejdrech 94
#define UDP_FREE_PORTS_END      65535
95
 
4707 mejdrech 96
/** Processes the received UDP packet queue.
4701 mejdrech 97
 *  Is used as an entry point from the underlying IP module.
4730 mejdrech 98
 *  Locks the global lock and calls udp_process_packet() function.
4701 mejdrech 99
 *  @param device_id The device identifier. Ignored parameter.
4707 mejdrech 100
 *  @param packet The received packet queue. Input/output parameter.
4701 mejdrech 101
 *  @param receiver The target service. Ignored parameter.
4707 mejdrech 102
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
4701 mejdrech 103
 *  @returns EOK on success.
4730 mejdrech 104
 *  @returns Other error codes as defined for the udp_process_packet() function.
105
 */
106
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
107
 
108
/** Processes the received UDP packet queue.
109
 *  Notifies the destination socket application.
110
 *  Releases the packet on error or sends an ICMP error notification..
111
 *  @param packet The received packet queue. Input/output parameter.
112
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
113
 *  @returns EOK on success.
4701 mejdrech 114
 *  @returns EINVAL if the packet is not valid.
115
 *  @returns EINVAL if the stored packet address is not the an_addr_t.
116
 *  @returns EINVAL if the packet does not contain any data.
117
 *  @returns NO_DATA if the packet content is shorter than the user datagram header.
118
 *  @returns ENOMEM if there is not enough memory left.
119
 *  @returns EADDRNOTAVAIL if the destination socket does not exist.
120
 *  @returns Other error codes as defined for the ip_client_process_packet() function.
121
 */
4730 mejdrech 122
int udp_process_packet( packet_t packet, services_t error );
4701 mejdrech 123
 
4707 mejdrech 124
/** Releases the packet and returns the result.
125
 *  @param packet The packet queue to be released. Input parameter.
126
 *  @param result The result to be returned. Input parameter.
127
 *  @return The result parameter.
128
 */
4713 mejdrech 129
int udp_release_and_return( packet_t packet, int result );
4707 mejdrech 130
 
4701 mejdrech 131
/** @name Socket messages processing functions
132
 */
133
/*@{*/
134
 
135
/** Processes the socket client messages.
136
 *  Runs until the client module disconnects.
137
 *  @param callid The message identifier. Input parameter.
138
 *  @param call The message parameters. Input parameter.
139
 *  @returns EOK on success.
140
 *  @see socket.h
141
 */
4713 mejdrech 142
int udp_process_client_messages( ipc_callid_t callid, ipc_call_t call );
4701 mejdrech 143
 
144
/** Sends data from the socket to the remote address.
145
 *  Binds the socket to a free port if not already connected/bound.
146
 *  Handles the NET_SOCKET_SENDTO message.
4720 mejdrech 147
 *  Supports AF_INET and AF_INET6 address families.
4701 mejdrech 148
 *  @param local_sockets The application local sockets. Input/output parameter.
149
 *  @param socket_id Socket identifier. Input parameter.
150
 *  @param addr The destination address. Input parameter.
151
 *  @param addrlen The address length. Input parameter.
152
 *  @param fragments The number of data fragments. Input parameter.
4726 mejdrech 153
 *  @param data_fragment_size The data fragment size in bytes. Input parameter.
4701 mejdrech 154
 *  @param flags Various send flags. Input parameter.
155
 *  @returns EOK on success.
156
 *  @returns EAFNOTSUPPORT if the address family is not supported.
157
 *  @returns ENOTSOCK if the socket is not found.
158
 *  @returns EINVAL if the address is invalid.
159
 *  @returns ENOTCONN if the sending socket is not and cannot be bound.
160
 *  @returns ENOMEM if there is not enough memory left.
161
 *  @returns Other error codes as defined for the socket_read_packet_data() function.
162
 *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
163
 *  @returns Other error codes as defined for the ip_send_msg() function.
164
 */
4726 mejdrech 165
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 );
4701 mejdrech 166
 
167
/** Receives data to the socket.
168
 *  Handles the NET_SOCKET_RECVFROM message.
4720 mejdrech 169
 *  Replies the source address as well.
4701 mejdrech 170
 *  @param local_sockets The application local sockets. Input parameter.
171
 *  @param socket_id Socket identifier. Input parameter.
172
 *  @param flags Various receive flags. Input parameter.
4720 mejdrech 173
 *  @param addrlen The source address length. Output parameter.
4701 mejdrech 174
 *  @returns The number of bytes received.
175
 *  @returns ENOTSOCK if the socket is not found.
176
 *  @returns NO_DATA if there are no received packets or data.
177
 *  @returns ENOMEM if there is not enough memory left.
178
 *  @returns EINVAL if the received address is not an IP address.
179
 *  @returns Other error codes as defined for the packet_translate() function.
4722 mejdrech 180
 *  @returns Other error codes as defined for the data_reply() function.
4701 mejdrech 181
 */
4720 mejdrech 182
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
4701 mejdrech 183
 
184
/*@}*/
185
 
186
/** UDP global data.
187
 */
4499 mejdrech 188
udp_globals_t   udp_globals;
189
 
190
int udp_initialize( async_client_conn_t client_connection ){
4579 mejdrech 191
    ERROR_DECLARE;
4499 mejdrech 192
 
4730 mejdrech 193
    measured_string_t   names[] = {{ "UDP_CHECKSUM_COMPUTING", 22 }, { "UDP_AUTOBINDING", 15 }};
4722 mejdrech 194
    measured_string_ref configuration;
195
    size_t              count = sizeof( names ) / sizeof( measured_string_t );
196
    char *              data;
197
 
4700 mejdrech 198
    fibril_rwlock_initialize( & udp_globals.lock );
199
    fibril_rwlock_write_lock( & udp_globals.lock );
4707 mejdrech 200
    udp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP );
201
    if( udp_globals.icmp_phone < 0 ){
202
        return udp_globals.icmp_phone;
203
    }
4499 mejdrech 204
    udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
4579 mejdrech 205
    if( udp_globals.ip_phone < 0 ){
206
        return udp_globals.ip_phone;
207
    }
4726 mejdrech 208
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.packet_dimension.addr_len, & udp_globals.packet_dimension.prefix, & udp_globals.packet_dimension.content, & udp_globals.packet_dimension.suffix ));
4579 mejdrech 209
    ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
4726 mejdrech 210
    udp_globals.packet_dimension.prefix += sizeof( udp_header_t );
211
    udp_globals.packet_dimension.content -= sizeof( udp_header_t );
4700 mejdrech 212
    udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
4722 mejdrech 213
    // get configuration
214
    udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
4730 mejdrech 215
    udp_globals.autobinding = NET_DEFAULT_UDP_AUTOBINDING;
4722 mejdrech 216
    configuration = & names[ 0 ];
217
    ERROR_PROPAGATE( net_get_conf_req( udp_globals.net_phone, & configuration, count, & data ));
218
    if( configuration ){
219
        if( configuration[ 0 ].value ){
220
            udp_globals.checksum_computing = ( configuration[ 0 ].value[ 0 ] == 'y' );
221
        }
4730 mejdrech 222
        if( configuration[ 1 ].value ){
223
            udp_globals.autobinding = ( configuration[ 1 ].value[ 0 ] == 'y' );
224
        }
4722 mejdrech 225
        net_free_settings( configuration, data );
226
    }
4700 mejdrech 227
    fibril_rwlock_write_unlock( & udp_globals.lock );
4579 mejdrech 228
    return EOK;
4499 mejdrech 229
}
230
 
4707 mejdrech 231
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
4730 mejdrech 232
    int result;
233
 
4738 mejdrech 234
    fibril_rwlock_write_lock( & udp_globals.lock );
4730 mejdrech 235
    result = udp_process_packet( packet, error );
4743 mejdrech 236
    if( result != EOK ){
237
        fibril_rwlock_write_unlock( & udp_globals.lock );
238
    }
4730 mejdrech 239
 
240
    return result;
241
}
242
 
243
int udp_process_packet( packet_t packet, services_t error ){
4589 mejdrech 244
    ERROR_DECLARE;
245
 
4708 mejdrech 246
    size_t          length;
247
    size_t          offset;
248
    int             result;
4589 mejdrech 249
    udp_header_ref  header;
4738 mejdrech 250
    socket_core_ref socket;
4589 mejdrech 251
    packet_t        next_packet;
4708 mejdrech 252
    size_t          total_length;
4722 mejdrech 253
    uint32_t        checksum;
4589 mejdrech 254
    int             fragments;
255
    packet_t        tmp_packet;
4707 mejdrech 256
    icmp_type_t     type;
257
    icmp_code_t     code;
4722 mejdrech 258
    ip_pseudo_header_ref    ip_header;
259
    struct sockaddr *       src;
260
    struct sockaddr *       dest;
4589 mejdrech 261
 
4707 mejdrech 262
    if( error ){
263
        switch( error ){
264
            case SERVICE_ICMP:
4731 mejdrech 265
                // ignore error
266
                // length = icmp_client_header_length( packet );
4707 mejdrech 267
                // process error
4708 mejdrech 268
                result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
269
                if( result < 0 ){
4713 mejdrech 270
                    return udp_release_and_return( packet, result );
4707 mejdrech 271
                }
4708 mejdrech 272
                length = ( size_t ) result;
4707 mejdrech 273
                if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){
4713 mejdrech 274
                    return udp_release_and_return( packet, ERROR_CODE );
4707 mejdrech 275
                }
276
                break;
277
            default:
4713 mejdrech 278
                return udp_release_and_return( packet, ENOTSUP );
4707 mejdrech 279
        }
280
    }
4701 mejdrech 281
    // TODO process received ipopts?
4708 mejdrech 282
    result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL );
283
    if( result < 0 ){
4713 mejdrech 284
        return udp_release_and_return( packet, result );
4707 mejdrech 285
    }
4708 mejdrech 286
    offset = ( size_t ) result;
4589 mejdrech 287
 
288
    length = packet_get_data_length( packet );
4707 mejdrech 289
    if( length <= 0 ){
4713 mejdrech 290
        return udp_release_and_return( packet, EINVAL );
4707 mejdrech 291
    }
292
    if( length < sizeof( udp_header_t ) + offset ){
4713 mejdrech 293
        return udp_release_and_return( packet, NO_DATA );
4707 mejdrech 294
    }
4731 mejdrech 295
 
296
    // trim all but UDP header
297
    if( ERROR_OCCURRED( packet_trim( packet, offset, 0 ))){
298
        return udp_release_and_return( packet, ERROR_CODE );
299
    }
300
 
301
    // get udp header
302
    header = ( udp_header_ref ) packet_get_data( packet );
303
    if( ! header ){
4713 mejdrech 304
        return udp_release_and_return( packet, NO_DATA );
4707 mejdrech 305
    }
4589 mejdrech 306
    // find the destination socket
4738 mejdrech 307
    socket = socket_port_find( & udp_globals.sockets, ntohs( header->destination_port ), SOCKET_MAP_KEY_LISTENING, 0 );
4707 mejdrech 308
    if( ! socket ){
4731 mejdrech 309
        if( tl_prepare_icmp_packet( udp_globals.net_phone, udp_globals.icmp_phone, packet, error ) == EOK ){
310
            icmp_destination_unreachable_msg( udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet );
311
        }
4707 mejdrech 312
        return EADDRNOTAVAIL;
313
    }
4730 mejdrech 314
 
4589 mejdrech 315
    // count the received packet fragments
316
    next_packet = packet;
317
    fragments = 0;
4728 mejdrech 318
    total_length = ntohs( header->total_length );
4722 mejdrech 319
    // compute header checksum if set
4728 mejdrech 320
    if( header->checksum && ( ! error )){
4722 mejdrech 321
        result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest );
322
        if( result <= 0 ){
323
            return udp_release_and_return( packet, result );
324
        }
325
        if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_UDP, src, result, dest, result, total_length, & ip_header, & length ))){
326
            return udp_release_and_return( packet, ERROR_CODE );
327
        }else{
328
            checksum = compute_checksum( 0, ip_header, length );
329
            // the udp header checksum will be added with the first fragment later
330
            free( ip_header );
331
        }
332
    }else{
4728 mejdrech 333
        header->checksum = 0;
4722 mejdrech 334
        checksum = 0;
335
    }
4730 mejdrech 336
 
4589 mejdrech 337
    do{
338
        ++ fragments;
4722 mejdrech 339
        length = packet_get_data_length( next_packet );
4708 mejdrech 340
        if( length <= 0 ){
4713 mejdrech 341
            return udp_release_and_return( packet, NO_DATA );
4707 mejdrech 342
        }
4589 mejdrech 343
        if( total_length < length ){
4707 mejdrech 344
            if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){
4713 mejdrech 345
                return udp_release_and_return( packet, ERROR_CODE );
4707 mejdrech 346
            }
4722 mejdrech 347
            // add partial checksum if set
4728 mejdrech 348
            if( header->checksum ){
4722 mejdrech 349
                checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet ));
350
            }
4589 mejdrech 351
            // relese the rest of the packet fragments
352
            tmp_packet = pq_next( next_packet );
353
            while( tmp_packet ){
354
                next_packet = pq_detach( tmp_packet );
355
                pq_release( udp_globals.net_phone, packet_get_id( tmp_packet ));
356
                tmp_packet = next_packet;
357
            }
4722 mejdrech 358
            // exit the loop
4589 mejdrech 359
            break;
360
        }
361
        total_length -= length;
4722 mejdrech 362
        // add partial checksum if set
4728 mejdrech 363
        if( header->checksum ){
4722 mejdrech 364
            checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet ));
4589 mejdrech 365
        }
366
    }while(( next_packet = pq_next( next_packet )) && ( total_length > 0 ));
4730 mejdrech 367
 
4722 mejdrech 368
    // check checksum
4728 mejdrech 369
    if( header->checksum ){
4722 mejdrech 370
        if( flip_checksum( compact_checksum( checksum ))){
4731 mejdrech 371
            if( tl_prepare_icmp_packet( udp_globals.net_phone, udp_globals.icmp_phone, packet, error ) == EOK ){
372
                // checksum error ICMP
373
                icmp_parameter_problem_msg( udp_globals.icmp_phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->checksum )) - (( size_t ) (( void * ) header )), packet );
374
            }
375
            return EINVAL;
4722 mejdrech 376
        }
377
    }
4730 mejdrech 378
 
4589 mejdrech 379
    // queue the received packet
4738 mejdrech 380
    if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){
4713 mejdrech 381
        return udp_release_and_return( packet, ERROR_CODE );
4707 mejdrech 382
    }
4499 mejdrech 383
 
4589 mejdrech 384
    // notify the destination socket
4743 mejdrech 385
    fibril_rwlock_write_unlock( & udp_globals.lock );
4738 mejdrech 386
    async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments );
4499 mejdrech 387
    return EOK;
388
}
389
 
390
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
4505 mejdrech 391
    ERROR_DECLARE;
392
 
393
    packet_t    packet;
394
 
4499 mejdrech 395
    * answer_count = 0;
396
    switch( IPC_GET_METHOD( * call )){
397
        case NET_TL_RECEIVED:
4700 mejdrech 398
            if( ! ERROR_OCCURRED( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
4707 mejdrech 399
                ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_UDP, IPC_GET_ERROR( call ));
4700 mejdrech 400
            }
401
            return ERROR_CODE;
4579 mejdrech 402
        case IPC_M_CONNECT_TO_ME:
4713 mejdrech 403
            return udp_process_client_messages( callid, * call );
4499 mejdrech 404
    }
405
    return ENOTSUP;
406
}
407
 
4713 mejdrech 408
int udp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){
4579 mejdrech 409
    int                     res;
410
    bool                    keep_on_going = true;
411
    socket_cores_t          local_sockets;
4701 mejdrech 412
    int                     app_phone = IPC_GET_PHONE( & call );
4720 mejdrech 413
    struct sockaddr *       addr;
4579 mejdrech 414
    size_t                  addrlen;
4738 mejdrech 415
    fibril_rwlock_t         lock;
4701 mejdrech 416
    ipc_call_t              answer;
417
    int                     answer_count;
4579 mejdrech 418
 
419
    /*
420
     * Accept the connection
4743 mejdrech 421
     *  - Answer the first IPC_M_CONNECT_TO_ME call.
4579 mejdrech 422
     */
423
    ipc_answer_0( callid, EOK );
424
 
4726 mejdrech 425
    // The client connection is only in one fibril and therefore no additional locks are needed.
426
 
4579 mejdrech 427
    socket_cores_initialize( & local_sockets );
4738 mejdrech 428
    fibril_rwlock_initialize( & lock );
4579 mejdrech 429
 
430
    while( keep_on_going ){
431
        // refresh data
4713 mejdrech 432
        refresh_answer( & answer, & answer_count );
4579 mejdrech 433
 
4701 mejdrech 434
        callid = async_get_call( & call );
4589 mejdrech 435
//      printf( "message %d\n", IPC_GET_METHOD( * call ));
4579 mejdrech 436
 
4701 mejdrech 437
        switch( IPC_GET_METHOD( call )){
4579 mejdrech 438
            case IPC_M_PHONE_HUNGUP:
439
                keep_on_going = false;
440
                res = EOK;
441
                break;
442
            case NET_SOCKET:
4738 mejdrech 443
                fibril_rwlock_write_lock( & lock );
4726 mejdrech 444
                res = socket_create( & local_sockets, app_phone, NULL, SOCKET_SET_SOCKET_ID( answer ));
4738 mejdrech 445
                fibril_rwlock_write_unlock( & lock );
4731 mejdrech 446
                // TODO max fragment size
4726 mejdrech 447
                * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
4589 mejdrech 448
                * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
4701 mejdrech 449
                answer_count = 3;
4579 mejdrech 450
                break;
451
            case NET_SOCKET_BIND:
4722 mejdrech 452
                res = data_receive(( void ** ) & addr, & addrlen );
4713 mejdrech 453
                if( res == EOK ){
4738 mejdrech 454
                    fibril_rwlock_read_lock( & lock );
4713 mejdrech 455
                    fibril_rwlock_write_lock( & udp_globals.lock );
456
                    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 );
457
                    fibril_rwlock_write_unlock( & udp_globals.lock );
4738 mejdrech 458
                    fibril_rwlock_read_unlock( & lock );
4713 mejdrech 459
                    free( addr );
4579 mejdrech 460
                }
4713 mejdrech 461
                break;
4579 mejdrech 462
            case NET_SOCKET_SENDTO:
4722 mejdrech 463
                res = data_receive(( void ** ) & addr, & addrlen );
4713 mejdrech 464
                if( res == EOK ){
4738 mejdrech 465
                    fibril_rwlock_read_lock( & lock );
466
                    fibril_rwlock_write_lock( & udp_globals.lock );
4726 mejdrech 467
                    res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call ));
4743 mejdrech 468
                    if( res != EOK ){
469
                        fibril_rwlock_write_unlock( & udp_globals.lock );
470
                    }
4738 mejdrech 471
                    fibril_rwlock_read_unlock( & lock );
4713 mejdrech 472
                    free( addr );
4579 mejdrech 473
                }
474
                break;
475
            case NET_SOCKET_RECVFROM:
4738 mejdrech 476
                fibril_rwlock_read_lock( & lock );
477
                fibril_rwlock_write_lock( & udp_globals.lock );
4720 mejdrech 478
                res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
4738 mejdrech 479
                fibril_rwlock_write_unlock( & udp_globals.lock );
480
                fibril_rwlock_read_unlock( & lock );
4589 mejdrech 481
                if( res > 0 ){
482
                    * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
4720 mejdrech 483
                    * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
4701 mejdrech 484
                    answer_count = 2;
4589 mejdrech 485
                    res = EOK;
486
                }
4579 mejdrech 487
                break;
488
            case NET_SOCKET_CLOSE:
4738 mejdrech 489
                fibril_rwlock_write_lock( & lock );
4700 mejdrech 490
                fibril_rwlock_write_lock( & udp_globals.lock );
4738 mejdrech 491
                res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets, NULL );
4700 mejdrech 492
                fibril_rwlock_write_unlock( & udp_globals.lock );
4738 mejdrech 493
                fibril_rwlock_write_unlock( & lock );
4579 mejdrech 494
                break;
495
            case NET_SOCKET_GETSOCKOPT:
496
            case NET_SOCKET_SETSOCKOPT:
497
            default:
498
                res = ENOTSUP;
499
                break;
500
        }
501
 
4589 mejdrech 502
//      printf( "res = %d\n", res );
4579 mejdrech 503
 
4713 mejdrech 504
        answer_call( callid, res, & answer, answer_count );
4579 mejdrech 505
    }
506
 
4738 mejdrech 507
    // release all local sockets
508
    socket_cores_release( udp_globals.net_phone, & local_sockets, & udp_globals.sockets, NULL );
4579 mejdrech 509
 
510
    return EOK;
511
}
512
 
4726 mejdrech 513
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 ){
4579 mejdrech 514
    ERROR_DECLARE;
515
 
516
    socket_core_ref         socket;
517
    packet_t                packet;
4589 mejdrech 518
    packet_t                next_packet;
4579 mejdrech 519
    udp_header_ref          header;
4589 mejdrech 520
    int                     index;
4708 mejdrech 521
    size_t                  total_length;
522
    int                     result;
4720 mejdrech 523
    uint16_t                dest_port;
4722 mejdrech 524
    uint32_t                checksum;
525
    ip_pseudo_header_ref    ip_header;
526
    size_t                  headerlen;
527
    device_id_t             device_id;
4579 mejdrech 528
 
4726 mejdrech 529
    ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & dest_port ));
4589 mejdrech 530
 
4720 mejdrech 531
    socket = socket_cores_find( local_sockets, socket_id );
532
    if( ! socket ) return ENOTSOCK;
4589 mejdrech 533
 
4730 mejdrech 534
    if(( socket->port <= 0 ) && udp_globals.autobinding ){
535
        // bind the socket to a random free port if not bound
4743 mejdrech 536
//      do{
4730 mejdrech 537
            // try to find a free port
4738 mejdrech 538
//          fibril_rwlock_read_unlock( & udp_globals.lock );
539
//          fibril_rwlock_write_lock( & udp_globals.lock );
4730 mejdrech 540
            // might be changed in the meantime
4743 mejdrech 541
//          if( socket->port <= 0 ){
4738 mejdrech 542
                if( ERROR_OCCURRED( socket_bind_free_port( & udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ))){
4743 mejdrech 543
//                  fibril_rwlock_write_unlock( & udp_globals.lock );
544
//                  fibril_rwlock_read_lock( & udp_globals.lock );
4738 mejdrech 545
                    return ERROR_CODE;
546
                }
4730 mejdrech 547
                // set the next port as the search starting port number
548
                udp_globals.last_used_port = socket->port;
4743 mejdrech 549
//          }
4738 mejdrech 550
//          fibril_rwlock_write_unlock( & udp_globals.lock );
551
//          fibril_rwlock_read_lock( & udp_globals.lock );
4730 mejdrech 552
            // might be changed in the meantime
4743 mejdrech 553
//      }while( socket->port <= 0 );
4579 mejdrech 554
    }
4720 mejdrech 555
 
556
    // TODO do not ask all the time
4726 mejdrech 557
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.packet_dimension.addr_len, & udp_globals.packet_dimension.prefix, & udp_globals.packet_dimension.content, & udp_globals.packet_dimension.suffix ));
4720 mejdrech 558
 
559
    // read the first packet fragment
4726 mejdrech 560
    result = tl_socket_read_packet_data( udp_globals.net_phone, & packet, sizeof( udp_header_t ), & udp_globals.packet_dimension, addr, addrlen );
4720 mejdrech 561
    if( result < 0 ) return result;
562
    total_length = ( size_t ) result;
4722 mejdrech 563
    if( udp_globals.checksum_computing ){
564
        checksum = compute_checksum( 0, packet_get_data( packet ), packet_get_data_length( packet ));
565
    }else{
566
        checksum = 0;
567
    }
4720 mejdrech 568
    // prefix the udp header
569
    header = PACKET_PREFIX( packet, udp_header_t );
570
    if( ! header ){
4722 mejdrech 571
        return udp_release_and_return( packet, ENOMEM );
4720 mejdrech 572
    }
4722 mejdrech 573
    bzero( header, sizeof( * header ));
4720 mejdrech 574
    // read the rest of the packet fragments
575
    for( index = 1; index < fragments; ++ index ){
4726 mejdrech 576
        result = tl_socket_read_packet_data( udp_globals.net_phone, & next_packet, 0, & udp_globals.packet_dimension, addr, addrlen );
4720 mejdrech 577
        if( result < 0 ){
578
            return udp_release_and_return( packet, result );
579
        }
580
        packet = pq_add( packet, next_packet, index, 0 );
581
        total_length += ( size_t ) result;
4722 mejdrech 582
        if( udp_globals.checksum_computing ){
583
            checksum = compute_checksum( checksum, packet_get_data( next_packet ), packet_get_data_length( next_packet ));
584
        }
4720 mejdrech 585
    }
586
    // set the udp header
4730 mejdrech 587
    header->source_port = htons(( socket->port > 0 ) ? socket->port : 0 );
4728 mejdrech 588
    header->destination_port = htons( dest_port );
4730 mejdrech 589
    header->total_length = htons( total_length + sizeof( * header ));
4728 mejdrech 590
    header->checksum = 0;
4722 mejdrech 591
    if( udp_globals.checksum_computing ){
592
        if( ERROR_OCCURRED( ip_get_route_req( udp_globals.ip_phone, IPPROTO_UDP, addr, addrlen, & device_id, & ip_header, & headerlen ))){
593
            return udp_release_and_return( packet, ERROR_CODE );
594
        }
595
        if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( ip_header, headerlen, total_length + sizeof( udp_header_t )))){
596
            free( ip_header );
597
            return udp_release_and_return( packet, ERROR_CODE );
598
        }
4731 mejdrech 599
        checksum = compute_checksum( checksum, ip_header, headerlen );
4722 mejdrech 600
        checksum = compute_checksum( checksum, ( uint8_t * ) header, sizeof( * header ));
4728 mejdrech 601
        header->checksum = htons( flip_checksum( compact_checksum( checksum )));
4722 mejdrech 602
        free( ip_header );
603
    }else{
4726 mejdrech 604
        device_id = -1;
4722 mejdrech 605
    }
4720 mejdrech 606
    // prepare the first packet fragment
607
    if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
4722 mejdrech 608
        return udp_release_and_return( packet, ERROR_CODE );
4720 mejdrech 609
    }
610
    // send the packet
4743 mejdrech 611
    fibril_rwlock_write_unlock( & udp_globals.lock );
612
    ip_send_msg( udp_globals.ip_phone, device_id, packet, SERVICE_UDP, 0 );
613
    return EOK;
4579 mejdrech 614
}
615
 
4720 mejdrech 616
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){
4579 mejdrech 617
    ERROR_DECLARE;
618
 
4589 mejdrech 619
    socket_core_ref socket;
620
    int             packet_id;
621
    packet_t        packet;
622
    udp_header_ref  header;
4720 mejdrech 623
    struct sockaddr *   addr;
4708 mejdrech 624
    size_t          length;
625
    uint8_t *       data;
626
    int             result;
4589 mejdrech 627
 
628
    // find the socket
629
    socket = socket_cores_find( local_sockets, socket_id );
630
    if( ! socket ) return ENOTSOCK;
631
    // get the next received packet
632
    packet_id = dyn_fifo_value( & socket->received );
4701 mejdrech 633
    if( packet_id < 0 ) return NO_DATA;
4589 mejdrech 634
    ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
635
    // get udp header
636
    data = packet_get_data( packet );
637
    if( ! data ){
638
        pq_release( udp_globals.net_phone, packet_id );
639
        return NO_DATA;
640
    }
641
    header = ( udp_header_ref ) data;
4720 mejdrech 642
 
643
    // set the source address port
644
    result = packet_get_addr( packet, ( uint8_t ** ) & addr, NULL );
4728 mejdrech 645
    if( ERROR_OCCURRED( tl_set_address_port( addr, result, ntohs( header->source_port )))){
4589 mejdrech 646
        pq_release( udp_globals.net_phone, packet_id );
4720 mejdrech 647
        return ERROR_CODE;
4589 mejdrech 648
    }
4720 mejdrech 649
    * addrlen = ( size_t ) result;
4589 mejdrech 650
    // send the source address
4722 mejdrech 651
    ERROR_PROPAGATE( data_reply( addr, * addrlen ));
4720 mejdrech 652
 
4738 mejdrech 653
    // trim the header
654
    ERROR_PROPAGATE( packet_trim( packet, sizeof( udp_header_t ), 0 ));
655
 
656
    // reply the packets
657
    ERROR_PROPAGATE( socket_reply_packets( packet, & length ));
658
 
4589 mejdrech 659
    // release the packet
660
    dyn_fifo_pop( & socket->received );
661
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
662
    // return the total length
4708 mejdrech 663
    return ( int ) length;
4589 mejdrech 664
}
665
 
4713 mejdrech 666
int udp_release_and_return( packet_t packet, int result ){
4707 mejdrech 667
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
668
    return result;
669
}
670
 
4499 mejdrech 671
/** @}
672
 */