GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / net / net_tcpbuf.c
blobd07d849bd0b393abd5fc177a12073ee666d62cae
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * TCP Protocol File: net_tcp.c
5 *
6 * This file contains a very simple TCP.
7 *
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
9 *
10 *********************************************************************
12 * Copyright 2000,2001,2002,2003
13 * Broadcom Corporation. All rights reserved.
15 * This software is furnished under license and may be used and
16 * copied only in accordance with the following terms and
17 * conditions. Subject to these conditions, you may download,
18 * copy, install, use, modify and distribute modified or unmodified
19 * copies of this software in source and/or binary form. No title
20 * or ownership is transferred hereby.
22 * 1) Any source code used, modified or distributed must reproduce
23 * and retain this copyright notice and list of conditions
24 * as they appear in the source file.
26 * 2) No right is granted to use any trade name, trademark, or
27 * logo of Broadcom Corporation. The "Broadcom Corporation"
28 * name may not be used to endorse or promote products derived
29 * from this software without the prior written permission of
30 * Broadcom Corporation.
32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44 * THE POSSIBILITY OF SUCH DAMAGE.
45 ********************************************************************* */
48 #include "lib_types.h"
49 #include "lib_string.h"
50 #include "lib_queue.h"
51 #include "lib_malloc.h"
52 #include "lib_printf.h"
54 #include "cfe_error.h"
56 #include "net_tcpbuf.h"
60 /* *********************************************************************
61 * tmb_init(buf)
63 * Initialize a modulo buffer's pointers to the "empty" state
65 * Input parameters:
66 * buf - modulo buffer structure
68 * Return value:
69 * nothing
70 ********************************************************************* */
72 void tmb_init(tcpmodbuf_t *buf)
74 buf->tmb_addptr = 0;
75 buf->tmb_remptr = 0;
76 buf->tmb_len = 0;
79 /* *********************************************************************
80 * tmb_adjust(buf,amt)
82 * Move the "remove" pointer ahead by 'amt' without retrieving the
83 * data.
85 * Input parameters:
86 * buf - modulo buffer structure
87 * amt - number of bytes to move
89 * Return value:
90 * nothing
91 ********************************************************************* */
94 void tmb_adjust(tcpmodbuf_t *buf,int amt)
96 buf->tmb_len -= amt;
97 buf->tmb_remptr = (buf->tmb_remptr + amt) % buf->tmb_bufsize;
100 /* *********************************************************************
101 * tmb_alloc(buf,size)
103 * Allocate memory for the modulo buffer.
105 * Input parameters:
106 * buf - modulo buffer structure
107 * size - size of data in modulo buffer
109 * Return value:
110 * 0 if ok
111 * -1 if error
112 ********************************************************************* */
114 int tmb_alloc(tcpmodbuf_t *buf,int size)
116 buf->tmb_buf = KMALLOC(size,0);
117 if (!buf->tmb_buf) return -1;
118 buf->tmb_bufsize = size;
120 tmb_init(buf);
122 return 0;
125 /* *********************************************************************
126 * tmb_free(buf)
128 * Free memory associated with the modulo buffer
130 * Input parameters:
131 * buf - modulo buffer
133 * Return value:
134 * nothing
135 ********************************************************************* */
137 void tmb_free(tcpmodbuf_t *buf)
139 if (buf->tmb_buf) KFREE(buf->tmb_buf);
140 buf->tmb_buf = NULL;
144 /* *********************************************************************
145 * tmb_copyin(tmb,buf,len,update)
147 * Copy data into the modulo buffer at the 'add' pointer
149 * Input parameters:
150 * tmb - modulo buffer structure
151 * buf,len - buffer and length of buffer to copy in
152 * update - true to advance 'add' pointer (usually true for copyin)
154 * Return value:
155 * number of bytes actually added to the buffer
156 ********************************************************************* */
158 int tmb_copyin(tcpmodbuf_t *tmb,uint8_t *buf,int len,int update)
160 int maxlen;
161 int l;
162 int newptr;
163 int retlen;
165 if (len == 0) return 0;
167 /* Set 'maxlen' to the max # of bytes we will send now */
168 maxlen = tmb->tmb_bufsize - tmb->tmb_len;
169 if (maxlen > len) maxlen = len;
171 retlen = maxlen; /* we'll return this later. */
173 /* Copy the bytes into the buffer and deal with buffer wrap */
174 l = tmb->tmb_bufsize - tmb->tmb_addptr;
175 if (l > maxlen) l = maxlen;
177 memcpy(tmb->tmb_buf + tmb->tmb_addptr,buf,l);
178 maxlen -= l;
179 buf += l;
181 if (maxlen) {
182 memcpy(tmb->tmb_buf,buf,maxlen);
183 newptr = maxlen;
185 else {
186 newptr = tmb->tmb_addptr + l;
189 if (update) {
190 tmb->tmb_len += retlen; /* this many more in the buffer */
191 tmb->tmb_addptr = newptr;
194 return retlen;
197 /* *********************************************************************
198 * tmb_copyout(tmb,buf,len,update)
200 * Copy data out of the modulo buffer from the 'remove' pointer
202 * Input parameters:
203 * tmb - modulo buffer structure
204 * buf,len - buffer and length of buffer to copy out
205 * update - true to advance 'remove' pointer
207 * Return value:
208 * number of bytes actually removed from the buffer
209 ********************************************************************* */
211 int tmb_copyout(tcpmodbuf_t *tmb,uint8_t *buf,int len,int update)
213 int maxlen;
214 int l;
215 int newptr;
216 int retlen;
218 /* Compute how many bytes to return */
219 maxlen = tmb->tmb_len;
220 if (maxlen > len) maxlen = len;
222 retlen = maxlen;
224 l = tmb->tmb_bufsize - tmb->tmb_remptr;
225 if (l > maxlen) l = maxlen;
227 memcpy(buf,tmb->tmb_buf + tmb->tmb_remptr,l);
228 buf += l;
229 maxlen -= l;
231 if (maxlen) {
232 memcpy(buf,tmb->tmb_buf,maxlen);
233 newptr = maxlen;
235 else {
236 newptr = tmb->tmb_remptr + l;
239 if (update) {
240 tmb->tmb_len -= retlen;
241 tmb->tmb_remptr = newptr;
244 return retlen;
247 /* *********************************************************************
248 * tmb_copyout2(tmb,buf,offset,len)
250 * Copy data out of the modulo buffer from a specific offset from
251 * the remove pointer. This is done without updating the
252 * remove pointer - we use this to dig data out when segmenting
253 * packets for transmission.
255 * Input parameters:
256 * tmb - modulo buffer structure
257 * buf,len - buffer and length of buffer to copy out
258 * offset - offset from remove pointer to start
260 * Return value:
261 * number of bytes actually copied out of the buffer
262 ********************************************************************* */
264 int tmb_copyout2(tcpmodbuf_t *tmb,uint8_t *buf,int offset,int len)
266 int maxlen;
267 int l;
268 int retlen;
269 int remptr;
271 /* Compute how many bytes to return */
272 maxlen = tmb->tmb_len - offset;
274 /* if offset is beyond length, get out now. */
275 if (maxlen <= 0) return 0;
277 /* otherwise choose min(max_can_copy,max_to_copy) */
278 if (maxlen > len) maxlen = len;
279 retlen = maxlen;
281 /* Adjust remove pointer for offset */
282 remptr = (tmb->tmb_remptr + offset) % tmb->tmb_bufsize;
284 l = tmb->tmb_bufsize - remptr;
285 if (l > maxlen) l = maxlen;
287 memcpy(buf,tmb->tmb_buf + remptr,l);
288 buf += l;
289 maxlen -= l;
291 if (maxlen) {
292 memcpy(buf,tmb->tmb_buf,maxlen);
295 return retlen;