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