source: mainline/uspace/srv/net/tcp/segment.c@ 7c15d6f

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

Fix two TCP memory leaks.

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