/*
 * Copyright (c) 2009 Lukas Mejdrech
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup tcp
 *  @{
 */

/** @file
 *  TCP header definition.
 *  Names according to the linux src/include/linux/tcp.h header file.
 */

#ifndef __NET_TCP_HEADER_H__
#define __NET_TCP_HEADER_H__

#include <sys/types.h>

/** Type definition of the transmission datagram header.
 *  @see tcp_header
 */
typedef struct tcp_header	tcp_header_t;

/** Type definition of the transmission datagram header pointer.
 *  @see tcp_header
 */
typedef tcp_header_t *		tcp_header_ref;

/** Transmission datagram header.
 */
struct tcp_header{
	/** The 16-bit source port number, used by the receiver to reply.
	 */
	uint16_t	source;
	/** The 16-bit destination port number.
	 */
	uint16_t	dest;
	/** The sequence number of the first data byte in this segment.
	 *  If the SYN control bit is set, the sequence number is the initial sequence number (n) and the first data byte is n+1.
	 */
	uint32_t	seq;
	/** If the ACK control bit is set, this field contains the value of the next sequence number that the receiver is expecting to receive.
	 */
	uint32_t	ack_seq;
#ifdef ARCH_IS_BIG_ENDIAN
	/** The number of 32-bit words in the TCP header.
	 *  It indicates where the data begins.
	 */
	uint8_t	doff:4;
	/** Four bits reserved for future use.
	 *  Must be zero.
	 */
	uint8_t	res1:4;
#else
	/** Four bits reserved for future use.
	 *  Must be zero.
	 */
	uint8_t	res1:4;
	/** The number of 32-bit words in the TCP header.
	 *  It indicates where the data begins.
	 */
	uint8_t	doff:4;
#endif
#ifdef ARCH_IS_BIG_ENDIAN
	/** Two bits reserved for future use.
	 *  Must be zero.
	 */
	uint8_t	res2:2;
	/** Indicates that the urgent pointer field is significant in this segment.
	 */
	uint8_t	urg:1;
	/** Indicates that the acknowledgment field is significant in this segment.
	 */
	uint8_t	ack:1;
	/** Push function.
	 */
	uint8_t	psh:1;
	/** Resets the connection.
	 */
	uint8_t	rst:1;
	/** Synchronizes the sequence numbers.
	 */
	uint8_t	syn:1;
	/** No more data from sender.
	 */
	uint8_t	fin:1;
#else
	/** No more data from sender.
	 */
	uint8_t	fin:1;
	/** Synchronizes the sequence numbers.
	 */
	uint8_t	syn:1;
	/** Resets the connection.
	 */
	uint8_t	rst:1;
	/** Push function.
	 */
	uint8_t	psh:1;
	/** Indicates that the acknowledgment field is significant in this segment.
	 */
	uint8_t	ack:1;
	/** Indicates that the urgent pointer field is significant in this segment.
	 */
	uint8_t	urg:1;
	/** Two bits reserved for future use.
	 *  Must be zero.
	 */
	uint8_t	res2:2;
#endif
	/** Used in ACK segments.
	 *  It specifies the number of data bytes, beginning with the one indicated in the acknowledgment number field that the receiver (the sender of this segment) is willing to accept.
	 */
	uint16_t	window;
	/** The 16-bit one's complement of the one's complement sum of all 16-bit words in a pseudo-header, the TCP header, and the TCP data.
	 *  While computing the checksum, the checksum field itself is considered zero.
	 *  The pseudo header conceptually prefixed to the TCP header contains the source address, the destination address, the protocol, and the TCP length.
	 *  This information gives protection against misrouted datagrams.
	 *  If the computed checksum is zero, it is transmitted as all ones (the equivalent in one's complement arithmetic).
	 */
	uint16_t	check;
	/** Points to the first data octet following the urgent data.
	 *  Only significant when the URG control bit is set.
	 */
	uint16_t	urg_ptr;
} __attribute__ ((packed));

#endif

/** @}
 */
