Rev 4750 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4750 | Rev 4756 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
2 | * Copyright (c) 2008 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup tcp |
29 | /** @addtogroup tcp |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * TCP module implementation. |
34 | * TCP module implementation. |
35 | * @see tcp.h |
35 | * @see tcp.h |
36 | * \todo |
- | |
37 | */ |
36 | */ |
38 | 37 | ||
39 | #include <assert.h> |
38 | #include <assert.h> |
40 | #include <async.h> |
39 | #include <async.h> |
41 | #include <fibril_sync.h> |
40 | #include <fibril_sync.h> |
42 | #include <malloc.h> |
41 | #include <malloc.h> |
43 | //TODO remove stdio |
42 | //TODO remove stdio |
44 | #include <stdio.h> |
43 | #include <stdio.h> |
45 | 44 | ||
46 | #include <ipc/ipc.h> |
45 | #include <ipc/ipc.h> |
47 | #include <ipc/services.h> |
46 | #include <ipc/services.h> |
48 | 47 | ||
49 | #include "../../err.h" |
48 | #include "../../err.h" |
50 | #include "../../messages.h" |
49 | #include "../../messages.h" |
51 | #include "../../modules.h" |
50 | #include "../../modules.h" |
52 | 51 | ||
53 | #include "../../structures/dynamic_fifo.h" |
52 | #include "../../structures/dynamic_fifo.h" |
54 | #include "../../structures/packet/packet_client.h" |
53 | #include "../../structures/packet/packet_client.h" |
55 | 54 | ||
56 | #include "../../include/checksum.h" |
55 | #include "../../include/checksum.h" |
57 | #include "../../include/in.h" |
56 | #include "../../include/in.h" |
58 | #include "../../include/in6.h" |
57 | #include "../../include/in6.h" |
59 | #include "../../include/inet.h" |
58 | #include "../../include/inet.h" |
60 | #include "../../include/ip_client.h" |
59 | #include "../../include/ip_client.h" |
61 | #include "../../include/ip_interface.h" |
60 | #include "../../include/ip_interface.h" |
62 | #include "../../include/ip_protocols.h" |
61 | #include "../../include/ip_protocols.h" |
63 | #include "../../include/icmp_client.h" |
62 | #include "../../include/icmp_client.h" |
64 | #include "../../include/icmp_interface.h" |
63 | #include "../../include/icmp_interface.h" |
65 | #include "../../include/net_interface.h" |
64 | #include "../../include/net_interface.h" |
66 | #include "../../include/socket_codes.h" |
65 | #include "../../include/socket_codes.h" |
67 | #include "../../include/socket_errno.h" |
66 | #include "../../include/socket_errno.h" |
68 | #include "../../include/tcp_codes.h" |
67 | #include "../../include/tcp_codes.h" |
69 | 68 | ||
70 | #include "../../socket/socket_core.h" |
69 | #include "../../socket/socket_core.h" |
71 | #include "../../socket/socket_messages.h" |
70 | #include "../../socket/socket_messages.h" |
72 | 71 | ||
73 | #include "../tl_common.h" |
72 | #include "../tl_common.h" |
74 | #include "../tl_messages.h" |
73 | #include "../tl_messages.h" |
75 | 74 | ||
76 | #include "tcp.h" |
75 | #include "tcp.h" |
77 | #include "tcp_header.h" |
76 | #include "tcp_header.h" |
78 | #include "tcp_module.h" |
77 | #include "tcp_module.h" |
79 | 78 | ||
80 | /** The TCP window default value. |
79 | /** The TCP window default value. |
81 | */ |
80 | */ |
82 | #define NET_DEFAULT_TCP_WINDOW 10240 |
81 | #define NET_DEFAULT_TCP_WINDOW 10240 |
83 | 82 | ||
84 | /** \todo |
83 | /** Initial timeout for new connections. |
85 | */ |
84 | */ |
86 | #define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L |
85 | #define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L |
87 | 86 | ||
88 | /** \todo |
87 | /** Default timeout for closing. |
89 | */ |
88 | */ |
90 | #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L |
89 | #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L |
91 | 90 | ||
92 | /** The initial outgoing sequence number. |
91 | /** The initial outgoing sequence number. |
93 | */ |
92 | */ |
94 | #define TCP_INITIAL_SEQUENCE_NUMBER 2999 |
93 | #define TCP_INITIAL_SEQUENCE_NUMBER 2999 |
95 | 94 | ||
96 | /** Maximum TCP fragment size. |
95 | /** Maximum TCP fragment size. |
97 | */ |
96 | */ |
98 | #define MAX_TCP_FRAGMENT_SIZE 65535 |
97 | #define MAX_TCP_FRAGMENT_SIZE 65535 |
99 | 98 | ||
100 | /** Free ports pool start. |
99 | /** Free ports pool start. |
101 | */ |
100 | */ |
102 | #define TCP_FREE_PORTS_START 1025 |
101 | #define TCP_FREE_PORTS_START 1025 |
103 | 102 | ||
104 | /** Free ports pool end. |
103 | /** Free ports pool end. |
105 | */ |
104 | */ |
106 | #define TCP_FREE_PORTS_END 65535 |
105 | #define TCP_FREE_PORTS_END 65535 |
107 | 106 | ||
108 | /** \todo |
107 | /** Timeout for connection initialization, SYN sent. |
109 | */ |
108 | */ |
110 | #define TCP_SYN_SENT_TIMEOUT 1000000L |
109 | #define TCP_SYN_SENT_TIMEOUT 1000000L |
111 | 110 | ||
112 | /** The maximum number of timeouts in a row before singaling connection lost. |
111 | /** The maximum number of timeouts in a row before singaling connection lost. |
113 | */ |
112 | */ |
114 | #define TCP_MAX_TIMEOUTS 8 |
113 | #define TCP_MAX_TIMEOUTS 8 |
115 | 114 | ||
116 | /** The number of acknowledgements before retransmit. |
115 | /** The number of acknowledgements before retransmit. |
117 | */ |
116 | */ |
118 | #define TCP_FAST_RETRANSMIT_COUNT 3 |
117 | #define TCP_FAST_RETRANSMIT_COUNT 3 |
119 | 118 | ||
- | 119 | /** Returns a value indicating whether the value is in the interval respecting the possible overflow. |
|
- | 120 | * The high end and/or the value may overflow, be lower than the low value. |
|
- | 121 | * @param[in] lower The last value before the interval. |
|
120 | /** \todo |
122 | * @param[in] value The value to be checked. |
- | 123 | * @param[in] high_equal The last value in the interval. |
|
121 | */ |
124 | */ |
122 | #define IS_IN_INTERVAL_OVERFLOW( lower, value, higher_equal ) (((( lower ) < ( value )) && ((( value ) <= ( higher_equal )) || (( higher_equal ) < ( lower )))) || ((( value ) <= ( higher_equal )) && (( higher_equal ) < ( lower )))) |
125 | #define IS_IN_INTERVAL_OVERFLOW( lower, value, higher_equal ) (((( lower ) < ( value )) && ((( value ) <= ( higher_equal )) || (( higher_equal ) < ( lower )))) || ((( value ) <= ( higher_equal )) && (( higher_equal ) < ( lower )))) |
123 | 126 | ||
124 | /** Type definition of the TCP timeout. |
127 | /** Type definition of the TCP timeout. |
125 | * @see tcp_timeout |
128 | * @see tcp_timeout |
126 | */ |
129 | */ |
127 | typedef struct tcp_timeout tcp_timeout_t; |
130 | typedef struct tcp_timeout tcp_timeout_t; |
128 | 131 | ||
129 | /** Type definition of the TCP timeout pointer. |
132 | /** Type definition of the TCP timeout pointer. |
130 | * @see tcp_timeout |
133 | * @see tcp_timeout |
131 | */ |
134 | */ |
132 | typedef tcp_timeout_t * tcp_timeout_ref; |
135 | typedef tcp_timeout_t * tcp_timeout_ref; |
133 | 136 | ||
134 | /** TCP reply timeout data. |
137 | /** TCP reply timeout data. |
135 | * Used as a timeouting fibril argument. |
138 | * Used as a timeouting fibril argument. |
136 | * @see tcp_timeout() |
139 | * @see tcp_timeout() |
137 | */ |
140 | */ |
138 | struct tcp_timeout{ |
141 | struct tcp_timeout{ |
139 | /** TCP global data are going to be read only. |
142 | /** TCP global data are going to be read only. |
140 | */ |
143 | */ |
141 | int globals_read_only; |
144 | int globals_read_only; |
142 | /** Socket port. |
145 | /** Socket port. |
143 | */ |
146 | */ |
144 | int port; |
147 | int port; |
145 | /** Local sockets. |
148 | /** Local sockets. |
146 | */ |
149 | */ |
147 | socket_cores_ref local_sockets; |
150 | socket_cores_ref local_sockets; |
148 | /** Socket identifier. |
151 | /** Socket identifier. |
149 | */ |
152 | */ |
150 | int socket_id; |
153 | int socket_id; |
151 | /** Socket state. |
154 | /** Socket state. |
152 | */ |
155 | */ |
153 | tcp_socket_state_t state; |
156 | tcp_socket_state_t state; |
154 | /** Sent packet sequence number. |
157 | /** Sent packet sequence number. |
155 | */ |
158 | */ |
156 | int sequence_number; |
159 | int sequence_number; |
157 | /** Timeout in microseconds. |
160 | /** Timeout in microseconds. |
158 | */ |
161 | */ |
159 | suseconds_t timeout; |
162 | suseconds_t timeout; |
160 | /** Port map key. |
163 | /** Port map key. |
161 | */ |
164 | */ |
162 | char * key; |
165 | char * key; |
163 | /** Port map key length. |
166 | /** Port map key length. |
164 | */ |
167 | */ |
165 | size_t key_length; |
168 | size_t key_length; |
166 | }; |
169 | }; |
167 | 170 | ||
168 | /** Releases the packet and returns the result. |
171 | /** Releases the packet and returns the result. |
169 | * @param packet The packet queue to be released. Input parameter. |
172 | * @param[in] packet The packet queue to be released. |
170 | * @param result The result to be returned. Input parameter. |
173 | * @param[in] result The result to be returned. |
171 | * @return The result parameter. |
174 | * @return The result parameter. |
172 | */ |
175 | */ |
173 | int tcp_release_and_return( packet_t packet, int result ); |
176 | int tcp_release_and_return( packet_t packet, int result ); |
174 | 177 | ||
175 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ); |
178 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ); |
176 | int tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only ); |
179 | int tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only ); |
177 | void tcp_free_socket_data( socket_core_ref socket ); |
180 | void tcp_free_socket_data( socket_core_ref socket ); |
178 | int tcp_timeout( void * data ); |
181 | int tcp_timeout( void * data ); |
179 | int tcp_release_after_timeout( void * data ); |
182 | int tcp_release_after_timeout( void * data ); |
180 | int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error ); |
183 | int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error ); |
181 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ); |
184 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ); |
182 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ); |
185 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ); |
183 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ); |
186 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ); |
184 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ); |
187 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ); |
185 | void tcp_send_packets( device_id_t device_id, packet_t packet ); |
188 | void tcp_send_packets( device_id_t device_id, packet_t packet ); |
186 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ); |
189 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ); |
187 | packet_t tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ); |
190 | packet_t tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ); |
188 | packet_t tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ); |
191 | packet_t tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ); |
189 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ); |
192 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ); |
190 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ); |
193 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ); |
191 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ); |
194 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ); |
192 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ); |
195 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ); |
193 | int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen ); |
196 | int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen ); |
194 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ); |
197 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ); |
195 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ); |
198 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ); |
196 | int tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length ); |
199 | int tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length ); |
197 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ); |
200 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ); |
198 | 201 | ||
199 | int tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ); |
202 | int tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ); |
200 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ); |
203 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ); |
201 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ); |
204 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ); |
202 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ); |
205 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ); |
203 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ); |
206 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ); |
204 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags ); |
207 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags ); |
205 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen ); |
208 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen ); |
206 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id ); |
209 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id ); |
207 | 210 | ||
208 | /** TCP global data. |
211 | /** TCP global data. |
209 | */ |
212 | */ |
210 | tcp_globals_t tcp_globals; |
213 | tcp_globals_t tcp_globals; |
211 | 214 | ||
212 | /** Initializes the module. |
215 | /** Initializes the module. |
213 | */ |
216 | */ |
214 | int tcp_initialize( async_client_conn_t client_connection ){ |
217 | int tcp_initialize( async_client_conn_t client_connection ){ |
215 | ERROR_DECLARE; |
218 | ERROR_DECLARE; |
216 | 219 | ||
217 | assert( client_connection ); |
220 | assert( client_connection ); |
218 | fibril_rwlock_initialize( & tcp_globals.lock ); |
221 | fibril_rwlock_initialize( & tcp_globals.lock ); |
219 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
222 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
220 | tcp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP ); |
223 | tcp_globals.icmp_phone = icmp_connect_module( SERVICE_ICMP ); |
221 | if( tcp_globals.icmp_phone < 0 ){ |
224 | if( tcp_globals.icmp_phone < 0 ){ |
222 | return tcp_globals.icmp_phone; |
225 | return tcp_globals.icmp_phone; |
223 | } |
226 | } |
224 | tcp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg ); |
227 | tcp_globals.ip_phone = ip_bind_service( SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg ); |
225 | if( tcp_globals.ip_phone < 0 ){ |
228 | if( tcp_globals.ip_phone < 0 ){ |
226 | return tcp_globals.ip_phone; |
229 | return tcp_globals.ip_phone; |
227 | } |
230 | } |
228 | ERROR_PROPAGATE( socket_ports_initialize( & tcp_globals.sockets )); |
231 | ERROR_PROPAGATE( socket_ports_initialize( & tcp_globals.sockets )); |
229 | if( ERROR_OCCURRED( packet_dimensions_initialize( & tcp_globals.dimensions ))){ |
232 | if( ERROR_OCCURRED( packet_dimensions_initialize( & tcp_globals.dimensions ))){ |
230 | socket_ports_destroy( & tcp_globals.sockets ); |
233 | socket_ports_destroy( & tcp_globals.sockets ); |
231 | return ERROR_CODE; |
234 | return ERROR_CODE; |
232 | } |
235 | } |
233 | tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; |
236 | tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; |
234 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
237 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
235 | return EOK; |
238 | return EOK; |
236 | } |
239 | } |
237 | 240 | ||
238 | int tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
241 | int tcp_received_msg( device_id_t device_id, packet_t packet, services_t receiver, services_t error ){ |
239 | ERROR_DECLARE; |
242 | ERROR_DECLARE; |
240 | 243 | ||
241 | if( receiver != SERVICE_TCP ) return EREFUSED; |
244 | if( receiver != SERVICE_TCP ) return EREFUSED; |
242 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
245 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
243 | if( ERROR_OCCURRED( tcp_process_packet( device_id, packet, error ))){ |
246 | if( ERROR_OCCURRED( tcp_process_packet( device_id, packet, error ))){ |
244 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
247 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
245 | } |
248 | } |
246 | printf( "receive %d \n", ERROR_CODE ); |
249 | printf( "receive %d \n", ERROR_CODE ); |
247 | 250 | ||
248 | return ERROR_CODE; |
251 | return ERROR_CODE; |
249 | } |
252 | } |
250 | 253 | ||
251 | int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error ){ |
254 | int tcp_process_packet( device_id_t device_id, packet_t packet, services_t error ){ |
252 | ERROR_DECLARE; |
255 | ERROR_DECLARE; |
253 | 256 | ||
254 | size_t length; |
257 | size_t length; |
255 | size_t offset; |
258 | size_t offset; |
256 | int result; |
259 | int result; |
257 | tcp_header_ref header; |
260 | tcp_header_ref header; |
258 | socket_core_ref socket; |
261 | socket_core_ref socket; |
259 | tcp_socket_data_ref socket_data; |
262 | tcp_socket_data_ref socket_data; |
260 | packet_t next_packet; |
263 | packet_t next_packet; |
261 | size_t total_length; |
264 | size_t total_length; |
262 | uint32_t checksum; |
265 | uint32_t checksum; |
263 | int fragments; |
266 | int fragments; |
264 | icmp_type_t type; |
267 | icmp_type_t type; |
265 | icmp_code_t code; |
268 | icmp_code_t code; |
266 | struct sockaddr * src; |
269 | struct sockaddr * src; |
267 | struct sockaddr * dest; |
270 | struct sockaddr * dest; |
268 | size_t addrlen; |
271 | size_t addrlen; |
269 | 272 | ||
270 | printf( "p1 \n" ); |
273 | printf( "p1 \n" ); |
271 | if( error ){ |
274 | if( error ){ |
272 | switch( error ){ |
275 | switch( error ){ |
273 | case SERVICE_ICMP: |
276 | case SERVICE_ICMP: |
274 | // process error |
277 | // process error |
275 | result = icmp_client_process_packet( packet, & type, & code, NULL, NULL ); |
278 | result = icmp_client_process_packet( packet, & type, & code, NULL, NULL ); |
276 | if( result < 0 ){ |
279 | if( result < 0 ){ |
277 | return tcp_release_and_return( packet, result ); |
280 | return tcp_release_and_return( packet, result ); |
278 | } |
281 | } |
279 | length = ( size_t ) result; |
282 | length = ( size_t ) result; |
280 | if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
283 | if( ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
281 | return tcp_release_and_return( packet, ERROR_CODE ); |
284 | return tcp_release_and_return( packet, ERROR_CODE ); |
282 | } |
285 | } |
283 | break; |
286 | break; |
284 | default: |
287 | default: |
285 | return tcp_release_and_return( packet, ENOTSUP ); |
288 | return tcp_release_and_return( packet, ENOTSUP ); |
286 | } |
289 | } |
287 | } |
290 | } |
288 | 291 | ||
289 | // TODO process received ipopts? |
292 | // TODO process received ipopts? |
290 | result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
293 | result = ip_client_process_packet( packet, NULL, NULL, NULL, NULL, NULL ); |
291 | // printf("ip len %d\n", result ); |
294 | // printf("ip len %d\n", result ); |
292 | if( result < 0 ){ |
295 | if( result < 0 ){ |
293 | return tcp_release_and_return( packet, result ); |
296 | return tcp_release_and_return( packet, result ); |
294 | } |
297 | } |
295 | offset = ( size_t ) result; |
298 | offset = ( size_t ) result; |
296 | 299 | ||
297 | length = packet_get_data_length( packet ); |
300 | length = packet_get_data_length( packet ); |
298 | // printf("packet len %d\n", length ); |
301 | // printf("packet len %d\n", length ); |
299 | if( length <= 0 ){ |
302 | if( length <= 0 ){ |
300 | return tcp_release_and_return( packet, EINVAL ); |
303 | return tcp_release_and_return( packet, EINVAL ); |
301 | } |
304 | } |
302 | if( length < sizeof( tcp_header_t ) + offset ){ |
305 | if( length < sizeof( tcp_header_t ) + offset ){ |
303 | return tcp_release_and_return( packet, NO_DATA ); |
306 | return tcp_release_and_return( packet, NO_DATA ); |
304 | } |
307 | } |
305 | 308 | ||
306 | // trim all but TCP header |
309 | // trim all but TCP header |
307 | if( ERROR_OCCURRED( packet_trim( packet, offset, 0 ))){ |
310 | if( ERROR_OCCURRED( packet_trim( packet, offset, 0 ))){ |
308 | return tcp_release_and_return( packet, ERROR_CODE ); |
311 | return tcp_release_and_return( packet, ERROR_CODE ); |
309 | } |
312 | } |
310 | 313 | ||
311 | // get tcp header |
314 | // get tcp header |
312 | header = ( tcp_header_ref ) packet_get_data( packet ); |
315 | header = ( tcp_header_ref ) packet_get_data( packet ); |
313 | if( ! header ){ |
316 | if( ! header ){ |
314 | return tcp_release_and_return( packet, NO_DATA ); |
317 | return tcp_release_and_return( packet, NO_DATA ); |
315 | } |
318 | } |
316 | // printf( "header len %d, port %d \n", TCP_HEADER_LENGTH( header ), ntohs( header->destination_port )); |
319 | // printf( "header len %d, port %d \n", TCP_HEADER_LENGTH( header ), ntohs( header->destination_port )); |
317 | 320 | ||
318 | result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest ); |
321 | result = packet_get_addr( packet, ( uint8_t ** ) & src, ( uint8_t ** ) & dest ); |
319 | if( result <= 0 ){ |
322 | if( result <= 0 ){ |
320 | return tcp_release_and_return( packet, result ); |
323 | return tcp_release_and_return( packet, result ); |
321 | } |
324 | } |
322 | addrlen = ( size_t ) result; |
325 | addrlen = ( size_t ) result; |
323 | 326 | ||
324 | if( ERROR_OCCURRED( tl_set_address_port( src, addrlen, ntohs( header->source_port )))){ |
327 | if( ERROR_OCCURRED( tl_set_address_port( src, addrlen, ntohs( header->source_port )))){ |
325 | return tcp_release_and_return( packet, ERROR_CODE ); |
328 | return tcp_release_and_return( packet, ERROR_CODE ); |
326 | } |
329 | } |
327 | 330 | ||
328 | // find the destination socket |
331 | // find the destination socket |
329 | socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), ( const char * ) src, addrlen ); |
332 | socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), ( const char * ) src, addrlen ); |
330 | if( ! socket ){ |
333 | if( ! socket ){ |
331 | // printf("listening?\n"); |
334 | // printf("listening?\n"); |
332 | // find the listening destination socket |
335 | // find the listening destination socket |
333 | socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), SOCKET_MAP_KEY_LISTENING, 0 ); |
336 | socket = socket_port_find( & tcp_globals.sockets, ntohs( header->destination_port ), SOCKET_MAP_KEY_LISTENING, 0 ); |
334 | if( ! socket ){ |
337 | if( ! socket ){ |
335 | if( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ) == EOK ){ |
338 | if( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ) == EOK ){ |
336 | icmp_destination_unreachable_msg( tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet ); |
339 | icmp_destination_unreachable_msg( tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet ); |
337 | } |
340 | } |
338 | return EADDRNOTAVAIL; |
341 | return EADDRNOTAVAIL; |
339 | } |
342 | } |
340 | } |
343 | } |
341 | printf("socket id %d\n", socket->socket_id ); |
344 | printf("socket id %d\n", socket->socket_id ); |
342 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
345 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
343 | assert( socket_data ); |
346 | assert( socket_data ); |
344 | 347 | ||
345 | // some data received, clear the timeout counter |
348 | // some data received, clear the timeout counter |
346 | socket_data->timeout_count = 0; |
349 | socket_data->timeout_count = 0; |
347 | 350 | ||
348 | // count the received packet fragments |
351 | // count the received packet fragments |
349 | next_packet = packet; |
352 | next_packet = packet; |
350 | fragments = 0; |
353 | fragments = 0; |
351 | checksum = 0; |
354 | checksum = 0; |
352 | total_length = 0; |
355 | total_length = 0; |
353 | do{ |
356 | do{ |
354 | ++ fragments; |
357 | ++ fragments; |
355 | length = packet_get_data_length( next_packet ); |
358 | length = packet_get_data_length( next_packet ); |
356 | if( length <= 0 ){ |
359 | if( length <= 0 ){ |
357 | return tcp_release_and_return( packet, NO_DATA ); |
360 | return tcp_release_and_return( packet, NO_DATA ); |
358 | } |
361 | } |
359 | total_length += length; |
362 | total_length += length; |
360 | // add partial checksum if set |
363 | // add partial checksum if set |
361 | if( ! error ){ |
364 | if( ! error ){ |
362 | checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet )); |
365 | checksum = compute_checksum( checksum, packet_get_data( packet ), packet_get_data_length( packet )); |
363 | } |
366 | } |
364 | }while(( next_packet = pq_next( next_packet ))); |
367 | }while(( next_packet = pq_next( next_packet ))); |
365 | // printf( "fragments %d of %d bytes\n", fragments, total_length ); |
368 | // printf( "fragments %d of %d bytes\n", fragments, total_length ); |
366 | 369 | ||
367 | // printf("lock?\n"); |
370 | // printf("lock?\n"); |
368 | fibril_rwlock_write_lock( socket_data->local_lock ); |
371 | fibril_rwlock_write_lock( socket_data->local_lock ); |
369 | // printf("locked\n"); |
372 | // printf("locked\n"); |
370 | if( ! error ){ |
373 | if( ! error ){ |
371 | if( socket_data->state == TCP_SOCKET_LISTEN ){ |
374 | if( socket_data->state == TCP_SOCKET_LISTEN ){ |
372 | if( socket_data->pseudo_header ){ |
375 | if( socket_data->pseudo_header ){ |
373 | free( socket_data->pseudo_header ); |
376 | free( socket_data->pseudo_header ); |
374 | socket_data->pseudo_header = NULL; |
377 | socket_data->pseudo_header = NULL; |
375 | socket_data->headerlen = 0; |
378 | socket_data->headerlen = 0; |
376 | } |
379 | } |
377 | if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, & socket_data->pseudo_header, & socket_data->headerlen ))){ |
380 | if( ERROR_OCCURRED( ip_client_get_pseudo_header( IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, & socket_data->pseudo_header, & socket_data->headerlen ))){ |
378 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
381 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
379 | return tcp_release_and_return( packet, ERROR_CODE ); |
382 | return tcp_release_and_return( packet, ERROR_CODE ); |
380 | } |
383 | } |
381 | }else if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, total_length ))){ |
384 | }else if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, total_length ))){ |
382 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
385 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
383 | return tcp_release_and_return( packet, ERROR_CODE ); |
386 | return tcp_release_and_return( packet, ERROR_CODE ); |
384 | } |
387 | } |
385 | checksum = compute_checksum( checksum, socket_data->pseudo_header, socket_data->headerlen ); |
388 | checksum = compute_checksum( checksum, socket_data->pseudo_header, socket_data->headerlen ); |
386 | if( flip_checksum( compact_checksum( checksum ))){ |
389 | if( flip_checksum( compact_checksum( checksum ))){ |
387 | printf( "checksum err %x -> %x\n", header->checksum, flip_checksum( compact_checksum( checksum ))); |
390 | printf( "checksum err %x -> %x\n", header->checksum, flip_checksum( compact_checksum( checksum ))); |
388 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
391 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
389 | if( ! ERROR_OCCURRED( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ))){ |
392 | if( ! ERROR_OCCURRED( tl_prepare_icmp_packet( tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error ))){ |
390 | // checksum error ICMP |
393 | // checksum error ICMP |
391 | icmp_parameter_problem_msg( tcp_globals.icmp_phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->checksum )) - (( size_t ) (( void * ) header )), packet ); |
394 | icmp_parameter_problem_msg( tcp_globals.icmp_phone, ICMP_PARAM_POINTER, (( size_t ) (( void * ) & header->checksum )) - (( size_t ) (( void * ) header )), packet ); |
392 | } |
395 | } |
393 | return EINVAL; |
396 | return EINVAL; |
394 | } |
397 | } |
395 | } |
398 | } |
396 | 399 | ||
397 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
400 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
398 | 401 | ||
399 | // TODO error reporting/handling |
402 | // TODO error reporting/handling |
400 | // printf( "st %d\n", socket_data->state ); |
403 | // printf( "st %d\n", socket_data->state ); |
401 | switch( socket_data->state ){ |
404 | switch( socket_data->state ){ |
402 | case TCP_SOCKET_LISTEN: |
405 | case TCP_SOCKET_LISTEN: |
403 | ERROR_CODE = tcp_process_listen( socket, socket_data, header, packet, src, dest, addrlen ); |
406 | ERROR_CODE = tcp_process_listen( socket, socket_data, header, packet, src, dest, addrlen ); |
404 | break; |
407 | break; |
405 | case TCP_SOCKET_SYN_RECEIVED: |
408 | case TCP_SOCKET_SYN_RECEIVED: |
406 | ERROR_CODE = tcp_process_syn_received( socket, socket_data, header, packet ); |
409 | ERROR_CODE = tcp_process_syn_received( socket, socket_data, header, packet ); |
407 | break; |
410 | break; |
408 | case TCP_SOCKET_SYN_SENT: |
411 | case TCP_SOCKET_SYN_SENT: |
409 | ERROR_CODE = tcp_process_syn_sent( socket, socket_data, header, packet ); |
412 | ERROR_CODE = tcp_process_syn_sent( socket, socket_data, header, packet ); |
410 | break; |
413 | break; |
411 | case TCP_SOCKET_FIN_WAIT_1: |
414 | case TCP_SOCKET_FIN_WAIT_1: |
412 | // ack changing the state to FIN_WAIT_2 gets processed later |
415 | // ack changing the state to FIN_WAIT_2 gets processed later |
413 | case TCP_SOCKET_FIN_WAIT_2: |
416 | case TCP_SOCKET_FIN_WAIT_2: |
414 | // fin changing state to LAST_ACK gets processed later |
417 | // fin changing state to LAST_ACK gets processed later |
415 | case TCP_SOCKET_LAST_ACK: |
418 | case TCP_SOCKET_LAST_ACK: |
416 | // ack releasing the socket get processed later |
419 | // ack releasing the socket get processed later |
417 | case TCP_SOCKET_CLOSING: |
420 | case TCP_SOCKET_CLOSING: |
418 | // ack releasing the socket gets processed later |
421 | // ack releasing the socket gets processed later |
419 | case TCP_SOCKET_ESTABLISHED: |
422 | case TCP_SOCKET_ESTABLISHED: |
420 | ERROR_CODE = tcp_process_established( socket, socket_data, header, packet, fragments, total_length ); |
423 | ERROR_CODE = tcp_process_established( socket, socket_data, header, packet, fragments, total_length ); |
421 | break; |
424 | break; |
422 | default: |
425 | default: |
423 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
426 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
424 | } |
427 | } |
425 | 428 | ||
426 | if( ERROR_CODE != EOK ){ |
429 | if( ERROR_CODE != EOK ){ |
427 | printf( "process %d\n", ERROR_CODE ); |
430 | printf( "process %d\n", ERROR_CODE ); |
428 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
431 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
429 | } |
432 | } |
430 | return EOK; |
433 | return EOK; |
431 | } |
434 | } |
432 | 435 | ||
433 | int tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length ){ |
436 | int tcp_process_established( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, int fragments, size_t total_length ){ |
434 | ERROR_DECLARE; |
437 | ERROR_DECLARE; |
435 | 438 | ||
436 | packet_t next_packet; |
439 | packet_t next_packet; |
437 | packet_t tmp_packet; |
440 | packet_t tmp_packet; |
438 | uint32_t old_incoming; |
441 | uint32_t old_incoming; |
439 | size_t order; |
442 | size_t order; |
440 | uint32_t sequence_number; |
443 | uint32_t sequence_number; |
441 | size_t length; |
444 | size_t length; |
442 | size_t offset; |
445 | size_t offset; |
443 | uint32_t new_sequence_number; |
446 | uint32_t new_sequence_number; |
444 | 447 | ||
445 | assert( socket ); |
448 | assert( socket ); |
446 | assert( socket_data ); |
449 | assert( socket_data ); |
447 | assert( socket->specific_data == socket_data ); |
450 | assert( socket->specific_data == socket_data ); |
448 | assert( header ); |
451 | assert( header ); |
449 | assert( packet ); |
452 | assert( packet ); |
450 | 453 | ||
451 | new_sequence_number = ntohl( header->sequence_number ); |
454 | new_sequence_number = ntohl( header->sequence_number ); |
452 | old_incoming = socket_data->next_incoming; |
455 | old_incoming = socket_data->next_incoming; |
453 | 456 | ||
454 | if( header->finalize ){ |
457 | if( header->finalize ){ |
455 | socket_data->fin_incoming = new_sequence_number; |
458 | socket_data->fin_incoming = new_sequence_number; |
456 | } |
459 | } |
457 | 460 | ||
458 | // printf( "pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length ); |
461 | // printf( "pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length ); |
459 | // trim begining if containing expected data |
462 | // trim begining if containing expected data |
460 | if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length )){ |
463 | if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length )){ |
461 | // get the acknowledged offset |
464 | // get the acknowledged offset |
462 | if( socket_data->next_incoming < new_sequence_number ){ |
465 | if( socket_data->next_incoming < new_sequence_number ){ |
463 | offset = new_sequence_number - socket_data->next_incoming; |
466 | offset = new_sequence_number - socket_data->next_incoming; |
464 | }else{ |
467 | }else{ |
465 | offset = socket_data->next_incoming - new_sequence_number; |
468 | offset = socket_data->next_incoming - new_sequence_number; |
466 | } |
469 | } |
467 | // printf( "offset %d\n", offset ); |
470 | // printf( "offset %d\n", offset ); |
468 | new_sequence_number += offset; |
471 | new_sequence_number += offset; |
469 | total_length -= offset; |
472 | total_length -= offset; |
470 | length = packet_get_data_length( packet ); |
473 | length = packet_get_data_length( packet ); |
471 | // trim the acknowledged data |
474 | // trim the acknowledged data |
472 | while( length <= offset ){ |
475 | while( length <= offset ){ |
473 | // release the acknowledged packets |
476 | // release the acknowledged packets |
474 | next_packet = pq_next( packet ); |
477 | next_packet = pq_next( packet ); |
475 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
478 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
476 | packet = next_packet; |
479 | packet = next_packet; |
477 | offset -= length; |
480 | offset -= length; |
478 | length = packet_get_data_length( packet ); |
481 | length = packet_get_data_length( packet ); |
479 | } |
482 | } |
480 | if(( offset > 0 ) |
483 | if(( offset > 0 ) |
481 | && ( ERROR_OCCURRED( packet_trim( packet, offset, 0 )))){ |
484 | && ( ERROR_OCCURRED( packet_trim( packet, offset, 0 )))){ |
482 | return tcp_release_and_return( packet, ERROR_CODE ); |
485 | return tcp_release_and_return( packet, ERROR_CODE ); |
483 | } |
486 | } |
484 | assert( new_sequence_number == socket_data->next_incoming ); |
487 | assert( new_sequence_number == socket_data->next_incoming ); |
485 | } |
488 | } |
486 | 489 | ||
487 | // release if overflowing the window |
490 | // release if overflowing the window |
488 | // if( IS_IN_INTERVAL_OVERFLOW( socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length )){ |
491 | // if( IS_IN_INTERVAL_OVERFLOW( socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length )){ |
489 | // return tcp_release_and_return( packet, EOVERFLOW ); |
492 | // return tcp_release_and_return( packet, EOVERFLOW ); |
490 | // } |
493 | // } |
491 | 494 | ||
492 | /* |
495 | /* |
493 | // trim end if overflowing the window |
496 | // trim end if overflowing the window |
494 | if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length )){ |
497 | if( IS_IN_INTERVAL_OVERFLOW( new_sequence_number, socket_data->next_incoming + socket_data->window, new_sequence_number + total_length )){ |
495 | // get the allowed data length |
498 | // get the allowed data length |
496 | if( socket_data->next_incoming + socket_data->window < new_sequence_number ){ |
499 | if( socket_data->next_incoming + socket_data->window < new_sequence_number ){ |
497 | offset = new_sequence_number - socket_data->next_incoming + socket_data->window; |
500 | offset = new_sequence_number - socket_data->next_incoming + socket_data->window; |
498 | }else{ |
501 | }else{ |
499 | offset = socket_data->next_incoming + socket_data->window - new_sequence_number; |
502 | offset = socket_data->next_incoming + socket_data->window - new_sequence_number; |
500 | } |
503 | } |
501 | next_packet = packet; |
504 | next_packet = packet; |
502 | // trim the overflowing data |
505 | // trim the overflowing data |
503 | while( next_packet && ( offset > 0 )){ |
506 | while( next_packet && ( offset > 0 )){ |
504 | length = packet_get_data_length( packet ); |
507 | length = packet_get_data_length( packet ); |
505 | if( length <= offset ){ |
508 | if( length <= offset ){ |
506 | next_packet = pq_next( next_packet ); |
509 | next_packet = pq_next( next_packet ); |
507 | }else if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - offset ))){ |
510 | }else if( ERROR_OCCURRED( packet_trim( next_packet, 0, length - offset ))){ |
508 | return tcp_release_and_return( packet, ERROR_CODE ); |
511 | return tcp_release_and_return( packet, ERROR_CODE ); |
509 | } |
512 | } |
510 | offset -= length; |
513 | offset -= length; |
511 | total_length -= length - offset; |
514 | total_length -= length - offset; |
512 | } |
515 | } |
513 | // release the overflowing packets |
516 | // release the overflowing packets |
514 | next_packet = pq_next( next_packet ); |
517 | next_packet = pq_next( next_packet ); |
515 | if( next_packet ){ |
518 | if( next_packet ){ |
516 | tmp_packet = next_packet; |
519 | tmp_packet = next_packet; |
517 | next_packet = pq_next( next_packet ); |
520 | next_packet = pq_next( next_packet ); |
518 | pq_insert_after( tmp_packet, next_packet ); |
521 | pq_insert_after( tmp_packet, next_packet ); |
519 | pq_release( tcp_globals.net_phone, packet_get_id( tmp_packet )); |
522 | pq_release( tcp_globals.net_phone, packet_get_id( tmp_packet )); |
520 | } |
523 | } |
521 | assert( new_sequence_number + total_length == socket_data->next_incoming + socket_data->window ); |
524 | assert( new_sequence_number + total_length == socket_data->next_incoming + socket_data->window ); |
522 | } |
525 | } |
523 | */ |
526 | */ |
524 | // the expected one arrived? |
527 | // the expected one arrived? |
525 | if( new_sequence_number == socket_data->next_incoming ){ |
528 | if( new_sequence_number == socket_data->next_incoming ){ |
526 | printf("expected\n"); |
529 | printf("expected\n"); |
527 | // process acknowledgement |
530 | // process acknowledgement |
528 | tcp_process_acknowledgement( socket, socket_data, header ); |
531 | tcp_process_acknowledgement( socket, socket_data, header ); |
529 | 532 | ||
530 | // remove the header |
533 | // remove the header |
531 | total_length -= TCP_HEADER_LENGTH( header ); |
534 | total_length -= TCP_HEADER_LENGTH( header ); |
532 | if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){ |
535 | if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){ |
533 | return tcp_release_and_return( packet, ERROR_CODE ); |
536 | return tcp_release_and_return( packet, ERROR_CODE ); |
534 | } |
537 | } |
535 | 538 | ||
536 | if( total_length ){ |
539 | if( total_length ){ |
537 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, fragments, total_length )); |
540 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, fragments, total_length )); |
538 | }else{ |
541 | }else{ |
539 | total_length = 1; |
542 | total_length = 1; |
540 | } |
543 | } |
541 | socket_data->next_incoming = old_incoming + total_length; |
544 | socket_data->next_incoming = old_incoming + total_length; |
542 | packet = socket_data->incoming; |
545 | packet = socket_data->incoming; |
543 | while( packet ){ |
546 | while( packet ){ |
544 | if( ERROR_OCCURRED( pq_get_order( socket_data->incoming, & order, NULL ))){ |
547 | if( ERROR_OCCURRED( pq_get_order( socket_data->incoming, & order, NULL ))){ |
545 | // remove the corrupted packet |
548 | // remove the corrupted packet |
546 | next_packet = pq_detach( packet ); |
549 | next_packet = pq_detach( packet ); |
547 | if( packet == socket_data->incoming ){ |
550 | if( packet == socket_data->incoming ){ |
548 | socket_data->incoming = next_packet; |
551 | socket_data->incoming = next_packet; |
549 | } |
552 | } |
550 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
553 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
551 | packet = next_packet; |
554 | packet = next_packet; |
552 | continue; |
555 | continue; |
553 | } |
556 | } |
554 | sequence_number = ( uint32_t ) order; |
557 | sequence_number = ( uint32_t ) order; |
555 | if( IS_IN_INTERVAL_OVERFLOW( sequence_number, old_incoming, socket_data->next_incoming )){ |
558 | if( IS_IN_INTERVAL_OVERFLOW( sequence_number, old_incoming, socket_data->next_incoming )){ |
556 | // move to the next |
559 | // move to the next |
557 | packet = pq_next( packet ); |
560 | packet = pq_next( packet ); |
558 | // coninual data? |
561 | // coninual data? |
559 | }else if( IS_IN_INTERVAL_OVERFLOW( old_incoming, sequence_number, socket_data->next_incoming )){ |
562 | }else if( IS_IN_INTERVAL_OVERFLOW( old_incoming, sequence_number, socket_data->next_incoming )){ |
560 | // detach the packet |
563 | // detach the packet |
561 | next_packet = pq_detach( packet ); |
564 | next_packet = pq_detach( packet ); |
562 | if( packet == socket_data->incoming ){ |
565 | if( packet == socket_data->incoming ){ |
563 | socket_data->incoming = next_packet; |
566 | socket_data->incoming = next_packet; |
564 | } |
567 | } |
565 | // get data length |
568 | // get data length |
566 | length = packet_get_data_length( packet ); |
569 | length = packet_get_data_length( packet ); |
567 | new_sequence_number = sequence_number + length; |
570 | new_sequence_number = sequence_number + length; |
568 | if( length <= 0 ){ |
571 | if( length <= 0 ){ |
569 | // remove the empty packet |
572 | // remove the empty packet |
570 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
573 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
571 | packet = next_packet; |
574 | packet = next_packet; |
572 | continue; |
575 | continue; |
573 | } |
576 | } |
574 | // exactly following |
577 | // exactly following |
575 | if( sequence_number == socket_data->next_incoming ){ |
578 | if( sequence_number == socket_data->next_incoming ){ |
576 | // queue received data |
579 | // queue received data |
577 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet ))); |
580 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet ))); |
578 | socket_data->next_incoming = new_sequence_number; |
581 | socket_data->next_incoming = new_sequence_number; |
579 | packet = next_packet; |
582 | packet = next_packet; |
580 | continue; |
583 | continue; |
581 | // at least partly following data? |
584 | // at least partly following data? |
582 | }else if( IS_IN_INTERVAL_OVERFLOW( sequence_number, socket_data->next_incoming, new_sequence_number )){ |
585 | }else if( IS_IN_INTERVAL_OVERFLOW( sequence_number, socket_data->next_incoming, new_sequence_number )){ |
583 | if( socket_data->next_incoming < new_sequence_number ){ |
586 | if( socket_data->next_incoming < new_sequence_number ){ |
584 | length = new_sequence_number - socket_data->next_incoming; |
587 | length = new_sequence_number - socket_data->next_incoming; |
585 | }else{ |
588 | }else{ |
586 | length = socket_data->next_incoming - new_sequence_number; |
589 | length = socket_data->next_incoming - new_sequence_number; |
587 | } |
590 | } |
588 | if( ! ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
591 | if( ! ERROR_OCCURRED( packet_trim( packet, length, 0 ))){ |
589 | // queue received data |
592 | // queue received data |
590 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet ))); |
593 | ERROR_PROPAGATE( tcp_queue_received_packet( socket, socket_data, packet, 1, packet_get_data_length( packet ))); |
591 | socket_data->next_incoming = new_sequence_number; |
594 | socket_data->next_incoming = new_sequence_number; |
592 | packet = next_packet; |
595 | packet = next_packet; |
593 | continue; |
596 | continue; |
594 | } |
597 | } |
595 | } |
598 | } |
596 | // remove the duplicit or corrupted packet |
599 | // remove the duplicit or corrupted packet |
597 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
600 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
598 | packet = next_packet; |
601 | packet = next_packet; |
599 | continue; |
602 | continue; |
600 | }else{ |
603 | }else{ |
601 | break; |
604 | break; |
602 | } |
605 | } |
603 | } |
606 | } |
604 | }else if( IS_IN_INTERVAL( socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window )){ |
607 | }else if( IS_IN_INTERVAL( socket_data->next_incoming, new_sequence_number, socket_data->next_incoming + socket_data->window )){ |
605 | printf("in window\n"); |
608 | printf("in window\n"); |
606 | // process acknowledgement |
609 | // process acknowledgement |
607 | tcp_process_acknowledgement( socket, socket_data, header ); |
610 | tcp_process_acknowledgement( socket, socket_data, header ); |
608 | 611 | ||
609 | // remove the header |
612 | // remove the header |
610 | total_length -= TCP_HEADER_LENGTH( header ); |
613 | total_length -= TCP_HEADER_LENGTH( header ); |
611 | if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){ |
614 | if( ERROR_OCCURRED( packet_trim( packet, TCP_HEADER_LENGTH( header ), 0 ))){ |
612 | return tcp_release_and_return( packet, ERROR_CODE ); |
615 | return tcp_release_and_return( packet, ERROR_CODE ); |
613 | } |
616 | } |
614 | 617 | ||
615 | next_packet = pq_detach( packet ); |
618 | next_packet = pq_detach( packet ); |
616 | length = packet_get_data_length( packet ); |
619 | length = packet_get_data_length( packet ); |
617 | tmp_packet = pq_add( socket_data->incoming, packet, new_sequence_number, length ); |
620 | tmp_packet = pq_add( socket_data->incoming, packet, new_sequence_number, length ); |
618 | if( ! tmp_packet ){ |
621 | if( ! tmp_packet ){ |
619 | // remove the corrupted packets |
622 | // remove the corrupted packets |
620 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
623 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
621 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
624 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
622 | }else{ |
625 | }else{ |
623 | socket_data->incoming = tmp_packet; |
626 | socket_data->incoming = tmp_packet; |
624 | while( next_packet ){ |
627 | while( next_packet ){ |
625 | new_sequence_number += length; |
628 | new_sequence_number += length; |
626 | tmp_packet = pq_detach( next_packet ); |
629 | tmp_packet = pq_detach( next_packet ); |
627 | length = packet_get_data_length( next_packet ); |
630 | length = packet_get_data_length( next_packet ); |
628 | if( ERROR_OCCURRED( pq_set_order( next_packet, new_sequence_number, length )) |
631 | if( ERROR_OCCURRED( pq_set_order( next_packet, new_sequence_number, length )) |
629 | || ERROR_OCCURRED( pq_insert_after( packet, next_packet ))){ |
632 | || ERROR_OCCURRED( pq_insert_after( packet, next_packet ))){ |
630 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
633 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
631 | } |
634 | } |
632 | next_packet = tmp_packet; |
635 | next_packet = tmp_packet; |
633 | } |
636 | } |
634 | } |
637 | } |
635 | }else{ |
638 | }else{ |
636 | printf("unexpected\n"); |
639 | printf("unexpected\n"); |
637 | // release duplicite or restricted |
640 | // release duplicite or restricted |
638 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
641 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
639 | } |
642 | } |
640 | 643 | ||
641 | // change state according to the acknowledging incoming fin |
644 | // change state according to the acknowledging incoming fin |
642 | if( IS_IN_INTERVAL_OVERFLOW( old_incoming, socket_data->fin_incoming, socket_data->next_incoming )){ |
645 | if( IS_IN_INTERVAL_OVERFLOW( old_incoming, socket_data->fin_incoming, socket_data->next_incoming )){ |
643 | switch( socket_data->state ){ |
646 | switch( socket_data->state ){ |
644 | case TCP_SOCKET_FIN_WAIT_1: |
647 | case TCP_SOCKET_FIN_WAIT_1: |
645 | case TCP_SOCKET_FIN_WAIT_2: |
648 | case TCP_SOCKET_FIN_WAIT_2: |
646 | case TCP_SOCKET_CLOSING: |
649 | case TCP_SOCKET_CLOSING: |
647 | socket_data->state = TCP_SOCKET_CLOSING; |
650 | socket_data->state = TCP_SOCKET_CLOSING; |
648 | break; |
651 | break; |
649 | //case TCP_ESTABLISHED: |
652 | //case TCP_ESTABLISHED: |
650 | default: |
653 | default: |
651 | socket_data->state = TCP_SOCKET_CLOSE_WAIT; |
654 | socket_data->state = TCP_SOCKET_CLOSE_WAIT; |
652 | break; |
655 | break; |
653 | } |
656 | } |
654 | } |
657 | } |
655 | 658 | ||
656 | packet = tcp_get_packets_to_send( socket, socket_data ); |
659 | packet = tcp_get_packets_to_send( socket, socket_data ); |
657 | if( ! packet ){ |
660 | if( ! packet ){ |
658 | // create the notification packet |
661 | // create the notification packet |
659 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 0 )); |
662 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 0 )); |
660 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, 1 )); |
663 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, 1 )); |
661 | packet = tcp_send_prepare_packet( socket, socket_data, packet, 1, socket_data->last_outgoing + 1 ); |
664 | packet = tcp_send_prepare_packet( socket, socket_data, packet, 1, socket_data->last_outgoing + 1 ); |
662 | } |
665 | } |
663 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
666 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
664 | // send the packet |
667 | // send the packet |
665 | tcp_send_packets( socket_data->device_id, packet ); |
668 | tcp_send_packets( socket_data->device_id, packet ); |
666 | return EOK; |
669 | return EOK; |
667 | } |
670 | } |
668 | 671 | ||
669 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ){ |
672 | int tcp_queue_received_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length ){ |
670 | ERROR_DECLARE; |
673 | ERROR_DECLARE; |
671 | 674 | ||
672 | assert( socket ); |
675 | assert( socket ); |
673 | assert( socket_data ); |
676 | assert( socket_data ); |
674 | assert( socket->specific_data == socket_data ); |
677 | assert( socket->specific_data == socket_data ); |
675 | assert( packet ); |
678 | assert( packet ); |
676 | assert( fragments >= 1 ); |
679 | assert( fragments >= 1 ); |
677 | assert( socket_data->window > total_length ); |
680 | assert( socket_data->window > total_length ); |
678 | 681 | ||
679 | // queue the received packet |
682 | // queue the received packet |
680 | if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ |
683 | if( ERROR_OCCURRED( dyn_fifo_push( & socket->received, packet_get_id( packet ), SOCKET_MAX_RECEIVED_SIZE ))){ |
681 | return tcp_release_and_return( packet, ERROR_CODE ); |
684 | return tcp_release_and_return( packet, ERROR_CODE ); |
682 | } |
685 | } |
683 | 686 | ||
684 | // decrease the window size |
687 | // decrease the window size |
685 | socket_data->window -= total_length; |
688 | socket_data->window -= total_length; |
686 | 689 | ||
687 | // notify the destination socket |
690 | // notify the destination socket |
688 | async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments ); |
691 | async_msg_5( socket->phone, NET_SOCKET_RECEIVED, ( ipcarg_t ) socket->socket_id, 0, 0, 0, ( ipcarg_t ) fragments ); |
689 | return EOK; |
692 | return EOK; |
690 | } |
693 | } |
691 | 694 | ||
692 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){ |
695 | int tcp_process_syn_sent( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){ |
693 | ERROR_DECLARE; |
696 | ERROR_DECLARE; |
694 | 697 | ||
695 | packet_t next_packet; |
698 | packet_t next_packet; |
696 | 699 | ||
697 | assert( socket ); |
700 | assert( socket ); |
698 | assert( socket_data ); |
701 | assert( socket_data ); |
699 | assert( socket->specific_data == socket_data ); |
702 | assert( socket->specific_data == socket_data ); |
700 | assert( header ); |
703 | assert( header ); |
701 | assert( packet ); |
704 | assert( packet ); |
702 | 705 | ||
703 | if( header->synchronize ){ |
706 | if( header->synchronize ){ |
704 | // process acknowledgement |
707 | // process acknowledgement |
705 | tcp_process_acknowledgement( socket, socket_data, header ); |
708 | tcp_process_acknowledgement( socket, socket_data, header ); |
706 | 709 | ||
707 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1; |
710 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1; |
708 | // release additional packets |
711 | // release additional packets |
709 | next_packet = pq_detach( packet ); |
712 | next_packet = pq_detach( packet ); |
710 | if( next_packet ){ |
713 | if( next_packet ){ |
711 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
714 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
712 | } |
715 | } |
713 | // trim if longer than the header |
716 | // trim if longer than the header |
714 | if(( packet_get_data_length( packet ) > sizeof( * header )) |
717 | if(( packet_get_data_length( packet ) > sizeof( * header )) |
715 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){ |
718 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){ |
716 | return tcp_release_and_return( packet, ERROR_CODE ); |
719 | return tcp_release_and_return( packet, ERROR_CODE ); |
717 | } |
720 | } |
718 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 ); |
721 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 ); |
719 | fibril_mutex_lock( & socket_data->operation.mutex ); |
722 | fibril_mutex_lock( & socket_data->operation.mutex ); |
720 | socket_data->operation.result = tcp_queue_packet( socket, socket_data, packet, 1 ); |
723 | socket_data->operation.result = tcp_queue_packet( socket, socket_data, packet, 1 ); |
721 | if( socket_data->operation.result == EOK ){ |
724 | if( socket_data->operation.result == EOK ){ |
722 | socket_data->state = TCP_SOCKET_ESTABLISHED; |
725 | socket_data->state = TCP_SOCKET_ESTABLISHED; |
723 | packet = tcp_get_packets_to_send( socket, socket_data ); |
726 | packet = tcp_get_packets_to_send( socket, socket_data ); |
724 | if( packet ){ |
727 | if( packet ){ |
725 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
728 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
726 | // send the packet |
729 | // send the packet |
727 | tcp_send_packets( socket_data->device_id, packet ); |
730 | tcp_send_packets( socket_data->device_id, packet ); |
728 | // signal the result |
731 | // signal the result |
729 | fibril_condvar_signal( & socket_data->operation.condvar ); |
732 | fibril_condvar_signal( & socket_data->operation.condvar ); |
730 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
733 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
731 | return EOK; |
734 | return EOK; |
732 | } |
735 | } |
733 | } |
736 | } |
734 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
737 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
735 | } |
738 | } |
736 | return tcp_release_and_return( packet, EINVAL ); |
739 | return tcp_release_and_return( packet, EINVAL ); |
737 | } |
740 | } |
738 | 741 | ||
739 | int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen ){ |
742 | int tcp_process_listen( socket_core_ref listening_socket, tcp_socket_data_ref listening_socket_data, tcp_header_ref header, packet_t packet, struct sockaddr * src, struct sockaddr * dest, size_t addrlen ){ |
740 | ERROR_DECLARE; |
743 | ERROR_DECLARE; |
741 | 744 | ||
742 | packet_t next_packet; |
745 | packet_t next_packet; |
743 | socket_core_ref socket; |
746 | socket_core_ref socket; |
744 | tcp_socket_data_ref socket_data; |
747 | tcp_socket_data_ref socket_data; |
745 | int socket_id; |
748 | int socket_id; |
746 | int listening_socket_id = listening_socket->socket_id; |
749 | int listening_socket_id = listening_socket->socket_id; |
747 | int listening_port = listening_socket->port; |
750 | int listening_port = listening_socket->port; |
748 | 751 | ||
749 | assert( listening_socket ); |
752 | assert( listening_socket ); |
750 | assert( listening_socket_data ); |
753 | assert( listening_socket_data ); |
751 | assert( listening_socket->specific_data == listening_socket_data ); |
754 | assert( listening_socket->specific_data == listening_socket_data ); |
752 | assert( header ); |
755 | assert( header ); |
753 | assert( packet ); |
756 | assert( packet ); |
754 | 757 | ||
755 | // printf( "syn %d\n", header->synchronize ); |
758 | // printf( "syn %d\n", header->synchronize ); |
756 | if( header->synchronize ){ |
759 | if( header->synchronize ){ |
757 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data )); |
760 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data )); |
758 | if( ! socket_data ){ |
761 | if( ! socket_data ){ |
759 | return tcp_release_and_return( packet, ENOMEM ); |
762 | return tcp_release_and_return( packet, ENOMEM ); |
760 | }else{ |
763 | }else{ |
761 | tcp_initialize_socket_data( socket_data ); |
764 | tcp_initialize_socket_data( socket_data ); |
762 | socket_data->local_lock = listening_socket_data->local_lock; |
765 | socket_data->local_lock = listening_socket_data->local_lock; |
763 | socket_data->local_sockets = listening_socket_data->local_sockets; |
766 | socket_data->local_sockets = listening_socket_data->local_sockets; |
764 | socket_data->listening_socket_id = listening_socket->socket_id; |
767 | socket_data->listening_socket_id = listening_socket->socket_id; |
765 | 768 | ||
766 | socket_data->next_incoming = ntohl( header->sequence_number ); |
769 | socket_data->next_incoming = ntohl( header->sequence_number ); |
767 | socket_data->treshold = socket_data->next_incoming + ntohs( header->window ); |
770 | socket_data->treshold = socket_data->next_incoming + ntohs( header->window ); |
768 | 771 | ||
769 | socket_data->addrlen = addrlen; |
772 | socket_data->addrlen = addrlen; |
770 | socket_data->addr = malloc( socket_data->addrlen ); |
773 | socket_data->addr = malloc( socket_data->addrlen ); |
771 | if( ! socket_data->addr ){ |
774 | if( ! socket_data->addr ){ |
772 | free( socket_data ); |
775 | free( socket_data ); |
773 | return tcp_release_and_return( packet, ENOMEM ); |
776 | return tcp_release_and_return( packet, ENOMEM ); |
774 | } |
777 | } |
775 | memcpy( socket_data->addr, src, socket_data->addrlen ); |
778 | memcpy( socket_data->addr, src, socket_data->addrlen ); |
776 | 779 | ||
777 | socket_data->dest_port = ntohs( header->source_port ); |
780 | socket_data->dest_port = ntohs( header->source_port ); |
778 | if( ERROR_OCCURRED( tl_set_address_port( socket_data->addr, socket_data->addrlen, socket_data->dest_port ))){ |
781 | if( ERROR_OCCURRED( tl_set_address_port( socket_data->addr, socket_data->addrlen, socket_data->dest_port ))){ |
779 | free( socket_data->addr ); |
782 | free( socket_data->addr ); |
780 | free( socket_data ); |
783 | free( socket_data ); |
781 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
784 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
782 | return ERROR_CODE; |
785 | return ERROR_CODE; |
783 | } |
786 | } |
784 | 787 | ||
785 | // printf( "addr %p\n", socket_data->addr, socket_data->addrlen ); |
788 | // printf( "addr %p\n", socket_data->addr, socket_data->addrlen ); |
786 | // create a socket |
789 | // create a socket |
787 | if( ERROR_OCCURRED( socket_create( socket_data->local_sockets, listening_socket->phone, socket_data, & socket_id ))){ |
790 | if( ERROR_OCCURRED( socket_create( socket_data->local_sockets, listening_socket->phone, socket_data, & socket_id ))){ |
788 | free( socket_data->addr ); |
791 | free( socket_data->addr ); |
789 | free( socket_data ); |
792 | free( socket_data ); |
790 | return tcp_release_and_return( packet, ERROR_CODE ); |
793 | return tcp_release_and_return( packet, ERROR_CODE ); |
791 | } |
794 | } |
792 | 795 | ||
793 | printf("new_sock %d\n", socket_id); |
796 | printf("new_sock %d\n", socket_id); |
794 | socket_data->pseudo_header = listening_socket_data->pseudo_header; |
797 | socket_data->pseudo_header = listening_socket_data->pseudo_header; |
795 | socket_data->headerlen = listening_socket_data->headerlen; |
798 | socket_data->headerlen = listening_socket_data->headerlen; |
796 | listening_socket_data->pseudo_header = NULL; |
799 | listening_socket_data->pseudo_header = NULL; |
797 | listening_socket_data->headerlen = 0; |
800 | listening_socket_data->headerlen = 0; |
798 | 801 | ||
799 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
802 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
800 | // printf("list lg\n"); |
803 | // printf("list lg\n"); |
801 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
804 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
802 | // printf("list locked\n"); |
805 | // printf("list locked\n"); |
803 | // find the destination socket |
806 | // find the destination socket |
804 | listening_socket = socket_port_find( & tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0 ); |
807 | listening_socket = socket_port_find( & tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0 ); |
805 | if(( ! listening_socket ) || ( listening_socket->socket_id != listening_socket_id )){ |
808 | if(( ! listening_socket ) || ( listening_socket->socket_id != listening_socket_id )){ |
806 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
809 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
807 | // a shadow may remain until app hangs up |
810 | // a shadow may remain until app hangs up |
808 | return tcp_release_and_return( packet, EOK/*ENOTSOCK*/ ); |
811 | return tcp_release_and_return( packet, EOK/*ENOTSOCK*/ ); |
809 | } |
812 | } |
810 | // printf("port %d\n", listening_socket->port ); |
813 | // printf("port %d\n", listening_socket->port ); |
811 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data; |
814 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data; |
812 | assert( listening_socket_data ); |
815 | assert( listening_socket_data ); |
813 | 816 | ||
814 | // printf("list ll\n"); |
817 | // printf("list ll\n"); |
815 | fibril_rwlock_write_lock( listening_socket_data->local_lock ); |
818 | fibril_rwlock_write_lock( listening_socket_data->local_lock ); |
816 | // printf("list locked\n"); |
819 | // printf("list locked\n"); |
817 | 820 | ||
818 | socket = socket_cores_find( listening_socket_data->local_sockets, socket_id ); |
821 | socket = socket_cores_find( listening_socket_data->local_sockets, socket_id ); |
819 | if( ! socket ){ |
822 | if( ! socket ){ |
820 | // where is the socket?!? |
823 | // where is the socket?!? |
821 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
824 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
822 | return ENOTSOCK; |
825 | return ENOTSOCK; |
823 | } |
826 | } |
824 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
827 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
825 | assert( socket_data ); |
828 | assert( socket_data ); |
826 | 829 | ||
827 | // uint8_t * data = socket_data->addr; |
830 | // uint8_t * data = socket_data->addr; |
828 | // printf( "addr %d of %x %x %x %x-%x %x %x %x-%x %x %x %x-%x %x %x %x\n", socket_data->addrlen, data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] ); |
831 | // printf( "addr %d of %x %x %x %x-%x %x %x %x-%x %x %x %x-%x %x %x %x\n", socket_data->addrlen, data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ], data[ 6 ], data[ 7 ], data[ 8 ], data[ 9 ], data[ 10 ], data[ 11 ], data[ 12 ], data[ 13 ], data[ 14 ], data[ 15 ] ); |
829 | 832 | ||
830 | ERROR_CODE = socket_port_add( & tcp_globals.sockets, listening_port, socket, ( const char * ) socket_data->addr, socket_data->addrlen ); |
833 | ERROR_CODE = socket_port_add( & tcp_globals.sockets, listening_port, socket, ( const char * ) socket_data->addr, socket_data->addrlen ); |
831 | assert( socket == socket_port_find( & tcp_globals.sockets, listening_port, ( const char * ) socket_data->addr, socket_data->addrlen )); |
834 | assert( socket == socket_port_find( & tcp_globals.sockets, listening_port, ( const char * ) socket_data->addr, socket_data->addrlen )); |
832 | //ERROR_CODE = socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ); |
835 | //ERROR_CODE = socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ); |
833 | //tcp_globals.last_used_port = socket->port; |
836 | //tcp_globals.last_used_port = socket->port; |
834 | // printf("bound %d\n", socket->port ); |
837 | // printf("bound %d\n", socket->port ); |
835 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
838 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
836 | if( ERROR_CODE != EOK ){ |
839 | if( ERROR_CODE != EOK ){ |
837 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
840 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
838 | return tcp_release_and_return( packet, ERROR_CODE ); |
841 | return tcp_release_and_return( packet, ERROR_CODE ); |
839 | } |
842 | } |
840 | 843 | ||
841 | socket_data->state = TCP_SOCKET_LISTEN; |
844 | socket_data->state = TCP_SOCKET_LISTEN; |
842 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1; |
845 | socket_data->next_incoming = ntohl( header->sequence_number ) + 1; |
843 | // release additional packets |
846 | // release additional packets |
844 | next_packet = pq_detach( packet ); |
847 | next_packet = pq_detach( packet ); |
845 | if( next_packet ){ |
848 | if( next_packet ){ |
846 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
849 | pq_release( tcp_globals.net_phone, packet_get_id( next_packet )); |
847 | } |
850 | } |
848 | // trim if longer than the header |
851 | // trim if longer than the header |
849 | if(( packet_get_data_length( packet ) > sizeof( * header )) |
852 | if(( packet_get_data_length( packet ) > sizeof( * header )) |
850 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){ |
853 | && ERROR_OCCURRED( packet_trim( packet, 0, packet_get_data_length( packet ) - sizeof( * header )))){ |
851 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
854 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
852 | return tcp_release_and_return( packet, ERROR_CODE ); |
855 | return tcp_release_and_return( packet, ERROR_CODE ); |
853 | } |
856 | } |
854 | tcp_prepare_operation_header( socket, socket_data, header, 1, 0 ); |
857 | tcp_prepare_operation_header( socket, socket_data, header, 1, 0 ); |
855 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))){ |
858 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 ))){ |
856 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
859 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
857 | return ERROR_CODE; |
860 | return ERROR_CODE; |
858 | } |
861 | } |
859 | packet = tcp_get_packets_to_send( socket, socket_data ); |
862 | packet = tcp_get_packets_to_send( socket, socket_data ); |
860 | // printf("send %d\n", packet_get_id( packet )); |
863 | // printf("send %d\n", packet_get_id( packet )); |
861 | if( ! packet ){ |
864 | if( ! packet ){ |
862 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
865 | socket_destroy( tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
863 | return EINVAL; |
866 | return EINVAL; |
864 | }else{ |
867 | }else{ |
865 | socket_data->state = TCP_SOCKET_SYN_RECEIVED; |
868 | socket_data->state = TCP_SOCKET_SYN_RECEIVED; |
866 | // printf("unlock\n"); |
869 | // printf("unlock\n"); |
867 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
870 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
868 | // send the packet |
871 | // send the packet |
869 | tcp_send_packets( socket_data->device_id, packet ); |
872 | tcp_send_packets( socket_data->device_id, packet ); |
870 | return EOK; |
873 | return EOK; |
871 | } |
874 | } |
872 | } |
875 | } |
873 | } |
876 | } |
874 | return tcp_release_and_return( packet, EINVAL ); |
877 | return tcp_release_and_return( packet, EINVAL ); |
875 | } |
878 | } |
876 | 879 | ||
877 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){ |
880 | int tcp_process_syn_received( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet ){ |
878 | ERROR_DECLARE; |
881 | ERROR_DECLARE; |
879 | 882 | ||
880 | socket_core_ref listening_socket; |
883 | socket_core_ref listening_socket; |
881 | tcp_socket_data_ref listening_socket_data; |
884 | tcp_socket_data_ref listening_socket_data; |
882 | 885 | ||
883 | assert( socket ); |
886 | assert( socket ); |
884 | assert( socket_data ); |
887 | assert( socket_data ); |
885 | assert( socket->specific_data == socket_data ); |
888 | assert( socket->specific_data == socket_data ); |
886 | assert( header ); |
889 | assert( header ); |
887 | assert( packet ); |
890 | assert( packet ); |
888 | 891 | ||
889 | printf("syn_rec\n"); |
892 | printf("syn_rec\n"); |
890 | if( header->acknowledge ){ |
893 | if( header->acknowledge ){ |
891 | // process acknowledgement |
894 | // process acknowledgement |
892 | tcp_process_acknowledgement( socket, socket_data, header ); |
895 | tcp_process_acknowledgement( socket, socket_data, header ); |
893 | 896 | ||
894 | socket_data->next_incoming = ntohl( header->sequence_number );// + 1; |
897 | socket_data->next_incoming = ntohl( header->sequence_number );// + 1; |
895 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
898 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
896 | socket_data->state = TCP_SOCKET_ESTABLISHED; |
899 | socket_data->state = TCP_SOCKET_ESTABLISHED; |
897 | listening_socket = socket_cores_find( socket_data->local_sockets, socket_data->listening_socket_id ); |
900 | listening_socket = socket_cores_find( socket_data->local_sockets, socket_data->listening_socket_id ); |
898 | if( listening_socket ){ |
901 | if( listening_socket ){ |
899 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data; |
902 | listening_socket_data = ( tcp_socket_data_ref ) listening_socket->specific_data; |
900 | assert( listening_socket_data ); |
903 | assert( listening_socket_data ); |
901 | 904 | ||
902 | // queue the received packet |
905 | // queue the received packet |
903 | if( ! ERROR_OCCURRED( dyn_fifo_push( & listening_socket->accepted, socket->socket_id, listening_socket_data->backlog ))){ |
906 | if( ! ERROR_OCCURRED( dyn_fifo_push( & listening_socket->accepted, socket->socket_id, listening_socket_data->backlog ))){ |
904 | // notify the destination socket |
907 | // notify the destination socket |
905 | async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, 0, 0, 0, ( ipcarg_t ) socket->socket_id ); |
908 | async_msg_5( socket->phone, NET_SOCKET_ACCEPTED, ( ipcarg_t ) listening_socket->socket_id, 0, 0, 0, ( ipcarg_t ) socket->socket_id ); |
906 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
909 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
907 | return EOK; |
910 | return EOK; |
908 | } |
911 | } |
909 | } |
912 | } |
910 | // send FIN |
913 | // send FIN |
911 | socket_data->state = TCP_SOCKET_FIN_WAIT_1; |
914 | socket_data->state = TCP_SOCKET_FIN_WAIT_1; |
912 | 915 | ||
913 | // create the notification packet |
916 | // create the notification packet |
914 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 )); |
917 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 )); |
915 | 918 | ||
916 | // send the packet |
919 | // send the packet |
917 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 )); |
920 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 )); |
918 | 921 | ||
919 | // flush packets |
922 | // flush packets |
920 | packet = tcp_get_packets_to_send( socket, socket_data ); |
923 | packet = tcp_get_packets_to_send( socket, socket_data ); |
921 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
924 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
922 | if( packet ){ |
925 | if( packet ){ |
923 | // send the packet |
926 | // send the packet |
924 | tcp_send_packets( socket_data->device_id, packet ); |
927 | tcp_send_packets( socket_data->device_id, packet ); |
925 | } |
928 | } |
926 | return EOK; |
929 | return EOK; |
927 | }else{ |
930 | }else{ |
928 | return tcp_release_and_return( packet, EINVAL ); |
931 | return tcp_release_and_return( packet, EINVAL ); |
929 | } |
932 | } |
930 | return EINVAL; |
933 | return EINVAL; |
931 | } |
934 | } |
932 | 935 | ||
933 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ){ |
936 | void tcp_process_acknowledgement( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header ){ |
934 | size_t number; |
937 | size_t number; |
935 | size_t length; |
938 | size_t length; |
936 | packet_t packet; |
939 | packet_t packet; |
937 | packet_t next; |
940 | packet_t next; |
938 | packet_t acknowledged = NULL; |
941 | packet_t acknowledged = NULL; |
939 | packet_t first; |
942 | packet_t first; |
940 | uint32_t old; |
943 | uint32_t old; |
941 | 944 | ||
942 | assert( socket ); |
945 | assert( socket ); |
943 | assert( socket_data ); |
946 | assert( socket_data ); |
944 | assert( socket->specific_data == socket_data ); |
947 | assert( socket->specific_data == socket_data ); |
945 | assert( header ); |
948 | assert( header ); |
946 | 949 | ||
947 | if( header->acknowledge ){ |
950 | if( header->acknowledge ){ |
948 | number = ntohl( header->acknowledgement_number ); |
951 | number = ntohl( header->acknowledgement_number ); |
949 | // if more data acknowledged |
952 | // if more data acknowledged |
950 | if( number != socket_data->expected ){ |
953 | if( number != socket_data->expected ){ |
951 | old = socket_data->expected; |
954 | old = socket_data->expected; |
952 | if( IS_IN_INTERVAL_OVERFLOW( old, socket_data->fin_outgoing, number )){ |
955 | if( IS_IN_INTERVAL_OVERFLOW( old, socket_data->fin_outgoing, number )){ |
953 | switch( socket_data->state ){ |
956 | switch( socket_data->state ){ |
954 | case TCP_SOCKET_FIN_WAIT_1: |
957 | case TCP_SOCKET_FIN_WAIT_1: |
955 | socket_data->state = TCP_SOCKET_FIN_WAIT_2; |
958 | socket_data->state = TCP_SOCKET_FIN_WAIT_2; |
956 | break; |
959 | break; |
957 | case TCP_SOCKET_LAST_ACK: |
960 | case TCP_SOCKET_LAST_ACK: |
958 | case TCP_SOCKET_CLOSING: |
961 | case TCP_SOCKET_CLOSING: |
959 | // fin acknowledged - release the socket in another fibril |
962 | // fin acknowledged - release the socket in another fibril |
960 | tcp_prepare_timeout( tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true ); |
963 | tcp_prepare_timeout( tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true ); |
961 | break; |
964 | break; |
962 | default: |
965 | default: |
963 | break; |
966 | break; |
964 | } |
967 | } |
965 | } |
968 | } |
966 | // update the treshold if higher than set |
969 | // update the treshold if higher than set |
967 | if( number + ntohs( header->window ) > socket_data->expected + socket_data->treshold ){ |
970 | if( number + ntohs( header->window ) > socket_data->expected + socket_data->treshold ){ |
968 | socket_data->treshold = number + ntohs( header->window ) - socket_data->expected; |
971 | socket_data->treshold = number + ntohs( header->window ) - socket_data->expected; |
969 | } |
972 | } |
970 | // set new expected sequence number |
973 | // set new expected sequence number |
971 | socket_data->expected = number; |
974 | socket_data->expected = number; |
972 | socket_data->expected_count = 1; |
975 | socket_data->expected_count = 1; |
973 | packet = socket_data->outgoing; |
976 | packet = socket_data->outgoing; |
974 | while( pq_get_order( packet, & number, & length ) == EOK ){ |
977 | while( pq_get_order( packet, & number, & length ) == EOK ){ |
975 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) old, ( uint32_t )( number + length ), ( uint32_t ) socket_data->expected )){ |
978 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) old, ( uint32_t )( number + length ), ( uint32_t ) socket_data->expected )){ |
976 | next = pq_detach( packet ); |
979 | next = pq_detach( packet ); |
977 | if( packet == socket_data->outgoing ){ |
980 | if( packet == socket_data->outgoing ){ |
978 | socket_data->outgoing = next; |
981 | socket_data->outgoing = next; |
979 | } |
982 | } |
980 | // add to acknowledged or release |
983 | // add to acknowledged or release |
981 | first = pq_add( acknowledged, packet, 0, 0 ); |
984 | first = pq_add( acknowledged, packet, 0, 0 ); |
982 | if( first ){ |
985 | if( first ){ |
983 | acknowledged = first; |
986 | acknowledged = first; |
984 | }else{ |
987 | }else{ |
985 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
988 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
986 | } |
989 | } |
987 | packet = next; |
990 | packet = next; |
988 | }else if( old < socket_data->expected ){ |
991 | }else if( old < socket_data->expected ){ |
989 | break; |
992 | break; |
990 | } |
993 | } |
991 | } |
994 | } |
992 | // release acknowledged |
995 | // release acknowledged |
993 | if( acknowledged ){ |
996 | if( acknowledged ){ |
994 | pq_release( tcp_globals.net_phone, packet_get_id( acknowledged )); |
997 | pq_release( tcp_globals.net_phone, packet_get_id( acknowledged )); |
995 | } |
998 | } |
996 | return; |
999 | return; |
997 | // if the same as the previous time |
1000 | // if the same as the previous time |
998 | }else if( number == socket_data->expected ){ |
1001 | }else if( number == socket_data->expected ){ |
999 | // increase the counter |
1002 | // increase the counter |
1000 | ++ socket_data->expected_count; |
1003 | ++ socket_data->expected_count; |
1001 | if( socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT ){ |
1004 | if( socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT ){ |
1002 | socket_data->expected_count = 1; |
1005 | socket_data->expected_count = 1; |
1003 | // TODO retransmit lock |
1006 | // TODO retransmit lock |
1004 | //tcp_retransmit_packet( socket, socket_data, number ); |
1007 | //tcp_retransmit_packet( socket, socket_data, number ); |
1005 | } |
1008 | } |
1006 | } |
1009 | } |
1007 | } |
1010 | } |
1008 | } |
1011 | } |
1009 | 1012 | ||
1010 | int tcp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
1013 | int tcp_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
1011 | ERROR_DECLARE; |
1014 | ERROR_DECLARE; |
1012 | 1015 | ||
1013 | packet_t packet; |
1016 | packet_t packet; |
1014 | 1017 | ||
1015 | assert( call ); |
1018 | assert( call ); |
1016 | assert( answer ); |
1019 | assert( answer ); |
1017 | assert( answer_count ); |
1020 | assert( answer_count ); |
1018 | 1021 | ||
1019 | * answer_count = 0; |
1022 | * answer_count = 0; |
1020 | switch( IPC_GET_METHOD( * call )){ |
1023 | switch( IPC_GET_METHOD( * call )){ |
1021 | case NET_TL_RECEIVED: |
1024 | case NET_TL_RECEIVED: |
1022 | //fibril_rwlock_read_lock( & tcp_globals.lock ); |
1025 | //fibril_rwlock_read_lock( & tcp_globals.lock ); |
1023 | if( ! ERROR_OCCURRED( packet_translate( tcp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
1026 | if( ! ERROR_OCCURRED( packet_translate( tcp_globals.net_phone, & packet, IPC_GET_PACKET( call )))){ |
1024 | ERROR_CODE = tcp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_TCP, IPC_GET_ERROR( call )); |
1027 | ERROR_CODE = tcp_received_msg( IPC_GET_DEVICE( call ), packet, SERVICE_TCP, IPC_GET_ERROR( call )); |
1025 | } |
1028 | } |
1026 | //fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1029 | //fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1027 | return ERROR_CODE; |
1030 | return ERROR_CODE; |
1028 | case IPC_M_CONNECT_TO_ME: |
1031 | case IPC_M_CONNECT_TO_ME: |
1029 | return tcp_process_client_messages( callid, * call ); |
1032 | return tcp_process_client_messages( callid, * call ); |
1030 | } |
1033 | } |
1031 | return ENOTSUP; |
1034 | return ENOTSUP; |
1032 | } |
1035 | } |
1033 | 1036 | ||
1034 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ){ |
1037 | void tcp_refresh_socket_data( tcp_socket_data_ref socket_data ){ |
1035 | assert( socket_data ); |
1038 | assert( socket_data ); |
1036 | 1039 | ||
1037 | bzero( socket_data, sizeof( * socket_data )); |
1040 | bzero( socket_data, sizeof( * socket_data )); |
1038 | socket_data->state = TCP_SOCKET_INITIAL; |
1041 | socket_data->state = TCP_SOCKET_INITIAL; |
1039 | socket_data->device_id = -1; |
1042 | socket_data->device_id = -1; |
1040 | socket_data->window = NET_DEFAULT_TCP_WINDOW; |
1043 | socket_data->window = NET_DEFAULT_TCP_WINDOW; |
1041 | socket_data->treshold = socket_data->window; |
1044 | socket_data->treshold = socket_data->window; |
1042 | socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER; |
1045 | socket_data->last_outgoing = TCP_INITIAL_SEQUENCE_NUMBER; |
1043 | socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT; |
1046 | socket_data->timeout = NET_DEFAULT_TCP_INITIAL_TIMEOUT; |
1044 | socket_data->acknowledged = socket_data->last_outgoing; |
1047 | socket_data->acknowledged = socket_data->last_outgoing; |
1045 | socket_data->next_outgoing = socket_data->last_outgoing + 1; |
1048 | socket_data->next_outgoing = socket_data->last_outgoing + 1; |
1046 | socket_data->expected = socket_data->next_outgoing; |
1049 | socket_data->expected = socket_data->next_outgoing; |
1047 | } |
1050 | } |
1048 | 1051 | ||
1049 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ){ |
1052 | void tcp_initialize_socket_data( tcp_socket_data_ref socket_data ){ |
1050 | assert( socket_data ); |
1053 | assert( socket_data ); |
1051 | 1054 | ||
1052 | tcp_refresh_socket_data( socket_data ); |
1055 | tcp_refresh_socket_data( socket_data ); |
1053 | fibril_mutex_initialize( & socket_data->operation.mutex ); |
1056 | fibril_mutex_initialize( & socket_data->operation.mutex ); |
1054 | fibril_condvar_initialize( & socket_data->operation.condvar ); |
1057 | fibril_condvar_initialize( & socket_data->operation.condvar ); |
1055 | } |
1058 | } |
1056 | 1059 | ||
1057 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){ |
1060 | int tcp_process_client_messages( ipc_callid_t callid, ipc_call_t call ){ |
1058 | int res; |
1061 | int res; |
1059 | bool keep_on_going = true; |
1062 | bool keep_on_going = true; |
1060 | socket_cores_t local_sockets; |
1063 | socket_cores_t local_sockets; |
1061 | int app_phone = IPC_GET_PHONE( & call ); |
1064 | int app_phone = IPC_GET_PHONE( & call ); |
1062 | struct sockaddr * addr; |
1065 | struct sockaddr * addr; |
1063 | size_t addrlen; |
1066 | size_t addrlen; |
1064 | fibril_rwlock_t lock; |
1067 | fibril_rwlock_t lock; |
1065 | ipc_call_t answer; |
1068 | ipc_call_t answer; |
1066 | int answer_count; |
1069 | int answer_count; |
1067 | tcp_socket_data_ref socket_data; |
1070 | tcp_socket_data_ref socket_data; |
1068 | socket_core_ref socket; |
1071 | socket_core_ref socket; |
1069 | 1072 | ||
1070 | /* |
1073 | /* |
1071 | * Accept the connection |
1074 | * Accept the connection |
1072 | * - Answer the first IPC_M_CONNECT_ME_TO call. |
1075 | * - Answer the first IPC_M_CONNECT_ME_TO call. |
1073 | */ |
1076 | */ |
1074 | ipc_answer_0( callid, EOK ); |
1077 | ipc_answer_0( callid, EOK ); |
1075 | 1078 | ||
1076 | socket_cores_initialize( & local_sockets ); |
1079 | socket_cores_initialize( & local_sockets ); |
1077 | fibril_rwlock_initialize( & lock ); |
1080 | fibril_rwlock_initialize( & lock ); |
1078 | 1081 | ||
1079 | while( keep_on_going ){ |
1082 | while( keep_on_going ){ |
1080 | // refresh data |
1083 | // refresh data |
1081 | refresh_answer( & answer, & answer_count ); |
1084 | refresh_answer( & answer, & answer_count ); |
1082 | 1085 | ||
1083 | callid = async_get_call( & call ); |
1086 | callid = async_get_call( & call ); |
1084 | // printf( "message %d\n", IPC_GET_METHOD( * call )); |
1087 | // printf( "message %d\n", IPC_GET_METHOD( * call )); |
1085 | 1088 | ||
1086 | switch( IPC_GET_METHOD( call )){ |
1089 | switch( IPC_GET_METHOD( call )){ |
1087 | case IPC_M_PHONE_HUNGUP: |
1090 | case IPC_M_PHONE_HUNGUP: |
1088 | keep_on_going = false; |
1091 | keep_on_going = false; |
1089 | res = EOK; |
1092 | res = EOK; |
1090 | break; |
1093 | break; |
1091 | case NET_SOCKET: |
1094 | case NET_SOCKET: |
1092 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data )); |
1095 | socket_data = ( tcp_socket_data_ref ) malloc( sizeof( * socket_data )); |
1093 | if( ! socket_data ){ |
1096 | if( ! socket_data ){ |
1094 | res = ENOMEM; |
1097 | res = ENOMEM; |
1095 | }else{ |
1098 | }else{ |
1096 | tcp_initialize_socket_data( socket_data ); |
1099 | tcp_initialize_socket_data( socket_data ); |
1097 | socket_data->local_lock = & lock; |
1100 | socket_data->local_lock = & lock; |
1098 | socket_data->local_sockets = & local_sockets; |
1101 | socket_data->local_sockets = & local_sockets; |
1099 | fibril_rwlock_write_lock( & lock ); |
1102 | fibril_rwlock_write_lock( & lock ); |
1100 | res = socket_create( & local_sockets, app_phone, socket_data, SOCKET_SET_SOCKET_ID( answer )); |
1103 | res = socket_create( & local_sockets, app_phone, socket_data, SOCKET_SET_SOCKET_ID( answer )); |
1101 | fibril_rwlock_write_unlock( & lock ); |
1104 | fibril_rwlock_write_unlock( & lock ); |
1102 | if( res == EOK ){ |
1105 | if( res == EOK ){ |
1103 | * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( tcp_header_t ); |
1106 | * SOCKET_SET_HEADER_SIZE( answer ) = sizeof( tcp_header_t ); |
1104 | * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE; |
1107 | * SOCKET_SET_DATA_FRAGMENT_SIZE( answer ) = MAX_TCP_FRAGMENT_SIZE; |
1105 | answer_count = 3; |
1108 | answer_count = 3; |
1106 | }else{ |
1109 | }else{ |
1107 | free( socket_data ); |
1110 | free( socket_data ); |
1108 | } |
1111 | } |
1109 | } |
1112 | } |
1110 | break; |
1113 | break; |
1111 | case NET_SOCKET_BIND: |
1114 | case NET_SOCKET_BIND: |
1112 | res = data_receive(( void ** ) & addr, & addrlen ); |
1115 | res = data_receive(( void ** ) & addr, & addrlen ); |
1113 | if( res == EOK ){ |
1116 | if( res == EOK ){ |
1114 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1117 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1115 | fibril_rwlock_write_lock( & lock ); |
1118 | fibril_rwlock_write_lock( & lock ); |
1116 | res = socket_bind( & local_sockets, & tcp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ); |
1119 | res = socket_bind( & local_sockets, & tcp_globals.sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port ); |
1117 | if( res == EOK ){ |
1120 | if( res == EOK ){ |
1118 | socket = socket_cores_find( & local_sockets, SOCKET_GET_SOCKET_ID( call )); |
1121 | socket = socket_cores_find( & local_sockets, SOCKET_GET_SOCKET_ID( call )); |
1119 | if( socket ){ |
1122 | if( socket ){ |
1120 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1123 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1121 | assert( socket_data ); |
1124 | assert( socket_data ); |
1122 | socket_data->state = TCP_SOCKET_LISTEN; |
1125 | socket_data->state = TCP_SOCKET_LISTEN; |
1123 | } |
1126 | } |
1124 | } |
1127 | } |
1125 | fibril_rwlock_write_unlock( & lock ); |
1128 | fibril_rwlock_write_unlock( & lock ); |
1126 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1129 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1127 | free( addr ); |
1130 | free( addr ); |
1128 | } |
1131 | } |
1129 | break; |
1132 | break; |
1130 | case NET_SOCKET_LISTEN: |
1133 | case NET_SOCKET_LISTEN: |
1131 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1134 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1132 | // fibril_rwlock_write_lock( & tcp_globals.lock ); |
1135 | // fibril_rwlock_write_lock( & tcp_globals.lock ); |
1133 | fibril_rwlock_write_lock( & lock ); |
1136 | fibril_rwlock_write_lock( & lock ); |
1134 | res = tcp_listen_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_BACKLOG( call )); |
1137 | res = tcp_listen_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_BACKLOG( call )); |
1135 | fibril_rwlock_write_unlock( & lock ); |
1138 | fibril_rwlock_write_unlock( & lock ); |
1136 | // fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1139 | // fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1137 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1140 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1138 | break; |
1141 | break; |
1139 | case NET_SOCKET_CONNECT: |
1142 | case NET_SOCKET_CONNECT: |
1140 | res = data_receive(( void ** ) & addr, & addrlen ); |
1143 | res = data_receive(( void ** ) & addr, & addrlen ); |
1141 | if( res == EOK ){ |
1144 | if( res == EOK ){ |
1142 | // the global lock may released in the tcp_connect_message() function |
1145 | // the global lock may released in the tcp_connect_message() function |
1143 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1146 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1144 | fibril_rwlock_write_lock( & lock ); |
1147 | fibril_rwlock_write_lock( & lock ); |
1145 | res = tcp_connect_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen ); |
1148 | res = tcp_connect_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), addr, addrlen ); |
1146 | if( res != EOK ){ |
1149 | if( res != EOK ){ |
1147 | fibril_rwlock_write_unlock( & lock ); |
1150 | fibril_rwlock_write_unlock( & lock ); |
1148 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1151 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1149 | free( addr ); |
1152 | free( addr ); |
1150 | } |
1153 | } |
1151 | } |
1154 | } |
1152 | break; |
1155 | break; |
1153 | case NET_SOCKET_ACCEPT: |
1156 | case NET_SOCKET_ACCEPT: |
1154 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1157 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1155 | fibril_rwlock_write_lock( & lock ); |
1158 | fibril_rwlock_write_lock( & lock ); |
1156 | res = tcp_accept_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), & addrlen ); |
1159 | res = tcp_accept_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), & addrlen ); |
1157 | fibril_rwlock_write_unlock( & lock ); |
1160 | fibril_rwlock_write_unlock( & lock ); |
1158 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1161 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1159 | if( res > 0 ){ |
1162 | if( res > 0 ){ |
1160 | * SOCKET_SET_SOCKET_ID( answer ) = res; |
1163 | * SOCKET_SET_SOCKET_ID( answer ) = res; |
1161 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; |
1164 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; |
1162 | answer_count = 2; |
1165 | answer_count = 2; |
1163 | } |
1166 | } |
1164 | break; |
1167 | break; |
1165 | case NET_SOCKET_SEND: |
1168 | case NET_SOCKET_SEND: |
1166 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1169 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1167 | fibril_rwlock_write_lock( & lock ); |
1170 | fibril_rwlock_write_lock( & lock ); |
1168 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call )); |
1171 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call )); |
1169 | if( res != EOK ){ |
1172 | if( res != EOK ){ |
1170 | fibril_rwlock_write_unlock( & lock ); |
1173 | fibril_rwlock_write_unlock( & lock ); |
1171 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1174 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1172 | } |
1175 | } |
1173 | break; |
1176 | break; |
1174 | case NET_SOCKET_SENDTO: |
1177 | case NET_SOCKET_SENDTO: |
1175 | res = data_receive(( void ** ) & addr, & addrlen ); |
1178 | res = data_receive(( void ** ) & addr, & addrlen ); |
1176 | if( res == EOK ){ |
1179 | if( res == EOK ){ |
1177 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1180 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1178 | fibril_rwlock_write_lock( & lock ); |
1181 | fibril_rwlock_write_lock( & lock ); |
1179 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call )); |
1182 | res = tcp_send_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_DATA_FRAGMENTS( call ), SOCKET_GET_DATA_FRAGMENT_SIZE( call ), SOCKET_GET_FLAGS( call )); |
1180 | if( res != EOK ){ |
1183 | if( res != EOK ){ |
1181 | fibril_rwlock_write_unlock( & lock ); |
1184 | fibril_rwlock_write_unlock( & lock ); |
1182 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1185 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1183 | } |
1186 | } |
1184 | free( addr ); |
1187 | free( addr ); |
1185 | } |
1188 | } |
1186 | break; |
1189 | break; |
1187 | case NET_SOCKET_RECV: |
1190 | case NET_SOCKET_RECV: |
1188 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1191 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1189 | fibril_rwlock_write_lock( & lock ); |
1192 | fibril_rwlock_write_lock( & lock ); |
1190 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), NULL ); |
1193 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), NULL ); |
1191 | fibril_rwlock_write_unlock( & lock ); |
1194 | fibril_rwlock_write_unlock( & lock ); |
1192 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1195 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1193 | if( res > 0 ){ |
1196 | if( res > 0 ){ |
1194 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; |
1197 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; |
1195 | answer_count = 1; |
1198 | answer_count = 1; |
1196 | res = EOK; |
1199 | res = EOK; |
1197 | } |
1200 | } |
1198 | break; |
1201 | break; |
1199 | case NET_SOCKET_RECVFROM: |
1202 | case NET_SOCKET_RECVFROM: |
1200 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1203 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1201 | fibril_rwlock_write_lock( & lock ); |
1204 | fibril_rwlock_write_lock( & lock ); |
1202 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen ); |
1205 | res = tcp_recvfrom_message( & local_sockets, SOCKET_GET_SOCKET_ID( call ), SOCKET_GET_FLAGS( call ), & addrlen ); |
1203 | fibril_rwlock_write_unlock( & lock ); |
1206 | fibril_rwlock_write_unlock( & lock ); |
1204 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1207 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1205 | if( res > 0 ){ |
1208 | if( res > 0 ){ |
1206 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; |
1209 | * SOCKET_SET_READ_DATA_LENGTH( answer ) = res; |
1207 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; |
1210 | * SOCKET_SET_ADDRESS_LENGTH( answer ) = addrlen; |
1208 | answer_count = 2; |
1211 | answer_count = 2; |
1209 | res = EOK; |
1212 | res = EOK; |
1210 | } |
1213 | } |
1211 | break; |
1214 | break; |
1212 | case NET_SOCKET_CLOSE: |
1215 | case NET_SOCKET_CLOSE: |
1213 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1216 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1214 | fibril_rwlock_write_lock( & lock ); |
1217 | fibril_rwlock_write_lock( & lock ); |
1215 | res = tcp_close_message( & local_sockets, SOCKET_GET_SOCKET_ID( call )); |
1218 | res = tcp_close_message( & local_sockets, SOCKET_GET_SOCKET_ID( call )); |
1216 | if( res != EOK ){ |
1219 | if( res != EOK ){ |
1217 | fibril_rwlock_write_unlock( & lock ); |
1220 | fibril_rwlock_write_unlock( & lock ); |
1218 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1221 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1219 | } |
1222 | } |
1220 | break; |
1223 | break; |
1221 | case NET_SOCKET_GETSOCKOPT: |
1224 | case NET_SOCKET_GETSOCKOPT: |
1222 | case NET_SOCKET_SETSOCKOPT: |
1225 | case NET_SOCKET_SETSOCKOPT: |
1223 | default: |
1226 | default: |
1224 | res = ENOTSUP; |
1227 | res = ENOTSUP; |
1225 | break; |
1228 | break; |
1226 | } |
1229 | } |
1227 | 1230 | ||
1228 | // printf( "res = %d\n", res ); |
1231 | // printf( "res = %d\n", res ); |
1229 | 1232 | ||
1230 | answer_call( callid, res, & answer, answer_count ); |
1233 | answer_call( callid, res, & answer, answer_count ); |
1231 | } |
1234 | } |
1232 | 1235 | ||
1233 | printf("release\n"); |
1236 | printf("release\n"); |
1234 | // release all local sockets |
1237 | // release all local sockets |
1235 | socket_cores_release( tcp_globals.net_phone, & local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
1238 | socket_cores_release( tcp_globals.net_phone, & local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
1236 | 1239 | ||
1237 | return EOK; |
1240 | return EOK; |
1238 | } |
1241 | } |
1239 | 1242 | ||
1240 | int tcp_timeout( void * data ){ |
1243 | int tcp_timeout( void * data ){ |
1241 | tcp_timeout_ref timeout = data; |
1244 | tcp_timeout_ref timeout = data; |
1242 | int keep_write_lock = false; |
1245 | int keep_write_lock = false; |
1243 | socket_core_ref socket; |
1246 | socket_core_ref socket; |
1244 | tcp_socket_data_ref socket_data; |
1247 | tcp_socket_data_ref socket_data; |
1245 | 1248 | ||
1246 | assert( timeout ); |
1249 | assert( timeout ); |
1247 | 1250 | ||
1248 | // sleep the given timeout |
1251 | // sleep the given timeout |
1249 | async_usleep( timeout->timeout ); |
1252 | async_usleep( timeout->timeout ); |
1250 | // lock the globals |
1253 | // lock the globals |
1251 | if( timeout->globals_read_only ){ |
1254 | if( timeout->globals_read_only ){ |
1252 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1255 | fibril_rwlock_read_lock( & tcp_globals.lock ); |
1253 | }else{ |
1256 | }else{ |
1254 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1257 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1255 | } |
1258 | } |
1256 | // find the pending operation socket |
1259 | // find the pending operation socket |
1257 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length ); |
1260 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length ); |
1258 | if( socket && ( socket->socket_id == timeout->socket_id )){ |
1261 | if( socket && ( socket->socket_id == timeout->socket_id )){ |
1259 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1262 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1260 | assert( socket_data ); |
1263 | assert( socket_data ); |
1261 | if( socket_data->local_sockets == timeout->local_sockets ){ |
1264 | if( socket_data->local_sockets == timeout->local_sockets ){ |
1262 | fibril_rwlock_write_lock( socket_data->local_lock ); |
1265 | fibril_rwlock_write_lock( socket_data->local_lock ); |
1263 | if( timeout->sequence_number ){ |
1266 | if( timeout->sequence_number ){ |
1264 | // increase the timeout counter; |
1267 | // increase the timeout counter; |
1265 | ++ socket_data->timeout_count; |
1268 | ++ socket_data->timeout_count; |
1266 | if( socket_data->timeout_count == TCP_MAX_TIMEOUTS ){ |
1269 | if( socket_data->timeout_count == TCP_MAX_TIMEOUTS ){ |
1267 | // TODO release as connection lost |
1270 | // TODO release as connection lost |
1268 | //tcp_refresh_socket_data( socket_data ); |
1271 | //tcp_refresh_socket_data( socket_data ); |
1269 | } |
1272 | } |
1270 | // retransmit |
1273 | // retransmit |
1271 | // TODO enable retransmit |
1274 | // TODO enable retransmit |
1272 | //tcp_retransmit_packet( socket, socket_data, timeout->sequence_number ); |
1275 | //tcp_retransmit_packet( socket, socket_data, timeout->sequence_number ); |
1273 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1276 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1274 | }else{ |
1277 | }else{ |
1275 | fibril_mutex_lock( & socket_data->operation.mutex ); |
1278 | fibril_mutex_lock( & socket_data->operation.mutex ); |
1276 | // set the timeout operation result if state not changed |
1279 | // set the timeout operation result if state not changed |
1277 | if( socket_data->state == timeout->state ){ |
1280 | if( socket_data->state == timeout->state ){ |
1278 | socket_data->operation.result = ETIMEOUT; |
1281 | socket_data->operation.result = ETIMEOUT; |
1279 | // notify the main fibril |
1282 | // notify the main fibril |
1280 | fibril_condvar_signal( & socket_data->operation.condvar ); |
1283 | fibril_condvar_signal( & socket_data->operation.condvar ); |
1281 | // keep the global write lock |
1284 | // keep the global write lock |
1282 | keep_write_lock = true; |
1285 | keep_write_lock = true; |
1283 | }else{ |
1286 | }else{ |
1284 | // operation is ok, do nothing |
1287 | // operation is ok, do nothing |
1285 | // unlocking from now on, so the unlock order does not matter... |
1288 | // unlocking from now on, so the unlock order does not matter... |
1286 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1289 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1287 | } |
1290 | } |
1288 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
1291 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
1289 | } |
1292 | } |
1290 | } |
1293 | } |
1291 | } |
1294 | } |
1292 | // unlock only if no socket |
1295 | // unlock only if no socket |
1293 | if( timeout->globals_read_only ){ |
1296 | if( timeout->globals_read_only ){ |
1294 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1297 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1295 | }else if( ! keep_write_lock ){ |
1298 | }else if( ! keep_write_lock ){ |
1296 | // release if not desired |
1299 | // release if not desired |
1297 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1300 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1298 | } |
1301 | } |
1299 | // release the timeout structure |
1302 | // release the timeout structure |
1300 | free( timeout ); |
1303 | free( timeout ); |
1301 | return EOK; |
1304 | return EOK; |
1302 | } |
1305 | } |
1303 | 1306 | ||
1304 | int tcp_release_after_timeout( void * data ){ |
1307 | int tcp_release_after_timeout( void * data ){ |
1305 | tcp_timeout_ref timeout = data; |
1308 | tcp_timeout_ref timeout = data; |
1306 | socket_core_ref socket; |
1309 | socket_core_ref socket; |
1307 | tcp_socket_data_ref socket_data; |
1310 | tcp_socket_data_ref socket_data; |
1308 | fibril_rwlock_t * local_lock; |
1311 | fibril_rwlock_t * local_lock; |
1309 | 1312 | ||
1310 | assert( timeout ); |
1313 | assert( timeout ); |
1311 | 1314 | ||
1312 | // sleep the given timeout |
1315 | // sleep the given timeout |
1313 | async_usleep( timeout->timeout ); |
1316 | async_usleep( timeout->timeout ); |
1314 | // lock the globals |
1317 | // lock the globals |
1315 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1318 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1316 | // find the pending operation socket |
1319 | // find the pending operation socket |
1317 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length ); |
1320 | socket = socket_port_find( & tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length ); |
1318 | if( socket && ( socket->socket_id == timeout->socket_id )){ |
1321 | if( socket && ( socket->socket_id == timeout->socket_id )){ |
1319 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1322 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1320 | assert( socket_data ); |
1323 | assert( socket_data ); |
1321 | if( socket_data->local_sockets == timeout->local_sockets ){ |
1324 | if( socket_data->local_sockets == timeout->local_sockets ){ |
1322 | local_lock = socket_data->local_lock; |
1325 | local_lock = socket_data->local_lock; |
1323 | fibril_rwlock_write_lock( local_lock ); |
1326 | fibril_rwlock_write_lock( local_lock ); |
1324 | socket_destroy( tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
1327 | socket_destroy( tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, & tcp_globals.sockets, tcp_free_socket_data ); |
1325 | fibril_rwlock_write_unlock( local_lock ); |
1328 | fibril_rwlock_write_unlock( local_lock ); |
1326 | } |
1329 | } |
1327 | } |
1330 | } |
1328 | // unlock the globals |
1331 | // unlock the globals |
1329 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1332 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1330 | // release the timeout structure |
1333 | // release the timeout structure |
1331 | free( timeout ); |
1334 | free( timeout ); |
1332 | return EOK; |
1335 | return EOK; |
1333 | } |
1336 | } |
1334 | 1337 | ||
1335 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ){ |
1338 | void tcp_retransmit_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number ){ |
1336 | packet_t packet; |
1339 | packet_t packet; |
1337 | packet_t copy; |
1340 | packet_t copy; |
1338 | size_t data_length; |
1341 | size_t data_length; |
1339 | 1342 | ||
1340 | assert( socket ); |
1343 | assert( socket ); |
1341 | assert( socket_data ); |
1344 | assert( socket_data ); |
1342 | assert( socket->specific_data == socket_data ); |
1345 | assert( socket->specific_data == socket_data ); |
1343 | 1346 | ||
1344 | // sent packet? |
1347 | // sent packet? |
1345 | packet = pq_find( socket_data->outgoing, sequence_number ); |
1348 | packet = pq_find( socket_data->outgoing, sequence_number ); |
1346 | printf("retransmit %d\n", packet_get_id( packet )); |
1349 | printf("retransmit %d\n", packet_get_id( packet )); |
1347 | if( packet ){ |
1350 | if( packet ){ |
1348 | pq_get_order( packet, NULL, & data_length ); |
1351 | pq_get_order( packet, NULL, & data_length ); |
1349 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, sequence_number ); |
1352 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, sequence_number ); |
1350 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1353 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1351 | // printf( "r send %d\n", packet_get_id( packet )); |
1354 | // printf( "r send %d\n", packet_get_id( packet )); |
1352 | if( copy ){ |
1355 | if( copy ){ |
1353 | tcp_send_packets( socket_data->device_id, copy ); |
1356 | tcp_send_packets( socket_data->device_id, copy ); |
1354 | } |
1357 | } |
1355 | }else{ |
1358 | }else{ |
1356 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1359 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1357 | } |
1360 | } |
1358 | } |
1361 | } |
1359 | 1362 | ||
1360 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ){ |
1363 | int tcp_listen_message( socket_cores_ref local_sockets, int socket_id, int backlog ){ |
1361 | socket_core_ref socket; |
1364 | socket_core_ref socket; |
1362 | tcp_socket_data_ref socket_data; |
1365 | tcp_socket_data_ref socket_data; |
1363 | 1366 | ||
1364 | assert( local_sockets ); |
1367 | assert( local_sockets ); |
1365 | 1368 | ||
1366 | if( backlog < 0 ) return EINVAL; |
1369 | if( backlog < 0 ) return EINVAL; |
1367 | // find the socket |
1370 | // find the socket |
1368 | socket = socket_cores_find( local_sockets, socket_id ); |
1371 | socket = socket_cores_find( local_sockets, socket_id ); |
1369 | if( ! socket ) return ENOTSOCK; |
1372 | if( ! socket ) return ENOTSOCK; |
1370 | // get the socket specific data |
1373 | // get the socket specific data |
1371 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1374 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1372 | assert( socket_data ); |
1375 | assert( socket_data ); |
1373 | // set the backlog |
1376 | // set the backlog |
1374 | socket_data->backlog = backlog; |
1377 | socket_data->backlog = backlog; |
1375 | return EOK; |
1378 | return EOK; |
1376 | } |
1379 | } |
1377 | 1380 | ||
1378 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ){ |
1381 | int tcp_connect_message( socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen ){ |
1379 | ERROR_DECLARE; |
1382 | ERROR_DECLARE; |
1380 | 1383 | ||
1381 | socket_core_ref socket; |
1384 | socket_core_ref socket; |
1382 | 1385 | ||
1383 | assert( local_sockets ); |
1386 | assert( local_sockets ); |
1384 | assert( addr ); |
1387 | assert( addr ); |
1385 | assert( addrlen > 0 ); |
1388 | assert( addrlen > 0 ); |
1386 | 1389 | ||
1387 | // find the socket |
1390 | // find the socket |
1388 | socket = socket_cores_find( local_sockets, socket_id ); |
1391 | socket = socket_cores_find( local_sockets, socket_id ); |
1389 | if( ! socket ) return ENOTSOCK; |
1392 | if( ! socket ) return ENOTSOCK; |
1390 | if( ERROR_OCCURRED( tcp_connect_core( socket, local_sockets, addr, addrlen ))){ |
1393 | if( ERROR_OCCURRED( tcp_connect_core( socket, local_sockets, addr, addrlen ))){ |
1391 | tcp_free_socket_data( socket ); |
1394 | tcp_free_socket_data( socket ); |
1392 | // unbind if bound |
1395 | // unbind if bound |
1393 | if( socket->port > 0 ){ |
1396 | if( socket->port > 0 ){ |
1394 | socket_ports_exclude( & tcp_globals.sockets, socket->port ); |
1397 | socket_ports_exclude( & tcp_globals.sockets, socket->port ); |
1395 | socket->port = 0; |
1398 | socket->port = 0; |
1396 | } |
1399 | } |
1397 | } |
1400 | } |
1398 | return ERROR_CODE; |
1401 | return ERROR_CODE; |
1399 | } |
1402 | } |
1400 | 1403 | ||
1401 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ){ |
1404 | int tcp_connect_core( socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen ){ |
1402 | ERROR_DECLARE; |
1405 | ERROR_DECLARE; |
1403 | 1406 | ||
1404 | tcp_socket_data_ref socket_data; |
1407 | tcp_socket_data_ref socket_data; |
1405 | packet_t packet; |
1408 | packet_t packet; |
1406 | 1409 | ||
1407 | assert( socket ); |
1410 | assert( socket ); |
1408 | assert( addr ); |
1411 | assert( addr ); |
1409 | assert( addrlen > 0 ); |
1412 | assert( addrlen > 0 ); |
1410 | 1413 | ||
1411 | // get the socket specific data |
1414 | // get the socket specific data |
1412 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1415 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1413 | assert( socket_data ); |
1416 | assert( socket_data ); |
1414 | assert( socket->specific_data == socket_data ); |
1417 | assert( socket->specific_data == socket_data ); |
1415 | if(( socket_data->state != TCP_SOCKET_INITIAL ) |
1418 | if(( socket_data->state != TCP_SOCKET_INITIAL ) |
1416 | && (( socket_data->state != TCP_SOCKET_LISTEN ) || ( socket->port <= 0 ))){ |
1419 | && (( socket_data->state != TCP_SOCKET_LISTEN ) || ( socket->port <= 0 ))){ |
1417 | return EINVAL; |
1420 | return EINVAL; |
1418 | } |
1421 | } |
1419 | // get the destination port |
1422 | // get the destination port |
1420 | ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & socket_data->dest_port )); |
1423 | ERROR_PROPAGATE( tl_get_address_port( addr, addrlen, & socket_data->dest_port )); |
1421 | if( socket->port <= 0 ){ |
1424 | if( socket->port <= 0 ){ |
1422 | // try to find a free port |
1425 | // try to find a free port |
1423 | ERROR_PROPAGATE( socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port )); |
1426 | ERROR_PROPAGATE( socket_bind_free_port( & tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port )); |
1424 | // set the next port as the search starting port number |
1427 | // set the next port as the search starting port number |
1425 | tcp_globals.last_used_port = socket->port; |
1428 | tcp_globals.last_used_port = socket->port; |
1426 | } |
1429 | } |
1427 | ERROR_PROPAGATE( ip_get_route_req( tcp_globals.ip_phone, IPPROTO_TCP, addr, addrlen, & socket_data->device_id, & socket_data->pseudo_header, & socket_data->headerlen )); |
1430 | ERROR_PROPAGATE( ip_get_route_req( tcp_globals.ip_phone, IPPROTO_TCP, addr, addrlen, & socket_data->device_id, & socket_data->pseudo_header, & socket_data->headerlen )); |
1428 | 1431 | ||
1429 | // create the notification packet |
1432 | // create the notification packet |
1430 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 1, 0 )); |
1433 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 1, 0 )); |
1431 | 1434 | ||
1432 | // unlock the globals and wait for an operation |
1435 | // unlock the globals and wait for an operation |
1433 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1436 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1434 | 1437 | ||
1435 | socket_data->addr = addr; |
1438 | socket_data->addr = addr; |
1436 | socket_data->addrlen = addrlen; |
1439 | socket_data->addrlen = addrlen; |
1437 | // send the packet |
1440 | // send the packet |
1438 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 )) |
1441 | if( ERROR_OCCURRED( tcp_queue_packet( socket, socket_data, packet, 1 )) |
1439 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false ))){ |
1442 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false ))){ |
1440 | socket_data->addr = NULL; |
1443 | socket_data->addr = NULL; |
1441 | socket_data->addrlen = 0; |
1444 | socket_data->addrlen = 0; |
1442 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1445 | fibril_rwlock_write_lock( & tcp_globals.lock ); |
1443 | }else{ |
1446 | }else{ |
1444 | packet = tcp_get_packets_to_send( socket, socket_data ); |
1447 | packet = tcp_get_packets_to_send( socket, socket_data ); |
1445 | if( packet ){ |
1448 | if( packet ){ |
1446 | fibril_mutex_lock( & socket_data->operation.mutex ); |
1449 | fibril_mutex_lock( & socket_data->operation.mutex ); |
1447 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1450 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1448 | // send the packet |
1451 | // send the packet |
1449 | printf( "connecting %d\n", packet_get_id( packet )); |
1452 | printf( "connecting %d\n", packet_get_id( packet )); |
1450 | tcp_send_packets( socket_data->device_id, packet ); |
1453 | tcp_send_packets( socket_data->device_id, packet ); |
1451 | // wait for a reply |
1454 | // wait for a reply |
1452 | fibril_condvar_wait( & socket_data->operation.condvar, & socket_data->operation.mutex ); |
1455 | fibril_condvar_wait( & socket_data->operation.condvar, & socket_data->operation.mutex ); |
1453 | ERROR_CODE = socket_data->operation.result; |
1456 | ERROR_CODE = socket_data->operation.result; |
1454 | if( ERROR_CODE != EOK ){ |
1457 | if( ERROR_CODE != EOK ){ |
1455 | socket_data->addr = NULL; |
1458 | socket_data->addr = NULL; |
1456 | socket_data->addrlen = 0; |
1459 | socket_data->addrlen = 0; |
1457 | } |
1460 | } |
1458 | }else{ |
1461 | }else{ |
1459 | socket_data->addr = NULL; |
1462 | socket_data->addr = NULL; |
1460 | socket_data->addrlen = 0; |
1463 | socket_data->addrlen = 0; |
1461 | ERROR_CODE = EINTR; |
1464 | ERROR_CODE = EINTR; |
1462 | } |
1465 | } |
1463 | } |
1466 | } |
1464 | 1467 | ||
1465 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
1468 | fibril_mutex_unlock( & socket_data->operation.mutex ); |
1466 | 1469 | ||
1467 | // return the result |
1470 | // return the result |
1468 | return ERROR_CODE; |
1471 | return ERROR_CODE; |
1469 | } |
1472 | } |
1470 | 1473 | ||
1471 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){ |
1474 | int tcp_queue_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){ |
1472 | ERROR_DECLARE; |
1475 | ERROR_DECLARE; |
1473 | 1476 | ||
1474 | tcp_header_ref header; |
1477 | tcp_header_ref header; |
1475 | 1478 | ||
1476 | assert( socket ); |
1479 | assert( socket ); |
1477 | assert( socket_data ); |
1480 | assert( socket_data ); |
1478 | assert( socket->specific_data == socket_data ); |
1481 | assert( socket->specific_data == socket_data ); |
1479 | 1482 | ||
1480 | // get tcp header |
1483 | // get tcp header |
1481 | header = ( tcp_header_ref ) packet_get_data( packet ); |
1484 | header = ( tcp_header_ref ) packet_get_data( packet ); |
1482 | if( ! header ) return NO_DATA; |
1485 | if( ! header ) return NO_DATA; |
1483 | header->destination_port = htons( socket_data->dest_port ); |
1486 | header->destination_port = htons( socket_data->dest_port ); |
1484 | header->source_port = htons( socket->port ); |
1487 | header->source_port = htons( socket->port ); |
1485 | header->sequence_number = htonl( socket_data->next_outgoing ); |
1488 | header->sequence_number = htonl( socket_data->next_outgoing ); |
1486 | if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) socket_data->addr, socket_data->addrlen ))){ |
1489 | if( ERROR_OCCURRED( packet_set_addr( packet, NULL, ( uint8_t * ) socket_data->addr, socket_data->addrlen ))){ |
1487 | return tcp_release_and_return( packet, EINVAL ); |
1490 | return tcp_release_and_return( packet, EINVAL ); |
1488 | } |
1491 | } |
1489 | // remember the outgoing FIN |
1492 | // remember the outgoing FIN |
1490 | if( header->finalize ){ |
1493 | if( header->finalize ){ |
1491 | socket_data->fin_outgoing = socket_data->next_outgoing; |
1494 | socket_data->fin_outgoing = socket_data->next_outgoing; |
1492 | } |
1495 | } |
1493 | return EOK; |
1496 | return EOK; |
1494 | } |
1497 | } |
1495 | 1498 | ||
1496 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){ |
1499 | int tcp_queue_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length ){ |
1497 | ERROR_DECLARE; |
1500 | ERROR_DECLARE; |
1498 | packet_t first; |
1501 | packet_t first; |
1499 | 1502 | ||
1500 | assert( socket ); |
1503 | assert( socket ); |
1501 | assert( socket_data ); |
1504 | assert( socket_data ); |
1502 | assert( socket->specific_data == socket_data ); |
1505 | assert( socket->specific_data == socket_data ); |
1503 | 1506 | ||
1504 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, data_length )); |
1507 | ERROR_PROPAGATE( tcp_queue_prepare_packet( socket, socket_data, packet, data_length )); |
1505 | 1508 | ||
1506 | first = pq_add( socket_data->outgoing, packet, socket_data->next_outgoing, data_length ); |
1509 | first = pq_add( socket_data->outgoing, packet, socket_data->next_outgoing, data_length ); |
1507 | if( ! first ){ |
1510 | if( ! first ){ |
1508 | return tcp_release_and_return( packet, EINVAL ); |
1511 | return tcp_release_and_return( packet, EINVAL ); |
1509 | } |
1512 | } |
1510 | socket_data->outgoing = first; |
1513 | socket_data->outgoing = first; |
1511 | socket_data->next_outgoing += data_length; |
1514 | socket_data->next_outgoing += data_length; |
1512 | return EOK; |
1515 | return EOK; |
1513 | } |
1516 | } |
1514 | 1517 | ||
1515 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ){ |
1518 | packet_t tcp_get_packets_to_send( socket_core_ref socket, tcp_socket_data_ref socket_data ){ |
1516 | ERROR_DECLARE; |
1519 | ERROR_DECLARE; |
1517 | 1520 | ||
1518 | packet_t packet; |
1521 | packet_t packet; |
1519 | packet_t copy; |
1522 | packet_t copy; |
1520 | packet_t sending = NULL; |
1523 | packet_t sending = NULL; |
1521 | packet_t previous = NULL; |
1524 | packet_t previous = NULL; |
1522 | size_t data_length; |
1525 | size_t data_length; |
1523 | 1526 | ||
1524 | assert( socket ); |
1527 | assert( socket ); |
1525 | assert( socket_data ); |
1528 | assert( socket_data ); |
1526 | assert( socket->specific_data == socket_data ); |
1529 | assert( socket->specific_data == socket_data ); |
1527 | 1530 | ||
1528 | packet = pq_find( socket_data->outgoing, socket_data->last_outgoing + 1 ); |
1531 | packet = pq_find( socket_data->outgoing, socket_data->last_outgoing + 1 ); |
1529 | while( packet ){ |
1532 | while( packet ){ |
1530 | pq_get_order( packet, NULL, & data_length ); |
1533 | pq_get_order( packet, NULL, & data_length ); |
1531 | // send only if fits into the window |
1534 | // send only if fits into the window |
1532 | // respecting the possible overflow |
1535 | // respecting the possible overflow |
1533 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) socket_data->last_outgoing, ( uint32_t )( socket_data->last_outgoing + data_length ), ( uint32_t )( socket_data->expected + socket_data->treshold ))){ |
1536 | if( IS_IN_INTERVAL_OVERFLOW(( uint32_t ) socket_data->last_outgoing, ( uint32_t )( socket_data->last_outgoing + data_length ), ( uint32_t )( socket_data->expected + socket_data->treshold ))){ |
1534 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, socket_data->last_outgoing + 1 ); |
1537 | copy = tcp_prepare_copy( socket, socket_data, packet, data_length, socket_data->last_outgoing + 1 ); |
1535 | if( ! copy ){ |
1538 | if( ! copy ){ |
1536 | return sending; |
1539 | return sending; |
1537 | } |
1540 | } |
1538 | if( ! sending ){ |
1541 | if( ! sending ){ |
1539 | sending = copy; |
1542 | sending = copy; |
1540 | }else{ |
1543 | }else{ |
1541 | if( ERROR_OCCURRED( pq_insert_after( previous, copy ))){ |
1544 | if( ERROR_OCCURRED( pq_insert_after( previous, copy ))){ |
1542 | pq_release( tcp_globals.net_phone, packet_get_id( copy )); |
1545 | pq_release( tcp_globals.net_phone, packet_get_id( copy )); |
1543 | return sending; |
1546 | return sending; |
1544 | } |
1547 | } |
1545 | } |
1548 | } |
1546 | previous = copy; |
1549 | previous = copy; |
1547 | packet = pq_next( packet ); |
1550 | packet = pq_next( packet ); |
1548 | // overflow occurred ? |
1551 | // overflow occurred ? |
1549 | if(( ! packet ) && ( socket_data->last_outgoing > socket_data->next_outgoing )){ |
1552 | if(( ! packet ) && ( socket_data->last_outgoing > socket_data->next_outgoing )){ |
1550 | printf("gpts overflow\n"); |
1553 | printf("gpts overflow\n"); |
1551 | // continue from the beginning |
1554 | // continue from the beginning |
1552 | packet = socket_data->outgoing; |
1555 | packet = socket_data->outgoing; |
1553 | } |
1556 | } |
1554 | socket_data->last_outgoing += data_length; |
1557 | socket_data->last_outgoing += data_length; |
1555 | }else{ |
1558 | }else{ |
1556 | break; |
1559 | break; |
1557 | } |
1560 | } |
1558 | } |
1561 | } |
1559 | return sending; |
1562 | return sending; |
1560 | } |
1563 | } |
1561 | 1564 | ||
1562 | packet_t tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ){ |
1565 | packet_t tcp_send_prepare_packet( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ){ |
1563 | ERROR_DECLARE; |
1566 | ERROR_DECLARE; |
1564 | 1567 | ||
1565 | tcp_header_ref header; |
1568 | tcp_header_ref header; |
1566 | uint32_t checksum; |
1569 | uint32_t checksum; |
1567 | 1570 | ||
1568 | assert( socket ); |
1571 | assert( socket ); |
1569 | assert( socket_data ); |
1572 | assert( socket_data ); |
1570 | assert( socket->specific_data == socket_data ); |
1573 | assert( socket->specific_data == socket_data ); |
1571 | 1574 | ||
1572 | // adjust the pseudo header |
1575 | // adjust the pseudo header |
1573 | if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length( packet )))){ |
1576 | if( ERROR_OCCURRED( ip_client_set_pseudo_header_data_length( socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length( packet )))){ |
1574 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1577 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1575 | return NULL; |
1578 | return NULL; |
1576 | } |
1579 | } |
1577 | 1580 | ||
1578 | // get the header |
1581 | // get the header |
1579 | header = ( tcp_header_ref ) packet_get_data( packet ); |
1582 | header = ( tcp_header_ref ) packet_get_data( packet ); |
1580 | if( ! header ){ |
1583 | if( ! header ){ |
1581 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1584 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1582 | return NULL; |
1585 | return NULL; |
1583 | } |
1586 | } |
1584 | assert( ntohl( header->sequence_number ) == sequence_number ); |
1587 | assert( ntohl( header->sequence_number ) == sequence_number ); |
1585 | 1588 | ||
1586 | // adjust the header |
1589 | // adjust the header |
1587 | if( socket_data->next_incoming ){ |
1590 | if( socket_data->next_incoming ){ |
1588 | header->acknowledgement_number = htonl( socket_data->next_incoming ); |
1591 | header->acknowledgement_number = htonl( socket_data->next_incoming ); |
1589 | header->acknowledge = 1; |
1592 | header->acknowledge = 1; |
1590 | } |
1593 | } |
1591 | header->window = htons( socket_data->window ); |
1594 | header->window = htons( socket_data->window ); |
1592 | 1595 | ||
1593 | // checksum |
1596 | // checksum |
1594 | header->checksum = 0; |
1597 | header->checksum = 0; |
1595 | checksum = compute_checksum( 0, socket_data->pseudo_header, socket_data->headerlen ); |
1598 | checksum = compute_checksum( 0, socket_data->pseudo_header, socket_data->headerlen ); |
1596 | checksum = compute_checksum( checksum, ( uint8_t * ) packet_get_data( packet ), packet_get_data_length( packet )); |
1599 | checksum = compute_checksum( checksum, ( uint8_t * ) packet_get_data( packet ), packet_get_data_length( packet )); |
1597 | header->checksum = htons( flip_checksum( compact_checksum( checksum ))); |
1600 | header->checksum = htons( flip_checksum( compact_checksum( checksum ))); |
1598 | // prepare the packet |
1601 | // prepare the packet |
1599 | if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_TCP, 0, 0, 0, 0 )) |
1602 | if( ERROR_OCCURRED( ip_client_prepare_packet( packet, IPPROTO_TCP, 0, 0, 0, 0 )) |
1600 | // prepare the timeout |
1603 | // prepare the timeout |
1601 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true ))){ |
1604 | || ERROR_OCCURRED( tcp_prepare_timeout( tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true ))){ |
1602 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1605 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1603 | return NULL; |
1606 | return NULL; |
1604 | } |
1607 | } |
1605 | return packet; |
1608 | return packet; |
1606 | } |
1609 | } |
1607 | 1610 | ||
1608 | packet_t tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ){ |
1611 | packet_t tcp_prepare_copy( socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, size_t sequence_number ){ |
1609 | packet_t copy; |
1612 | packet_t copy; |
1610 | 1613 | ||
1611 | assert( socket ); |
1614 | assert( socket ); |
1612 | assert( socket_data ); |
1615 | assert( socket_data ); |
1613 | assert( socket->specific_data == socket_data ); |
1616 | assert( socket->specific_data == socket_data ); |
1614 | 1617 | ||
1615 | // make a copy of the packet |
1618 | // make a copy of the packet |
1616 | copy = packet_get_copy( tcp_globals.net_phone, packet ); |
1619 | copy = packet_get_copy( tcp_globals.net_phone, packet ); |
1617 | if( ! copy ) return NULL; |
1620 | if( ! copy ) return NULL; |
1618 | 1621 | ||
1619 | return tcp_send_prepare_packet( socket, socket_data, copy, data_length, sequence_number ); |
1622 | return tcp_send_prepare_packet( socket, socket_data, copy, data_length, sequence_number ); |
1620 | } |
1623 | } |
1621 | 1624 | ||
1622 | void tcp_send_packets( device_id_t device_id, packet_t packet ){ |
1625 | void tcp_send_packets( device_id_t device_id, packet_t packet ){ |
1623 | packet_t next; |
1626 | packet_t next; |
1624 | 1627 | ||
1625 | while( packet ){ |
1628 | while( packet ){ |
1626 | next = pq_detach( packet ); |
1629 | next = pq_detach( packet ); |
1627 | ip_send_msg( tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0 ); |
1630 | ip_send_msg( tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0 ); |
1628 | packet = next; |
1631 | packet = next; |
1629 | } |
1632 | } |
1630 | } |
1633 | } |
1631 | 1634 | ||
1632 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ){ |
1635 | void tcp_prepare_operation_header( socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize ){ |
1633 | assert( socket ); |
1636 | assert( socket ); |
1634 | assert( socket_data ); |
1637 | assert( socket_data ); |
1635 | assert( socket->specific_data == socket_data ); |
1638 | assert( socket->specific_data == socket_data ); |
1636 | assert( header ); |
1639 | assert( header ); |
1637 | 1640 | ||
1638 | bzero( header, sizeof( * header )); |
1641 | bzero( header, sizeof( * header )); |
1639 | header->source_port = htons( socket->port ); |
1642 | header->source_port = htons( socket->port ); |
1640 | header->source_port = htons( socket_data->dest_port ); |
1643 | header->source_port = htons( socket_data->dest_port ); |
1641 | header->header_length = TCP_COMPUTE_HEADER_LENGTH( sizeof( * header )); |
1644 | header->header_length = TCP_COMPUTE_HEADER_LENGTH( sizeof( * header )); |
1642 | header->synchronize = synchronize; |
1645 | header->synchronize = synchronize; |
1643 | header->finalize = finalize; |
1646 | header->finalize = finalize; |
1644 | } |
1647 | } |
1645 | 1648 | ||
1646 | int tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only ){ |
1649 | int tcp_prepare_timeout( int ( * timeout_function )( void * tcp_timeout_t ), socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, int globals_read_only ){ |
1647 | tcp_timeout_ref operation_timeout; |
1650 | tcp_timeout_ref operation_timeout; |
1648 | fid_t fibril; |
1651 | fid_t fibril; |
1649 | 1652 | ||
1650 | assert( socket ); |
1653 | assert( socket ); |
1651 | assert( socket_data ); |
1654 | assert( socket_data ); |
1652 | assert( socket->specific_data == socket_data ); |
1655 | assert( socket->specific_data == socket_data ); |
1653 | 1656 | ||
1654 | // prepare the timeout with key bundle structure |
1657 | // prepare the timeout with key bundle structure |
1655 | operation_timeout = malloc( sizeof( * operation_timeout ) + socket->key_length + 1 ); |
1658 | operation_timeout = malloc( sizeof( * operation_timeout ) + socket->key_length + 1 ); |
1656 | if( ! operation_timeout ) return ENOMEM; |
1659 | if( ! operation_timeout ) return ENOMEM; |
1657 | bzero( operation_timeout, sizeof( * operation_timeout )); |
1660 | bzero( operation_timeout, sizeof( * operation_timeout )); |
1658 | operation_timeout->globals_read_only = globals_read_only; |
1661 | operation_timeout->globals_read_only = globals_read_only; |
1659 | operation_timeout->port = socket->port; |
1662 | operation_timeout->port = socket->port; |
1660 | operation_timeout->local_sockets = socket_data->local_sockets; |
1663 | operation_timeout->local_sockets = socket_data->local_sockets; |
1661 | operation_timeout->socket_id = socket->socket_id; |
1664 | operation_timeout->socket_id = socket->socket_id; |
1662 | operation_timeout->timeout = timeout; |
1665 | operation_timeout->timeout = timeout; |
1663 | operation_timeout->sequence_number = sequence_number; |
1666 | operation_timeout->sequence_number = sequence_number; |
1664 | operation_timeout->state = state; |
1667 | operation_timeout->state = state; |
1665 | 1668 | ||
1666 | // copy the key |
1669 | // copy the key |
1667 | operation_timeout->key = (( char * ) operation_timeout ) + sizeof( * operation_timeout ); |
1670 | operation_timeout->key = (( char * ) operation_timeout ) + sizeof( * operation_timeout ); |
1668 | operation_timeout->key_length = socket->key_length; |
1671 | operation_timeout->key_length = socket->key_length; |
1669 | memcpy( operation_timeout->key, socket->key, socket->key_length ); |
1672 | memcpy( operation_timeout->key, socket->key, socket->key_length ); |
1670 | operation_timeout->key[ operation_timeout->key_length ] = '\0'; |
1673 | operation_timeout->key[ operation_timeout->key_length ] = '\0'; |
1671 | 1674 | ||
1672 | // prepare the timeouting thread |
1675 | // prepare the timeouting thread |
1673 | fibril = fibril_create( timeout_function, operation_timeout ); |
1676 | fibril = fibril_create( timeout_function, operation_timeout ); |
1674 | if( ! fibril ){ |
1677 | if( ! fibril ){ |
1675 | free( operation_timeout ); |
1678 | free( operation_timeout ); |
1676 | return EPARTY; |
1679 | return EPARTY; |
1677 | } |
1680 | } |
1678 | // fibril_mutex_lock( & socket_data->operation.mutex ); |
1681 | // fibril_mutex_lock( & socket_data->operation.mutex ); |
1679 | // start the timeouting fibril |
1682 | // start the timeouting fibril |
1680 | fibril_add_ready( fibril ); |
1683 | fibril_add_ready( fibril ); |
1681 | //socket_data->state = state; |
1684 | //socket_data->state = state; |
1682 | return EOK; |
1685 | return EOK; |
1683 | } |
1686 | } |
1684 | 1687 | ||
1685 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){ |
1688 | int tcp_recvfrom_message( socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen ){ |
1686 | ERROR_DECLARE; |
1689 | ERROR_DECLARE; |
1687 | 1690 | ||
1688 | socket_core_ref socket; |
1691 | socket_core_ref socket; |
1689 | tcp_socket_data_ref socket_data; |
1692 | tcp_socket_data_ref socket_data; |
1690 | int packet_id; |
1693 | int packet_id; |
1691 | packet_t packet; |
1694 | packet_t packet; |
1692 | size_t length; |
1695 | size_t length; |
1693 | 1696 | ||
1694 | assert( local_sockets ); |
1697 | assert( local_sockets ); |
1695 | 1698 | ||
1696 | // find the socket |
1699 | // find the socket |
1697 | socket = socket_cores_find( local_sockets, socket_id ); |
1700 | socket = socket_cores_find( local_sockets, socket_id ); |
1698 | if( ! socket ) return ENOTSOCK; |
1701 | if( ! socket ) return ENOTSOCK; |
1699 | // get the socket specific data |
1702 | // get the socket specific data |
1700 | if( ! socket->specific_data ) return NO_DATA; |
1703 | if( ! socket->specific_data ) return NO_DATA; |
1701 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1704 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1702 | 1705 | ||
1703 | // check state |
1706 | // check state |
1704 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){ |
1707 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){ |
1705 | return ENOTCONN; |
1708 | return ENOTCONN; |
1706 | } |
1709 | } |
1707 | 1710 | ||
1708 | // send the source address if desired |
1711 | // send the source address if desired |
1709 | if( addrlen ){ |
1712 | if( addrlen ){ |
1710 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen )); |
1713 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen )); |
1711 | * addrlen = socket_data->addrlen; |
1714 | * addrlen = socket_data->addrlen; |
1712 | } |
1715 | } |
1713 | 1716 | ||
1714 | // get the next received packet |
1717 | // get the next received packet |
1715 | packet_id = dyn_fifo_value( & socket->received ); |
1718 | packet_id = dyn_fifo_value( & socket->received ); |
1716 | if( packet_id < 0 ) return NO_DATA; |
1719 | if( packet_id < 0 ) return NO_DATA; |
1717 | ERROR_PROPAGATE( packet_translate( tcp_globals.net_phone, & packet, packet_id )); |
1720 | ERROR_PROPAGATE( packet_translate( tcp_globals.net_phone, & packet, packet_id )); |
1718 | 1721 | ||
1719 | // reply the packets |
1722 | // reply the packets |
1720 | ERROR_PROPAGATE( socket_reply_packets( packet, & length )); |
1723 | ERROR_PROPAGATE( socket_reply_packets( packet, & length )); |
1721 | 1724 | ||
1722 | // release the packet |
1725 | // release the packet |
1723 | dyn_fifo_pop( & socket->received ); |
1726 | dyn_fifo_pop( & socket->received ); |
1724 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1727 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1725 | // return the total length |
1728 | // return the total length |
1726 | return ( int ) length; |
1729 | return ( int ) length; |
1727 | } |
1730 | } |
1728 | 1731 | ||
1729 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags ){ |
1732 | int tcp_send_message( socket_cores_ref local_sockets, int socket_id, int fragments, size_t data_fragment_size, int flags ){ |
1730 | ERROR_DECLARE; |
1733 | ERROR_DECLARE; |
1731 | 1734 | ||
1732 | socket_core_ref socket; |
1735 | socket_core_ref socket; |
1733 | tcp_socket_data_ref socket_data; |
1736 | tcp_socket_data_ref socket_data; |
1734 | packet_dimension_ref packet_dimension; |
1737 | packet_dimension_ref packet_dimension; |
1735 | packet_t packet; |
1738 | packet_t packet; |
1736 | size_t total_length; |
1739 | size_t total_length; |
1737 | tcp_header_ref header; |
1740 | tcp_header_ref header; |
1738 | int index; |
1741 | int index; |
1739 | int result; |
1742 | int result; |
1740 | 1743 | ||
1741 | assert( local_sockets ); |
1744 | assert( local_sockets ); |
1742 | 1745 | ||
1743 | // find the socket |
1746 | // find the socket |
1744 | socket = socket_cores_find( local_sockets, socket_id ); |
1747 | socket = socket_cores_find( local_sockets, socket_id ); |
1745 | if( ! socket ) return ENOTSOCK; |
1748 | if( ! socket ) return ENOTSOCK; |
1746 | // get the socket specific data |
1749 | // get the socket specific data |
1747 | if( ! socket->specific_data ) return NO_DATA; |
1750 | if( ! socket->specific_data ) return NO_DATA; |
1748 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1751 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1749 | 1752 | ||
1750 | // check state |
1753 | // check state |
1751 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){ |
1754 | if(( socket_data->state != TCP_SOCKET_ESTABLISHED ) && ( socket_data->state != TCP_SOCKET_CLOSE_WAIT )){ |
1752 | return ENOTCONN; |
1755 | return ENOTCONN; |
1753 | } |
1756 | } |
1754 | 1757 | ||
1755 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); |
1758 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); |
1756 | 1759 | ||
1757 | // TODO return the device_id + data_fragment_size if different - the client should send it again |
1760 | // TODO return the device_id + data_fragment_size if different - the client should send it again |
1758 | // ( two messages are better than ip fragmentation ) |
1761 | // ( two messages are better than ip fragmentation ) |
1759 | 1762 | ||
1760 | for( index = 0; index < fragments; ++ index ){ |
1763 | for( index = 0; index < fragments; ++ index ){ |
1761 | // read the data fragment |
1764 | // read the data fragment |
1762 | result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, sizeof( tcp_header_t ), packet_dimension, socket_data->addr, socket_data->addrlen ); |
1765 | result = tl_socket_read_packet_data( tcp_globals.net_phone, & packet, sizeof( tcp_header_t ), packet_dimension, socket_data->addr, socket_data->addrlen ); |
1763 | if( result < 0 ) return result; |
1766 | if( result < 0 ) return result; |
1764 | total_length = ( size_t ) result; |
1767 | total_length = ( size_t ) result; |
1765 | // prefix the tcp header |
1768 | // prefix the tcp header |
1766 | header = PACKET_PREFIX( packet, tcp_header_t ); |
1769 | header = PACKET_PREFIX( packet, tcp_header_t ); |
1767 | if( ! header ){ |
1770 | if( ! header ){ |
1768 | return tcp_release_and_return( packet, ENOMEM ); |
1771 | return tcp_release_and_return( packet, ENOMEM ); |
1769 | } |
1772 | } |
1770 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 ); |
1773 | tcp_prepare_operation_header( socket, socket_data, header, 0, 0 ); |
1771 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 0 )); |
1774 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 0 )); |
1772 | } |
1775 | } |
1773 | 1776 | ||
1774 | // flush packets |
1777 | // flush packets |
1775 | packet = tcp_get_packets_to_send( socket, socket_data ); |
1778 | packet = tcp_get_packets_to_send( socket, socket_data ); |
1776 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1779 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1777 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1780 | fibril_rwlock_read_unlock( & tcp_globals.lock ); |
1778 | if( packet ){ |
1781 | if( packet ){ |
1779 | // send the packet |
1782 | // send the packet |
1780 | tcp_send_packets( socket_data->device_id, packet ); |
1783 | tcp_send_packets( socket_data->device_id, packet ); |
1781 | } |
1784 | } |
1782 | 1785 | ||
1783 | return EOK; |
1786 | return EOK; |
1784 | } |
1787 | } |
1785 | 1788 | ||
1786 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id ){ |
1789 | int tcp_close_message( socket_cores_ref local_sockets, int socket_id ){ |
1787 | ERROR_DECLARE; |
1790 | ERROR_DECLARE; |
1788 | 1791 | ||
1789 | socket_core_ref socket; |
1792 | socket_core_ref socket; |
1790 | tcp_socket_data_ref socket_data; |
1793 | tcp_socket_data_ref socket_data; |
1791 | packet_t packet; |
1794 | packet_t packet; |
1792 | 1795 | ||
1793 | // find the socket |
1796 | // find the socket |
1794 | socket = socket_cores_find( local_sockets, socket_id ); |
1797 | socket = socket_cores_find( local_sockets, socket_id ); |
1795 | if( ! socket ) return ENOTSOCK; |
1798 | if( ! socket ) return ENOTSOCK; |
1796 | // get the socket specific data |
1799 | // get the socket specific data |
1797 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1800 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1798 | assert( socket_data ); |
1801 | assert( socket_data ); |
1799 | 1802 | ||
1800 | // check state |
1803 | // check state |
1801 | switch( socket_data->state ){ |
1804 | switch( socket_data->state ){ |
1802 | case TCP_SOCKET_ESTABLISHED: |
1805 | case TCP_SOCKET_ESTABLISHED: |
1803 | socket_data->state = TCP_SOCKET_FIN_WAIT_1; |
1806 | socket_data->state = TCP_SOCKET_FIN_WAIT_1; |
1804 | break; |
1807 | break; |
1805 | case TCP_SOCKET_CLOSE_WAIT: |
1808 | case TCP_SOCKET_CLOSE_WAIT: |
1806 | socket_data->state = TCP_SOCKET_LAST_ACK; |
1809 | socket_data->state = TCP_SOCKET_LAST_ACK; |
1807 | break; |
1810 | break; |
1808 | // case TCP_SOCKET_LISTEN: |
1811 | // case TCP_SOCKET_LISTEN: |
1809 | default: |
1812 | default: |
1810 | // just destroy |
1813 | // just destroy |
1811 | if( ! ERROR_OCCURRED( socket_destroy( tcp_globals.net_phone, socket_id, local_sockets, & tcp_globals.sockets, tcp_free_socket_data ))){ |
1814 | if( ! ERROR_OCCURRED( socket_destroy( tcp_globals.net_phone, socket_id, local_sockets, & tcp_globals.sockets, tcp_free_socket_data ))){ |
1812 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1815 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1813 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1816 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1814 | } |
1817 | } |
1815 | return ERROR_CODE; |
1818 | return ERROR_CODE; |
1816 | } |
1819 | } |
1817 | // send FIN |
1820 | // send FIN |
1818 | // TODO should I wait to complete? |
1821 | // TODO should I wait to complete? |
1819 | 1822 | ||
1820 | // create the notification packet |
1823 | // create the notification packet |
1821 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 )); |
1824 | ERROR_PROPAGATE( tcp_create_notification_packet( & packet, socket, socket_data, 0, 1 )); |
1822 | 1825 | ||
1823 | // send the packet |
1826 | // send the packet |
1824 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 )); |
1827 | ERROR_PROPAGATE( tcp_queue_packet( socket, socket_data, packet, 1 )); |
1825 | 1828 | ||
1826 | // flush packets |
1829 | // flush packets |
1827 | packet = tcp_get_packets_to_send( socket, socket_data ); |
1830 | packet = tcp_get_packets_to_send( socket, socket_data ); |
1828 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1831 | fibril_rwlock_write_unlock( socket_data->local_lock ); |
1829 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1832 | fibril_rwlock_write_unlock( & tcp_globals.lock ); |
1830 | if( packet ){ |
1833 | if( packet ){ |
1831 | // send the packet |
1834 | // send the packet |
1832 | tcp_send_packets( socket_data->device_id, packet ); |
1835 | tcp_send_packets( socket_data->device_id, packet ); |
1833 | } |
1836 | } |
1834 | return EOK; |
1837 | return EOK; |
1835 | } |
1838 | } |
1836 | 1839 | ||
1837 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ){ |
1840 | int tcp_create_notification_packet( packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize ){ |
1838 | ERROR_DECLARE; |
1841 | ERROR_DECLARE; |
1839 | 1842 | ||
1840 | packet_dimension_ref packet_dimension; |
1843 | packet_dimension_ref packet_dimension; |
1841 | tcp_header_ref header; |
1844 | tcp_header_ref header; |
1842 | 1845 | ||
1843 | assert( packet ); |
1846 | assert( packet ); |
1844 | 1847 | ||
1845 | // get the device packet dimension |
1848 | // get the device packet dimension |
1846 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); |
1849 | ERROR_PROPAGATE( tl_get_ip_packet_dimension( tcp_globals.ip_phone, & tcp_globals.dimensions, socket_data->device_id, & packet_dimension )); |
1847 | // get a new packet |
1850 | // get a new packet |
1848 | * packet = packet_get_4( tcp_globals.net_phone, sizeof( tcp_header_t ), packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix ); |
1851 | * packet = packet_get_4( tcp_globals.net_phone, sizeof( tcp_header_t ), packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix ); |
1849 | if( ! * packet ) return ENOMEM; |
1852 | if( ! * packet ) return ENOMEM; |
1850 | // allocate space in the packet |
1853 | // allocate space in the packet |
1851 | header = PACKET_SUFFIX( * packet, tcp_header_t ); |
1854 | header = PACKET_SUFFIX( * packet, tcp_header_t ); |
1852 | if( ! header ){ |
1855 | if( ! header ){ |
1853 | tcp_release_and_return( * packet, ENOMEM ); |
1856 | tcp_release_and_return( * packet, ENOMEM ); |
1854 | } |
1857 | } |
1855 | 1858 | ||
1856 | tcp_prepare_operation_header( socket, socket_data, header, synchronize, finalize ); |
1859 | tcp_prepare_operation_header( socket, socket_data, header, synchronize, finalize ); |
1857 | return EOK; |
1860 | return EOK; |
1858 | } |
1861 | } |
1859 | 1862 | ||
1860 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen ){ |
1863 | int tcp_accept_message( socket_cores_ref local_sockets, int socket_id, size_t * addrlen ){ |
1861 | ERROR_DECLARE; |
1864 | ERROR_DECLARE; |
1862 | 1865 | ||
1863 | socket_core_ref accepted; |
1866 | socket_core_ref accepted; |
1864 | socket_core_ref socket; |
1867 | socket_core_ref socket; |
1865 | tcp_socket_data_ref socket_data; |
1868 | tcp_socket_data_ref socket_data; |
1866 | 1869 | ||
1867 | assert( local_sockets ); |
1870 | assert( local_sockets ); |
1868 | assert( addrlen ); |
1871 | assert( addrlen ); |
1869 | 1872 | ||
1870 | // find the socket |
1873 | // find the socket |
1871 | socket = socket_cores_find( local_sockets, socket_id ); |
1874 | socket = socket_cores_find( local_sockets, socket_id ); |
1872 | if( ! socket ) return ENOTSOCK; |
1875 | if( ! socket ) return ENOTSOCK; |
1873 | // get the socket specific data |
1876 | // get the socket specific data |
1874 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1877 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1875 | assert( socket_data ); |
1878 | assert( socket_data ); |
1876 | 1879 | ||
1877 | // check state |
1880 | // check state |
1878 | if( socket_data->state != TCP_SOCKET_LISTEN ){ |
1881 | if( socket_data->state != TCP_SOCKET_LISTEN ){ |
1879 | return EINVAL; |
1882 | return EINVAL; |
1880 | } |
1883 | } |
1881 | 1884 | ||
1882 | do{ |
1885 | do{ |
1883 | socket_id = dyn_fifo_value( & socket->accepted ); |
1886 | socket_id = dyn_fifo_value( & socket->accepted ); |
1884 | if( socket_id < 0 ) return ENOTSOCK; |
1887 | if( socket_id < 0 ) return ENOTSOCK; |
1885 | 1888 | ||
1886 | accepted = socket_cores_find( local_sockets, socket_id ); |
1889 | accepted = socket_cores_find( local_sockets, socket_id ); |
1887 | if( ! accepted ) return ENOTSOCK; |
1890 | if( ! accepted ) return ENOTSOCK; |
1888 | // get the socket specific data |
1891 | // get the socket specific data |
1889 | socket_data = ( tcp_socket_data_ref ) accepted->specific_data; |
1892 | socket_data = ( tcp_socket_data_ref ) accepted->specific_data; |
1890 | assert( socket_data ); |
1893 | assert( socket_data ); |
1891 | if( socket_data->state == TCP_SOCKET_ESTABLISHED ){ |
1894 | if( socket_data->state == TCP_SOCKET_ESTABLISHED ){ |
1892 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen )); |
1895 | ERROR_PROPAGATE( data_reply( socket_data->addr, socket_data->addrlen )); |
1893 | * addrlen = socket_data->addrlen; |
1896 | * addrlen = socket_data->addrlen; |
1894 | } |
1897 | } |
1895 | dyn_fifo_pop( & socket->accepted ); |
1898 | dyn_fifo_pop( & socket->accepted ); |
1896 | }while( socket_data->state != TCP_SOCKET_ESTABLISHED ); |
1899 | }while( socket_data->state != TCP_SOCKET_ESTABLISHED ); |
1897 | printf("ret accept %d\n", accepted->socket_id ); |
1900 | printf("ret accept %d\n", accepted->socket_id ); |
1898 | return accepted->socket_id; |
1901 | return accepted->socket_id; |
1899 | } |
1902 | } |
1900 | 1903 | ||
1901 | void tcp_free_socket_data( socket_core_ref socket ){ |
1904 | void tcp_free_socket_data( socket_core_ref socket ){ |
1902 | tcp_socket_data_ref socket_data; |
1905 | tcp_socket_data_ref socket_data; |
1903 | 1906 | ||
1904 | assert( socket ); |
1907 | assert( socket ); |
1905 | 1908 | ||
1906 | printf( "destroy_socket %d\n", socket->socket_id ); |
1909 | printf( "destroy_socket %d\n", socket->socket_id ); |
1907 | 1910 | ||
1908 | // get the socket specific data |
1911 | // get the socket specific data |
1909 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1912 | socket_data = ( tcp_socket_data_ref ) socket->specific_data; |
1910 | assert( socket_data ); |
1913 | assert( socket_data ); |
1911 | //free the pseudo header |
1914 | //free the pseudo header |
1912 | if( socket_data->pseudo_header ){ |
1915 | if( socket_data->pseudo_header ){ |
1913 | if( socket_data->headerlen ){ |
1916 | if( socket_data->headerlen ){ |
1914 | printf("d pseudo\n"); |
1917 | printf("d pseudo\n"); |
1915 | free( socket_data->pseudo_header ); |
1918 | free( socket_data->pseudo_header ); |
1916 | socket_data->headerlen = 0; |
1919 | socket_data->headerlen = 0; |
1917 | } |
1920 | } |
1918 | socket_data->pseudo_header = NULL; |
1921 | socket_data->pseudo_header = NULL; |
1919 | } |
1922 | } |
1920 | socket_data->headerlen = 0; |
1923 | socket_data->headerlen = 0; |
1921 | // free the address |
1924 | // free the address |
1922 | if( socket_data->addr ){ |
1925 | if( socket_data->addr ){ |
1923 | if( socket_data->addrlen ){ |
1926 | if( socket_data->addrlen ){ |
1924 | printf("d addr\n"); |
1927 | printf("d addr\n"); |
1925 | free( socket_data->addr ); |
1928 | free( socket_data->addr ); |
1926 | socket_data->addrlen = 0; |
1929 | socket_data->addrlen = 0; |
1927 | } |
1930 | } |
1928 | socket_data->addr = NULL; |
1931 | socket_data->addr = NULL; |
1929 | } |
1932 | } |
1930 | socket_data->addrlen = 0; |
1933 | socket_data->addrlen = 0; |
1931 | } |
1934 | } |
1932 | 1935 | ||
1933 | int tcp_release_and_return( packet_t packet, int result ){ |
1936 | int tcp_release_and_return( packet_t packet, int result ){ |
1934 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1937 | pq_release( tcp_globals.net_phone, packet_get_id( packet )); |
1935 | return result; |
1938 | return result; |
1936 | } |
1939 | } |
1937 | 1940 | ||
1938 | /** @} |
1941 | /** @} |
1939 | */ |
1942 | */ |
1940 | 1943 |