Subversion Repositories HelenOS

Rev

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

Rev 4579 Rev 4589
Line 41... Line 41...
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
 
42
 
43
#include "../../err.h"
43
#include "../../err.h"
44
#include "../../messages.h"
44
#include "../../messages.h"
45
#include "../../modules.h"
45
#include "../../modules.h"
-
 
46
 
-
 
47
#include "../../structures/dynamic_fifo.h"
46
#include "../../structures/packet/packet_client.h"
48
#include "../../structures/packet/packet_client.h"
47
 
49
 
48
#include "../../include/in.h"
50
#include "../../include/in.h"
49
#include "../../include/inet.h"
51
#include "../../include/inet.h"
50
#include "../../include/ip_client.h"
52
#include "../../include/ip_client.h"
51
#include "../../include/ip_interface.h"
53
#include "../../include/ip_interface.h"
52
#include "../../include/ip_protocols.h"
54
#include "../../include/ip_protocols.h"
53
#include "../../include/socket.h"
55
#include "../../include/socket.h"
54
#include "../../include/socket_errno.h"
56
#include "../../include/socket_errno.h"
55
//#include "../../include/udp_interface.h"
-
 
56
 
57
 
57
#include "../../socket/socket_core.h"
58
#include "../../socket/socket_core.h"
58
#include "../../socket/socket_messages.h"
59
#include "../../socket/socket_messages.h"
59
 
60
 
60
#include "../tl_messages.h"
61
#include "../tl_messages.h"
61
 
62
 
62
#include "udp.h"
63
#include "udp.h"
63
#include "udp_header.h"
64
#include "udp_header.h"
64
#include "udp_module.h"
65
#include "udp_module.h"
65
 
66
 
-
 
67
#define MAX_UDP_FRAGMENT_SIZE   65535
-
 
68
 
66
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
69
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver );
-
 
70
int udp_process_packet( packet_t packet );
67
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
71
int process_client_messages( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count );
68
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, void * data, size_t length, int flags );
72
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags );
-
 
73
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags );
69
int socket_get_data( void ** data, size_t * length );
74
int socket_read_data( void ** data, size_t * length );
-
 
75
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in );
-
 
76
int socket_write_data( void * data, size_t data_length );
70
 
77
 
71
udp_globals_t   udp_globals;
78
udp_globals_t   udp_globals;
72
 
79
 
73
/** Initializes the module.
80
/** Initializes the module.
74
 */
81
 */
75
int udp_initialize( async_client_conn_t client_connection ){
82
int udp_initialize( async_client_conn_t client_connection ){
76
    ERROR_DECLARE;
83
    ERROR_DECLARE;
77
 
84
 
78
    udp_globals.port_search_start = 1025;
-
 
79
    udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
85
    udp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_UDP, SERVICE_UDP, client_connection, udp_received_msg );
80
    if( udp_globals.ip_phone < 0 ){
86
    if( udp_globals.ip_phone < 0 ){
81
        return udp_globals.ip_phone;
87
        return udp_globals.ip_phone;
82
    }
88
    }
83
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
89
    ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
Line 86... Line 92...
86
    udp_globals.content -= sizeof( udp_header_t );
92
    udp_globals.content -= sizeof( udp_header_t );
87
    return EOK;
93
    return EOK;
88
}
94
}
89
 
95
 
