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