Subversion Repositories HelenOS

Rev

Rev 4707 | Rev 4713 | 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>
4499 mejdrech 41
 
42
#include <ipc/ipc.h>
43
#include <ipc/services.h>
44
 
45
#include "../../err.h"
46
#include "../../messages.h"
47
#include "../../modules.h"
4589 mejdrech 48
 
49
#include "../../structures/dynamic_fifo.h"
4499 mejdrech 50
#include "../../structures/packet/packet_client.h"
51
 
4579 mejdrech 52
#include "../../include/in.h"
53
#include "../../include/inet.h"
4499 mejdrech 54
#include "../../include/ip_client.h"
55
#include "../../include/ip_interface.h"
56
#include "../../include/ip_protocols.h"
4707 mejdrech 57
#include "../../include/icmp_client.h"
58
#include "../../include/icmp_interface.h"
4579 mejdrech 59
#include "../../include/socket.h"
60
#include "../../include/socket_errno.h"
4499 mejdrech 61
 
4579 mejdrech 62
#include "../../socket/socket_core.h"
63
#include "../../socket/socket_messages.h"
64
 
4499 mejdrech 65
#include "../tl_messages.h"
66
 
67
#include "udp.h"
4579 mejdrech 68
#include "udp_header.h"
4499 mejdrech 69
#include "udp_module.h"
70
 
4701 mejdrech 71
/** Maximum UDP fragment size.
72
 */
4589 mejdrech 73
#define MAX_UDP_FRAGMENT_SIZE   65535
74
 
4701 mejdrech 75
/** Free ports pool start.
76
 */
4700 mejdrech 77
#define UDP_FREE_PORTS_START    1025
4701 mejdrech 78
 
79
/** Free ports pool end.
80
 */
4700 mejdrech 81
#define UDP_FREE_PORTS_END      65535
82
 
4707 mejdrech 83
/** Processes the received UDP packet queue.
4701 mejdrech 84
 *  Is used as an entry point from the underlying IP module.
4707 mejdrech 85
 *  Notifies the destination socket application.
86
 *  Releases the packet on error or send an ICMP error notification..
4701 mejdrech 87
 *  @param device_id The device identifier. Ignored parameter.
4707 mejdrech 88
 *  @param packet The received packet queue. Input/output parameter.
4701 mejdrech 89
 *  @param receiver The target service. Ignored parameter.
4707 mejdrech 90
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
4701 mejdrech 91
 *  @returns EOK on success.
92
 *  @returns EINVAL if the packet is not valid.
93
 *  @returns EINVAL if the stored packet address is not the an_addr_t.
94
 *  @returns EINVAL if the packet does not contain any data.
95
 *  @returns NO_DATA if the packet content is shorter than the user datagram header.
96
 *  @returns ENOMEM if there is not enough memory left.
97
 *  @returns EADDRNOTAVAIL if the destination socket does not exist.
98
 *  @returns Other error codes as defined for the ip_client_process_packet() function.
99
 */
4707 mejdrech 100
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error );
4701 mejdrech 101
 
4707 mejdrech 102
/** Releases the packet and returns the result.
103
 *  @param packet The packet queue to be released. Input parameter.
104
 *  @param result The result to be returned. Input parameter.
105
 *  @return The result parameter.
106
 */
107
static int  release_and_return( packet_t packet, int result );
108
 
109
/** Sends the port unreachable ICMP notification.
110
 *  Sends the first packet and releases all the others.
111
 *  Releases the packet queu on error.
112
 *  @param packet The packet to be send. Input parameter.
113
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
114
 */
115
void    udp_send_icmp_port_unreachable( packet_t packet, services_t error );
116
 
4701 mejdrech 117
/** @name Socket messages processing functions
118
 */
119
/*@{*/
120
 
121
/** Processes the socket client messages.
122
 *  Runs until the client module disconnects.
123
 *  @param callid The message identifier. Input parameter.
124
 *  @param call The message parameters. Input parameter.
125
 *  @returns EOK on success.
126
 *  @see socket.h
127
 */