90
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver ){
96
int udp_received_msg( device_id_t device_id, packet_t packet, services_t receiver ){
-
 
97
    ERROR_DECLARE;
-
 
98
 
-
 
99
    if( ERROR_OCCURRED( udp_process_packet( packet ))){
-
 
100
        pq_release( udp_globals.net_phone, packet_get_id( packet ));
-
 
101
        return ERROR_CODE;
-
 
102
    }
-
 
103
 
-
 
104
    return EOK;
-
 
105
}
-
 
106
 
-
 
107
int udp_process_packet( packet_t packet ){
-
 
108
    ERROR_DECLARE;
-
 
109
 
-
 
110
    uint8_t *       src;
-
 
111
    uint8_t *       dest;
-
 
112
    int             length;
-
 
113
    void *          data;
-
 
114
    udp_header_ref  header;
-
 
115
    socket_core_ref *   socket;
-
 
116
    packet_t        next_packet;
-
 
117
    int             total_length;
-
 
118
//  uint16_t        checksum;
-
 
119
    int             fragments;
-
 
120
    packet_t        tmp_packet;
-
 
121
 
-
 
122
    // get packet data
-
 
123
    length = packet_get_addr( packet, & src, & dest );
-
 
124
    if( length < 0 ) return length;
-
 
125
    if( length != sizeof( in_addr_t )) return EINVAL;
91
    // TODO received
126
    // TODO received ipopts?
-
 
127
    ERROR_PROPAGATE( ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ));
-
 
128
 
92
    //  TODO remove debug dump:
129
    //  TODO remove debug dump:
93
    uint8_t *   data;
130
/*  uint8_t *   rdata;
-
 
131
    rdata = packet_get_data( packet );
-
 
132
    printf( "Receiving udp packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id( packet ), packet_get_data_length( packet ), rdata[ 0 ], rdata[ 1 ], rdata[ 2 ], rdata[ 3 ], rdata[ 4 ], rdata[ 5 ], rdata[ 6 ], rdata[ 7 ], rdata[ 8 ], rdata[ 9 ], rdata[ 10 ], rdata[ 11 ], rdata[ 12 ], rdata[ 13 ], rdata[ 14 ], rdata[ 15 ], rdata[ 16 ], rdata[ 17 ], rdata[ 18 ], rdata[ 19 ], rdata[ 20 ], rdata[ 21 ], rdata[ 22 ], rdata[ 23 ], rdata[ 24 ], rdata[ 25 ], rdata[ 26 ], rdata[ 27 ], rdata[ 28 ], rdata[ 29 ], rdata[ 30 ], rdata[ 31 ], rdata[ 32 ], rdata[ 33 ], rdata[ 34 ], rdata[ 35 ], rdata[ 36 ], rdata[ 37 ], rdata[ 38 ], rdata[ 39 ], rdata[ 40 ], rdata[ 41 ], rdata[ 42 ], rdata[ 43 ], rdata[ 44 ], rdata[ 45 ], rdata[ 46 ], rdata[ 47 ], rdata[ 48 ], rdata[ 49 ], rdata[ 50 ], rdata[ 51 ], rdata[ 52 ], rdata[ 53 ], rdata[ 54 ], rdata[ 55 ], rdata[ 56 ], rdata[ 57 ], rdata[ 58 ], rdata[ 59 ] );
-
 
133
*/
-
 
134
    length = packet_get_data_length( packet );
-
 
135
    if( length < 0 ) return length;
-
 
136
    if( length < sizeof( udp_header_t )) return NO_DATA;
94
    data = packet_get_data( packet );
137
    data = packet_get_data( packet );
-
 
138
    if( ! data ) return NO_DATA;
-
 
139
    // get udp header
-
 
140
    header = ( udp_header_ref ) data;
-
 
141
    // find the destination socket
-
 
142
    socket = socket_ports_find( & udp_globals.sockets, ntohs( header->dest ));
-
 
143
    if( ! socket ) return EADDRNOTAVAIL;
-
 
144
    // count the received packet fragments
-
 
145
    next_packet = packet;
-
 
146
    fragments = 0;
-
 
147
    total_length = ntohs( header->len );
-
 
148
    do{
-
 
149
        ++ fragments;
95
    printf( "Receiving packet:\n\tid\t= %d\n\tlength\t= %d\n\tdata\t= %.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX\n\t\t%.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX:%.2hhX %.2hhX %.2hhX %.2hhX\n", packet_get_id( packet ), packet_get_data_length( packet ), data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ], data[ 16 ], data[ 17 ], data[ 18 ], data[ 19 ], data[ 20 ], data[ 21 ], data[ 22 ], data[ 23 ], data[ 24 ], data[ 25 ], data[ 26 ], data[ 27 ], data[ 28 ], data[ 29 ], data[ 30 ], data[ 31 ], data[ 32 ], data[ 33 ], data[ 34 ], data[ 35 ], data[ 36 ], data[ 37 ], data[ 38 ], data[ 39 ], data[ 40 ], data[ 41 ], data[ 42 ], data[ 43 ], data[ 44 ], data[ 45 ], data[ 46 ], data[ 47 ], data[ 48 ], data[ 49 ], data[ 50 ], data[ 51 ], data[ 52 ], data[ 53 ], data[ 54 ], data[ 55 ], data[ 56 ], data[ 57 ], data[ 58 ], data[ 59 ] );
150
        length = packet_get_data_length( packet );
-
 
151
        if( length < 0 ) return length;
-
 
152
        if( ! length ) return NO_DATA;
-
 
153
        if( total_length < length ){
-
 
154
            // cut of the suffix if too long
-
 
155
            ERROR_PROPAGATE( packet_trim( next_packet, 0, length - total_length ));
-
 
156
            // relese the rest of the packet fragments
-
 
157
            tmp_packet = pq_next( next_packet );
-
 
158
            while( tmp_packet ){
-
 
159
                next_packet = pq_detach( tmp_packet );
96
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
160
                pq_release( udp_globals.net_phone, packet_get_id( tmp_packet ));
-
 
161
                tmp_packet = next_packet;
-
 
162
            }
-
 
163
            break;
-
 
164
        }
-
 
165
        total_length -= length;
-
 
166
    /*  if( header->header_checksum ){
-
 
167
        }
-
 
168
    */
-
 
169
    }while(( next_packet = pq_next( next_packet )) && ( total_length > 0 ));
