diff --git a/projects/tlayer/main.c b/projects/tlayer/main.c index 662ae60cb5..d4cd2b7c02 100644 --- a/projects/tlayer/main.c +++ b/projects/tlayer/main.c @@ -52,7 +52,7 @@ char tcp_close_thread_stack[TCP_CLOSE_THREAD_STACK_SIZE]; typedef struct tcp_msg_t { int node_number; - char tcp_string_msg[50]; + char tcp_string_msg[80]; }tcp_message_t; tcp_message_t current_message; @@ -135,6 +135,7 @@ void init_tcp_server(void) sockaddr6_t stSockAddr; int read_bytes; char buff_msg[MAX_TCP_BUFFER]; + memset(buff_msg, 0, MAX_TCP_BUFFER); int SocketFD = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); if(-1 == SocketFD) @@ -183,7 +184,7 @@ void init_tcp_server(void) if (read_bytes > 0) { - printf("--- Message: %s ---\n", buff_msg); + printf("--- Read bytes: %i, Strlen(): %i, Message: %s ---\n", read_bytes, strlen(buff_msg), buff_msg); } } } @@ -231,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; @@ -251,7 +252,7 @@ void send_tcp_bulk(char *str) sscanf(str, "send_tcp_bulk %i %s", &count, msg_string); for (i = 0; i < count; i++) { - sprintf(command, "send_tcp %s%i", msg_string, i); + sprintf(command, "send_tcp %s%.5i", msg_string, i); send_tcp_msg(command); } } @@ -262,14 +263,14 @@ void send_tcp_bandwidth_test(char *str) uint32_t secs; int i = 0, count; - char command[61]; - char msg_string[] = "abcdefghijklmnopqrstuvwxyz0123456789!-=$%&/()"; + char command[80]; + char msg_string[] = "abcdefghijklmnopqrstuvwxyz0123456789!-=/%$"; sscanf(str, "tcp_bw %i", &count); start = vtimer_now(); for (i = 0; i < count; i++) { - sprintf(command, "send_tcp %s%i", msg_string, i); + sprintf(command, "send_tcp %s%.5i", msg_string, i); send_tcp_msg(command); } end = vtimer_now(); diff --git a/sys/net/destiny/destiny.c b/sys/net/destiny/destiny.c index 33b581aab8..d5f9389d01 100644 --- a/sys/net/destiny/destiny.c +++ b/sys/net/destiny/destiny.c @@ -16,8 +16,6 @@ #include "tcp_timer.h" #include "destiny.h" -#define TCP_HC - void init_transport_layer(void) { printf("Initializing transport layer packages.\n"); @@ -30,7 +28,9 @@ void init_transport_layer(void) // TCP srand(vtimer_now().microseconds); +#ifdef TCP_HC global_context_counter = rand(); +#endif global_sequence_counter = rand(); int tcp_thread_pid = thread_create(tcp_stack_buffer, TCP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, tcp_packet_handler, "tcp_packet_handler"); diff --git a/sys/net/destiny/destiny.h b/sys/net/destiny/destiny.h index 77a01f9265..7a793e488c 100644 --- a/sys/net/destiny/destiny.h +++ b/sys/net/destiny/destiny.h @@ -8,6 +8,8 @@ #ifndef DESTINY_H_ #define DESTINY_H_ +#define TCP_HC + void init_transport_layer(void); #endif /* DESTINY_H_ */ diff --git a/sys/net/destiny/socket.c b/sys/net/destiny/socket.c index 15a524950d..69c5318946 100644 --- a/sys/net/destiny/socket.c +++ b/sys/net/destiny/socket.c @@ -91,7 +91,10 @@ void print_tcp_status(int in_or_out, ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_hea printf("ACK: %lx, SEQ: %lx, Window: %x\n", tcp_header->ack_nr, tcp_header->seq_nr, tcp_header->window); printf("ACK: %lu, SEQ: %lu, Window: %u\n", tcp_header->ack_nr, tcp_header->seq_nr, tcp_header->window); print_tcp_flags(tcp_header); -// print_tcp_cb(&tcp_socket->tcp_control); + print_tcp_cb(&tcp_socket->tcp_control); +#ifdef TCP_HC + printf_tcp_context(&tcp_socket->tcp_control.tcp_context); +#endif } void print_socket(socket_t *current_socket) @@ -345,13 +348,13 @@ int check_tcp_consistency(socket_t *current_tcp_socket, tcp_hdr_t *tcp_header) // ACK of not yet sent byte, discard return ACK_NO_TOO_BIG; } - else if (tcp_header->ack_nr < (current_tcp_socket->tcp_control.send_una)) + else if (tcp_header->ack_nr <= (current_tcp_socket->tcp_control.send_una)) { // ACK of previous segments, maybe dropped? return ACK_NO_TOO_SMALL; } } - if ((current_tcp_socket->tcp_control.rcv_nxt > 0) && (tcp_header->seq_nr <= current_tcp_socket->tcp_control.rcv_nxt)) + else if ((current_tcp_socket->tcp_control.rcv_nxt > 0) && (tcp_header->seq_nr < current_tcp_socket->tcp_control.rcv_nxt)) { // segment repetition, maybe ACK got lost? return SEQ_NO_TOO_SMALL; @@ -359,6 +362,15 @@ int check_tcp_consistency(socket_t *current_tcp_socket, tcp_hdr_t *tcp_header) return PACKET_OK; } +void switch_tcp_packet_byte_order(tcp_hdr_t *current_tcp_packet) + { + current_tcp_packet->seq_nr = HTONL(current_tcp_packet->seq_nr); + current_tcp_packet->ack_nr = HTONL(current_tcp_packet->ack_nr); + current_tcp_packet->window = HTONS(current_tcp_packet->window); + current_tcp_packet->checksum = HTONS(current_tcp_packet->checksum); + current_tcp_packet->urg_pointer = HTONS(current_tcp_packet->urg_pointer); + } + int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length) { socket_t *current_tcp_socket = ¤t_socket->socket_values; @@ -373,6 +385,8 @@ int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet, i current_tcp_packet->checksum = ~tcp_csum(temp_ipv6_header, current_tcp_packet); + + #ifdef TCP_HC uint16_t compressed_size; @@ -381,9 +395,8 @@ int send_tcp(socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet, i return 1; #else - // send TCP SYN packet - sixlowpan_send(¤t_tcp_socket->foreign_address.sin6_addr, (uint8_t*)(current_tcp_packet), TCP_HDR_LEN+payload_length, IPPROTO_TCP, current_tcp_socket); - + switch_tcp_packet_byte_order(current_tcp_packet); + sixlowpan_send(¤t_tcp_socket->foreign_address.sin6_addr, (uint8_t*)(current_tcp_packet), TCP_HDR_LEN+payload_length, IPPROTO_TCP); return 1; #endif } @@ -441,6 +454,11 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen) mutex_lock(&global_context_counter_mutex); current_tcp_socket->tcp_control.tcp_context.context_id = global_context_counter; mutex_unlock(&global_context_counter_mutex, 0); + + // Remember TCP Context for possible TCP_RETRY + tcp_hc_context_t saved_tcp_context; + memcpy(&saved_tcp_context, ¤t_tcp_socket->tcp_control.tcp_context, sizeof(tcp_hc_context_t)); + printf("Context ID in Connect(): %u\n", current_tcp_socket->tcp_control.tcp_context.context_id); #endif @@ -461,8 +479,19 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen) msg_receive(&msg_from_server); if (msg_from_server.type == TCP_TIMEOUT) { +#ifdef TCP_HC + // We did not send anything successful so restore last context + memcpy(¤t_tcp_socket->tcp_control.tcp_context, &saved_tcp_context, sizeof(tcp_hc_context_t)); +#endif return -1; } +#ifdef TCP_HC + else if (msg_from_server.type == TCP_RETRY) + { + // We retry sending a packet so set everything to last values again + memcpy(¤t_tcp_socket->tcp_control.tcp_context, &saved_tcp_context, sizeof(tcp_hc_context_t)); + } +#endif } // Read packet content @@ -494,6 +523,7 @@ int connect(int socket, sockaddr6_t *addr, uint32_t addrlen) int32_t send(int s, void *msg, uint64_t len, int flags) { + printf("In Send(): %s\n", (char*) msg); // Variables msg_t recv_msg; int32_t sent_bytes = 0; @@ -540,6 +570,13 @@ 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; + +#ifdef TCP_HC + // Remember TCP Context for possible TCP_RETRY + tcp_hc_context_t saved_tcp_context; + memcpy(&saved_tcp_context, ¤t_tcp_socket->tcp_control.tcp_context, sizeof(tcp_hc_context_t)); +#endif + send_tcp(current_int_tcp_socket, current_tcp_packet, temp_ipv6_header, 0, sent_bytes); // Remember current time @@ -569,12 +606,19 @@ 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; +#ifdef TCP_HC + memcpy(¤t_tcp_socket->tcp_control.tcp_context, &saved_tcp_context, sizeof(tcp_hc_context_t)); + current_tcp_socket->tcp_control.tcp_context.hc_type = MOSTLY_COMPRESSED_HEADER; +#endif break; } case TCP_TIMEOUT: { current_tcp_socket->tcp_control.send_nxt -= sent_bytes; current_tcp_socket->tcp_control.send_wnd += sent_bytes; +#ifdef TCP_HC + memcpy(¤t_tcp_socket->tcp_control.tcp_context, &saved_tcp_context, sizeof(tcp_hc_context_t)); +#endif return -1; break; } @@ -585,8 +629,9 @@ int32_t send(int s, void *msg, uint64_t len, int flags) uint8_t read_from_socket(socket_internal_t *current_int_tcp_socket, void *buf, int len) { - if (len > current_int_tcp_socket->tcp_input_buffer_end) + if (len >= current_int_tcp_socket->tcp_input_buffer_end) { + printArrayRange(current_int_tcp_socket->tcp_input_buffer, current_int_tcp_socket->tcp_input_buffer_end, "Incoming_SOCKET"); mutex_lock(¤t_int_tcp_socket->tcp_buffer_mutex); uint8_t read_bytes = current_int_tcp_socket->tcp_input_buffer_end; memcpy(buf, current_int_tcp_socket->tcp_input_buffer, current_int_tcp_socket->tcp_input_buffer_end); @@ -896,10 +941,10 @@ int handle_new_tcp_connection(socket_internal_t *current_queued_int_socket, sock tcp_hdr_t *syn_ack_packet = ((tcp_hdr_t*)(&send_buffer[IPV6_HDR_LEN])); current_queued_int_socket->recv_pid = thread_getpid(); - +#ifdef TCP_HC memcpy(¤t_queued_int_socket->socket_values.tcp_control.tcp_context.context_id, &server_socket->socket_values.tcp_control.tcp_context.context_id, sizeof(server_socket->socket_values.tcp_control.tcp_context.context_id)); - +#endif // Remember current time current_queued_int_socket->socket_values.tcp_control.last_packet_time = vtimer_now(); @@ -939,8 +984,10 @@ int handle_new_tcp_connection(socket_internal_t *current_queued_int_socket, sock set_tcp_cb(¤t_queued_socket->tcp_control, tcp_header->seq_nr+1, current_queued_socket->tcp_control.rcv_wnd, tcp_header->ack_nr, tcp_header->ack_nr, tcp_header->window); +#ifdef TCP_HC // Copy TCP context information into new socket memset(&server_socket->socket_values.tcp_control.tcp_context, 0, sizeof(tcp_hc_context_t)); +#endif // Update connection status information current_queued_socket->tcp_control.state = ESTABLISHED; diff --git a/sys/net/destiny/socket.h b/sys/net/destiny/socket.h index b0837c9559..19ac0d96b6 100644 --- a/sys/net/destiny/socket.h +++ b/sys/net/destiny/socket.h @@ -138,6 +138,7 @@ typedef struct __attribute__((packed)) tcp_hc_con uint32_t seq_snd; // Last sent packet values uint32_t ack_snd; uint16_t wnd_snd; + uint8_t hc_type; } tcp_hc_context_t; typedef struct __attribute__((packed)) tcp_control_block @@ -172,15 +173,15 @@ typedef struct __attribute__ ((packed)) sock_t sockaddr6_t foreign_address; } socket_t; -typedef struct __attribute__ ((packed)) socket_in_t +typedef struct socket_in_t { uint8_t socket_id; uint8_t recv_pid; uint8_t send_pid; uint8_t tcp_input_buffer_end; - uint8_t tcp_input_buffer[MAX_TCP_BUFFER]; mutex_t tcp_buffer_mutex; socket_t socket_values; + uint8_t tcp_input_buffer[MAX_TCP_BUFFER]; } socket_internal_t; socket_internal_t sockets[MAX_SOCKETS]; @@ -213,6 +214,7 @@ void set_tcp_cb(tcp_cb_t *tcp_control, uint32_t rcv_nxt, uint16_t rcv_wnd, uint3 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); +void switch_tcp_packet_byte_order(tcp_hdr_t *current_tcp_packet); int send_tcp(socket_internal_t *current_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_ */ diff --git a/sys/net/destiny/tcp.c b/sys/net/destiny/tcp.c index 06ac7d9876..79b1aa0114 100644 --- a/sys/net/destiny/tcp.c +++ b/sys/net/destiny/tcp.c @@ -73,6 +73,8 @@ uint8_t handle_payload(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socket_in } else { + printf("Payload Length: %i\n", tcp_payload_len); + 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; @@ -116,8 +118,11 @@ void handle_tcp_ack_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socke } else if (tcp_socket->socket_values.tcp_control.state == ESTABLISHED) { - m_send_tcp.content.ptr = (char*)tcp_header; - net_msg_send(&m_send_tcp, tcp_socket->send_pid, 0, TCP_ACK); + 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"); @@ -136,7 +141,9 @@ void handle_tcp_syn_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socke 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); @@ -221,19 +228,29 @@ void handle_tcp_no_flags_packet(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, if (tcp_payload_len > 0) { - read_bytes = handle_payload(ipv6_header, tcp_header, tcp_socket, payload); + 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; + // 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 - send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); + 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(); + send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); + } + // ACK packet probably got lost + else + { + block_continue_thread(); + send_tcp(tcp_socket, current_tcp_packet, temp_ipv6_header, TCP_ACK, 0); + } } } @@ -254,19 +271,22 @@ void tcp_packet_handler (void) tcp_header = ((tcp_hdr_t*)(m_recv_ip.content.ptr + IPV6_HDR_LEN)); payload = (uint8_t*)(m_recv_ip.content.ptr + IPV6_HDR_LEN + TCP_HDR_LEN); - +// printArrayRange(((uint8_t *)tcp_header), ipv6_header->length, "Incoming_TCP"); #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); - print_tcp_status(INC_PACKET, ipv6_header, tcp_header, &tcp_socket->socket_values); + + if ((chksum == 0xffff) && (tcp_socket != NULL)) { #ifdef TCP_HC update_tcp_hc_context(true, tcp_socket, tcp_header); +// print_tcp_status(INC_PACKET, ipv6_header, tcp_header, &tcp_socket->socket_values); #endif // Remove reserved bits from tcp flags field uint8_t tcp_flags = tcp_header->reserved_flags & REMOVE_RESERVED; @@ -324,6 +344,8 @@ void tcp_packet_handler (void) 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/destiny/tcp_hc.c b/sys/net/destiny/tcp_hc.c index e3ef18943d..9f016ed9d5 100644 --- a/sys/net/destiny/tcp_hc.c +++ b/sys/net/destiny/tcp_hc.c @@ -60,6 +60,10 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current uint16_t packet_size = 0; if (tcp_cb->state != ESTABLISHED) +// if ((tcp_cb->state != ESTABLISHED) || +// ((tcp_cb->state == ESTABLISHED) && +// IS_TCP_ACK(((tcp_hdr_t*)current_tcp_packet)->reserved_flags) && +// (tcp_cb->tcp_context.ack_snd == ((tcp_hdr_t*)current_tcp_packet)->ack_nr))) { // draft-aayadi-6lowpan-tcphc-01: 5.1 Full header TCP segment. Establishing Connection @@ -70,13 +74,18 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current memset(current_tcp_packet, 0x01, 1); // Adding Context ID - memcpy(current_tcp_packet + 1, &tcp_context->context_id, 2); + uint16_t current_context = HTONS(tcp_context->context_id); + memcpy(current_tcp_packet + 1, ¤t_context, 2); // Return correct header length (+3) packet_size = TCP_HDR_LEN + 3 + payload_length; // Update the tcp context fields update_tcp_hc_context(false, current_socket, (tcp_hdr_t *)(current_tcp_packet+3)); + + // Convert TCP packet to network byte order + switch_tcp_packet_byte_order((tcp_hdr_t *)(current_tcp_packet+3)); + return packet_size; } else @@ -130,7 +139,7 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current tcp_hc_header |= 0x0800; // Copy first 16 less significant bits of sequence number into buffer - *((uint16_t *)current_tcp_packet) = (uint16_t)(full_tcp_header.seq_nr & 0x0000FFFF); + *((uint16_t *)current_tcp_packet) = HTONS((uint16_t)(full_tcp_header.seq_nr & 0x0000FFFF)); current_tcp_packet += 2; packet_size += 2; } @@ -141,7 +150,8 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current tcp_hc_header |= 0x0C00; // Copy all bits of sequence number into buffer - memcpy(current_tcp_packet, &full_tcp_header.seq_nr, 4); + uint32_t cur_seq_no = HTONL(full_tcp_header.seq_nr); + memcpy(current_tcp_packet, &cur_seq_no, 4); current_tcp_packet += 4; packet_size += 4; } @@ -149,6 +159,13 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current /*----------------------------------*/ /*| Acknowledgment number handling |*/ /*----------------------------------*/ + if ((IS_TCP_ACK(full_tcp_header.reserved_flags) && + (tcp_cb->tcp_context.ack_snd == full_tcp_header.ack_nr))) + { +// printf("TCP Context Ack Send: %lu, Current TCP Packet Ack: %lu\n", tcp_cb->tcp_context.ack_snd, ((tcp_hdr_t*)current_tcp_packet)->ack_nr); + tcp_context->ack_snd = tcp_context->seq_rcv; +// printf("TCP Context Ack Send: %lu, TCP Context Seq Recv: %lu\n", tcp_cb->tcp_context.ack_snd, tcp_context->seq_rcv); + } if (full_tcp_header.ack_nr == tcp_context->ack_snd) { // Nothing to do, Ack = (0|0) @@ -171,7 +188,7 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current tcp_hc_header |= 0x0200; // Copy first 16 less significant bits of acknowledgment number into buffer - *((uint16_t *)current_tcp_packet) = (uint16_t)(full_tcp_header.ack_nr & 0x0000FFFF); + *((uint16_t *)current_tcp_packet) = HTONS((uint16_t)(full_tcp_header.ack_nr & 0x0000FFFF)); current_tcp_packet += 2; packet_size += 2; } @@ -182,7 +199,8 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current tcp_hc_header |= 0x0300; // Copy all bits of acknowledgment number into buffer - memcpy(current_tcp_packet, &full_tcp_header.ack_nr, 4); + uint32_t cur_ack_nr = HTONL(full_tcp_header.ack_nr); + memcpy(current_tcp_packet, &cur_ack_nr, 4); current_tcp_packet += 4; packet_size += 4; } @@ -223,7 +241,8 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current tcp_hc_header |= 0x00C0; // Copy all bits of window size into buffer - memcpy(current_tcp_packet, &full_tcp_header.window, 2); + uint16_t cur_window = HTONS(full_tcp_header.window); + memcpy(current_tcp_packet, &cur_window, 2); current_tcp_packet += 2; packet_size += 2; } @@ -236,15 +255,18 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current } // Copy checksum into buffer - memcpy(current_tcp_packet, &full_tcp_header.checksum, 2); + uint16_t cur_chk_sum = HTONS(full_tcp_header.checksum); + memcpy(current_tcp_packet, &cur_chk_sum, 2); current_tcp_packet += 2; packet_size += 2; // Copy TCP_HC Bytes into buffer - memcpy(tcp_packet_begin, &tcp_hc_header, 2); + uint16_t cur_tcp_hc_header = HTONS(tcp_hc_header); + memcpy(tcp_packet_begin, &cur_tcp_hc_header, 2); // Copy TCP_HC Context ID into buffer - memcpy(tcp_packet_begin+2, &tcp_context->context_id, 2); + uint16_t cur_context_id = HTONS(tcp_context->context_id); + memcpy(tcp_packet_begin+2, &cur_context_id, 2); // Move payload to end of tcp header memmove(current_tcp_packet, tcp_packet_begin+TCP_HDR_LEN, payload_length); @@ -252,8 +274,8 @@ uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current // Adding TCP payload length to TCP_HC header length packet_size += payload_length; - print_tcp_status(OUT_PACKET, temp_ipv6_header, &full_tcp_header, current_tcp_socket); update_tcp_hc_context(false, current_socket, &full_tcp_header); +// print_tcp_status(OUT_PACKET, temp_ipv6_header, &full_tcp_header, current_tcp_socket); return packet_size; } } @@ -268,12 +290,14 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) // Full header TCP segment if (*(((uint8_t *)temp_ipv6_header)+IPV6_HDR_LEN) == 0x01) { + switch_tcp_packet_byte_order(((tcp_hdr_t *)(((uint8_t*)temp_ipv6_header)+IPV6_HDR_LEN+3))); current_socket = get_tcp_socket(temp_ipv6_header, ((tcp_hdr_t *)(((uint8_t*)temp_ipv6_header)+IPV6_HDR_LEN+3))); if (current_socket != NULL) { if (current_socket->socket_values.tcp_control.state == LISTEN) { memcpy(¤t_socket->socket_values.tcp_control.tcp_context.context_id, ((uint8_t *)temp_ipv6_header)+IPV6_HDR_LEN+1, 2); + current_socket->socket_values.tcp_control.tcp_context.context_id = NTOHS(current_socket->socket_values.tcp_control.tcp_context.context_id); } memmove(((uint8_t *)temp_ipv6_header)+IPV6_HDR_LEN, (((uint8_t*)temp_ipv6_header)+IPV6_HDR_LEN+3), temp_ipv6_header->length-3); temp_ipv6_header->length -= 3; @@ -296,9 +320,11 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) // Current context ID uint16_t current_context; memcpy(¤t_context, (packet_buffer+2), 2); + current_context = NTOHS(current_context); // Copy TCP_HC header into local variable memcpy(&tcp_hc_header, packet_buffer, 2); + tcp_hc_header = NTOHS(tcp_hc_header); // Setting pointer to first tcp_hc field packet_buffer += 4; @@ -337,7 +363,7 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) else if (BITSET(tcp_hc_header, 11) && !BITSET(tcp_hc_header, 10)) { // Seq = (1|0), copy 2 bytes of tcp_hc packet and 2 bytes from previous packet - full_tcp_header.seq_nr |= *((uint16_t *)packet_buffer); + full_tcp_header.seq_nr |= NTOHS(*((uint16_t *)packet_buffer)); full_tcp_header.seq_nr |= ((current_tcp_context->seq_rcv)&0xFFFF0000); packet_buffer += 2; packet_size += 2; @@ -347,6 +373,7 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) { // Seq = (1|1), copy 4 bytes of tcp_hc packet memcpy(&full_tcp_header.seq_nr, packet_buffer, 4); + full_tcp_header.seq_nr = NTOHL(full_tcp_header.seq_nr); packet_buffer += 4; packet_size += 4; } @@ -373,7 +400,7 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) else if (BITSET(tcp_hc_header, 9) && !BITSET(tcp_hc_header, 8)) { // Ack = (1|0), copy 2 bytes of tcp_hc packet and 2 bytes from previous packet - full_tcp_header.ack_nr |= *((uint16_t *)packet_buffer); + full_tcp_header.ack_nr |= NTOHS(*((uint16_t *)packet_buffer)); full_tcp_header.ack_nr |= ((current_tcp_context->ack_rcv)&0xFFFF0000); packet_buffer += 2; packet_size += 2; @@ -384,6 +411,7 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) { // Ack = (1|1), copy 4 bytes of tcp_hc packet memcpy(&full_tcp_header.ack_nr, packet_buffer, 4); + full_tcp_header.ack_nr = NTOHL(full_tcp_header.ack_nr); packet_buffer += 4; packet_size += 4; SET_TCP_ACK(full_tcp_header.reserved_flags); @@ -420,6 +448,7 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) { // Wnd = (1|1), copy 2 bytes of tcp_hc packet memcpy(&full_tcp_header.window, packet_buffer, 2); + full_tcp_header.window = NTOHS(full_tcp_header.window); packet_buffer += 2; packet_size += 2; } @@ -440,6 +469,7 @@ socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header) // Copy checksum into into tcp header memcpy(&full_tcp_header.checksum, packet_buffer, 2); + full_tcp_header.checksum = NTOHS(full_tcp_header.checksum); packet_buffer += 2; packet_size += 2; diff --git a/sys/net/destiny/tcp_hc.h b/sys/net/destiny/tcp_hc.h index ece16961c6..33eabe8d84 100644 --- a/sys/net/destiny/tcp_hc.h +++ b/sys/net/destiny/tcp_hc.h @@ -15,6 +15,11 @@ #include "socket.h" #ifdef TCP_HC + +#define FULL_HEADER 1 +#define MOSTLY_COMPRESSED_HEADER 2 +#define COMPRESSED_HEADER 3 + void update_tcp_hc_context(bool incoming, socket_internal_t *current_socket, tcp_hdr_t *current_tcp_packet); uint16_t compress_tcp_packet(socket_internal_t *current_socket, uint8_t *current_tcp_packet, ipv6_hdr_t *temp_ipv6_header, uint8_t flags, uint8_t payload_length); socket_internal_t *decompress_tcp_packet(ipv6_hdr_t *temp_ipv6_header); diff --git a/sys/net/destiny/tcp_timer.c b/sys/net/destiny/tcp_timer.c index b036553556..0a0699dec6 100644 --- a/sys/net/destiny/tcp_timer.c +++ b/sys/net/destiny/tcp_timer.c @@ -111,10 +111,11 @@ void inc_global_variables(void) mutex_lock(&global_sequence_clunter_mutex); global_sequence_counter += rand(); mutex_unlock(&global_sequence_clunter_mutex, 0); - +#ifdef TCP_HC mutex_lock(&global_context_counter_mutex); global_context_counter += rand(); mutex_unlock(&global_context_counter_mutex, 0); +#endif } void tcp_general_timer(void) diff --git a/sys/net/destiny/tcp_timer.h b/sys/net/destiny/tcp_timer.h index a697e2b2ce..f12eefbd4d 100644 --- a/sys/net/destiny/tcp_timer.h +++ b/sys/net/destiny/tcp_timer.h @@ -13,7 +13,7 @@ #define TCP_SYN_INITIAL_TIMEOUT 6*SECONDS #define TCP_SYN_TIMEOUT 24*SECONDS #define TCP_MAX_SYN_RETRIES 3 -#define TCP_ACK_TIMEOUT 3*SECONDS // still static, should be calculated via RTT +#define TCP_ACK_TIMEOUT 2*SECONDS // still static, should be calculated via RTT #define TCP_ACK_MAX_TIMEOUT 90*SECONDS #define TCP_NOT_DEFINED 0 diff --git a/sys/net/net_help/msg_help.c b/sys/net/net_help/msg_help.c index 6f287fe0f1..89d6fd518f 100644 --- a/sys/net/net_help/msg_help.c +++ b/sys/net/net_help/msg_help.c @@ -8,6 +8,17 @@ #include #include #include "msg_help.h" +#include "sys/net/destiny/tcp_timer.h" + +void block_continue_thread(void) + { +// msg_t recv_m; +// recv_m.type = TCP_NOT_DEFINED; +// while (recv_m.type != TCP_CONTINUE) +// { +// net_msg_receive(&recv_m); +// } + } int net_msg_receive(msg_t *m) { diff --git a/sys/net/net_help/msg_help.h b/sys/net/net_help/msg_help.h index 3912e585f3..f0c5aa180d 100644 --- a/sys/net/net_help/msg_help.h +++ b/sys/net/net_help/msg_help.h @@ -26,6 +26,7 @@ #define RETURNNOW 4000 +void block_continue_thread(void); int net_msg_receive(msg_t *m); int net_msg_reply(msg_t *m, msg_t *reply, uint16_t message); int net_msg_send(msg_t *m, unsigned int pid, bool block, uint16_t message); diff --git a/sys/net/net_help/net_help.c b/sys/net/net_help/net_help.c index b00c4fae63..5666d6e90d 100644 --- a/sys/net/net_help/net_help.c +++ b/sys/net/net_help/net_help.c @@ -11,6 +11,17 @@ #include "net_help.h" +void printArrayRange(uint8_t *array, uint16_t len, char *str) + { + int i = 0; + printf("-------------%s-------------\n", str); + for (i = 0; i < len; i++) + { + printf("%#x ", *(array+i)); + } + printf("\n--------------------------\n"); + } + uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len) { int count; diff --git a/sys/net/net_help/net_help.h b/sys/net/net_help/net_help.h index a915c45259..d1773f3d6f 100644 --- a/sys/net/net_help/net_help.h +++ b/sys/net/net_help/net_help.h @@ -21,5 +21,6 @@ #define CMP_IPV6_ADDR(a, b) (memcmp(a, b, 16)) uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len); +void printArrayRange(uint8_t *array, uint16_t len, char *str); #endif /* COMMON_H_ */ diff --git a/sys/net/sixlowpan/sixlowpan.c b/sys/net/sixlowpan/sixlowpan.c index bf69e54479..2afc717224 100644 --- a/sys/net/sixlowpan/sixlowpan.c +++ b/sys/net/sixlowpan/sixlowpan.c @@ -17,6 +17,7 @@ #include "transceiver.h" #include "ieee802154_frame.h" #include "sys/net/destiny/in.h" +#include "sys/net/net_help/net_help.h" uint16_t packet_length; uint8_t packet_dispatch; @@ -140,17 +141,6 @@ void lowpan_init(ieee_802154_long_t *addr, uint8_t *data){ tag++; } -void printArrayRange(uint8_t *array, uint16_t len, char *str) - { - int i = 0; - printf("-------------%s-------------\n", str); - for (i = 0; i < len; i++) - { - printf("%#x ", *(array+i)); - } - printf("\n--------------------------\n"); - } - void printLongLocalAddr(ieee_802154_long_t *saddr) { char text[9]; @@ -417,7 +407,7 @@ void check_timeout(void) { if ((cur_time - temp_buf->timestamp) >= LOWPAN_REAS_BUF_TIMEOUT) { - printf("TIMEOUT! cur_time: %li, temp_buf: %li\n", cur_time, temp_buf->timestamp); +// printf("TIMEOUT! cur_time: %li, temp_buf: %li\n", cur_time, temp_buf->timestamp); temp_buf = collect_garbage(temp_buf); } else