Subversion Repositories HelenOS

Rev

Rev 4722 | Rev 4728 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4722 Rev 4726
Line 63... Line 63...
63
#include "../../include/socket_errno.h"
63
#include "../../include/socket_errno.h"
64
 
64
 
65
#include "../../socket/socket_core.h"
65
#include "../../socket/socket_core.h"
66
#include "../../socket/socket_messages.h"
66
#include "../../socket/socket_messages.h"
67
 
67
 
-
 
68
#include "../tl_common.h"
68
#include "../tl_messages.h"
69
#include "../tl_messages.h"
69
 
70
 
70
#include "udp.h"
71
#include "udp.h"
71
#include "udp_header.h"
72
#include "udp_header.h"
72
#include "udp_module.h"
73
#include "udp_module.h"
Line 111... Line 112...
111
 *  @param result The result to be returned. Input parameter.
112
 *  @param result The result to be returned. Input parameter.
112
 *  @return The result parameter.
113
 *  @return The result parameter.
113
 */
114
 */
114
int udp_release_and_return( packet_t packet, int result );
115
int udp_release_and_return( packet_t packet, int result );
115
 
116
 
116
/** Sends the port unreachable ICMP notification.
-
 
117
 *  Sends the first packet and releases all the others.
-
 
118
 *  Releases the packet queu on error.
-
 
119
 *  @param packet The packet to be send. Input parameter.
-
 
120
 *  @param error The packet error reporting service. Prefixes the received packet. Input parameter.
-
 
121
 */
-
 
122
void    udp_send_icmp_port_unreachable( packet_t packet, services_t error );
-
 
123
 
-
 
124
/** @name Socket messages processing functions
117
/** @name Socket messages processing functions
125
 */
118
 */
126
/*@{*/
119
/*@{*/
127
 
120
 
128
/** Processes the socket client messages.
121
/** Processes the socket client messages.
Line 141... Line 134...
141
 *  @param local_sockets The application local sockets. Input/output parameter.
134
 *  @param local_sockets The application local sockets. Input/output parameter.
142
 *  @param socket_id Socket identifier. Input parameter.
135
 *  @param socket_id Socket identifier. Input parameter.
143
 *  @param addr The destination address. Input parameter.
136
 *  @param addr The destination address. Input parameter.
144
 *  @param addrlen The address length. Input parameter.
137
 *  @param addrlen The address length. Input parameter.
145
 *  @param fragments The number of data fragments. Input parameter.
138
 *  @param fragments The number of data fragments. Input parameter.
-
 
139
 *  @param data_fragment_size The data fragment size in bytes. Input parameter.
146
 *  @param flags Various send flags. Input parameter.
140
 *  @param flags Various send flags. Input parameter.
147
 *  @returns EOK on success.
141
 *  @returns EOK on success.
148
 *  @returns EAFNOTSUPPORT if the address family is not supported.
142
 *  @returns EAFNOTSUPPORT if the address family is not supported.
149
 *  @returns ENOTSOCK if the socket is not found.
143
 *  @returns ENOTSOCK if the socket is not found.
150
 *  @returns EINVAL if the address is invalid.
144
 *  @returns EINVAL if the address is invalid.
Line 152... Line 146...
152
 *  @returns ENOMEM if there is not enough memory left.
146
 *  @returns ENOMEM if there is not enough memory left.
153
 *  @returns Other error codes as defined for the socket_read_packet_data() function.
147
 *  @returns Other error codes as defined for the socket_read_packet_data() function.
154
 *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
148
 *  @returns Other error codes as defined for the ip_client_prepare_packet() function.
155
 *  @returns Other error codes as defined for the ip_send_msg() function.
149
 *  @returns Other error codes as defined for the ip_send_msg() function.
156
 */
150
 */
157
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, int flags );
151
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 );
158
 
152
 
159
/** Receives data to the socket.
153
/** Receives data to the socket.
160
 *  Handles the NET_SOCKET_RECVFROM message.
154
 *  Handles the NET_SOCKET_RECVFROM message.
161
 *  Replies the source address as well.
155
 *  Replies the source address as well.
162
 *  @param local_sockets The application local sockets. Input parameter.
156
 *  @param local_sockets The application local sockets. Input parameter.
Line 173... Line 167...
173
 */
167
 */
174
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
168
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen );
175
 
169
 
176
/*@}*/
170
/*@}*/
177
 
171
 
