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