128
int process_client_messages( ipc_callid_t callid, ipc_call_t call );
129
 
130
/** Sends data from the socket to the remote address.
131
 *  Binds the socket to a free port if not already connected/bound.
132
 *  Handles the NET_SOCKET_SENDTO message.
133
 *  @param local_sockets The application local sockets. Input/output parameter.
134
 *  @param socket_id Socket identifier. Input parameter.
135
 *  @param addr The destination address. Input parameter.
136
 *  @param addrlen The address length. Input parameter.
137
 *  @param fragments The number of data fragments. Input parameter.
138
 *  @param flags Various send flags. Input parameter.
139
 *  @returns EOK on success.
140
 *  @returns EAFNOTSUPPORT if the address family is not supported.
141
 *  @returns ENOTSOCK if the socket is not found.
142
 *  @returns EINVAL if the address is invalid.
143
 *  @returns ENOTCONN if the sending socket is not and cannot be bound.
144
 *  @returns ENOMEM if there is not enough memory left.
145
 *  @returns Other error codes as defined for the socket_read_packet_data() function.
146
 *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
147
 *  @returns Other error codes as defined for the ip_send_msg() function.
148
 */
4589 mejdrech 149
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags );
4701 mejdrech 150
 
151
/** Receives data to the socket.
152
 *  Handles the NET_SOCKET_RECVFROM message.
153
 *  @param local_sockets The application local sockets. Input parameter.
154
 *  @param socket_id Socket identifier. Input parameter.
155
 *  @param flags Various receive flags. Input parameter.
156
 *  @returns The number of bytes received.
157
 *  @returns ENOTSOCK if the socket is not found.
158
 *  @returns NO_DATA if there are no received packets or data.
159
 *  @returns ENOMEM if there is not enough memory left.
160
 *  @returns EINVAL if the received address is not an IP address.
161
 *  @returns Other error codes as defined for the packet_translate() function.
162
 *  @returns Other error codes as defined for the socket_write_data() function.
163
 */
4589 mejdrech 164
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags );
4701 mejdrech 165
 
166
/*@}*/
167
 
168
/** Receives data from the socket.
169
 *  The received data buffer is allocated and returned.
170
 *  @param data The data buffer to be filled. Output parameter.
171
 *  @param length The buffer length. Output parameter.
172
 *  @returns EOK on success.
173
 *  @returns EBADMEM if the data or the length parameter is NULL.
174
 *  @returns EINVAL if the client does not send data.
175
 *  @returns ENOMEM if there is not enough memory left.
176
 *  @returns Other error codes as defined for the ipc_data_write_finalize() function.
177
 */
4589 mejdrech 178
int socket_read_data( void ** data, size_t * length );
4701 mejdrech 179
 
180
/** Receives data from the socket into a packet.
181
 *  @param packet The new created packet. Output parameter.
182
 *  @param prefix Reserved packet data prefix length. Input parameter.
183
 *  @param address_in The destination address to be set. Input parameter.
184
 *  @returns Number of bytes received.
185
 *  @returns EINVAL if the client does not send data.
186
 *  @returns ENOMEM if there is not enough memory left.
187
 *  @returns Other error codes as defined for the ipc_data_read_finalize() function.
188
 */
4589 mejdrech 189
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in );
4701 mejdrech 190
 
191
/** Replies the data to the socket.
192
 *  @param data The data buffer to be sent. Input parameter.
193
 *  @param data_length The buffer length. Input parameter.
194
 *  @returns EOK on success.
195
 *  @returns EINVAL if the client does not expect all the data.
196
 *  @returns Other error codes as defined for the ipc_data_read_finalize() function.
197
 */
4589 mejdrech 198
int socket_write_data( void * data, size_t data_length );
4499 mejdrech 199
 
4701 mejdrech 200
/** UDP global data.
201
 */
