1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * TCP Protocol File: net_tcp.c
6 * This file contains a very simple TCP.
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
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 /* *********************************************************************
63 * Initialize a modulo buffer's pointers to the "empty" state
66 * buf - modulo buffer structure
70 ********************************************************************* */
72 void tmb_init(tcpmodbuf_t
*buf
)
79 /* *********************************************************************
82 * Move the "remove" pointer ahead by 'amt' without retrieving the
86 * buf - modulo buffer structure
87 * amt - number of bytes to move
91 ********************************************************************* */
94 void tmb_adjust(tcpmodbuf_t
*buf
,int 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.
106 * buf - modulo buffer structure
107 * size - size of data in modulo buffer
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
;
125 /* *********************************************************************
128 * Free memory associated with the modulo buffer
131 * buf - modulo buffer
135 ********************************************************************* */
137 void tmb_free(tcpmodbuf_t
*buf
)
139 if (buf
->tmb_buf
) KFREE(buf
->tmb_buf
);
144 /* *********************************************************************
145 * tmb_copyin(tmb,buf,len,update)
147 * Copy data into the modulo buffer at the 'add' pointer
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)
155 * number of bytes actually added to the buffer
156 ********************************************************************* */
158 int tmb_copyin(tcpmodbuf_t
*tmb
,uint8_t *buf
,int len
,int update
)
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
);
182 memcpy(tmb
->tmb_buf
,buf
,maxlen
);
186 newptr
= tmb
->tmb_addptr
+ l
;
190 tmb
->tmb_len
+= retlen
; /* this many more in the buffer */
191 tmb
->tmb_addptr
= newptr
;
197 /* *********************************************************************
198 * tmb_copyout(tmb,buf,len,update)
200 * Copy data out of the modulo buffer from the 'remove' pointer
203 * tmb - modulo buffer structure
204 * buf,len - buffer and length of buffer to copy out
205 * update - true to advance 'remove' pointer
208 * number of bytes actually removed from the buffer
209 ********************************************************************* */
211 int tmb_copyout(tcpmodbuf_t
*tmb
,uint8_t *buf
,int len
,int update
)
218 /* Compute how many bytes to return */
219 maxlen
= tmb
->tmb_len
;
220 if (maxlen
> len
) maxlen
= len
;
224 l
= tmb
->tmb_bufsize
- tmb
->tmb_remptr
;
225 if (l
> maxlen
) l
= maxlen
;
227 memcpy(buf
,tmb
->tmb_buf
+ tmb
->tmb_remptr
,l
);
232 memcpy(buf
,tmb
->tmb_buf
,maxlen
);
236 newptr
= tmb
->tmb_remptr
+ l
;
240 tmb
->tmb_len
-= retlen
;
241 tmb
->tmb_remptr
= newptr
;
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.
256 * tmb - modulo buffer structure
257 * buf,len - buffer and length of buffer to copy out
258 * offset - offset from remove pointer to start
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
)
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
;
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
);
292 memcpy(buf
,tmb
->tmb_buf
,maxlen
);