Changes in uspace/srv/net/tl/tcp/tcp.c [c5b59ce:14f1db0] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tcp/tcp.c
rc5b59ce r14f1db0 42 42 //TODO remove stdio 43 43 #include <stdio.h> 44 #include <errno.h>45 #include <err.h>46 44 47 45 #include <ipc/ipc.h> 48 46 #include <ipc/services.h> 49 47 48 #include <net_err.h> 50 49 #include <net_messages.h> 51 50 #include <net_modules.h> … … 64 63 #include <net_interface.h> 65 64 #include <socket_codes.h> 65 #include <socket_errno.h> 66 66 #include <tcp_codes.h> 67 67 #include <socket_core.h> … … 76 76 #include "tcp_module.h" 77 77 78 /** TCP module name. */ 78 /** TCP module name. 79 */ 79 80 #define NAME "TCP protocol" 80 81 81 /** The TCP window default value. */ 82 #define NET_DEFAULT_TCP_WINDOW 10240 83 84 /** Initial timeout for new connections. */ 82 /** The TCP window default value. 83 */ 84 #define NET_DEFAULT_TCP_WINDOW 10240 85 86 /** Initial timeout for new connections. 87 */ 85 88 #define NET_DEFAULT_TCP_INITIAL_TIMEOUT 3000000L 86 89 87 /** Default timeout for closing. */ 88 #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L 89 90 /** The initial outgoing sequence number. */ 91 #define TCP_INITIAL_SEQUENCE_NUMBER 2999 92 93 /** Maximum TCP fragment size. */ 94 #define MAX_TCP_FRAGMENT_SIZE 65535 95 96 /** Free ports pool start. */ 97 #define TCP_FREE_PORTS_START 1025 98 99 /** Free ports pool end. */ 90 /** Default timeout for closing. 91 */ 92 #define NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT 2000L 93 94 /** The initial outgoing sequence number. 95 */ 96 #define TCP_INITIAL_SEQUENCE_NUMBER 2999 97 98 /** Maximum TCP fragment size. 99 */ 100 #define MAX_TCP_FRAGMENT_SIZE 65535 101 102 /** Free ports pool start. 103 */ 104 #define TCP_FREE_PORTS_START 1025 105 106 /** Free ports pool end. 107 */ 100 108 #define TCP_FREE_PORTS_END 65535 101 109 102 /** Timeout for connection initialization, SYN sent. */ 103 #define TCP_SYN_SENT_TIMEOUT 1000000L 104 105 /** The maximum number of timeouts in a row before singaling connection lost. */ 110 /** Timeout for connection initialization, SYN sent. 111 */ 112 #define TCP_SYN_SENT_TIMEOUT 1000000L 113 114 /** The maximum number of timeouts in a row before singaling connection lost. 115 */ 106 116 #define TCP_MAX_TIMEOUTS 8 107 117 108 /** The number of acknowledgements before retransmit. */ 118 /** The number of acknowledgements before retransmit. 119 */ 109 120 #define TCP_FAST_RETRANSMIT_COUNT 3 110 121 111 /** Returns a value indicating whether the value is in the interval respecting 112 * the possible overflow. 113 * 122 /** Returns a value indicating whether the value is in the interval respecting the possible overflow. 114 123 * The high end and/or the value may overflow, be lower than the low value. 115 124 * @param[in] lower The last value before the interval. … … 117 126 * @param[in] higher_equal The last value in the interval. 118 127 */ 119 #define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \ 120 ((((lower) < (value)) && (((value) <= (higher_equal)) || \ 121 ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && \ 122 ((higher_equal) < (lower)))) 128 #define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) ((((lower) < (value)) && (((value) <= (higher_equal)) || ((higher_equal) < (lower)))) || (((value) <= (higher_equal)) && ((higher_equal) < (lower)))) 123 129 124 130 /** Type definition of the TCP timeout. 125 131 * @see tcp_timeout 126 132 */ 127 typedef struct tcp_timeout 133 typedef struct tcp_timeout tcp_timeout_t; 128 134 129 135 /** Type definition of the TCP timeout pointer. 130 136 * @see tcp_timeout 131 137 */ 132 typedef tcp_timeout_t * tcp_timeout_ref;138 typedef tcp_timeout_t * tcp_timeout_ref; 133 139 134 140 /** TCP reply timeout data. … … 136 142 * @see tcp_timeout() 137 143 */ 138 struct tcp_timeout { 139 /** TCP global data are going to be read only. */ 144 struct tcp_timeout{ 145 /** TCP global data are going to be read only. 146 */ 140 147 int globals_read_only; 141 142 /** Socket port.*/148 /** Socket port. 149 */ 143 150 int port; 144 145 /** Local sockets.*/151 /** Local sockets. 152 */ 146 153 socket_cores_ref local_sockets; 147 148 /** Socket identifier.*/154 /** Socket identifier. 155 */ 149 156 int socket_id; 150 151 /** Socket state.*/157 /** Socket state. 158 */ 152 159 tcp_socket_state_t state; 153 154 /** Sent packet sequence number.*/160 /** Sent packet sequence number. 161 */ 155 162 int sequence_number; 156 157 /** Timeout in microseconds.*/163 /** Timeout in microseconds. 164 */ 158 165 suseconds_t timeout; 159 160 /** Port map key.*/161 char * key;162 163 /** Port map key length.*/166 /** Port map key. 167 */ 168 char * key; 169 /** Port map key length. 170 */ 164 171 size_t key_length; 165 172 }; … … 172 179 int tcp_release_and_return(packet_t packet, int result); 173 180 174 void tcp_prepare_operation_header(socket_core_ref socket, 175 tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, 176 int finalize); 177 int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t), 178 socket_core_ref socket, tcp_socket_data_ref socket_data, 179 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, 180 int globals_read_only); 181 void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize); 182 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); 181 183 void tcp_free_socket_data(socket_core_ref socket); 182 183 int tcp_timeout(void *data); 184 185 int tcp_release_after_timeout(void *data); 186 187 int tcp_process_packet(device_id_t device_id, packet_t packet, 188 services_t error); 189 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, 190 struct sockaddr *addr, socklen_t addrlen); 191 int tcp_queue_prepare_packet(socket_core_ref socket, 192 tcp_socket_data_ref socket_data, packet_t packet, size_t data_length); 193 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, 194 packet_t packet, size_t data_length); 195 packet_t tcp_get_packets_to_send(socket_core_ref socket, 196 tcp_socket_data_ref socket_data); 184 int tcp_timeout(void * data); 185 int tcp_release_after_timeout(void * data); 186 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error); 187 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen); 188 int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length); 189 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length); 190 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data); 197 191 void tcp_send_packets(device_id_t device_id, packet_t packet); 198 199 void tcp_process_acknowledgement(socket_core_ref socket, 200 tcp_socket_data_ref socket_data, tcp_header_ref header); 201 packet_t tcp_send_prepare_packet(socket_core_ref socket, 202 tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, 203 size_t sequence_number); 204 packet_t tcp_prepare_copy(socket_core_ref socket, 205 tcp_socket_data_ref socket_data, packet_t packet, size_t data_length, 206 size_t sequence_number); 207 void tcp_retransmit_packet(socket_core_ref socket, 208 tcp_socket_data_ref socket_data, size_t sequence_number); 209 int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, 210 tcp_socket_data_ref socket_data, int synchronize, int finalize); 192 void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header); 193 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); 194 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); 195 void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number); 196 int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize); 211 197 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data); 212 213 198 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data); 214 215 int tcp_process_listen(socket_core_ref listening_socket, 216 tcp_socket_data_ref listening_socket_data, tcp_header_ref header, 217 packet_t packet, struct sockaddr *src, struct sockaddr *dest, 218 size_t addrlen); 219 int tcp_process_syn_sent(socket_core_ref socket, 220 tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet); 221 int tcp_process_syn_received(socket_core_ref socket, 222 tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet); 223 int tcp_process_established(socket_core_ref socket, 224 tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet, 225 int fragments, size_t total_length); 226 int tcp_queue_received_packet(socket_core_ref socket, 227 tcp_socket_data_ref socket_data, packet_t packet, int fragments, 228 size_t total_length); 229 230 int tcp_received_msg(device_id_t device_id, packet_t packet, 231 services_t receiver, services_t error); 199 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); 200 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet); 201 int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet); 202 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); 203 int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length); 204 205 int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error); 232 206 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call); 233 234 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, 235 int backlog); 236 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, 237 struct sockaddr *addr, socklen_t addrlen); 238 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, 239 int flags, size_t * addrlen); 240 int tcp_send_message(socket_cores_ref local_sockets, int socket_id, 241 int fragments, size_t * data_fragment_size, int flags); 242 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, 243 int new_socket_id, size_t * data_fragment_size, size_t * addrlen); 207 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog); 208 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen); 209 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen); 210 int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags); 211 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen); 244 212 int tcp_close_message(socket_cores_ref local_sockets, int socket_id); 245 213 246 /** TCP global data. */247 tcp_globals_t tcp_globals; 248 249 int tcp_initialize(async_client_conn_t client_connection) 250 {214 /** TCP global data. 215 */ 216 tcp_globals_t tcp_globals; 217 218 int tcp_initialize(async_client_conn_t client_connection){ 251 219 ERROR_DECLARE; 252 220 253 221 assert(client_connection); 254 255 222 fibril_rwlock_initialize(&tcp_globals.lock); 256 223 fibril_rwlock_write_lock(&tcp_globals.lock); 257 258 tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 259 ICMP_CONNECT_TIMEOUT); 260 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, 261 SERVICE_TCP, client_connection, tcp_received_msg); 262 if (tcp_globals.ip_phone < 0) 224 tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, ICMP_CONNECT_TIMEOUT); 225 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, client_connection, tcp_received_msg); 226 if(tcp_globals.ip_phone < 0){ 263 227 return tcp_globals.ip_phone; 264 228 } 265 229 ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets)); 266 if (ERROR_OCCURRED(packet_dimensions_initialize( 267 &tcp_globals.dimensions))) { 230 if(ERROR_OCCURRED(packet_dimensions_initialize(&tcp_globals.dimensions))){ 268 231 socket_ports_destroy(&tcp_globals.sockets); 269 232 return ERROR_CODE; 270 233 } 271 272 234 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; 273 235 fibril_rwlock_write_unlock(&tcp_globals.lock); 274 275 236 return EOK; 276 237 } 277 238 278 int 279 tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, 280 services_t error) 281 { 239 int tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, services_t error){ 282 240 ERROR_DECLARE; 283 241 284 if (receiver != SERVICE_TCP)242 if(receiver != SERVICE_TCP){ 285 243 return EREFUSED; 286 244 } 287 245 fibril_rwlock_write_lock(&tcp_globals.lock); 288 if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error)))246 if(ERROR_OCCURRED(tcp_process_packet(device_id, packet, error))){ 289 247 fibril_rwlock_write_unlock(&tcp_globals.lock); 290 248 } 291 249 printf("receive %d \n", ERROR_CODE); 292 250 … … 294 252 } 295 253 296 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error) 297 { 254 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error){ 298 255 ERROR_DECLARE; 299 256 … … 302 259 int result; 303 260 tcp_header_ref header; 304 socket_core_ref socket;261 socket_core_ref socket; 305 262 tcp_socket_data_ref socket_data; 306 263 packet_t next_packet; … … 310 267 icmp_type_t type; 311 268 icmp_code_t code; 312 struct sockaddr * src;313 struct sockaddr * dest;269 struct sockaddr * src; 270 struct sockaddr * dest; 314 271 size_t addrlen; 315 272 316 if (error) { 317 switch (error) { 318 case SERVICE_ICMP: 319 // process error 320 result = icmp_client_process_packet(packet, &type, 321 &code, NULL, NULL); 322 if (result < 0) 323 return tcp_release_and_return(packet, result); 324 325 length = (size_t) result; 326 if (ERROR_OCCURRED(packet_trim(packet, length, 0))) { 327 return tcp_release_and_return(packet, 328 ERROR_CODE); 329 } 330 break; 331 332 default: 333 return tcp_release_and_return(packet, ENOTSUP); 273 printf("p1 \n"); 274 if(error){ 275 switch(error){ 276 case SERVICE_ICMP: 277 // process error 278 result = icmp_client_process_packet(packet, &type, &code, NULL, NULL); 279 if(result < 0){ 280 return tcp_release_and_return(packet, result); 281 } 282 length = (size_t) result; 283 if(ERROR_OCCURRED(packet_trim(packet, length, 0))){ 284 return tcp_release_and_return(packet, ERROR_CODE); 285 } 286 break; 287 default: 288 return tcp_release_and_return(packet, ENOTSUP); 334 289 } 335 290 } … … 337 292 // TODO process received ipopts? 338 293 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL); 339 if (result < 0) 294 // printf("ip len %d\n", result); 295 if(result < 0){ 340 296 return tcp_release_and_return(packet, result); 341 297 } 342 298 offset = (size_t) result; 343 299 344 300 length = packet_get_data_length(packet); 345 if (length <= 0) 301 // printf("packet len %d\n", length); 302 if(length <= 0){ 346 303 return tcp_release_and_return(packet, EINVAL); 347 348 if (length < TCP_HEADER_SIZE + offset)304 } 305 if(length < TCP_HEADER_SIZE + offset){ 349 306 return tcp_release_and_return(packet, NO_DATA); 307 } 350 308 351 309 // trim all but TCP header 352 if (ERROR_OCCURRED(packet_trim(packet, offset, 0)))310 if(ERROR_OCCURRED(packet_trim(packet, offset, 0))){ 353 311 return tcp_release_and_return(packet, ERROR_CODE); 312 } 354 313 355 314 // get tcp header 356 315 header = (tcp_header_ref) packet_get_data(packet); 357 if (!header)316 if(! header){ 358 317 return tcp_release_and_return(packet, NO_DATA); 359 360 // printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), 361 // ntohs(header->destination_port)); 318 } 319 // printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), ntohs(header->destination_port)); 362 320 363 321 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest); 364 if (result <= 0)322 if(result <= 0){ 365 323 return tcp_release_and_return(packet, result); 366 324 } 367 325 addrlen = (size_t) result; 368 326 369 if (ERROR_OCCURRED(tl_set_address_port(src, addrlen, 370 ntohs(header->source_port)))) 327 if(ERROR_OCCURRED(tl_set_address_port(src, addrlen, ntohs(header->source_port)))){ 371 328 return tcp_release_and_return(packet, ERROR_CODE); 372 329 } 330 373 331 // find the destination socket 374 socket = socket_port_find(&tcp_globals.sockets, 375 ntohs(header->destination_port), (const char *) src, addrlen);376 if (!socket) { 332 socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), (const char *) src, addrlen); 333 if(! socket){ 334 // printf("listening?\n"); 377 335 // find the listening destination socket 378 socket = socket_port_find(&tcp_globals.sockets, 379 ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 380 0); 381 if (!socket) { 382 if (tl_prepare_icmp_packet(tcp_globals.net_phone, 383 tcp_globals.icmp_phone, packet, error) == EOK) { 384 icmp_destination_unreachable_msg( 385 tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 386 0, packet); 336 socket = socket_port_find(&tcp_globals.sockets, ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING, 0); 337 if(! socket){ 338 if(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error) == EOK){ 339 icmp_destination_unreachable_msg(tcp_globals.icmp_phone, ICMP_PORT_UNREACH, 0, packet); 387 340 } 388 341 return EADDRNOTAVAIL; … … 401 354 checksum = 0; 402 355 total_length = 0; 403 do 404 ++ fragments;356 do{ 357 ++ fragments; 405 358 length = packet_get_data_length(next_packet); 406 if (length <= 0)359 if(length <= 0){ 407 360 return tcp_release_and_return(packet, NO_DATA); 408 361 } 409 362 total_length += length; 410 411 363 // add partial checksum if set 412 if (!error) { 413 checksum = compute_checksum(checksum, 414 packet_get_data(packet), 415 packet_get_data_length(packet)); 416 } 417 418 } while ((next_packet = pq_next(next_packet))); 419 364 if(! error){ 365 checksum = compute_checksum(checksum, packet_get_data(packet), packet_get_data_length(packet)); 366 } 367 }while((next_packet = pq_next(next_packet))); 368 // printf("fragments %d of %d bytes\n", fragments, total_length); 369 370 // printf("lock?\n"); 420 371 fibril_rwlock_write_lock(socket_data->local_lock); 421 422 if (error) 423 goto error; 424 425 if (socket_data->state == TCP_SOCKET_LISTEN) { 426 427 if (socket_data->pseudo_header) { 428 free(socket_data->pseudo_header); 429 socket_data->pseudo_header = NULL; 430 socket_data->headerlen = 0; 431 } 432 433 if (ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src, 434 addrlen, dest, addrlen, total_length, 435 &socket_data->pseudo_header, &socket_data->headerlen))) { 372 // printf("locked\n"); 373 if(! error){ 374 if(socket_data->state == TCP_SOCKET_LISTEN){ 375 if(socket_data->pseudo_header){ 376 free(socket_data->pseudo_header); 377 socket_data->pseudo_header = NULL; 378 socket_data->headerlen = 0; 379 } 380 if(ERROR_OCCURRED(ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen, dest, addrlen, total_length, &socket_data->pseudo_header, &socket_data->headerlen))){ 381 fibril_rwlock_write_unlock(socket_data->local_lock); 382 return tcp_release_and_return(packet, ERROR_CODE); 383 } 384 }else if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, total_length))){ 436 385 fibril_rwlock_write_unlock(socket_data->local_lock); 437 386 return tcp_release_and_return(packet, ERROR_CODE); 438 387 } 439 440 } else if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length( 441 socket_data->pseudo_header, socket_data->headerlen, 442 total_length))) { 443 fibril_rwlock_write_unlock(socket_data->local_lock); 444 return tcp_release_and_return(packet, ERROR_CODE); 445 } 446 447 checksum = compute_checksum(checksum, socket_data->pseudo_header, 448 socket_data->headerlen); 449 if (flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO) { 450 printf("checksum err %x -> %x\n", header->checksum, 451 flip_checksum(compact_checksum(checksum))); 452 fibril_rwlock_write_unlock(socket_data->local_lock); 453 454 if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone, 455 tcp_globals.icmp_phone, packet, error))) { 456 // checksum error ICMP 457 icmp_parameter_problem_msg(tcp_globals.icmp_phone, 458 ICMP_PARAM_POINTER, 459 ((size_t) ((void *) &header->checksum)) - 460 ((size_t) ((void *) header)), packet); 461 } 462 463 return EINVAL; 464 } 465 466 error: 388 checksum = compute_checksum(checksum, socket_data->pseudo_header, socket_data->headerlen); 389 if(flip_checksum(compact_checksum(checksum)) != IP_CHECKSUM_ZERO){ 390 printf("checksum err %x -> %x\n", header->checksum, flip_checksum(compact_checksum(checksum))); 391 fibril_rwlock_write_unlock(socket_data->local_lock); 392 if(! ERROR_OCCURRED(tl_prepare_icmp_packet(tcp_globals.net_phone, tcp_globals.icmp_phone, packet, error))){ 393 // checksum error ICMP 394 icmp_parameter_problem_msg(tcp_globals.icmp_phone, ICMP_PARAM_POINTER, ((size_t) ((void *) &header->checksum)) - ((size_t) ((void *) header)), packet); 395 } 396 return EINVAL; 397 } 398 } 399 467 400 fibril_rwlock_read_unlock(&tcp_globals.lock); 468 401 469 402 // TODO error reporting/handling 470 switch (socket_data->state) { 471 case TCP_SOCKET_LISTEN: 472 ERROR_CODE = tcp_process_listen(socket, socket_data, header, 473 packet, src, dest, addrlen); 474 break; 475 case TCP_SOCKET_SYN_RECEIVED: 476 ERROR_CODE = tcp_process_syn_received(socket, socket_data, 477 header, packet); 478 break; 479 case TCP_SOCKET_SYN_SENT: 480 ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header, 481 packet); 482 break; 483 case TCP_SOCKET_FIN_WAIT_1: 484 // ack changing the state to FIN_WAIT_2 gets processed later 485 case TCP_SOCKET_FIN_WAIT_2: 486 // fin changing state to LAST_ACK gets processed later 487 case TCP_SOCKET_LAST_ACK: 488 // ack releasing the socket get processed later 489 case TCP_SOCKET_CLOSING: 490 // ack releasing the socket gets processed later 491 case TCP_SOCKET_ESTABLISHED: 492 ERROR_CODE = tcp_process_established(socket, socket_data, 493 header, packet, fragments, total_length); 494 break; 495 default: 496 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 497 } 498 499 if (ERROR_CODE != EOK) { 403 // printf("st %d\n", socket_data->state); 404 switch(socket_data->state){ 405 case TCP_SOCKET_LISTEN: 406 ERROR_CODE = tcp_process_listen(socket, socket_data, header, packet, src, dest, addrlen); 407 break; 408 case TCP_SOCKET_SYN_RECEIVED: 409 ERROR_CODE = tcp_process_syn_received(socket, socket_data, header, packet); 410 break; 411 case TCP_SOCKET_SYN_SENT: 412 ERROR_CODE = tcp_process_syn_sent(socket, socket_data, header, packet); 413 break; 414 case TCP_SOCKET_FIN_WAIT_1: 415 // ack changing the state to FIN_WAIT_2 gets processed later 416 case TCP_SOCKET_FIN_WAIT_2: 417 // fin changing state to LAST_ACK gets processed later 418 case TCP_SOCKET_LAST_ACK: 419 // ack releasing the socket get processed later 420 case TCP_SOCKET_CLOSING: 421 // ack releasing the socket gets processed later 422 case TCP_SOCKET_ESTABLISHED: 423 ERROR_CODE = tcp_process_established(socket, socket_data, header, packet, fragments, total_length); 424 break; 425 default: 426 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 427 } 428 429 if(ERROR_CODE != EOK){ 500 430 printf("process %d\n", ERROR_CODE); 501 431 fibril_rwlock_write_unlock(socket_data->local_lock); 502 432 } 503 504 433 return EOK; 505 434 } 506 435 507 int 508 tcp_process_established(socket_core_ref socket, tcp_socket_data_ref socket_data, 509 tcp_header_ref header, packet_t packet, int fragments, 510 size_t total_length) 511 { 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){ 512 437 ERROR_DECLARE; 513 438 … … 530 455 old_incoming = socket_data->next_incoming; 531 456 532 if (header->finalize)457 if(header->finalize){ 533 458 socket_data->fin_incoming = new_sequence_number; 534 459 } 460 461 // printf("pe %d < %d <= %d\n", new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length); 535 462 // trim begining if containing expected data 536 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number, 537 socket_data->next_incoming, new_sequence_number + total_length)) { 538 463 if(IS_IN_INTERVAL_OVERFLOW(new_sequence_number, socket_data->next_incoming, new_sequence_number + total_length)){ 539 464 // get the acknowledged offset 540 if (socket_data->next_incoming < new_sequence_number) { 541 offset = new_sequence_number - 542 socket_data->next_incoming; 543 } else { 544 offset = socket_data->next_incoming - 545 new_sequence_number; 546 } 547 465 if(socket_data->next_incoming < new_sequence_number){ 466 offset = new_sequence_number - socket_data->next_incoming; 467 }else{ 468 offset = socket_data->next_incoming - new_sequence_number; 469 } 470 // printf("offset %d\n", offset); 548 471 new_sequence_number += offset; 549 472 total_length -= offset; 550 473 length = packet_get_data_length(packet); 551 474 // trim the acknowledged data 552 while (length <= offset){475 while(length <= offset){ 553 476 // release the acknowledged packets 554 477 next_packet = pq_next(packet); 555 pq_release_remote(tcp_globals.net_phone, 556 packet_get_id(packet)); 478 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 557 479 packet = next_packet; 558 480 offset -= length; 559 481 length = packet_get_data_length(packet); 560 482 } 561 562 if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet, 563 offset, 0)))) 483 if((offset > 0) 484 && (ERROR_OCCURRED(packet_trim(packet, offset, 0)))){ 564 485 return tcp_release_and_return(packet, ERROR_CODE); 565 486 } 566 487 assert(new_sequence_number == socket_data->next_incoming); 567 488 } 568 489 569 490 // release if overflowing the window 491 // if(IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming + socket_data->window, new_sequence_number, new_sequence_number + total_length)){ 492 // return tcp_release_and_return(packet, EOVERFLOW); 493 // } 494 570 495 /* 571 if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming +572 socket_data->window, new_sequence_number, new_sequence_number +573 total_length)) {574 return tcp_release_and_return(packet, EOVERFLOW);575 }576 577 496 // trim end if overflowing the window 578 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number, 579 socket_data->next_incoming + socket_data->window, 580 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)){ 581 498 // get the allowed data length 582 if (socket_data->next_incoming + socket_data->window < 583 new_sequence_number) { 584 offset = new_sequence_number - 585 socket_data->next_incoming + socket_data->window; 586 } else { 587 offset = socket_data->next_incoming + 588 socket_data->window - new_sequence_number; 499 if(socket_data->next_incoming + socket_data->window < new_sequence_number){ 500 offset = new_sequence_number - socket_data->next_incoming + socket_data->window; 501 }else{ 502 offset = socket_data->next_incoming + socket_data->window - new_sequence_number; 589 503 } 590 504 next_packet = packet; 591 505 // trim the overflowing data 592 while (next_packet && (offset > 0)){506 while(next_packet && (offset > 0)){ 593 507 length = packet_get_data_length(packet); 594 if (length <= offset)508 if(length <= offset){ 595 509 next_packet = pq_next(next_packet); 596 else if (ERROR_OCCURRED(packet_trim(next_packet, 0, 597 length - offset))) 598 return tcp_release_and_return(packet, 599 ERROR_CODE); 510 }else if(ERROR_OCCURRED(packet_trim(next_packet, 0, length - offset))){ 511 return tcp_release_and_return(packet, ERROR_CODE); 512 } 600 513 offset -= length; 601 514 total_length -= length - offset; … … 603 516 // release the overflowing packets 604 517 next_packet = pq_next(next_packet); 605 if (next_packet){518 if(next_packet){ 606 519 tmp_packet = next_packet; 607 520 next_packet = pq_next(next_packet); 608 521 pq_insert_after(tmp_packet, next_packet); 609 pq_release_remote(tcp_globals.net_phone, 610 packet_get_id(tmp_packet)); 611 } 612 assert(new_sequence_number + total_length == 613 socket_data->next_incoming + socket_data->window); 522 pq_release_remote(tcp_globals.net_phone, packet_get_id(tmp_packet)); 523 } 524 assert(new_sequence_number + total_length == socket_data->next_incoming + socket_data->window); 614 525 } 615 526 */ 616 527 // the expected one arrived? 617 if (new_sequence_number == socket_data->next_incoming){528 if(new_sequence_number == socket_data->next_incoming){ 618 529 printf("expected\n"); 619 530 // process acknowledgement … … 622 533 // remove the header 623 534 total_length -= TCP_HEADER_LENGTH(header); 624 if (ERROR_OCCURRED(packet_trim(packet, 625 TCP_HEADER_LENGTH(header), 0))) 535 if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){ 626 536 return tcp_release_and_return(packet, ERROR_CODE); 627 628 if (total_length) { 629 ERROR_PROPAGATE(tcp_queue_received_packet(socket,630 631 } else{537 } 538 539 if(total_length){ 540 ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, fragments, total_length)); 541 }else{ 632 542 total_length = 1; 633 543 } 634 635 544 socket_data->next_incoming = old_incoming + total_length; 636 545 packet = socket_data->incoming; 637 while (packet) { 638 639 if (ERROR_OCCURRED(pq_get_order(socket_data->incoming, 640 &order, NULL))) { 546 while(packet){ 547 if(ERROR_OCCURRED(pq_get_order(socket_data->incoming, &order, NULL))){ 641 548 // remove the corrupted packet 642 549 next_packet = pq_detach(packet); 643 if (packet == socket_data->incoming)550 if(packet == socket_data->incoming){ 644 551 socket_data->incoming = next_packet; 645 pq_release_remote(tcp_globals.net_phone,646 552 } 553 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 647 554 packet = next_packet; 648 555 continue; 649 556 } 650 651 557 sequence_number = (uint32_t) order; 652 if (IS_IN_INTERVAL_OVERFLOW(sequence_number, 653 old_incoming, socket_data->next_incoming)) { 558 if(IS_IN_INTERVAL_OVERFLOW(sequence_number, old_incoming, socket_data->next_incoming)){ 654 559 // move to the next 655 560 packet = pq_next(packet); 656 // coninual data? 657 } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming, 658 sequence_number, socket_data->next_incoming)) { 561 // coninual data? 562 }else if(IS_IN_INTERVAL_OVERFLOW(old_incoming, sequence_number, socket_data->next_incoming)){ 659 563 // detach the packet 660 564 next_packet = pq_detach(packet); 661 if (packet == socket_data->incoming)565 if(packet == socket_data->incoming){ 662 566 socket_data->incoming = next_packet; 567 } 663 568 // get data length 664 569 length = packet_get_data_length(packet); 665 570 new_sequence_number = sequence_number + length; 666 if (length <= 0){571 if(length <= 0){ 667 572 // remove the empty packet 668 pq_release_remote(tcp_globals.net_phone, 669 packet_get_id(packet)); 573 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 670 574 packet = next_packet; 671 575 continue; 672 576 } 673 577 // exactly following 674 if (sequence_number == 675 socket_data->next_incoming) { 578 if(sequence_number == socket_data->next_incoming){ 676 579 // queue received data 677 ERROR_PROPAGATE( 678 tcp_queue_received_packet(socket, 679 socket_data, packet, 1, 680 packet_get_data_length(packet))); 681 socket_data->next_incoming = 682 new_sequence_number; 580 ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet))); 581 socket_data->next_incoming = new_sequence_number; 683 582 packet = next_packet; 684 583 continue; 685 // at least partly following data? 686 } else if (IS_IN_INTERVAL_OVERFLOW( 687 sequence_number, socket_data->next_incoming, 688 new_sequence_number)) { 689 if (socket_data->next_incoming < 690 new_sequence_number) { 691 length = new_sequence_number - 692 socket_data->next_incoming; 693 } else { 694 length = 695 socket_data->next_incoming - 696 new_sequence_number; 584 // at least partly following data? 585 }else if(IS_IN_INTERVAL_OVERFLOW(sequence_number, socket_data->next_incoming, new_sequence_number)){ 586 if(socket_data->next_incoming < new_sequence_number){ 587 length = new_sequence_number - socket_data->next_incoming; 588 }else{ 589 length = socket_data->next_incoming - new_sequence_number; 697 590 } 698 if (ERROR_NONE(packet_trim(packet, 699 length, 0))) { 591 if(! ERROR_OCCURRED(packet_trim(packet, length, 0))){ 700 592 // queue received data 701 ERROR_PROPAGATE( 702 tcp_queue_received_packet( 703 socket, socket_data, packet, 704 1, packet_get_data_length( 705 packet))); 706 socket_data->next_incoming = 707 new_sequence_number; 593 ERROR_PROPAGATE(tcp_queue_received_packet(socket, socket_data, packet, 1, packet_get_data_length(packet))); 594 socket_data->next_incoming = new_sequence_number; 708 595 packet = next_packet; 709 596 continue; … … 711 598 } 712 599 // remove the duplicit or corrupted packet 713 pq_release_remote(tcp_globals.net_phone, 714 packet_get_id(packet)); 600 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 715 601 packet = next_packet; 716 602 continue; 717 } else{603 }else{ 718 604 break; 719 605 } 720 606 } 721 } else if (IS_IN_INTERVAL(socket_data->next_incoming, 722 new_sequence_number, 723 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)){ 724 608 printf("in window\n"); 725 609 // process acknowledgement … … 728 612 // remove the header 729 613 total_length -= TCP_HEADER_LENGTH(header); 730 if (ERROR_OCCURRED(packet_trim(packet, 731 TCP_HEADER_LENGTH(header), 0))) 614 if(ERROR_OCCURRED(packet_trim(packet, TCP_HEADER_LENGTH(header), 0))){ 732 615 return tcp_release_and_return(packet, ERROR_CODE); 616 } 733 617 734 618 next_packet = pq_detach(packet); 735 619 length = packet_get_data_length(packet); 736 if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, 737 new_sequence_number, length))) { 620 if(ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, new_sequence_number, length))){ 738 621 // remove the corrupted packets 739 pq_release_remote(tcp_globals.net_phone, 740 packet_get_id(packet)); 741 pq_release_remote(tcp_globals.net_phone, 742 packet_get_id(next_packet)); 743 } else { 744 while (next_packet) { 622 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 623 pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet)); 624 }else{ 625 while(next_packet){ 745 626 new_sequence_number += length; 746 627 tmp_packet = pq_detach(next_packet); 747 628 length = packet_get_data_length(next_packet); 748 if (ERROR_OCCURRED(pq_set_order(next_packet, 749 new_sequence_number, length)) || 750 ERROR_OCCURRED(pq_insert_after(packet, 751 next_packet))) { 752 pq_release_remote(tcp_globals.net_phone, 753 packet_get_id(next_packet)); 629 if(ERROR_OCCURRED(pq_set_order(next_packet, new_sequence_number, length)) 630 || ERROR_OCCURRED(pq_insert_after(packet, next_packet))){ 631 pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet)); 754 632 } 755 633 next_packet = tmp_packet; 756 634 } 757 635 } 758 } else{636 }else{ 759 637 printf("unexpected\n"); 760 638 // release duplicite or restricted … … 763 641 764 642 // change state according to the acknowledging incoming fin 765 if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, 766 socket_data->next_incoming)) { 767 switch (socket_data->state) { 768 case TCP_SOCKET_FIN_WAIT_1: 769 case TCP_SOCKET_FIN_WAIT_2: 770 case TCP_SOCKET_CLOSING: 771 socket_data->state = TCP_SOCKET_CLOSING; 772 break; 643 if(IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, socket_data->next_incoming)){ 644 switch(socket_data->state){ 645 case TCP_SOCKET_FIN_WAIT_1: 646 case TCP_SOCKET_FIN_WAIT_2: 647 case TCP_SOCKET_CLOSING: 648 socket_data->state = TCP_SOCKET_CLOSING; 649 break; 773 650 //case TCP_ESTABLISHED: 774 default:775 socket_data->state = TCP_SOCKET_CLOSE_WAIT;776 break;651 default: 652 socket_data->state = TCP_SOCKET_CLOSE_WAIT; 653 break; 777 654 } 778 655 } 779 656 780 657 packet = tcp_get_packets_to_send(socket, socket_data); 781 if (!packet){658 if(! packet){ 782 659 // create the notification packet 783 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, 784 socket_data, 0, 0)); 785 ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, 786 packet, 1)); 787 packet = tcp_send_prepare_packet(socket, socket_data, packet, 1, 788 socket_data->last_outgoing + 1); 789 } 790 660 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 0)); 661 ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, 1)); 662 packet = tcp_send_prepare_packet(socket, socket_data, packet, 1, socket_data->last_outgoing + 1); 663 } 791 664 fibril_rwlock_write_unlock(socket_data->local_lock); 792 793 665 // send the packet 794 666 tcp_send_packets(socket_data->device_id, packet); 795 796 667 return EOK; 797 668 } 798 669 799 int 800 tcp_queue_received_packet(socket_core_ref socket, 801 tcp_socket_data_ref socket_data, packet_t packet, int fragments, 802 size_t total_length) 803 { 670 int tcp_queue_received_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, int fragments, size_t total_length){ 804 671 ERROR_DECLARE; 805 672 … … 814 681 815 682 // queue the received packet 816 if (ERROR_OCCURRED(dyn_fifo_push(&socket->received, 817 packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) || 818 ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone, 819 &tcp_globals.dimensions, socket_data->device_id, 820 &packet_dimension))) { 683 if(ERROR_OCCURRED(dyn_fifo_push(&socket->received, packet_get_id(packet), SOCKET_MAX_RECEIVED_SIZE)) 684 || ERROR_OCCURRED(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension))){ 821 685 return tcp_release_and_return(packet, ERROR_CODE); 822 686 } … … 826 690 827 691 // notify the destination socket 828 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 829 (ipcarg_t) socket->socket_id, 830 ((packet_dimension->content < socket_data->data_fragment_size) ? 831 packet_dimension->content : socket_data->data_fragment_size), 0, 0, 832 (ipcarg_t) fragments); 833 692 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, (ipcarg_t) socket->socket_id, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size), 0, 0, (ipcarg_t) fragments); 834 693 return EOK; 835 694 } 836 695 837 int 838 tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, 839 tcp_header_ref header, packet_t packet) 840 { 696 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){ 841 697 ERROR_DECLARE; 842 698 … … 849 705 assert(packet); 850 706 851 if (!header->synchronize) 852 return tcp_release_and_return(packet, EINVAL); 853 854 // process acknowledgement 855 tcp_process_acknowledgement(socket, socket_data, header); 856 857 socket_data->next_incoming = ntohl(header->sequence_number) + 1; 858 // release additional packets 859 next_packet = pq_detach(packet); 860 if (next_packet) { 861 pq_release_remote(tcp_globals.net_phone, 862 packet_get_id(next_packet)); 863 } 864 // trim if longer than the header 865 if ((packet_get_data_length(packet) > sizeof(*header)) && 866 ERROR_OCCURRED(packet_trim(packet, 0, 867 packet_get_data_length(packet) - sizeof(*header)))) { 868 return tcp_release_and_return(packet, ERROR_CODE); 869 } 870 tcp_prepare_operation_header(socket, socket_data, header, 0, 0); 871 fibril_mutex_lock(&socket_data->operation.mutex); 872 socket_data->operation.result = tcp_queue_packet(socket, socket_data, 873 packet, 1); 874 if (socket_data->operation.result == EOK) { 875 socket_data->state = TCP_SOCKET_ESTABLISHED; 876 packet = tcp_get_packets_to_send(socket, socket_data); 877 if (packet) { 878 fibril_rwlock_write_unlock( socket_data->local_lock); 879 // send the packet 880 tcp_send_packets(socket_data->device_id, packet); 881 // signal the result 882 fibril_condvar_signal( &socket_data->operation.condvar); 883 fibril_mutex_unlock( &socket_data->operation.mutex); 884 return EOK; 885 } 886 } 887 fibril_mutex_unlock(&socket_data->operation.mutex); 707 if(header->synchronize){ 708 // process acknowledgement 709 tcp_process_acknowledgement(socket, socket_data, header); 710 711 socket_data->next_incoming = ntohl(header->sequence_number) + 1; 712 // release additional packets 713 next_packet = pq_detach(packet); 714 if(next_packet){ 715 pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet)); 716 } 717 // trim if longer than the header 718 if((packet_get_data_length(packet) > sizeof(*header)) 719 && ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){ 720 return tcp_release_and_return(packet, ERROR_CODE); 721 } 722 tcp_prepare_operation_header(socket, socket_data, header, 0, 0); 723 fibril_mutex_lock(&socket_data->operation.mutex); 724 socket_data->operation.result = tcp_queue_packet(socket, socket_data, packet, 1); 725 if(socket_data->operation.result == EOK){ 726 socket_data->state = TCP_SOCKET_ESTABLISHED; 727 packet = tcp_get_packets_to_send(socket, socket_data); 728 if(packet){ 729 fibril_rwlock_write_unlock(socket_data->local_lock); 730 // send the packet 731 tcp_send_packets(socket_data->device_id, packet); 732 // signal the result 733 fibril_condvar_signal(&socket_data->operation.condvar); 734 fibril_mutex_unlock(&socket_data->operation.mutex); 735 return EOK; 736 } 737 } 738 fibril_mutex_unlock(&socket_data->operation.mutex); 739 } 888 740 return tcp_release_and_return(packet, EINVAL); 889 741 } 890 742 891 int 892 tcp_process_listen(socket_core_ref listening_socket, 893 tcp_socket_data_ref listening_socket_data, tcp_header_ref header, 894 packet_t packet, struct sockaddr *src, struct sockaddr *dest, 895 size_t addrlen) 896 { 743 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){ 897 744 ERROR_DECLARE; 898 745 … … 910 757 assert(packet); 911 758 912 if (!header->synchronize) 913 return tcp_release_and_return(packet, EINVAL); 914 915 socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data)); 916 if (!socket_data) 917 return tcp_release_and_return(packet, ENOMEM); 918 919 tcp_initialize_socket_data(socket_data); 920 socket_data->local_lock = listening_socket_data->local_lock; 921 socket_data->local_sockets = listening_socket_data->local_sockets; 922 socket_data->listening_socket_id = listening_socket->socket_id; 923 socket_data->next_incoming = ntohl(header->sequence_number); 924 socket_data->treshold = socket_data->next_incoming + 925 ntohs(header->window); 926 socket_data->addrlen = addrlen; 927 socket_data->addr = malloc(socket_data->addrlen); 928 if (!socket_data->addr) { 929 free(socket_data); 930 return tcp_release_and_return(packet, ENOMEM); 931 } 932 memcpy(socket_data->addr, src, socket_data->addrlen); 933 socket_data->dest_port = ntohs(header->source_port); 934 if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr, 935 socket_data->addrlen, socket_data->dest_port))) { 936 free(socket_data->addr); 937 free(socket_data); 938 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 939 return ERROR_CODE; 940 } 941 942 // create a socket 943 socket_id = -1; 944 if (ERROR_OCCURRED(socket_create(socket_data->local_sockets, 945 listening_socket->phone, socket_data, &socket_id))) { 946 free(socket_data->addr); 947 free(socket_data); 948 return tcp_release_and_return(packet, ERROR_CODE); 949 } 950 951 printf("new_sock %d\n", socket_id); 952 socket_data->pseudo_header = listening_socket_data->pseudo_header; 953 socket_data->headerlen = listening_socket_data->headerlen; 954 listening_socket_data->pseudo_header = NULL; 955 listening_socket_data->headerlen = 0; 956 957 fibril_rwlock_write_unlock(socket_data->local_lock); 958 fibril_rwlock_write_lock(&tcp_globals.lock); 959 960 // find the destination socket 961 listening_socket = socket_port_find(&tcp_globals.sockets, 962 listening_port, SOCKET_MAP_KEY_LISTENING, 0); 963 if ((!listening_socket) || 964 (listening_socket->socket_id != listening_socket_id)) { 965 fibril_rwlock_write_unlock(&tcp_globals.lock); 966 // a shadow may remain until app hangs up 967 return tcp_release_and_return(packet, EOK /*ENOTSOCK*/); 968 } 969 listening_socket_data = 970 (tcp_socket_data_ref) listening_socket->specific_data; 971 assert(listening_socket_data); 972 973 fibril_rwlock_write_lock(listening_socket_data->local_lock); 974 975 socket = socket_cores_find(listening_socket_data->local_sockets, 976 socket_id); 977 if (!socket) { 978 // where is the socket?!? 979 fibril_rwlock_write_unlock(&tcp_globals.lock); 980 return ENOTSOCK; 981 } 982 socket_data = (tcp_socket_data_ref) socket->specific_data; 983 assert(socket_data); 984 985 ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port, 986 socket, (const char *) socket_data->addr, socket_data->addrlen); 987 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, 988 (const char *) socket_data->addr, socket_data->addrlen)); 989 990 // ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket, 991 // TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, 992 // tcp_globals.last_used_port); 993 // tcp_globals.last_used_port = socket->port; 994 fibril_rwlock_write_unlock(&tcp_globals.lock); 995 if (ERROR_CODE != EOK) { 996 socket_destroy(tcp_globals.net_phone, socket->socket_id, 997 socket_data->local_sockets, &tcp_globals.sockets, 998 tcp_free_socket_data); 999 return tcp_release_and_return(packet, ERROR_CODE); 1000 } 1001 1002 socket_data->state = TCP_SOCKET_LISTEN; 1003 socket_data->next_incoming = ntohl(header->sequence_number) + 1; 1004 1005 // release additional packets 1006 next_packet = pq_detach(packet); 1007 if (next_packet) { 1008 pq_release_remote(tcp_globals.net_phone, 1009 packet_get_id(next_packet)); 1010 } 1011 1012 // trim if longer than the header 1013 if ((packet_get_data_length(packet) > sizeof(*header)) && 1014 ERROR_OCCURRED(packet_trim(packet, 0, 1015 packet_get_data_length(packet) - sizeof(*header)))) { 1016 socket_destroy(tcp_globals.net_phone, socket->socket_id, 1017 socket_data->local_sockets, &tcp_globals.sockets, 1018 tcp_free_socket_data); 1019 return tcp_release_and_return(packet, ERROR_CODE); 1020 } 1021 1022 tcp_prepare_operation_header(socket, socket_data, header, 1, 0); 1023 1024 if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))) { 1025 socket_destroy(tcp_globals.net_phone, socket->socket_id, 1026 socket_data->local_sockets, &tcp_globals.sockets, 1027 tcp_free_socket_data); 1028 return ERROR_CODE; 1029 } 1030 1031 packet = tcp_get_packets_to_send(socket, socket_data); 1032 if (!packet) { 1033 socket_destroy(tcp_globals.net_phone, socket->socket_id, 1034 socket_data->local_sockets, &tcp_globals.sockets, 1035 tcp_free_socket_data); 1036 return EINVAL; 1037 } 1038 1039 socket_data->state = TCP_SOCKET_SYN_RECEIVED; 1040 fibril_rwlock_write_unlock(socket_data->local_lock); 1041 1042 // send the packet 1043 tcp_send_packets(socket_data->device_id, packet); 1044 1045 return EOK; 1046 } 1047 1048 int 1049 tcp_process_syn_received(socket_core_ref socket, 1050 tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet) 1051 { 759 // printf("syn %d\n", header->synchronize); 760 if(header->synchronize){ 761 socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data)); 762 if(! socket_data){ 763 return tcp_release_and_return(packet, ENOMEM); 764 }else{ 765 tcp_initialize_socket_data(socket_data); 766 socket_data->local_lock = listening_socket_data->local_lock; 767 socket_data->local_sockets = listening_socket_data->local_sockets; 768 socket_data->listening_socket_id = listening_socket->socket_id; 769 770 socket_data->next_incoming = ntohl(header->sequence_number); 771 socket_data->treshold = socket_data->next_incoming + ntohs(header->window); 772 773 socket_data->addrlen = addrlen; 774 socket_data->addr = malloc(socket_data->addrlen); 775 if(! socket_data->addr){ 776 free(socket_data); 777 return tcp_release_and_return(packet, ENOMEM); 778 } 779 memcpy(socket_data->addr, src, socket_data->addrlen); 780 781 socket_data->dest_port = ntohs(header->source_port); 782 if(ERROR_OCCURRED(tl_set_address_port(socket_data->addr, socket_data->addrlen, socket_data->dest_port))){ 783 free(socket_data->addr); 784 free(socket_data); 785 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 786 return ERROR_CODE; 787 } 788 789 // printf("addr %p\n", socket_data->addr, socket_data->addrlen); 790 // create a socket 791 socket_id = -1; 792 if(ERROR_OCCURRED(socket_create(socket_data->local_sockets, listening_socket->phone, socket_data, &socket_id))){ 793 free(socket_data->addr); 794 free(socket_data); 795 return tcp_release_and_return(packet, ERROR_CODE); 796 } 797 798 printf("new_sock %d\n", socket_id); 799 socket_data->pseudo_header = listening_socket_data->pseudo_header; 800 socket_data->headerlen = listening_socket_data->headerlen; 801 listening_socket_data->pseudo_header = NULL; 802 listening_socket_data->headerlen = 0; 803 804 fibril_rwlock_write_unlock(socket_data->local_lock); 805 // printf("list lg\n"); 806 fibril_rwlock_write_lock(&tcp_globals.lock); 807 // printf("list locked\n"); 808 // find the destination socket 809 listening_socket = socket_port_find(&tcp_globals.sockets, listening_port, SOCKET_MAP_KEY_LISTENING, 0); 810 if((! listening_socket) || (listening_socket->socket_id != listening_socket_id)){ 811 fibril_rwlock_write_unlock(&tcp_globals.lock); 812 // a shadow may remain until app hangs up 813 return tcp_release_and_return(packet, EOK/*ENOTSOCK*/); 814 } 815 // printf("port %d\n", listening_socket->port); 816 listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data; 817 assert(listening_socket_data); 818 819 // printf("list ll\n"); 820 fibril_rwlock_write_lock(listening_socket_data->local_lock); 821 // printf("list locked\n"); 822 823 socket = socket_cores_find(listening_socket_data->local_sockets, socket_id); 824 if(! socket){ 825 // where is the socket?!? 826 fibril_rwlock_write_unlock(&tcp_globals.lock); 827 return ENOTSOCK; 828 } 829 socket_data = (tcp_socket_data_ref) socket->specific_data; 830 assert(socket_data); 831 832 // uint8_t * data = socket_data->addr; 833 // 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]); 834 835 ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port, socket, (const char *) socket_data->addr, socket_data->addrlen); 836 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, (const char *) socket_data->addr, socket_data->addrlen)); 837 //ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port); 838 //tcp_globals.last_used_port = socket->port; 839 // printf("bound %d\n", socket->port); 840 fibril_rwlock_write_unlock(&tcp_globals.lock); 841 if(ERROR_CODE != EOK){ 842 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data); 843 return tcp_release_and_return(packet, ERROR_CODE); 844 } 845 846 socket_data->state = TCP_SOCKET_LISTEN; 847 socket_data->next_incoming = ntohl(header->sequence_number) + 1; 848 // release additional packets 849 next_packet = pq_detach(packet); 850 if(next_packet){ 851 pq_release_remote(tcp_globals.net_phone, packet_get_id(next_packet)); 852 } 853 // trim if longer than the header 854 if((packet_get_data_length(packet) > sizeof(*header)) 855 && ERROR_OCCURRED(packet_trim(packet, 0, packet_get_data_length(packet) - sizeof(*header)))){ 856 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data); 857 return tcp_release_and_return(packet, ERROR_CODE); 858 } 859 tcp_prepare_operation_header(socket, socket_data, header, 1, 0); 860 if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1))){ 861 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data); 862 return ERROR_CODE; 863 } 864 packet = tcp_get_packets_to_send(socket, socket_data); 865 // printf("send %d\n", packet_get_id(packet)); 866 if(! packet){ 867 socket_destroy(tcp_globals.net_phone, socket->socket_id, socket_data->local_sockets, &tcp_globals.sockets, tcp_free_socket_data); 868 return EINVAL; 869 }else{ 870 socket_data->state = TCP_SOCKET_SYN_RECEIVED; 871 // printf("unlock\n"); 872 fibril_rwlock_write_unlock(socket_data->local_lock); 873 // send the packet 874 tcp_send_packets(socket_data->device_id, packet); 875 return EOK; 876 } 877 } 878 } 879 return tcp_release_and_return(packet, EINVAL); 880 } 881 882 int tcp_process_syn_received(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, packet_t packet){ 1052 883 ERROR_DECLARE; 1053 884 … … 1061 892 assert(packet); 1062 893 1063 if (!header->acknowledge) 894 printf("syn_rec\n"); 895 if(header->acknowledge){ 896 // process acknowledgement 897 tcp_process_acknowledgement(socket, socket_data, header); 898 899 socket_data->next_incoming = ntohl(header->sequence_number);// + 1; 900 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 901 socket_data->state = TCP_SOCKET_ESTABLISHED; 902 listening_socket = socket_cores_find(socket_data->local_sockets, socket_data->listening_socket_id); 903 if(listening_socket){ 904 listening_socket_data = (tcp_socket_data_ref) listening_socket->specific_data; 905 assert(listening_socket_data); 906 907 // queue the received packet 908 if(! ERROR_OCCURRED(dyn_fifo_push(&listening_socket->accepted, (-1 * socket->socket_id), listening_socket_data->backlog))){ 909 // notify the destination socket 910 async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, (ipcarg_t) listening_socket->socket_id, socket_data->data_fragment_size, TCP_HEADER_SIZE, 0, (ipcarg_t) socket->socket_id); 911 fibril_rwlock_write_unlock(socket_data->local_lock); 912 return EOK; 913 } 914 } 915 // send FIN 916 socket_data->state = TCP_SOCKET_FIN_WAIT_1; 917 918 // create the notification packet 919 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1)); 920 921 // send the packet 922 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1)); 923 924 // flush packets 925 packet = tcp_get_packets_to_send(socket, socket_data); 926 fibril_rwlock_write_unlock(socket_data->local_lock); 927 if(packet){ 928 // send the packet 929 tcp_send_packets(socket_data->device_id, packet); 930 } 931 return EOK; 932 }else{ 1064 933 return tcp_release_and_return(packet, EINVAL); 1065 1066 // process acknowledgement 1067 tcp_process_acknowledgement(socket, socket_data, header); 1068 1069 socket_data->next_incoming = ntohl(header->sequence_number); // + 1; 1070 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1071 socket_data->state = TCP_SOCKET_ESTABLISHED; 1072 listening_socket = socket_cores_find(socket_data->local_sockets, 1073 socket_data->listening_socket_id); 1074 if (listening_socket) { 1075 listening_socket_data = 1076 (tcp_socket_data_ref) listening_socket->specific_data; 1077 assert(listening_socket_data); 1078 1079 // queue the received packet 1080 if (ERROR_NONE(dyn_fifo_push(&listening_socket->accepted, 1081 (-1 * socket->socket_id), 1082 listening_socket_data->backlog))) { 1083 1084 // notify the destination socket 1085 async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, 1086 (ipcarg_t) listening_socket->socket_id, 1087 socket_data->data_fragment_size, TCP_HEADER_SIZE, 1088 0, (ipcarg_t) socket->socket_id); 1089 1090 fibril_rwlock_write_unlock(socket_data->local_lock); 1091 return EOK; 1092 } 1093 } 1094 // send FIN 1095 socket_data->state = TCP_SOCKET_FIN_WAIT_1; 1096 1097 // create the notification packet 1098 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, 1099 socket_data, 0, 1)); 1100 1101 // send the packet 1102 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1)); 1103 1104 // flush packets 1105 packet = tcp_get_packets_to_send(socket, socket_data); 1106 fibril_rwlock_write_unlock(socket_data->local_lock); 1107 if (packet) { 1108 // send the packet 1109 tcp_send_packets(socket_data->device_id, packet); 1110 } 1111 1112 return EOK; 1113 } 1114 1115 void 1116 tcp_process_acknowledgement(socket_core_ref socket, 1117 tcp_socket_data_ref socket_data, tcp_header_ref header) 1118 { 934 } 935 return EINVAL; 936 } 937 938 void tcp_process_acknowledgement(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header){ 1119 939 size_t number; 1120 940 size_t length; … … 1129 949 assert(header); 1130 950 1131 if (!header->acknowledge) 1132 return; 1133 1134 number = ntohl(header->acknowledgement_number); 1135 // if more data acknowledged 1136 if (number != socket_data->expected) { 1137 old = socket_data->expected; 1138 if (IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing, 1139 number)) { 1140 switch (socket_data->state) { 1141 case TCP_SOCKET_FIN_WAIT_1: 1142 socket_data->state = TCP_SOCKET_FIN_WAIT_2; 1143 break; 1144 case TCP_SOCKET_LAST_ACK: 1145 case TCP_SOCKET_CLOSING: 1146 // fin acknowledged - release the socket in 1147 // another fibril 1148 tcp_prepare_timeout(tcp_release_after_timeout, 1149 socket, socket_data, 0, 1150 TCP_SOCKET_TIME_WAIT, 1151 NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true); 1152 break; 1153 default: 1154 break; 1155 } 1156 } 1157 // update the treshold if higher than set 1158 if (number + ntohs(header->window) > 1159 socket_data->expected + socket_data->treshold) { 1160 socket_data->treshold = number + ntohs(header->window) - 1161 socket_data->expected; 1162 } 1163 // set new expected sequence number 1164 socket_data->expected = number; 1165 socket_data->expected_count = 1; 1166 packet = socket_data->outgoing; 1167 while (pq_get_order(packet, &number, &length) == EOK) { 1168 if (IS_IN_INTERVAL_OVERFLOW((uint32_t) old, 1169 (uint32_t) (number + length), 1170 (uint32_t) socket_data->expected)) { 1171 next = pq_detach(packet); 1172 if (packet == socket_data->outgoing) 1173 socket_data->outgoing = next; 1174 1175 // add to acknowledged or release 1176 if (pq_add(&acknowledged, packet, 0, 0) != EOK) 1177 pq_release_remote(tcp_globals.net_phone, 1178 packet_get_id(packet)); 1179 packet = next; 1180 } else if (old < socket_data->expected) 1181 break; 1182 } 1183 // release acknowledged 1184 if (acknowledged) { 1185 pq_release_remote(tcp_globals.net_phone, 1186 packet_get_id(acknowledged)); 1187 } 1188 return; 951 if(header->acknowledge){ 952 number = ntohl(header->acknowledgement_number); 953 // if more data acknowledged 954 if(number != socket_data->expected){ 955 old = socket_data->expected; 956 if(IS_IN_INTERVAL_OVERFLOW(old, socket_data->fin_outgoing, number)){ 957 switch(socket_data->state){ 958 case TCP_SOCKET_FIN_WAIT_1: 959 socket_data->state = TCP_SOCKET_FIN_WAIT_2; 960 break; 961 case TCP_SOCKET_LAST_ACK: 962 case TCP_SOCKET_CLOSING: 963 // fin acknowledged - release the socket in another fibril 964 tcp_prepare_timeout(tcp_release_after_timeout, socket, socket_data, 0, TCP_SOCKET_TIME_WAIT, NET_DEFAULT_TCP_TIME_WAIT_TIMEOUT, true); 965 break; 966 default: 967 break; 968 } 969 } 970 // update the treshold if higher than set 971 if(number + ntohs(header->window) > socket_data->expected + socket_data->treshold){ 972 socket_data->treshold = number + ntohs(header->window) - socket_data->expected; 973 } 974 // set new expected sequence number 975 socket_data->expected = number; 976 socket_data->expected_count = 1; 977 packet = socket_data->outgoing; 978 while(pq_get_order(packet, &number, &length) == EOK){ 979 if(IS_IN_INTERVAL_OVERFLOW((uint32_t) old, (uint32_t)(number + length), (uint32_t) socket_data->expected)){ 980 next = pq_detach(packet); 981 if(packet == socket_data->outgoing){ 982 socket_data->outgoing = next; 983 } 984 // add to acknowledged or release 985 if(pq_add(&acknowledged, packet, 0, 0) != EOK){ 986 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 987 } 988 packet = next; 989 }else if(old < socket_data->expected){ 990 break; 991 } 992 } 993 // release acknowledged 994 if(acknowledged){ 995 pq_release_remote(tcp_globals.net_phone, packet_get_id(acknowledged)); 996 } 997 return; 1189 998 // if the same as the previous time 1190 } 1191 if (number == socket_data->expected) { 1192 // increase the counter 1193 ++socket_data->expected_count; 1194 if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) { 1195 socket_data->expected_count = 1; 1196 // TODO retransmit lock 1197 //tcp_retransmit_packet(socket, socket_data, number); 1198 } 1199 } 1200 } 1201 1202 int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call, 1203 ipc_call_t * answer, int *answer_count) 1204 { 999 }else if(number == socket_data->expected){ 1000 // increase the counter 1001 ++ socket_data->expected_count; 1002 if(socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT){ 1003 socket_data->expected_count = 1; 1004 // TODO retransmit lock 1005 //tcp_retransmit_packet(socket, socket_data, number); 1006 } 1007 } 1008 } 1009 } 1010 1011 int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){ 1205 1012 ERROR_DECLARE; 1206 1013 … … 1212 1019 1213 1020 *answer_count = 0; 1214 switch (IPC_GET_METHOD(*call)) { 1215 case NET_TL_RECEIVED: 1216 //fibril_rwlock_read_lock(&tcp_globals.lock); 1217 if (ERROR_NONE(packet_translate_remote(tcp_globals.net_phone, 1218 &packet, IPC_GET_PACKET(call)))) { 1219 ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call), 1220 packet, SERVICE_TCP, IPC_GET_ERROR(call)); 1221 } 1222 //fibril_rwlock_read_unlock(&tcp_globals.lock); 1223 return ERROR_CODE; 1224 1225 case IPC_M_CONNECT_TO_ME: 1226 return tcp_process_client_messages(callid, *call); 1227 } 1228 1021 switch(IPC_GET_METHOD(*call)){ 1022 case NET_TL_RECEIVED: 1023 //fibril_rwlock_read_lock(&tcp_globals.lock); 1024 if(! ERROR_OCCURRED(packet_translate_remote(tcp_globals.net_phone, &packet, IPC_GET_PACKET(call)))){ 1025 ERROR_CODE = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP, IPC_GET_ERROR(call)); 1026 } 1027 //fibril_rwlock_read_unlock(&tcp_globals.lock); 1028 return ERROR_CODE; 1029 case IPC_M_CONNECT_TO_ME: 1030 return tcp_process_client_messages(callid, * call); 1031 } 1229 1032 return ENOTSUP; 1230 1033 } 1231 1034 1232 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data) 1233 { 1035 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data){ 1234 1036 assert(socket_data); 1235 1037 … … 1246 1048 } 1247 1049 1248 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data) 1249 { 1050 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data){ 1250 1051 assert(socket_data); 1251 1052 … … 1256 1057 } 1257 1058 1258 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call) 1259 { 1059 int tcp_process_client_messages(ipc_callid_t callid, ipc_call_t call){ 1260 1060 int res; 1261 1061 bool keep_on_going = true; 1262 1062 socket_cores_t local_sockets; 1263 1063 int app_phone = IPC_GET_PHONE(&call); 1264 struct sockaddr * addr;1064 struct sockaddr * addr; 1265 1065 int socket_id; 1266 1066 size_t addrlen; … … 1283 1083 fibril_rwlock_initialize(&lock); 1284 1084 1285 while (keep_on_going){1085 while(keep_on_going){ 1286 1086 1287 1087 // answer the call 1288 1088 answer_call(callid, res, &answer, answer_count); 1089 1289 1090 // refresh data 1290 1091 refresh_answer(&answer, &answer_count); 1092 1291 1093 // get the next call 1292 1094 callid = async_get_call(&call); 1293 1095 1294 1096 // process the call 1295 switch (IPC_GET_METHOD(call)) { 1296 case IPC_M_PHONE_HUNGUP: 1297 keep_on_going = false; 1298 res = EHANGUP; 1299 break; 1300 1301 case NET_SOCKET: 1302 socket_data = 1303 (tcp_socket_data_ref) malloc(sizeof(*socket_data)); 1304 if (!socket_data) { 1305 res = ENOMEM; 1097 switch(IPC_GET_METHOD(call)){ 1098 case IPC_M_PHONE_HUNGUP: 1099 keep_on_going = false; 1100 res = EHANGUP; 1306 1101 break; 1307 } 1308 1309 tcp_initialize_socket_data(socket_data); 1310 socket_data->local_lock = &lock; 1311 socket_data->local_sockets = &local_sockets; 1312 fibril_rwlock_write_lock(&lock); 1313 socket_id = SOCKET_GET_SOCKET_ID(call); 1314 res = socket_create(&local_sockets, app_phone, 1315 socket_data, &socket_id); 1316 SOCKET_SET_SOCKET_ID(answer, socket_id); 1317 fibril_rwlock_write_unlock(&lock); 1318 if (res != EOK) { 1319 free(socket_data); 1102 case NET_SOCKET: 1103 socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data)); 1104 if(! socket_data){ 1105 res = ENOMEM; 1106 }else{ 1107 tcp_initialize_socket_data(socket_data); 1108 socket_data->local_lock = &lock; 1109 socket_data->local_sockets = &local_sockets; 1110 fibril_rwlock_write_lock(&lock); 1111 socket_id = SOCKET_GET_SOCKET_ID(call); 1112 res = socket_create(&local_sockets, app_phone, socket_data, &socket_id); 1113 SOCKET_SET_SOCKET_ID(answer, socket_id); 1114 fibril_rwlock_write_unlock(&lock); 1115 if(res == EOK){ 1116 if (tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, DEVICE_INVALID_ID, &packet_dimension) == EOK){ 1117 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size)); 1118 } 1119 // SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE); 1120 SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE); 1121 answer_count = 3; 1122 }else{ 1123 free(socket_data); 1124 } 1125 } 1320 1126 break; 1321 }1322 if (tl_get_ip_packet_dimension(tcp_globals.ip_phone,1323 &tcp_globals.dimensions, DEVICE_INVALID_ID,1324 &packet_dimension) == EOK) {1325 SOCKET_SET_DATA_FRAGMENT_SIZE(answer,1326 ((packet_dimension->content <1327 socket_data->data_fragment_size) ?1328 packet_dimension->content :1329 socket_data->data_fragment_size));1330 }1331 // SOCKET_SET_DATA_FRAGMENT_SIZE(answer, MAX_TCP_FRAGMENT_SIZE);1332 SOCKET_SET_HEADER_SIZE(answer, TCP_HEADER_SIZE);1333 answer_count = 3;1334 break;1335 1336 case NET_SOCKET_BIND:1337 res = data_receive((void **) &addr, &addrlen);1338 if (res != EOK)1127 case NET_SOCKET_BIND: 1128 res = data_receive((void **) &addr, &addrlen); 1129 if(res == EOK){ 1130 fibril_rwlock_write_lock(&tcp_globals.lock); 1131 fibril_rwlock_write_lock(&lock); 1132 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); 1133 if(res == EOK){ 1134 socket = socket_cores_find(&local_sockets, SOCKET_GET_SOCKET_ID(call)); 1135 if(socket){ 1136 socket_data = (tcp_socket_data_ref) socket->specific_data; 1137 assert(socket_data); 1138 socket_data->state = TCP_SOCKET_LISTEN; 1139 } 1140 } 1141 fibril_rwlock_write_unlock(&lock); 1142 fibril_rwlock_write_unlock(&tcp_globals.lock); 1143 free(addr); 1144 } 1339 1145 break; 1340 fibril_rwlock_write_lock(&tcp_globals.lock); 1341 fibril_rwlock_write_lock(&lock); 1342 res = socket_bind(&local_sockets, &tcp_globals.sockets, 1343 SOCKET_GET_SOCKET_ID(call), addr, addrlen, 1344 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, 1345 tcp_globals.last_used_port); 1346 if (res == EOK) { 1347 socket = socket_cores_find(&local_sockets, 1348 SOCKET_GET_SOCKET_ID(call)); 1349 if (socket) { 1350 socket_data = (tcp_socket_data_ref) 1351 socket->specific_data; 1352 assert(socket_data); 1353 socket_data->state = TCP_SOCKET_LISTEN; 1146 case NET_SOCKET_LISTEN: 1147 fibril_rwlock_read_lock(&tcp_globals.lock); 1148 // fibril_rwlock_write_lock(&tcp_globals.lock); 1149 fibril_rwlock_write_lock(&lock); 1150 res = tcp_listen_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_BACKLOG(call)); 1151 fibril_rwlock_write_unlock(&lock); 1152 // fibril_rwlock_write_unlock(&tcp_globals.lock); 1153 fibril_rwlock_read_unlock(&tcp_globals.lock); 1154 break; 1155 case NET_SOCKET_CONNECT: 1156 res = data_receive((void **) &addr, &addrlen); 1157 if(res == EOK){ 1158 // the global lock may be released in the tcp_connect_message() function 1159 fibril_rwlock_write_lock(&tcp_globals.lock); 1160 fibril_rwlock_write_lock(&lock); 1161 res = tcp_connect_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), addr, addrlen); 1162 if(res != EOK){ 1163 fibril_rwlock_write_unlock(&lock); 1164 fibril_rwlock_write_unlock(&tcp_globals.lock); 1165 free(addr); 1166 } 1354 1167 } 1355 }1356 fibril_rwlock_write_unlock(&lock);1357 fibril_rwlock_write_unlock(&tcp_globals.lock);1358 free(addr);1359 break;1360 1361 case NET_SOCKET_LISTEN:1362 fibril_rwlock_read_lock(&tcp_globals.lock);1363 // fibril_rwlock_write_lock(&tcp_globals.lock);1364 fibril_rwlock_write_lock(&lock);1365 res = tcp_listen_message(&local_sockets,1366 SOCKET_GET_SOCKET_ID(call),1367 SOCKET_GET_BACKLOG(call));1368 fibril_rwlock_write_unlock(&lock);1369 // fibril_rwlock_write_unlock(&tcp_globals.lock);1370 fibril_rwlock_read_unlock(&tcp_globals.lock);1371 break;1372 1373 case NET_SOCKET_CONNECT:1374 res = data_receive((void **) &addr, &addrlen);1375 if (res != EOK)1376 1168 break; 1377 // the global lock may be released in the 1378 // tcp_connect_message() function 1379 fibril_rwlock_write_lock(&tcp_globals.lock); 1380 fibril_rwlock_write_lock(&lock); 1381 res = tcp_connect_message(&local_sockets, 1382 SOCKET_GET_SOCKET_ID(call), addr, addrlen); 1383 if (res != EOK) { 1384 fibril_rwlock_write_unlock(&lock); 1385 fibril_rwlock_write_unlock(&tcp_globals.lock); 1386 free(addr); 1387 } 1388 break; 1389 1390 case NET_SOCKET_ACCEPT: 1391 fibril_rwlock_read_lock(&tcp_globals.lock); 1392 fibril_rwlock_write_lock(&lock); 1393 res = tcp_accept_message(&local_sockets, 1394 SOCKET_GET_SOCKET_ID(call), 1395 SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen); 1396 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 1397 fibril_rwlock_write_unlock(&lock); 1398 fibril_rwlock_read_unlock(&tcp_globals.lock); 1399 if (res > 0) { 1400 SOCKET_SET_SOCKET_ID(answer, res); 1401 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen); 1402 answer_count = 3; 1403 } 1404 break; 1405 1406 case NET_SOCKET_SEND: 1407 fibril_rwlock_read_lock(&tcp_globals.lock); 1408 fibril_rwlock_write_lock(&lock); 1409 res = tcp_send_message(&local_sockets, 1410 SOCKET_GET_SOCKET_ID(call), 1411 SOCKET_GET_DATA_FRAGMENTS(call), &size, 1412 SOCKET_GET_FLAGS(call)); 1413 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 1414 if (res != EOK) { 1169 case NET_SOCKET_ACCEPT: 1170 fibril_rwlock_read_lock(&tcp_globals.lock); 1171 fibril_rwlock_write_lock(&lock); 1172 res = tcp_accept_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_NEW_SOCKET_ID(call), &size, &addrlen); 1173 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 1415 1174 fibril_rwlock_write_unlock(&lock); 1416 1175 fibril_rwlock_read_unlock(&tcp_globals.lock); 1417 } else { 1418 answer_count = 2; 1419 } 1420 break; 1421 1422 case NET_SOCKET_SENDTO: 1423 res = data_receive((void **) &addr, &addrlen); 1424 if (res != EOK) 1176 if(res > 0){ 1177 SOCKET_SET_SOCKET_ID(answer, res); 1178 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen); 1179 answer_count = 3; 1180 } 1425 1181 break; 1426 fibril_rwlock_read_lock(&tcp_globals.lock); 1427 fibril_rwlock_write_lock(&lock); 1428 res = tcp_send_message(&local_sockets, 1429 SOCKET_GET_SOCKET_ID(call), 1430 SOCKET_GET_DATA_FRAGMENTS(call), &size, 1431 SOCKET_GET_FLAGS(call)); 1432 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 1433 if (res != EOK) { 1182 case NET_SOCKET_SEND: 1183 fibril_rwlock_read_lock(&tcp_globals.lock); 1184 fibril_rwlock_write_lock(&lock); 1185 res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call)); 1186 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 1187 if(res != EOK){ 1188 fibril_rwlock_write_unlock(&lock); 1189 fibril_rwlock_read_unlock(&tcp_globals.lock); 1190 }else{ 1191 answer_count = 2; 1192 } 1193 break; 1194 case NET_SOCKET_SENDTO: 1195 res = data_receive((void **) &addr, &addrlen); 1196 if(res == EOK){ 1197 fibril_rwlock_read_lock(&tcp_globals.lock); 1198 fibril_rwlock_write_lock(&lock); 1199 res = tcp_send_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_DATA_FRAGMENTS(call), &size, SOCKET_GET_FLAGS(call)); 1200 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size); 1201 if(res != EOK){ 1202 fibril_rwlock_write_unlock(&lock); 1203 fibril_rwlock_read_unlock(&tcp_globals.lock); 1204 }else{ 1205 answer_count = 2; 1206 } 1207 free(addr); 1208 } 1209 break; 1210 case NET_SOCKET_RECV: 1211 fibril_rwlock_read_lock(&tcp_globals.lock); 1212 fibril_rwlock_write_lock(&lock); 1213 res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), NULL); 1434 1214 fibril_rwlock_write_unlock(&lock); 1435 1215 fibril_rwlock_read_unlock(&tcp_globals.lock); 1436 } else { 1437 answer_count = 2; 1438 } 1439 free(addr); 1440 break; 1441 1442 case NET_SOCKET_RECV: 1443 fibril_rwlock_read_lock(&tcp_globals.lock); 1444 fibril_rwlock_write_lock(&lock); 1445 res = tcp_recvfrom_message(&local_sockets, 1446 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), 1447 NULL); 1448 fibril_rwlock_write_unlock(&lock); 1449 fibril_rwlock_read_unlock(&tcp_globals.lock); 1450 if (res > 0) { 1451 SOCKET_SET_READ_DATA_LENGTH(answer, res); 1452 answer_count = 1; 1453 res = EOK; 1454 } 1455 break; 1456 1457 case NET_SOCKET_RECVFROM: 1458 fibril_rwlock_read_lock(&tcp_globals.lock); 1459 fibril_rwlock_write_lock(&lock); 1460 res = tcp_recvfrom_message(&local_sockets, 1461 SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), 1462 &addrlen); 1463 fibril_rwlock_write_unlock(&lock); 1464 fibril_rwlock_read_unlock(&tcp_globals.lock); 1465 if (res > 0) { 1466 SOCKET_SET_READ_DATA_LENGTH(answer, res); 1467 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen); 1468 answer_count = 3; 1469 res = EOK; 1470 } 1471 break; 1472 1473 case NET_SOCKET_CLOSE: 1474 fibril_rwlock_write_lock(&tcp_globals.lock); 1475 fibril_rwlock_write_lock(&lock); 1476 res = tcp_close_message(&local_sockets, 1477 SOCKET_GET_SOCKET_ID(call)); 1478 if (res != EOK) { 1216 if(res > 0){ 1217 SOCKET_SET_READ_DATA_LENGTH(answer, res); 1218 answer_count = 1; 1219 res = EOK; 1220 } 1221 break; 1222 case NET_SOCKET_RECVFROM: 1223 fibril_rwlock_read_lock(&tcp_globals.lock); 1224 fibril_rwlock_write_lock(&lock); 1225 res = tcp_recvfrom_message(&local_sockets, SOCKET_GET_SOCKET_ID(call), SOCKET_GET_FLAGS(call), &addrlen); 1479 1226 fibril_rwlock_write_unlock(&lock); 1480 fibril_rwlock_write_unlock(&tcp_globals.lock); 1481 } 1482 break; 1483 1484 case NET_SOCKET_GETSOCKOPT: 1485 case NET_SOCKET_SETSOCKOPT: 1486 default: 1487 res = ENOTSUP; 1488 break; 1227 fibril_rwlock_read_unlock(&tcp_globals.lock); 1228 if(res > 0){ 1229 SOCKET_SET_READ_DATA_LENGTH(answer, res); 1230 SOCKET_SET_ADDRESS_LENGTH(answer, addrlen); 1231 answer_count = 3; 1232 res = EOK; 1233 } 1234 break; 1235 case NET_SOCKET_CLOSE: 1236 fibril_rwlock_write_lock(&tcp_globals.lock); 1237 fibril_rwlock_write_lock(&lock); 1238 res = tcp_close_message(&local_sockets, SOCKET_GET_SOCKET_ID(call)); 1239 if(res != EOK){ 1240 fibril_rwlock_write_unlock(&lock); 1241 fibril_rwlock_write_unlock(&tcp_globals.lock); 1242 } 1243 break; 1244 case NET_SOCKET_GETSOCKOPT: 1245 case NET_SOCKET_SETSOCKOPT: 1246 default: 1247 res = ENOTSUP; 1248 break; 1489 1249 } 1490 1250 } … … 1495 1255 printf("release\n"); 1496 1256 // release all local sockets 1497 socket_cores_release(tcp_globals.net_phone, &local_sockets, 1498 &tcp_globals.sockets, tcp_free_socket_data); 1257 socket_cores_release(tcp_globals.net_phone, &local_sockets, &tcp_globals.sockets, tcp_free_socket_data); 1499 1258 1500 1259 return EOK; 1501 1260 } 1502 1261 1503 int tcp_timeout(void *data) 1504 { 1262 int tcp_timeout(void * data){ 1505 1263 tcp_timeout_ref timeout = data; 1506 1264 int keep_write_lock = false; … … 1513 1271 async_usleep(timeout->timeout); 1514 1272 // lock the globals 1515 if (timeout->globals_read_only)1273 if(timeout->globals_read_only){ 1516 1274 fibril_rwlock_read_lock(&tcp_globals.lock); 1517 else1275 }else{ 1518 1276 fibril_rwlock_write_lock(&tcp_globals.lock); 1519 1277 } 1520 1278 // find the pending operation socket 1521 socket = socket_port_find(&tcp_globals.sockets, timeout->port, 1522 timeout->key, timeout->key_length); 1523 if (!(socket && (socket->socket_id == timeout->socket_id))) 1524 goto out; 1525 1526 socket_data = (tcp_socket_data_ref) socket->specific_data; 1527 assert(socket_data); 1528 if (socket_data->local_sockets != timeout->local_sockets) 1529 goto out; 1530 1531 fibril_rwlock_write_lock(socket_data->local_lock); 1532 if (timeout->sequence_number) { 1533 // increase the timeout counter; 1534 ++socket_data->timeout_count; 1535 if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) { 1536 // TODO release as connection lost 1537 //tcp_refresh_socket_data(socket_data); 1538 fibril_rwlock_write_unlock(socket_data->local_lock); 1539 } else { 1540 // retransmit 1541 // tcp_retransmit_packet(socket, 1542 // socket_data, timeout->sequence_number); 1543 fibril_rwlock_write_unlock(socket_data->local_lock); 1544 } 1545 } else { 1546 fibril_mutex_lock(&socket_data->operation.mutex); 1547 // set the timeout operation result if state not 1548 // changed 1549 if (socket_data->state == timeout->state) { 1550 socket_data->operation.result = ETIMEOUT; 1551 // notify the main fibril 1552 fibril_condvar_signal(&socket_data->operation.condvar); 1553 // keep the global write lock 1554 keep_write_lock = true; 1555 } else { 1556 // operation is ok, do nothing 1557 // unlocking from now on, so the unlocki 1558 // order does not matter... 1559 fibril_rwlock_write_unlock(socket_data->local_lock); 1560 } 1561 fibril_mutex_unlock(&socket_data->operation.mutex); 1562 } 1563 1564 out: 1279 socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length); 1280 if(socket && (socket->socket_id == timeout->socket_id)){ 1281 socket_data = (tcp_socket_data_ref) socket->specific_data; 1282 assert(socket_data); 1283 if(socket_data->local_sockets == timeout->local_sockets){ 1284 fibril_rwlock_write_lock(socket_data->local_lock); 1285 if(timeout->sequence_number){ 1286 // increase the timeout counter; 1287 ++ socket_data->timeout_count; 1288 if(socket_data->timeout_count == TCP_MAX_TIMEOUTS){ 1289 // TODO release as connection lost 1290 //tcp_refresh_socket_data(socket_data); 1291 fibril_rwlock_write_unlock(socket_data->local_lock); 1292 }else{ 1293 // retransmit 1294 // tcp_retransmit_packet(socket, socket_data, timeout->sequence_number); 1295 fibril_rwlock_write_unlock(socket_data->local_lock); 1296 } 1297 }else{ 1298 fibril_mutex_lock(&socket_data->operation.mutex); 1299 // set the timeout operation result if state not changed 1300 if(socket_data->state == timeout->state){ 1301 socket_data->operation.result = ETIMEOUT; 1302 // notify the main fibril 1303 fibril_condvar_signal(&socket_data->operation.condvar); 1304 // keep the global write lock 1305 keep_write_lock = true; 1306 }else{ 1307 // operation is ok, do nothing 1308 // unlocking from now on, so the unlock order does not matter... 1309 fibril_rwlock_write_unlock(socket_data->local_lock); 1310 } 1311 fibril_mutex_unlock(&socket_data->operation.mutex); 1312 } 1313 } 1314 } 1565 1315 // unlock only if no socket 1566 if (timeout->globals_read_only)1316 if(timeout->globals_read_only){ 1567 1317 fibril_rwlock_read_unlock(&tcp_globals.lock); 1568 else if (!keep_write_lock)1318 }else if(! keep_write_lock){ 1569 1319 // release if not desired 1570 1320 fibril_rwlock_write_unlock(&tcp_globals.lock); 1571 1321 } 1572 1322 // release the timeout structure 1573 1323 free(timeout); … … 1575 1325 } 1576 1326 1577 int tcp_release_after_timeout(void *data) 1578 { 1327 int tcp_release_after_timeout(void * data){ 1579 1328 tcp_timeout_ref timeout = data; 1580 1329 socket_core_ref socket; 1581 1330 tcp_socket_data_ref socket_data; 1582 fibril_rwlock_t * local_lock;1331 fibril_rwlock_t * local_lock; 1583 1332 1584 1333 assert(timeout); … … 1589 1338 fibril_rwlock_write_lock(&tcp_globals.lock); 1590 1339 // find the pending operation socket 1591 socket = socket_port_find(&tcp_globals.sockets, timeout->port, 1592 timeout->key, timeout->key_length); 1593 if (socket && (socket->socket_id == timeout->socket_id)) { 1340 socket = socket_port_find(&tcp_globals.sockets, timeout->port, timeout->key, timeout->key_length); 1341 if(socket && (socket->socket_id == timeout->socket_id)){ 1594 1342 socket_data = (tcp_socket_data_ref) socket->specific_data; 1595 1343 assert(socket_data); 1596 if (socket_data->local_sockets == timeout->local_sockets){1344 if(socket_data->local_sockets == timeout->local_sockets){ 1597 1345 local_lock = socket_data->local_lock; 1598 1346 fibril_rwlock_write_lock(local_lock); 1599 socket_destroy(tcp_globals.net_phone, 1600 timeout->socket_id, timeout->local_sockets, 1601 &tcp_globals.sockets, tcp_free_socket_data); 1347 socket_destroy(tcp_globals.net_phone, timeout->socket_id, timeout->local_sockets, &tcp_globals.sockets, tcp_free_socket_data); 1602 1348 fibril_rwlock_write_unlock(local_lock); 1603 1349 } … … 1610 1356 } 1611 1357 1612 void 1613 tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, 1614 size_t sequence_number) 1615 { 1358 void tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, size_t sequence_number){ 1616 1359 packet_t packet; 1617 1360 packet_t copy; … … 1625 1368 packet = pq_find(socket_data->outgoing, sequence_number); 1626 1369 printf("retransmit %d\n", packet_get_id(packet)); 1627 if (packet){1370 if(packet){ 1628 1371 pq_get_order(packet, NULL, &data_length); 1629 copy = tcp_prepare_copy(socket, socket_data, packet, 1630 data_length, sequence_number); 1372 copy = tcp_prepare_copy(socket, socket_data, packet, data_length, sequence_number); 1631 1373 fibril_rwlock_write_unlock(socket_data->local_lock); 1632 // 1633 if (copy)1374 // printf("r send %d\n", packet_get_id(packet)); 1375 if(copy){ 1634 1376 tcp_send_packets(socket_data->device_id, copy); 1635 } else { 1377 } 1378 }else{ 1636 1379 fibril_rwlock_write_unlock(socket_data->local_lock); 1637 1380 } 1638 1381 } 1639 1382 1640 int 1641 tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog) 1642 { 1383 int tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog){ 1643 1384 socket_core_ref socket; 1644 1385 tcp_socket_data_ref socket_data; … … 1646 1387 assert(local_sockets); 1647 1388 1648 if (backlog < 0)1389 if(backlog < 0){ 1649 1390 return EINVAL; 1650 1391 } 1651 1392 // find the socket 1652 1393 socket = socket_cores_find(local_sockets, socket_id); 1653 if (!socket)1394 if(! socket){ 1654 1395 return ENOTSOCK; 1655 1396 } 1656 1397 // get the socket specific data 1657 1398 socket_data = (tcp_socket_data_ref) socket->specific_data; … … 1659 1400 // set the backlog 1660 1401 socket_data->backlog = backlog; 1661 1662 1402 return EOK; 1663 1403 } 1664 1404 1665 int 1666 tcp_connect_message(socket_cores_ref local_sockets, int socket_id, 1667 struct sockaddr *addr, socklen_t addrlen) 1668 { 1405 int tcp_connect_message(socket_cores_ref local_sockets, int socket_id, struct sockaddr * addr, socklen_t addrlen){ 1669 1406 ERROR_DECLARE; 1670 1407 … … 1677 1414 // find the socket 1678 1415 socket = socket_cores_find(local_sockets, socket_id); 1679 if (!socket)1416 if(! socket){ 1680 1417 return ENOTSOCK; 1681 1682 if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, 1683 addrlen))) { 1418 } 1419 if(ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, addrlen))){ 1684 1420 tcp_free_socket_data(socket); 1685 1421 // unbind if bound 1686 if (socket->port > 0) { 1687 socket_ports_exclude(&tcp_globals.sockets, 1688 socket->port); 1422 if(socket->port > 0){ 1423 socket_ports_exclude(&tcp_globals.sockets, socket->port); 1689 1424 socket->port = 0; 1690 1425 } … … 1693 1428 } 1694 1429 1695 int 1696 tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, 1697 struct sockaddr *addr, socklen_t addrlen) 1698 { 1430 int tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, struct sockaddr * addr, socklen_t addrlen){ 1699 1431 ERROR_DECLARE; 1700 1432 … … 1710 1442 assert(socket_data); 1711 1443 assert(socket->specific_data == socket_data); 1712 if ((socket_data->state != TCP_SOCKET_INITIAL) && 1713 ((socket_data->state != TCP_SOCKET_LISTEN) || 1714 (socket->port <= 0))) 1444 if((socket_data->state != TCP_SOCKET_INITIAL) 1445 && ((socket_data->state != TCP_SOCKET_LISTEN) || (socket->port <= 0))){ 1715 1446 return EINVAL; 1716 1447 } 1717 1448 // get the destination port 1718 ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, 1719 &socket_data->dest_port)); 1720 if (socket->port <= 0) { 1449 ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, &socket_data->dest_port)); 1450 if(socket->port <= 0){ 1721 1451 // try to find a free port 1722 ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets, 1723 socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, 1724 tcp_globals.last_used_port)); 1452 ERROR_PROPAGATE(socket_bind_free_port(&tcp_globals.sockets, socket, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, tcp_globals.last_used_port)); 1725 1453 // set the next port as the search starting port number 1726 1454 tcp_globals.last_used_port = socket->port; 1727 1455 } 1728 1729 ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP, 1730 addr, addrlen, &socket_data->device_id, 1731 &socket_data->pseudo_header, &socket_data->headerlen)); 1456 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)); 1732 1457 1733 1458 // create the notification packet 1734 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, 1735 socket_data, 1, 0)); 1459 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 1, 0)); 1736 1460 1737 1461 // unlock the globals and wait for an operation … … 1741 1465 socket_data->addrlen = addrlen; 1742 1466 // send the packet 1743 if (ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) || 1744 ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, 1745 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))) { 1746 1467 if(ERROR_OCCURRED(tcp_queue_packet(socket, socket_data, packet, 1)) 1468 || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0, TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false))){ 1747 1469 socket_data->addr = NULL; 1748 1470 socket_data->addrlen = 0; 1749 1471 fibril_rwlock_write_lock(&tcp_globals.lock); 1750 1751 } else { 1752 1472 }else{ 1753 1473 packet = tcp_get_packets_to_send(socket, socket_data); 1754 if (packet){1474 if(packet){ 1755 1475 fibril_mutex_lock(&socket_data->operation.mutex); 1756 1476 fibril_rwlock_write_unlock(socket_data->local_lock); … … 1758 1478 printf("connecting %d\n", packet_get_id(packet)); 1759 1479 tcp_send_packets(socket_data->device_id, packet); 1760 1761 1480 // wait for a reply 1762 fibril_condvar_wait(&socket_data->operation.condvar, 1763 &socket_data->operation.mutex); 1481 fibril_condvar_wait(&socket_data->operation.condvar, &socket_data->operation.mutex); 1764 1482 ERROR_CODE = socket_data->operation.result; 1765 if (ERROR_CODE != EOK){1483 if(ERROR_CODE != EOK){ 1766 1484 socket_data->addr = NULL; 1767 1485 socket_data->addrlen = 0; 1768 1486 } 1769 } else{1487 }else{ 1770 1488 socket_data->addr = NULL; 1771 1489 socket_data->addrlen = 0; … … 1780 1498 } 1781 1499 1782 int 1783 tcp_queue_prepare_packet(socket_core_ref socket, 1784 tcp_socket_data_ref socket_data, packet_t packet, size_t data_length) 1785 { 1500 int tcp_queue_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){ 1786 1501 ERROR_DECLARE; 1787 1502 … … 1794 1509 // get tcp header 1795 1510 header = (tcp_header_ref) packet_get_data(packet); 1796 if (!header)1511 if(! header){ 1797 1512 return NO_DATA; 1798 1513 } 1799 1514 header->destination_port = htons(socket_data->dest_port); 1800 1515 header->source_port = htons(socket->port); 1801 1516 header->sequence_number = htonl(socket_data->next_outgoing); 1802 1803 if (ERROR_OCCURRED(packet_set_addr(packet, NULL, 1804 (uint8_t *) socket_data->addr, socket_data->addrlen))) 1517 if(ERROR_OCCURRED(packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr, socket_data->addrlen))){ 1805 1518 return tcp_release_and_return(packet, EINVAL); 1806 1519 } 1807 1520 // remember the outgoing FIN 1808 if (header->finalize)1521 if(header->finalize){ 1809 1522 socket_data->fin_outgoing = socket_data->next_outgoing; 1810 1523 } 1811 1524 return EOK; 1812 1525 } 1813 1526 1814 int 1815 tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, 1816 packet_t packet, size_t data_length) 1817 { 1527 int tcp_queue_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, packet_t packet, size_t data_length){ 1818 1528 ERROR_DECLARE; 1819 1529 … … 1822 1532 assert(socket->specific_data == socket_data); 1823 1533 1824 ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, 1825 data_length)); 1826 1827 if (ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet, 1828 socket_data->next_outgoing, data_length))) 1534 ERROR_PROPAGATE(tcp_queue_prepare_packet(socket, socket_data, packet, data_length)); 1535 1536 if(ERROR_OCCURRED(pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing, data_length))){ 1829 1537 return tcp_release_and_return(packet, ERROR_CODE); 1830 1538 } 1831 1539 socket_data->next_outgoing += data_length; 1832 1540 return EOK; 1833 1541 } 1834 1542 1835 packet_t 1836 tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data) 1837 { 1543 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data){ 1838 1544 ERROR_DECLARE; 1839 1545 … … 1849 1555 1850 1556 packet = pq_find(socket_data->outgoing, socket_data->last_outgoing + 1); 1851 while (packet){1557 while(packet){ 1852 1558 pq_get_order(packet, NULL, &data_length); 1853 1854 1559 // send only if fits into the window 1855 1560 // respecting the possible overflow 1856 if (!IS_IN_INTERVAL_OVERFLOW( 1857 (uint32_t) socket_data->last_outgoing, 1858 (uint32_t) (socket_data->last_outgoing + data_length), 1859 (uint32_t) (socket_data->expected + socket_data->treshold))) 1561 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))){ 1562 copy = tcp_prepare_copy(socket, socket_data, packet, data_length, socket_data->last_outgoing + 1); 1563 if(! copy){ 1564 return sending; 1565 } 1566 if(! sending){ 1567 sending = copy; 1568 }else{ 1569 if(ERROR_OCCURRED(pq_insert_after(previous, copy))){ 1570 pq_release_remote(tcp_globals.net_phone, packet_get_id(copy)); 1571 return sending; 1572 } 1573 } 1574 previous = copy; 1575 packet = pq_next(packet); 1576 // overflow occurred ? 1577 if((! packet) && (socket_data->last_outgoing > socket_data->next_outgoing)){ 1578 printf("gpts overflow\n"); 1579 // continue from the beginning 1580 packet = socket_data->outgoing; 1581 } 1582 socket_data->last_outgoing += data_length; 1583 }else{ 1860 1584 break; 1861 1862 copy = tcp_prepare_copy(socket, socket_data, packet, 1863 data_length, socket_data->last_outgoing + 1); 1864 if (!copy) 1865 return sending; 1866 1867 if (!sending) { 1868 sending = copy; 1869 } else if (ERROR_OCCURRED(pq_insert_after(previous, copy))) { 1870 pq_release_remote(tcp_globals.net_phone, 1871 packet_get_id(copy)); 1872 return sending; 1873 } 1874 1875 previous = copy; 1876 packet = pq_next(packet); 1877 // overflow occurred ? 1878 if ((!packet) && 1879 (socket_data->last_outgoing > socket_data->next_outgoing)) { 1880 printf("gpts overflow\n"); 1881 // continue from the beginning 1882 packet = socket_data->outgoing; 1883 } 1884 socket_data->last_outgoing += data_length; 1885 } 1886 1585 } 1586 } 1887 1587 return sending; 1888 1588 } 1889 1589 1890 packet_t 1891 tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, 1892 packet_t packet, size_t data_length, size_t sequence_number) 1893 { 1590 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){ 1894 1591 ERROR_DECLARE; 1895 1592 … … 1902 1599 1903 1600 // adjust the pseudo header 1904 if (ERROR_OCCURRED(ip_client_set_pseudo_header_data_length( 1905 socket_data->pseudo_header, socket_data->headerlen, 1906 packet_get_data_length(packet)))) { 1601 if(ERROR_OCCURRED(ip_client_set_pseudo_header_data_length(socket_data->pseudo_header, socket_data->headerlen, packet_get_data_length(packet)))){ 1907 1602 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1908 1603 return NULL; … … 1911 1606 // get the header 1912 1607 header = (tcp_header_ref) packet_get_data(packet); 1913 if (!header){1608 if(! header){ 1914 1609 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1915 1610 return NULL; … … 1918 1613 1919 1614 // adjust the header 1920 if (socket_data->next_incoming) { 1921 header->acknowledgement_number = 1922 htonl(socket_data->next_incoming); 1615 if(socket_data->next_incoming){ 1616 header->acknowledgement_number = htonl(socket_data->next_incoming); 1923 1617 header->acknowledge = 1; 1924 1618 } … … 1927 1621 // checksum 1928 1622 header->checksum = 0; 1929 checksum = compute_checksum(0, socket_data->pseudo_header, 1930 socket_data->headerlen); 1931 checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), 1932 packet_get_data_length(packet)); 1623 checksum = compute_checksum(0, socket_data->pseudo_header, socket_data->headerlen); 1624 checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), packet_get_data_length(packet)); 1933 1625 header->checksum = htons(flip_checksum(compact_checksum(checksum))); 1934 1935 1626 // prepare the packet 1936 if (ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 1937 0, 0)) || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, 1938 socket_data, sequence_number, socket_data->state, 1939 socket_data->timeout, true))) { 1627 if(ERROR_OCCURRED(ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0)) 1628 // prepare the timeout 1629 || ERROR_OCCURRED(tcp_prepare_timeout(tcp_timeout, socket, socket_data, sequence_number, socket_data->state, socket_data->timeout, true))){ 1940 1630 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1941 1631 return NULL; 1942 1632 } 1943 1944 1633 return packet; 1945 1634 } 1946 1635 1947 packet_t 1948 tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref socket_data, 1949 packet_t packet, size_t data_length, size_t sequence_number) 1950 { 1636 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){ 1951 1637 packet_t copy; 1952 1638 … … 1957 1643 // make a copy of the packet 1958 1644 copy = packet_get_copy(tcp_globals.net_phone, packet); 1959 if (!copy)1645 if(! copy){ 1960 1646 return NULL; 1961 1962 return tcp_send_prepare_packet(socket, socket_data, copy, data_length, 1963 sequence_number); 1964 } 1965 1966 void tcp_send_packets(device_id_t device_id, packet_t packet) 1967 { 1647 } 1648 1649 return tcp_send_prepare_packet(socket, socket_data, copy, data_length, sequence_number); 1650 } 1651 1652 void tcp_send_packets(device_id_t device_id, packet_t packet){ 1968 1653 packet_t next; 1969 1654 1970 while (packet){1655 while(packet){ 1971 1656 next = pq_detach(packet); 1972 ip_send_msg(tcp_globals.ip_phone, device_id, packet, 1973 SERVICE_TCP, 0); 1657 ip_send_msg(tcp_globals.ip_phone, device_id, packet, SERVICE_TCP, 0); 1974 1658 packet = next; 1975 1659 } 1976 1660 } 1977 1661 1978 void 1979 tcp_prepare_operation_header(socket_core_ref socket, 1980 tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, 1981 int finalize) 1982 { 1662 void tcp_prepare_operation_header(socket_core_ref socket, tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, int finalize){ 1983 1663 assert(socket); 1984 1664 assert(socket_data); … … 1994 1674 } 1995 1675 1996 int 1997 tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t), 1998 socket_core_ref socket, tcp_socket_data_ref socket_data, 1999 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, 2000 int globals_read_only) 2001 { 1676 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){ 2002 1677 tcp_timeout_ref operation_timeout; 2003 1678 fid_t fibril; … … 2008 1683 2009 1684 // prepare the timeout with key bundle structure 2010 operation_timeout = malloc(sizeof(*operation_timeout) + 2011 socket->key_length + 1); 2012 if (!operation_timeout) 1685 operation_timeout = malloc(sizeof(*operation_timeout) + socket->key_length + 1); 1686 if(! operation_timeout){ 2013 1687 return ENOMEM; 2014 1688 } 2015 1689 bzero(operation_timeout, sizeof(*operation_timeout)); 2016 1690 operation_timeout->globals_read_only = globals_read_only; … … 2023 1697 2024 1698 // copy the key 2025 operation_timeout->key = ((char *) operation_timeout) + 2026 sizeof(*operation_timeout); 1699 operation_timeout->key = ((char *) operation_timeout) + sizeof(*operation_timeout); 2027 1700 operation_timeout->key_length = socket->key_length; 2028 1701 memcpy(operation_timeout->key, socket->key, socket->key_length); … … 2031 1704 // prepare the timeouting thread 2032 1705 fibril = fibril_create(timeout_function, operation_timeout); 2033 if (!fibril){1706 if(! fibril){ 2034 1707 free(operation_timeout); 2035 1708 return EPARTY; 2036 1709 } 2037 // 1710 // fibril_mutex_lock(&socket_data->operation.mutex); 2038 1711 // start the timeouting fibril 2039 1712 fibril_add_ready(fibril); … … 2042 1715 } 2043 1716 2044 int 2045 tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, 2046 size_t * addrlen) 2047 { 1717 int tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, size_t * addrlen){ 2048 1718 ERROR_DECLARE; 2049 1719 … … 2058 1728 // find the socket 2059 1729 socket = socket_cores_find(local_sockets, socket_id); 2060 if (!socket)1730 if(! socket){ 2061 1731 return ENOTSOCK; 2062 1732 } 2063 1733 // get the socket specific data 2064 if (!socket->specific_data)1734 if(! socket->specific_data){ 2065 1735 return NO_DATA; 2066 1736 } 2067 1737 socket_data = (tcp_socket_data_ref) socket->specific_data; 2068 1738 2069 1739 // check state 2070 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) && 2071 (socket_data->state != TCP_SOCKET_CLOSE_WAIT)) 1740 if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){ 2072 1741 return ENOTCONN; 1742 } 2073 1743 2074 1744 // send the source address if desired 2075 if (addrlen) { 2076 ERROR_PROPAGATE(data_reply(socket_data->addr, 2077 socket_data->addrlen)); 1745 if(addrlen){ 1746 ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen)); 2078 1747 *addrlen = socket_data->addrlen; 2079 1748 } … … 2081 1750 // get the next received packet 2082 1751 packet_id = dyn_fifo_value(&socket->received); 2083 if (packet_id < 0)1752 if(packet_id < 0){ 2084 1753 return NO_DATA; 2085 2086 ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet, 2087 packet_id)); 1754 } 1755 ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet, packet_id)); 2088 1756 2089 1757 // reply the packets … … 2097 1765 } 2098 1766 2099 int 2100 tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, 2101 size_t * data_fragment_size, int flags) 2102 { 1767 int tcp_send_message(socket_cores_ref local_sockets, int socket_id, int fragments, size_t * data_fragment_size, int flags){ 2103 1768 ERROR_DECLARE; 2104 1769 … … 2117 1782 // find the socket 2118 1783 socket = socket_cores_find(local_sockets, socket_id); 2119 if (!socket)1784 if(! socket){ 2120 1785 return ENOTSOCK; 2121 1786 } 2122 1787 // get the socket specific data 2123 if (!socket->specific_data)1788 if(! socket->specific_data){ 2124 1789 return NO_DATA; 2125 1790 } 2126 1791 socket_data = (tcp_socket_data_ref) socket->specific_data; 2127 1792 2128 1793 // check state 2129 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) && 2130 (socket_data->state != TCP_SOCKET_CLOSE_WAIT)) 1794 if((socket_data->state != TCP_SOCKET_ESTABLISHED) && (socket_data->state != TCP_SOCKET_CLOSE_WAIT)){ 2131 1795 return ENOTCONN; 2132 2133 ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, 2134 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension)); 2135 2136 *data_fragment_size = 2137 ((packet_dimension->content < socket_data->data_fragment_size) ? 2138 packet_dimension->content : socket_data->data_fragment_size); 2139 2140 for (index = 0; index < fragments; ++index) { 1796 } 1797 1798 ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension)); 1799 1800 *data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size); 1801 1802 for(index = 0; index < fragments; ++ index){ 2141 1803 // read the data fragment 2142 result = tl_socket_read_packet_data(tcp_globals.net_phone, 2143 &packet, TCP_HEADER_SIZE, packet_dimension, 2144 socket_data->addr, socket_data->addrlen); 2145 if (result < 0) 1804 result = tl_socket_read_packet_data(tcp_globals.net_phone, &packet, TCP_HEADER_SIZE, packet_dimension, socket_data->addr, socket_data->addrlen); 1805 if(result < 0){ 2146 1806 return result; 2147 1807 } 2148 1808 total_length = (size_t) result; 2149 1809 // prefix the tcp header 2150 1810 header = PACKET_PREFIX(packet, tcp_header_t); 2151 if (!header)1811 if(! header){ 2152 1812 return tcp_release_and_return(packet, ENOMEM); 2153 1813 } 2154 1814 tcp_prepare_operation_header(socket, socket_data, header, 0, 0); 2155 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 2156 0)); 1815 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 0)); 2157 1816 } 2158 1817 … … 2161 1820 fibril_rwlock_write_unlock(socket_data->local_lock); 2162 1821 fibril_rwlock_read_unlock(&tcp_globals.lock); 2163 2164 if (packet) { 1822 if(packet){ 2165 1823 // send the packet 2166 1824 tcp_send_packets(socket_data->device_id, packet); … … 2170 1828 } 2171 1829 2172 int 2173 tcp_close_message(socket_cores_ref local_sockets, int socket_id) 2174 { 1830 int tcp_close_message(socket_cores_ref local_sockets, int socket_id){ 2175 1831 ERROR_DECLARE; 2176 1832 … … 2181 1837 // find the socket 2182 1838 socket = socket_cores_find(local_sockets, socket_id); 2183 if (!socket)1839 if(! socket){ 2184 1840 return ENOTSOCK; 2185 1841 } 2186 1842 // get the socket specific data 2187 1843 socket_data = (tcp_socket_data_ref) socket->specific_data; … … 2189 1845 2190 1846 // check state 2191 switch (socket_data->state) { 2192 case TCP_SOCKET_ESTABLISHED: 2193 socket_data->state = TCP_SOCKET_FIN_WAIT_1; 2194 break; 2195 2196 case TCP_SOCKET_CLOSE_WAIT: 2197 socket_data->state = TCP_SOCKET_LAST_ACK; 2198 break; 2199 2200 // case TCP_SOCKET_LISTEN: 2201 2202 default: 2203 // just destroy 2204 if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id, 2205 local_sockets, &tcp_globals.sockets, 2206 tcp_free_socket_data))) { 2207 fibril_rwlock_write_unlock(socket_data->local_lock); 2208 fibril_rwlock_write_unlock(&tcp_globals.lock); 2209 } 2210 return ERROR_CODE; 2211 } 2212 1847 switch(socket_data->state){ 1848 case TCP_SOCKET_ESTABLISHED: 1849 socket_data->state = TCP_SOCKET_FIN_WAIT_1; 1850 break; 1851 case TCP_SOCKET_CLOSE_WAIT: 1852 socket_data->state = TCP_SOCKET_LAST_ACK; 1853 break; 1854 // case TCP_SOCKET_LISTEN: 1855 default: 1856 // just destroy 1857 if(! ERROR_OCCURRED(socket_destroy(tcp_globals.net_phone, socket_id, local_sockets, &tcp_globals.sockets, tcp_free_socket_data))){ 1858 fibril_rwlock_write_unlock(socket_data->local_lock); 1859 fibril_rwlock_write_unlock(&tcp_globals.lock); 1860 } 1861 return ERROR_CODE; 1862 } 2213 1863 // send FIN 2214 1864 // TODO should I wait to complete? 2215 1865 2216 1866 // create the notification packet 2217 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, 2218 socket_data, 0, 1)); 1867 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, socket_data, 0, 1)); 2219 1868 2220 1869 // send the packet … … 2225 1874 fibril_rwlock_write_unlock(socket_data->local_lock); 2226 1875 fibril_rwlock_write_unlock(&tcp_globals.lock); 2227 2228 if (packet) { 1876 if(packet){ 2229 1877 // send the packet 2230 1878 tcp_send_packets(socket_data->device_id, packet); 2231 1879 } 2232 2233 1880 return EOK; 2234 1881 } 2235 1882 2236 int 2237 tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, 2238 tcp_socket_data_ref socket_data, int synchronize, int finalize) 2239 { 1883 int tcp_create_notification_packet(packet_t * packet, socket_core_ref socket, tcp_socket_data_ref socket_data, int synchronize, int finalize){ 2240 1884 ERROR_DECLARE; 2241 1885 … … 2246 1890 2247 1891 // get the device packet dimension 2248 ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, 2249 &tcp_globals.dimensions, socket_data->device_id, 2250 &packet_dimension)); 2251 1892 ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension)); 2252 1893 // get a new packet 2253 *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE, 2254 packet_dimension->addr_len, packet_dimension->prefix, 2255 packet_dimension->suffix); 2256 2257 if (!*packet) 1894 *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE, packet_dimension->addr_len, packet_dimension->prefix, packet_dimension->suffix); 1895 if(! * packet){ 2258 1896 return ENOMEM; 2259 1897 } 2260 1898 // allocate space in the packet 2261 1899 header = PACKET_SUFFIX(*packet, tcp_header_t); 2262 if (!header)1900 if(! header){ 2263 1901 tcp_release_and_return(*packet, ENOMEM); 2264 2265 tcp_prepare_operation_header(socket, socket_data, header, synchronize, 2266 finalize); 2267 1902 } 1903 1904 tcp_prepare_operation_header(socket, socket_data, header, synchronize, finalize); 2268 1905 return EOK; 2269 1906 } 2270 1907 2271 int 2272 tcp_accept_message(socket_cores_ref local_sockets, int socket_id, 2273 int new_socket_id, size_t * data_fragment_size, size_t * addrlen) 2274 { 1908 int tcp_accept_message(socket_cores_ref local_sockets, int socket_id, int new_socket_id, size_t * data_fragment_size, size_t * addrlen){ 2275 1909 ERROR_DECLARE; 2276 1910 … … 2286 1920 // find the socket 2287 1921 socket = socket_cores_find(local_sockets, socket_id); 2288 if (!socket)1922 if(! socket){ 2289 1923 return ENOTSOCK; 2290 1924 } 2291 1925 // get the socket specific data 2292 1926 socket_data = (tcp_socket_data_ref) socket->specific_data; … … 2294 1928 2295 1929 // check state 2296 if (socket_data->state != TCP_SOCKET_LISTEN)1930 if(socket_data->state != TCP_SOCKET_LISTEN){ 2297 1931 return EINVAL; 2298 2299 do { 1932 } 1933 1934 do{ 2300 1935 socket_id = dyn_fifo_value(&socket->accepted); 2301 if (socket_id < 0)1936 if(socket_id < 0){ 2302 1937 return ENOTSOCK; 1938 } 2303 1939 socket_id *= -1; 2304 1940 2305 1941 accepted = socket_cores_find(local_sockets, socket_id); 2306 if (!accepted)1942 if(! accepted){ 2307 1943 return ENOTSOCK; 2308 1944 } 2309 1945 // get the socket specific data 2310 1946 socket_data = (tcp_socket_data_ref) accepted->specific_data; 2311 1947 assert(socket_data); 2312 1948 // TODO can it be in another state? 2313 if (socket_data->state == TCP_SOCKET_ESTABLISHED) { 2314 ERROR_PROPAGATE(data_reply(socket_data->addr, 2315 socket_data->addrlen)); 2316 ERROR_PROPAGATE(tl_get_ip_packet_dimension( 2317 tcp_globals.ip_phone, &tcp_globals.dimensions, 2318 socket_data->device_id, &packet_dimension)); 1949 if(socket_data->state == TCP_SOCKET_ESTABLISHED){ 1950 ERROR_PROPAGATE(data_reply(socket_data->addr, socket_data->addrlen)); 1951 ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, &tcp_globals.dimensions, socket_data->device_id, &packet_dimension)); 2319 1952 *addrlen = socket_data->addrlen; 2320 2321 *data_fragment_size = 2322 ((packet_dimension->content < 2323 socket_data->data_fragment_size) ? 2324 packet_dimension->content : 2325 socket_data->data_fragment_size); 2326 2327 if (new_socket_id > 0) { 2328 ERROR_PROPAGATE(socket_cores_update( 2329 local_sockets, accepted->socket_id, 2330 new_socket_id)); 1953 *data_fragment_size = ((packet_dimension->content < socket_data->data_fragment_size) ? packet_dimension->content : socket_data->data_fragment_size); 1954 if(new_socket_id > 0){ 1955 ERROR_PROPAGATE(socket_cores_update(local_sockets, accepted->socket_id, new_socket_id)); 2331 1956 accepted->socket_id = new_socket_id; 2332 1957 } 2333 1958 } 2334 1959 dyn_fifo_pop(&socket->accepted); 2335 } while (socket_data->state != TCP_SOCKET_ESTABLISHED); 2336 1960 }while(socket_data->state != TCP_SOCKET_ESTABLISHED); 2337 1961 printf("ret accept %d\n", accepted->socket_id); 2338 1962 return accepted->socket_id; 2339 1963 } 2340 1964 2341 void 2342 tcp_free_socket_data(socket_core_ref socket) 2343 { 1965 void tcp_free_socket_data(socket_core_ref socket){ 2344 1966 tcp_socket_data_ref socket_data; 2345 1967 … … 2352 1974 assert(socket_data); 2353 1975 //free the pseudo header 2354 if (socket_data->pseudo_header){2355 if (socket_data->headerlen){1976 if(socket_data->pseudo_header){ 1977 if(socket_data->headerlen){ 2356 1978 printf("d pseudo\n"); 2357 1979 free(socket_data->pseudo_header); … … 2362 1984 socket_data->headerlen = 0; 2363 1985 // free the address 2364 if (socket_data->addr){2365 if (socket_data->addrlen){1986 if(socket_data->addr){ 1987 if(socket_data->addrlen){ 2366 1988 printf("d addr\n"); 2367 1989 free(socket_data->addr); … … 2373 1995 } 2374 1996 2375 int tcp_release_and_return(packet_t packet, int result) 2376 { 1997 int tcp_release_and_return(packet_t packet, int result){ 2377 1998 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 2378 1999 return result; … … 2392 2013 */ 2393 2014 ipc_answer_0(iid, EOK); 2394 2395 while 2015 2016 while(true) { 2396 2017 ipc_call_t answer; 2397 2018 int answer_count; 2398 2399 /* 2400 Clear the answer structure 2401 */ 2019 2020 /* Clear the answer structure */ 2402 2021 refresh_answer(&answer, &answer_count); 2403 2404 /* 2405 Fetch the next message 2406 */ 2022 2023 /* Fetch the next message */ 2407 2024 ipc_call_t call; 2408 2025 ipc_callid_t callid = async_get_call(&call); 2409 2410 /* 2411 Process the message 2412 */ 2026 2027 /* Process the message */ 2413 2028 int res = tl_module_message_standalone(callid, &call, &answer, 2414 2029 &answer_count); 2415 2416 /* 2417 End if said to either by the message or the processing result 2418 */ 2419 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || 2420 (res == EHANGUP)) 2030 2031 /* End if said to either by the message or the processing result */ 2032 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP)) 2421 2033 return; 2422 2423 /* 2424 Answer the message 2425 */ 2034 2035 /* Answer the message */ 2426 2036 answer_call(callid, res, &answer, answer_count); 2427 2037 } … … 2437 2047 * 2438 2048 */ 2439 int 2440 main(int argc, char *argv[]) 2049 int main(int argc, char *argv[]) 2441 2050 { 2442 2051 ERROR_DECLARE; 2443 2444 /* 2445 Start the module 2446 */ 2052 2053 /* Start the module */ 2447 2054 if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection))) 2448 2055 return ERROR_CODE; 2449 2056 2450 2057 return EOK; 2451 2058 }
Note:
See TracChangeset
for help on using the changeset viewer.