4499 mejdrech 202
udp_globals_t   udp_globals;
203
 
204
int udp_initialize( async_client_conn_t client_connection ){
4579 mejdrech 205
    ERROR_DECLARE;
4499 mejdrech 206
 
4700 mejdrech 207
    fibril_rwlock_initialize( & udp_globals.lock );
208
    fibril_rwlock_write_lock( & udp_globals.lock );
4707 mejdrech 209
    udp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP );
210
    if( udp_globals.icmp_phone < 0 ){
211
        return udp_globals.icmp_phone;
212
    }
4499 mejdrech 213
    udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
4579 mejdrech 214
    if( udp_globals.ip_phone < 0 ){
215
        return udp_globals.ip_phone;
216
    }
217
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
218
    ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
219
    udp_globals.prefix += sizeof( udp_header_t );
220
    udp_globals.content -= sizeof( udp_header_t );
4700 mejdrech 221
    udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
222
    fibril_rwlock_write_unlock( & udp_globals.lock );
4579 mejdrech 223
    return EOK;
4499 mejdrech 224
}
225
 
4707 mejdrech 226
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){
4589 mejdrech 227
    ERROR_DECLARE;
228
 
4708 mejdrech 229
    size_t          length;
230
    size_t          offset;
231
    int             result;
4707 mejdrech 232
    uint8_t *       data;
4589 mejdrech 233
    udp_header_ref  header;
234
    socket_core_ref *   socket;
235
    packet_t        next_packet;
4708 mejdrech 236
    size_t          total_length;
4589 mejdrech 237
//  uint16_t        checksum;
238
    int             fragments;
239
    packet_t        tmp_packet;
4707 mejdrech 240
    icmp_type_t     type;
241
    icmp_code_t     code;
4589 mejdrech 242
 
4707 mejdrech 243
    if( error ){
244
        switch( error ){
245
            case SERVICE_ICMP:
246
                // process error
247
                // TODO remove debug dump
248
                // length = icmp_client_header_length( packet );
4708 mejdrech 249
                result = icmp_client_process_packet( packet, & type, & code, NULL, NULL );
250
                if( result < 0 ){
251
                    return release_and_return( packet, result );
4707 mejdrech 252
                }
253
                printf( "ICMP error %d (%d) in packet %d\n", type, code, packet_get_id( packet ) );
4708 mejdrech 254
                length = ( size_t ) result;
4707 mejdrech 255
                if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){
256
                    return release_and_return( packet, ERROR_CODE );
257
                }
258
                break;
259
            default:
260
                return release_and_return( packet, ENOTSUP );
261
        }
262
    }
4701 mejdrech 263
    // TODO process received ipopts?
4708 mejdrech 264
    result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL );
265
    if( result < 0 ){
266
        return release_and_return( packet, result );
4707 mejdrech 267
    }
4708 mejdrech 268
    offset = ( size_t ) result;
4589 mejdrech 269
 
270
    length = packet_get_data_length( packet );
4707 mejdrech 271
    if( length <= 0 ){
272
        return release_and_return( packet, EINVAL );
273
    }
274
    if( length < sizeof( udp_header_t ) + offset ){
275
        return release_and_return( packet, NO_DATA );
276
    }
4499 mejdrech 277
    data = packet_get_data( packet );
4707 mejdrech 278
    if( ! data ){
279
        return release_and_return( packet, NO_DATA );
280
    }
4589 mejdrech 281
    // get udp header
4707 mejdrech 282
    header = ( udp_header_ref )( data + offset );
4589 mejdrech 283
    // find the destination socket
284
    socket = socket_ports_find( & udp_globals.sockets, ntohs( header->dest ));
4707 mejdrech 285
    if( ! socket ){
286
        udp_send_icmp_port_unreachable( packet, error );
287
        return EADDRNOTAVAIL;
288
    }
289
    // trim after successful processing to be able to send an ICMP error message!
