diff --git a/sys/net/destiny/socket.c b/sys/net/destiny/socket.c index 5f8af82ff..439fd592b 100644 --- a/sys/net/destiny/socket.c +++ b/sys/net/destiny/socket.c @@ -293,7 +293,7 @@ socket_internal_t *get_udp_socket(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header bool is_four_touple(socket_internal_t *current_socket, ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header) { - return ((ipv6_get_addr_match(¤t_socket->socket_values.local_address.sin6_addr + return ((ipv6_get_addr_match(¤t_socket->socket_values.local_address.sin6_addr, &ipv6_header->destaddr) == 128) && (current_socket->socket_values.local_address.sin6_port == tcp_header->dst_port) && (ipv6_get_addr_match(¤t_socket->socket_values.foreign_address.sin6_addr, @@ -528,9 +528,9 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen) current_tcp_socket->tcp_control.rcv_irs = 0; mutex_lock(&global_sequence_clunter_mutex); - current_tcp_socket->tcp_control.send_is = global_sequence_counter; + current_tcp_socket->tcp_control.send_iss = global_sequence_counter; mutex_unlock(&global_sequence_clunter_mutex, 0); - current_tcp_socket->tcp_control.state = SYN_SENT; + current_tcp_socket->tcp_control.state = SYN_SENT; #ifdef TCP_HC /* Choosing random number Context ID */ diff --git a/sys/net/destiny/tcp.c b/sys/net/destiny/tcp.c index 8f82f641e..de7818d87 100644 --- a/sys/net/destiny/tcp.c +++ b/sys/net/destiny/tcp.c @@ -1,381 +1,381 @@ -/** - * Destiny TCP 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.c - * @brief TCP implementation - * @author Oliver Gesch - * @} - */ - - -#include -#include -#include -#include -#include - -#include "vtimer.h" -#include "tcp_timer.h" -#include "tcp_hc.h" -#include "tcp.h" -#include "in.h" -#include "socket.h" -#include "../net_help/net_help.h" -#include "../net_help/msg_help.h" -#include "../sixlowpan/sixlowpan.h" - -void printTCPHeader(tcp_hdr_t *tcp_header) -{ - printf("\nBEGIN: TCP HEADER\n"); - printf("ack_nr: %" PRIu32 "\n", tcp_header->ack_nr); - printf("checksum: %i\n", tcp_header->checksum); - printf("dataOffset_reserved: %i\n", tcp_header->dataOffset_reserved); - printf("dst_port: %i\n", tcp_header->dst_port); - printf("reserved_flags: %i\n", tcp_header->reserved_flags); - printf("seq_nr: %" PRIu32 "\n", tcp_header->seq_nr); - printf("src_port: %i\n", tcp_header->src_port); - printf("urg_pointer: %i\n", tcp_header->urg_pointer); - printf("window: %i\n", tcp_header->window); - printf("END: TCP HEADER\n"); -} - -void printArrayRange_tcp(uint8_t *udp_header, uint16_t len) -{ - int i = 0; - printf("-------------MEMORY-------------\n"); - - for(i = 0; i < len; i++) { - printf("%#x ", *(udp_header + i)); - } - - printf("-------------MEMORY-------------\n"); -} - -uint16_t tcp_csum(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header) -{ - uint16_t sum; - uint16_t len = ipv6_header->length; - - sum = len + IPPROTO_TCP; - sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t)); - sum = csum(sum, (uint8_t *)tcp_header, len); - return (sum == 0) ? 0xffff : HTONS(sum); -} - -uint8_t handle_payload(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket, uint8_t *payload) -{ - msg_t m_send_tcp, m_recv_tcp; - uint8_t tcp_payload_len = ipv6_header->length - TCP_HDR_LEN; - uint8_t acknowledged_bytes = 0; - - if(tcp_payload_len > tcp_socket->socket_values.tcp_control.rcv_wnd) { - mutex_lock(&tcp_socket->tcp_buffer_mutex); - memcpy(tcp_socket->tcp_input_buffer, payload, - tcp_socket->socket_values.tcp_control.rcv_wnd); - acknowledged_bytes = tcp_socket->socket_values.tcp_control.rcv_wnd; - tcp_socket->socket_values.tcp_control.rcv_wnd = 0; - tcp_socket->tcp_input_buffer_end = tcp_socket->tcp_input_buffer_end + - tcp_socket->socket_values.tcp_control.rcv_wnd; - mutex_unlock(&tcp_socket->tcp_buffer_mutex, 0); - } - else { - mutex_lock(&tcp_socket->tcp_buffer_mutex); - memcpy(tcp_socket->tcp_input_buffer, payload, tcp_payload_len); - tcp_socket->socket_values.tcp_control.rcv_wnd = - tcp_socket->socket_values.tcp_control.rcv_wnd - tcp_payload_len; - acknowledged_bytes = tcp_payload_len; - tcp_socket->tcp_input_buffer_end = tcp_socket->tcp_input_buffer_end + - tcp_payload_len; - mutex_unlock(&tcp_socket->tcp_buffer_mutex, 0); - } - - if(thread_getstatus(tcp_socket->recv_pid) == STATUS_RECEIVE_BLOCKED) { - net_msg_send_recv(&m_send_tcp, &m_recv_tcp, tcp_socket->recv_pid, UNDEFINED); - } - - return acknowledged_bytes; -} - -void handle_tcp_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket) -{ - msg_t m_recv_tcp, m_send_tcp; - uint8_t target_pid; - - if(tcp_socket->socket_values.tcp_control.state == LAST_ACK) { - target_pid = tcp_socket->recv_pid; - close_socket(tcp_socket); - msg_send(&m_send_tcp, target_pid, 0); - return; - } - else if(tcp_socket->socket_values.tcp_control.state == CLOSING) { - msg_send(&m_send_tcp, tcp_socket->recv_pid, 0); - msg_send(&m_send_tcp, tcp_socket->send_pid, 0); - return; - } - else if(getWaitingConnectionSocket(tcp_socket->socket_id, ipv6_header, - tcp_header) != NULL) { - m_send_tcp.content.ptr = (char *)tcp_header; - net_msg_send_recv(&m_send_tcp, &m_recv_tcp, tcp_socket->recv_pid, TCP_ACK); - return; - } - else if(tcp_socket->socket_values.tcp_control.state == ESTABLISHED) { - if(check_tcp_consistency(&tcp_socket->socket_values, tcp_header) == PACKET_OK) { - m_send_tcp.content.ptr = (char *)tcp_header; - net_msg_send(&m_send_tcp, tcp_socket->send_pid, 0, TCP_ACK); - return; - } - } - - printf("NO WAY OF HANDLING THIS ACK!\n"); -} - -void handle_tcp_rst_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket) -{ - /* TODO: Reset connection */ -} - -void handle_tcp_syn_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket) -{ - msg_t m_send_tcp; - - if(tcp_socket->socket_values.tcp_control.state == LISTEN) { - socket_internal_t *new_socket = new_tcp_queued_socket(ipv6_header, - tcp_header); - - if(new_socket != NULL) { -#ifdef TCP_HC - update_tcp_hc_context(true, new_socket, tcp_header); -#endif - /* notify socket function accept(..) that a new connection request - * has arrived. No need to wait for an answer because the server - * accept() function isnt reading from anything other than the - * queued sockets */ - net_msg_send(&m_send_tcp, tcp_socket->recv_pid, 0, TCP_SYN); - } - else { - printf("Dropped TCP SYN Message because an error occured while - requesting a new queued socket!\n"); - } - } - else { - printf("Dropped TCP SYN Message because socket was not in state LISTEN!"); - } -} - -void handle_tcp_syn_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket) -{ - msg_t m_send_tcp; - - if(tcp_socket->socket_values.tcp_control.state == SYN_SENT) { - m_send_tcp.content.ptr = (char *) tcp_header; - net_msg_send(&m_send_tcp, tcp_socket->recv_pid, 0, TCP_SYN_ACK); - } - else { - printf("Socket not in state SYN_SENT, dropping SYN-ACK-packet!"); - } -} - -void handle_tcp_fin_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket) -{ - msg_t m_send; - socket_t *current_tcp_socket = &tcp_socket->socket_values; - uint8_t send_buffer[BUFFER_SIZE]; - ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); - tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); - - 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); - -#ifdef TCP_HC - current_tcp_socket->tcp_control.tcp_context.hc_type = COMPRESSED_HEADER; -#endif - - if(current_tcp_socket->tcp_control.state == FIN_WAIT_1) { - current_tcp_socket->tcp_control.state = CLOSING; - - send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0); - } - else { - current_tcp_socket->tcp_control.state = LAST_ACK; - - send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0); - } - - net_msg_send(&m_send, tcp_socket->recv_pid, 0, CLOSE_CONN); -} - -void handle_tcp_fin_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket) -{ - msg_t m_send; - socket_t *current_tcp_socket = &tcp_socket->socket_values; - uint8_t send_buffer[BUFFER_SIZE]; - ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); - tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); - - current_tcp_socket->tcp_control.state = CLOSED; - - 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); - -#ifdef TCP_HC - current_tcp_socket->tcp_control.tcp_context.hc_type = COMPRESSED_HEADER; -#endif - - send_tcp(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); -} - -void handle_tcp_no_flags_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, - socket_internal_t *tcp_socket, uint8_t *payload) -{ - uint8_t tcp_payload_len = ipv6_header->length - TCP_HDR_LEN, read_bytes = 0; - socket_t *current_tcp_socket = &tcp_socket->socket_values; - uint8_t send_buffer[BUFFER_SIZE]; - ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); - tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); - - if(tcp_payload_len > 0) { - - if(check_tcp_consistency(current_tcp_socket, tcp_header) == PACKET_OK) { - read_bytes = handle_payload(ipv6_header, tcp_header, tcp_socket, payload); - - /* Refresh TCP status values */ - current_tcp_socket->tcp_control.state = ESTABLISHED; - - set_tcp_cb(¤t_tcp_socket->tcp_control, - tcp_header->seq_nr + read_bytes, - current_tcp_socket->tcp_control.rcv_wnd, - current_tcp_socket->tcp_control.send_nxt, - current_tcp_socket->tcp_control.send_una, - current_tcp_socket->tcp_control.send_wnd); - - /* Send packet */ - // block_continue_thread(); -#ifdef TCP_HC - current_tcp_socket->tcp_control.tcp_context.hc_type = COMPRESSED_HEADER; -#endif - send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); - } - /* ACK packet probably got lost */ - else { - // block_continue_thread(); -#ifdef TCP_HC - current_tcp_socket->tcp_control.tcp_context.hc_type = FULL_HEADER; -#endif - send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); - } - } -} - -void tcp_packet_handler(void) -{ - msg_t m_recv_ip, m_send_ip; - ipv6_hdr_t *ipv6_header; - tcp_hdr_t *tcp_header; - uint8_t *payload; - socket_internal_t *tcp_socket = NULL; - uint16_t chksum; - - while(1) { - msg_receive(&m_recv_ip); - - ipv6_header = ((ipv6_hdr_t *)m_recv_ip.content.ptr); - tcp_header = ((tcp_hdr_t *)(m_recv_ip.content.ptr + IPV6_HDR_LEN)); -#ifdef TCP_HC - tcp_socket = decompress_tcp_packet(ipv6_header); -#else - switch_tcp_packet_byte_order(tcp_header); - tcp_socket = get_tcp_socket(ipv6_header, tcp_header); -#endif - chksum = tcp_csum(ipv6_header, tcp_header); - - payload = (uint8_t *)(m_recv_ip.content.ptr + IPV6_HDR_LEN + - tcp_header->dataOffset_reserved * 4); - - if((chksum == 0xffff) && (tcp_socket != NULL)) { -#ifdef TCP_HC - update_tcp_hc_context(true, tcp_socket, tcp_header); -#endif - /* Remove reserved bits from tcp flags field */ - uint8_t tcp_flags = tcp_header->reserved_flags & REMOVE_RESERVED; - - switch(tcp_flags) { - case TCP_ACK: { - /* only ACK Bit set */ - handle_tcp_ack_packet(ipv6_header, tcp_header, tcp_socket); - break; - } - - case TCP_RST: { - printf("RST Bit set!\n"); - /* only RST Bit set */ - handle_tcp_rst_packet(ipv6_header, tcp_header, tcp_socket); - break; - } - - case TCP_SYN: { - /* only SYN Bit set, look for matching, listening socket - * and request new queued socket */ - printf("SYN Bit set!\n"); - handle_tcp_syn_packet(ipv6_header, tcp_header, tcp_socket); - break; - } - - case TCP_SYN_ACK: { - /* only SYN and ACK Bit set, complete three way handshake - * when socket in state SYN_SENT */ - handle_tcp_syn_ack_packet(ipv6_header, tcp_header, tcp_socket); - break; - } - - case TCP_FIN: { - printf("FIN Bit set!\n"); - /* only FIN Bit set */ - handle_tcp_fin_packet(ipv6_header, tcp_header, tcp_socket); - break; - } - - case TCP_FIN_ACK: { - printf("FIN ACK Bit set!\n"); - /* only FIN and ACK Bit set */ - handle_tcp_fin_ack_packet(ipv6_header, tcp_header, tcp_socket); - break; - } - - default: { - handle_tcp_no_flags_packet(ipv6_header, tcp_header, - tcp_socket, payload); - } - } - } - else { - printf("Wrong checksum (%x) or no corresponding socket found!\n", - chksum); - printArrayRange(((uint8_t *)ipv6_header), IPV6_HDR_LEN + - ipv6_header->length, "Incoming"); - print_tcp_status(INC_PACKET, ipv6_header, tcp_header, - &tcp_socket->socket_values); - } - - msg_reply(&m_recv_ip, &m_send_ip); - } -} +/** + * Destiny TCP 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.c + * @brief TCP implementation + * @author Oliver Gesch + * @} + */ + + +#include +#include +#include +#include +#include + +#include "vtimer.h" +#include "tcp_timer.h" +#include "tcp_hc.h" +#include "tcp.h" +#include "in.h" +#include "socket.h" +#include "../net_help/net_help.h" +#include "../net_help/msg_help.h" +#include "../sixlowpan/sixlowpan.h" + +void printTCPHeader(tcp_hdr_t *tcp_header) +{ + printf("\nBEGIN: TCP HEADER\n"); + printf("ack_nr: %" PRIu32 "\n", tcp_header->ack_nr); + printf("checksum: %i\n", tcp_header->checksum); + printf("dataOffset_reserved: %i\n", tcp_header->dataOffset_reserved); + printf("dst_port: %i\n", tcp_header->dst_port); + printf("reserved_flags: %i\n", tcp_header->reserved_flags); + printf("seq_nr: %" PRIu32 "\n", tcp_header->seq_nr); + printf("src_port: %i\n", tcp_header->src_port); + printf("urg_pointer: %i\n", tcp_header->urg_pointer); + printf("window: %i\n", tcp_header->window); + printf("END: TCP HEADER\n"); +} + +void printArrayRange_tcp(uint8_t *udp_header, uint16_t len) +{ + int i = 0; + printf("-------------MEMORY-------------\n"); + + for(i = 0; i < len; i++) { + printf("%#x ", *(udp_header + i)); + } + + printf("-------------MEMORY-------------\n"); +} + +uint16_t tcp_csum(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header) +{ + uint16_t sum; + uint16_t len = ipv6_header->length; + + sum = len + IPPROTO_TCP; + sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t)); + sum = csum(sum, (uint8_t *)tcp_header, len); + return (sum == 0) ? 0xffff : HTONS(sum); +} + +uint8_t handle_payload(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket, uint8_t *payload) +{ + msg_t m_send_tcp, m_recv_tcp; + uint8_t tcp_payload_len = ipv6_header->length - TCP_HDR_LEN; + uint8_t acknowledged_bytes = 0; + + if(tcp_payload_len > tcp_socket->socket_values.tcp_control.rcv_wnd) { + mutex_lock(&tcp_socket->tcp_buffer_mutex); + memcpy(tcp_socket->tcp_input_buffer, payload, + tcp_socket->socket_values.tcp_control.rcv_wnd); + acknowledged_bytes = tcp_socket->socket_values.tcp_control.rcv_wnd; + tcp_socket->socket_values.tcp_control.rcv_wnd = 0; + tcp_socket->tcp_input_buffer_end = tcp_socket->tcp_input_buffer_end + + tcp_socket->socket_values.tcp_control.rcv_wnd; + mutex_unlock(&tcp_socket->tcp_buffer_mutex, 0); + } + else { + mutex_lock(&tcp_socket->tcp_buffer_mutex); + memcpy(tcp_socket->tcp_input_buffer, payload, tcp_payload_len); + tcp_socket->socket_values.tcp_control.rcv_wnd = + tcp_socket->socket_values.tcp_control.rcv_wnd - tcp_payload_len; + acknowledged_bytes = tcp_payload_len; + tcp_socket->tcp_input_buffer_end = tcp_socket->tcp_input_buffer_end + + tcp_payload_len; + mutex_unlock(&tcp_socket->tcp_buffer_mutex, 0); + } + + if(thread_getstatus(tcp_socket->recv_pid) == STATUS_RECEIVE_BLOCKED) { + net_msg_send_recv(&m_send_tcp, &m_recv_tcp, tcp_socket->recv_pid, UNDEFINED); + } + + return acknowledged_bytes; +} + +void handle_tcp_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket) +{ + msg_t m_recv_tcp, m_send_tcp; + uint8_t target_pid; + + if(tcp_socket->socket_values.tcp_control.state == LAST_ACK) { + target_pid = tcp_socket->recv_pid; + close_socket(tcp_socket); + msg_send(&m_send_tcp, target_pid, 0); + return; + } + else if(tcp_socket->socket_values.tcp_control.state == CLOSING) { + msg_send(&m_send_tcp, tcp_socket->recv_pid, 0); + msg_send(&m_send_tcp, tcp_socket->send_pid, 0); + return; + } + else if(getWaitingConnectionSocket(tcp_socket->socket_id, ipv6_header, + tcp_header) != NULL) { + m_send_tcp.content.ptr = (char *)tcp_header; + net_msg_send_recv(&m_send_tcp, &m_recv_tcp, tcp_socket->recv_pid, TCP_ACK); + return; + } + else if(tcp_socket->socket_values.tcp_control.state == ESTABLISHED) { + if(check_tcp_consistency(&tcp_socket->socket_values, tcp_header) == PACKET_OK) { + m_send_tcp.content.ptr = (char *)tcp_header; + net_msg_send(&m_send_tcp, tcp_socket->send_pid, 0, TCP_ACK); + return; + } + } + + printf("NO WAY OF HANDLING THIS ACK!\n"); +} + +void handle_tcp_rst_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket) +{ + /* TODO: Reset connection */ +} + +void handle_tcp_syn_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket) +{ + msg_t m_send_tcp; + + if(tcp_socket->socket_values.tcp_control.state == LISTEN) { + socket_internal_t *new_socket = new_tcp_queued_socket(ipv6_header, + tcp_header); + + if(new_socket != NULL) { +#ifdef TCP_HC + update_tcp_hc_context(true, new_socket, tcp_header); +#endif + /* notify socket function accept(..) that a new connection request + * has arrived. No need to wait for an answer because the server + * accept() function isnt reading from anything other than the + * queued sockets */ + net_msg_send(&m_send_tcp, tcp_socket->recv_pid, 0, TCP_SYN); + } + else { + printf("Dropped TCP SYN Message because an error occured while "\ + "requesting a new queued socket!\n"); + } + } + else { + printf("Dropped TCP SYN Message because socket was not in state LISTEN!"); + } +} + +void handle_tcp_syn_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket) +{ + msg_t m_send_tcp; + + if(tcp_socket->socket_values.tcp_control.state == SYN_SENT) { + m_send_tcp.content.ptr = (char *) tcp_header; + net_msg_send(&m_send_tcp, tcp_socket->recv_pid, 0, TCP_SYN_ACK); + } + else { + printf("Socket not in state SYN_SENT, dropping SYN-ACK-packet!"); + } +} + +void handle_tcp_fin_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket) +{ + msg_t m_send; + socket_t *current_tcp_socket = &tcp_socket->socket_values; + uint8_t send_buffer[BUFFER_SIZE]; + ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); + tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); + + 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); + +#ifdef TCP_HC + current_tcp_socket->tcp_control.tcp_context.hc_type = COMPRESSED_HEADER; +#endif + + if(current_tcp_socket->tcp_control.state == FIN_WAIT_1) { + current_tcp_socket->tcp_control.state = CLOSING; + + send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0); + } + else { + current_tcp_socket->tcp_control.state = LAST_ACK; + + send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_FIN_ACK, 0); + } + + net_msg_send(&m_send, tcp_socket->recv_pid, 0, CLOSE_CONN); +} + +void handle_tcp_fin_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket) +{ + msg_t m_send; + socket_t *current_tcp_socket = &tcp_socket->socket_values; + uint8_t send_buffer[BUFFER_SIZE]; + ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); + tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); + + current_tcp_socket->tcp_control.state = CLOSED; + + 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); + +#ifdef TCP_HC + current_tcp_socket->tcp_control.tcp_context.hc_type = COMPRESSED_HEADER; +#endif + + send_tcp(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); +} + +void handle_tcp_no_flags_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, + socket_internal_t *tcp_socket, uint8_t *payload) +{ + uint8_t tcp_payload_len = ipv6_header->length - TCP_HDR_LEN, read_bytes = 0; + socket_t *current_tcp_socket = &tcp_socket->socket_values; + uint8_t send_buffer[BUFFER_SIZE]; + ipv6_hdr_t *temp_ipv6_header = ((ipv6_hdr_t *)(&send_buffer)); + tcp_hdr_t *current_tcp_packet = ((tcp_hdr_t *)(&send_buffer[IPV6_HDR_LEN])); + + if(tcp_payload_len > 0) { + + if(check_tcp_consistency(current_tcp_socket, tcp_header) == PACKET_OK) { + read_bytes = handle_payload(ipv6_header, tcp_header, tcp_socket, payload); + + /* Refresh TCP status values */ + current_tcp_socket->tcp_control.state = ESTABLISHED; + + set_tcp_cb(¤t_tcp_socket->tcp_control, + tcp_header->seq_nr + read_bytes, + current_tcp_socket->tcp_control.rcv_wnd, + current_tcp_socket->tcp_control.send_nxt, + current_tcp_socket->tcp_control.send_una, + current_tcp_socket->tcp_control.send_wnd); + + /* Send packet */ + // block_continue_thread(); +#ifdef TCP_HC + current_tcp_socket->tcp_control.tcp_context.hc_type = COMPRESSED_HEADER; +#endif + send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); + } + /* ACK packet probably got lost */ + else { + // block_continue_thread(); +#ifdef TCP_HC + current_tcp_socket->tcp_control.tcp_context.hc_type = FULL_HEADER; +#endif + send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); + } + } +} + +void tcp_packet_handler(void) +{ + msg_t m_recv_ip, m_send_ip; + ipv6_hdr_t *ipv6_header; + tcp_hdr_t *tcp_header; + uint8_t *payload; + socket_internal_t *tcp_socket = NULL; + uint16_t chksum; + + while(1) { + msg_receive(&m_recv_ip); + + ipv6_header = ((ipv6_hdr_t *)m_recv_ip.content.ptr); + tcp_header = ((tcp_hdr_t *)(m_recv_ip.content.ptr + IPV6_HDR_LEN)); +#ifdef TCP_HC + tcp_socket = decompress_tcp_packet(ipv6_header); +#else + switch_tcp_packet_byte_order(tcp_header); + tcp_socket = get_tcp_socket(ipv6_header, tcp_header); +#endif + chksum = tcp_csum(ipv6_header, tcp_header); + + payload = (uint8_t *)(m_recv_ip.content.ptr + IPV6_HDR_LEN + + tcp_header->dataOffset_reserved * 4); + + if((chksum == 0xffff) && (tcp_socket != NULL)) { +#ifdef TCP_HC + update_tcp_hc_context(true, tcp_socket, tcp_header); +#endif + /* Remove reserved bits from tcp flags field */ + uint8_t tcp_flags = tcp_header->reserved_flags & REMOVE_RESERVED; + + switch(tcp_flags) { + case TCP_ACK: { + /* only ACK Bit set */ + handle_tcp_ack_packet(ipv6_header, tcp_header, tcp_socket); + break; + } + + case TCP_RST: { + printf("RST Bit set!\n"); + /* only RST Bit set */ + handle_tcp_rst_packet(ipv6_header, tcp_header, tcp_socket); + break; + } + + case TCP_SYN: { + /* only SYN Bit set, look for matching, listening socket + * and request new queued socket */ + printf("SYN Bit set!\n"); + handle_tcp_syn_packet(ipv6_header, tcp_header, tcp_socket); + break; + } + + case TCP_SYN_ACK: { + /* only SYN and ACK Bit set, complete three way handshake + * when socket in state SYN_SENT */ + handle_tcp_syn_ack_packet(ipv6_header, tcp_header, tcp_socket); + break; + } + + case TCP_FIN: { + printf("FIN Bit set!\n"); + /* only FIN Bit set */ + handle_tcp_fin_packet(ipv6_header, tcp_header, tcp_socket); + break; + } + + case TCP_FIN_ACK: { + printf("FIN ACK Bit set!\n"); + /* only FIN and ACK Bit set */ + handle_tcp_fin_ack_packet(ipv6_header, tcp_header, tcp_socket); + break; + } + + default: { + handle_tcp_no_flags_packet(ipv6_header, tcp_header, + tcp_socket, payload); + } + } + } + else { + printf("Wrong checksum (%x) or no corresponding socket found!\n", + chksum); + printArrayRange(((uint8_t *)ipv6_header), IPV6_HDR_LEN + + ipv6_header->length, "Incoming"); + print_tcp_status(INC_PACKET, ipv6_header, tcp_header, + &tcp_socket->socket_values); + } + + msg_reply(&m_recv_ip, &m_send_ip); + } +} diff --git a/sys/net/sixlowpan/bordermultiplex.c b/sys/net/sixlowpan/bordermultiplex.c index e122ba09d..c54356ae0 100644 --- a/sys/net/sixlowpan/bordermultiplex.c +++ b/sys/net/sixlowpan/bordermultiplex.c @@ -47,7 +47,7 @@ void demultiplex(border_packet_t *packet, int len) switch(l3_header_buf->ethertype) { case(BORDER_ETHERTYPE_IPV6): { - struct ipv6_hdr_t *ipv6_buf = (struct ipv6_hdr_t *)(((unsigned char *)packet) + sizeof(border_l3_header_t)); + ipv6_hdr_t *ipv6_buf = (ipv6_hdr_t *)(((unsigned char *)packet) + sizeof(border_l3_header_t)); border_send_ipv6_over_lowpan(ipv6_buf, 1, 1); break; } @@ -102,7 +102,7 @@ void demultiplex(border_packet_t *packet, int len) } } -void multiplex_send_ipv6_over_uart(struct ipv6_hdr_t *packet) +void multiplex_send_ipv6_over_uart(ipv6_hdr_t *packet) { border_l3_header_t *serial_buf; diff --git a/sys/net/sixlowpan/bordermultiplex.h b/sys/net/sixlowpan/bordermultiplex.h index e13140994..8e8d2c448 100644 --- a/sys/net/sixlowpan/bordermultiplex.h +++ b/sys/net/sixlowpan/bordermultiplex.h @@ -83,7 +83,7 @@ typedef struct __attribute__((packed)) { #define BORDER_BUFFER_SIZE (sizeof (border_l3_header_t) + MTU) void demultiplex(border_packet_t *packet, int len); -void multiplex_send_ipv6_over_uart(struct ipv6_hdr_t *packet); +void multiplex_send_ipv6_over_uart(ipv6_hdr_t *packet); void multiplex_send_addr_over_uart(ipv6_addr_t *addr); int readpacket(uint8_t *packet_buf, size_t size); diff --git a/sys/net/sixlowpan/sixlowborder.c b/sys/net/sixlowpan/sixlowborder.c index d777a5f3f..bb5170390 100644 --- a/sys/net/sixlowpan/sixlowborder.c +++ b/sys/net/sixlowpan/sixlowborder.c @@ -159,7 +159,7 @@ uint8_t border_initialize(transceiver_type_t trans, ipv6_addr_t *border_router_a return SUCCESS; } -void border_send_ipv6_over_lowpan(struct ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag) +void border_send_ipv6_over_lowpan(ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag) { uint16_t offset = IPV6_HDR_LEN + HTONS(packet->length); @@ -175,11 +175,11 @@ void border_send_ipv6_over_lowpan(struct ipv6_hdr_t *packet, uint8_t aro_flag, u void border_process_lowpan(void) { msg_t m; - struct ipv6_hdr_t *ipv6_buf; + ipv6_hdr_t *ipv6_buf; while(1) { msg_receive(&m); - ipv6_buf = (struct ipv6_hdr_t *)m.content.ptr; + ipv6_buf = (ipv6_hdr_t *)m.content.ptr; if(ipv6_buf->nextheader == PROTO_NUM_ICMPV6) { struct icmpv6_hdr_t *icmp_buf = (struct icmpv6_hdr_t *)(((uint8_t *)ipv6_buf) + IPV6_HDR_LEN); diff --git a/sys/net/sixlowpan/sixlowborder.h b/sys/net/sixlowpan/sixlowborder.h index 6726fe681..4d0729e35 100644 --- a/sys/net/sixlowpan/sixlowborder.h +++ b/sys/net/sixlowpan/sixlowborder.h @@ -36,7 +36,7 @@ uint8_t *get_serial_out_buffer(int offset); uint8_t *get_serial_in_buffer(int offset); uint8_t border_initialize(transceiver_type_t trans, ipv6_addr_t *border_router_addr); -void border_send_ipv6_over_lowpan(struct ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag); +void border_send_ipv6_over_lowpan(ipv6_hdr_t *packet, uint8_t aro_flag, uint8_t sixco_flag); void border_process_lowpan(void); #endif /* SIXLOWBORDER_H*/ diff --git a/sys/net/sixlowpan/sixlowip.c b/sys/net/sixlowpan/sixlowip.c index 6480c06db..a6cec8168 100644 --- a/sys/net/sixlowpan/sixlowip.c +++ b/sys/net/sixlowpan/sixlowip.c @@ -36,7 +36,7 @@ uint8_t ip_send_buffer[BUFFER_SIZE]; uint8_t buffer[BUFFER_SIZE]; msg_t msg_queue[IP_PKT_RECV_BUF_SIZE]; -struct ipv6_hdr_t *ipv6_buf; +ipv6_hdr_t *ipv6_buf; struct icmpv6_hdr_t *icmp_buf; uint8_t ipv6_ext_hdr_len; uint8_t *nextheader; @@ -46,9 +46,9 @@ int udp_packet_handler_pid = 0; int tcp_packet_handler_pid = 0; int rpl_process_pid = 0; -struct ipv6_hdr_t *get_ipv6_buf_send(void) +ipv6_hdr_t *get_ipv6_buf_send(void) { - return ((struct ipv6_hdr_t *) & (ip_send_buffer[LL_HDR_LEN])); + return ((ipv6_hdr_t *) & (ip_send_buffer[LL_HDR_LEN])); } uint8_t *get_payload_buf_send(uint8_t ext_len) @@ -56,9 +56,9 @@ uint8_t *get_payload_buf_send(uint8_t ext_len) return &(ip_send_buffer[LLHDR_IPV6HDR_LEN + ext_len]); } -struct ipv6_hdr_t *get_ipv6_buf(void) +ipv6_hdr_t *get_ipv6_buf(void) { - return ((struct ipv6_hdr_t *) & (buffer[LL_HDR_LEN])); + return ((ipv6_hdr_t *)&(buffer[LL_HDR_LEN])); } struct icmpv6_hdr_t *get_icmpv6_buf(uint8_t ext_len) @@ -176,7 +176,7 @@ void ipv6_process(void) while(1) { msg_receive(&m_recv_lowpan); - ipv6_buf = (struct ipv6_hdr_t *) m_recv_lowpan.content.ptr; + ipv6_buf = (ipv6_hdr_t *)m_recv_lowpan.content.ptr; /* identifiy packet */ nextheader = &ipv6_buf->nextheader; diff --git a/sys/net/sixlowpan/sixlowip.h b/sys/net/sixlowpan/sixlowip.h index 2f945502a..88746a6cb 100644 --- a/sys/net/sixlowpan/sixlowip.h +++ b/sys/net/sixlowpan/sixlowip.h @@ -147,7 +147,7 @@ extern iface_t iface; /* function prototypes */ struct icmpv6_hdr_t *get_icmpv6_buf(uint8_t ext_len); -struct ipv6_hdr_t *get_ipv6_buf(void); +ipv6_hdr_t *get_ipv6_buf(void); uint8_t *get_payload_buf(uint8_t ext_len); uint8_t *get_payload_buf_send(uint8_t ext_len); diff --git a/sys/net/sixlowpan/sixlowmac.c b/sys/net/sixlowpan/sixlowmac.c index 7f17ca09e..477494e21 100644 --- a/sys/net/sixlowpan/sixlowmac.c +++ b/sys/net/sixlowpan/sixlowmac.c @@ -21,6 +21,8 @@ #include #include +#include + #include "sixlowmac.h" #include "sixlowip.h" #include "sixlownd.h" @@ -33,7 +35,7 @@ #include "transceiver.h" #include "vtimer.h" #include "ieee802154_frame.h" -#include "sys/net/net_help/net_help.h" +#include "net_help/net_help.h" char radio_stack_buffer[RADIO_STACK_SIZE]; msg_t msg_q[RADIO_RCV_BUF_SIZE]; @@ -223,7 +225,7 @@ void send_ieee802154_frame(ieee_802154_long_t *addr, uint8_t *payload, p.data = buf; msg_send_receive(&mesg, &transceiver_rsp, transceiver_pid); - printf("%s, %u: %u\n", __FILE__, __LINE__, transceiver_rsp.content.value); + printf("%s, %u: %"PRIu32"\n", __FILE__, __LINE__, transceiver_rsp.content.value); hwtimer_wait(5000); } diff --git a/sys/net/sixlowpan/sixlownd.c b/sys/net/sixlowpan/sixlownd.c index cf27152d4..93e90c3a5 100644 --- a/sys/net/sixlowpan/sixlownd.c +++ b/sys/net/sixlowpan/sixlownd.c @@ -58,19 +58,19 @@ plist_t plist[OPT_PI_LIST_LEN]; static uint8_t *llao; addr_list_t *addr_list_ptr; -static struct ipv6_hdr_t *ipv6_buf; +static ipv6_hdr_t *ipv6_buf; static struct icmpv6_hdr_t *icmp_buf; static struct rtr_adv_t *rtr_adv_buf; static struct nbr_sol_t *nbr_sol_buf; static struct nbr_adv_t *nbr_adv_buf; -static struct opt_buf_t *opt_buf; -static struct opt_stllao_t *opt_stllao_buf; -static struct opt_mtu_t *opt_mtu_buf; -static struct opt_abro_t *opt_abro_buf; -static struct opt_6co_hdr_t *opt_6co_hdr_buf; +static opt_buf_t *opt_buf; +static opt_stllao_t *opt_stllao_buf; +static opt_mtu_t *opt_mtu_buf; +static opt_abro_t *opt_abro_buf; +static opt_6co_hdr_t *opt_6co_hdr_buf; static uint8_t *opt_6co_prefix_buf; -static struct opt_pi_t *opt_pi_buf; -static struct opt_aro_t *opt_aro_buf; +static opt_pi_t *opt_pi_buf; +static opt_aro_t *opt_aro_buf; nbr_cache_t *nbr_entry; def_rtr_lst_t *def_rtr_entry; @@ -98,51 +98,51 @@ int min(int a, int b) static struct para_prob_t *get_para_prob_buf(uint8_t ext_len) { - return ((struct para_prob_t *) & (buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); + return ((struct para_prob_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); } static struct rtr_adv_t *get_rtr_adv_buf(uint8_t ext_len) { - return ((struct rtr_adv_t *) & (buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); + return ((struct rtr_adv_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); } static struct nbr_sol_t *get_nbr_sol_buf(uint8_t ext_len) { - return ((struct nbr_sol_t *) & (buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); + return ((struct nbr_sol_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); } static struct nbr_adv_t *get_nbr_adv_buf(uint8_t ext_len) { - return ((struct nbr_adv_t *) & (buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); + return ((struct nbr_adv_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len])); } -static struct opt_buf_t *get_opt_buf(uint8_t ext_len, uint8_t opt_len) +static opt_buf_t *get_opt_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_buf_t *) & (buffer[LLHDR_ICMPV6HDR_LEN + + return ((opt_buf_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } -static struct opt_stllao_t *get_opt_stllao_buf(uint8_t ext_len, uint8_t opt_len) +static opt_stllao_t *get_opt_stllao_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_stllao_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + + return ((opt_stllao_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } -static struct opt_mtu_t *get_opt_mtu_buf(uint8_t ext_len, uint8_t opt_len) +static opt_mtu_t *get_opt_mtu_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_mtu_t *) & (buffer[LLHDR_ICMPV6HDR_LEN + + return ((opt_mtu_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } -static struct opt_abro_t *get_opt_abro_buf(uint8_t ext_len, uint8_t opt_len) +static opt_abro_t *get_opt_abro_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_abro_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + + return ((opt_abro_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } -static struct opt_6co_hdr_t *get_opt_6co_hdr_buf(uint8_t ext_len, uint8_t opt_len) +static opt_6co_hdr_t *get_opt_6co_hdr_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_6co_hdr_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + + return ((opt_6co_hdr_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } @@ -151,15 +151,15 @@ static uint8_t *get_opt_6co_prefix_buf(uint8_t ext_len, uint8_t opt_len) return ((uint8_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } -static struct opt_pi_t *get_opt_pi_buf(uint8_t ext_len, uint8_t opt_len) +static opt_pi_t *get_opt_pi_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_pi_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + + return ((opt_pi_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } -static struct opt_aro_t *get_opt_aro_buf(uint8_t ext_len, uint8_t opt_len) +static opt_aro_t *get_opt_aro_buf(uint8_t ext_len, uint8_t opt_len) { - return ((struct opt_aro_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + + return ((opt_aro_t *)&(buffer[LLHDR_ICMPV6HDR_LEN + ext_len + opt_len])); } diff --git a/sys/net/sixlowpan/sixlowpan.h b/sys/net/sixlowpan/sixlowpan.h index 76eb95886..98034ee98 100644 --- a/sys/net/sixlowpan/sixlowpan.h +++ b/sys/net/sixlowpan/sixlowpan.h @@ -55,7 +55,7 @@ extern mutex_t lowpan_context_mutex; extern uint8_t static_route; extern uint16_t local_address; -typedef struct lowpan_context_t { +typedef struct { uint8_t num; ipv6_addr_t prefix; uint8_t length;