-
 
170
    // queue the received packet
-
 
171
    ERROR_PROPAGATE( dyn_fifo_push( &( ** socket ).received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ));
97
 
172
 
-
 
173
    // notify the destination socket
-
 
174
    async_msg_2(( ** socket ).phone, NET_SOCKET_RECEIVED, ( ** socket ).socket_id, fragments );
98
    return EOK;
175
    return EOK;
99
}
176
}
100
 
177
 
101
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
178
int udp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
102
    ERROR_DECLARE;
179
    ERROR_DECLARE;
Line 121... Line 198...
121
    bool                    keep_on_going = true;
198
    bool                    keep_on_going = true;
122
    socket_cores_t          local_sockets;
199
    socket_cores_t          local_sockets;
123
    int                     app_phone = IPC_GET_PHONE( call );
200
    int                     app_phone = IPC_GET_PHONE( call );
124
    void *                  addr;
201
    void *                  addr;
125
    size_t                  addrlen;
202
    size_t                  addrlen;
126
    void *                  data;
-
 
127
    size_t                  length;
-
 
128
 
203
 
129
    /*
204
    /*
130
     * Accept the connection
205
     * Accept the connection
131
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
206
     *  - Answer the first IPC_M_CONNECT_ME_TO call.
132
     */
207
     */
Line 145... Line 220...
145
        IPC_SET_ARG3( * answer, 0 );
220
        IPC_SET_ARG3( * answer, 0 );
146
        IPC_SET_ARG4( * answer, 0 );
221
        IPC_SET_ARG4( * answer, 0 );
147
        IPC_SET_ARG5( * answer, 0 );
222
        IPC_SET_ARG5( * answer, 0 );
148
 
223
 
149
        callid = async_get_call( call );
224
        callid = async_get_call( call );
150
        printf( "message %d\n", IPC_GET_METHOD( * call ));
225
//      printf( "message %d\n", IPC_GET_METHOD( * call ));
151
 
226
 