290
    ERROR_PROPAGATE( packet_trim( packet, offset, 0 ));
4589 mejdrech 291
    // count the received packet fragments
292
    next_packet = packet;
293
    fragments = 0;
294
    total_length = ntohs( header->len );
295
    do{
296
        ++ fragments;
297
        length = packet_get_data_length( packet );
4708 mejdrech 298
        if( length <= 0 ){
4707 mejdrech 299
            return release_and_return( packet, NO_DATA );
300
        }
4589 mejdrech 301
        if( total_length < length ){
302
            // cut of the suffix if too long
4707 mejdrech 303
            if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - total_length ))){
304
                return release_and_return( packet, ERROR_CODE );
305
            }
4589 mejdrech 306
            // relese the rest of the packet fragments
307
            tmp_packet = pq_next( next_packet );
308
            while( tmp_packet ){
309
                next_packet = pq_detach( tmp_packet );
310
                pq_release( udp_globals.net_phone, packet_get_id( tmp_packet ));
311
                tmp_packet = next_packet;
312
            }
313
            break;
314
        }
315
        total_length -= length;
316
    /*  if( header->header_checksum ){
317
        }
318
    */
319
    }while(( next_packet = pq_next( next_packet )) && ( total_length > 0 ));
320
    // queue the received packet
4707 mejdrech 321
    if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){
322
        return release_and_return( packet, ERROR_CODE );
323
    }
4499 mejdrech 324
 
4589 mejdrech 325
    // notify the destination socket
4708 mejdrech 326
    async_msg_2(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) ( ** socket ).socket_id, ( ipcarg_t ) fragments );
4499 mejdrech 327
    return EOK;
328
}
329
 
330
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
4505 mejdrech 331
    ERROR_DECLARE;
332
 
333
    packet_t    packet;
334
 
4499 mejdrech 335
    * answer_count = 0;
336
    switch( IPC_GET_METHOD( * call )){
337
        case NET_TL_RECEIVED:
4700 mejdrech 338
            fibril_rwlock_read_lock( & udp_globals.lock );
339
            if( ! ERROR_OCCURRED( packet_translate( udp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){
4707 mejdrech 340
                ERROR_CODE = udp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_UDP, IPC_GET_ERROR( call ));
4700 mejdrech 341
            }
342
            fibril_rwlock_read_unlock( & udp_globals.lock );
343
            return ERROR_CODE;
4579 mejdrech 344
        case IPC_M_CONNECT_TO_ME:
4701 mejdrech 345
            return process_client_messages( callid, * call );
4499 mejdrech 346
    }
347
    return ENOTSUP;
348
}
349
 
