Subversion Repositories HelenOS

Rev

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

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