Changes in uspace/srv/net/tl/tcp/tcp.c [28a3e74:472020fc] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tcp/tcp.c
r28a3e74 r472020fc 28 28 29 29 /** @addtogroup tcp 30 * @{30 * @{ 31 31 */ 32 32 33 33 /** @file 34 * TCP module implementation.35 * @see tcp.h34 * TCP module implementation. 35 * @see tcp.h 36 36 */ 37 37 … … 40 40 #include <fibril_synch.h> 41 41 #include <malloc.h> 42 / * TODO remove stdio */42 //TODO remove stdio 43 43 #include <stdio.h> 44 44 #include <errno.h> 45 45 #include <err.h> 46 47 #include <ipc/ipc.h> 46 48 #include <ipc/services.h> 47 49 #include <ipc/net.h> … … 63 65 #include <ip_interface.h> 64 66 #include <icmp_client.h> 65 #include <icmp_ remote.h>67 #include <icmp_interface.h> 66 68 #include <net_interface.h> 67 69 #include <socket_core.h> 68 70 #include <tl_common.h> 69 #include <tl_ remote.h>70 #include <tl_ skel.h>71 #include <tl_local.h> 72 #include <tl_interface.h> 71 73 72 74 #include "tcp.h" 73 75 #include "tcp_header.h" 76 #include "tcp_module.h" 74 77 75 78 /** TCP module name. */ 76 #define NAME "tcp"79 #define NAME "TCP protocol" 77 80 78 81 /** The TCP window default value. */ … … 107 110 108 111 /** Returns a value indicating whether the value is in the interval respecting 109 * the possible overflow.112 * the possible overflow. 110 113 * 111 * The high end and/or the value may overflow, be lower than the low value. 112 * 113 * @param[in] lower The last value before the interval. 114 * @param[in] value The value to be checked. 115 * @param[in] higher_equal The last value in the interval. 114 * The high end and/or the value may overflow, be lower than the low value. 115 * @param[in] lower The last value before the interval. 116 * @param[in] value The value to be checked. 117 * @param[in] higher_equal The last value in the interval. 116 118 */ 117 119 #define IS_IN_INTERVAL_OVERFLOW(lower, value, higher_equal) \ … … 124 126 */ 125 127 typedef struct tcp_timeout tcp_timeout_t; 128 129 /** Type definition of the TCP timeout pointer. 130 * @see tcp_timeout 131 */ 132 typedef tcp_timeout_t *tcp_timeout_ref; 126 133 127 134 /** TCP reply timeout data. … … 137 144 138 145 /** Local sockets. */ 139 socket_cores_ t *local_sockets;146 socket_cores_ref local_sockets; 140 147 141 148 /** Socket identifier. */ … … 152 159 153 160 /** Port map key. */ 154 uint8_t*key;161 char *key; 155 162 156 163 /** Port map key length. */ … … 158 165 }; 159 166 160 static int tcp_release_and_return(packet_t *, int); 161 static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *, 162 tcp_header_t *, int synchronize, int); 163 static int tcp_prepare_timeout(int (*)(void *), socket_core_t *, 164 tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int); 165 static void tcp_free_socket_data(socket_core_t *); 166 167 static int tcp_timeout(void *); 168 169 static int tcp_release_after_timeout(void *); 170 171 static int tcp_process_packet(device_id_t, packet_t *, services_t); 172 static int tcp_connect_core(socket_core_t *, socket_cores_t *, 173 struct sockaddr *, socklen_t); 174 static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *, 175 packet_t *, size_t); 176 static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *, 177 size_t); 178 static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *); 179 static void tcp_send_packets(device_id_t, packet_t *); 180 181 static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *, 182 tcp_header_t *); 183 static packet_t *tcp_send_prepare_packet(socket_core_t *, tcp_socket_data_t *, 184 packet_t *, size_t, size_t); 185 static packet_t *tcp_prepare_copy(socket_core_t *, tcp_socket_data_t *, 186 packet_t *, size_t, size_t); 187 /* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *, 188 size_t); 189 static int tcp_create_notification_packet(packet_t **, socket_core_t *, 190 tcp_socket_data_t *, int, int); 191 static void tcp_refresh_socket_data(tcp_socket_data_t *); 192 193 static void tcp_initialize_socket_data(tcp_socket_data_t *); 194 195 static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *, 196 tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t); 197 static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *, 198 tcp_header_t *, packet_t *); 199 static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *, 200 tcp_header_t *, packet_t *); 201 static int tcp_process_established(socket_core_t *, tcp_socket_data_t *, 202 tcp_header_t *, packet_t *, int, size_t); 203 static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *, 204 packet_t *, int, size_t); 205 static void tcp_queue_received_end_of_data(socket_core_t *socket); 206 207 static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t); 208 static int tcp_process_client_messages(ipc_callid_t, ipc_call_t); 209 210 static int tcp_listen_message(socket_cores_t *, int, int); 211 static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *, 212 socklen_t); 213 static int tcp_recvfrom_message(socket_cores_t *, int, int, size_t *); 214 static int tcp_send_message(socket_cores_t *, int, int, size_t *, int); 215 static int tcp_accept_message(socket_cores_t *, int, int, size_t *, size_t *); 216 static int tcp_close_message(socket_cores_t *, int); 167 /** Releases the packet and returns the result. 168 * @param[in] packet The packet queue to be released. 169 * @param[in] result The result to be returned. 170 * @return The result parameter. 171 */ 172 int tcp_release_and_return(packet_t packet, int result); 173 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_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); 197 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); 211 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data); 212 213 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); 232 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); 244 int tcp_close_message(socket_cores_ref local_sockets, int socket_id); 217 245 218 246 /** TCP global data. */ 219 247 tcp_globals_t tcp_globals; 220 248 221 int tcp_received_msg(device_id_t device_id, packet_t *packet, 222 services_t receiver, services_t error) 223 { 224 int rc; 249 int tcp_initialize(async_client_conn_t client_connection) 250 { 251 ERROR_DECLARE; 252 253 assert(client_connection); 254 255 fibril_rwlock_initialize(&tcp_globals.lock); 256 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); 262 if (tcp_globals.ip_phone < 0) 263 return tcp_globals.ip_phone; 264 265 ERROR_PROPAGATE(socket_ports_initialize(&tcp_globals.sockets)); 266 if (ERROR_OCCURRED(packet_dimensions_initialize( 267 &tcp_globals.dimensions))) { 268 socket_ports_destroy(&tcp_globals.sockets); 269 return ERROR_CODE; 270 } 271 272 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; 273 fibril_rwlock_write_unlock(&tcp_globals.lock); 274 275 return EOK; 276 } 277 278 int 279 tcp_received_msg(device_id_t device_id, packet_t packet, services_t receiver, 280 services_t error) 281 { 282 ERROR_DECLARE; 225 283 226 284 if (receiver != SERVICE_TCP) … … 228 286 229 287 fibril_rwlock_write_lock(&tcp_globals.lock); 230 rc = tcp_process_packet(device_id, packet, error); 231 if (rc != EOK) 288 if (ERROR_OCCURRED(tcp_process_packet(device_id, packet, error))) 232 289 fibril_rwlock_write_unlock(&tcp_globals.lock); 233 290 234 printf("receive %d \n", rc); 235 236 return rc; 237 } 238 239 int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error) 240 { 291 printf("receive %d \n", ERROR_CODE); 292 293 return ERROR_CODE; 294 } 295 296 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error) 297 { 298 ERROR_DECLARE; 299 241 300 size_t length; 242 301 size_t offset; 243 302 int result; 244 tcp_header_ t *header;245 socket_core_ t *socket;246 tcp_socket_data_ t *socket_data;247 packet_t *next_packet;303 tcp_header_ref header; 304 socket_core_ref socket; 305 tcp_socket_data_ref socket_data; 306 packet_t next_packet; 248 307 size_t total_length; 249 308 uint32_t checksum; … … 254 313 struct sockaddr *dest; 255 314 size_t addrlen; 256 int rc; 257 258 switch (error) { 259 case SERVICE_NONE: 260 break; 261 case SERVICE_ICMP: 262 /* Process error */ 263 result = icmp_client_process_packet(packet, &type, &code, NULL, 264 NULL); 265 if (result < 0) 266 return tcp_release_and_return(packet, result); 267 268 length = (size_t) result; 269 rc = packet_trim(packet, length, 0); 270 if (rc != EOK) 271 return tcp_release_and_return(packet, rc); 272 break; 273 default: 274 return tcp_release_and_return(packet, ENOTSUP); 275 } 276 277 /* TODO process received ipopts? */ 315 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); 334 } 335 } 336 337 // TODO process received ipopts? 278 338 result = ip_client_process_packet(packet, NULL, NULL, NULL, NULL, NULL); 279 339 if (result < 0) … … 289 349 return tcp_release_and_return(packet, NO_DATA); 290 350 291 /* Trim all but TCP header */ 292 rc = packet_trim(packet, offset, 0); 293 if (rc != EOK) 294 return tcp_release_and_return(packet, rc); 295 296 /* Get tcp header */ 297 header = (tcp_header_t *) packet_get_data(packet); 351 // trim all but TCP header 352 if (ERROR_OCCURRED(packet_trim(packet, offset, 0))) 353 return tcp_release_and_return(packet, ERROR_CODE); 354 355 // get tcp header 356 header = (tcp_header_ref) packet_get_data(packet); 298 357 if (!header) 299 358 return tcp_release_and_return(packet, NO_DATA); 300 359 301 #if 0 302 printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), 303 ntohs(header->destination_port)); 304 #endif 360 // printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), 361 // ntohs(header->destination_port)); 362 305 363 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest); 306 364 if (result <= 0) … … 309 367 addrlen = (size_t) result; 310 368 311 rc = tl_set_address_port(src, addrlen, ntohs(header->source_port));312 if (rc != EOK)313 return tcp_release_and_return(packet, rc);369 if (ERROR_OCCURRED(tl_set_address_port(src, addrlen, 370 ntohs(header->source_port)))) 371 return tcp_release_and_return(packet, ERROR_CODE); 314 372 315 / * Find the destination socket */373 // find the destination socket 316 374 socket = socket_port_find(&tcp_globals.sockets, 317 ntohs(header->destination_port), ( uint8_t*) src, addrlen);375 ntohs(header->destination_port), (const char *) src, addrlen); 318 376 if (!socket) { 319 / * Find the listening destination socket */377 // find the listening destination socket 320 378 socket = socket_port_find(&tcp_globals.sockets, 321 ntohs(header->destination_port), 322 (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 323 } 324 325 if (!socket) { 326 if (tl_prepare_icmp_packet(tcp_globals.net_phone, 327 tcp_globals.icmp_phone, packet, error) == EOK) { 328 icmp_destination_unreachable_msg(tcp_globals.icmp_phone, 329 ICMP_PORT_UNREACH, 0, packet); 330 } 331 return EADDRNOTAVAIL; 332 } 333 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); 387 } 388 return EADDRNOTAVAIL; 389 } 390 } 334 391 printf("socket id %d\n", socket->socket_id); 335 socket_data = (tcp_socket_data_ t *) socket->specific_data;392 socket_data = (tcp_socket_data_ref) socket->specific_data; 336 393 assert(socket_data); 337 394 338 / * Some data received, clear the timeout counter */395 // some data received, clear the timeout counter 339 396 socket_data->timeout_count = 0; 340 397 341 / * Count the received packet fragments */398 // count the received packet fragments 342 399 next_packet = packet; 343 400 fragments = 0; … … 345 402 total_length = 0; 346 403 do { 347 fragments++;404 ++fragments; 348 405 length = packet_get_data_length(next_packet); 349 406 if (length <= 0) … … 352 409 total_length += length; 353 410 354 / * Add partial checksum if set */411 // add partial checksum if set 355 412 if (!error) { 356 413 checksum = compute_checksum(checksum, … … 364 421 365 422 if (error) 366 goto has_error_service;423 goto error; 367 424 368 425 if (socket_data->state == TCP_SOCKET_LISTEN) { 426 369 427 if (socket_data->pseudo_header) { 370 428 free(socket_data->pseudo_header); … … 373 431 } 374 432 375 rc = ip_client_get_pseudo_header(IPPROTO_TCP, src, addrlen, 376 dest, addrlen, total_length, &socket_data->pseudo_header, 377 &socket_data->headerlen); 378 if (rc != EOK) { 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))) { 379 436 fibril_rwlock_write_unlock(socket_data->local_lock); 380 return tcp_release_and_return(packet, rc); 381 } 382 } else { 383 rc = ip_client_set_pseudo_header_data_length( 384 socket_data->pseudo_header, socket_data->headerlen, 385 total_length); 386 if (rc != EOK) { 387 fibril_rwlock_write_unlock(socket_data->local_lock); 388 return tcp_release_and_return(packet, rc); 389 } 437 return tcp_release_and_return(packet, ERROR_CODE); 438 } 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); 390 445 } 391 446 … … 397 452 fibril_rwlock_write_unlock(socket_data->local_lock); 398 453 399 rc = tl_prepare_icmp_packet(tcp_globals.net_phone, 400 tcp_globals.icmp_phone, packet, error); 401 if (rc == EOK) { 402 /* Checksum error ICMP */ 454 if (ERROR_NONE(tl_prepare_icmp_packet(tcp_globals.net_phone, 455 tcp_globals.icmp_phone, packet, error))) { 456 // checksum error ICMP 403 457 icmp_parameter_problem_msg(tcp_globals.icmp_phone, 404 458 ICMP_PARAM_POINTER, … … 410 464 } 411 465 412 has_error_service:413 fibril_rwlock_ write_unlock(&tcp_globals.lock);414 415 / * TODO error reporting/handling */466 error: 467 fibril_rwlock_read_unlock(&tcp_globals.lock); 468 469 // TODO error reporting/handling 416 470 switch (socket_data->state) { 417 471 case TCP_SOCKET_LISTEN: 418 rc = tcp_process_listen(socket, socket_data, header, packet,419 src, dest, addrlen);472 ERROR_CODE = tcp_process_listen(socket, socket_data, header, 473 packet, src, dest, addrlen); 420 474 break; 421 475 case TCP_SOCKET_SYN_RECEIVED: 422 rc = tcp_process_syn_received(socket, socket_data, header, 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, 423 481 packet); 424 482 break; 425 case TCP_SOCKET_SYN_SENT:426 rc = tcp_process_syn_sent(socket, socket_data, header, packet);427 break;428 483 case TCP_SOCKET_FIN_WAIT_1: 429 / * ack changing the state to FIN_WAIT_2 gets processed later */484 // ack changing the state to FIN_WAIT_2 gets processed later 430 485 case TCP_SOCKET_FIN_WAIT_2: 431 / * fin changing state to LAST_ACK gets processed later */486 // fin changing state to LAST_ACK gets processed later 432 487 case TCP_SOCKET_LAST_ACK: 433 / * ack releasing the socket get processed later */488 // ack releasing the socket get processed later 434 489 case TCP_SOCKET_CLOSING: 435 / * ack releasing the socket gets processed later */490 // ack releasing the socket gets processed later 436 491 case TCP_SOCKET_ESTABLISHED: 437 rc = tcp_process_established(socket, socket_data, header,438 packet, fragments, total_length);492 ERROR_CODE = tcp_process_established(socket, socket_data, 493 header, packet, fragments, total_length); 439 494 break; 440 495 default: … … 442 497 } 443 498 444 if (rc != EOK) { 499 if (ERROR_CODE != EOK) { 500 printf("process %d\n", ERROR_CODE); 445 501 fibril_rwlock_write_unlock(socket_data->local_lock); 446 printf("process %d\n", rc);447 502 } 448 503 … … 450 505 } 451 506 452 int tcp_process_established(socket_core_t *socket, tcp_socket_data_t * 453 socket_data, tcp_header_t *header, packet_t *packet, int fragments, 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, 454 510 size_t total_length) 455 511 { 456 packet_t *next_packet; 457 packet_t *tmp_packet; 512 ERROR_DECLARE; 513 514 packet_t next_packet; 515 packet_t tmp_packet; 458 516 uint32_t old_incoming; 459 517 size_t order; … … 462 520 size_t offset; 463 521 uint32_t new_sequence_number; 464 bool forced_ack;465 int rc;466 522 467 523 assert(socket); … … 471 527 assert(packet); 472 528 473 forced_ack = false;474 475 529 new_sequence_number = ntohl(header->sequence_number); 476 530 old_incoming = socket_data->next_incoming; 477 531 478 if (header->finalize) { 479 socket_data->fin_incoming = new_sequence_number + 480 total_length - TCP_HEADER_LENGTH(header); 481 } 482 483 /* Trim begining if containing expected data */ 532 if (header->finalize) 533 socket_data->fin_incoming = new_sequence_number; 534 535 // trim begining if containing expected data 484 536 if (IS_IN_INTERVAL_OVERFLOW(new_sequence_number, 485 537 socket_data->next_incoming, new_sequence_number + total_length)) { 486 538 487 / * Get the acknowledged offset */539 // get the acknowledged offset 488 540 if (socket_data->next_incoming < new_sequence_number) { 489 541 offset = new_sequence_number - … … 497 549 total_length -= offset; 498 550 length = packet_get_data_length(packet); 499 500 /* Trim the acknowledged data */ 551 // trim the acknowledged data 501 552 while (length <= offset) { 502 / * Release the acknowledged packets */553 // release the acknowledged packets 503 554 next_packet = pq_next(packet); 504 555 pq_release_remote(tcp_globals.net_phone, … … 509 560 } 510 561 511 if (offset > 0) { 512 rc = packet_trim(packet, offset, 0); 513 if (rc != EOK) 514 return tcp_release_and_return(packet, rc); 515 } 562 if ((offset > 0) && (ERROR_OCCURRED(packet_trim(packet, 563 offset, 0)))) 564 return tcp_release_and_return(packet, ERROR_CODE); 516 565 517 566 assert(new_sequence_number == socket_data->next_incoming); 518 567 } 519 568 520 / * Release if overflowing the window */569 // release if overflowing the window 521 570 /* 522 571 if (IS_IN_INTERVAL_OVERFLOW(socket_data->next_incoming + … … 545 594 if (length <= offset) 546 595 next_packet = pq_next(next_packet); 547 else { 548 rc = packet_trim(next_packet, 0, 549 length - offset)); 550 if (rc != EOK) 551 return tcp_release_and_return(packet, 552 rc); 553 } 596 else if (ERROR_OCCURRED(packet_trim(next_packet, 0, 597 length - offset))) 598 return tcp_release_and_return(packet, 599 ERROR_CODE); 554 600 offset -= length; 555 601 total_length -= length - offset; … … 568 614 } 569 615 */ 570 / * The expected one arrived? */616 // the expected one arrived? 571 617 if (new_sequence_number == socket_data->next_incoming) { 572 618 printf("expected\n"); 573 / * Process acknowledgement */619 // process acknowledgement 574 620 tcp_process_acknowledgement(socket, socket_data, header); 575 621 576 / * Remove the header */622 // remove the header 577 623 total_length -= TCP_HEADER_LENGTH(header); 578 rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);579 if (rc != EOK)580 return tcp_release_and_return(packet, rc);624 if (ERROR_OCCURRED(packet_trim(packet, 625 TCP_HEADER_LENGTH(header), 0))) 626 return tcp_release_and_return(packet, ERROR_CODE); 581 627 582 628 if (total_length) { 583 rc = tcp_queue_received_packet(socket, socket_data, 584 packet, fragments, total_length); 585 if (rc != EOK) 586 return rc; 629 ERROR_PROPAGATE(tcp_queue_received_packet(socket, 630 socket_data, packet, fragments, total_length)); 587 631 } else { 588 632 total_length = 1; … … 592 636 packet = socket_data->incoming; 593 637 while (packet) { 594 rc = pq_get_order(socket_data->incoming, &order, NULL); 595 if (rc != EOK) { 596 /* Remove the corrupted packet */ 638 639 if (ERROR_OCCURRED(pq_get_order(socket_data->incoming, 640 &order, NULL))) { 641 // remove the corrupted packet 597 642 next_packet = pq_detach(packet); 598 643 if (packet == socket_data->incoming) … … 607 652 if (IS_IN_INTERVAL_OVERFLOW(sequence_number, 608 653 old_incoming, socket_data->next_incoming)) { 609 / * Move to the next */654 // move to the next 610 655 packet = pq_next(packet); 611 / * Coninual data? */656 // coninual data? 612 657 } else if (IS_IN_INTERVAL_OVERFLOW(old_incoming, 613 658 sequence_number, socket_data->next_incoming)) { 614 / * Detach the packet */659 // detach the packet 615 660 next_packet = pq_detach(packet); 616 661 if (packet == socket_data->incoming) 617 662 socket_data->incoming = next_packet; 618 / * Get data length */663 // get data length 619 664 length = packet_get_data_length(packet); 620 665 new_sequence_number = sequence_number + length; 621 666 if (length <= 0) { 622 / * Remove the empty packet */667 // remove the empty packet 623 668 pq_release_remote(tcp_globals.net_phone, 624 669 packet_get_id(packet)); … … 626 671 continue; 627 672 } 628 / * Exactly following */673 // exactly following 629 674 if (sequence_number == 630 675 socket_data->next_incoming) { 631 /* Queue received data */ 632 rc = tcp_queue_received_packet(socket, 676 // queue received data 677 ERROR_PROPAGATE( 678 tcp_queue_received_packet(socket, 633 679 socket_data, packet, 1, 634 packet_get_data_length(packet)); 635 if (rc != EOK) 636 return rc; 680 packet_get_data_length(packet))); 637 681 socket_data->next_incoming = 638 682 new_sequence_number; 639 683 packet = next_packet; 640 684 continue; 641 / * At least partly following data? */642 } 643 if (IS_IN_INTERVAL_OVERFLOW(sequence_number,644 socket_data->next_incoming,new_sequence_number)) {685 // at least partly following data? 686 } else if (IS_IN_INTERVAL_OVERFLOW( 687 sequence_number, socket_data->next_incoming, 688 new_sequence_number)) { 645 689 if (socket_data->next_incoming < 646 690 new_sequence_number) { … … 652 696 new_sequence_number; 653 697 } 654 rc = packet_trim(packet,length, 0); 655 if (rc == EOK) { 656 /* Queue received data */ 657 rc = tcp_queue_received_packet( 698 if (ERROR_NONE(packet_trim(packet, 699 length, 0))) { 700 // queue received data 701 ERROR_PROPAGATE( 702 tcp_queue_received_packet( 658 703 socket, socket_data, packet, 659 704 1, packet_get_data_length( 660 packet)); 661 if (rc != EOK) 662 return rc; 705 packet))); 663 706 socket_data->next_incoming = 664 707 new_sequence_number; … … 667 710 } 668 711 } 669 / * Remove the duplicit or corrupted packet */712 // remove the duplicit or corrupted packet 670 713 pq_release_remote(tcp_globals.net_phone, 671 714 packet_get_id(packet)); … … 680 723 socket_data->next_incoming + socket_data->window)) { 681 724 printf("in window\n"); 682 / * Process acknowledgement */725 // process acknowledgement 683 726 tcp_process_acknowledgement(socket, socket_data, header); 684 727 685 / * Remove the header */728 // remove the header 686 729 total_length -= TCP_HEADER_LENGTH(header); 687 rc = packet_trim(packet, TCP_HEADER_LENGTH(header), 0);688 if (rc != EOK)689 return tcp_release_and_return(packet, rc);730 if (ERROR_OCCURRED(packet_trim(packet, 731 TCP_HEADER_LENGTH(header), 0))) 732 return tcp_release_and_return(packet, ERROR_CODE); 690 733 691 734 next_packet = pq_detach(packet); 692 735 length = packet_get_data_length(packet); 693 rc = pq_add(&socket_data->incoming, packet, new_sequence_number, 694 length); 695 if (rc != EOK) { 696 /* Remove the corrupted packets */ 736 if (ERROR_OCCURRED(pq_add(&socket_data->incoming, packet, 737 new_sequence_number, length))) { 738 // remove the corrupted packets 697 739 pq_release_remote(tcp_globals.net_phone, 698 740 packet_get_id(packet)); … … 704 746 tmp_packet = pq_detach(next_packet); 705 747 length = packet_get_data_length(next_packet); 706 707 rc = pq_set_order(next_packet, 708 new_sequence_number, length); 709 if (rc != EOK) { 710 pq_release_remote(tcp_globals.net_phone, 711 packet_get_id(next_packet)); 712 } 713 rc = pq_insert_after(packet, next_packet); 714 if (rc != EOK) { 748 if (ERROR_OCCURRED(pq_set_order(next_packet, 749 new_sequence_number, length)) || 750 ERROR_OCCURRED(pq_insert_after(packet, 751 next_packet))) { 715 752 pq_release_remote(tcp_globals.net_phone, 716 753 packet_get_id(next_packet)); … … 721 758 } else { 722 759 printf("unexpected\n"); 723 / * Release duplicite or restricted */760 // release duplicite or restricted 724 761 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 725 forced_ack = true; 726 } 727 728 /* If next in sequence is an incoming FIN */ 729 if (socket_data->next_incoming == socket_data->fin_incoming) { 730 /* Advance sequence number */ 731 socket_data->next_incoming += 1; 732 733 /* Handle FIN */ 762 } 763 764 // 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)) { 734 767 switch (socket_data->state) { 735 768 case TCP_SOCKET_FIN_WAIT_1: … … 738 771 socket_data->state = TCP_SOCKET_CLOSING; 739 772 break; 740 case TCP_SOCKET_ESTABLISHED: 741 /* Queue end-of-data marker on the socket. */ 742 tcp_queue_received_end_of_data(socket); 743 socket_data->state = TCP_SOCKET_CLOSE_WAIT; 744 break; 773 //case TCP_ESTABLISHED: 745 774 default: 746 775 socket_data->state = TCP_SOCKET_CLOSE_WAIT; … … 750 779 751 780 packet = tcp_get_packets_to_send(socket, socket_data); 752 if (!packet && (socket_data->next_incoming != old_incoming || forced_ack)) { 753 /* Create the notification packet */ 754 rc = tcp_create_notification_packet(&packet, socket, 755 socket_data, 0, 0); 756 if (rc != EOK) 757 return rc; 758 rc = tcp_queue_prepare_packet(socket, socket_data, packet, 1); 759 if (rc != EOK) 760 return rc; 781 if (!packet) { 782 // 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)); 761 787 packet = tcp_send_prepare_packet(socket, socket_data, packet, 1, 762 788 socket_data->last_outgoing + 1); … … 765 791 fibril_rwlock_write_unlock(socket_data->local_lock); 766 792 767 / * Send the packet */793 // send the packet 768 794 tcp_send_packets(socket_data->device_id, packet); 769 795 … … 771 797 } 772 798 773 int tcp_queue_received_packet(socket_core_t *socket, 774 tcp_socket_data_t *socket_data, packet_t *packet, int fragments, 799 int 800 tcp_queue_received_packet(socket_core_ref socket, 801 tcp_socket_data_ref socket_data, packet_t packet, int fragments, 775 802 size_t total_length) 776 803 { 777 packet_dimension_t *packet_dimension; 778 int rc; 804 ERROR_DECLARE; 805 806 packet_dimension_ref packet_dimension; 779 807 780 808 assert(socket); … … 785 813 assert(socket_data->window > total_length); 786 814 787 /* Queue the received packet */ 788 rc = dyn_fifo_push(&socket->received, packet_get_id(packet), 789 SOCKET_MAX_RECEIVED_SIZE); 790 if (rc != EOK) 791 return tcp_release_and_return(packet, rc); 792 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone, 793 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension); 794 if (rc != EOK) 795 return tcp_release_and_return(packet, rc); 796 797 /* Decrease the window size */ 815 // 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))) { 821 return tcp_release_and_return(packet, ERROR_CODE); 822 } 823 824 // decrease the window size 798 825 socket_data->window -= total_length; 799 826 800 / * Notify the destination socket */827 // notify the destination socket 801 828 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 802 ( sysarg_t) socket->socket_id,829 (ipcarg_t) socket->socket_id, 803 830 ((packet_dimension->content < socket_data->data_fragment_size) ? 804 831 packet_dimension->content : socket_data->data_fragment_size), 0, 0, 805 ( sysarg_t) fragments);832 (ipcarg_t) fragments); 806 833 807 834 return EOK; 808 835 } 809 836 810 /** Queue end-of-data marker on the socket. 811 * 812 * Next element in the sequence space is FIN. Queue end-of-data marker 813 * on the socket. 814 * 815 * @param socket Socket 816 */ 817 static void tcp_queue_received_end_of_data(socket_core_t *socket) 818 { 819 assert(socket != NULL); 820 821 /* Notify the destination socket */ 822 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 823 (sysarg_t) socket->socket_id, 824 0, 0, 0, 825 (sysarg_t) 0 /* 0 fragments == no more data */); 826 } 827 828 int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t * 829 socket_data, tcp_header_t *header, packet_t *packet) 830 { 831 packet_t *next_packet; 832 int rc; 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 { 841 ERROR_DECLARE; 842 843 packet_t next_packet; 833 844 834 845 assert(socket); … … 841 852 return tcp_release_and_return(packet, EINVAL); 842 853 843 / * Process acknowledgement */854 // process acknowledgement 844 855 tcp_process_acknowledgement(socket, socket_data, header); 845 856 846 857 socket_data->next_incoming = ntohl(header->sequence_number) + 1; 847 848 /* Release additional packets */ 858 // release additional packets 849 859 next_packet = pq_detach(packet); 850 860 if (next_packet) { … … 852 862 packet_get_id(next_packet)); 853 863 } 854 855 /* Trim if longer than the header */ 856 if (packet_get_data_length(packet) > sizeof(*header)) { 857 rc = packet_trim(packet, 0, 858 packet_get_data_length(packet) - sizeof(*header)); 859 if (rc != EOK) 860 return tcp_release_and_return(packet, rc); 861 } 862 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 } 863 870 tcp_prepare_operation_header(socket, socket_data, header, 0, 0); 864 871 fibril_mutex_lock(&socket_data->operation.mutex); 865 872 socket_data->operation.result = tcp_queue_packet(socket, socket_data, 866 873 packet, 1); 867 868 874 if (socket_data->operation.result == EOK) { 869 875 socket_data->state = TCP_SOCKET_ESTABLISHED; … … 871 877 if (packet) { 872 878 fibril_rwlock_write_unlock( socket_data->local_lock); 873 / * Send the packet */879 // send the packet 874 880 tcp_send_packets(socket_data->device_id, packet); 875 / * Signal the result */881 // signal the result 876 882 fibril_condvar_signal( &socket_data->operation.condvar); 877 883 fibril_mutex_unlock( &socket_data->operation.mutex); … … 879 885 } 880 886 } 881 882 887 fibril_mutex_unlock(&socket_data->operation.mutex); 883 888 return tcp_release_and_return(packet, EINVAL); 884 889 } 885 890 886 int tcp_process_listen(socket_core_t *listening_socket, 887 tcp_socket_data_t *listening_socket_data, tcp_header_t *header, 888 packet_t *packet, struct sockaddr *src, struct sockaddr *dest, 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, 889 895 size_t addrlen) 890 896 { 891 packet_t *next_packet; 892 socket_core_t *socket; 893 tcp_socket_data_t *socket_data; 897 ERROR_DECLARE; 898 899 packet_t next_packet; 900 socket_core_ref socket; 901 tcp_socket_data_ref socket_data; 894 902 int socket_id; 895 903 int listening_socket_id = listening_socket->socket_id; 896 904 int listening_port = listening_socket->port; 897 int rc;898 905 899 906 assert(listening_socket); … … 906 913 return tcp_release_and_return(packet, EINVAL); 907 914 908 socket_data = (tcp_socket_data_ t *) malloc(sizeof(*socket_data));915 socket_data = (tcp_socket_data_ref) malloc(sizeof(*socket_data)); 909 916 if (!socket_data) 910 917 return tcp_release_and_return(packet, ENOMEM); … … 923 930 return tcp_release_and_return(packet, ENOMEM); 924 931 } 925 926 932 memcpy(socket_data->addr, src, socket_data->addrlen); 927 933 socket_data->dest_port = ntohs(header->source_port); 928 rc = tl_set_address_port(socket_data->addr, socket_data->addrlen, 929 socket_data->dest_port); 930 if (rc != EOK) { 934 if (ERROR_OCCURRED(tl_set_address_port(socket_data->addr, 935 socket_data->addrlen, socket_data->dest_port))) { 931 936 free(socket_data->addr); 932 937 free(socket_data); 933 return tcp_release_and_return(packet, rc); 934 } 935 936 /* Create a socket */ 938 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 939 return ERROR_CODE; 940 } 941 942 // create a socket 937 943 socket_id = -1; 938 rc = socket_create(socket_data->local_sockets, listening_socket->phone, 939 socket_data, &socket_id); 940 if (rc != EOK) { 944 if (ERROR_OCCURRED(socket_create(socket_data->local_sockets, 945 listening_socket->phone, socket_data, &socket_id))) { 941 946 free(socket_data->addr); 942 947 free(socket_data); 943 return tcp_release_and_return(packet, rc);948 return tcp_release_and_return(packet, ERROR_CODE); 944 949 } 945 950 … … 953 958 fibril_rwlock_write_lock(&tcp_globals.lock); 954 959 955 / * Find the destination socket */960 // find the destination socket 956 961 listening_socket = socket_port_find(&tcp_globals.sockets, 957 listening_port, (uint8_t *)SOCKET_MAP_KEY_LISTENING, 0);958 if ( !listening_socket||962 listening_port, SOCKET_MAP_KEY_LISTENING, 0); 963 if ((!listening_socket) || 959 964 (listening_socket->socket_id != listening_socket_id)) { 960 965 fibril_rwlock_write_unlock(&tcp_globals.lock); 961 / * A shadow may remain until app hangs up */966 // a shadow may remain until app hangs up 962 967 return tcp_release_and_return(packet, EOK /*ENOTSOCK*/); 963 968 } 964 969 listening_socket_data = 965 (tcp_socket_data_ t *) listening_socket->specific_data;970 (tcp_socket_data_ref) listening_socket->specific_data; 966 971 assert(listening_socket_data); 967 972 … … 971 976 socket_id); 972 977 if (!socket) { 973 / * Where is the socket?!? */978 // where is the socket?!? 974 979 fibril_rwlock_write_unlock(&tcp_globals.lock); 975 980 return ENOTSOCK; 976 981 } 977 socket_data = (tcp_socket_data_ t *) socket->specific_data;982 socket_data = (tcp_socket_data_ref) socket->specific_data; 978 983 assert(socket_data); 979 984 980 rc = socket_port_add(&tcp_globals.sockets, listening_port, socket,981 (uint8_t*) socket_data->addr, socket_data->addrlen);985 ERROR_CODE = socket_port_add(&tcp_globals.sockets, listening_port, 986 socket, (const char *) socket_data->addr, socket_data->addrlen); 982 987 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, 983 ( uint8_t*) socket_data->addr, socket_data->addrlen));984 985 // rc= socket_bind_free_port(&tcp_globals.sockets, socket,988 (const char *) socket_data->addr, socket_data->addrlen)); 989 990 // ERROR_CODE = socket_bind_free_port(&tcp_globals.sockets, socket, 986 991 // TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, 987 992 // tcp_globals.last_used_port); 988 993 // tcp_globals.last_used_port = socket->port; 989 994 fibril_rwlock_write_unlock(&tcp_globals.lock); 990 if ( rc!= EOK) {995 if (ERROR_CODE != EOK) { 991 996 socket_destroy(tcp_globals.net_phone, socket->socket_id, 992 997 socket_data->local_sockets, &tcp_globals.sockets, 993 998 tcp_free_socket_data); 994 return tcp_release_and_return(packet, rc);999 return tcp_release_and_return(packet, ERROR_CODE); 995 1000 } 996 1001 … … 998 1003 socket_data->next_incoming = ntohl(header->sequence_number) + 1; 999 1004 1000 / * Release additional packets */1005 // release additional packets 1001 1006 next_packet = pq_detach(packet); 1002 1007 if (next_packet) { … … 1005 1010 } 1006 1011 1007 /* Trim if longer than the header */ 1008 if (packet_get_data_length(packet) > sizeof(*header)) { 1009 rc = packet_trim(packet, 0, 1010 packet_get_data_length(packet) - sizeof(*header)); 1011 if (rc != EOK) { 1012 socket_destroy(tcp_globals.net_phone, socket->socket_id, 1013 socket_data->local_sockets, &tcp_globals.sockets, 1014 tcp_free_socket_data); 1015 return tcp_release_and_return(packet, rc); 1016 } 1017 } 1018 1019 tcp_prepare_operation_header(socket, socket_data, header, 1, 0); 1020 1021 rc = tcp_queue_packet(socket, socket_data, packet, 1); 1022 if (rc != EOK) { 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)))) { 1023 1016 socket_destroy(tcp_globals.net_phone, socket->socket_id, 1024 1017 socket_data->local_sockets, &tcp_globals.sockets, 1025 1018 tcp_free_socket_data); 1026 return rc; 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; 1027 1029 } 1028 1030 … … 1038 1040 fibril_rwlock_write_unlock(socket_data->local_lock); 1039 1041 1040 / * Send the packet */1042 // send the packet 1041 1043 tcp_send_packets(socket_data->device_id, packet); 1042 1044 … … 1044 1046 } 1045 1047 1046 int tcp_process_syn_received(socket_core_t *socket, 1047 tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet) 1048 { 1049 socket_core_t *listening_socket; 1050 tcp_socket_data_t *listening_socket_data; 1051 int rc; 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 { 1052 ERROR_DECLARE; 1053 1054 socket_core_ref listening_socket; 1055 tcp_socket_data_ref listening_socket_data; 1052 1056 1053 1057 assert(socket); … … 1060 1064 return tcp_release_and_return(packet, EINVAL); 1061 1065 1062 / * Process acknowledgement */1066 // process acknowledgement 1063 1067 tcp_process_acknowledgement(socket, socket_data, header); 1064 1068 1065 socket_data->next_incoming = ntohl(header->sequence_number); /* + 1; */1069 socket_data->next_incoming = ntohl(header->sequence_number); // + 1; 1066 1070 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1067 1071 socket_data->state = TCP_SOCKET_ESTABLISHED; … … 1070 1074 if (listening_socket) { 1071 1075 listening_socket_data = 1072 (tcp_socket_data_ t *) listening_socket->specific_data;1076 (tcp_socket_data_ref) listening_socket->specific_data; 1073 1077 assert(listening_socket_data); 1074 1078 1075 /* Queue the received packet */ 1076 rc = dyn_fifo_push(&listening_socket->accepted, 1077 (-1 * socket->socket_id), listening_socket_data->backlog); 1078 if (rc == EOK) { 1079 /* Notify the destination socket */ 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 1080 1085 async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, 1081 ( sysarg_t) listening_socket->socket_id,1086 (ipcarg_t) listening_socket->socket_id, 1082 1087 socket_data->data_fragment_size, TCP_HEADER_SIZE, 1083 0, ( sysarg_t) socket->socket_id);1088 0, (ipcarg_t) socket->socket_id); 1084 1089 1085 1090 fibril_rwlock_write_unlock(socket_data->local_lock); … … 1087 1092 } 1088 1093 } 1089 / * Send FIN */1094 // send FIN 1090 1095 socket_data->state = TCP_SOCKET_FIN_WAIT_1; 1091 1096 1092 /* Create the notification packet */ 1093 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1); 1094 if (rc != EOK) 1095 return rc; 1096 1097 /* Send the packet */ 1098 rc = tcp_queue_packet(socket, socket_data, packet, 1); 1099 if (rc != EOK) 1100 return rc; 1101 1102 /* Flush packets */ 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 1103 1105 packet = tcp_get_packets_to_send(socket, socket_data); 1104 1106 fibril_rwlock_write_unlock(socket_data->local_lock); 1105 1107 if (packet) { 1106 / * Send the packet */1108 // send the packet 1107 1109 tcp_send_packets(socket_data->device_id, packet); 1108 1110 } … … 1111 1113 } 1112 1114 1113 void tcp_process_acknowledgement(socket_core_t *socket, 1114 tcp_socket_data_t *socket_data, tcp_header_t *header) 1115 void 1116 tcp_process_acknowledgement(socket_core_ref socket, 1117 tcp_socket_data_ref socket_data, tcp_header_ref header) 1115 1118 { 1116 1119 size_t number; 1117 1120 size_t length; 1118 packet_t *packet;1119 packet_t *next;1120 packet_t *acknowledged = NULL;1121 packet_t packet; 1122 packet_t next; 1123 packet_t acknowledged = NULL; 1121 1124 uint32_t old; 1122 1125 … … 1130 1133 1131 1134 number = ntohl(header->acknowledgement_number); 1132 1133 /* If more data acknowledged */ 1135 // if more data acknowledged 1134 1136 if (number != socket_data->expected) { 1135 1137 old = socket_data->expected; … … 1142 1144 case TCP_SOCKET_LAST_ACK: 1143 1145 case TCP_SOCKET_CLOSING: 1144 /* 1145 * FIN acknowledged - release the socket in 1146 * another fibril. 1147 */ 1146 // fin acknowledged - release the socket in 1147 // another fibril 1148 1148 tcp_prepare_timeout(tcp_release_after_timeout, 1149 1149 socket, socket_data, 0, … … 1155 1155 } 1156 1156 } 1157 1158 /* Update the treshold if higher than set */ 1157 // update the treshold if higher than set 1159 1158 if (number + ntohs(header->window) > 1160 1159 socket_data->expected + socket_data->treshold) { … … 1162 1161 socket_data->expected; 1163 1162 } 1164 1165 /* Set new expected sequence number */ 1163 // set new expected sequence number 1166 1164 socket_data->expected = number; 1167 1165 socket_data->expected_count = 1; … … 1175 1173 socket_data->outgoing = next; 1176 1174 1177 / * Add to acknowledged or release */1175 // add to acknowledged or release 1178 1176 if (pq_add(&acknowledged, packet, 0, 0) != EOK) 1179 1177 pq_release_remote(tcp_globals.net_phone, … … 1183 1181 break; 1184 1182 } 1185 1186 /* Release acknowledged */ 1183 // release acknowledged 1187 1184 if (acknowledged) { 1188 1185 pq_release_remote(tcp_globals.net_phone, … … 1190 1187 } 1191 1188 return; 1192 /* If the same as the previous time */ 1193 } 1194 1189 // if the same as the previous time 1190 } 1195 1191 if (number == socket_data->expected) { 1196 / * Increase the counter */1197 socket_data->expected_count++;1192 // increase the counter 1193 ++socket_data->expected_count; 1198 1194 if (socket_data->expected_count == TCP_FAST_RETRANSMIT_COUNT) { 1199 1195 socket_data->expected_count = 1; 1200 / * TODO retransmit lock */1196 // TODO retransmit lock 1201 1197 //tcp_retransmit_packet(socket, socket_data, number); 1202 1198 } … … 1204 1200 } 1205 1201 1206 /** Per-connection initialization 1207 * 1208 */ 1209 void tl_connection(void) 1210 { 1211 } 1212 1213 /** Processes the TCP message. 1214 * 1215 * @param[in] callid The message identifier. 1216 * @param[in] call The message parameters. 1217 * @param[out] answer The message answer parameters. 1218 * @param[out] answer_count The last parameter for the actual answer in the 1219 * answer parameter. 1220 * @return EOK on success. 1221 * @return ENOTSUP if the message is not known. 1222 * 1223 * @see tcp_interface.h 1224 * @see IS_NET_TCP_MESSAGE() 1225 */ 1226 int tl_message(ipc_callid_t callid, ipc_call_t *call, 1227 ipc_call_t *answer, size_t *answer_count) 1228 { 1202 int tcp_message_standalone(ipc_callid_t callid, ipc_call_t * call, 1203 ipc_call_t * answer, int *answer_count) 1204 { 1205 ERROR_DECLARE; 1206 1207 packet_t packet; 1208 1229 1209 assert(call); 1230 1210 assert(answer); … … 1232 1212 1233 1213 *answer_count = 0; 1234 switch (IPC_GET_IMETHOD(*call)) { 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 1235 1225 case IPC_M_CONNECT_TO_ME: 1236 1226 return tcp_process_client_messages(callid, *call); … … 1240 1230 } 1241 1231 1242 void tcp_refresh_socket_data(tcp_socket_data_ t *socket_data)1232 void tcp_refresh_socket_data(tcp_socket_data_ref socket_data) 1243 1233 { 1244 1234 assert(socket_data); … … 1256 1246 } 1257 1247 1258 void tcp_initialize_socket_data(tcp_socket_data_ t *socket_data)1248 void tcp_initialize_socket_data(tcp_socket_data_ref socket_data) 1259 1249 { 1260 1250 assert(socket_data); … … 1271 1261 bool keep_on_going = true; 1272 1262 socket_cores_t local_sockets; 1273 int app_phone = IPC_GET_PHONE( call);1263 int app_phone = IPC_GET_PHONE(&call); 1274 1264 struct sockaddr *addr; 1275 1265 int socket_id; … … 1278 1268 fibril_rwlock_t lock; 1279 1269 ipc_call_t answer; 1280 size_t answer_count;1281 tcp_socket_data_ t *socket_data;1282 socket_core_ t *socket;1283 packet_dimension_ t *packet_dimension;1270 int answer_count; 1271 tcp_socket_data_ref socket_data; 1272 socket_core_ref socket; 1273 packet_dimension_ref packet_dimension; 1284 1274 1285 1275 /* … … 1295 1285 while (keep_on_going) { 1296 1286 1297 / * Answer the call */1287 // answer the call 1298 1288 answer_call(callid, res, &answer, answer_count); 1299 / * Refresh data */1289 // refresh data 1300 1290 refresh_answer(&answer, &answer_count); 1301 / * Get the next call */1291 // get the next call 1302 1292 callid = async_get_call(&call); 1303 1293 1304 / * Process the call */1305 switch (IPC_GET_ IMETHOD(call)) {1294 // process the call 1295 switch (IPC_GET_METHOD(call)) { 1306 1296 case IPC_M_PHONE_HUNGUP: 1307 1297 keep_on_going = false; … … 1311 1301 case NET_SOCKET: 1312 1302 socket_data = 1313 (tcp_socket_data_ t *) malloc(sizeof(*socket_data));1303 (tcp_socket_data_ref) malloc(sizeof(*socket_data)); 1314 1304 if (!socket_data) { 1315 1305 res = ENOMEM; … … 1345 1335 1346 1336 case NET_SOCKET_BIND: 1347 res = async_data_write_accept((void **) &addr, false, 1348 0, 0, 0, &addrlen); 1337 res = data_receive((void **) &addr, &addrlen); 1349 1338 if (res != EOK) 1350 1339 break; … … 1359 1348 SOCKET_GET_SOCKET_ID(call)); 1360 1349 if (socket) { 1361 socket_data = (tcp_socket_data_ t *)1350 socket_data = (tcp_socket_data_ref) 1362 1351 socket->specific_data; 1363 1352 assert(socket_data); … … 1383 1372 1384 1373 case NET_SOCKET_CONNECT: 1385 res = async_data_write_accept((void **) &addr, false, 1386 0, 0, 0, &addrlen); 1374 res = data_receive((void **) &addr, &addrlen); 1387 1375 if (res != EOK) 1388 1376 break; 1389 /* 1390 * The global lock may be released in the 1391 * tcp_connect_message() function. 1392 */ 1377 // the global lock may be released in the 1378 // tcp_connect_message() function 1393 1379 fibril_rwlock_write_lock(&tcp_globals.lock); 1394 1380 fibril_rwlock_write_lock(&lock); … … 1435 1421 1436 1422 case NET_SOCKET_SENDTO: 1437 res = async_data_write_accept((void **) &addr, false, 1438 0, 0, 0, &addrlen); 1423 res = data_receive((void **) &addr, &addrlen); 1439 1424 if (res != EOK) 1440 1425 break; … … 1505 1490 } 1506 1491 1507 / * Release the application phone */1508 async_hangup(app_phone);1492 // release the application phone 1493 ipc_hangup(app_phone); 1509 1494 1510 1495 printf("release\n"); 1511 / * Release all local sockets */1496 // release all local sockets 1512 1497 socket_cores_release(tcp_globals.net_phone, &local_sockets, 1513 1498 &tcp_globals.sockets, tcp_free_socket_data); … … 1518 1503 int tcp_timeout(void *data) 1519 1504 { 1520 tcp_timeout_ t *timeout = data;1505 tcp_timeout_ref timeout = data; 1521 1506 int keep_write_lock = false; 1522 socket_core_ t *socket;1523 tcp_socket_data_ t *socket_data;1507 socket_core_ref socket; 1508 tcp_socket_data_ref socket_data; 1524 1509 1525 1510 assert(timeout); 1526 1511 1527 / * Sleep the given timeout */1512 // sleep the given timeout 1528 1513 async_usleep(timeout->timeout); 1529 / * Lock the globals */1514 // lock the globals 1530 1515 if (timeout->globals_read_only) 1531 1516 fibril_rwlock_read_lock(&tcp_globals.lock); … … 1533 1518 fibril_rwlock_write_lock(&tcp_globals.lock); 1534 1519 1535 / * Find the pending operation socket */1520 // find the pending operation socket 1536 1521 socket = socket_port_find(&tcp_globals.sockets, timeout->port, 1537 1522 timeout->key, timeout->key_length); 1538 if (! socket || (socket->socket_id != timeout->socket_id))1523 if (!(socket && (socket->socket_id == timeout->socket_id))) 1539 1524 goto out; 1540 1525 1541 socket_data = (tcp_socket_data_ t *) socket->specific_data;1526 socket_data = (tcp_socket_data_ref) socket->specific_data; 1542 1527 assert(socket_data); 1543 1528 if (socket_data->local_sockets != timeout->local_sockets) … … 1546 1531 fibril_rwlock_write_lock(socket_data->local_lock); 1547 1532 if (timeout->sequence_number) { 1548 / * Increase the timeout counter */1549 socket_data->timeout_count++;1533 // increase the timeout counter; 1534 ++socket_data->timeout_count; 1550 1535 if (socket_data->timeout_count == TCP_MAX_TIMEOUTS) { 1551 / * TODO release as connection lost */1536 // TODO release as connection lost 1552 1537 //tcp_refresh_socket_data(socket_data); 1553 1538 fibril_rwlock_write_unlock(socket_data->local_lock); 1554 1539 } else { 1555 / * Retransmit */1540 // retransmit 1556 1541 // tcp_retransmit_packet(socket, 1557 1542 // socket_data, timeout->sequence_number); … … 1560 1545 } else { 1561 1546 fibril_mutex_lock(&socket_data->operation.mutex); 1562 /* Set the timeout operation result if state not changed */ 1547 // set the timeout operation result if state not 1548 // changed 1563 1549 if (socket_data->state == timeout->state) { 1564 1550 socket_data->operation.result = ETIMEOUT; 1565 1566 /* Notify the main fibril */ 1551 // notify the main fibril 1567 1552 fibril_condvar_signal(&socket_data->operation.condvar); 1568 1569 /* Keep the global write lock */ 1553 // keep the global write lock 1570 1554 keep_write_lock = true; 1571 1555 } else { 1572 /* 1573 * Operation is ok, do nothing. 1574 * Unlocking from now on, so the unlocking 1575 * order does not matter. 1576 */ 1556 // operation is ok, do nothing 1557 // unlocking from now on, so the unlocki 1558 // order does not matter... 1577 1559 fibril_rwlock_write_unlock(socket_data->local_lock); 1578 1560 } … … 1581 1563 1582 1564 out: 1583 / * Unlock only if no socket */1565 // unlock only if no socket 1584 1566 if (timeout->globals_read_only) 1585 1567 fibril_rwlock_read_unlock(&tcp_globals.lock); 1586 1568 else if (!keep_write_lock) 1587 / * Release if not desired */1569 // release if not desired 1588 1570 fibril_rwlock_write_unlock(&tcp_globals.lock); 1589 1571 1590 / * Release the timeout structure */1572 // release the timeout structure 1591 1573 free(timeout); 1592 1574 return EOK; … … 1595 1577 int tcp_release_after_timeout(void *data) 1596 1578 { 1597 tcp_timeout_ t *timeout = data;1598 socket_core_ t *socket;1599 tcp_socket_data_ t *socket_data;1579 tcp_timeout_ref timeout = data; 1580 socket_core_ref socket; 1581 tcp_socket_data_ref socket_data; 1600 1582 fibril_rwlock_t *local_lock; 1601 1583 1602 1584 assert(timeout); 1603 1585 1604 / * Sleep the given timeout */1586 // sleep the given timeout 1605 1587 async_usleep(timeout->timeout); 1606 1607 /* Lock the globals */ 1588 // lock the globals 1608 1589 fibril_rwlock_write_lock(&tcp_globals.lock); 1609 1610 /* Find the pending operation socket */ 1590 // find the pending operation socket 1611 1591 socket = socket_port_find(&tcp_globals.sockets, timeout->port, 1612 1592 timeout->key, timeout->key_length); 1613 1614 1593 if (socket && (socket->socket_id == timeout->socket_id)) { 1615 socket_data = (tcp_socket_data_ t *) socket->specific_data;1594 socket_data = (tcp_socket_data_ref) socket->specific_data; 1616 1595 assert(socket_data); 1617 1596 if (socket_data->local_sockets == timeout->local_sockets) { … … 1624 1603 } 1625 1604 } 1626 1627 /* Unlock the globals */ 1605 // unlock the globals 1628 1606 fibril_rwlock_write_unlock(&tcp_globals.lock); 1629 1630 /* Release the timeout structure */ 1607 // release the timeout structure 1631 1608 free(timeout); 1632 1633 1609 return EOK; 1634 1610 } 1635 1611 1636 void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t * 1637 socket_data, size_t sequence_number) 1638 { 1639 packet_t *packet; 1640 packet_t *copy; 1612 void 1613 tcp_retransmit_packet(socket_core_ref socket, tcp_socket_data_ref socket_data, 1614 size_t sequence_number) 1615 { 1616 packet_t packet; 1617 packet_t copy; 1641 1618 size_t data_length; 1642 1619 … … 1645 1622 assert(socket->specific_data == socket_data); 1646 1623 1647 / * Sent packet? */1624 // sent packet? 1648 1625 packet = pq_find(socket_data->outgoing, sequence_number); 1649 1626 printf("retransmit %d\n", packet_get_id(packet)); … … 1661 1638 } 1662 1639 1663 int tcp_listen_message(socket_cores_t *local_sockets, int socket_id,1664 1665 { 1666 socket_core_ t *socket;1667 tcp_socket_data_ t *socket_data;1640 int 1641 tcp_listen_message(socket_cores_ref local_sockets, int socket_id, int backlog) 1642 { 1643 socket_core_ref socket; 1644 tcp_socket_data_ref socket_data; 1668 1645 1669 1646 assert(local_sockets); … … 1672 1649 return EINVAL; 1673 1650 1674 / * Find the socket */1651 // find the socket 1675 1652 socket = socket_cores_find(local_sockets, socket_id); 1676 1653 if (!socket) 1677 1654 return ENOTSOCK; 1678 1655 1679 / * Get the socket specific data */1680 socket_data = (tcp_socket_data_ t *) socket->specific_data;1656 // get the socket specific data 1657 socket_data = (tcp_socket_data_ref) socket->specific_data; 1681 1658 assert(socket_data); 1682 1683 /* Set the backlog */ 1659 // set the backlog 1684 1660 socket_data->backlog = backlog; 1685 1661 … … 1687 1663 } 1688 1664 1689 int tcp_connect_message(socket_cores_t *local_sockets, int socket_id, 1665 int 1666 tcp_connect_message(socket_cores_ref local_sockets, int socket_id, 1690 1667 struct sockaddr *addr, socklen_t addrlen) 1691 1668 { 1692 socket_core_t *socket; 1693 int rc; 1669 ERROR_DECLARE; 1670 1671 socket_core_ref socket; 1694 1672 1695 1673 assert(local_sockets); … … 1697 1675 assert(addrlen > 0); 1698 1676 1699 / * Find the socket */1677 // find the socket 1700 1678 socket = socket_cores_find(local_sockets, socket_id); 1701 1679 if (!socket) 1702 1680 return ENOTSOCK; 1703 1681 1704 rc = tcp_connect_core(socket, local_sockets, addr, addrlen);1705 if (rc != EOK) {1682 if (ERROR_OCCURRED(tcp_connect_core(socket, local_sockets, addr, 1683 addrlen))) { 1706 1684 tcp_free_socket_data(socket); 1707 / * Unbind if bound */1685 // unbind if bound 1708 1686 if (socket->port > 0) { 1709 1687 socket_ports_exclude(&tcp_globals.sockets, 1710 socket->port , free);1688 socket->port); 1711 1689 socket->port = 0; 1712 1690 } 1713 1691 } 1714 return rc; 1715 } 1716 1717 int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets, 1692 return ERROR_CODE; 1693 } 1694 1695 int 1696 tcp_connect_core(socket_core_ref socket, socket_cores_ref local_sockets, 1718 1697 struct sockaddr *addr, socklen_t addrlen) 1719 1698 { 1720 tcp_socket_data_t *socket_data; 1721 packet_t *packet; 1722 int rc; 1699 ERROR_DECLARE; 1700 1701 tcp_socket_data_ref socket_data; 1702 packet_t packet; 1723 1703 1724 1704 assert(socket); … … 1726 1706 assert(addrlen > 0); 1727 1707 1728 / * Get the socket specific data */1729 socket_data = (tcp_socket_data_ t *) socket->specific_data;1708 // get the socket specific data 1709 socket_data = (tcp_socket_data_ref) socket->specific_data; 1730 1710 assert(socket_data); 1731 1711 assert(socket->specific_data == socket_data); … … 1735 1715 return EINVAL; 1736 1716 1737 /* Get the destination port */ 1738 rc = tl_get_address_port(addr, addrlen, &socket_data->dest_port); 1739 if (rc != EOK) 1740 return rc; 1741 1717 // get the destination port 1718 ERROR_PROPAGATE(tl_get_address_port(addr, addrlen, 1719 &socket_data->dest_port)); 1742 1720 if (socket->port <= 0) { 1743 /* Try to find a free port */ 1744 rc = socket_bind_free_port(&tcp_globals.sockets, socket, 1745 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, 1746 tcp_globals.last_used_port); 1747 if (rc != EOK) 1748 return rc; 1749 /* Set the next port as the search starting port number */ 1721 // 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)); 1725 // set the next port as the search starting port number 1750 1726 tcp_globals.last_used_port = socket->port; 1751 1727 } 1752 1728 1753 rc =ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP,1729 ERROR_PROPAGATE(ip_get_route_req(tcp_globals.ip_phone, IPPROTO_TCP, 1754 1730 addr, addrlen, &socket_data->device_id, 1755 &socket_data->pseudo_header, &socket_data->headerlen); 1756 if (rc != EOK) 1757 return rc; 1758 1759 /* Create the notification packet */ 1760 rc = tcp_create_notification_packet(&packet, socket, socket_data, 1, 0); 1761 if (rc != EOK) 1762 return rc; 1763 1764 /* Unlock the globals and wait for an operation */ 1731 &socket_data->pseudo_header, &socket_data->headerlen)); 1732 1733 // create the notification packet 1734 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, 1735 socket_data, 1, 0)); 1736 1737 // unlock the globals and wait for an operation 1765 1738 fibril_rwlock_write_unlock(&tcp_globals.lock); 1766 1739 1767 1740 socket_data->addr = addr; 1768 1741 socket_data->addrlen = addrlen; 1769 1770 /* Send the packet */ 1771 1772 if (((rc = tcp_queue_packet(socket, socket_data, packet, 1)) != EOK) || 1773 ((rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data, 0, 1774 TCP_SOCKET_INITIAL, NET_DEFAULT_TCP_INITIAL_TIMEOUT, false)) != 1775 EOK)) { 1742 // 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 1776 1747 socket_data->addr = NULL; 1777 1748 socket_data->addrlen = 0; 1778 1749 fibril_rwlock_write_lock(&tcp_globals.lock); 1750 1779 1751 } else { 1752 1780 1753 packet = tcp_get_packets_to_send(socket, socket_data); 1781 1754 if (packet) { 1782 1755 fibril_mutex_lock(&socket_data->operation.mutex); 1783 1756 fibril_rwlock_write_unlock(socket_data->local_lock); 1784 1785 socket_data->state = TCP_SOCKET_SYN_SENT; 1786 1787 /* Send the packet */ 1757 // send the packet 1788 1758 printf("connecting %d\n", packet_get_id(packet)); 1789 1759 tcp_send_packets(socket_data->device_id, packet); 1790 1760 1791 / * Wait for a reply */1761 // wait for a reply 1792 1762 fibril_condvar_wait(&socket_data->operation.condvar, 1793 1763 &socket_data->operation.mutex); 1794 rc= socket_data->operation.result;1795 if ( rc!= EOK) {1764 ERROR_CODE = socket_data->operation.result; 1765 if (ERROR_CODE != EOK) { 1796 1766 socket_data->addr = NULL; 1797 1767 socket_data->addrlen = 0; … … 1800 1770 socket_data->addr = NULL; 1801 1771 socket_data->addrlen = 0; 1802 rc= EINTR;1772 ERROR_CODE = EINTR; 1803 1773 } 1804 1774 } 1805 1775 1806 1776 fibril_mutex_unlock(&socket_data->operation.mutex); 1807 return rc; 1808 } 1809 1810 int tcp_queue_prepare_packet(socket_core_t *socket, 1811 tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length) 1812 { 1813 tcp_header_t *header; 1814 int rc; 1777 1778 // return the result 1779 return ERROR_CODE; 1780 } 1781 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 { 1786 ERROR_DECLARE; 1787 1788 tcp_header_ref header; 1815 1789 1816 1790 assert(socket); … … 1818 1792 assert(socket->specific_data == socket_data); 1819 1793 1820 / * Get TCP header */1821 header = (tcp_header_ t *) packet_get_data(packet);1794 // get tcp header 1795 header = (tcp_header_ref) packet_get_data(packet); 1822 1796 if (!header) 1823 1797 return NO_DATA; … … 1827 1801 header->sequence_number = htonl(socket_data->next_outgoing); 1828 1802 1829 rc = packet_set_addr(packet, NULL, (uint8_t *) socket_data->addr, 1830 socket_data->addrlen); 1831 if (rc != EOK) 1803 if (ERROR_OCCURRED(packet_set_addr(packet, NULL, 1804 (uint8_t *) socket_data->addr, socket_data->addrlen))) 1832 1805 return tcp_release_and_return(packet, EINVAL); 1833 1806 1834 / * Remember the outgoing FIN */1807 // remember the outgoing FIN 1835 1808 if (header->finalize) 1836 1809 socket_data->fin_outgoing = socket_data->next_outgoing; … … 1839 1812 } 1840 1813 1841 int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data, 1842 packet_t *packet, size_t data_length) 1843 { 1844 int rc; 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 { 1818 ERROR_DECLARE; 1845 1819 1846 1820 assert(socket); … … 1848 1822 assert(socket->specific_data == socket_data); 1849 1823 1850 rc = tcp_queue_prepare_packet(socket, socket_data, packet, data_length); 1851 if (rc != EOK) 1852 return rc; 1853 1854 rc = pq_add(&socket_data->outgoing, packet, socket_data->next_outgoing, 1855 data_length); 1856 if (rc != EOK) 1857 return tcp_release_and_return(packet, rc); 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))) 1829 return tcp_release_and_return(packet, ERROR_CODE); 1858 1830 1859 1831 socket_data->next_outgoing += data_length; … … 1861 1833 } 1862 1834 1863 packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t * 1864 socket_data) 1865 { 1866 packet_t *packet; 1867 packet_t *copy; 1868 packet_t *sending = NULL; 1869 packet_t *previous = NULL; 1835 packet_t 1836 tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref socket_data) 1837 { 1838 ERROR_DECLARE; 1839 1840 packet_t packet; 1841 packet_t copy; 1842 packet_t sending = NULL; 1843 packet_t previous = NULL; 1870 1844 size_t data_length; 1871 int rc;1872 1845 1873 1846 assert(socket); … … 1879 1852 pq_get_order(packet, NULL, &data_length); 1880 1853 1881 /* 1882 * Send only if fits into the window, respecting the possible 1883 * overflow. 1884 */ 1854 // send only if fits into the window 1855 // respecting the possible overflow 1885 1856 if (!IS_IN_INTERVAL_OVERFLOW( 1886 1857 (uint32_t) socket_data->last_outgoing, … … 1896 1867 if (!sending) { 1897 1868 sending = copy; 1898 } else { 1899 rc = pq_insert_after(previous, copy); 1900 if (rc != EOK) { 1901 pq_release_remote(tcp_globals.net_phone, 1902 packet_get_id(copy)); 1903 return sending; 1904 } 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; 1905 1873 } 1906 1874 1907 1875 previous = copy; 1908 1876 packet = pq_next(packet); 1909 1910 /* Overflow occurred? */ 1911 if (!packet && 1877 // overflow occurred ? 1878 if ((!packet) && 1912 1879 (socket_data->last_outgoing > socket_data->next_outgoing)) { 1913 1880 printf("gpts overflow\n"); 1914 / * Continue from the beginning */1881 // continue from the beginning 1915 1882 packet = socket_data->outgoing; 1916 1883 } … … 1921 1888 } 1922 1889 1923 packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t * 1924 socket_data, packet_t *packet, size_t data_length, size_t sequence_number) 1925 { 1926 tcp_header_t *header; 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 { 1894 ERROR_DECLARE; 1895 1896 tcp_header_ref header; 1927 1897 uint32_t checksum; 1928 int rc;1929 1898 1930 1899 assert(socket); … … 1932 1901 assert(socket->specific_data == socket_data); 1933 1902 1934 / * Adjust the pseudo header */1935 rc = ip_client_set_pseudo_header_data_length(socket_data->pseudo_header,1936 socket_data-> headerlen, packet_get_data_length(packet));1937 if (rc != EOK) {1903 // 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)))) { 1938 1907 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1939 1908 return NULL; 1940 1909 } 1941 1910 1942 / * Get the header */1943 header = (tcp_header_ t *) packet_get_data(packet);1911 // get the header 1912 header = (tcp_header_ref) packet_get_data(packet); 1944 1913 if (!header) { 1945 1914 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); … … 1948 1917 assert(ntohl(header->sequence_number) == sequence_number); 1949 1918 1950 / * Adjust the header */1919 // adjust the header 1951 1920 if (socket_data->next_incoming) { 1952 1921 header->acknowledgement_number = … … 1956 1925 header->window = htons(socket_data->window); 1957 1926 1958 / * Checksum */1927 // checksum 1959 1928 header->checksum = 0; 1960 1929 checksum = compute_checksum(0, socket_data->pseudo_header, 1961 1930 socket_data->headerlen); 1962 checksum = compute_checksum(checksum, 1963 (uint8_t *) packet_get_data(packet), 1931 checksum = compute_checksum(checksum, (uint8_t *) packet_get_data(packet), 1964 1932 packet_get_data_length(packet)); 1965 1933 header->checksum = htons(flip_checksum(compact_checksum(checksum))); 1966 1934 1967 /* Prepare the packet */ 1968 rc = ip_client_prepare_packet(packet, IPPROTO_TCP, 0, 0, 0, 0); 1969 if (rc != EOK) { 1935 // 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))) { 1970 1940 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1971 1941 return NULL; 1972 1942 } 1973 1943 1974 rc = tcp_prepare_timeout(tcp_timeout, socket, socket_data,1975 sequence_number, socket_data->state, socket_data->timeout, true);1976 if (rc != EOK) {1977 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet));1978 return NULL;1979 }1980 1981 1944 return packet; 1982 1945 } 1983 1946 1984 packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t * 1985 socket_data, packet_t *packet, size_t data_length, size_t sequence_number) 1986 { 1987 packet_t *copy; 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 { 1951 packet_t copy; 1988 1952 1989 1953 assert(socket); … … 1991 1955 assert(socket->specific_data == socket_data); 1992 1956 1993 / * Make a copy of the packet */1957 // make a copy of the packet 1994 1958 copy = packet_get_copy(tcp_globals.net_phone, packet); 1995 1959 if (!copy) … … 2000 1964 } 2001 1965 2002 void tcp_send_packets(device_id_t device_id, packet_t *packet)2003 { 2004 packet_t *next;1966 void tcp_send_packets(device_id_t device_id, packet_t packet) 1967 { 1968 packet_t next; 2005 1969 2006 1970 while (packet) { … … 2012 1976 } 2013 1977 2014 void tcp_prepare_operation_header(socket_core_t *socket, 2015 tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize, 1978 void 1979 tcp_prepare_operation_header(socket_core_ref socket, 1980 tcp_socket_data_ref socket_data, tcp_header_ref header, int synchronize, 2016 1981 int finalize) 2017 1982 { … … 2029 1994 } 2030 1995 2031 int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t), 2032 socket_core_t *socket, tcp_socket_data_t *socket_data, 1996 int 1997 tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t), 1998 socket_core_ref socket, tcp_socket_data_ref socket_data, 2033 1999 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, 2034 2000 int globals_read_only) 2035 2001 { 2036 tcp_timeout_ t *operation_timeout;2002 tcp_timeout_ref operation_timeout; 2037 2003 fid_t fibril; 2038 2004 … … 2041 2007 assert(socket->specific_data == socket_data); 2042 2008 2043 / * Prepare the timeout with key bundle structure */2009 // prepare the timeout with key bundle structure 2044 2010 operation_timeout = malloc(sizeof(*operation_timeout) + 2045 2011 socket->key_length + 1); … … 2056 2022 operation_timeout->state = state; 2057 2023 2058 / * Copy the key */2059 operation_timeout->key = (( uint8_t*) operation_timeout) +2024 // copy the key 2025 operation_timeout->key = ((char *) operation_timeout) + 2060 2026 sizeof(*operation_timeout); 2061 2027 operation_timeout->key_length = socket->key_length; … … 2063 2029 operation_timeout->key[operation_timeout->key_length] = '\0'; 2064 2030 2065 / * Prepare the timeouting thread */2031 // prepare the timeouting thread 2066 2032 fibril = fibril_create(timeout_function, operation_timeout); 2067 2033 if (!fibril) { 2068 2034 free(operation_timeout); 2069 return ENOMEM; 2070 } 2071 2035 return EPARTY; /* FIXME: use another EC */ 2036 } 2072 2037 // fibril_mutex_lock(&socket_data->operation.mutex); 2073 / * Start the timeout fibril */2038 // start the timeouting fibril 2074 2039 fibril_add_ready(fibril); 2075 2040 //socket_data->state = state; … … 2077 2042 } 2078 2043 2079 int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id, 2080 int flags, size_t *addrlen) 2081 { 2082 socket_core_t *socket; 2083 tcp_socket_data_t *socket_data; 2044 int 2045 tcp_recvfrom_message(socket_cores_ref local_sockets, int socket_id, int flags, 2046 size_t * addrlen) 2047 { 2048 ERROR_DECLARE; 2049 2050 socket_core_ref socket; 2051 tcp_socket_data_ref socket_data; 2084 2052 int packet_id; 2085 packet_t *packet;2053 packet_t packet; 2086 2054 size_t length; 2087 int rc;2088 2055 2089 2056 assert(local_sockets); 2090 2057 2091 / * Find the socket */2058 // find the socket 2092 2059 socket = socket_cores_find(local_sockets, socket_id); 2093 2060 if (!socket) 2094 2061 return ENOTSOCK; 2095 2062 2096 / * Get the socket specific data */2063 // get the socket specific data 2097 2064 if (!socket->specific_data) 2098 2065 return NO_DATA; 2099 2066 2100 socket_data = (tcp_socket_data_ t *) socket->specific_data;2101 2102 / * Check state */2067 socket_data = (tcp_socket_data_ref) socket->specific_data; 2068 2069 // check state 2103 2070 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) && 2104 2071 (socket_data->state != TCP_SOCKET_CLOSE_WAIT)) 2105 2072 return ENOTCONN; 2106 2073 2107 / * Send the source address if desired */2074 // send the source address if desired 2108 2075 if (addrlen) { 2109 rc = data_reply(socket_data->addr, socket_data->addrlen); 2110 if (rc != EOK) 2111 return rc; 2076 ERROR_PROPAGATE(data_reply(socket_data->addr, 2077 socket_data->addrlen)); 2112 2078 *addrlen = socket_data->addrlen; 2113 2079 } 2114 2080 2115 / * Get the next received packet */2081 // get the next received packet 2116 2082 packet_id = dyn_fifo_value(&socket->received); 2117 2083 if (packet_id < 0) 2118 2084 return NO_DATA; 2119 2085 2120 rc = packet_translate_remote(tcp_globals.net_phone, &packet, packet_id); 2121 if (rc != EOK) 2122 return rc; 2123 2124 /* Reply the packets */ 2125 rc = socket_reply_packets(packet, &length); 2126 if (rc != EOK) 2127 return rc; 2128 2129 /* Release the packet */ 2086 ERROR_PROPAGATE(packet_translate_remote(tcp_globals.net_phone, &packet, 2087 packet_id)); 2088 2089 // reply the packets 2090 ERROR_PROPAGATE(socket_reply_packets(packet, &length)); 2091 2092 // release the packet 2130 2093 dyn_fifo_pop(&socket->received); 2131 2094 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 2132 2133 /* Return the total length */ 2095 // return the total length 2134 2096 return (int) length; 2135 2097 } 2136 2098 2137 int tcp_send_message(socket_cores_t *local_sockets, int socket_id, 2138 int fragments, size_t *data_fragment_size, int flags) 2139 { 2140 socket_core_t *socket; 2141 tcp_socket_data_t *socket_data; 2142 packet_dimension_t *packet_dimension; 2143 packet_t *packet; 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 { 2103 ERROR_DECLARE; 2104 2105 socket_core_ref socket; 2106 tcp_socket_data_ref socket_data; 2107 packet_dimension_ref packet_dimension; 2108 packet_t packet; 2144 2109 size_t total_length; 2145 tcp_header_ t *header;2110 tcp_header_ref header; 2146 2111 int index; 2147 2112 int result; 2148 int rc;2149 2113 2150 2114 assert(local_sockets); 2151 2115 assert(data_fragment_size); 2152 2116 2153 / * Find the socket */2117 // find the socket 2154 2118 socket = socket_cores_find(local_sockets, socket_id); 2155 2119 if (!socket) 2156 2120 return ENOTSOCK; 2157 2121 2158 / * Get the socket specific data */2122 // get the socket specific data 2159 2123 if (!socket->specific_data) 2160 2124 return NO_DATA; 2161 2125 2162 socket_data = (tcp_socket_data_ t *) socket->specific_data;2163 2164 / * Check state */2126 socket_data = (tcp_socket_data_ref) socket->specific_data; 2127 2128 // check state 2165 2129 if ((socket_data->state != TCP_SOCKET_ESTABLISHED) && 2166 2130 (socket_data->state != TCP_SOCKET_CLOSE_WAIT)) 2167 2131 return ENOTCONN; 2168 2132 2169 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone, 2170 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension); 2171 if (rc != EOK) 2172 return rc; 2133 ERROR_PROPAGATE(tl_get_ip_packet_dimension(tcp_globals.ip_phone, 2134 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension)); 2173 2135 2174 2136 *data_fragment_size = … … 2176 2138 packet_dimension->content : socket_data->data_fragment_size); 2177 2139 2178 for (index = 0; index < fragments; index++) {2179 / * Read the data fragment */2140 for (index = 0; index < fragments; ++index) { 2141 // read the data fragment 2180 2142 result = tl_socket_read_packet_data(tcp_globals.net_phone, 2181 2143 &packet, TCP_HEADER_SIZE, packet_dimension, … … 2185 2147 2186 2148 total_length = (size_t) result; 2187 2188 /* Prefix the TCP header */ 2149 // prefix the tcp header 2189 2150 header = PACKET_PREFIX(packet, tcp_header_t); 2190 2151 if (!header) … … 2192 2153 2193 2154 tcp_prepare_operation_header(socket, socket_data, header, 0, 0); 2194 rc = tcp_queue_packet(socket, socket_data, packet, total_length); 2195 if (rc != EOK) 2196 return rc; 2197 } 2198 2199 /* Flush packets */ 2155 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 2156 0)); 2157 } 2158 2159 // flush packets 2200 2160 packet = tcp_get_packets_to_send(socket, socket_data); 2201 2161 fibril_rwlock_write_unlock(socket_data->local_lock); … … 2203 2163 2204 2164 if (packet) { 2205 / * Send the packet */2165 // send the packet 2206 2166 tcp_send_packets(socket_data->device_id, packet); 2207 2167 } … … 2211 2171 2212 2172 int 2213 tcp_close_message(socket_cores_t *local_sockets, int socket_id) 2214 { 2215 socket_core_t *socket; 2216 tcp_socket_data_t *socket_data; 2217 packet_t *packet; 2218 int rc; 2219 2220 /* Find the socket */ 2173 tcp_close_message(socket_cores_ref local_sockets, int socket_id) 2174 { 2175 ERROR_DECLARE; 2176 2177 socket_core_ref socket; 2178 tcp_socket_data_ref socket_data; 2179 packet_t packet; 2180 2181 // find the socket 2221 2182 socket = socket_cores_find(local_sockets, socket_id); 2222 2183 if (!socket) 2223 2184 return ENOTSOCK; 2224 2185 2225 / * Get the socket specific data */2226 socket_data = (tcp_socket_data_ t *) socket->specific_data;2186 // get the socket specific data 2187 socket_data = (tcp_socket_data_ref) socket->specific_data; 2227 2188 assert(socket_data); 2228 2189 2229 / * Check state */2190 // check state 2230 2191 switch (socket_data->state) { 2231 2192 case TCP_SOCKET_ESTABLISHED: … … 2240 2201 2241 2202 default: 2242 / * Just destroy */2243 rc =socket_destroy(tcp_globals.net_phone, socket_id,2203 // just destroy 2204 if (ERROR_NONE(socket_destroy(tcp_globals.net_phone, socket_id, 2244 2205 local_sockets, &tcp_globals.sockets, 2245 tcp_free_socket_data); 2246 if (rc == EOK) { 2206 tcp_free_socket_data))) { 2247 2207 fibril_rwlock_write_unlock(socket_data->local_lock); 2248 2208 fibril_rwlock_write_unlock(&tcp_globals.lock); 2249 2209 } 2250 return rc; 2251 } 2252 2253 /* 2254 * Send FIN. 2255 * TODO should I wait to complete? 2256 */ 2257 2258 /* Create the notification packet */ 2259 rc = tcp_create_notification_packet(&packet, socket, socket_data, 0, 1); 2260 if (rc != EOK) 2261 return rc; 2262 2263 /* Send the packet */ 2264 rc = tcp_queue_packet(socket, socket_data, packet, 1); 2265 if (rc != EOK) 2266 return rc; 2267 2268 /* Flush packets */ 2210 return ERROR_CODE; 2211 } 2212 2213 // send FIN 2214 // TODO should I wait to complete? 2215 2216 // create the notification packet 2217 ERROR_PROPAGATE(tcp_create_notification_packet(&packet, socket, 2218 socket_data, 0, 1)); 2219 2220 // send the packet 2221 ERROR_PROPAGATE(tcp_queue_packet(socket, socket_data, packet, 1)); 2222 2223 // flush packets 2269 2224 packet = tcp_get_packets_to_send(socket, socket_data); 2270 2225 fibril_rwlock_write_unlock(socket_data->local_lock); … … 2272 2227 2273 2228 if (packet) { 2274 / * Send the packet */2229 // send the packet 2275 2230 tcp_send_packets(socket_data->device_id, packet); 2276 2231 } … … 2279 2234 } 2280 2235 2281 int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket, 2282 tcp_socket_data_t *socket_data, int synchronize, int finalize) 2283 { 2284 packet_dimension_t *packet_dimension; 2285 tcp_header_t *header; 2286 int rc; 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 { 2240 ERROR_DECLARE; 2241 2242 packet_dimension_ref packet_dimension; 2243 tcp_header_ref header; 2287 2244 2288 2245 assert(packet); 2289 2246 2290 /* Get the device packet dimension */ 2291 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone, 2292 &tcp_globals.dimensions, socket_data->device_id, &packet_dimension); 2293 if (rc != EOK) 2294 return rc; 2295 2296 /* Get a new packet */ 2247 // 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 2252 // get a new packet 2297 2253 *packet = packet_get_4_remote(tcp_globals.net_phone, TCP_HEADER_SIZE, 2298 2254 packet_dimension->addr_len, packet_dimension->prefix, … … 2302 2258 return ENOMEM; 2303 2259 2304 / * Allocate space in the packet */2260 // allocate space in the packet 2305 2261 header = PACKET_SUFFIX(*packet, tcp_header_t); 2306 2262 if (!header) … … 2313 2269 } 2314 2270 2315 int tcp_accept_message(socket_cores_t *local_sockets, int socket_id, 2316 int new_socket_id, size_t *data_fragment_size, size_t *addrlen) 2317 { 2318 socket_core_t *accepted; 2319 socket_core_t *socket; 2320 tcp_socket_data_t *socket_data; 2321 packet_dimension_t *packet_dimension; 2322 int rc; 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 { 2275 ERROR_DECLARE; 2276 2277 socket_core_ref accepted; 2278 socket_core_ref socket; 2279 tcp_socket_data_ref socket_data; 2280 packet_dimension_ref packet_dimension; 2323 2281 2324 2282 assert(local_sockets); … … 2326 2284 assert(addrlen); 2327 2285 2328 / * Find the socket */2286 // find the socket 2329 2287 socket = socket_cores_find(local_sockets, socket_id); 2330 2288 if (!socket) 2331 2289 return ENOTSOCK; 2332 2290 2333 / * Get the socket specific data */2334 socket_data = (tcp_socket_data_ t *) socket->specific_data;2291 // get the socket specific data 2292 socket_data = (tcp_socket_data_ref) socket->specific_data; 2335 2293 assert(socket_data); 2336 2294 2337 / * Check state */2295 // check state 2338 2296 if (socket_data->state != TCP_SOCKET_LISTEN) 2339 2297 return EINVAL; … … 2349 2307 return ENOTSOCK; 2350 2308 2351 / * Get the socket specific data */2352 socket_data = (tcp_socket_data_ t *) accepted->specific_data;2309 // get the socket specific data 2310 socket_data = (tcp_socket_data_ref) accepted->specific_data; 2353 2311 assert(socket_data); 2354 / * TODO can it be in another state? */2312 // TODO can it be in another state? 2355 2313 if (socket_data->state == TCP_SOCKET_ESTABLISHED) { 2356 rc = data_reply(socket_data->addr, 2357 socket_data->addrlen); 2358 if (rc != EOK) 2359 return rc; 2360 rc = tl_get_ip_packet_dimension(tcp_globals.ip_phone, 2361 &tcp_globals.dimensions, socket_data->device_id, 2362 &packet_dimension); 2363 if (rc != EOK) 2364 return rc; 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)); 2365 2319 *addrlen = socket_data->addrlen; 2366 2320 … … 2372 2326 2373 2327 if (new_socket_id > 0) { 2374 rc = socket_cores_update(local_sockets, 2375 accepted->socket_id, new_socket_id); 2376 if (rc != EOK) 2377 return rc; 2328 ERROR_PROPAGATE(socket_cores_update( 2329 local_sockets, accepted->socket_id, 2330 new_socket_id)); 2378 2331 accepted->socket_id = new_socket_id; 2379 2332 } … … 2386 2339 } 2387 2340 2388 void tcp_free_socket_data(socket_core_t *socket) 2389 { 2390 tcp_socket_data_t *socket_data; 2341 void 2342 tcp_free_socket_data(socket_core_ref socket) 2343 { 2344 tcp_socket_data_ref socket_data; 2391 2345 2392 2346 assert(socket); … … 2394 2348 printf("destroy_socket %d\n", socket->socket_id); 2395 2349 2396 / * Get the socket specific data */2397 socket_data = (tcp_socket_data_ t *) socket->specific_data;2350 // get the socket specific data 2351 socket_data = (tcp_socket_data_ref) socket->specific_data; 2398 2352 assert(socket_data); 2399 2400 /* Free the pseudo header */ 2353 //free the pseudo header 2401 2354 if (socket_data->pseudo_header) { 2402 2355 if (socket_data->headerlen) { … … 2407 2360 socket_data->pseudo_header = NULL; 2408 2361 } 2409 2410 2362 socket_data->headerlen = 0; 2411 2412 /* Free the address */ 2363 // free the address 2413 2364 if (socket_data->addr) { 2414 2365 if (socket_data->addrlen) { … … 2422 2373 } 2423 2374 2424 /** Releases the packet and returns the result. 2425 * 2426 * @param[in] packet The packet queue to be released. 2427 * @param[in] result The result to be returned. 2428 * @return The result parameter. 2429 */ 2430 int tcp_release_and_return(packet_t *packet, int result) 2375 int tcp_release_and_return(packet_t packet, int result) 2431 2376 { 2432 2377 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); … … 2434 2379 } 2435 2380 2436 /** Process IPC messages from the IP module2381 /** Default thread for new connections. 2437 2382 * 2438 * @param[in] iid Message identifier.2439 * @param[in,out] icall Message parameters.2383 * @param[in] iid The initial message identifier. 2384 * @param[in] icall The initial message call structure. 2440 2385 * 2441 2386 */ 2442 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall) 2443 { 2444 packet_t *packet; 2445 int rc; 2446 2387 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall) 2388 { 2389 /* 2390 * Accept the connection 2391 * - Answer the first IPC_M_CONNECT_ME_TO call. 2392 */ 2393 ipc_answer_0(iid, EOK); 2394 2447 2395 while (true) { 2448 switch (IPC_GET_IMETHOD(*icall)) { 2449 case NET_TL_RECEIVED: 2450 rc = packet_translate_remote(tcp_globals.net_phone, &packet, 2451 IPC_GET_PACKET(*icall)); 2452 if (rc == EOK) 2453 rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet, 2454 SERVICE_TCP, IPC_GET_ERROR(*icall)); 2455 2456 async_answer_0(iid, (sysarg_t) rc); 2457 break; 2458 default: 2459 async_answer_0(iid, (sysarg_t) ENOTSUP); 2460 } 2461 2462 iid = async_get_call(icall); 2463 } 2464 } 2465 2466 /** Initialize the TCP module. 2396 ipc_call_t answer; 2397 int answer_count; 2398 2399 /* 2400 Clear the answer structure 2401 */ 2402 refresh_answer(&answer, &answer_count); 2403 2404 /* 2405 Fetch the next message 2406 */ 2407 ipc_call_t call; 2408 ipc_callid_t callid = async_get_call(&call); 2409 2410 /* 2411 Process the message 2412 */ 2413 int res = tl_module_message_standalone(callid, &call, &answer, 2414 &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)) 2421 return; 2422 2423 /* 2424 Answer the message 2425 */ 2426 answer_call(callid, res, &answer, answer_count); 2427 } 2428 } 2429 2430 /** Starts the module. 2467 2431 * 2468 * @param[in] net_phone Network module phone. 2432 * @param argc The count of the command line arguments. Ignored parameter. 2433 * @param argv The command line parameters. Ignored parameter. 2469 2434 * 2470 * @returnEOK on success.2471 * @return ENOMEM if there is not enough memory left.2435 * @returns EOK on success. 2436 * @returns Other error codes as defined for each specific module start function. 2472 2437 * 2473 2438 */ 2474 int tl_initialize(int net_phone) 2475 { 2476 fibril_rwlock_initialize(&tcp_globals.lock); 2477 fibril_rwlock_write_lock(&tcp_globals.lock); 2478 2479 tcp_globals.net_phone = net_phone; 2480 2481 tcp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT); 2482 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, 2483 SERVICE_TCP, tcp_receiver); 2484 if (tcp_globals.ip_phone < 0) { 2485 fibril_rwlock_write_unlock(&tcp_globals.lock); 2486 return tcp_globals.ip_phone; 2487 } 2488 2489 int rc = socket_ports_initialize(&tcp_globals.sockets); 2490 if (rc != EOK) 2491 goto out; 2492 2493 rc = packet_dimensions_initialize(&tcp_globals.dimensions); 2494 if (rc != EOK) { 2495 socket_ports_destroy(&tcp_globals.sockets, free); 2496 goto out; 2497 } 2498 2499 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; 2500 2501 out: 2502 fibril_rwlock_write_unlock(&tcp_globals.lock); 2503 return rc; 2504 } 2505 2506 int main(int argc, char *argv[]) 2507 { 2508 return tl_module_start(SERVICE_TCP); 2439 int 2440 main(int argc, char *argv[]) 2441 { 2442 ERROR_DECLARE; 2443 2444 /* 2445 Start the module 2446 */ 2447 if (ERROR_OCCURRED(tl_module_start_standalone(tl_client_connection))) 2448 return ERROR_CODE; 2449 2450 return EOK; 2509 2451 } 2510 2452
Note:
See TracChangeset
for help on using the changeset viewer.