mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-25 14:33:52 +01:00
[sys net destiny]
- code cleanup - refactoring
This commit is contained in:
parent
07f03a46a7
commit
b7f5405ec7
@ -1,5 +1,5 @@
|
||||
SubDir TOP projects tlayer ;
|
||||
|
||||
Module tlayer : main.c : auto_init vtimer ps shell_commands config cc110x_ng destiny uart0 posix_io shell 6lowpan rtc rpl ;
|
||||
Module tlayer : main.c : auto_init vtimer ps shell_commands config cc110x_ng destiny uart0 posix_io shell 6lowpan rtc rpl net_help ;
|
||||
|
||||
UseModule tlayer ;
|
||||
|
||||
@ -28,12 +28,12 @@
|
||||
#include "sys/net/destiny/tcp_timer.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
#include "sys/net/net_help/msg_help.h"
|
||||
#include "sys/net/net_help/printf2.h"
|
||||
|
||||
#define SEND_TCP_THREAD_SIZE 3072
|
||||
#define SHELL_EXTRA_STACK_SIZE 3072
|
||||
#define TCP_CLOSE_THREAD_STACK_SIZE 3072
|
||||
|
||||
uint8_t udp_server_thread_pid;
|
||||
|
||||
char udp_server_stack_buffer[UDP_STACK_SIZE];
|
||||
|
||||
uint8_t tcp_server_thread_pid;
|
||||
@ -42,12 +42,13 @@ char tcp_server_stack_buffer[TCP_STACK_SIZE];
|
||||
uint8_t tcp_cht_pid;
|
||||
char tcp_cht_stack_buffer[TCP_STACK_SIZE];
|
||||
|
||||
// Socket ID used for sending/receiving packets via different threads
|
||||
int tcp_socket_id = -1;
|
||||
uint8_t tcp_send_pid = -1;
|
||||
|
||||
uint8_t tcp_send_pid = -1;
|
||||
char tcp_send_stack_buffer[SEND_TCP_THREAD_SIZE];
|
||||
// TODO: Add UDP_STACK_SIZE definition again!
|
||||
char shell_extra_stack[SHELL_EXTRA_STACK_SIZE];
|
||||
|
||||
char tcp_close_thread_stack[TCP_CLOSE_THREAD_STACK_SIZE];
|
||||
|
||||
typedef struct tcp_msg_t
|
||||
{
|
||||
@ -75,10 +76,13 @@ void tcp_ch(void)
|
||||
return;
|
||||
}
|
||||
memset(&stSockAddr, 0, sizeof(stSockAddr));
|
||||
|
||||
stSockAddr.sin6_family = AF_INET6;
|
||||
stSockAddr.sin6_port = HTONS(1100);
|
||||
|
||||
ipv6_init_address(&stSockAddr.sin6_addr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, current_message.node_number);
|
||||
ipv6_print_addr(&stSockAddr.sin6_addr);
|
||||
|
||||
if (-1 == connect(SocketFD, &stSockAddr, sizeof(stSockAddr)))
|
||||
{
|
||||
printf("Connect failed!\n");
|
||||
@ -91,7 +95,7 @@ void tcp_ch(void)
|
||||
read_bytes = recv(SocketFD, buff_msg, MAX_TCP_BUFFER, 0);
|
||||
if (read_bytes > 0)
|
||||
{
|
||||
printf("--- Message: %s ---\n", buff_msg);
|
||||
// printf("--- Message: %s ---\n", buff_msg);
|
||||
}
|
||||
|
||||
}
|
||||
@ -105,8 +109,10 @@ void init_udp_server(void)
|
||||
uint32_t fromlen;
|
||||
int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||
memset(&sa, 0, sizeof sa);
|
||||
|
||||
sa.sin6_family = AF_INET;
|
||||
sa.sin6_port = HTONS(7654);
|
||||
|
||||
fromlen = sizeof(sa);
|
||||
if (-1 == bind(sock, &sa, sizeof(sa)))
|
||||
{
|
||||
@ -120,8 +126,8 @@ void init_udp_server(void)
|
||||
{
|
||||
printf("ERROR: recsize < 0!\n");
|
||||
}
|
||||
// printf("recsize: %i\n ", recsize);
|
||||
// printf("datagram: %s\n", buffer_main);
|
||||
printf("recsize: %i\n ", recsize);
|
||||
printf("datagram: %s\n", buffer_main);
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,6 +148,7 @@ void init_tcp_server(void)
|
||||
|
||||
stSockAddr.sin6_family = AF_INET6;
|
||||
stSockAddr.sin6_port = HTONS(1100);
|
||||
|
||||
// TODO: HARDCODED! Use one of the IPv6 methods after initializing the node to get the correct ipv6 address!
|
||||
ipv6_init_address(&stSockAddr.sin6_addr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, get_radio_address());
|
||||
ipv6_print_addr(&stSockAddr.sin6_addr);
|
||||
@ -177,7 +184,7 @@ void init_tcp_server(void)
|
||||
|
||||
if (read_bytes > 0)
|
||||
{
|
||||
printf("--- Message: %s ---\n", buff_msg);
|
||||
// printf("--- Message: %s ---\n", buff_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -225,7 +232,7 @@ void send_tcp_msg(char *str)
|
||||
{
|
||||
msg_t send_msg, recv_msg;
|
||||
sscanf(str, "send_tcp %s", current_message.tcp_string_msg);
|
||||
printf("Message: %s\n", current_message.tcp_string_msg);
|
||||
// printf("Message: %s\n", current_message.tcp_string_msg);
|
||||
if (strcmp(current_message.tcp_string_msg, "close") == 0)
|
||||
{
|
||||
send_msg.content.value = 0;
|
||||
@ -235,7 +242,6 @@ void send_tcp_msg(char *str)
|
||||
send_msg.content.value = 1;
|
||||
}
|
||||
msg_send_receive(&send_msg, &recv_msg, tcp_send_pid);
|
||||
// msg_send(&send_msg, tcp_send_pid, 0);
|
||||
}
|
||||
|
||||
void send_tcp_bulk(char *str)
|
||||
@ -269,6 +275,7 @@ void send_tcp_bandwidth_test(char *str)
|
||||
}
|
||||
end = vtimer_now();
|
||||
total = timex_sub(end, start);
|
||||
secs = total.microseconds / 1000000;
|
||||
printf("Start: %lu, End: %lu, Total: %lu\n", start.microseconds, end.microseconds, total.microseconds);
|
||||
secs = total.microseconds / 1000000;
|
||||
printf("Time: %lu seconds, Bandwidth: %lu byte/second\n", secs, (count*48)/secs);
|
||||
@ -402,7 +409,7 @@ void send_packet(char *str){
|
||||
void send_udp(char *str)
|
||||
{
|
||||
timex_t start, end, total;
|
||||
uint32_t secs;
|
||||
float secs;
|
||||
int sock;
|
||||
sockaddr6_t sa;
|
||||
ipv6_addr_t ipaddr;
|
||||
@ -439,7 +446,7 @@ void send_udp(char *str)
|
||||
total = timex_sub(end, start);
|
||||
printf("Start: %lu, End: %lu, Total: %lu\n", start.microseconds, end.microseconds, total.microseconds);
|
||||
secs = total.microseconds / 1000000;
|
||||
printf("Time: %lu seconds, Bandwidth: %lu byte/second\n", secs, (count*48)/secs);
|
||||
printf("Time: %f seconds, Bandwidth: %f byte/second\n", secs, (count*48)/secs);
|
||||
close(sock);
|
||||
}
|
||||
|
||||
@ -509,8 +516,8 @@ void close_tcp_thread(void)
|
||||
|
||||
void close_tcp (char *str)
|
||||
{
|
||||
thread_create(shell_extra_stack, SHELL_EXTRA_STACK_SIZE, PRIORITY_MAIN,
|
||||
CREATE_STACKTEST, close_tcp_thread, "close_tcp_thread");
|
||||
thread_create(tcp_close_thread_stack, TCP_CLOSE_THREAD_STACK_SIZE, PRIORITY_MAIN,
|
||||
CREATE_STACKTEST, close_tcp_thread, "tcp_close_thread");
|
||||
}
|
||||
|
||||
const shell_command_t shell_commands[] = {
|
||||
@ -539,6 +546,7 @@ const shell_command_t shell_commands[] = {
|
||||
int main(void) {
|
||||
printf("6LoWPAN Transport Layers\n");
|
||||
posix_open(uart0_handler_pid, 0);
|
||||
|
||||
init_tl(NULL);
|
||||
|
||||
shell_t shell;
|
||||
|
||||
@ -28,6 +28,6 @@ void init_transport_layer(void)
|
||||
int tcp_thread_pid = thread_create(tcp_stack_buffer, TCP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, tcp_packet_handler, "tcp_packet_handler");
|
||||
set_tcp_packet_handler_pid(tcp_thread_pid);
|
||||
|
||||
tcp_timer_pid = thread_create(tcp_timer_stack, TCP_TIMER_STACKSIZE, PRIORITY_MAIN+1, CREATE_STACKTEST, tcp_general_timer, "tcp_general_timer");
|
||||
thread_create(tcp_timer_stack, TCP_TIMER_STACKSIZE, PRIORITY_MAIN+1, CREATE_STACKTEST, tcp_general_timer, "tcp_general_timer");
|
||||
|
||||
}
|
||||
|
||||
@ -10,6 +10,4 @@
|
||||
|
||||
void init_transport_layer(void);
|
||||
|
||||
int tcp_timer_pid;
|
||||
|
||||
#endif /* DESTINY_H_ */
|
||||
|
||||
@ -280,12 +280,12 @@ socket_internal_t *get_tcp_socket(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header
|
||||
while (i < MAX_SOCKETS+1)
|
||||
{
|
||||
current_socket = getSocket(i);
|
||||
// Check for matching 4 touple
|
||||
// Check for matching 4 touple, ESTABLISHED connection
|
||||
if( isTCPSocket(i) && is_four_touple(current_socket, ipv6_header, tcp_header))
|
||||
{
|
||||
return current_socket;
|
||||
}
|
||||
// TODO: Figure out a better strategy of using *.LOCAL_ADRESS than using the last Byte
|
||||
// Sockets in LISTEN and SYN_RCVD state should only be tested on local TCP values
|
||||
else if ( isTCPSocket(i) &&
|
||||
((current_socket->socket_values.tcp_control.state == LISTEN) || (current_socket->socket_values.tcp_control.state == SYN_RCVD)) &&
|
||||
(current_socket->socket_values.local_address.sin6_addr.uint8[15] == ipv6_header->destaddr.uint8[15]) &&
|
||||
@ -362,19 +362,8 @@ int check_tcp_consistency(socket_t *current_tcp_socket, tcp_hdr_t *tcp_header)
|
||||
return PACKET_OK;
|
||||
}
|
||||
|
||||
int send_tcp(sockaddr6_t *addr, socket_t *current_tcp_socket, tcp_hdr_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length)
|
||||
int send_tcp(socket_t *current_tcp_socket, tcp_hdr_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length)
|
||||
{
|
||||
if (addr != NULL)
|
||||
{
|
||||
// Local address information
|
||||
ipv6_addr_t src_addr;
|
||||
ipv6_get_saddr(&src_addr, &addr->sin6_addr);
|
||||
set_socket_address(¤t_tcp_socket->local_address, PF_INET6, HTONS(get_free_source_port(IPPROTO_TCP)), 0, &src_addr);
|
||||
|
||||
// Foreign address information
|
||||
set_socket_address(¤t_tcp_socket->foreign_address, addr->sin6_family, addr->sin6_port, addr->sin6_flowinfo, &addr->sin6_addr);
|
||||
}
|
||||
|
||||
set_tcp_packet(current_tcp_packet, current_tcp_socket->local_address.sin6_port, current_tcp_socket->foreign_address.sin6_port,
|
||||
(flags == TCP_ACK ? current_tcp_socket->tcp_control.send_una-1 : current_tcp_socket->tcp_control.send_una),
|
||||
current_tcp_socket->tcp_control.rcv_nxt, 0, flags, current_tcp_socket->tcp_control.rcv_wnd, 0, 0);
|
||||
@ -404,6 +393,7 @@ void set_tcp_cb(tcp_cb *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd, uint32_
|
||||
int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
|
||||
{
|
||||
// Variables
|
||||
ipv6_addr_t src_addr;
|
||||
socket_internal_t *current_int_tcp_socket;
|
||||
socket_t *current_tcp_socket;
|
||||
msg_t msg_from_server;
|
||||
@ -422,6 +412,13 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
|
||||
|
||||
current_int_tcp_socket->recv_pid = thread_getpid();
|
||||
|
||||
// Local address information
|
||||
ipv6_get_saddr(&src_addr, &addr->sin6_addr);
|
||||
set_socket_address(¤t_tcp_socket->local_address, PF_INET6, HTONS(get_free_source_port(IPPROTO_TCP)), 0, &src_addr);
|
||||
|
||||
// Foreign address information
|
||||
set_socket_address(¤t_tcp_socket->foreign_address, addr->sin6_family, addr->sin6_port, addr->sin6_flowinfo, &addr->sin6_addr);
|
||||
|
||||
// TODO: random number should be generated like common BSD socket implementation with a periodic timer increasing it
|
||||
// TODO: Add TCP MSS option field
|
||||
// Fill lcoal TCP socket information
|
||||
@ -442,7 +439,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
|
||||
while (msg_from_server.type == TCP_RETRY)
|
||||
{
|
||||
// Send packet
|
||||
send_tcp(addr, current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_SYN, 0);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_SYN, 0);
|
||||
|
||||
// wait for SYN ACK or RETRY
|
||||
msg_receive(&msg_from_server);
|
||||
@ -471,7 +468,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen)
|
||||
current_tcp_socket->tcp_control.send_nxt++;
|
||||
|
||||
// Send packet
|
||||
send_tcp(NULL, current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -523,7 +520,7 @@ int32_t send(int s, void *msg, uint64_t len, int flags)
|
||||
|
||||
current_tcp_socket->tcp_control.send_nxt += sent_bytes;
|
||||
current_tcp_socket->tcp_control.send_wnd -= sent_bytes;
|
||||
send_tcp(NULL, current_tcp_socket, current_tcp_packet, temp_ipv6_header, 0, sent_bytes);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, 0, sent_bytes);
|
||||
|
||||
// Remember current time
|
||||
current_tcp_socket->tcp_control.last_packet_time = vtimer_now();
|
||||
@ -736,7 +733,7 @@ int close(int s)
|
||||
current_socket->socket_values.tcp_control.send_una++;
|
||||
current_socket->socket_values.tcp_control.state = FIN_WAIT_1;
|
||||
|
||||
send_tcp(NULL, ¤t_socket->socket_values, current_tcp_packet, temp_ipv6_header, TCP_FIN, 0);
|
||||
send_tcp(¤t_socket->socket_values, current_tcp_packet, temp_ipv6_header, TCP_FIN, 0);
|
||||
msg_receive(&m_recv);
|
||||
close_socket(current_socket);
|
||||
printf("Returning from Close()!\n");
|
||||
@ -894,7 +891,7 @@ int handle_new_tcp_connection(socket_internal_t *current_queued_int_socket, sock
|
||||
while (msg_recv_client_ack.type == TCP_RETRY)
|
||||
{
|
||||
// Send packet
|
||||
send_tcp(NULL, current_queued_socket, syn_ack_packet, temp_ipv6_header, TCP_SYN_ACK, 0);
|
||||
send_tcp(current_queued_socket, syn_ack_packet, temp_ipv6_header, TCP_SYN_ACK, 0);
|
||||
|
||||
// wait for ACK from Client
|
||||
msg_receive(&msg_recv_client_ack);
|
||||
|
||||
@ -196,6 +196,6 @@ void set_tcp_cb(tcp_cb *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd, uint32_
|
||||
void set_tcp_packet(tcp_hdr_t *tcp_hdr, uint16_t src_port, uint16_t dst_port, uint32_t seq_nr, uint32_t ack_nr,
|
||||
uint8_t dataOffset_reserved, uint8_t reserved_flags, uint16_t window, uint16_t checksum, uint16_t urg_pointer);
|
||||
int check_tcp_consistency(socket_t *current_tcp_socket, tcp_hdr_t *tcp_header);
|
||||
int send_tcp(sockaddr6_t *addr, socket_t *current_tcp_socket, tcp_hdr_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length);
|
||||
int send_tcp(socket_t *current_tcp_socket, tcp_hdr_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length);
|
||||
bool isTCPSocket(uint8_t s);
|
||||
#endif /* SOCKET_H_ */
|
||||
|
||||
@ -176,13 +176,13 @@ void handle_tcp_fin_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socke
|
||||
{
|
||||
current_tcp_socket->tcp_control.state = CLOSING;
|
||||
|
||||
send_tcp(NULL, current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
current_tcp_socket->tcp_control.state = LAST_ACK;
|
||||
|
||||
send_tcp(NULL, current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0);
|
||||
}
|
||||
net_msg_send(&m_send, tcp_socket->recv_pid, 0, CLOSE_CONN);
|
||||
}
|
||||
@ -200,7 +200,7 @@ void handle_tcp_fin_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, s
|
||||
set_tcp_cb(¤t_tcp_socket->tcp_control, tcp_header->seq_nr+1, current_tcp_socket->tcp_control.send_wnd, tcp_header->ack_nr,
|
||||
tcp_header->ack_nr, tcp_header->window);
|
||||
|
||||
send_tcp(NULL, current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0);
|
||||
|
||||
msg_send(&m_send, tcp_socket->send_pid, 0);
|
||||
msg_send(&m_send, tcp_socket->recv_pid, 0);
|
||||
@ -231,7 +231,7 @@ void handle_tcp_no_flags_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header,
|
||||
current_tcp_socket->tcp_control.send_wnd);
|
||||
// printf(" INFOS: %lu\n", current_tcp_socket->tcp_control.rcv_nxt);
|
||||
// Send packet
|
||||
send_tcp(NULL, current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0);
|
||||
send_tcp(current_tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -113,7 +113,7 @@ void tcp_general_timer(void)
|
||||
while (1)
|
||||
{
|
||||
check_sockets();
|
||||
vtimer_set_wakeup(&tcp_vtimer, interval, tcp_timer_pid);
|
||||
vtimer_set_wakeup(&tcp_vtimer, interval, thread_getpid());
|
||||
thread_sleep();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user