adamdunkels | a2f3c42 | 2004-09-12 20:24:53 +0000 | [diff] [blame] | 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 2004/09/12 20:24:56 adamdunkels Exp $ |
| 34 | */ |
adamdunkels | ee26fe3 | 2004-03-25 09:47:10 +0000 | [diff] [blame] | 35 | |
| 36 | #include "uip-split.h" |
| 37 | #include "uip.h" |
| 38 | #include "uip-fw.h" |
| 39 | #include "uip_arch.h" |
| 40 | |
adamdunkels | 39f446c | 2004-08-09 20:49:53 +0000 | [diff] [blame] | 41 | #include "tcpip.h" |
| 42 | |
adamdunkels | ee26fe3 | 2004-03-25 09:47:10 +0000 | [diff] [blame] | 43 | #define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) |
| 44 | |
| 45 | /*-----------------------------------------------------------------------------*/ |
| 46 | void |
| 47 | uip_split_output(void) |
| 48 | { |
| 49 | u16_t tcplen, len1, len2; |
| 50 | |
| 51 | /* We only try to split maximum sized TCP segments. */ |
| 52 | if(BUF->proto == UIP_PROTO_TCP && |
| 53 | uip_len == UIP_BUFSIZE - UIP_LLH_LEN) { |
| 54 | |
| 55 | tcplen = uip_len - UIP_TCPIP_HLEN; |
| 56 | /* Split the segment in two. If the original packet length was |
| 57 | odd, we make the second packet one byte larger. */ |
| 58 | len1 = len2 = tcplen / 2; |
| 59 | if(len1 + len2 < tcplen) { |
| 60 | ++len2; |
| 61 | } |
| 62 | |
| 63 | /* Create the first packet. This is done by altering the length |
| 64 | field of the IP header and updating the checksums. */ |
| 65 | uip_len = len1 + UIP_TCPIP_HLEN; |
| 66 | BUF->len[0] = (uip_len >> 8); |
| 67 | BUF->len[1] = (uip_len & 0xff); |
| 68 | |
| 69 | /* Recalculate the TCP checksum. */ |
| 70 | BUF->tcpchksum = 0; |
| 71 | BUF->tcpchksum = ~(uip_tcpchksum()); |
| 72 | |
| 73 | /* Recalculate the IP checksum. */ |
| 74 | BUF->ipchksum = 0; |
| 75 | BUF->ipchksum = ~(uip_ipchksum()); |
| 76 | |
| 77 | /* Transmit the first packet. */ |
adamdunkels | 39f446c | 2004-08-09 20:49:53 +0000 | [diff] [blame] | 78 | /* uip_fw_output();*/ |
| 79 | tcpip_output(); |
adamdunkels | ee26fe3 | 2004-03-25 09:47:10 +0000 | [diff] [blame] | 80 | |
| 81 | /* Now, create the second packet. To do this, it is not enough to |
| 82 | just alter the length field, but we must also update the TCP |
| 83 | sequence number and point the uip_appdata to a new place in |
| 84 | memory. This place is detemined by the length of the first |
| 85 | packet (len1). */ |
| 86 | uip_len = len2 + UIP_TCPIP_HLEN; |
| 87 | BUF->len[0] = (uip_len >> 8); |
| 88 | BUF->len[1] = (uip_len & 0xff); |
| 89 | |
| 90 | uip_appdata += len1; |
| 91 | |
| 92 | uip_add32(BUF->seqno, len1); |
| 93 | BUF->seqno[0] = uip_acc32[0]; |
| 94 | BUF->seqno[1] = uip_acc32[1]; |
| 95 | BUF->seqno[2] = uip_acc32[2]; |
| 96 | BUF->seqno[3] = uip_acc32[3]; |
| 97 | |
| 98 | /* Recalculate the TCP checksum. */ |
| 99 | BUF->tcpchksum = 0; |
| 100 | BUF->tcpchksum = ~(uip_tcpchksum()); |
| 101 | |
| 102 | /* Recalculate the IP checksum. */ |
| 103 | BUF->ipchksum = 0; |
| 104 | BUF->ipchksum = ~(uip_ipchksum()); |
| 105 | |
| 106 | /* Transmit the second packet. */ |
adamdunkels | 39f446c | 2004-08-09 20:49:53 +0000 | [diff] [blame] | 107 | /* uip_fw_output();*/ |
| 108 | tcpip_output(); |
adamdunkels | ee26fe3 | 2004-03-25 09:47:10 +0000 | [diff] [blame] | 109 | } else { |
adamdunkels | 39f446c | 2004-08-09 20:49:53 +0000 | [diff] [blame] | 110 | /* uip_fw_output();*/ |
| 111 | tcpip_output(); |
adamdunkels | ee26fe3 | 2004-03-25 09:47:10 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | } |
| 115 | /*-----------------------------------------------------------------------------*/ |