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

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

Document functions and files.

  • Property mode set to 100644
File size: 4.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 a control segment.
56 *
57 * @return Control segment
58 */
59tcp_segment_t *tcp_segment_make_ctrl(tcp_control_t ctrl)
60{
61 tcp_segment_t *seg;
62
63 seg = tcp_segment_new();
64 if (seg == NULL)
65 return NULL;
66
67 seg->ctrl = ctrl;
68 seg->len = seq_no_control_len(ctrl);
69
70 return seg;
71}
72
73/** Create an RST segment.
74 *
75 * @param seg Segment we are replying to
76 * @return RST segment
77 */
78tcp_segment_t *tcp_segment_make_rst(tcp_segment_t *seg)
79{
80 tcp_segment_t *rseg;
81
82 rseg = tcp_segment_new();
83 if (rseg == NULL)
84 return NULL;
85
86 rseg->ctrl = CTL_RST;
87 rseg->seq = seg->ack;
88
89 return rseg;
90}
91
92/** Trim segment from left and right by the specified amount.
93 *
94 * Trim any text or control to remove the specified amount of sequence
95 * numbers from the left (lower sequence numbers) and right side
96 * (higher sequence numbers) of the segment.
97 *
98 * @param seg Segment, will be modified in place
99 * @param left Amount of sequence numbers to trim from the left
100 * @param right Amount of sequence numbers to trim from the right
101 */
102void tcp_segment_trim(tcp_segment_t *seg, uint32_t left, uint32_t right)
103{
104 uint32_t t_size;
105
106 assert(left + right <= seg->len);
107
108 /* Special case, entire segment trimmed from left */
109 if (left == seg->len) {
110 seg->seq = seg->seq + seg->len;
111 seg->len = 0;
112 return;
113 }
114
115 /* Special case, entire segment trimmed from right */
116 if (right == seg->len) {
117 seg->len = 0;
118 return;
119 }
120
121 /* General case */
122
123 t_size = tcp_segment_text_size(seg);
124
125 if (left > 0 && (seg->ctrl & CTL_SYN) != 0) {
126 /* Trim SYN */
127 seg->ctrl &= ~CTL_SYN;
128 seg->seq++;
129 seg->len--;
130 left--;
131 }
132
133 if (right > 0 && (seg->ctrl & CTL_FIN) != 0) {
134 /* Trim FIN */
135 seg->ctrl &= ~CTL_FIN;
136 seg->len--;
137 right--;
138 }
139
140 if (left > 0 || right > 0) {
141 /* Trim segment text */
142 assert(left + right <= t_size);
143
144 seg->data += left;
145 seg->len -= left + right;
146 }
147}
148
149/** Copy out text data from segment.
150 *
151 * Data is copied from the beginning of the segment text up to @a size bytes.
152 * @a size must not be greater than the size of the segment text, but
153 * it can be less.
154 *
155 * @param seg Segment
156 * @param buf Destination buffer
157 * @param size Size of destination buffer
158 */
159void tcp_segment_text_copy(tcp_segment_t *seg, void *buf, size_t size)
160{
161 assert(size <= tcp_segment_text_size(seg));
162 memcpy(buf, seg->data, size);
163}
164
165/** Return number of bytes in segment text.
166 *
167 * @param seg Segment
168 * @return Number of bytes in segment text
169 */
170size_t tcp_segment_text_size(tcp_segment_t *seg)
171{
172 return seg->len - seq_no_control_len(seg->ctrl);
173}
174
175/**
176 * @}
177 */
Note: See TracBrowser for help on using the repository browser.