152
        switch( IPC_GET_METHOD( * call )){
227
        switch( IPC_GET_METHOD( * call )){
153
            case IPC_M_PHONE_HUNGUP:
228
            case IPC_M_PHONE_HUNGUP:
154
                keep_on_going = false;
229
                keep_on_going = false;
155
                res = EOK;
230
                res = EOK;
156
                break;
231
                break;
157
            case NET_SOCKET:
232
            case NET_SOCKET:
158
                res = socket_create( & local_sockets, app_phone );
233
                res = socket_create( & local_sockets, app_phone, SOCKET_SET_SOCKET_ID( answer ));
-
 
234
                * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( udp_header_t );
-
 
235
                * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_UDP_FRAGMENT_SIZE;
-
 
236
                * answer_count = 3;
159
                break;
237
                break;
160
            case NET_SOCKET_BIND:
238
            case NET_SOCKET_BIND:
161
                if( ERROR_OCCURRED( socket_get_data( & addr, & addrlen ))){
239
                if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
162
                    res = ERROR_CODE;
240
                    res = ERROR_CODE;
163
                    break;
241
                    break;
164
                }
242
                }
165
                res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
243
                res = socket_bind( & local_sockets, & udp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen );
166
                free( addr );
244
                free( addr );
167
                break;
245
                break;
168
            case NET_SOCKET_SENDTO:
246
            case NET_SOCKET_SENDTO:
169
                if( ERROR_OCCURRED( socket_get_data( & addr, & addrlen ))){
247
                if( ERROR_OCCURRED( socket_read_data( & addr, & addrlen ))){
170
                    res = ERROR_CODE;
248
                    res = ERROR_CODE;
171
                    break;
249
                    break;
172
                }
250
                }
173
                if( ERROR_OCCURRED( socket_get_data( & data, & length ))){
-
 
174
                    free( addr );
-
 
175
                    res = ERROR_CODE;
-
 
176
                    break;
-
 
177
                }
-
 
178
                res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, data, length, SOCKET_GET_FLAGS( call ));
251
                res = udp_sendto_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_FLAGS( call ));
179
                free( addr );
252
                free( addr );
180
                free( data );
-
 
181
                break;
253
                break;
182
            case NET_SOCKET_RECVFROM:
254
            case NET_SOCKET_RECVFROM:
-
 
255
                res = udp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ));
-
 
256
                if( res > 0 ){
183
                // TODO read first received packet queue data continuesly
257
                    * SOCKET_SET_READ_DATA_LENGTH( answer ) = res;
-
 
258
                    * answer_count = 1;
184
                res = ENOTSUP;
259
                    res = EOK;
-
 
260
                }
185
                break;
261
                break;
186
            case NET_SOCKET_CLOSE:
262
            case NET_SOCKET_CLOSE:
187
                res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
263
                res = socket_destroy( udp_globals.net_phone, SOCKET_GET_SOCKET_ID( call ), & local_sockets, & udp_globals.sockets );
188
                break;
264
                break;
189
            case NET_SOCKET_GETSOCKOPT:
265
            case NET_SOCKET_GETSOCKOPT:
Line 191... Line 267...
191
            default:
267
            default:
192
                res = ENOTSUP;
268
                res = ENOTSUP;
193
                break;
269
                break;
194
        }
270
        }
195
 
271
 
196
        // TODO debug
-
 
197
        printf( "res = %d\n", res );
272
//      printf( "res = %d\n", res );
198
 
273
 
199
        switch( * answer_count ){
274
        switch( * answer_count ){
200
            case 0:     ipc_answer_0( callid, res );
275
            case 0:     ipc_answer_0( callid, res );
201
                    continue;
276
                    continue;
202
            case 1:     ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
277
            case 1:     ipc_answer_1( callid, res, IPC_GET_ARG1( * answer ));
Line 215... Line 290...
215
    socket_cores_destroy( & local_sockets );
290
    socket_cores_destroy( & local_sockets );
216
 
291
 
217
    return EOK;
292
    return EOK;
218
}
293
}
219
 
294
 
