2013-06-22 17:58:19 +02:00
|
|
|
/**
|
|
|
|
* Destiny TCP timer implementation
|
2012-01-24 03:19:11 +01:00
|
|
|
*
|
2013-06-22 17:58:19 +02:00
|
|
|
* 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 <oliver.gesch@googlemail.com>
|
|
|
|
* @}
|
2012-01-24 03:19:11 +01:00
|
|
|
*/
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2012-02-14 21:28:51 +01:00
|
|
|
#include <thread.h>
|
2012-01-24 03:19:11 +01:00
|
|
|
#include <stdio.h>
|
2012-02-14 21:28:51 +01:00
|
|
|
#include <string.h>
|
2012-02-05 00:33:55 +01:00
|
|
|
#include <stdlib.h>
|
2012-02-14 21:28:51 +01:00
|
|
|
#include <math.h>
|
2012-01-24 03:19:11 +01:00
|
|
|
#include "tcp_timer.h"
|
|
|
|
#include "vtimer.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "destiny.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "net_help/msg_help.h"
|
2013-05-22 16:54:20 +02:00
|
|
|
#include "../sixlowpan/sixlowpan.h"
|
2012-01-24 03:19:11 +01:00
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
void handle_synchro_timeout(socket_internal_t *current_socket)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 03:19:11 +01:00
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
void handle_established(socket_internal_t *current_socket)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 02:54:59 +01:00
|
|
|
|
2012-01-24 03:19:11 +01:00
|
|
|
void check_sockets(void)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 03:19:11 +01:00
|
|
|
|
2012-02-05 00:33:55 +01:00
|
|
|
void inc_global_variables(void)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
|
|
|
mutex_lock(&global_sequence_clunter_mutex);
|
|
|
|
global_sequence_counter += rand();
|
|
|
|
mutex_unlock(&global_sequence_clunter_mutex, 0);
|
2012-02-07 04:24:00 +01:00
|
|
|
#ifdef TCP_HC
|
2013-06-22 17:58:19 +02:00
|
|
|
mutex_lock(&global_context_counter_mutex);
|
|
|
|
global_context_counter += rand();
|
|
|
|
mutex_unlock(&global_context_counter_mutex, 0);
|
2012-02-07 04:24:00 +01:00
|
|
|
#endif
|
2013-06-22 17:58:19 +02:00
|
|
|
}
|
2012-02-05 00:33:55 +01:00
|
|
|
|
2012-01-24 03:19:11 +01:00
|
|
|
void tcp_general_timer(void)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|