Subversion Repositories HelenOS

Rev

Rev 4707 | Rev 4713 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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