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

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

PDU encoding and decoding.

  • Property mode set to 100644
File size: 5.3 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 <mem.h>
38#include <stdlib.h>
39#include "segment.h"
40#include "seq_no.h"
41#include "tcp_type.h"
42
43/** Alocate new segment structure. */
44tcp_segment_t *tcp_segment_new(void)
45{
46 return calloc(1, sizeof(tcp_segment_t));
47}
48
49/** Delete segment. */
50void tcp_segment_delete(tcp_segment_t *seg)
51{
52 free(seg);
53}
54
55/** Create duplicate of segment.
56 *
57 * @param seg Segment
58 * @return Duplicate segment
59 */
60tcp_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
89/** Create a control-only segment.
90 *
91 * @return Segment
92 */
93tcp_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
107/** Create an RST segment.
108 *
109 * @param seg Segment we are replying to
110 * @return RST segment
111 */
112tcp_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
126/** Create a control segment.
127 *
128 * @return Segment
129 */
130tcp_segment_t *tcp_segment_make_data(tcp_control_t ctrl, void *data,
131 size_t size)
132{
133 tcp_segment_t *seg;
134
135 seg = tcp_segment_new();
136 if (seg == NULL)
137 return NULL;
138
139 seg->ctrl = ctrl;
140 seg->len = seq_no_control_len(ctrl) + size;
141
142 seg->dfptr = seg->data = malloc(size);
143 if (seg->dfptr == NULL) {
144 free(seg);
145 return NULL;
146 }
147
148 memcpy(seg->data, data, size);
149
150 return seg;
151}
152
153
154/** Trim segment from left and right by the specified amount.
155 *
156 * Trim any text or control to remove the specified amount of sequence
157 * numbers from the left (lower sequence numbers) and right side
158 * (higher sequence numbers) of the segment.
159 *
160 * @param seg Segment, will be modified in place
161 * @param left Amount of sequence numbers to trim from the left
162 * @param right Amount of sequence numbers to trim from the right
163 */
164void tcp_segment_trim(tcp_segment_t *seg, uint32_t left, uint32_t right)
165{
166 uint32_t t_size;
167
168 assert(left + right <= seg->len);
169
170 /* Special case, entire segment trimmed from left */
171 if (left == seg->len) {
172 seg->seq = seg->seq + seg->len;
173 seg->len = 0;
174 return;
175 }
176
177 /* Special case, entire segment trimmed from right */
178 if (right == seg->len) {
179 seg->len = 0;
180 return;
181 }
182
183 /* General case */
184
185 t_size = tcp_segment_text_size(seg);
186
187 if (left > 0 && (seg->ctrl & CTL_SYN) != 0) {
188 /* Trim SYN */
189 seg->ctrl &= ~CTL_SYN;
190 seg->seq++;
191 seg->len--;
192 left--;
193 }
194
195 if (right > 0 && (seg->ctrl & CTL_FIN) != 0) {
196 /* Trim FIN */
197 seg->ctrl &= ~CTL_FIN;
198 seg->len--;
199 right--;
200 }
201
202 if (left > 0 || right > 0) {
203 /* Trim segment text */
204 assert(left + right <= t_size);
205
206 seg->data += left;
207 seg->len -= left + right;
208 }
209}
210
211/** Copy out text data from segment.
212 *
213 * Data is copied from the beginning of the segment text up to @a size bytes.
214 * @a size must not be greater than the size of the segment text, but
215 * it can be less.
216 *
217 * @param seg Segment
218 * @param buf Destination buffer
219 * @param size Size of destination buffer
220 */
221void tcp_segment_text_copy(tcp_segment_t *seg, void *buf, size_t size)
222{
223 assert(size <= tcp_segment_text_size(seg));
224 memcpy(buf, seg->data, size);
225}
226
227/** Return number of bytes in segment text.
228 *
229 * @param seg Segment
230 * @return Number of bytes in segment text
231 */
232size_t tcp_segment_text_size(tcp_segment_t *seg)
233{
234 return seg->len - seq_no_control_len(seg->ctrl);
235}
236
237/**
238 * @}
239 */
Note: See TracBrowser for help on using the repository browser.