Subversion Repositories HelenOS

Rev

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

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