[c5808b41] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 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
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[032bbe7] | 34 | * @file Segment processing
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
[0093ab6] | 37 | #include <mem.h>
|
---|
[c5808b41] | 38 | #include <stdlib.h>
|
---|
| 39 | #include "segment.h"
|
---|
| 40 | #include "seq_no.h"
|
---|
| 41 | #include "tcp_type.h"
|
---|
| 42 |
|
---|
[032bbe7] | 43 | /** Alocate new segment structure. */
|
---|
[c5808b41] | 44 | tcp_segment_t *tcp_segment_new(void)
|
---|
| 45 | {
|
---|
| 46 | return calloc(1, sizeof(tcp_segment_t));
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[032bbe7] | 49 | /** Delete segment. */
|
---|
[4c55a64] | 50 | void tcp_segment_delete(tcp_segment_t *seg)
|
---|
| 51 | {
|
---|
| 52 | free(seg);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[6df418c4] | 55 | /** Create duplicate of segment.
|
---|
| 56 | *
|
---|
| 57 | * @param seg Segment
|
---|
| 58 | * @return Duplicate segment
|
---|
| 59 | */
|
---|
| 60 | tcp_segment_t *tcp_segment_dup(tcp_segment_t *seg)
|
---|
| 61 | {
|
---|
| 62 | tcp_segment_t *scopy;
|
---|
| 63 | size_t tsize;
|
---|
| 64 |
|
---|
| 65 | scopy = tcp_segment_new();
|
---|
| 66 | if (scopy == NULL)
|
---|
| 67 | return NULL;
|
---|
| 68 |
|
---|
| 69 | scopy->ctrl = seg->ctrl;
|
---|
| 70 | scopy->seq = seg->seq;
|
---|
| 71 | scopy->ack = seg->ack;
|
---|
| 72 | scopy->len = seg->len;
|
---|
| 73 | scopy->wnd = seg->wnd;
|
---|
| 74 | scopy->up = seg->up;
|
---|
| 75 |
|
---|
| 76 | tsize = tcp_segment_text_size(seg);
|
---|
| 77 | scopy->data = calloc(tsize, 1);
|
---|
| 78 | if (scopy->data == NULL) {
|
---|
| 79 | free(scopy);
|
---|
| 80 | return NULL;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | memcpy(scopy->data, seg->data, tsize);
|
---|
| 84 | scopy->dfptr = scopy->data;
|
---|
| 85 |
|
---|
| 86 | return scopy;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[32105348] | 89 | /** Create a control-only segment.
|
---|
[032bbe7] | 90 | *
|
---|
[32105348] | 91 | * @return Segment
|
---|
[032bbe7] | 92 | */
|
---|
[c5808b41] | 93 | tcp_segment_t *tcp_segment_make_ctrl(tcp_control_t ctrl)
|
---|
| 94 | {
|
---|
| 95 | tcp_segment_t *seg;
|
---|
| 96 |
|
---|
| 97 | seg = tcp_segment_new();
|
---|
| 98 | if (seg == NULL)
|
---|
| 99 | return NULL;
|
---|
| 100 |
|
---|
| 101 | seg->ctrl = ctrl;
|
---|
| 102 | seg->len = seq_no_control_len(ctrl);
|
---|
| 103 |
|
---|
| 104 | return seg;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[032bbe7] | 107 | /** Create an RST segment.
|
---|
| 108 | *
|
---|
| 109 | * @param seg Segment we are replying to
|
---|
| 110 | * @return RST segment
|
---|
| 111 | */
|
---|
[c5808b41] | 112 | tcp_segment_t *tcp_segment_make_rst(tcp_segment_t *seg)
|
---|
| 113 | {
|
---|
| 114 | tcp_segment_t *rseg;
|
---|
| 115 |
|
---|
| 116 | rseg = tcp_segment_new();
|
---|
| 117 | if (rseg == NULL)
|
---|
| 118 | return NULL;
|
---|
| 119 |
|
---|
| 120 | rseg->ctrl = CTL_RST;
|
---|
| 121 | rseg->seq = seg->ack;
|
---|
| 122 |
|
---|
| 123 | return rseg;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[32105348] | 126 | /** Create a control segment.
|
---|
| 127 | *
|
---|
| 128 | * @return Segment
|
---|
| 129 | */
|
---|
| 130 | tcp_segment_t *tcp_segment_make_data(tcp_control_t ctrl, void *data,
|
---|
| 131 | size_t size)
|
---|
| 132 | {
|
---|
| 133 | tcp_segment_t *seg;
|
---|
| 134 |
|
---|
[8c7a054] | 135 | assert(ctrl != 0 || size > 0);
|
---|
[32105348] | 136 |
|
---|
| 137 | seg = tcp_segment_new();
|
---|
| 138 | if (seg == NULL)
|
---|
| 139 | return NULL;
|
---|
| 140 |
|
---|
| 141 | seg->ctrl = ctrl;
|
---|
| 142 | seg->len = seq_no_control_len(ctrl) + size;
|
---|
| 143 |
|
---|
| 144 | seg->dfptr = seg->data = malloc(size);
|
---|
| 145 | if (seg->dfptr == NULL) {
|
---|
| 146 | free(seg);
|
---|
| 147 | return NULL;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | memcpy(seg->data, data, size);
|
---|
| 151 |
|
---|
| 152 | return seg;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
[032bbe7] | 156 | /** Trim segment from left and right by the specified amount.
|
---|
[0093ab6] | 157 | *
|
---|
[032bbe7] | 158 | * Trim any text or control to remove the specified amount of sequence
|
---|
| 159 | * numbers from the left (lower sequence numbers) and right side
|
---|
| 160 | * (higher sequence numbers) of the segment.
|
---|
| 161 | *
|
---|
| 162 | * @param seg Segment, will be modified in place
|
---|
| 163 | * @param left Amount of sequence numbers to trim from the left
|
---|
| 164 | * @param right Amount of sequence numbers to trim from the right
|
---|
[0093ab6] | 165 | */
|
---|
| 166 | void tcp_segment_trim(tcp_segment_t *seg, uint32_t left, uint32_t right)
|
---|
| 167 | {
|
---|
| 168 | uint32_t t_size;
|
---|
| 169 |
|
---|
| 170 | assert(left + right <= seg->len);
|
---|
| 171 |
|
---|
| 172 | /* Special case, entire segment trimmed from left */
|
---|
| 173 | if (left == seg->len) {
|
---|
| 174 | seg->seq = seg->seq + seg->len;
|
---|
| 175 | seg->len = 0;
|
---|
| 176 | return;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /* Special case, entire segment trimmed from right */
|
---|
| 180 | if (right == seg->len) {
|
---|
| 181 | seg->len = 0;
|
---|
| 182 | return;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /* General case */
|
---|
| 186 |
|
---|
| 187 | t_size = tcp_segment_text_size(seg);
|
---|
| 188 |
|
---|
| 189 | if (left > 0 && (seg->ctrl & CTL_SYN) != 0) {
|
---|
| 190 | /* Trim SYN */
|
---|
| 191 | seg->ctrl &= ~CTL_SYN;
|
---|
| 192 | seg->seq++;
|
---|
| 193 | seg->len--;
|
---|
| 194 | left--;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if (right > 0 && (seg->ctrl & CTL_FIN) != 0) {
|
---|
| 198 | /* Trim FIN */
|
---|
| 199 | seg->ctrl &= ~CTL_FIN;
|
---|
| 200 | seg->len--;
|
---|
| 201 | right--;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | if (left > 0 || right > 0) {
|
---|
| 205 | /* Trim segment text */
|
---|
| 206 | assert(left + right <= t_size);
|
---|
| 207 |
|
---|
| 208 | seg->data += left;
|
---|
| 209 | seg->len -= left + right;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /** Copy out text data from segment.
|
---|
| 214 | *
|
---|
[032bbe7] | 215 | * Data is copied from the beginning of the segment text up to @a size bytes.
|
---|
| 216 | * @a size must not be greater than the size of the segment text, but
|
---|
| 217 | * it can be less.
|
---|
| 218 | *
|
---|
| 219 | * @param seg Segment
|
---|
| 220 | * @param buf Destination buffer
|
---|
| 221 | * @param size Size of destination buffer
|
---|
[0093ab6] | 222 | */
|
---|
| 223 | void tcp_segment_text_copy(tcp_segment_t *seg, void *buf, size_t size)
|
---|
| 224 | {
|
---|
| 225 | assert(size <= tcp_segment_text_size(seg));
|
---|
| 226 | memcpy(buf, seg->data, size);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[032bbe7] | 229 | /** Return number of bytes in segment text.
|
---|
| 230 | *
|
---|
| 231 | * @param seg Segment
|
---|
| 232 | * @return Number of bytes in segment text
|
---|
| 233 | */
|
---|
[0093ab6] | 234 | size_t tcp_segment_text_size(tcp_segment_t *seg)
|
---|
[c5808b41] | 235 | {
|
---|
| 236 | return seg->len - seq_no_control_len(seg->ctrl);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
| 240 | * @}
|
---|
| 241 | */
|
---|