178
/** Receives data from the socket into a packet.
-
 
179
 *  @param packet The new created packet. Output parameter.
-
 
180
 *  @param prefix Reserved packet data prefix length. Input parameter.
-
 
181
 *  @param addr The destination address. Input parameter.
-
 
182
 *  @param addrlen The address length. Input parameter.
-
 
183
 *  @returns Number of bytes received.
-
 
184
 *  @returns EINVAL if the client does not send data.
-
 
185
 *  @returns ENOMEM if there is not enough memory left.
-
 
186
 *  @returns Other error codes as defined for the ipc_data_read_finalize() function.
-
 
187
 */
-
 
188
int socket_read_packet_data( packet_ref packet, size_t prefix, const struct sockaddr * addr, socklen_t addrlen );
-
 
189
 
-
 
190
/** Sets the address port.
-
 
191
 *  Supports AF_INET and AF_INET6 address families.
-
 
192
 *  @param addr The address to be updated. Input/output parameter.
-
 
193
 *  @param addrlen The address length. Input parameter.
-
 
194
 *  @param port The port to be set. Input parameter.
-
 
195
 *  @returns EOK on success.
-
 
196
 *  @returns EINVAL if the address length does not match the address family.
-
 
197
 *  @returns EAFNOSUPPORT if the address family is not supported.
-
 
198
 */
-
 
199
int udp_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port );
-
 
200
 
-
 
201
/** UDP global data.
172
/** UDP global data.
202
 */
173
 */
203
udp_globals_t   udp_globals;
174
udp_globals_t   udp_globals;
204
 
175
 
205
int udp_initialize( async_client_conn_t client_connection ){
176
int udp_initialize( async_client_conn_t client_connection ){
Line 218... Line 189...
218
    }
189
    }
219
    udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
190
    udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
220
    if( udp_globals.ip_phone < 0 ){
191
    if( udp_globals.ip_phone < 0 ){
221
        return udp_globals.ip_phone;
192
        return udp_globals.ip_phone;
222
    }
193
    }
223
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
194
    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 ));
224
    ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
195
    ERROR_PROPAGATE( socket_ports_initialize( & udp_globals.sockets ));
225
    udp_globals.prefix += sizeof( udp_header_t );
196
    udp_globals.packet_dimension.prefix += sizeof( udp_header_t );
226
    udp_globals.content -= sizeof( udp_header_t );
197
    udp_globals.packet_dimension.content -= sizeof( udp_header_t );
227
    udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
198
    udp_globals.last_used_port = UDP_FREE_PORTS_START - 1;
228
    // get configuration
199
    // get configuration
229
    udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
200
    udp_globals.checksum_computing = NET_DEFAULT_UDP_CHECKSUM_COMPUTING;
230
    configuration = & names[ 0 ];
201
    configuration = & names[ 0 ];
231
    ERROR_PROPAGATE( net_get_conf_req( udp_globals.net_phone, & configuration, count, & data ));
202
    ERROR_PROPAGATE( net_get_conf_req( udp_globals.net_phone, & configuration, count, & data ));
Line 300... Line 271...
300
    // get udp header
271
    // get udp header
301
    header = ( udp_header_ref )( data + offset );
272
    header = ( udp_header_ref )( data + offset );
302
    // find the destination socket
273
    // find the destination socket
303
    socket = socket_ports_find( & udp_globals.sockets, ntohs( header->dest ));
274
    socket = socket_ports_find( & udp_globals.sockets, ntohs( header->dest ));
304
    if( ! socket ){
275
    if( ! socket ){
305
        udp_send_icmp_port_unreachable( packet, error );
276
        tl_send_icmp_port_unreachable( udp_globals.net_phone, udp_globals.icmp_phone, packet, error );
306
        return EADDRNOTAVAIL;
277
        return EADDRNOTAVAIL;
307
    }
278
    }
308
    // trim after successful processing to be able to send an ICMP error message!
279
    // trim after successful processing to be able to send an ICMP error message!
309
    ERROR_PROPAGATE( packet_trim( packet, offset, 0 ));
280
    ERROR_PROPAGATE( packet_trim( packet, offset, 0 ));
310
    // count the received packet fragments
281
    // count the received packet fragments
Line 371... Line 342...
371
    if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){
342
    if( ERROR_OCCURRED( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){
372
        return udp_release_and_return( packet, ERROR_CODE );
343
        return udp_release_and_return( packet, ERROR_CODE );
373
    }
344
    }
374
 
345
 
375
    // notify the destination socket
346
    // notify the destination socket
376
    async_msg_2(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) ( ** socket ).socket_id, ( ipcarg_t ) fragments );
