[21580dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup tcp
|
---|
[89e57cee] | 30 | * @{
|
---|
[21580dd] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
[89e57cee] | 34 | * TCP header definition.
|
---|
| 35 | * Based on the RFC 793.
|
---|
[21580dd] | 36 | */
|
---|
| 37 |
|
---|
[89e57cee] | 38 | #ifndef NET_TCP_HEADER_H_
|
---|
| 39 | #define NET_TCP_HEADER_H_
|
---|
[21580dd] | 40 |
|
---|
| 41 | #include <sys/types.h>
|
---|
| 42 |
|
---|
[89e57cee] | 43 | /** TCP header size in bytes. */
|
---|
| 44 | #define TCP_HEADER_SIZE sizeof(tcp_header_t)
|
---|
[ede63e4] | 45 |
|
---|
[21580dd] | 46 | /** Returns the actual TCP header length in bytes.
|
---|
[89e57cee] | 47 | * @param[in] header The TCP packet header.
|
---|
[21580dd] | 48 | */
|
---|
[0743493a] | 49 | #define TCP_HEADER_LENGTH(header) (GET_TCP_HEADER_LENGTH(header) * 4U)
|
---|
[21580dd] | 50 |
|
---|
| 51 | /** Returns the TCP header length.
|
---|
[89e57cee] | 52 | * @param[in] length The TCP header length in bytes.
|
---|
[21580dd] | 53 | */
|
---|
[89e57cee] | 54 | #define TCP_COMPUTE_HEADER_LENGTH(length) ((uint8_t) ((length) / 4U))
|
---|
[21580dd] | 55 |
|
---|
| 56 | /** Type definition of the transmission datagram header.
|
---|
[89e57cee] | 57 | * @see tcp_header
|
---|
[21580dd] | 58 | */
|
---|
[89e57cee] | 59 | typedef struct tcp_header tcp_header_t;
|
---|
[21580dd] | 60 |
|
---|
| 61 | /** Type definition of the transmission datagram header option.
|
---|
[89e57cee] | 62 | * @see tcp_option
|
---|
[21580dd] | 63 | */
|
---|
[89e57cee] | 64 | typedef struct tcp_option tcp_option_t;
|
---|
[21580dd] | 65 |
|
---|
[89e57cee] | 66 | /** Type definition of the Maximum segment size TCP option. */
|
---|
| 67 | typedef struct tcp_max_segment_size_option tcp_max_segment_size_option_t;
|
---|
[21580dd] | 68 |
|
---|
[89e57cee] | 69 | /** Transmission datagram header. */
|
---|
| 70 | struct tcp_header {
|
---|
[aadf01e] | 71 | uint16_t source_port;
|
---|
| 72 | uint16_t destination_port;
|
---|
| 73 | uint32_t sequence_number;
|
---|
| 74 | uint32_t acknowledgement_number;
|
---|
[89e57cee] | 75 |
|
---|
[0743493a] | 76 | uint8_t hlr; /* header length, reserved1 */
|
---|
| 77 |
|
---|
| 78 | #define GET_TCP_HEADER_LENGTH(header) \
|
---|
| 79 | (((header)->hlr & 0xf0) >> 4)
|
---|
| 80 | #define SET_TCP_HEADER_LENGTH(header, length) \
|
---|
| 81 | ((header)->hlr = \
|
---|
| 82 | ((length & 0x0f) << 4) | ((header)->hlr & 0x0f))
|
---|
| 83 |
|
---|
| 84 | #define GET_TCP_HEADER_RESERVED1(header) \
|
---|
| 85 | ((header)->hlr & 0x0f)
|
---|
| 86 | #define SET_TCP_HEADER_RESERVED1(header, reserved1) \
|
---|
| 87 | ((header)->hlr = \
|
---|
| 88 | (reserved1 & 0x0f) | ((header)->hlr & 0xf0))
|
---|
| 89 |
|
---|
| 90 | /* reserved2, urgent, acknowledge, push, reset, synchronize, finalize */
|
---|
| 91 | uint8_t ruaprsf;
|
---|
| 92 |
|
---|
| 93 | #define GET_TCP_HEADER_RESERVED2(header) \
|
---|
| 94 | (((header)->ruaprsf & 0xc0) >> 6)
|
---|
| 95 | #define SET_TCP_HEADER_RESERVED2(header, reserved2) \
|
---|
| 96 | ((header)->ruaprsf = \
|
---|
| 97 | ((reserved2 & 0x03) << 6) | ((header)->ruaprsf & 0x3f))
|
---|
| 98 |
|
---|
| 99 | #define GET_TCP_HEADER_URGENT(header) \
|
---|
| 100 | (((header)->ruaprsf & 0x20) >> 5)
|
---|
| 101 | #define SET_TCP_HEADER_URGENT(header, urgent) \
|
---|
| 102 | ((header)->ruaprsf = \
|
---|
| 103 | ((urgent & 0x01) << 5) | ((header)->ruaprsf & 0xdf))
|
---|
| 104 |
|
---|
| 105 | #define GET_TCP_HEADER_ACKNOWLEDGE(header) \
|
---|
| 106 | (((header)->ruaprsf & 0x10) >> 4)
|
---|
| 107 | #define SET_TCP_HEADER_ACKNOWLEDGE(header, acknowledge) \
|
---|
| 108 | ((header)->ruaprsf = \
|
---|
| 109 | ((acknowledge & 0x01) << 4) | ((header)->ruaprsf & 0xef))
|
---|
| 110 |
|
---|
| 111 | #define GET_TCP_HEADER_PUSH(header) \
|
---|
| 112 | (((header)->ruaprsf & 0x08) >> 3)
|
---|
| 113 | #define SET_TCP_HEADER_PUSH(header, push) \
|
---|
| 114 | ((header)->ruaprsf = \
|
---|
| 115 | ((push & 0x01) << 3) | ((header)->ruaprsf & 0xf7))
|
---|
| 116 |
|
---|
| 117 | #define GET_TCP_HEADER_RESET(header) \
|
---|
| 118 | (((header)->ruaprsf & 0x04) >> 2)
|
---|
| 119 | #define SET_TCP_HEADER_RESET(header, reset) \
|
---|
| 120 | ((header)->ruaprsf = \
|
---|
| 121 | ((reset & 0x01) << 2) | ((header)->ruaprsf & 0xfb))
|
---|
| 122 |
|
---|
| 123 | #define GET_TCP_HEADER_SYNCHRONIZE(header) \
|
---|
| 124 | (((header)->ruaprsf & 0x02) >> 1)
|
---|
| 125 | #define SET_TCP_HEADER_SYNCHRONIZE(header, synchronize) \
|
---|
| 126 | ((header)->ruaprsf = \
|
---|
| 127 | ((synchronize & 0x01) << 1) | ((header)->ruaprsf & 0xfd))
|
---|
| 128 |
|
---|
| 129 | #define GET_TCP_HEADER_FINALIZE(header) \
|
---|
| 130 | ((header)->ruaprsf & 0x01)
|
---|
| 131 | #define SET_TCP_HEADER_FINALIZE(header, finalize) \
|
---|
| 132 | ((header)->ruaprsf = \
|
---|
| 133 | (finalize & 0x01) | ((header)->ruaprsf & 0xfe))
|
---|
[89e57cee] | 134 |
|
---|
[aadf01e] | 135 | uint16_t window;
|
---|
| 136 | uint16_t checksum;
|
---|
| 137 | uint16_t urgent_pointer;
|
---|
[21580dd] | 138 | } __attribute__ ((packed));
|
---|
| 139 |
|
---|
[89e57cee] | 140 | /** Transmission datagram header option. */
|
---|
| 141 | struct tcp_option {
|
---|
| 142 | /** Option type. */
|
---|
[aadf01e] | 143 | uint8_t type;
|
---|
[89e57cee] | 144 | /** Option length. */
|
---|
[aadf01e] | 145 | uint8_t length;
|
---|
[21580dd] | 146 | };
|
---|
| 147 |
|
---|
[89e57cee] | 148 | /** Maximum segment size TCP option. */
|
---|
| 149 | struct tcp_max_segment_size_option {
|
---|
[21580dd] | 150 | /** TCP option.
|
---|
[89e57cee] | 151 | * @see TCPOPT_MAX_SEGMENT_SIZE
|
---|
| 152 | * @see TCPOPT_MAX_SEGMENT_SIZE_LENGTH
|
---|
[21580dd] | 153 | */
|
---|
[aadf01e] | 154 | tcp_option_t option;
|
---|
[89e57cee] | 155 |
|
---|
| 156 | /** Maximum segment size in bytes. */
|
---|
[aadf01e] | 157 | uint16_t max_segment_size;
|
---|
[21580dd] | 158 | } __attribute__ ((packed));
|
---|
| 159 |
|
---|
| 160 | #endif
|
---|
| 161 |
|
---|
| 162 | /** @}
|
---|
| 163 | */
|
---|