/** * Destiny TCP timer implementation * * Copyright (C) 2013 INRIA. * * This file subject to the terms and conditions of the GNU Lesser General * Public License. See the file LICENSE in the top level directory for more * details. * * @ingroup destiny * @{ * @file tcp_timer.c * @brief TCP timer * @author Oliver Gesch * @} */ #include #include #include #include #include #include "tcp_timer.h" #include "vtimer.h" #include "thread.h" #include "destiny.h" #include "socket.h" #include "net_help/msg_help.h" #include "../sixlowpan/sixlowpan.h" void handle_synchro_timeout(socket_internal_t *current_socket) { msg_t send; if (thread_getstatus(current_socket->recv_pid) == STATUS_RECEIVE_BLOCKED) { timex_t now; vtimer_now(&now); if ((current_socket->socket_values.tcp_control.no_of_retries == 0) && (timex_sub(now, current_socket->socket_values.tcp_control.last_packet_time).microseconds > TCP_SYN_INITIAL_TIMEOUT)) { current_socket->socket_values.tcp_control.no_of_retries++; net_msg_send(&send, current_socket->recv_pid, 0, TCP_RETRY); } else if ((current_socket->socket_values.tcp_control.no_of_retries > 0) && (timex_sub(now, current_socket->socket_values.tcp_control.last_packet_time).microseconds > (current_socket->socket_values.tcp_control.no_of_retries * TCP_SYN_TIMEOUT + TCP_SYN_INITIAL_TIMEOUT))) { current_socket->socket_values.tcp_control.no_of_retries++; if (current_socket->socket_values.tcp_control.no_of_retries > TCP_MAX_SYN_RETRIES) { net_msg_send(&send, current_socket->recv_pid, 0, TCP_TIMEOUT); } else { net_msg_send(&send, current_socket->recv_pid, 0, TCP_RETRY); } } } } void handle_established(socket_internal_t *current_socket) { msg_t send; double current_timeout = current_socket->socket_values.tcp_control.rto; if (current_timeout < SECOND) { current_timeout = SECOND; } uint8_t i; if ((current_socket->socket_values.tcp_control.send_nxt > current_socket->socket_values.tcp_control.send_una) && (thread_getstatus(current_socket->send_pid) == STATUS_RECEIVE_BLOCKED)) { for (i = 0; i < current_socket->socket_values.tcp_control.no_of_retries; i++) { current_timeout *= 2; } timex_t now; vtimer_now(&now); if (current_timeout > TCP_ACK_MAX_TIMEOUT) { net_msg_send(&send, current_socket->send_pid, 0, TCP_TIMEOUT); } else if (timex_sub(now, current_socket->socket_values.tcp_control.last_packet_time).microseconds > current_timeout) { current_socket->socket_values.tcp_control.no_of_retries++; net_msg_send(&send, current_socket->send_pid, 0, TCP_RETRY); } } } void check_sockets(void) { socket_internal_t *current_socket; uint8_t i = 1; while (i < MAX_SOCKETS + 1) { current_socket = getSocket(i); if (isTCPSocket(i)) { switch(current_socket->socket_values.tcp_control.state) { case ESTABLISHED: { handle_established(current_socket); break; } case SYN_SENT: { handle_synchro_timeout(current_socket); break; } case SYN_RCVD: { handle_synchro_timeout(current_socket); break; } default: { break; } } } i++; } } void inc_global_variables(void) { mutex_lock(&global_sequence_clunter_mutex); global_sequence_counter += rand(); mutex_unlock(&global_sequence_clunter_mutex); #ifdef TCP_HC mutex_lock(&global_context_counter_mutex); global_context_counter += rand(); mutex_unlock(&global_context_counter_mutex); #endif } void tcp_general_timer(void) { vtimer_t tcp_vtimer; timex_t interval = timex_set(0, TCP_TIMER_RESOLUTION); while (1) { inc_global_variables(); check_sockets(); vtimer_set_wakeup(&tcp_vtimer, interval, thread_getpid()); thread_sleep(); } }