347
    async_msg_5(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) ( ** socket ).socket_id, 0, 0, 0, ( ipcarg_t ) fragments );
377
    return EOK;
348
    return EOK;
378
}
349
}
379
 
350
 
380
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
351
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
381
    ERROR_DECLARE;
352
    ERROR_DECLARE;
Line 402... Line 373...
402
    bool                    keep_on_going = true;
373
    bool                    keep_on_going = true;
403
    socket_cores_t          local_sockets;
374
    socket_cores_t          local_sockets;
404
    int                     app_phone = IPC_GET_PHONE( & call );
375
    int                     app_phone = IPC_GET_PHONE( & call );
405
    struct sockaddr *       addr;
376
    struct sockaddr *       addr;
406
    size_t                  addrlen;
377
    size_t                  addrlen;
407
    fibril_rwlock_t         lock;
378
//  fibril_rwlock_t         lock;
408
    ipc_call_t              answer;
379
    ipc_call_t              answer;
409
    int                     answer_count;
380
    int                     answer_count;
410
 
381
 
411
    /*
382
    /*
412
     * Accept the connection
383
     * Accept the connection
413
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
384
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
414
     */
385
     */
415
    ipc_answer_0( callid, EOK );
386
    ipc_answer_0( callid, EOK );
416
 
387
 
-
 
388
    // The client connection is only in one fibril and therefore no additional locks are needed.
-
 
389
 
417
    socket_cores_initialize( & local_sockets );
390
    socket_cores_initialize( & local_sockets );
418
    fibril_rwlock_initialize( & lock );
391
//  fibril_rwlock_initialize( & lock );
419
 
392
 
420
    while( keep_on_going ){
393
    while( keep_on_going ){
421
        // refresh data
394
        // refresh data
422
        refresh_answer( & answer, & answer_count );
395
        refresh_answer( & answer, & answer_count );
423
 
396
 
Line 428... Line 401...
428
            case IPC_M_PHONE_HUNGUP:
401
            case IPC_M_PHONE_HUNGUP:
429
                keep_on_going = false;
402
                keep_on_going = false;
430
                res = EOK;
403
                res = EOK;
431
                break;
404
                break;
432
            case NET_SOCKET:
405
            case NET_SOCKET:
433
                fibril_rwlock_write_lock( & lock );
406
//              fibril_rwlock_write_lock( & lock );
434
                res = socket_create( & local_sockets, app_phone, SOCKET_SET_SOCKET_ID( answer ));
407
                res = socket_create( & local_sockets, app_phone, NULL, SOCKET_SET_SOCKET_ID( answer ));
435
                fibril_rwlock_write_unlock( & lock );
408
//              fibril_rwlock_write_unlock( & lock );
436
                * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
-
 
437
                * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
409
                * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
-
 
410
                * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
438
                answer_count = 3;
411
                answer_count = 3;
439
                break;
412
                break;
440
            case NET_SOCKET_BIND:
413
            case NET_SOCKET_BIND:
441
                res = data_receive(( void ** ) & addr, & addrlen );
414
                res = data_receive(( void ** ) & addr, & addrlen );
442
                if( res == EOK ){
415
                if( res == EOK ){
443
                    fibril_rwlock_write_lock( & lock );
416
//                  fibril_rwlock_write_lock( & lock );
444
                    fibril_rwlock_write_lock( & udp_globals.lock );
417
                    fibril_rwlock_write_lock( & udp_globals.lock );
445
                    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 );
418
                    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 );
446
                    fibril_rwlock_write_unlock( & udp_globals.lock );
419
                    fibril_rwlock_write_unlock( & udp_globals.lock );
447
                    fibril_rwlock_write_unlock( & lock );
420
//                  fibril_rwlock_write_unlock( & lock );
448
                    free( addr );
421
                    free( addr );
449
                }
422
                }
450
                break;
423
                break;
451
            case NET_SOCKET_SENDTO:
424
            case NET_SOCKET_SENDTO:
452
                res = data_receive(( void ** ) & addr, & addrlen );
425
                res = data_receive(( void ** ) & addr, & addrlen );
453
                if( res == EOK ){
426
                if( res == EOK ){
454
                    fibril_rwlock_read_lock( & lock );
427
//                  fibril_rwlock_read_lock( & lock );
455
                    fibril_rwlock_read_lock( & udp_globals.lock );
428
                    fibril_rwlock_read_lock( & udp_globals.lock );
456
                    res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_FLAGS( call ));
429
                    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 ));