4701 mejdrech 350
int process_client_messages( ipc_callid_t callid, ipc_call_t call ){
4579 mejdrech 351
    ERROR_DECLARE;
352
 
353
    int                     res;
354
    bool                    keep_on_going = true;
355
    socket_cores_t          local_sockets;
4701 mejdrech 356
    int                     app_phone = IPC_GET_PHONE( & call );
4579 mejdrech 357
    void *                  addr;
358
    size_t                  addrlen;
4700 mejdrech 359
    fibril_rwlock_t         lock;
4701 mejdrech 360
    ipc_call_t              answer;
361
    int                     answer_count;
4579 mejdrech 362
 
363
    /*
364
     * Accept the connection
365
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
366
     */
367
    ipc_answer_0( callid, EOK );
368
 
369
    socket_cores_initialize( & local_sockets );
4700 mejdrech 370
    fibril_rwlock_initialize( & lock );
4579 mejdrech 371
 
372
    while( keep_on_going ){
373
        // refresh data
4701 mejdrech 374
        answer_count = 0;
375
        IPC_SET_RETVAL( answer, 0 );
4579 mejdrech 376
        // just to be precize
4701 mejdrech 377
        IPC_SET_METHOD( answer, 0 );
378
        IPC_SET_ARG1( answer, 0 );
379
        IPC_SET_ARG2( answer, 0 );
380
        IPC_SET_ARG3( answer, 0 );
381
        IPC_SET_ARG4( answer, 0 );
382
        IPC_SET_ARG5( answer, 0 );
4579 mejdrech 383
 
4701 mejdrech 384
        callid = async_get_call( & call );
4589 mejdrech 385
//      printf( "message %d\n", IPC_GET_METHOD( * call ));
4579 mejdrech 386
 
4701 mejdrech 387
        switch( IPC_GET_METHOD( call )){
4579 mejdrech 388
            case IPC_M_PHONE_HUNGUP:
389
                keep_on_going = false;
390
                res = EOK;
391
                break;
392
            case NET_SOCKET:
4700 mejdrech 393
                fibril_rwlock_write_lock( & lock );
4589 mejdrech 394
                res = socket_create( & local_sockets, app_phone, SOCKET_SET_SOCKET_ID( answer ));
4700 mejdrech 395
                fibril_rwlock_write_unlock( & lock );
4589 mejdrech 396
                * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
397
                * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
4701 mejdrech 398
                answer_count = 3;
4579 mejdrech 399
                break;
400
            case NET_SOCKET_BIND:
4589 mejdrech 401
                if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
4579 mejdrech 402
                    res = ERROR_CODE;
403
                    break;
404
                }
4700 mejdrech 405
                fibril_rwlock_write_lock( & lock );
406
                fibril_rwlock_write_lock( & udp_globals.lock );
407
                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 );
408
                fibril_rwlock_write_unlock( & udp_globals.lock );
409
                fibril_rwlock_write_unlock( & lock );
4579 mejdrech 410
                free( addr );
4701 mejdrech 411
                break; 
4579 mejdrech 412
            case NET_SOCKET_SENDTO:
4589 mejdrech 413
                if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
4579 mejdrech 414
                    res = ERROR_CODE;
415
                    break;
416
                }
4700 mejdrech 417
                fibril_rwlock_read_lock( & lock );
418
                fibril_rwlock_read_lock( & udp_globals.lock );
4589 mejdrech 419
                res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_FLAGS( call ));
4700 mejdrech 420
                fibril_rwlock_read_unlock( & udp_globals.lock );
421
                fibril_rwlock_read_unlock( & lock );
4579 mejdrech 422
                free( addr );
423
                break;
424
            case NET_SOCKET_RECVFROM:
4700 mejdrech 425
                fibril_rwlock_read_lock( & lock );
426
                fibril_rwlock_read_lock( & udp_globals.lock );
4589 mejdrech 427
                res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ));
4700 mejdrech 428
                fibril_rwlock_read_unlock( & udp_globals.lock );
429
                fibril_rwlock_read_unlock( & lock );
4589 mejdrech 430
                if( res > 0 ){
431
                    * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
4603 mejdrech 432
                    * SOCKET_SET_ADDRESS_LENGTH( answer ) = sizeof( struct sockaddr_in );
4701 mejdrech 433
                    answer_count = 2;
4589 mejdrech 434
                    res = EOK;
435
                }
4579 mejdrech 436
                break;
437
            case NET_SOCKET_CLOSE:
4700 mejdrech 438
                fibril_rwlock_write_lock( & lock );
439
                fibril_rwlock_write_lock( & udp_globals.lock );
4579 mejdrech 440
                res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
4700 mejdrech 441
                fibril_rwlock_write_unlock( & udp_globals.lock );
442
                fibril_rwlock_write_unlock( & lock );
4579 mejdrech 443
                break;
444
            case NET_SOCKET_GETSOCKOPT:
445
            case NET_SOCKET_SETSOCKOPT:
446
            default:
447
                res = ENOTSUP;
448
                break;
449
        }
450
 
4589 mejdrech 451
//      printf( "res = %d\n", res );
4579 mejdrech 452
 
