Contiki 2.5
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  * $Id: uip-split.c,v 1.3 2010/01/25 13:53:15 adamdunkels Exp $
34  */
35 
36 #include <string.h>
37 
38 #include "net/uip-split.h"
39 #include "net/uip.h"
40 #include "net/uip-fw.h"
41 #include "net/uip_arch.h"
42 
43 #include "net/tcpip.h"
44 
45 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
46 
47 /*-----------------------------------------------------------------------------*/
48 void
50 {
51 #if UIP_TCP
52  u16_t tcplen, len1, len2;
53 
54  /* We only try to split maximum sized TCP segments. */
55  if(BUF->proto == UIP_PROTO_TCP &&
56  uip_len == UIP_TCP_MSS + UIP_TCPIP_HLEN) {
57 
58  tcplen = uip_len - UIP_TCPIP_HLEN;
59  /* Split the segment in two. If the original packet length was
60  odd, we make the second packet one byte larger. */
61  len1 = len2 = tcplen / 2;
62  if(len1 + len2 < tcplen) {
63  ++len2;
64  }
65 
66  /* Create the first packet. This is done by altering the length
67  field of the IP header and updating the checksums. */
68  uip_len = len1 + UIP_TCPIP_HLEN;
69 #if UIP_CONF_IPV6
70  /* For IPv6, the IP length field does not include the IPv6 IP header
71  length. */
72  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
73  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
74 #else /* UIP_CONF_IPV6 */
75  BUF->len[0] = uip_len >> 8;
76  BUF->len[1] = uip_len & 0xff;
77 #endif /* UIP_CONF_IPV6 */
78 
79  /* Recalculate the TCP checksum. */
80  BUF->tcpchksum = 0;
81  BUF->tcpchksum = ~(uip_tcpchksum());
82 
83 #if !UIP_CONF_IPV6
84  /* Recalculate the IP checksum. */
85  BUF->ipchksum = 0;
86  BUF->ipchksum = ~(uip_ipchksum());
87 #endif /* UIP_CONF_IPV6 */
88 
89  /* Transmit the first packet. */
90  /* uip_fw_output();*/
91 #if UIP_CONF_IPV6
92  tcpip_ipv6_output();
93 #else
94  tcpip_output();
95 #endif /* UIP_CONF_IPV6 */
96 
97  /* Now, create the second packet. To do this, it is not enough to
98  just alter the length field, but we must also update the TCP
99  sequence number and point the uip_appdata to a new place in
100  memory. This place is detemined by the length of the first
101  packet (len1). */
102  uip_len = len2 + UIP_TCPIP_HLEN;
103 #if UIP_CONF_IPV6
104  /* For IPv6, the IP length field does not include the IPv6 IP header
105  length. */
106  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
107  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
108 #else /* UIP_CONF_IPV6 */
109  BUF->len[0] = uip_len >> 8;
110  BUF->len[1] = uip_len & 0xff;
111 #endif /* UIP_CONF_IPV6 */
112 
113  /* uip_appdata += len1;*/
114  memcpy(uip_appdata, (u8_t *)uip_appdata + len1, len2);
115 
116  uip_add32(BUF->seqno, len1);
117  BUF->seqno[0] = uip_acc32[0];
118  BUF->seqno[1] = uip_acc32[1];
119  BUF->seqno[2] = uip_acc32[2];
120  BUF->seqno[3] = uip_acc32[3];
121 
122  /* Recalculate the TCP checksum. */
123  BUF->tcpchksum = 0;
124  BUF->tcpchksum = ~(uip_tcpchksum());
125 
126 #if !UIP_CONF_IPV6
127  /* Recalculate the IP checksum. */
128  BUF->ipchksum = 0;
129  BUF->ipchksum = ~(uip_ipchksum());
130 #endif /* UIP_CONF_IPV6 */
131 
132  /* Transmit the second packet. */
133  /* uip_fw_output();*/
134 #if UIP_CONF_IPV6
135  tcpip_ipv6_output();
136 #else
137  tcpip_output();
138 #endif /* UIP_CONF_IPV6 */
139  return;
140  }
141 #endif /* UIP_TCP */
142 
143  /* uip_fw_output();*/
144 #if UIP_CONF_IPV6
145  tcpip_ipv6_output();
146 #else
147  tcpip_output();
148 #endif /* UIP_CONF_IPV6 */
149 }
150 
151 /*-----------------------------------------------------------------------------*/