457
                    fibril_rwlock_read_unlock( & udp_globals.lock );
430
                    fibril_rwlock_read_unlock( & udp_globals.lock );
458
                    fibril_rwlock_read_unlock( & lock );
431
//                  fibril_rwlock_read_unlock( & lock );
459
                    free( addr );
432
                    free( addr );
460
                }
433
                }
461
                break;
434
                break;
462
            case NET_SOCKET_RECVFROM:
435
            case NET_SOCKET_RECVFROM:
463
                fibril_rwlock_read_lock( & lock );
436
//              fibril_rwlock_read_lock( & lock );
464
                fibril_rwlock_read_lock( & udp_globals.lock );
437
                fibril_rwlock_read_lock( & udp_globals.lock );
465
                res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
438
                res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen );
466
                fibril_rwlock_read_unlock( & udp_globals.lock );
439
                fibril_rwlock_read_unlock( & udp_globals.lock );
467
                fibril_rwlock_read_unlock( & lock );
440
//              fibril_rwlock_read_unlock( & lock );
468
                if( res > 0 ){
441
                if( res > 0 ){
469
                    * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
442
                    * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
470
                    * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
443
                    * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen;
471
                    answer_count = 2;
444
                    answer_count = 2;
472
                    res = EOK;
445
                    res = EOK;
473
                }
446
                }
474
                break;
447
                break;
475
            case NET_SOCKET_CLOSE:
448
            case NET_SOCKET_CLOSE:
476
                fibril_rwlock_write_lock( & lock );
449
//              fibril_rwlock_write_lock( & lock );
477
                fibril_rwlock_write_lock( & udp_globals.lock );
450
                fibril_rwlock_write_lock( & udp_globals.lock );
478
                res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
451
                res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
479
                fibril_rwlock_write_unlock( & udp_globals.lock );
452
                fibril_rwlock_write_unlock( & udp_globals.lock );
480
                fibril_rwlock_write_unlock( & lock );
453
//              fibril_rwlock_write_unlock( & lock );
481
                break;
454
                break;
482
            case NET_SOCKET_GETSOCKOPT:
455
            case NET_SOCKET_GETSOCKOPT:
483
            case NET_SOCKET_SETSOCKOPT:
456
            case NET_SOCKET_SETSOCKOPT:
484
            default:
457
            default:
485
                res = ENOTSUP;
458
                res = ENOTSUP;
Line 489... Line 462...
489
//      printf( "res = %d\n", res );
462
//      printf( "res = %d\n", res );
490
 
463
 
491
        answer_call( callid, res, & answer, answer_count );
464
        answer_call( callid, res, & answer, answer_count );
492
    }
465
    }
493
 
466
 
-
 
467
    // TODO call socket_destroy() on all bound!
494
    socket_cores_destroy( & local_sockets );
468
    socket_cores_destroy( & local_sockets );
495
 
469
 
496
    return EOK;
470
    return EOK;
497
}
471
}
498
 
472
 
499
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, const struct sockaddr * addr, socklen_t addrlen, int fragments, int flags ){
473
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 ){
500
    ERROR_DECLARE;
474
    ERROR_DECLARE;
501
 
475
 
502
    socket_core_ref         socket;
476
    socket_core_ref         socket;
503
    struct sockaddr_in *    address_in;
-
 
504
    struct sockaddr_in6 *   address_in6;
-
 
505
    packet_t                packet;
477
    packet_t                packet;
506
    packet_t                next_packet;
478
    packet_t                next_packet;
507
    udp_header_ref          header;
479
    udp_header_ref          header;
508
    int                     index;
480
    int                     index;
509
    size_t                  total_length;
481
    size_t                  total_length;
Line 512... Line 484...
512
    uint32_t                checksum;
484
    uint32_t                checksum;
513
    ip_pseudo_header_ref    ip_header;
485
    ip_pseudo_header_ref    ip_header;
514
    size_t                  headerlen;
486
    size_t                  headerlen;
515
    device_id_t             device_id;
487
    device_id_t             device_id;
516
 
488
 
517
    if( addrlen < sizeof( struct sockaddr )) return EINVAL;
-
 
518
    switch( addr->sa_family ){
-
 
519
        case AF_INET:
-
 
520
            if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
-
 
521
            address_in = ( struct sockaddr_in * ) addr;
-
 
522
            dest_port = address_in->sin_port;
-
 
523
            break;
-
 
524
        case AF_INET6:
-
 
525
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
489
    ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & dest_port ));