4701 mejdrech 453
        switch( answer_count ){
4708 mejdrech 454
            case 0:     ipc_answer_0( callid, ( ipcarg_t ) res );
4579 mejdrech 455
                    continue;
4708 mejdrech 456
            case 1:     ipc_answer_1( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ));
4579 mejdrech 457
                    continue;
4708 mejdrech 458
            case 2:     ipc_answer_2( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ));
4579 mejdrech 459
                    continue;
4708 mejdrech 460
            case 3:     ipc_answer_3( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ));
4579 mejdrech 461
                    continue;
4708 mejdrech 462
            case 4:     ipc_answer_4( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ), IPC_GET_ARG4( answer ));
4579 mejdrech 463
                    continue;
4708 mejdrech 464
            default:    ipc_answer_5( callid, ( ipcarg_t ) res, IPC_GET_ARG1( answer ), IPC_GET_ARG2( answer ), IPC_GET_ARG3( answer ), IPC_GET_ARG4( answer ), IPC_GET_ARG5( answer ));
4579 mejdrech 465
                    continue;
466
        }
467
    }
468
 
469
    socket_cores_destroy( & local_sockets );
470
 
471
    return EOK;
472
}
473
 
4589 mejdrech 474
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags ){
4579 mejdrech 475
    ERROR_DECLARE;
476
 
477
    socket_core_ref         socket;
478
    struct sockaddr *       address;
479
    struct sockaddr_in *    address_in;
480
    packet_t                packet;
4589 mejdrech 481
    packet_t                next_packet;
4579 mejdrech 482
    udp_header_ref          header;
4589 mejdrech 483
    int                     index;
4708 mejdrech 484
    size_t                  total_length;
485
    int                     result;
4579 mejdrech 486
 
487
    if( addrlen < sizeof( struct sockaddr )) return EINVAL;
488
    address = ( struct sockaddr * ) addr;
489
    switch( address->sa_family ){
490
        case AF_INET:
491
            if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
492
            address_in = ( struct sockaddr_in * ) addr;
493
            socket = socket_cores_find( local_sockets, socket_id );
494
            if( ! socket ) return ENOTSOCK;
4589 mejdrech 495
 
4700 mejdrech 496
            // bind the socket to a random free port if not bound
497
            if( socket->port <= 0 ){
498
                // try to find a free port
499
                fibril_rwlock_read_unlock( & udp_globals.lock );
500
                fibril_rwlock_write_lock( & udp_globals.lock );
501
                ERROR_PROPAGATE( socket_bind_free_port( & udp_globals.sockets, socket, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, udp_globals.last_used_port ));
502
                fibril_rwlock_write_unlock( & udp_globals.lock );
503
                fibril_rwlock_read_lock( & udp_globals.lock );
504
                // set the next port as the search starting port number
505
                udp_globals.last_used_port = socket->port;
506
            }
4579 mejdrech 507
            // TODO do not ask all the time
508
            ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
4589 mejdrech 509
 
510
            // read the first packet fragment
4708 mejdrech 511
            result = socket_read_packet_data( & packet, sizeof( udp_header_t ), address_in );
512
            if( result < 0 ) return result;
513
            total_length = ( size_t ) result;
4589 mejdrech 514
            // prefix the udp header
4579 mejdrech 515
            header = PACKET_PREFIX( packet, udp_header_t );
516
            if( ! header ){
517
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
518
                return ENOMEM;
519
            }
4589 mejdrech 520
            // read the rest of the packet fragments
521
            for( index = 1; index < fragments; ++ index ){
4708 mejdrech 522
                result = socket_read_packet_data( & next_packet, 0, address_in );
523
                if( result < 0 ){
524
                    return release_and_return( packet, result );
4589 mejdrech 525
                }
526
                packet = pq_add( packet, next_packet, index, 0 );
4708 mejdrech 527
                total_length += ( size_t ) result;
4589 mejdrech 528
            }
529
            // set the udp header
4708 mejdrech 530
            header->source = htons( socket->port );
4579 mejdrech 531
            header->dest = htons( address_in->sin_port );
4589 mejdrech 532
            header->len = htons( total_length + sizeof( udp_header_t ));
4579 mejdrech 533
            // TODO my ip address for the pseudo header checksum
534
            header->check = 0;
4589 mejdrech 535
            // prepare the first packet fragment
4579 mejdrech 536
            if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
537
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
538
                return ERROR_CODE;
539
            }
4589 mejdrech 540
            // send the packet
4707 mejdrech 541
            return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP, 0 );