220
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, void * data, size_t length, int flags ){
295
int udp_sendto_message( socket_cores_ref local_sockets, int socket_id, void * addr, size_t addrlen, int fragments, int flags ){
221
    ERROR_DECLARE;
296
    ERROR_DECLARE;
222
 
297
 
223
    socket_core_ref         socket;
298
    socket_core_ref         socket;
224
    struct sockaddr *       address;
299
    struct sockaddr *       address;
225
    struct sockaddr_in *    address_in;
300
    struct sockaddr_in *    address_in;
226
    packet_t                packet;
301
    packet_t                packet;
-
 
302
    packet_t                next_packet;
227
    udp_header_ref          header;
303
    udp_header_ref          header;
-
 
304
    int                     index;
-
 
305
    int                     total_length;
-
 
306
    int                     length;
228
 
307
 
229
    if( addrlen < sizeof( struct sockaddr )) return EINVAL;
308
    if( addrlen < sizeof( struct sockaddr )) return EINVAL;
230
    address = ( struct sockaddr * ) addr;
309
    address = ( struct sockaddr * ) addr;
231
    switch( address->sa_family ){
310
    switch( address->sa_family ){
232
        case AF_INET:
311
        case AF_INET:
233
            if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
312
            if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
234
            address_in = ( struct sockaddr_in * ) addr;
313
            address_in = ( struct sockaddr_in * ) addr;
235
            socket = socket_cores_find( local_sockets, socket_id );
314
            socket = socket_cores_find( local_sockets, socket_id );
236
            if( ! socket ) return ENOTSOCK;
315
            if( ! socket ) return ENOTSOCK;
237
/*          if( socket->port < 0 ){
-
 
238
                return ENOTCONN;
-
 
239
                socket->port = udp_globals.port_search_start;
-
 
240
                while( socket_ports_find( global_sockets, socket->port ) >= 0 ){
-
 
241
                    socket_port = ( socket->port + 1 ) % ( 65535 - 1025 ) + 1025;
-
 
242
                }
-
 
243
                ERROR_PROPAGATE( socket_ports_add( global_sockets, socket->port );
-
 
244
                udp_globals.port_search_start = socket->port + 1;
-
 
245
            }
316
 
246
*/          // TODO create and send fragmented packets
-
 
247
            // TODO do not ask all the time
317
            // TODO do not ask all the time
248
            ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
318
            ERROR_PROPAGATE( ip_packet_size_req( udp_globals.ip_phone, -1, & udp_globals.addr_len, & udp_globals.prefix, & udp_globals.content, & udp_globals.suffix ));
249
            packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, udp_globals.prefix, udp_globals.suffix );
-
 
-
 
319
 
250
            if( ! packet ) return ENOMEM;
320
            // read the first packet fragment
251
            if( ERROR_OCCURRED( packet_copy_data( packet, data, length ))
321
            total_length = socket_read_packet_data( & packet, sizeof( udp_header_t ), address_in );
252
            || ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
-
 
253
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
322
            if( total_length < 0 ) return total_length;
254
                return ERROR_CODE;
323
            // prefix the udp header
255
            }
-
 
256
            header = PACKET_PREFIX( packet, udp_header_t );
324
            header = PACKET_PREFIX( packet, udp_header_t );
257
            if( ! header ){
325
            if( ! header ){
258
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
326
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
259
                return ENOMEM;
327
                return ENOMEM;
260
            }
328
            }
-
 
329
            // read the rest of the packet fragments
-
 
330
            for( index = 1; index < fragments; ++ index ){
-
 
331
                length = socket_read_packet_data( & next_packet, 0, address_in );
-
 
332
                if( length < 0 ){
-
 
333
                    pq_release( udp_globals.net_phone, packet_get_id( packet ));
-
 
334
                    return length;
-
 
335
                }
-
 
336
                packet = pq_add( packet, next_packet, index, 0 );
-
 
337
                total_length += length;
-
 
338
            }
-
 
339
            // set the udp header
261
            header->source = ( socket->port < 0 ) ? 0 : htons( socket->port );
340
            header->source = ( socket->port < 0 ) ? 0 : htons( socket->port );
262
            header->dest = htons( address_in->sin_port );
341
            header->dest = htons( address_in->sin_port );
263
            header->len = htons( length );
342
            header->len = htons( total_length + sizeof( udp_header_t ));
264
            // TODO my ip address for the pseudo header checksum
343
            // TODO my ip address for the pseudo header checksum
265
            header->check = 0;
344
            header->check = 0;
-
 
345
            // prepare the first packet fragment
266
            if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
346
            if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_UDP, 0, 0, 0, 0 ))){
267
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
347
                pq_release( udp_globals.net_phone, packet_get_id( packet ));
268
                return ERROR_CODE;
348
                return ERROR_CODE;
269
            }
