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