4579 mejdrech 542
        // TODO IPv6
543
    }
4708 mejdrech 544
    return EAFNOSUPPORT;
4579 mejdrech 545
}
546
 
4589 mejdrech 547
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags ){
4579 mejdrech 548
    ERROR_DECLARE;
549
 
4589 mejdrech 550
    socket_core_ref socket;
551
    int             packet_id;
552
    packet_t        packet;
553
    udp_header_ref  header;
554
    struct sockaddr_in      address;
4708 mejdrech 555
    size_t          length;
4589 mejdrech 556
    packet_t        next_packet;
4708 mejdrech 557
    uint8_t *       data;
558
    size_t          fragments;
559
    size_t *        lengths;
560
    int             result;
561
    size_t          index;
4589 mejdrech 562
    uint8_t *       addr;
563
 
564
    // find the socket
565
    socket = socket_cores_find( local_sockets, socket_id );
566
    if( ! socket ) return ENOTSOCK;
567
    // get the next received packet
568
    packet_id = dyn_fifo_value( & socket->received );
4701 mejdrech 569
    if( packet_id < 0 ) return NO_DATA;
4589 mejdrech 570
    ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
571
    // get udp header
572
    data = packet_get_data( packet );
573
    if( ! data ){
574
        pq_release( udp_globals.net_phone, packet_id );
575
        return NO_DATA;
576
    }
577
    header = ( udp_header_ref ) data;
578
    // set the source address
579
    address.sin_family = PF_INET;
4603 mejdrech 580
    address.sin_port = ntohs( header->source );
4708 mejdrech 581
    result = packet_get_addr( packet, & addr, NULL );
582
    if( result != sizeof( address.sin_addr.s_addr )){
4589 mejdrech 583
        pq_release( udp_globals.net_phone, packet_id );
584
        return EINVAL;
585
    }
586
    address.sin_addr.s_addr = *(( uint32_t * ) addr );
587
    bzero( & address.sin_zero, sizeof( address.sin_zero ));
588
    // send the source address
589
    ERROR_PROPAGATE( socket_write_data( & address, sizeof( address )));
590
    next_packet = pq_next( packet );
591
    if( ! next_packet ){
592
        // write all if only one fragment
593
        ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), packet_get_data_length( packet ) - sizeof( udp_header_t )));
594
        // store the total length
595
        length = packet_get_data_length( packet ) - sizeof( udp_header_t );
596
    }else{
597
        // count the packet fragments
598
        fragments = 1;
599
        next_packet = pq_next( packet );
600
        while(( next_packet = pq_next( next_packet ))){
601
            ++ fragments;
602
        }
603
        // compute and store the fragment lengths
4708 mejdrech 604
        lengths = ( size_t * ) malloc( sizeof( size_t ) * fragments + sizeof( size_t ));
4589 mejdrech 605
        if( ! lengths ) return ENOMEM;
606
        lengths[ 0 ] = packet_get_data_length( packet ) - sizeof( udp_header_t );
607
        lengths[ fragments ] = lengths[ 0 ];
608
        next_packet = pq_next( packet );
609
        for( index = 1; index < fragments; ++ index ){
610
            lengths[ index ] = packet_get_data_length( next_packet );
611
            lengths[ fragments ] += lengths[ index ];
612
            next_packet = pq_next( packet );
613
        }while( next_packet );
614
        // write the fragment lengths
615
        ERROR_PROPAGATE( socket_write_data( lengths, sizeof( int ) * ( fragments + 1 )));
616
        // write the first fragment
617
        ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), lengths[ 0 ] ));