349
            }
-
 
350
            // send the packet
270
            return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP );
351
            return ip_send_msg( udp_globals.ip_phone, socket->device_id, packet, SERVICE_UDP );
271
            break;
-
 
272
        // TODO IPv6
352
        // TODO IPv6
273
        default:
353
        default:
274
            return EAFNOSUPPORT;
354
            return EAFNOSUPPORT;
275
    }
355
    }
276
    return EOK;
356
    return EOK;
277
}
357
}
278
 
358
 
-
 
359
int udp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags ){
-
 
360
    ERROR_DECLARE;
-
 
361
 
-
 
362
    socket_core_ref socket;
-
 
363
    int             packet_id;
-
 
364
    packet_t        packet;
-
 
365
    udp_header_ref  header;
-
 
366
    struct sockaddr_in      address;
-
 
367
    int             length;
-
 
368
    packet_t        next_packet;
-
 
369
    void *          data;
-
 
370
    int             fragments;
-
 
371
    int *           lengths;
-
 
372
    int             index;
-
 
373
    uint8_t *       addr;
-
 
374
 
-
 
375
    // find the socket
-
 
376
    socket = socket_cores_find( local_sockets, socket_id );
-
 
377
    if( ! socket ) return ENOTSOCK;
-
 
378
    // get the next received packet
-
 
379
    packet_id = dyn_fifo_value( & socket->received );
-
 
380
    if( packet_id < 0 ) return packet_id;
-
 
381
    ERROR_PROPAGATE( packet_translate( udp_globals.net_phone, & packet, packet_id ));
-
 
382
    // get udp header
-
 
383
    data = packet_get_data( packet );
-
 
384
    if( ! data ){
-
 
385
        pq_release( udp_globals.net_phone, packet_id );
-
 
386
        return NO_DATA;
-
 
387
    }
-
 
388
    header = ( udp_header_ref ) data;
-
 
389
    // set the source address
-
 
390
    address.sin_family = PF_INET;
-
 
391
    address.sin_port = ntohs( header->dest );
-
 
392
    length = packet_get_addr( packet, & addr, NULL );
-
 
393
    if( length != sizeof( address.sin_addr.s_addr )){
-
 
394
        pq_release( udp_globals.net_phone, packet_id );
-
 
395
        return EINVAL;
-
 
396
    }
-
 
397
    address.sin_addr.s_addr = *(( uint32_t * ) addr );
-
 
398
    bzero( & address.sin_zero, sizeof( address.sin_zero ));
-
 
399
    // send the source address
-
 
400
    ERROR_PROPAGATE( socket_write_data( & address, sizeof( address )));
-
 
401
    next_packet = pq_next( packet );
-
 
402
    if( ! next_packet ){
-
 
403
        // write all if only one fragment
-
 
404
        ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), packet_get_data_length( packet ) - sizeof( udp_header_t )));
-
 
405
        // store the total length
-
 
406
        length = packet_get_data_length( packet ) - sizeof( udp_header_t );
-
 