526
            address_in6 = ( struct sockaddr_in6 * ) addr;
-
 
527
            dest_port = address_in6->sin6_port;
-
 
528
            break;
-
 
529
        default:
-
 
530
            return EAFNOSUPPORT;
-
 
531
    }
-
 
532
 
490
 
533
    socket = socket_cores_find( local_sockets, socket_id );
491
    socket = socket_cores_find( local_sockets, socket_id );
534
    if( ! socket ) return ENOTSOCK;
492
    if( ! socket ) return ENOTSOCK;
535
 
493
 
536
    // bind the socket to a random free port if not bound
494
    // bind the socket to a random free port if not bound
Line 546... Line 504...
546
        fibril_rwlock_write_unlock( & udp_globals.lock );
504
        fibril_rwlock_write_unlock( & udp_globals.lock );
547
        fibril_rwlock_read_lock( & udp_globals.lock );
505
        fibril_rwlock_read_lock( & udp_globals.lock );
548
    }
506
    }
549
 
507
 
550
    // TODO do not ask all the time
508
    // TODO do not ask all the time
551
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, socket->device_id, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
509
    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 ));
552
 
510
 
553
    // read the first packet fragment
511
    // read the first packet fragment
554
    result = socket_read_packet_data( & packet, sizeof( udp_header_t ), addr, addrlen );
512
    result = tl_socket_read_packet_data( udp_globals.net_phone, & packet, sizeof( udp_header_t ), & udp_globals.packet_dimension, addr, addrlen );
555
    if( result < 0 ) return result;
513
    if( result < 0 ) return result;
556
    total_length = ( size_t ) result;
514
    total_length = ( size_t ) result;
557
    if( udp_globals.checksum_computing ){
515
    if( udp_globals.checksum_computing ){
558
        checksum = compute_checksum( 0, packet_get_data( packet ), packet_get_data_length( packet ));
516
        checksum = compute_checksum( 0, packet_get_data( packet ), packet_get_data_length( packet ));
559
    }else{
517
    }else{
Line 565... Line 523...
565
        return udp_release_and_return( packet, ENOMEM );
523
        return udp_release_and_return( packet, ENOMEM );
566
    }
524
    }
567
    bzero( header, sizeof( * header ));
525
    bzero( header, sizeof( * header ));
568
    // read the rest of the packet fragments
526
    // read the rest of the packet fragments
569
    for( index = 1; index < fragments; ++ index ){
527
    for( index = 1; index < fragments; ++ index ){
570
        result = socket_read_packet_data( & next_packet, 0, addr, addrlen );
528
        result = tl_socket_read_packet_data( udp_globals.net_phone, & next_packet, 0, & udp_globals.packet_dimension, addr, addrlen );
571
        if( result < 0 ){
529
        if( result < 0 ){
572
            return udp_release_and_return( packet, result );
530
            return udp_release_and_return( packet, result );
573
        }
531
        }
574
        packet = pq_add( packet, next_packet, index, 0 );
532
        packet = pq_add( packet, next_packet, index, 0 );
575
        total_length += ( size_t ) result;
533
        total_length += ( size_t ) result;
Line 597... Line 555...
597
*/      checksum = compute_checksum( checksum, ip_header, headerlen );
555
*/      checksum = compute_checksum( checksum, ip_header, headerlen );
598
        checksum = compute_checksum( checksum, ( uint8_t * ) header, sizeof( * header ));
556
        checksum = compute_checksum( checksum, ( uint8_t * ) header, sizeof( * header ));
599
        header->check = htons( flip_checksum( compact_checksum( checksum )));
557
        header->check = htons( flip_checksum( compact_checksum( checksum )));
600
        free( ip_header );
558
        free( ip_header );
601
    }else{
559
    }else{
602
        device_id = socket->device_id;
560
        device_id = -1;
603
    }
561
    }
604
    // prepare the first packet fragment
562
    // prepare the first packet fragment
605
    if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
563
    if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
606
        return udp_release_and_return( packet, ERROR_CODE );
564
        return udp_release_and_return( packet, ERROR_CODE );
607
    }
565
    }
Line 640... Line 598...
640
    }
598
    }
641
    header = ( udp_header_ref ) data;
599
    header = ( udp_header_ref ) data;
642
 
600
 
643
    // set the source address port
601
    // set the source address port
644
    result = packet_get_addr( packet, ( uint8_t ** ) & addr, NULL );
602
    result = packet_get_addr( packet, ( uint8_t ** ) & addr, NULL );
