source: mainline/uspace/srv/net/tl/tcp/segment.c@ 5f9ecd3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5f9ecd3 was 6896409c, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Fix off-by-one bug in BIT_V.
Fix FIN being sent too early.
Fix FIN not being sent.
Fix seg→len from decoded PDU.
Add segment dumps.

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