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