645
    if( ERROR_OCCURRED( udp_set_address_port( addr, result, ntohs( header->source )))){
603
    if( ERROR_OCCURRED( tl_set_address_port( addr, result, ntohs( header->source )))){
646
        pq_release( udp_globals.net_phone, packet_id );
604
        pq_release( udp_globals.net_phone, packet_id );
647
        return ERROR_CODE;
605
        return ERROR_CODE;
648
    }
606
    }
649
    * addrlen = ( size_t ) result;
607
    * addrlen = ( size_t ) result;
650
    // send the source address
608
    // send the source address
Line 693... Line 651...
693
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
651
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
694
    // return the total length
652
    // return the total length
695
    return ( int ) length;
653
    return ( int ) length;
696
}
654
}
697
 
655
 
698
int socket_read_packet_data( packet_ref packet, size_t prefix, const struct sockaddr * addr, socklen_t addrlen ){
-
 
699
    ERROR_DECLARE;
-
 
700
 
-
 
701
    ipc_callid_t    callid;
-
 
702
    size_t          length;
-
 
703
    void *          data;
-
 
704
 
-
 
705
    // get the data length
-
 
706
    if( ! ipc_data_write_receive( & callid, & length )) return EINVAL;
-
 
707
    // get a new packet
-
 
708
    * packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, prefix + udp_globals.prefix, udp_globals.suffix );
-
 
709
    if( ! packet ) return ENOMEM;
-
 
710
    // allocate space in the packet
-
 
711
    data = packet_suffix( * packet, length );
-
 
712
    if( ! data ){
-
 
713
        return udp_release_and_return( * packet, ENOMEM );
-
 
714
    }
-
 
715
    // read the data into the packet
-
 
716
    if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length ))
-
 
717
    // set the packet destination address
-
 
718
    || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){
-
 
719
        return udp_release_and_return( * packet, ERROR_CODE );
-
 
720
    }
-
 
721
    return ( int ) length;
-
 
722
}
-
 
723
 
-
 
724
int udp_release_and_return( packet_t packet, int result ){
656
int udp_release_and_return( packet_t packet, int result ){
725
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
657
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
726
    return result;
658
    return result;
727
}
659
}
728
 
660
 
729
void udp_send_icmp_port_unreachable( packet_t packet, services_t error ){
-
 
730
    packet_t    next;
-
 
731
    uint8_t *   src;
-
 
732
    int         length;
-
 
733
 
-
 
734
    // detach the first packet and release the others
-
 
735
    next = pq_detach( packet );
-
 
736
    if( next ){
-
 
737
        pq_release( udp_globals.net_phone, packet_get_id( next ));
-
 
738
    }
-
 
739
    length = packet_get_addr( packet, & src, NULL );
-
 
740
    if(( length > 0 )
-
 
741
    && ( ! error )
-
 
742
    && ( udp_globals.icmp_phone >= 0 )
-
 
743
    // set both addresses to the source one (avoids the source address deletion before setting the destination one)
-
 
744
    && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){
-
 
745
        icmp_destination_unreachable_msg( udp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet );
-
 
746
    }else{
-
 
747
        udp_release_and_return( packet, EINVAL );
-
 
748
    }
-
 
749
}
-
 
750
 
-
 
751
int udp_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){
-
 
752
    struct sockaddr_in *    address_in;
-
 
753
    struct sockaddr_in6 *   address_in6;
-
 
754
    size_t                  length;
-
 
755
 
-
 
756
    if( addrlen < 0 ) return EINVAL;
-
 
757
    length = ( size_t ) addrlen;
-
 
758
    if( length < sizeof( struct sockaddr )) return EINVAL;
-
 
759
    switch( addr->sa_family ){
-
 
760
        case AF_INET:
-
 
761
            if( length != sizeof( struct sockaddr_in )) return EINVAL;
-
 
762
            address_in = ( struct sockaddr_in * ) addr;
-
 
763
            address_in->sin_port = port;
-
 
764
            return EOK;
-
 
765
        case AF_INET6:
-
 
766
            if( length != sizeof( struct sockaddr_in6 )) return EINVAL;
-
 
767
            address_in6 = ( struct sockaddr_in6 * ) addr;
-
 
768
            address_in6->sin6_port = port;
-
 
769
            return EOK;
-
 
770
        default:
-
 
771
            return EAFNOSUPPORT;
-
 
772
    }
-
 
773
}
-
 
774
 
-
 
775
/** @}
661
/** @}
776
 */
662
 */