Subversion Repositories HelenOS

Rev

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

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