Contiki 3.x
uip-split.c
1 /*
2  * Copyright (c) 2004, Swedish Institute of Computer Science.
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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include <string.h>
36 
37 #include "net/ip/uip-split.h"
38 #include "net/ip/uip.h"
39 #include "net/ipv4/uip-fw.h"
40 #include "net/ip/uip_arch.h"
41 
42 #include "net/ip/tcpip.h"
43 
44 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
45 
46 #ifdef UIP_SPLIT_CONF_SIZE
47 #define UIP_SPLIT_SIZE UIP_SPLIT_CONF_SIZE
48 #else /* UIP_SPLIT_CONF_SIZE */
49 #define UIP_SPLIT_SIZE UIP_TCP_MSS
50 #endif /* UIP_SPLIT_CONF_SIZE */
51 
52 /*-----------------------------------------------------------------------------*/
53 void
55 {
56 #if UIP_TCP
57  uint16_t tcplen, len1, len2;
58 
59  /* We only split TCP segments that are larger than or equal to
60  UIP_SPLIT_SIZE, which is configurable through
61  UIP_SPLIT_CONF_SIZE. */
62  if(BUF->proto == UIP_PROTO_TCP &&
63  uip_len >= UIP_SPLIT_SIZE + UIP_TCPIP_HLEN) {
64 
65  tcplen = uip_len - UIP_TCPIP_HLEN;
66  /* Split the segment in two. If the original packet length was
67  odd, we make the second packet one byte larger. */
68  len1 = len2 = tcplen / 2;
69  if(len1 + len2 < tcplen) {
70  ++len2;
71  }
72 
73  /* Create the first packet. This is done by altering the length
74  field of the IP header and updating the checksums. */
75  uip_len = len1 + UIP_TCPIP_HLEN;
76 #if UIP_CONF_IPV6
77  /* For IPv6, the IP length field does not include the IPv6 IP header
78  length. */
79  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
80  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
81 #else /* UIP_CONF_IPV6 */
82  BUF->len[0] = uip_len >> 8;
83  BUF->len[1] = uip_len & 0xff;
84 #endif /* UIP_CONF_IPV6 */
85 
86  /* Recalculate the TCP checksum. */
87  BUF->tcpchksum = 0;
88  BUF->tcpchksum = ~(uip_tcpchksum());
89 
90 #if !UIP_CONF_IPV6
91  /* Recalculate the IP checksum. */
92  BUF->ipchksum = 0;
93  BUF->ipchksum = ~(uip_ipchksum());
94 #endif /* UIP_CONF_IPV6 */
95 
96  /* Transmit the first packet. */
97  /* uip_fw_output();*/
98 #if UIP_CONF_IPV6
100 #else
101  tcpip_output();
102 #endif /* UIP_CONF_IPV6 */
103 
104  /* Now, create the second packet. To do this, it is not enough to
105  just alter the length field, but we must also update the TCP
106  sequence number and point the uip_appdata to a new place in
107  memory. This place is detemined by the length of the first
108  packet (len1). */
109  uip_len = len2 + UIP_TCPIP_HLEN;
110 #if UIP_CONF_IPV6
111  /* For IPv6, the IP length field does not include the IPv6 IP header
112  length. */
113  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
114  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
115 #else /* UIP_CONF_IPV6 */
116  BUF->len[0] = uip_len >> 8;
117  BUF->len[1] = uip_len & 0xff;
118 #endif /* UIP_CONF_IPV6 */
119 
120  /* uip_appdata += len1;*/
121  memcpy(uip_appdata, (uint8_t *)uip_appdata + len1, len2);
122 
123  uip_add32(BUF->seqno, len1);
124  BUF->seqno[0] = uip_acc32[0];
125  BUF->seqno[1] = uip_acc32[1];
126  BUF->seqno[2] = uip_acc32[2];
127  BUF->seqno[3] = uip_acc32[3];
128 
129  /* Recalculate the TCP checksum. */
130  BUF->tcpchksum = 0;
131  BUF->tcpchksum = ~(uip_tcpchksum());
132 
133 #if !UIP_CONF_IPV6
134  /* Recalculate the IP checksum. */
135  BUF->ipchksum = 0;
136  BUF->ipchksum = ~(uip_ipchksum());
137 #endif /* UIP_CONF_IPV6 */
138 
139  /* Transmit the second packet. */
140  /* uip_fw_output();*/
141 #if UIP_CONF_IPV6
143 #else
144  tcpip_output();
145 #endif /* UIP_CONF_IPV6 */
146  return;
147  }
148 #endif /* UIP_TCP */
149 
150  /* uip_fw_output();*/
151 #if UIP_CONF_IPV6
153 #else
154  tcpip_output();
155 #endif /* UIP_CONF_IPV6 */
156 }
157 
158 /*-----------------------------------------------------------------------------*/
uint8_t uip_acc32[4]
4-byte array used for the 32-bit sequence number calculations.
uint16_t uip_ipchksum(void)
Calculate the IP header checksum of the packet header in uip_buf.
Definition: uip6.c:343
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
void tcpip_ipv6_output(void)
This function does address resolution and then calls tcpip_output.
Definition: tcpip.c:540
uIP packet forwarding header file.
Header file for the uIP TCP/IP stack.
Header for the Contiki/uIP interface.
uint8_t tcpip_output(const uip_lladdr_t *a)
Output packet to layer 2 The eventual parameter is the MAC address of the destination.
Definition: tcpip.c:115
void uip_split_output(void)
Handle outgoing packets.
Definition: uip-split.c:54
uint16_t uip_tcpchksum(void)
Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
Definition: uip_arch.c:310
void uip_add32(uint8_t *op32, uint16_t op16)
Carry out a 32-bit addition.
Definition: uip_arch.c:45
Module for splitting outbound TCP segments in two to avoid the delayed ACK throughput degradation...
Declarations of architecture specific functions.
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74