#ifndef ETHERLIB_TCP_SEGMENT_H #define ETHERLIB_TCP_SEGMENT_H #include #include "../../packet_sieve.h" #define ETH_TCP_PACKET_CLASS (6) #define ETH_TCP_HEADER_SIZE (20) #define TCP_MAX_OPT_VAL_LEN (8) /** * TCP option kinds. */ typedef enum { TCP_OPT_KIND_EOL = 0, // End of Operation List Option TCP_OPT_KIND_NOP = 1, // No-Operation TCP_OPT_KIND_MSS = 2 // Maximum Segment Size } TcpOptionKind; /** * TCP options */ typedef struct TcpOption_ { uint8_t kind; ///< Kind of the specific option (~type) uint8_t size; ///< Size of value (NOT including 'kind' and 'length' fields) [=TCP opt length - 2] struct TcpOption_ * next; ///< Next option in the list uint8_t value[]; ///< Value } TcpOption; /** * Allocate new TCP option. * @param kind kind of TCP option. * @param size size of value * @return pointer to newly allocated option */ TcpOption * tcpopt_new(uint32_t kind, uint32_t size); /** * TCP segment flags */ typedef enum { TCP_FLAG_FIN = 0x01, TCP_FLAG_SYN = 0x02, TCP_FLAG_RESET = 0x04, TCP_FLAG_PUSH = 0x08, TCP_FLAG_ACK = 0x10, TCP_FLAG_URGENT = 0x20, TCP_FLAG_ECNECHO = 0x40, TCP_FLAG_CWR = 0x80, TCP_FLAG_NONCE = 0x100 } TcpFlag; /** * TCP connection block states. */ typedef enum { TCP_STATE_CLOSED = 0, TCP_STATE_LISTEN, TCP_STATE_SYN_RCVD, TCP_STATE_SYN_SENT, TCP_STATE_ESTAB, TCP_STATE_FIN_WAIT_1, TCP_STATE_FIN_WAIT_2, TCP_STATE_CLOSE_WAIT, TCP_STATE_CLOSING, TCP_STATE_LAST_ACK, TCP_STATE_TIME_WAIT } TcpConnectionState; /** * TCP segment properties. */ typedef struct { PcktPropsHeader uint16_t SourcePort; ///< Source port uint16_t DestinationPort; ///< Destination port uint32_t SequenceNumber; ///< TCP sequence number uint32_t AcknowledgementNumber; ///< TCP ack. number uint8_t DataOffset; ///< Data offset in 32-bits (4 bit-long field) uint16_t Flags; ///< TCP flags (9-bit, 8 + 4, but upper 3 is reserved) uint16_t Window; ///< TCP window uint16_t Checksum; ///< Checksum uint16_t UrgentPtr; ///< Urgent pointer TcpOption * options; ///< Linked list of TCP options } TcpProps; struct EthInterface_; /** * Parse raw TCP segments. * @param hdr pointer to the TCP packet header * @param size total packet size * @param pcktHdrLe pointer to property storage * @return 0 on success or -1 on failure */ int parse_tcp(const uint8_t *hdr, uint32_t size, PcktHeaderElement *pcktHdrLe, struct EthInterface_ *intf, PcktProcFnPassbackData *pb); /** * Insert TCP header. * @param hdr space where the header is to be inserted * @param headers linked list of header, top is always relevant */ void insert_tcp_header(uint8_t * hdr, const PcktHeaderElement * headers); /** * Get the flattened size of the option chain. * @param optLe list of TCP options * @return flattened size of options */ uint32_t tcp_get_options_size(TcpOption * optLe); /** * Get TCP option by filtering against kind field. * @param kind option kind to lookup * @param opt linked list of options * @return pointer to option OR NULL if not found */ TcpOption * tcp_get_option_by_kind(TcpOption * opt, uint16_t kind); #endif //ETHERLIB_TCP_SEGMENT_H