407
    }else{
-
 
408
        // count the packet fragments
-
 
409
        fragments = 1;
-
 
410
        next_packet = pq_next( packet );
-
 
411
        while(( next_packet = pq_next( next_packet ))){
-
 
412
            ++ fragments;
-
 
413
        }
-
 
414
        // compute and store the fragment lengths
-
 
415
        lengths = ( int * ) malloc( sizeof( int ) * ( fragments + 1 ));
-
 
416
        if( ! lengths ) return ENOMEM;
-
 
417
        lengths[ 0 ] = packet_get_data_length( packet ) - sizeof( udp_header_t );
-
 
418
        lengths[ fragments ] = lengths[ 0 ];
-
 
419
        next_packet = pq_next( packet );
-
 
420
        for( index = 1; index < fragments; ++ index ){
-
 
421
            lengths[ index ] = packet_get_data_length( next_packet );
-
 
422
            lengths[ fragments ] += lengths[ index ];
-
 
423
            next_packet = pq_next( packet );
-
 
424
        }while( next_packet );
-
 
425
        // write the fragment lengths
-
 
426
        ERROR_PROPAGATE( socket_write_data( lengths, sizeof( int ) * ( fragments + 1 )));
-
 
427
        // write the first fragment
-
 
428
        ERROR_PROPAGATE( socket_write_data( data + sizeof( udp_header_t ), lengths[ 0 ] ));
-
 
429
        next_packet = pq_next( packet );
-
 
430
        // write the rest of the fragments
-
 
431
        for( index = 1; index < fragments; ++ index ){
-
 
432
            ERROR_PROPAGATE( socket_write_data( packet_get_data( next_packet ), lengths[ index ] ));
-
 
433
            next_packet = pq_next( packet );
-
 
434
        }while( next_packet );
-
 
435
        // store the total length
-
 
436
        length = lengths[ fragments ];
-
 
437
        free( lengths );
-
 
438
    }
-
 
439
    // release the packet
-
 
440
    dyn_fifo_pop( & socket->received );
-
 
441
    pq_release( udp_globals.net_phone, packet_get_id( packet ));
-
 
442
    // return the total length
-
 
443
    return length;
-
 
444
}
-
 
445
 
-
 
446
int socket_write_data( void * data, size_t data_length ){
-
 
447
    size_t          length;
-
 
448
    ipc_callid_t    callid;
-
 
449
 
-
 
450
    if(( ! ipc_data_read_receive( & callid, & length ))
-
 
451
    || ( length < data_length )){
-
 
452
        return EINVAL;
-
 
453
    }
-
 
454
    return ipc_data_read_finalize( callid, data, data_length );
-
 
455
}
-
 
456
 
279
int socket_get_data( void ** data, size_t * length ){
457
int socket_read_data( void ** data, size_t * length ){
280
    ERROR_DECLARE;
458
    ERROR_DECLARE;
281
 
459
 
282
    ipc_callid_t    callid;
460
    ipc_callid_t    callid;
283
 
461
 
284
    if( !( data && length )) return EBADMEM;
462
    if( !( data && length )) return EBADMEM;
Line 290... Line 468...
290
        return ERROR_CODE;
468
        return ERROR_CODE;
291
    }
469
    }
292
    return EOK;
470
    return EOK;
293
}
471
}
294
 
472
 
-
 
473
int socket_read_packet_data( packet_ref packet, size_t prefix, struct sockaddr_in * address_in ){
-
 
474
    ERROR_DECLARE;
-
 
475
 
-
 
476
    ipc_callid_t    callid;
-
 
477
    size_t          length;
-
 
478
    void *          data;
-
 
479
 
-
 
480
    // get the data length
-
 
481
    if( ! ipc_data_write_receive( & callid, & length )) return EINVAL;
-
 
482
    // get a new packet
-
 
483
    * packet = packet_get_4( udp_globals.net_phone, length, udp_globals.addr_len, prefix + udp_globals.prefix, udp_globals.suffix );
-
 
484
    if( ! packet ) return ENOMEM;
-
 
485
    // allocate space in the packet
-
 
486
    data = packet_suffix( * packet, length );
-
 
487
    if( ! data ){
-
 
488
        pq_release( udp_globals.net_phone, packet_get_id( * packet ));
-
 
489
        return ENOMEM;
-
 
490
    }
-
 
491
    // read the data into the packet
-
 
492
    if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length ))
-
 
493
    // set the packet destination address
-
 
494
    || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) & address_in->sin_addr.s_addr, sizeof( address_in->sin_addr.s_addr )))){
-
 
495
        pq_release( udp_globals.net_phone, packet_get_id( * packet ));
-
 
496
        return ERROR_CODE;
-
 
497
    }
-
 
498
    return length;
-
 
499
}
-
 
500
 
295
/** @}
501
/** @}
296
 */
502
 */