2012-01-24 03:19:11 +01:00
|
|
|
/*
|
|
|
|
* tcp_timer.c
|
|
|
|
*
|
|
|
|
* Created on: 21.01.2012
|
|
|
|
* Author: Oliver
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "tcp_timer.h"
|
|
|
|
#include "vtimer.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "destiny.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "net_help/msg_help.h"
|
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
void handle_synchro_timeout(socket_internal_t *current_socket)
|
2012-01-24 03:19:11 +01:00
|
|
|
{
|
|
|
|
msg_t send;
|
2012-01-25 03:38:22 +01:00
|
|
|
if ((current_socket->socket_values.tcp_control.no_of_retry == 0) &&
|
|
|
|
(timex_sub(vtimer_now(), current_socket->socket_values.tcp_control.last_packet_time).microseconds >
|
2012-01-24 03:19:11 +01:00
|
|
|
TCP_SYN_INITIAL_TIMEOUT))
|
|
|
|
{
|
2012-01-25 03:38:22 +01:00
|
|
|
current_socket->socket_values.tcp_control.no_of_retry++;
|
2012-01-27 02:54:59 +01:00
|
|
|
net_msg_send(&send, current_socket->recv_pid, 0, TCP_RETRY);
|
2012-01-24 03:19:11 +01:00
|
|
|
printf("FIRST RETRY!\n");
|
|
|
|
}
|
2012-01-25 03:38:22 +01:00
|
|
|
else if ((current_socket->socket_values.tcp_control.no_of_retry > 0) &&
|
|
|
|
(timex_sub(vtimer_now(), current_socket->socket_values.tcp_control.last_packet_time).microseconds >
|
|
|
|
(current_socket->socket_values.tcp_control.no_of_retry * TCP_SYN_TIMEOUT + TCP_SYN_INITIAL_TIMEOUT)))
|
2012-01-24 03:19:11 +01:00
|
|
|
{
|
2012-01-25 03:38:22 +01:00
|
|
|
current_socket->socket_values.tcp_control.no_of_retry++;
|
|
|
|
if (current_socket->socket_values.tcp_control.no_of_retry > TCP_MAX_SYN_RETRIES)
|
2012-01-24 03:19:11 +01:00
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
net_msg_send(&send, current_socket->recv_pid, 0, TCP_TIMEOUT);
|
2012-01-24 03:19:11 +01:00
|
|
|
printf("TCP SYN TIMEOUT!!\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
net_msg_send(&send, current_socket->recv_pid, 0, TCP_RETRY);
|
2012-01-24 03:19:11 +01:00
|
|
|
printf("NEXT RETRY!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 02:54:59 +01:00
|
|
|
void handle_established(socket_internal_t *current_socket)
|
|
|
|
{
|
|
|
|
msg_t send;
|
|
|
|
uint32_t current_timeout = TCP_ACK_TIMEOUT;
|
|
|
|
uint8_t i;
|
|
|
|
if (current_socket->socket_values.tcp_control.send_nxt > current_socket->socket_values.tcp_control.send_una)
|
|
|
|
{
|
|
|
|
for(i = 0; i < current_socket->socket_values.tcp_control.no_of_retry; i++)
|
|
|
|
{
|
|
|
|
current_timeout *= 2;
|
|
|
|
}
|
|
|
|
if (current_timeout > TCP_ACK_MAX_TIMEOUT)
|
|
|
|
{
|
|
|
|
net_msg_send(&send, current_socket->send_pid, 0, TCP_TIMEOUT);
|
|
|
|
printf("GOT NO ACK: TIMEOUT!\n");
|
|
|
|
}
|
|
|
|
else if (timex_sub(vtimer_now(), current_socket->socket_values.tcp_control.last_packet_time).microseconds >
|
|
|
|
current_timeout)
|
|
|
|
{
|
|
|
|
current_socket->socket_values.tcp_control.no_of_retry++;
|
|
|
|
net_msg_send(&send, current_socket->send_pid, 0, TCP_RETRY);
|
|
|
|
printf("GOT NO ACK YET, %i. RETRY!\n", current_socket->socket_values.tcp_control.no_of_retry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-24 03:19:11 +01:00
|
|
|
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))
|
|
|
|
{
|
2012-01-25 03:38:22 +01:00
|
|
|
switch (current_socket->socket_values.tcp_control.state)
|
2012-01-24 03:19:11 +01:00
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
case ESTABLISHED:
|
|
|
|
{
|
|
|
|
handle_established(current_socket);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-24 03:19:11 +01:00
|
|
|
case SYN_SENT:
|
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
handle_synchro_timeout(current_socket);
|
2012-01-24 03:19:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SYN_RCVD:
|
|
|
|
{
|
2012-01-27 02:54:59 +01:00
|
|
|
handle_synchro_timeout(current_socket);
|
2012-01-24 03:19:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tcp_general_timer(void)
|
|
|
|
{
|
|
|
|
vtimer_t tcp_vtimer;
|
|
|
|
timex_t interval = timex_set(0, 500*1000);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
check_sockets();
|
|
|
|
// cur_time = vtimer_now();
|
|
|
|
// printf("Time Seconds: %li uSeconds: %li\n", cur_time.seconds, cur_time.microseconds);
|
|
|
|
vtimer_set_wakeup(&tcp_vtimer, interval, tcp_timer_pid);
|
|
|
|
thread_sleep();
|
|
|
|
}
|
|
|
|
}
|