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