618
        next_packet = pq_next( packet );
619
        // write the rest of the fragments
620
        for( index = 1; index < fragments; ++ index ){
621
            ERROR_PROPAGATE( socket_write_data( packet_get_data( next_packet ), lengths[ index ] ));
622
            next_packet = pq_next( packet );
623
        }while( next_packet );
624
        // store the total length
625
        length = lengths[ fragments ];
626
        free( lengths );
627
    }
628
    // release the packet
629
    dyn_fifo_pop( & socket->received );
630
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
631
    // return the total length
4708 mejdrech 632
    return ( int ) length;
4589 mejdrech 633
}
634
 
635
int socket_write_data( void * data, size_t data_length ){
636
    size_t          length;
4579 mejdrech 637
    ipc_callid_t    callid;
638
 
4589 mejdrech 639
    if(( ! ipc_data_read_receive( & callid, & length ))
640
    || ( length < data_length )){
641
        return EINVAL;
642
    }
643
    return ipc_data_read_finalize( callid, data, data_length );
644
}
645
 
646
int socket_read_data( void ** data, size_t * length ){
647
    ERROR_DECLARE;
648
 
649
    ipc_callid_t    callid;
650
 
4579 mejdrech 651
    if( !( data && length )) return EBADMEM;
652
    if( ! ipc_data_write_receive( & callid, length )) return EINVAL;
653
    * data = malloc( * length );
4701 mejdrech 654
    if( !( * data )) return ENOMEM;
4579 mejdrech 655
    if( ERROR_OCCURRED( ipc_data_write_finalize( callid, * data, * length ))){
656
        free( data );
657
        return ERROR_CODE;
658
    }
659
    return EOK;
660
}
661
 
4589 mejdrech 662
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in ){
663
    ERROR_DECLARE;
664
 
665
    ipc_callid_t    callid;
666
    size_t          length;
667
    void *          data;
668
 
669
    // get the data length
670
    if( ! ipc_data_write_receive( & callid, & length )) return EINVAL;
671
    // get a new packet
672
    * packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, prefix + udp_globals.prefix, udp_globals.suffix );
673
    if( ! packet ) return ENOMEM;
674
    // allocate space in the packet
675
    data = packet_suffix( * packet, length );
676
    if( ! data ){
677
        pq_release( udp_globals.net_phone, packet_get_id( * packet ));
678
        return ENOMEM;
679
    }
680
    // read the data into the packet
681
    if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length ))
682
    // set the packet destination address
683
    || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
684
        pq_release( udp_globals.net_phone, packet_get_id( * packet ));
685
        return ERROR_CODE;
686
    }
4708 mejdrech 687
    return ( int ) length;
4589 mejdrech 688
}
689
 
4707 mejdrech 690
static int  release_and_return( packet_t packet, int result ){
691
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
692
    return result;
693
}
694
 
695
void udp_send_icmp_port_unreachable( packet_t packet, services_t error ){
696
    packet_t    next;
697
    uint8_t *   src;
698
    int         length;
699
 
700
    // detach the first packet and release the others
701
    next = pq_detach( packet );
702
    if( next ){
703
        pq_release( udp_globals.net_phone, packet_get_id( next ));
704
    }
705
    length = packet_get_addr( packet, & src, NULL );
706
    if(( length > 0 )
707
    && ( ! error )
708
    && ( udp_globals.icmp_phone >= 0 )
709
    // set both addresses to the source one (avoids the source address deletion before setting the destination one)
4708 mejdrech 710
    && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){
4707 mejdrech 711
        icmp_destination_unreachable_msg( udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet );
712
    }else{
4708 mejdrech 713
        release_and_return( packet, EINVAL );
4707 mejdrech 714
    }
715
}
716
 
4499 mejdrech 717
/** @}
718
 */