Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.
lwip/core/tcp_out.c@0:ec559500a63f, 2011-04-08 (annotated)
- Committer:
- andrewbonney
- Date:
- Fri Apr 08 14:39:41 2011 +0000
- Revision:
- 0:ec559500a63f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewbonney | 0:ec559500a63f | 1 | /** |
andrewbonney | 0:ec559500a63f | 2 | * @file |
andrewbonney | 0:ec559500a63f | 3 | * Transmission Control Protocol, outgoing traffic |
andrewbonney | 0:ec559500a63f | 4 | * |
andrewbonney | 0:ec559500a63f | 5 | * The output functions of TCP. |
andrewbonney | 0:ec559500a63f | 6 | * |
andrewbonney | 0:ec559500a63f | 7 | */ |
andrewbonney | 0:ec559500a63f | 8 | |
andrewbonney | 0:ec559500a63f | 9 | /* |
andrewbonney | 0:ec559500a63f | 10 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
andrewbonney | 0:ec559500a63f | 11 | * All rights reserved. |
andrewbonney | 0:ec559500a63f | 12 | * |
andrewbonney | 0:ec559500a63f | 13 | * Redistribution and use in source and binary forms, with or without modification, |
andrewbonney | 0:ec559500a63f | 14 | * are permitted provided that the following conditions are met: |
andrewbonney | 0:ec559500a63f | 15 | * |
andrewbonney | 0:ec559500a63f | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
andrewbonney | 0:ec559500a63f | 17 | * this list of conditions and the following disclaimer. |
andrewbonney | 0:ec559500a63f | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
andrewbonney | 0:ec559500a63f | 19 | * this list of conditions and the following disclaimer in the documentation |
andrewbonney | 0:ec559500a63f | 20 | * and/or other materials provided with the distribution. |
andrewbonney | 0:ec559500a63f | 21 | * 3. The name of the author may not be used to endorse or promote products |
andrewbonney | 0:ec559500a63f | 22 | * derived from this software without specific prior written permission. |
andrewbonney | 0:ec559500a63f | 23 | * |
andrewbonney | 0:ec559500a63f | 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
andrewbonney | 0:ec559500a63f | 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
andrewbonney | 0:ec559500a63f | 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
andrewbonney | 0:ec559500a63f | 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
andrewbonney | 0:ec559500a63f | 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
andrewbonney | 0:ec559500a63f | 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
andrewbonney | 0:ec559500a63f | 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
andrewbonney | 0:ec559500a63f | 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
andrewbonney | 0:ec559500a63f | 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
andrewbonney | 0:ec559500a63f | 33 | * OF SUCH DAMAGE. |
andrewbonney | 0:ec559500a63f | 34 | * |
andrewbonney | 0:ec559500a63f | 35 | * This file is part of the lwIP TCP/IP stack. |
andrewbonney | 0:ec559500a63f | 36 | * |
andrewbonney | 0:ec559500a63f | 37 | * Author: Adam Dunkels <adam@sics.se> |
andrewbonney | 0:ec559500a63f | 38 | * |
andrewbonney | 0:ec559500a63f | 39 | */ |
andrewbonney | 0:ec559500a63f | 40 | |
andrewbonney | 0:ec559500a63f | 41 | #include "lwip/opt.h" |
andrewbonney | 0:ec559500a63f | 42 | |
andrewbonney | 0:ec559500a63f | 43 | #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */ |
andrewbonney | 0:ec559500a63f | 44 | |
andrewbonney | 0:ec559500a63f | 45 | #include "lwip/tcp_impl.h" |
andrewbonney | 0:ec559500a63f | 46 | #include "lwip/def.h" |
andrewbonney | 0:ec559500a63f | 47 | #include "lwip/mem.h" |
andrewbonney | 0:ec559500a63f | 48 | #include "lwip/memp.h" |
andrewbonney | 0:ec559500a63f | 49 | #include "lwip/sys.h" |
andrewbonney | 0:ec559500a63f | 50 | #include "lwip/ip_addr.h" |
andrewbonney | 0:ec559500a63f | 51 | #include "lwip/netif.h" |
andrewbonney | 0:ec559500a63f | 52 | #include "lwip/inet_chksum.h" |
andrewbonney | 0:ec559500a63f | 53 | #include "lwip/stats.h" |
andrewbonney | 0:ec559500a63f | 54 | #include "lwip/snmp.h" |
andrewbonney | 0:ec559500a63f | 55 | |
andrewbonney | 0:ec559500a63f | 56 | #include <string.h> |
andrewbonney | 0:ec559500a63f | 57 | |
andrewbonney | 0:ec559500a63f | 58 | /* Define some copy-macros for checksum-on-copy so that the code looks |
andrewbonney | 0:ec559500a63f | 59 | nicer by preventing too many ifdef's. */ |
andrewbonney | 0:ec559500a63f | 60 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 61 | #define TCP_DATA_COPY(dst, src, len, seg) do { \ |
andrewbonney | 0:ec559500a63f | 62 | tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \ |
andrewbonney | 0:ec559500a63f | 63 | len, &seg->chksum, &seg->chksum_swapped); \ |
andrewbonney | 0:ec559500a63f | 64 | seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0) |
andrewbonney | 0:ec559500a63f | 65 | #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) \ |
andrewbonney | 0:ec559500a63f | 66 | tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped); |
andrewbonney | 0:ec559500a63f | 67 | #else /* TCP_CHECKSUM_ON_COPY*/ |
andrewbonney | 0:ec559500a63f | 68 | #define TCP_DATA_COPY(dst, src, len, seg) MEMCPY(dst, src, len) |
andrewbonney | 0:ec559500a63f | 69 | #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len) |
andrewbonney | 0:ec559500a63f | 70 | #endif /* TCP_CHECKSUM_ON_COPY*/ |
andrewbonney | 0:ec559500a63f | 71 | |
andrewbonney | 0:ec559500a63f | 72 | /** Define this to 1 for an extra check that the output checksum is valid |
andrewbonney | 0:ec559500a63f | 73 | * (usefule when the checksum is generated by the application, not the stack) */ |
andrewbonney | 0:ec559500a63f | 74 | #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK |
andrewbonney | 0:ec559500a63f | 75 | #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK 0 |
andrewbonney | 0:ec559500a63f | 76 | #endif |
andrewbonney | 0:ec559500a63f | 77 | |
andrewbonney | 0:ec559500a63f | 78 | /* Forward declarations.*/ |
andrewbonney | 0:ec559500a63f | 79 | static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb); |
andrewbonney | 0:ec559500a63f | 80 | |
andrewbonney | 0:ec559500a63f | 81 | /** Allocate a pbuf and create a tcphdr at p->payload, used for output |
andrewbonney | 0:ec559500a63f | 82 | * functions other than the default tcp_output -> tcp_output_segment |
andrewbonney | 0:ec559500a63f | 83 | * (e.g. tcp_send_empty_ack, etc.) |
andrewbonney | 0:ec559500a63f | 84 | * |
andrewbonney | 0:ec559500a63f | 85 | * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr) |
andrewbonney | 0:ec559500a63f | 86 | * @param optlen length of header-options |
andrewbonney | 0:ec559500a63f | 87 | * @param datalen length of tcp data to reserve in pbuf |
andrewbonney | 0:ec559500a63f | 88 | * @param seqno_be seqno in network byte order (big-endian) |
andrewbonney | 0:ec559500a63f | 89 | * @return pbuf with p->payload being the tcp_hdr |
andrewbonney | 0:ec559500a63f | 90 | */ |
andrewbonney | 0:ec559500a63f | 91 | static struct pbuf * |
andrewbonney | 0:ec559500a63f | 92 | tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen, |
andrewbonney | 0:ec559500a63f | 93 | u32_t seqno_be /* already in network byte order */) |
andrewbonney | 0:ec559500a63f | 94 | { |
andrewbonney | 0:ec559500a63f | 95 | struct tcp_hdr *tcphdr; |
andrewbonney | 0:ec559500a63f | 96 | struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM); |
andrewbonney | 0:ec559500a63f | 97 | if (p != NULL) { |
andrewbonney | 0:ec559500a63f | 98 | LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr", |
andrewbonney | 0:ec559500a63f | 99 | (p->len >= TCP_HLEN + optlen)); |
andrewbonney | 0:ec559500a63f | 100 | tcphdr = (struct tcp_hdr *)p->payload; |
andrewbonney | 0:ec559500a63f | 101 | tcphdr->src = htons(pcb->local_port); |
andrewbonney | 0:ec559500a63f | 102 | tcphdr->dest = htons(pcb->remote_port); |
andrewbonney | 0:ec559500a63f | 103 | tcphdr->seqno = seqno_be; |
andrewbonney | 0:ec559500a63f | 104 | tcphdr->ackno = htonl(pcb->rcv_nxt); |
andrewbonney | 0:ec559500a63f | 105 | TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK); |
andrewbonney | 0:ec559500a63f | 106 | tcphdr->wnd = htons(pcb->rcv_ann_wnd); |
andrewbonney | 0:ec559500a63f | 107 | tcphdr->chksum = 0; |
andrewbonney | 0:ec559500a63f | 108 | tcphdr->urgp = 0; |
andrewbonney | 0:ec559500a63f | 109 | |
andrewbonney | 0:ec559500a63f | 110 | /* If we're sending a packet, update the announced right window edge */ |
andrewbonney | 0:ec559500a63f | 111 | pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd; |
andrewbonney | 0:ec559500a63f | 112 | } |
andrewbonney | 0:ec559500a63f | 113 | return p; |
andrewbonney | 0:ec559500a63f | 114 | } |
andrewbonney | 0:ec559500a63f | 115 | |
andrewbonney | 0:ec559500a63f | 116 | /** |
andrewbonney | 0:ec559500a63f | 117 | * Called by tcp_close() to send a segment including FIN flag but not data. |
andrewbonney | 0:ec559500a63f | 118 | * |
andrewbonney | 0:ec559500a63f | 119 | * @param pcb the tcp_pcb over which to send a segment |
andrewbonney | 0:ec559500a63f | 120 | * @return ERR_OK if sent, another err_t otherwise |
andrewbonney | 0:ec559500a63f | 121 | */ |
andrewbonney | 0:ec559500a63f | 122 | err_t |
andrewbonney | 0:ec559500a63f | 123 | tcp_send_fin(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 124 | { |
andrewbonney | 0:ec559500a63f | 125 | /* first, try to add the fin to the last unsent segment */ |
andrewbonney | 0:ec559500a63f | 126 | if (pcb->unsent != NULL) { |
andrewbonney | 0:ec559500a63f | 127 | struct tcp_seg *last_unsent; |
andrewbonney | 0:ec559500a63f | 128 | for (last_unsent = pcb->unsent; last_unsent->next != NULL; |
andrewbonney | 0:ec559500a63f | 129 | last_unsent = last_unsent->next); |
andrewbonney | 0:ec559500a63f | 130 | |
andrewbonney | 0:ec559500a63f | 131 | if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) { |
andrewbonney | 0:ec559500a63f | 132 | /* no SYN/FIN/RST flag in the header, we can add the FIN flag */ |
andrewbonney | 0:ec559500a63f | 133 | TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN); |
andrewbonney | 0:ec559500a63f | 134 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 135 | } |
andrewbonney | 0:ec559500a63f | 136 | } |
andrewbonney | 0:ec559500a63f | 137 | /* no data, no length, flags, copy=1, no optdata */ |
andrewbonney | 0:ec559500a63f | 138 | return tcp_enqueue_flags(pcb, TCP_FIN); |
andrewbonney | 0:ec559500a63f | 139 | } |
andrewbonney | 0:ec559500a63f | 140 | |
andrewbonney | 0:ec559500a63f | 141 | /** |
andrewbonney | 0:ec559500a63f | 142 | * Create a TCP segment with prefilled header. |
andrewbonney | 0:ec559500a63f | 143 | * |
andrewbonney | 0:ec559500a63f | 144 | * Called by tcp_write and tcp_enqueue_flags. |
andrewbonney | 0:ec559500a63f | 145 | * |
andrewbonney | 0:ec559500a63f | 146 | * @param pcb Protocol control block for the TCP connection. |
andrewbonney | 0:ec559500a63f | 147 | * @param p pbuf that is used to hold the TCP header. |
andrewbonney | 0:ec559500a63f | 148 | * @param flags TCP flags for header. |
andrewbonney | 0:ec559500a63f | 149 | * @param seqno TCP sequence number of this packet |
andrewbonney | 0:ec559500a63f | 150 | * @param optflags options to include in TCP header |
andrewbonney | 0:ec559500a63f | 151 | * @return a new tcp_seg pointing to p, or NULL. |
andrewbonney | 0:ec559500a63f | 152 | * The TCP header is filled in except ackno and wnd. |
andrewbonney | 0:ec559500a63f | 153 | * p is freed on failure. |
andrewbonney | 0:ec559500a63f | 154 | */ |
andrewbonney | 0:ec559500a63f | 155 | static struct tcp_seg * |
andrewbonney | 0:ec559500a63f | 156 | tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags) |
andrewbonney | 0:ec559500a63f | 157 | { |
andrewbonney | 0:ec559500a63f | 158 | struct tcp_seg *seg; |
andrewbonney | 0:ec559500a63f | 159 | u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags); |
andrewbonney | 0:ec559500a63f | 160 | |
andrewbonney | 0:ec559500a63f | 161 | if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) { |
andrewbonney | 0:ec559500a63f | 162 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no memory.\n")); |
andrewbonney | 0:ec559500a63f | 163 | pbuf_free(p); |
andrewbonney | 0:ec559500a63f | 164 | return NULL; |
andrewbonney | 0:ec559500a63f | 165 | } |
andrewbonney | 0:ec559500a63f | 166 | seg->flags = optflags; |
andrewbonney | 0:ec559500a63f | 167 | seg->next = NULL; |
andrewbonney | 0:ec559500a63f | 168 | seg->p = p; |
andrewbonney | 0:ec559500a63f | 169 | seg->dataptr = p->payload; |
andrewbonney | 0:ec559500a63f | 170 | seg->len = p->tot_len - optlen; |
andrewbonney | 0:ec559500a63f | 171 | #if TCP_OVERSIZE_DBGCHECK |
andrewbonney | 0:ec559500a63f | 172 | seg->oversize_left = 0; |
andrewbonney | 0:ec559500a63f | 173 | #endif /* TCP_OVERSIZE_DBGCHECK */ |
andrewbonney | 0:ec559500a63f | 174 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 175 | seg->chksum = 0; |
andrewbonney | 0:ec559500a63f | 176 | seg->chksum_swapped = 0; |
andrewbonney | 0:ec559500a63f | 177 | /* check optflags */ |
andrewbonney | 0:ec559500a63f | 178 | LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED", |
andrewbonney | 0:ec559500a63f | 179 | (optflags & TF_SEG_DATA_CHECKSUMMED) == 0); |
andrewbonney | 0:ec559500a63f | 180 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 181 | |
andrewbonney | 0:ec559500a63f | 182 | /* build TCP header */ |
andrewbonney | 0:ec559500a63f | 183 | if (pbuf_header(p, TCP_HLEN)) { |
andrewbonney | 0:ec559500a63f | 184 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no room for TCP header in pbuf.\n")); |
andrewbonney | 0:ec559500a63f | 185 | TCP_STATS_INC(tcp.err); |
andrewbonney | 0:ec559500a63f | 186 | tcp_seg_free(seg); |
andrewbonney | 0:ec559500a63f | 187 | return NULL; |
andrewbonney | 0:ec559500a63f | 188 | } |
andrewbonney | 0:ec559500a63f | 189 | seg->tcphdr = (struct tcp_hdr *)seg->p->payload; |
andrewbonney | 0:ec559500a63f | 190 | seg->tcphdr->src = htons(pcb->local_port); |
andrewbonney | 0:ec559500a63f | 191 | seg->tcphdr->dest = htons(pcb->remote_port); |
andrewbonney | 0:ec559500a63f | 192 | seg->tcphdr->seqno = htonl(seqno); |
andrewbonney | 0:ec559500a63f | 193 | /* ackno is set in tcp_output */ |
andrewbonney | 0:ec559500a63f | 194 | TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags); |
andrewbonney | 0:ec559500a63f | 195 | /* wnd and chksum are set in tcp_output */ |
andrewbonney | 0:ec559500a63f | 196 | seg->tcphdr->urgp = 0; |
andrewbonney | 0:ec559500a63f | 197 | return seg; |
andrewbonney | 0:ec559500a63f | 198 | } |
andrewbonney | 0:ec559500a63f | 199 | |
andrewbonney | 0:ec559500a63f | 200 | /** |
andrewbonney | 0:ec559500a63f | 201 | * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end. |
andrewbonney | 0:ec559500a63f | 202 | * |
andrewbonney | 0:ec559500a63f | 203 | * This function is like pbuf_alloc(layer, length, PBUF_RAM) except |
andrewbonney | 0:ec559500a63f | 204 | * there may be extra bytes available at the end. |
andrewbonney | 0:ec559500a63f | 205 | * |
andrewbonney | 0:ec559500a63f | 206 | * @param layer flag to define header size. |
andrewbonney | 0:ec559500a63f | 207 | * @param length size of the pbuf's payload. |
andrewbonney | 0:ec559500a63f | 208 | * @param max_length maximum usable size of payload+oversize. |
andrewbonney | 0:ec559500a63f | 209 | * @param oversize pointer to a u16_t that will receive the number of usable tail bytes. |
andrewbonney | 0:ec559500a63f | 210 | * @param pcb The TCP connection that willo enqueue the pbuf. |
andrewbonney | 0:ec559500a63f | 211 | * @param apiflags API flags given to tcp_write. |
andrewbonney | 0:ec559500a63f | 212 | * @param first_seg true when this pbuf will be used in the first enqueued segment. |
andrewbonney | 0:ec559500a63f | 213 | * @param |
andrewbonney | 0:ec559500a63f | 214 | */ |
andrewbonney | 0:ec559500a63f | 215 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 216 | static struct pbuf * |
andrewbonney | 0:ec559500a63f | 217 | tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length, |
andrewbonney | 0:ec559500a63f | 218 | u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags, |
andrewbonney | 0:ec559500a63f | 219 | u8_t first_seg) |
andrewbonney | 0:ec559500a63f | 220 | { |
andrewbonney | 0:ec559500a63f | 221 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 222 | u16_t alloc = length; |
andrewbonney | 0:ec559500a63f | 223 | |
andrewbonney | 0:ec559500a63f | 224 | #if LWIP_NETIF_TX_SINGLE_PBUF |
andrewbonney | 0:ec559500a63f | 225 | LWIP_UNUSED_ARG(max_length); |
andrewbonney | 0:ec559500a63f | 226 | LWIP_UNUSED_ARG(pcb); |
andrewbonney | 0:ec559500a63f | 227 | LWIP_UNUSED_ARG(apiflags); |
andrewbonney | 0:ec559500a63f | 228 | LWIP_UNUSED_ARG(first_seg); |
andrewbonney | 0:ec559500a63f | 229 | /* always create MSS-sized pbufs */ |
andrewbonney | 0:ec559500a63f | 230 | alloc = TCP_MSS; |
andrewbonney | 0:ec559500a63f | 231 | #else /* LWIP_NETIF_TX_SINGLE_PBUF */ |
andrewbonney | 0:ec559500a63f | 232 | if (length < max_length) { |
andrewbonney | 0:ec559500a63f | 233 | /* Should we allocate an oversized pbuf, or just the minimum |
andrewbonney | 0:ec559500a63f | 234 | * length required? If tcp_write is going to be called again |
andrewbonney | 0:ec559500a63f | 235 | * before this segment is transmitted, we want the oversized |
andrewbonney | 0:ec559500a63f | 236 | * buffer. If the segment will be transmitted immediately, we can |
andrewbonney | 0:ec559500a63f | 237 | * save memory by allocating only length. We use a simple |
andrewbonney | 0:ec559500a63f | 238 | * heuristic based on the following information: |
andrewbonney | 0:ec559500a63f | 239 | * |
andrewbonney | 0:ec559500a63f | 240 | * Did the user set TCP_WRITE_FLAG_MORE? |
andrewbonney | 0:ec559500a63f | 241 | * |
andrewbonney | 0:ec559500a63f | 242 | * Will the Nagle algorithm defer transmission of this segment? |
andrewbonney | 0:ec559500a63f | 243 | */ |
andrewbonney | 0:ec559500a63f | 244 | if ((apiflags & TCP_WRITE_FLAG_MORE) || |
andrewbonney | 0:ec559500a63f | 245 | (!(pcb->flags & TF_NODELAY) && |
andrewbonney | 0:ec559500a63f | 246 | (!first_seg || |
andrewbonney | 0:ec559500a63f | 247 | pcb->unsent != NULL || |
andrewbonney | 0:ec559500a63f | 248 | pcb->unacked != NULL))) { |
andrewbonney | 0:ec559500a63f | 249 | alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(length + TCP_OVERSIZE)); |
andrewbonney | 0:ec559500a63f | 250 | } |
andrewbonney | 0:ec559500a63f | 251 | } |
andrewbonney | 0:ec559500a63f | 252 | #endif /* LWIP_NETIF_TX_SINGLE_PBUF */ |
andrewbonney | 0:ec559500a63f | 253 | p = pbuf_alloc(layer, alloc, PBUF_RAM); |
andrewbonney | 0:ec559500a63f | 254 | if (p == NULL) { |
andrewbonney | 0:ec559500a63f | 255 | return NULL; |
andrewbonney | 0:ec559500a63f | 256 | } |
andrewbonney | 0:ec559500a63f | 257 | LWIP_ASSERT("need unchained pbuf", p->next == NULL); |
andrewbonney | 0:ec559500a63f | 258 | *oversize = p->len - length; |
andrewbonney | 0:ec559500a63f | 259 | /* trim p->len to the currently used size */ |
andrewbonney | 0:ec559500a63f | 260 | p->len = p->tot_len = length; |
andrewbonney | 0:ec559500a63f | 261 | return p; |
andrewbonney | 0:ec559500a63f | 262 | } |
andrewbonney | 0:ec559500a63f | 263 | #else /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 264 | #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM) |
andrewbonney | 0:ec559500a63f | 265 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 266 | |
andrewbonney | 0:ec559500a63f | 267 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 268 | /** Add a checksum of newly added data to the segment */ |
andrewbonney | 0:ec559500a63f | 269 | static void |
andrewbonney | 0:ec559500a63f | 270 | tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum, |
andrewbonney | 0:ec559500a63f | 271 | u8_t *seg_chksum_swapped) |
andrewbonney | 0:ec559500a63f | 272 | { |
andrewbonney | 0:ec559500a63f | 273 | u32_t helper; |
andrewbonney | 0:ec559500a63f | 274 | /* add chksum to old chksum and fold to u16_t */ |
andrewbonney | 0:ec559500a63f | 275 | helper = chksum + *seg_chksum; |
andrewbonney | 0:ec559500a63f | 276 | chksum = FOLD_U32T(helper); |
andrewbonney | 0:ec559500a63f | 277 | if ((len & 1) != 0) { |
andrewbonney | 0:ec559500a63f | 278 | *seg_chksum_swapped = 1 - *seg_chksum_swapped; |
andrewbonney | 0:ec559500a63f | 279 | chksum = SWAP_BYTES_IN_WORD(chksum); |
andrewbonney | 0:ec559500a63f | 280 | } |
andrewbonney | 0:ec559500a63f | 281 | *seg_chksum = chksum; |
andrewbonney | 0:ec559500a63f | 282 | } |
andrewbonney | 0:ec559500a63f | 283 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 284 | |
andrewbonney | 0:ec559500a63f | 285 | /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen). |
andrewbonney | 0:ec559500a63f | 286 | * |
andrewbonney | 0:ec559500a63f | 287 | * @param pcb the tcp pcb to check for |
andrewbonney | 0:ec559500a63f | 288 | * @param len length of data to send (checked agains snd_buf) |
andrewbonney | 0:ec559500a63f | 289 | * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise |
andrewbonney | 0:ec559500a63f | 290 | */ |
andrewbonney | 0:ec559500a63f | 291 | static err_t |
andrewbonney | 0:ec559500a63f | 292 | tcp_write_checks(struct tcp_pcb *pcb, u16_t len) |
andrewbonney | 0:ec559500a63f | 293 | { |
andrewbonney | 0:ec559500a63f | 294 | /* connection is in invalid state for data transmission? */ |
andrewbonney | 0:ec559500a63f | 295 | if ((pcb->state != ESTABLISHED) && |
andrewbonney | 0:ec559500a63f | 296 | (pcb->state != CLOSE_WAIT) && |
andrewbonney | 0:ec559500a63f | 297 | (pcb->state != SYN_SENT) && |
andrewbonney | 0:ec559500a63f | 298 | (pcb->state != SYN_RCVD)) { |
andrewbonney | 0:ec559500a63f | 299 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n")); |
andrewbonney | 0:ec559500a63f | 300 | return ERR_CONN; |
andrewbonney | 0:ec559500a63f | 301 | } else if (len == 0) { |
andrewbonney | 0:ec559500a63f | 302 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 303 | } |
andrewbonney | 0:ec559500a63f | 304 | |
andrewbonney | 0:ec559500a63f | 305 | /* fail on too much data */ |
andrewbonney | 0:ec559500a63f | 306 | if (len > pcb->snd_buf) { |
andrewbonney | 0:ec559500a63f | 307 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n", |
andrewbonney | 0:ec559500a63f | 308 | len, pcb->snd_buf)); |
andrewbonney | 0:ec559500a63f | 309 | pcb->flags |= TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 310 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 311 | } |
andrewbonney | 0:ec559500a63f | 312 | |
andrewbonney | 0:ec559500a63f | 313 | LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen)); |
andrewbonney | 0:ec559500a63f | 314 | |
andrewbonney | 0:ec559500a63f | 315 | /* If total number of pbufs on the unsent/unacked queues exceeds the |
andrewbonney | 0:ec559500a63f | 316 | * configured maximum, return an error */ |
andrewbonney | 0:ec559500a63f | 317 | /* check for configured max queuelen and possible overflow */ |
andrewbonney | 0:ec559500a63f | 318 | if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) { |
andrewbonney | 0:ec559500a63f | 319 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n", |
andrewbonney | 0:ec559500a63f | 320 | pcb->snd_queuelen, TCP_SND_QUEUELEN)); |
andrewbonney | 0:ec559500a63f | 321 | TCP_STATS_INC(tcp.memerr); |
andrewbonney | 0:ec559500a63f | 322 | pcb->flags |= TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 323 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 324 | } |
andrewbonney | 0:ec559500a63f | 325 | if (pcb->snd_queuelen != 0) { |
andrewbonney | 0:ec559500a63f | 326 | LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty", |
andrewbonney | 0:ec559500a63f | 327 | pcb->unacked != NULL || pcb->unsent != NULL); |
andrewbonney | 0:ec559500a63f | 328 | } else { |
andrewbonney | 0:ec559500a63f | 329 | LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty", |
andrewbonney | 0:ec559500a63f | 330 | pcb->unacked == NULL && pcb->unsent == NULL); |
andrewbonney | 0:ec559500a63f | 331 | } |
andrewbonney | 0:ec559500a63f | 332 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 333 | } |
andrewbonney | 0:ec559500a63f | 334 | |
andrewbonney | 0:ec559500a63f | 335 | /** |
andrewbonney | 0:ec559500a63f | 336 | * Write data for sending (but does not send it immediately). |
andrewbonney | 0:ec559500a63f | 337 | * |
andrewbonney | 0:ec559500a63f | 338 | * It waits in the expectation of more data being sent soon (as |
andrewbonney | 0:ec559500a63f | 339 | * it can send them more efficiently by combining them together). |
andrewbonney | 0:ec559500a63f | 340 | * To prompt the system to send data now, call tcp_output() after |
andrewbonney | 0:ec559500a63f | 341 | * calling tcp_write(). |
andrewbonney | 0:ec559500a63f | 342 | * |
andrewbonney | 0:ec559500a63f | 343 | * @param pcb Protocol control block for the TCP connection to enqueue data for. |
andrewbonney | 0:ec559500a63f | 344 | * @param arg Pointer to the data to be enqueued for sending. |
andrewbonney | 0:ec559500a63f | 345 | * @param len Data length in bytes |
andrewbonney | 0:ec559500a63f | 346 | * @param apiflags combination of following flags : |
andrewbonney | 0:ec559500a63f | 347 | * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack |
andrewbonney | 0:ec559500a63f | 348 | * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent, |
andrewbonney | 0:ec559500a63f | 349 | * @return ERR_OK if enqueued, another err_t on error |
andrewbonney | 0:ec559500a63f | 350 | */ |
andrewbonney | 0:ec559500a63f | 351 | err_t |
andrewbonney | 0:ec559500a63f | 352 | tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags) |
andrewbonney | 0:ec559500a63f | 353 | { |
andrewbonney | 0:ec559500a63f | 354 | struct pbuf *concat_p = NULL; |
andrewbonney | 0:ec559500a63f | 355 | struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL; |
andrewbonney | 0:ec559500a63f | 356 | u16_t pos = 0; /* position in 'arg' data */ |
andrewbonney | 0:ec559500a63f | 357 | u16_t queuelen; |
andrewbonney | 0:ec559500a63f | 358 | u8_t optlen = 0; |
andrewbonney | 0:ec559500a63f | 359 | u8_t optflags = 0; |
andrewbonney | 0:ec559500a63f | 360 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 361 | u16_t oversize = 0; |
andrewbonney | 0:ec559500a63f | 362 | u16_t oversize_used = 0; |
andrewbonney | 0:ec559500a63f | 363 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 364 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 365 | u16_t concat_chksum = 0; |
andrewbonney | 0:ec559500a63f | 366 | u8_t concat_chksum_swapped = 0; |
andrewbonney | 0:ec559500a63f | 367 | u16_t concat_chksummed = 0; |
andrewbonney | 0:ec559500a63f | 368 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 369 | err_t err; |
andrewbonney | 0:ec559500a63f | 370 | |
andrewbonney | 0:ec559500a63f | 371 | #if LWIP_NETIF_TX_SINGLE_PBUF |
andrewbonney | 0:ec559500a63f | 372 | /* Always copy to try to create single pbufs for TX */ |
andrewbonney | 0:ec559500a63f | 373 | apiflags |= TCP_WRITE_FLAG_COPY; |
andrewbonney | 0:ec559500a63f | 374 | #endif /* LWIP_NETIF_TX_SINGLE_PBUF */ |
andrewbonney | 0:ec559500a63f | 375 | |
andrewbonney | 0:ec559500a63f | 376 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n", |
andrewbonney | 0:ec559500a63f | 377 | (void *)pcb, arg, len, (u16_t)apiflags)); |
andrewbonney | 0:ec559500a63f | 378 | LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)", |
andrewbonney | 0:ec559500a63f | 379 | arg != NULL, return ERR_ARG;); |
andrewbonney | 0:ec559500a63f | 380 | |
andrewbonney | 0:ec559500a63f | 381 | err = tcp_write_checks(pcb, len); |
andrewbonney | 0:ec559500a63f | 382 | if (err != ERR_OK) { |
andrewbonney | 0:ec559500a63f | 383 | return err; |
andrewbonney | 0:ec559500a63f | 384 | } |
andrewbonney | 0:ec559500a63f | 385 | queuelen = pcb->snd_queuelen; |
andrewbonney | 0:ec559500a63f | 386 | |
andrewbonney | 0:ec559500a63f | 387 | #if LWIP_TCP_TIMESTAMPS |
andrewbonney | 0:ec559500a63f | 388 | if ((pcb->flags & TF_TIMESTAMP)) { |
andrewbonney | 0:ec559500a63f | 389 | optflags = TF_SEG_OPTS_TS; |
andrewbonney | 0:ec559500a63f | 390 | optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS); |
andrewbonney | 0:ec559500a63f | 391 | } |
andrewbonney | 0:ec559500a63f | 392 | #endif /* LWIP_TCP_TIMESTAMPS */ |
andrewbonney | 0:ec559500a63f | 393 | |
andrewbonney | 0:ec559500a63f | 394 | |
andrewbonney | 0:ec559500a63f | 395 | /* |
andrewbonney | 0:ec559500a63f | 396 | * TCP segmentation is done in three phases with increasing complexity: |
andrewbonney | 0:ec559500a63f | 397 | * |
andrewbonney | 0:ec559500a63f | 398 | * 1. Copy data directly into an oversized pbuf. |
andrewbonney | 0:ec559500a63f | 399 | * 2. Chain a new pbuf to the end of pcb->unsent. |
andrewbonney | 0:ec559500a63f | 400 | * 3. Create new segments. |
andrewbonney | 0:ec559500a63f | 401 | * |
andrewbonney | 0:ec559500a63f | 402 | * We may run out of memory at any point. In that case we must |
andrewbonney | 0:ec559500a63f | 403 | * return ERR_MEM and not change anything in pcb. Therefore, all |
andrewbonney | 0:ec559500a63f | 404 | * changes are recorded in local variables and committed at the end |
andrewbonney | 0:ec559500a63f | 405 | * of the function. Some pcb fields are maintained in local copies: |
andrewbonney | 0:ec559500a63f | 406 | * |
andrewbonney | 0:ec559500a63f | 407 | * queuelen = pcb->snd_queuelen |
andrewbonney | 0:ec559500a63f | 408 | * oversize = pcb->unsent_oversize |
andrewbonney | 0:ec559500a63f | 409 | * |
andrewbonney | 0:ec559500a63f | 410 | * These variables are set consistently by the phases: |
andrewbonney | 0:ec559500a63f | 411 | * |
andrewbonney | 0:ec559500a63f | 412 | * seg points to the last segment tampered with. |
andrewbonney | 0:ec559500a63f | 413 | * |
andrewbonney | 0:ec559500a63f | 414 | * pos records progress as data is segmented. |
andrewbonney | 0:ec559500a63f | 415 | */ |
andrewbonney | 0:ec559500a63f | 416 | |
andrewbonney | 0:ec559500a63f | 417 | /* Find the tail of the unsent queue. */ |
andrewbonney | 0:ec559500a63f | 418 | if (pcb->unsent != NULL) { |
andrewbonney | 0:ec559500a63f | 419 | u16_t space; |
andrewbonney | 0:ec559500a63f | 420 | u16_t unsent_optlen; |
andrewbonney | 0:ec559500a63f | 421 | |
andrewbonney | 0:ec559500a63f | 422 | /* @todo: this could be sped up by keeping last_unsent in the pcb */ |
andrewbonney | 0:ec559500a63f | 423 | for (last_unsent = pcb->unsent; last_unsent->next != NULL; |
andrewbonney | 0:ec559500a63f | 424 | last_unsent = last_unsent->next); |
andrewbonney | 0:ec559500a63f | 425 | |
andrewbonney | 0:ec559500a63f | 426 | /* Usable space at the end of the last unsent segment */ |
andrewbonney | 0:ec559500a63f | 427 | unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags); |
andrewbonney | 0:ec559500a63f | 428 | space = pcb->mss - (last_unsent->len + unsent_optlen); |
andrewbonney | 0:ec559500a63f | 429 | |
andrewbonney | 0:ec559500a63f | 430 | /* |
andrewbonney | 0:ec559500a63f | 431 | * Phase 1: Copy data directly into an oversized pbuf. |
andrewbonney | 0:ec559500a63f | 432 | * |
andrewbonney | 0:ec559500a63f | 433 | * The number of bytes copied is recorded in the oversize_used |
andrewbonney | 0:ec559500a63f | 434 | * variable. The actual copying is done at the bottom of the |
andrewbonney | 0:ec559500a63f | 435 | * function. |
andrewbonney | 0:ec559500a63f | 436 | */ |
andrewbonney | 0:ec559500a63f | 437 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 438 | #if TCP_OVERSIZE_DBGCHECK |
andrewbonney | 0:ec559500a63f | 439 | /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */ |
andrewbonney | 0:ec559500a63f | 440 | LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)", |
andrewbonney | 0:ec559500a63f | 441 | pcb->unsent_oversize == last_unsent->oversize_left); |
andrewbonney | 0:ec559500a63f | 442 | #endif /* TCP_OVERSIZE_DBGCHECK */ |
andrewbonney | 0:ec559500a63f | 443 | oversize = pcb->unsent_oversize; |
andrewbonney | 0:ec559500a63f | 444 | if (oversize > 0) { |
andrewbonney | 0:ec559500a63f | 445 | LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space); |
andrewbonney | 0:ec559500a63f | 446 | seg = last_unsent; |
andrewbonney | 0:ec559500a63f | 447 | oversize_used = oversize < len ? oversize : len; |
andrewbonney | 0:ec559500a63f | 448 | pos += oversize_used; |
andrewbonney | 0:ec559500a63f | 449 | oversize -= oversize_used; |
andrewbonney | 0:ec559500a63f | 450 | space -= oversize_used; |
andrewbonney | 0:ec559500a63f | 451 | } |
andrewbonney | 0:ec559500a63f | 452 | /* now we are either finished or oversize is zero */ |
andrewbonney | 0:ec559500a63f | 453 | LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len)); |
andrewbonney | 0:ec559500a63f | 454 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 455 | |
andrewbonney | 0:ec559500a63f | 456 | /* |
andrewbonney | 0:ec559500a63f | 457 | * Phase 2: Chain a new pbuf to the end of pcb->unsent. |
andrewbonney | 0:ec559500a63f | 458 | * |
andrewbonney | 0:ec559500a63f | 459 | * We don't extend segments containing SYN/FIN flags or options |
andrewbonney | 0:ec559500a63f | 460 | * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at |
andrewbonney | 0:ec559500a63f | 461 | * the end. |
andrewbonney | 0:ec559500a63f | 462 | */ |
andrewbonney | 0:ec559500a63f | 463 | if ((pos < len) && (space > 0) && (last_unsent->len > 0)) { |
andrewbonney | 0:ec559500a63f | 464 | u16_t seglen = space < len - pos ? space : len - pos; |
andrewbonney | 0:ec559500a63f | 465 | seg = last_unsent; |
andrewbonney | 0:ec559500a63f | 466 | |
andrewbonney | 0:ec559500a63f | 467 | /* Create a pbuf with a copy or reference to seglen bytes. We |
andrewbonney | 0:ec559500a63f | 468 | * can use PBUF_RAW here since the data appears in the middle of |
andrewbonney | 0:ec559500a63f | 469 | * a segment. A header will never be prepended. */ |
andrewbonney | 0:ec559500a63f | 470 | if (apiflags & TCP_WRITE_FLAG_COPY) { |
andrewbonney | 0:ec559500a63f | 471 | /* Data is copied */ |
andrewbonney | 0:ec559500a63f | 472 | if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) { |
andrewbonney | 0:ec559500a63f | 473 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, |
andrewbonney | 0:ec559500a63f | 474 | ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", |
andrewbonney | 0:ec559500a63f | 475 | seglen)); |
andrewbonney | 0:ec559500a63f | 476 | goto memerr; |
andrewbonney | 0:ec559500a63f | 477 | } |
andrewbonney | 0:ec559500a63f | 478 | #if TCP_OVERSIZE_DBGCHECK |
andrewbonney | 0:ec559500a63f | 479 | last_unsent->oversize_left = oversize; |
andrewbonney | 0:ec559500a63f | 480 | #endif /* TCP_OVERSIZE_DBGCHECK */ |
andrewbonney | 0:ec559500a63f | 481 | TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped); |
andrewbonney | 0:ec559500a63f | 482 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 483 | concat_chksummed += seglen; |
andrewbonney | 0:ec559500a63f | 484 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 485 | } else { |
andrewbonney | 0:ec559500a63f | 486 | /* Data is not copied */ |
andrewbonney | 0:ec559500a63f | 487 | if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) { |
andrewbonney | 0:ec559500a63f | 488 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, |
andrewbonney | 0:ec559500a63f | 489 | ("tcp_write: could not allocate memory for zero-copy pbuf\n")); |
andrewbonney | 0:ec559500a63f | 490 | goto memerr; |
andrewbonney | 0:ec559500a63f | 491 | } |
andrewbonney | 0:ec559500a63f | 492 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 493 | /* calculate the checksum of nocopy-data */ |
andrewbonney | 0:ec559500a63f | 494 | tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen, |
andrewbonney | 0:ec559500a63f | 495 | &concat_chksum, &concat_chksum_swapped); |
andrewbonney | 0:ec559500a63f | 496 | concat_chksummed += seglen; |
andrewbonney | 0:ec559500a63f | 497 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 498 | /* reference the non-volatile payload data */ |
andrewbonney | 0:ec559500a63f | 499 | concat_p->payload = (u8_t*)arg + pos; |
andrewbonney | 0:ec559500a63f | 500 | } |
andrewbonney | 0:ec559500a63f | 501 | |
andrewbonney | 0:ec559500a63f | 502 | pos += seglen; |
andrewbonney | 0:ec559500a63f | 503 | queuelen += pbuf_clen(concat_p); |
andrewbonney | 0:ec559500a63f | 504 | } |
andrewbonney | 0:ec559500a63f | 505 | } else { |
andrewbonney | 0:ec559500a63f | 506 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 507 | LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)", |
andrewbonney | 0:ec559500a63f | 508 | pcb->unsent_oversize == 0); |
andrewbonney | 0:ec559500a63f | 509 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 510 | } |
andrewbonney | 0:ec559500a63f | 511 | |
andrewbonney | 0:ec559500a63f | 512 | /* |
andrewbonney | 0:ec559500a63f | 513 | * Phase 3: Create new segments. |
andrewbonney | 0:ec559500a63f | 514 | * |
andrewbonney | 0:ec559500a63f | 515 | * The new segments are chained together in the local 'queue' |
andrewbonney | 0:ec559500a63f | 516 | * variable, ready to be appended to pcb->unsent. |
andrewbonney | 0:ec559500a63f | 517 | */ |
andrewbonney | 0:ec559500a63f | 518 | while (pos < len) { |
andrewbonney | 0:ec559500a63f | 519 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 520 | u16_t left = len - pos; |
andrewbonney | 0:ec559500a63f | 521 | u16_t max_len = pcb->mss - optlen; |
andrewbonney | 0:ec559500a63f | 522 | u16_t seglen = left > max_len ? max_len : left; |
andrewbonney | 0:ec559500a63f | 523 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 524 | u16_t chksum = 0; |
andrewbonney | 0:ec559500a63f | 525 | u8_t chksum_swapped = 0; |
andrewbonney | 0:ec559500a63f | 526 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 527 | |
andrewbonney | 0:ec559500a63f | 528 | if (apiflags & TCP_WRITE_FLAG_COPY) { |
andrewbonney | 0:ec559500a63f | 529 | /* If copy is set, memory should be allocated and data copied |
andrewbonney | 0:ec559500a63f | 530 | * into pbuf */ |
andrewbonney | 0:ec559500a63f | 531 | if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, pcb->mss, &oversize, pcb, apiflags, queue == NULL)) == NULL) { |
andrewbonney | 0:ec559500a63f | 532 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen)); |
andrewbonney | 0:ec559500a63f | 533 | goto memerr; |
andrewbonney | 0:ec559500a63f | 534 | } |
andrewbonney | 0:ec559500a63f | 535 | LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen", |
andrewbonney | 0:ec559500a63f | 536 | (p->len >= seglen)); |
andrewbonney | 0:ec559500a63f | 537 | TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped); |
andrewbonney | 0:ec559500a63f | 538 | } else { |
andrewbonney | 0:ec559500a63f | 539 | /* Copy is not set: First allocate a pbuf for holding the data. |
andrewbonney | 0:ec559500a63f | 540 | * Since the referenced data is available at least until it is |
andrewbonney | 0:ec559500a63f | 541 | * sent out on the link (as it has to be ACKed by the remote |
andrewbonney | 0:ec559500a63f | 542 | * party) we can safely use PBUF_ROM instead of PBUF_REF here. |
andrewbonney | 0:ec559500a63f | 543 | */ |
andrewbonney | 0:ec559500a63f | 544 | struct pbuf *p2; |
andrewbonney | 0:ec559500a63f | 545 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 546 | LWIP_ASSERT("oversize == 0", oversize == 0); |
andrewbonney | 0:ec559500a63f | 547 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 548 | if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) { |
andrewbonney | 0:ec559500a63f | 549 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for zero-copy pbuf\n")); |
andrewbonney | 0:ec559500a63f | 550 | goto memerr; |
andrewbonney | 0:ec559500a63f | 551 | } |
andrewbonney | 0:ec559500a63f | 552 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 553 | /* calculate the checksum of nocopy-data */ |
andrewbonney | 0:ec559500a63f | 554 | chksum = ~inet_chksum((u8_t*)arg + pos, seglen); |
andrewbonney | 0:ec559500a63f | 555 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 556 | /* reference the non-volatile payload data */ |
andrewbonney | 0:ec559500a63f | 557 | p2->payload = (u8_t*)arg + pos; |
andrewbonney | 0:ec559500a63f | 558 | |
andrewbonney | 0:ec559500a63f | 559 | /* Second, allocate a pbuf for the headers. */ |
andrewbonney | 0:ec559500a63f | 560 | if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) { |
andrewbonney | 0:ec559500a63f | 561 | /* If allocation fails, we have to deallocate the data pbuf as |
andrewbonney | 0:ec559500a63f | 562 | * well. */ |
andrewbonney | 0:ec559500a63f | 563 | pbuf_free(p2); |
andrewbonney | 0:ec559500a63f | 564 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for header pbuf\n")); |
andrewbonney | 0:ec559500a63f | 565 | goto memerr; |
andrewbonney | 0:ec559500a63f | 566 | } |
andrewbonney | 0:ec559500a63f | 567 | /* Concatenate the headers and data pbufs together. */ |
andrewbonney | 0:ec559500a63f | 568 | pbuf_cat(p/*header*/, p2/*data*/); |
andrewbonney | 0:ec559500a63f | 569 | } |
andrewbonney | 0:ec559500a63f | 570 | |
andrewbonney | 0:ec559500a63f | 571 | queuelen += pbuf_clen(p); |
andrewbonney | 0:ec559500a63f | 572 | |
andrewbonney | 0:ec559500a63f | 573 | /* Now that there are more segments queued, we check again if the |
andrewbonney | 0:ec559500a63f | 574 | * length of the queue exceeds the configured maximum or |
andrewbonney | 0:ec559500a63f | 575 | * overflows. */ |
andrewbonney | 0:ec559500a63f | 576 | if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) { |
andrewbonney | 0:ec559500a63f | 577 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN)); |
andrewbonney | 0:ec559500a63f | 578 | pbuf_free(p); |
andrewbonney | 0:ec559500a63f | 579 | goto memerr; |
andrewbonney | 0:ec559500a63f | 580 | } |
andrewbonney | 0:ec559500a63f | 581 | |
andrewbonney | 0:ec559500a63f | 582 | if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) { |
andrewbonney | 0:ec559500a63f | 583 | goto memerr; |
andrewbonney | 0:ec559500a63f | 584 | } |
andrewbonney | 0:ec559500a63f | 585 | #if TCP_OVERSIZE_DBGCHECK |
andrewbonney | 0:ec559500a63f | 586 | seg->oversize_left = oversize; |
andrewbonney | 0:ec559500a63f | 587 | #endif /* TCP_OVERSIZE_DBGCHECK */ |
andrewbonney | 0:ec559500a63f | 588 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 589 | seg->chksum = chksum; |
andrewbonney | 0:ec559500a63f | 590 | seg->chksum_swapped = chksum_swapped; |
andrewbonney | 0:ec559500a63f | 591 | seg->flags |= TF_SEG_DATA_CHECKSUMMED; |
andrewbonney | 0:ec559500a63f | 592 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 593 | /* Fix dataptr for the nocopy case */ |
andrewbonney | 0:ec559500a63f | 594 | if ((apiflags & TCP_WRITE_FLAG_COPY) == 0) { |
andrewbonney | 0:ec559500a63f | 595 | seg->dataptr = (u8_t*)arg + pos; |
andrewbonney | 0:ec559500a63f | 596 | } |
andrewbonney | 0:ec559500a63f | 597 | |
andrewbonney | 0:ec559500a63f | 598 | /* first segment of to-be-queued data? */ |
andrewbonney | 0:ec559500a63f | 599 | if (queue == NULL) { |
andrewbonney | 0:ec559500a63f | 600 | queue = seg; |
andrewbonney | 0:ec559500a63f | 601 | } else { |
andrewbonney | 0:ec559500a63f | 602 | /* Attach the segment to the end of the queued segments */ |
andrewbonney | 0:ec559500a63f | 603 | LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL); |
andrewbonney | 0:ec559500a63f | 604 | prev_seg->next = seg; |
andrewbonney | 0:ec559500a63f | 605 | } |
andrewbonney | 0:ec559500a63f | 606 | /* remember last segment of to-be-queued data for next iteration */ |
andrewbonney | 0:ec559500a63f | 607 | prev_seg = seg; |
andrewbonney | 0:ec559500a63f | 608 | |
andrewbonney | 0:ec559500a63f | 609 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 610 | ntohl(seg->tcphdr->seqno), |
andrewbonney | 0:ec559500a63f | 611 | ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg))); |
andrewbonney | 0:ec559500a63f | 612 | |
andrewbonney | 0:ec559500a63f | 613 | pos += seglen; |
andrewbonney | 0:ec559500a63f | 614 | } |
andrewbonney | 0:ec559500a63f | 615 | |
andrewbonney | 0:ec559500a63f | 616 | /* |
andrewbonney | 0:ec559500a63f | 617 | * All three segmentation phases were successful. We can commit the |
andrewbonney | 0:ec559500a63f | 618 | * transaction. |
andrewbonney | 0:ec559500a63f | 619 | */ |
andrewbonney | 0:ec559500a63f | 620 | |
andrewbonney | 0:ec559500a63f | 621 | /* |
andrewbonney | 0:ec559500a63f | 622 | * Phase 1: If data has been added to the preallocated tail of |
andrewbonney | 0:ec559500a63f | 623 | * last_unsent, we update the length fields of the pbuf chain. |
andrewbonney | 0:ec559500a63f | 624 | */ |
andrewbonney | 0:ec559500a63f | 625 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 626 | if (oversize_used > 0) { |
andrewbonney | 0:ec559500a63f | 627 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 628 | /* Bump tot_len of whole chain, len of tail */ |
andrewbonney | 0:ec559500a63f | 629 | for (p = last_unsent->p; p; p = p->next) { |
andrewbonney | 0:ec559500a63f | 630 | p->tot_len += oversize_used; |
andrewbonney | 0:ec559500a63f | 631 | if (p->next == NULL) { |
andrewbonney | 0:ec559500a63f | 632 | TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent); |
andrewbonney | 0:ec559500a63f | 633 | p->len += oversize_used; |
andrewbonney | 0:ec559500a63f | 634 | } |
andrewbonney | 0:ec559500a63f | 635 | } |
andrewbonney | 0:ec559500a63f | 636 | last_unsent->len += oversize_used; |
andrewbonney | 0:ec559500a63f | 637 | #if TCP_OVERSIZE_DBGCHECK |
andrewbonney | 0:ec559500a63f | 638 | last_unsent->oversize_left -= oversize_used; |
andrewbonney | 0:ec559500a63f | 639 | #endif /* TCP_OVERSIZE_DBGCHECK */ |
andrewbonney | 0:ec559500a63f | 640 | } |
andrewbonney | 0:ec559500a63f | 641 | pcb->unsent_oversize = oversize; |
andrewbonney | 0:ec559500a63f | 642 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 643 | |
andrewbonney | 0:ec559500a63f | 644 | /* |
andrewbonney | 0:ec559500a63f | 645 | * Phase 2: concat_p can be concatenated onto last_unsent->p |
andrewbonney | 0:ec559500a63f | 646 | */ |
andrewbonney | 0:ec559500a63f | 647 | if (concat_p != NULL) { |
andrewbonney | 0:ec559500a63f | 648 | LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty", |
andrewbonney | 0:ec559500a63f | 649 | (last_unsent != NULL)); |
andrewbonney | 0:ec559500a63f | 650 | pbuf_cat(last_unsent->p, concat_p); |
andrewbonney | 0:ec559500a63f | 651 | last_unsent->len += concat_p->tot_len; |
andrewbonney | 0:ec559500a63f | 652 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 653 | if (concat_chksummed) { |
andrewbonney | 0:ec559500a63f | 654 | tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum, |
andrewbonney | 0:ec559500a63f | 655 | &last_unsent->chksum_swapped); |
andrewbonney | 0:ec559500a63f | 656 | last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED; |
andrewbonney | 0:ec559500a63f | 657 | } |
andrewbonney | 0:ec559500a63f | 658 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 659 | } |
andrewbonney | 0:ec559500a63f | 660 | |
andrewbonney | 0:ec559500a63f | 661 | /* |
andrewbonney | 0:ec559500a63f | 662 | * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that |
andrewbonney | 0:ec559500a63f | 663 | * is harmless |
andrewbonney | 0:ec559500a63f | 664 | */ |
andrewbonney | 0:ec559500a63f | 665 | if (last_unsent == NULL) { |
andrewbonney | 0:ec559500a63f | 666 | pcb->unsent = queue; |
andrewbonney | 0:ec559500a63f | 667 | } else { |
andrewbonney | 0:ec559500a63f | 668 | last_unsent->next = queue; |
andrewbonney | 0:ec559500a63f | 669 | } |
andrewbonney | 0:ec559500a63f | 670 | |
andrewbonney | 0:ec559500a63f | 671 | /* |
andrewbonney | 0:ec559500a63f | 672 | * Finally update the pcb state. |
andrewbonney | 0:ec559500a63f | 673 | */ |
andrewbonney | 0:ec559500a63f | 674 | pcb->snd_lbb += len; |
andrewbonney | 0:ec559500a63f | 675 | pcb->snd_buf -= len; |
andrewbonney | 0:ec559500a63f | 676 | pcb->snd_queuelen = queuelen; |
andrewbonney | 0:ec559500a63f | 677 | |
andrewbonney | 0:ec559500a63f | 678 | LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n", |
andrewbonney | 0:ec559500a63f | 679 | pcb->snd_queuelen)); |
andrewbonney | 0:ec559500a63f | 680 | if (pcb->snd_queuelen != 0) { |
andrewbonney | 0:ec559500a63f | 681 | LWIP_ASSERT("tcp_write: valid queue length", |
andrewbonney | 0:ec559500a63f | 682 | pcb->unacked != NULL || pcb->unsent != NULL); |
andrewbonney | 0:ec559500a63f | 683 | } |
andrewbonney | 0:ec559500a63f | 684 | |
andrewbonney | 0:ec559500a63f | 685 | /* Set the PSH flag in the last segment that we enqueued. */ |
andrewbonney | 0:ec559500a63f | 686 | if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) { |
andrewbonney | 0:ec559500a63f | 687 | TCPH_SET_FLAG(seg->tcphdr, TCP_PSH); |
andrewbonney | 0:ec559500a63f | 688 | } |
andrewbonney | 0:ec559500a63f | 689 | |
andrewbonney | 0:ec559500a63f | 690 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 691 | memerr: |
andrewbonney | 0:ec559500a63f | 692 | pcb->flags |= TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 693 | TCP_STATS_INC(tcp.memerr); |
andrewbonney | 0:ec559500a63f | 694 | |
andrewbonney | 0:ec559500a63f | 695 | if (concat_p != NULL) { |
andrewbonney | 0:ec559500a63f | 696 | pbuf_free(concat_p); |
andrewbonney | 0:ec559500a63f | 697 | } |
andrewbonney | 0:ec559500a63f | 698 | if (queue != NULL) { |
andrewbonney | 0:ec559500a63f | 699 | tcp_segs_free(queue); |
andrewbonney | 0:ec559500a63f | 700 | } |
andrewbonney | 0:ec559500a63f | 701 | if (pcb->snd_queuelen != 0) { |
andrewbonney | 0:ec559500a63f | 702 | LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL || |
andrewbonney | 0:ec559500a63f | 703 | pcb->unsent != NULL); |
andrewbonney | 0:ec559500a63f | 704 | } |
andrewbonney | 0:ec559500a63f | 705 | LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen)); |
andrewbonney | 0:ec559500a63f | 706 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 707 | } |
andrewbonney | 0:ec559500a63f | 708 | |
andrewbonney | 0:ec559500a63f | 709 | /** |
andrewbonney | 0:ec559500a63f | 710 | * Enqueue TCP options for transmission. |
andrewbonney | 0:ec559500a63f | 711 | * |
andrewbonney | 0:ec559500a63f | 712 | * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl(). |
andrewbonney | 0:ec559500a63f | 713 | * |
andrewbonney | 0:ec559500a63f | 714 | * @param pcb Protocol control block for the TCP connection. |
andrewbonney | 0:ec559500a63f | 715 | * @param flags TCP header flags to set in the outgoing segment. |
andrewbonney | 0:ec559500a63f | 716 | * @param optdata pointer to TCP options, or NULL. |
andrewbonney | 0:ec559500a63f | 717 | * @param optlen length of TCP options in bytes. |
andrewbonney | 0:ec559500a63f | 718 | */ |
andrewbonney | 0:ec559500a63f | 719 | err_t |
andrewbonney | 0:ec559500a63f | 720 | tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags) |
andrewbonney | 0:ec559500a63f | 721 | { |
andrewbonney | 0:ec559500a63f | 722 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 723 | struct tcp_seg *seg; |
andrewbonney | 0:ec559500a63f | 724 | u8_t optflags = 0; |
andrewbonney | 0:ec559500a63f | 725 | u8_t optlen = 0; |
andrewbonney | 0:ec559500a63f | 726 | |
andrewbonney | 0:ec559500a63f | 727 | LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen)); |
andrewbonney | 0:ec559500a63f | 728 | |
andrewbonney | 0:ec559500a63f | 729 | LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)", |
andrewbonney | 0:ec559500a63f | 730 | (flags & (TCP_SYN | TCP_FIN)) != 0); |
andrewbonney | 0:ec559500a63f | 731 | |
andrewbonney | 0:ec559500a63f | 732 | /* check for configured max queuelen and possible overflow */ |
andrewbonney | 0:ec559500a63f | 733 | if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) { |
andrewbonney | 0:ec559500a63f | 734 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n", |
andrewbonney | 0:ec559500a63f | 735 | pcb->snd_queuelen, TCP_SND_QUEUELEN)); |
andrewbonney | 0:ec559500a63f | 736 | TCP_STATS_INC(tcp.memerr); |
andrewbonney | 0:ec559500a63f | 737 | pcb->flags |= TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 738 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 739 | } |
andrewbonney | 0:ec559500a63f | 740 | |
andrewbonney | 0:ec559500a63f | 741 | if (flags & TCP_SYN) { |
andrewbonney | 0:ec559500a63f | 742 | optflags = TF_SEG_OPTS_MSS; |
andrewbonney | 0:ec559500a63f | 743 | } |
andrewbonney | 0:ec559500a63f | 744 | #if LWIP_TCP_TIMESTAMPS |
andrewbonney | 0:ec559500a63f | 745 | if ((pcb->flags & TF_TIMESTAMP)) { |
andrewbonney | 0:ec559500a63f | 746 | optflags |= TF_SEG_OPTS_TS; |
andrewbonney | 0:ec559500a63f | 747 | } |
andrewbonney | 0:ec559500a63f | 748 | #endif /* LWIP_TCP_TIMESTAMPS */ |
andrewbonney | 0:ec559500a63f | 749 | optlen = LWIP_TCP_OPT_LENGTH(optflags); |
andrewbonney | 0:ec559500a63f | 750 | |
andrewbonney | 0:ec559500a63f | 751 | /* tcp_enqueue_flags is always called with either SYN or FIN in flags. |
andrewbonney | 0:ec559500a63f | 752 | * We need one available snd_buf byte to do that. |
andrewbonney | 0:ec559500a63f | 753 | * This means we can't send FIN while snd_buf==0. A better fix would be to |
andrewbonney | 0:ec559500a63f | 754 | * not include SYN and FIN sequence numbers in the snd_buf count. */ |
andrewbonney | 0:ec559500a63f | 755 | if (pcb->snd_buf == 0) { |
andrewbonney | 0:ec559500a63f | 756 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: no send buffer available\n")); |
andrewbonney | 0:ec559500a63f | 757 | TCP_STATS_INC(tcp.memerr); |
andrewbonney | 0:ec559500a63f | 758 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 759 | } |
andrewbonney | 0:ec559500a63f | 760 | |
andrewbonney | 0:ec559500a63f | 761 | /* Allocate pbuf with room for TCP header + options */ |
andrewbonney | 0:ec559500a63f | 762 | if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) { |
andrewbonney | 0:ec559500a63f | 763 | pcb->flags |= TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 764 | TCP_STATS_INC(tcp.memerr); |
andrewbonney | 0:ec559500a63f | 765 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 766 | } |
andrewbonney | 0:ec559500a63f | 767 | LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen", |
andrewbonney | 0:ec559500a63f | 768 | (p->len >= optlen)); |
andrewbonney | 0:ec559500a63f | 769 | |
andrewbonney | 0:ec559500a63f | 770 | /* Allocate memory for tcp_seg, and fill in fields. */ |
andrewbonney | 0:ec559500a63f | 771 | if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) { |
andrewbonney | 0:ec559500a63f | 772 | pcb->flags |= TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 773 | TCP_STATS_INC(tcp.memerr); |
andrewbonney | 0:ec559500a63f | 774 | return ERR_MEM; |
andrewbonney | 0:ec559500a63f | 775 | } |
andrewbonney | 0:ec559500a63f | 776 | LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0); |
andrewbonney | 0:ec559500a63f | 777 | |
andrewbonney | 0:ec559500a63f | 778 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, |
andrewbonney | 0:ec559500a63f | 779 | ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n", |
andrewbonney | 0:ec559500a63f | 780 | ntohl(seg->tcphdr->seqno), |
andrewbonney | 0:ec559500a63f | 781 | ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg), |
andrewbonney | 0:ec559500a63f | 782 | (u16_t)flags)); |
andrewbonney | 0:ec559500a63f | 783 | |
andrewbonney | 0:ec559500a63f | 784 | /* Now append seg to pcb->unsent queue */ |
andrewbonney | 0:ec559500a63f | 785 | if (pcb->unsent == NULL) { |
andrewbonney | 0:ec559500a63f | 786 | pcb->unsent = seg; |
andrewbonney | 0:ec559500a63f | 787 | } else { |
andrewbonney | 0:ec559500a63f | 788 | struct tcp_seg *useg; |
andrewbonney | 0:ec559500a63f | 789 | for (useg = pcb->unsent; useg->next != NULL; useg = useg->next); |
andrewbonney | 0:ec559500a63f | 790 | useg->next = seg; |
andrewbonney | 0:ec559500a63f | 791 | } |
andrewbonney | 0:ec559500a63f | 792 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 793 | /* The new unsent tail has no space */ |
andrewbonney | 0:ec559500a63f | 794 | pcb->unsent_oversize = 0; |
andrewbonney | 0:ec559500a63f | 795 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 796 | |
andrewbonney | 0:ec559500a63f | 797 | /* SYN and FIN bump the sequence number */ |
andrewbonney | 0:ec559500a63f | 798 | if ((flags & TCP_SYN) || (flags & TCP_FIN)) { |
andrewbonney | 0:ec559500a63f | 799 | pcb->snd_lbb++; |
andrewbonney | 0:ec559500a63f | 800 | /* optlen does not influence snd_buf */ |
andrewbonney | 0:ec559500a63f | 801 | pcb->snd_buf--; |
andrewbonney | 0:ec559500a63f | 802 | } |
andrewbonney | 0:ec559500a63f | 803 | if (flags & TCP_FIN) { |
andrewbonney | 0:ec559500a63f | 804 | pcb->flags |= TF_FIN; |
andrewbonney | 0:ec559500a63f | 805 | } |
andrewbonney | 0:ec559500a63f | 806 | |
andrewbonney | 0:ec559500a63f | 807 | /* update number of segments on the queues */ |
andrewbonney | 0:ec559500a63f | 808 | pcb->snd_queuelen += pbuf_clen(seg->p); |
andrewbonney | 0:ec559500a63f | 809 | LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen)); |
andrewbonney | 0:ec559500a63f | 810 | if (pcb->snd_queuelen != 0) { |
andrewbonney | 0:ec559500a63f | 811 | LWIP_ASSERT("tcp_enqueue_flags: invalid queue length", |
andrewbonney | 0:ec559500a63f | 812 | pcb->unacked != NULL || pcb->unsent != NULL); |
andrewbonney | 0:ec559500a63f | 813 | } |
andrewbonney | 0:ec559500a63f | 814 | |
andrewbonney | 0:ec559500a63f | 815 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 816 | } |
andrewbonney | 0:ec559500a63f | 817 | |
andrewbonney | 0:ec559500a63f | 818 | |
andrewbonney | 0:ec559500a63f | 819 | #if LWIP_TCP_TIMESTAMPS |
andrewbonney | 0:ec559500a63f | 820 | /* Build a timestamp option (12 bytes long) at the specified options pointer) |
andrewbonney | 0:ec559500a63f | 821 | * |
andrewbonney | 0:ec559500a63f | 822 | * @param pcb tcp_pcb |
andrewbonney | 0:ec559500a63f | 823 | * @param opts option pointer where to store the timestamp option |
andrewbonney | 0:ec559500a63f | 824 | */ |
andrewbonney | 0:ec559500a63f | 825 | static void |
andrewbonney | 0:ec559500a63f | 826 | tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts) |
andrewbonney | 0:ec559500a63f | 827 | { |
andrewbonney | 0:ec559500a63f | 828 | /* Pad with two NOP options to make everything nicely aligned */ |
andrewbonney | 0:ec559500a63f | 829 | opts[0] = PP_HTONL(0x0101080A); |
andrewbonney | 0:ec559500a63f | 830 | opts[1] = htonl(sys_now()); |
andrewbonney | 0:ec559500a63f | 831 | opts[2] = htonl(pcb->ts_recent); |
andrewbonney | 0:ec559500a63f | 832 | } |
andrewbonney | 0:ec559500a63f | 833 | #endif |
andrewbonney | 0:ec559500a63f | 834 | |
andrewbonney | 0:ec559500a63f | 835 | /** Send an ACK without data. |
andrewbonney | 0:ec559500a63f | 836 | * |
andrewbonney | 0:ec559500a63f | 837 | * @param pcb Protocol control block for the TCP connection to send the ACK |
andrewbonney | 0:ec559500a63f | 838 | */ |
andrewbonney | 0:ec559500a63f | 839 | err_t |
andrewbonney | 0:ec559500a63f | 840 | tcp_send_empty_ack(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 841 | { |
andrewbonney | 0:ec559500a63f | 842 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 843 | struct tcp_hdr *tcphdr; |
andrewbonney | 0:ec559500a63f | 844 | u8_t optlen = 0; |
andrewbonney | 0:ec559500a63f | 845 | |
andrewbonney | 0:ec559500a63f | 846 | #if LWIP_TCP_TIMESTAMPS |
andrewbonney | 0:ec559500a63f | 847 | if (pcb->flags & TF_TIMESTAMP) { |
andrewbonney | 0:ec559500a63f | 848 | optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS); |
andrewbonney | 0:ec559500a63f | 849 | } |
andrewbonney | 0:ec559500a63f | 850 | #endif |
andrewbonney | 0:ec559500a63f | 851 | |
andrewbonney | 0:ec559500a63f | 852 | p = tcp_output_alloc_header(pcb, optlen, 0, htonl(pcb->snd_nxt)); |
andrewbonney | 0:ec559500a63f | 853 | if (p == NULL) { |
andrewbonney | 0:ec559500a63f | 854 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n")); |
andrewbonney | 0:ec559500a63f | 855 | return ERR_BUF; |
andrewbonney | 0:ec559500a63f | 856 | } |
andrewbonney | 0:ec559500a63f | 857 | tcphdr = (struct tcp_hdr *)p->payload; |
andrewbonney | 0:ec559500a63f | 858 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, |
andrewbonney | 0:ec559500a63f | 859 | ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt)); |
andrewbonney | 0:ec559500a63f | 860 | /* remove ACK flags from the PCB, as we send an empty ACK now */ |
andrewbonney | 0:ec559500a63f | 861 | pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW); |
andrewbonney | 0:ec559500a63f | 862 | |
andrewbonney | 0:ec559500a63f | 863 | /* NB. MSS option is only sent on SYNs, so ignore it here */ |
andrewbonney | 0:ec559500a63f | 864 | #if LWIP_TCP_TIMESTAMPS |
andrewbonney | 0:ec559500a63f | 865 | pcb->ts_lastacksent = pcb->rcv_nxt; |
andrewbonney | 0:ec559500a63f | 866 | |
andrewbonney | 0:ec559500a63f | 867 | if (pcb->flags & TF_TIMESTAMP) { |
andrewbonney | 0:ec559500a63f | 868 | tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1)); |
andrewbonney | 0:ec559500a63f | 869 | } |
andrewbonney | 0:ec559500a63f | 870 | #endif |
andrewbonney | 0:ec559500a63f | 871 | |
andrewbonney | 0:ec559500a63f | 872 | #if CHECKSUM_GEN_TCP |
andrewbonney | 0:ec559500a63f | 873 | tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip), |
andrewbonney | 0:ec559500a63f | 874 | IP_PROTO_TCP, p->tot_len); |
andrewbonney | 0:ec559500a63f | 875 | #endif |
andrewbonney | 0:ec559500a63f | 876 | #if LWIP_NETIF_HWADDRHINT |
andrewbonney | 0:ec559500a63f | 877 | ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos, |
andrewbonney | 0:ec559500a63f | 878 | IP_PROTO_TCP, &(pcb->addr_hint)); |
andrewbonney | 0:ec559500a63f | 879 | #else /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 880 | ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos, |
andrewbonney | 0:ec559500a63f | 881 | IP_PROTO_TCP); |
andrewbonney | 0:ec559500a63f | 882 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 883 | pbuf_free(p); |
andrewbonney | 0:ec559500a63f | 884 | |
andrewbonney | 0:ec559500a63f | 885 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 886 | } |
andrewbonney | 0:ec559500a63f | 887 | |
andrewbonney | 0:ec559500a63f | 888 | /** |
andrewbonney | 0:ec559500a63f | 889 | * Find out what we can send and send it |
andrewbonney | 0:ec559500a63f | 890 | * |
andrewbonney | 0:ec559500a63f | 891 | * @param pcb Protocol control block for the TCP connection to send data |
andrewbonney | 0:ec559500a63f | 892 | * @return ERR_OK if data has been sent or nothing to send |
andrewbonney | 0:ec559500a63f | 893 | * another err_t on error |
andrewbonney | 0:ec559500a63f | 894 | */ |
andrewbonney | 0:ec559500a63f | 895 | err_t |
andrewbonney | 0:ec559500a63f | 896 | tcp_output(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 897 | { |
andrewbonney | 0:ec559500a63f | 898 | struct tcp_seg *seg, *useg; |
andrewbonney | 0:ec559500a63f | 899 | u32_t wnd, snd_nxt; |
andrewbonney | 0:ec559500a63f | 900 | #if TCP_CWND_DEBUG |
andrewbonney | 0:ec559500a63f | 901 | s16_t i = 0; |
andrewbonney | 0:ec559500a63f | 902 | #endif /* TCP_CWND_DEBUG */ |
andrewbonney | 0:ec559500a63f | 903 | |
andrewbonney | 0:ec559500a63f | 904 | /* First, check if we are invoked by the TCP input processing |
andrewbonney | 0:ec559500a63f | 905 | code. If so, we do not output anything. Instead, we rely on the |
andrewbonney | 0:ec559500a63f | 906 | input processing code to call us when input processing is done |
andrewbonney | 0:ec559500a63f | 907 | with. */ |
andrewbonney | 0:ec559500a63f | 908 | if (tcp_input_pcb == pcb) { |
andrewbonney | 0:ec559500a63f | 909 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 910 | } |
andrewbonney | 0:ec559500a63f | 911 | |
andrewbonney | 0:ec559500a63f | 912 | wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd); |
andrewbonney | 0:ec559500a63f | 913 | |
andrewbonney | 0:ec559500a63f | 914 | seg = pcb->unsent; |
andrewbonney | 0:ec559500a63f | 915 | |
andrewbonney | 0:ec559500a63f | 916 | /* If the TF_ACK_NOW flag is set and no data will be sent (either |
andrewbonney | 0:ec559500a63f | 917 | * because the ->unsent queue is empty or because the window does |
andrewbonney | 0:ec559500a63f | 918 | * not allow it), construct an empty ACK segment and send it. |
andrewbonney | 0:ec559500a63f | 919 | * |
andrewbonney | 0:ec559500a63f | 920 | * If data is to be sent, we will just piggyback the ACK (see below). |
andrewbonney | 0:ec559500a63f | 921 | */ |
andrewbonney | 0:ec559500a63f | 922 | if (pcb->flags & TF_ACK_NOW && |
andrewbonney | 0:ec559500a63f | 923 | (seg == NULL || |
andrewbonney | 0:ec559500a63f | 924 | ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) { |
andrewbonney | 0:ec559500a63f | 925 | return tcp_send_empty_ack(pcb); |
andrewbonney | 0:ec559500a63f | 926 | } |
andrewbonney | 0:ec559500a63f | 927 | |
andrewbonney | 0:ec559500a63f | 928 | /* useg should point to last segment on unacked queue */ |
andrewbonney | 0:ec559500a63f | 929 | useg = pcb->unacked; |
andrewbonney | 0:ec559500a63f | 930 | if (useg != NULL) { |
andrewbonney | 0:ec559500a63f | 931 | for (; useg->next != NULL; useg = useg->next); |
andrewbonney | 0:ec559500a63f | 932 | } |
andrewbonney | 0:ec559500a63f | 933 | |
andrewbonney | 0:ec559500a63f | 934 | #if TCP_OUTPUT_DEBUG |
andrewbonney | 0:ec559500a63f | 935 | if (seg == NULL) { |
andrewbonney | 0:ec559500a63f | 936 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n", |
andrewbonney | 0:ec559500a63f | 937 | (void*)pcb->unsent)); |
andrewbonney | 0:ec559500a63f | 938 | } |
andrewbonney | 0:ec559500a63f | 939 | #endif /* TCP_OUTPUT_DEBUG */ |
andrewbonney | 0:ec559500a63f | 940 | #if TCP_CWND_DEBUG |
andrewbonney | 0:ec559500a63f | 941 | if (seg == NULL) { |
andrewbonney | 0:ec559500a63f | 942 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F |
andrewbonney | 0:ec559500a63f | 943 | ", cwnd %"U16_F", wnd %"U32_F |
andrewbonney | 0:ec559500a63f | 944 | ", seg == NULL, ack %"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 945 | pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack)); |
andrewbonney | 0:ec559500a63f | 946 | } else { |
andrewbonney | 0:ec559500a63f | 947 | LWIP_DEBUGF(TCP_CWND_DEBUG, |
andrewbonney | 0:ec559500a63f | 948 | ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F |
andrewbonney | 0:ec559500a63f | 949 | ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 950 | pcb->snd_wnd, pcb->cwnd, wnd, |
andrewbonney | 0:ec559500a63f | 951 | ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len, |
andrewbonney | 0:ec559500a63f | 952 | ntohl(seg->tcphdr->seqno), pcb->lastack)); |
andrewbonney | 0:ec559500a63f | 953 | } |
andrewbonney | 0:ec559500a63f | 954 | #endif /* TCP_CWND_DEBUG */ |
andrewbonney | 0:ec559500a63f | 955 | /* data available and window allows it to be sent? */ |
andrewbonney | 0:ec559500a63f | 956 | while (seg != NULL && |
andrewbonney | 0:ec559500a63f | 957 | ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) { |
andrewbonney | 0:ec559500a63f | 958 | LWIP_ASSERT("RST not expected here!", |
andrewbonney | 0:ec559500a63f | 959 | (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0); |
andrewbonney | 0:ec559500a63f | 960 | /* Stop sending if the nagle algorithm would prevent it |
andrewbonney | 0:ec559500a63f | 961 | * Don't stop: |
andrewbonney | 0:ec559500a63f | 962 | * - if tcp_write had a memory error before (prevent delayed ACK timeout) or |
andrewbonney | 0:ec559500a63f | 963 | * - if FIN was already enqueued for this PCB (SYN is always alone in a segment - |
andrewbonney | 0:ec559500a63f | 964 | * either seg->next != NULL or pcb->unacked == NULL; |
andrewbonney | 0:ec559500a63f | 965 | * RST is no sent using tcp_write/tcp_output. |
andrewbonney | 0:ec559500a63f | 966 | */ |
andrewbonney | 0:ec559500a63f | 967 | if((tcp_do_output_nagle(pcb) == 0) && |
andrewbonney | 0:ec559500a63f | 968 | ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){ |
andrewbonney | 0:ec559500a63f | 969 | break; |
andrewbonney | 0:ec559500a63f | 970 | } |
andrewbonney | 0:ec559500a63f | 971 | #if TCP_CWND_DEBUG |
andrewbonney | 0:ec559500a63f | 972 | LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n", |
andrewbonney | 0:ec559500a63f | 973 | pcb->snd_wnd, pcb->cwnd, wnd, |
andrewbonney | 0:ec559500a63f | 974 | ntohl(seg->tcphdr->seqno) + seg->len - |
andrewbonney | 0:ec559500a63f | 975 | pcb->lastack, |
andrewbonney | 0:ec559500a63f | 976 | ntohl(seg->tcphdr->seqno), pcb->lastack, i)); |
andrewbonney | 0:ec559500a63f | 977 | ++i; |
andrewbonney | 0:ec559500a63f | 978 | #endif /* TCP_CWND_DEBUG */ |
andrewbonney | 0:ec559500a63f | 979 | |
andrewbonney | 0:ec559500a63f | 980 | pcb->unsent = seg->next; |
andrewbonney | 0:ec559500a63f | 981 | |
andrewbonney | 0:ec559500a63f | 982 | if (pcb->state != SYN_SENT) { |
andrewbonney | 0:ec559500a63f | 983 | TCPH_SET_FLAG(seg->tcphdr, TCP_ACK); |
andrewbonney | 0:ec559500a63f | 984 | pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW); |
andrewbonney | 0:ec559500a63f | 985 | } |
andrewbonney | 0:ec559500a63f | 986 | |
andrewbonney | 0:ec559500a63f | 987 | tcp_output_segment(seg, pcb); |
andrewbonney | 0:ec559500a63f | 988 | |
andrewbonney | 0:ec559500a63f | 989 | snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg); |
andrewbonney | 0:ec559500a63f | 990 | |
andrewbonney | 0:ec559500a63f | 991 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: pcb->snd_nxt was %"U32_F" seqno=%"U32_F" len=%"U32_F" new snd_nxt=%"U32_F"\n", //DG |
andrewbonney | 0:ec559500a63f | 992 | pcb->snd_nxt, ntohl(seg->tcphdr->seqno), TCP_TCPLEN(seg), snd_nxt)); |
andrewbonney | 0:ec559500a63f | 993 | |
andrewbonney | 0:ec559500a63f | 994 | if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) { |
andrewbonney | 0:ec559500a63f | 995 | pcb->snd_nxt = snd_nxt; |
andrewbonney | 0:ec559500a63f | 996 | } |
andrewbonney | 0:ec559500a63f | 997 | /* put segment on unacknowledged list if length > 0 */ |
andrewbonney | 0:ec559500a63f | 998 | if (TCP_TCPLEN(seg) > 0) { |
andrewbonney | 0:ec559500a63f | 999 | seg->next = NULL; |
andrewbonney | 0:ec559500a63f | 1000 | /* unacked list is empty? */ |
andrewbonney | 0:ec559500a63f | 1001 | if (pcb->unacked == NULL) { |
andrewbonney | 0:ec559500a63f | 1002 | pcb->unacked = seg; |
andrewbonney | 0:ec559500a63f | 1003 | useg = seg; |
andrewbonney | 0:ec559500a63f | 1004 | /* unacked list is not empty? */ |
andrewbonney | 0:ec559500a63f | 1005 | } else { |
andrewbonney | 0:ec559500a63f | 1006 | /* In the case of fast retransmit, the packet should not go to the tail |
andrewbonney | 0:ec559500a63f | 1007 | * of the unacked queue, but rather somewhere before it. We need to check for |
andrewbonney | 0:ec559500a63f | 1008 | * this case. -STJ Jul 27, 2004 */ |
andrewbonney | 0:ec559500a63f | 1009 | if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) { |
andrewbonney | 0:ec559500a63f | 1010 | /* add segment to before tail of unacked list, keeping the list sorted */ |
andrewbonney | 0:ec559500a63f | 1011 | struct tcp_seg **cur_seg = &(pcb->unacked); |
andrewbonney | 0:ec559500a63f | 1012 | while (*cur_seg && |
andrewbonney | 0:ec559500a63f | 1013 | TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) { |
andrewbonney | 0:ec559500a63f | 1014 | cur_seg = &((*cur_seg)->next ); |
andrewbonney | 0:ec559500a63f | 1015 | } |
andrewbonney | 0:ec559500a63f | 1016 | seg->next = (*cur_seg); |
andrewbonney | 0:ec559500a63f | 1017 | (*cur_seg) = seg; |
andrewbonney | 0:ec559500a63f | 1018 | } else { |
andrewbonney | 0:ec559500a63f | 1019 | /* add segment to tail of unacked list */ |
andrewbonney | 0:ec559500a63f | 1020 | useg->next = seg; |
andrewbonney | 0:ec559500a63f | 1021 | useg = useg->next; |
andrewbonney | 0:ec559500a63f | 1022 | } |
andrewbonney | 0:ec559500a63f | 1023 | } |
andrewbonney | 0:ec559500a63f | 1024 | /* do not queue empty segments on the unacked list */ |
andrewbonney | 0:ec559500a63f | 1025 | } else { |
andrewbonney | 0:ec559500a63f | 1026 | tcp_seg_free(seg); |
andrewbonney | 0:ec559500a63f | 1027 | } |
andrewbonney | 0:ec559500a63f | 1028 | seg = pcb->unsent; |
andrewbonney | 0:ec559500a63f | 1029 | } |
andrewbonney | 0:ec559500a63f | 1030 | #if TCP_OVERSIZE |
andrewbonney | 0:ec559500a63f | 1031 | if (pcb->unsent == NULL) { |
andrewbonney | 0:ec559500a63f | 1032 | /* last unsent has been removed, reset unsent_oversize */ |
andrewbonney | 0:ec559500a63f | 1033 | pcb->unsent_oversize = 0; |
andrewbonney | 0:ec559500a63f | 1034 | } |
andrewbonney | 0:ec559500a63f | 1035 | #endif /* TCP_OVERSIZE */ |
andrewbonney | 0:ec559500a63f | 1036 | |
andrewbonney | 0:ec559500a63f | 1037 | if (seg != NULL && pcb->persist_backoff == 0 && |
andrewbonney | 0:ec559500a63f | 1038 | ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) { |
andrewbonney | 0:ec559500a63f | 1039 | /* prepare for persist timer */ |
andrewbonney | 0:ec559500a63f | 1040 | pcb->persist_cnt = 0; |
andrewbonney | 0:ec559500a63f | 1041 | pcb->persist_backoff = 1; |
andrewbonney | 0:ec559500a63f | 1042 | } |
andrewbonney | 0:ec559500a63f | 1043 | |
andrewbonney | 0:ec559500a63f | 1044 | pcb->flags &= ~TF_NAGLEMEMERR; |
andrewbonney | 0:ec559500a63f | 1045 | return ERR_OK; |
andrewbonney | 0:ec559500a63f | 1046 | } |
andrewbonney | 0:ec559500a63f | 1047 | |
andrewbonney | 0:ec559500a63f | 1048 | /** |
andrewbonney | 0:ec559500a63f | 1049 | * Called by tcp_output() to actually send a TCP segment over IP. |
andrewbonney | 0:ec559500a63f | 1050 | * |
andrewbonney | 0:ec559500a63f | 1051 | * @param seg the tcp_seg to send |
andrewbonney | 0:ec559500a63f | 1052 | * @param pcb the tcp_pcb for the TCP connection used to send the segment |
andrewbonney | 0:ec559500a63f | 1053 | */ |
andrewbonney | 0:ec559500a63f | 1054 | static void |
andrewbonney | 0:ec559500a63f | 1055 | tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 1056 | { |
andrewbonney | 0:ec559500a63f | 1057 | u16_t len; |
andrewbonney | 0:ec559500a63f | 1058 | struct netif *netif; |
andrewbonney | 0:ec559500a63f | 1059 | u32_t *opts; |
andrewbonney | 0:ec559500a63f | 1060 | |
andrewbonney | 0:ec559500a63f | 1061 | /** @bug Exclude retransmitted segments from this count. */ |
andrewbonney | 0:ec559500a63f | 1062 | snmp_inc_tcpoutsegs(); |
andrewbonney | 0:ec559500a63f | 1063 | |
andrewbonney | 0:ec559500a63f | 1064 | /* The TCP header has already been constructed, but the ackno and |
andrewbonney | 0:ec559500a63f | 1065 | wnd fields remain. */ |
andrewbonney | 0:ec559500a63f | 1066 | seg->tcphdr->ackno = htonl(pcb->rcv_nxt); |
andrewbonney | 0:ec559500a63f | 1067 | |
andrewbonney | 0:ec559500a63f | 1068 | /* advertise our receive window size in this TCP segment */ |
andrewbonney | 0:ec559500a63f | 1069 | seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd); |
andrewbonney | 0:ec559500a63f | 1070 | |
andrewbonney | 0:ec559500a63f | 1071 | pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd; |
andrewbonney | 0:ec559500a63f | 1072 | |
andrewbonney | 0:ec559500a63f | 1073 | /* Add any requested options. NB MSS option is only set on SYN |
andrewbonney | 0:ec559500a63f | 1074 | packets, so ignore it here */ |
andrewbonney | 0:ec559500a63f | 1075 | LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)(seg->tcphdr + 1) % 4) == 0); |
andrewbonney | 0:ec559500a63f | 1076 | opts = (u32_t *)(void *)(seg->tcphdr + 1); |
andrewbonney | 0:ec559500a63f | 1077 | if (seg->flags & TF_SEG_OPTS_MSS) { |
andrewbonney | 0:ec559500a63f | 1078 | TCP_BUILD_MSS_OPTION(*opts); |
andrewbonney | 0:ec559500a63f | 1079 | opts += 1; |
andrewbonney | 0:ec559500a63f | 1080 | } |
andrewbonney | 0:ec559500a63f | 1081 | #if LWIP_TCP_TIMESTAMPS |
andrewbonney | 0:ec559500a63f | 1082 | pcb->ts_lastacksent = pcb->rcv_nxt; |
andrewbonney | 0:ec559500a63f | 1083 | |
andrewbonney | 0:ec559500a63f | 1084 | if (seg->flags & TF_SEG_OPTS_TS) { |
andrewbonney | 0:ec559500a63f | 1085 | tcp_build_timestamp_option(pcb, opts); |
andrewbonney | 0:ec559500a63f | 1086 | opts += 3; |
andrewbonney | 0:ec559500a63f | 1087 | } |
andrewbonney | 0:ec559500a63f | 1088 | #endif |
andrewbonney | 0:ec559500a63f | 1089 | |
andrewbonney | 0:ec559500a63f | 1090 | /* If we don't have a local IP address, we get one by |
andrewbonney | 0:ec559500a63f | 1091 | calling ip_route(). */ |
andrewbonney | 0:ec559500a63f | 1092 | if (ip_addr_isany(&(pcb->local_ip))) { |
andrewbonney | 0:ec559500a63f | 1093 | netif = ip_route(&(pcb->remote_ip)); |
andrewbonney | 0:ec559500a63f | 1094 | if (netif == NULL) { |
andrewbonney | 0:ec559500a63f | 1095 | return; |
andrewbonney | 0:ec559500a63f | 1096 | } |
andrewbonney | 0:ec559500a63f | 1097 | ip_addr_copy(pcb->local_ip, netif->ip_addr); |
andrewbonney | 0:ec559500a63f | 1098 | } |
andrewbonney | 0:ec559500a63f | 1099 | |
andrewbonney | 0:ec559500a63f | 1100 | /* Set retransmission timer running if it is not currently enabled */ |
andrewbonney | 0:ec559500a63f | 1101 | if(pcb->rtime == -1) { |
andrewbonney | 0:ec559500a63f | 1102 | pcb->rtime = 0; |
andrewbonney | 0:ec559500a63f | 1103 | } |
andrewbonney | 0:ec559500a63f | 1104 | |
andrewbonney | 0:ec559500a63f | 1105 | if (pcb->rttest == 0) { |
andrewbonney | 0:ec559500a63f | 1106 | pcb->rttest = tcp_ticks; |
andrewbonney | 0:ec559500a63f | 1107 | pcb->rtseq = ntohl(seg->tcphdr->seqno); |
andrewbonney | 0:ec559500a63f | 1108 | |
andrewbonney | 0:ec559500a63f | 1109 | LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq)); |
andrewbonney | 0:ec559500a63f | 1110 | } |
andrewbonney | 0:ec559500a63f | 1111 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 1112 | htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) + |
andrewbonney | 0:ec559500a63f | 1113 | seg->len)); |
andrewbonney | 0:ec559500a63f | 1114 | |
andrewbonney | 0:ec559500a63f | 1115 | len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload); |
andrewbonney | 0:ec559500a63f | 1116 | |
andrewbonney | 0:ec559500a63f | 1117 | seg->p->len -= len; |
andrewbonney | 0:ec559500a63f | 1118 | seg->p->tot_len -= len; |
andrewbonney | 0:ec559500a63f | 1119 | |
andrewbonney | 0:ec559500a63f | 1120 | seg->p->payload = seg->tcphdr; |
andrewbonney | 0:ec559500a63f | 1121 | |
andrewbonney | 0:ec559500a63f | 1122 | seg->tcphdr->chksum = 0; |
andrewbonney | 0:ec559500a63f | 1123 | #if CHECKSUM_GEN_TCP |
andrewbonney | 0:ec559500a63f | 1124 | #if TCP_CHECKSUM_ON_COPY |
andrewbonney | 0:ec559500a63f | 1125 | { |
andrewbonney | 0:ec559500a63f | 1126 | u32_t acc; |
andrewbonney | 0:ec559500a63f | 1127 | #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK |
andrewbonney | 0:ec559500a63f | 1128 | u16_t chksum_slow = inet_chksum_pseudo(seg->p, &(pcb->local_ip), |
andrewbonney | 0:ec559500a63f | 1129 | &(pcb->remote_ip), |
andrewbonney | 0:ec559500a63f | 1130 | IP_PROTO_TCP, seg->p->tot_len); |
andrewbonney | 0:ec559500a63f | 1131 | #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */ |
andrewbonney | 0:ec559500a63f | 1132 | if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) { |
andrewbonney | 0:ec559500a63f | 1133 | LWIP_ASSERT("data included but not checksummed", |
andrewbonney | 0:ec559500a63f | 1134 | seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4)); |
andrewbonney | 0:ec559500a63f | 1135 | } |
andrewbonney | 0:ec559500a63f | 1136 | |
andrewbonney | 0:ec559500a63f | 1137 | /* rebuild TCP header checksum (TCP header changes for retransmissions!) */ |
andrewbonney | 0:ec559500a63f | 1138 | acc = inet_chksum_pseudo_partial(seg->p, &(pcb->local_ip), |
andrewbonney | 0:ec559500a63f | 1139 | &(pcb->remote_ip), |
andrewbonney | 0:ec559500a63f | 1140 | IP_PROTO_TCP, seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4); |
andrewbonney | 0:ec559500a63f | 1141 | /* add payload checksum */ |
andrewbonney | 0:ec559500a63f | 1142 | if (seg->chksum_swapped) { |
andrewbonney | 0:ec559500a63f | 1143 | seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum); |
andrewbonney | 0:ec559500a63f | 1144 | seg->chksum_swapped = 0; |
andrewbonney | 0:ec559500a63f | 1145 | } |
andrewbonney | 0:ec559500a63f | 1146 | acc += (u16_t)~(seg->chksum); |
andrewbonney | 0:ec559500a63f | 1147 | seg->tcphdr->chksum = FOLD_U32T(acc); |
andrewbonney | 0:ec559500a63f | 1148 | #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK |
andrewbonney | 0:ec559500a63f | 1149 | if (chksum_slow != seg->tcphdr->chksum) { |
andrewbonney | 0:ec559500a63f | 1150 | LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING, |
andrewbonney | 0:ec559500a63f | 1151 | ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n", |
andrewbonney | 0:ec559500a63f | 1152 | seg->tcphdr->chksum, chksum_slow)); |
andrewbonney | 0:ec559500a63f | 1153 | seg->tcphdr->chksum = chksum_slow; |
andrewbonney | 0:ec559500a63f | 1154 | } |
andrewbonney | 0:ec559500a63f | 1155 | #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */ |
andrewbonney | 0:ec559500a63f | 1156 | } |
andrewbonney | 0:ec559500a63f | 1157 | #else /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 1158 | seg->tcphdr->chksum = inet_chksum_pseudo(seg->p, &(pcb->local_ip), |
andrewbonney | 0:ec559500a63f | 1159 | &(pcb->remote_ip), |
andrewbonney | 0:ec559500a63f | 1160 | IP_PROTO_TCP, seg->p->tot_len); |
andrewbonney | 0:ec559500a63f | 1161 | #endif /* TCP_CHECKSUM_ON_COPY */ |
andrewbonney | 0:ec559500a63f | 1162 | #endif /* CHECKSUM_GEN_TCP */ |
andrewbonney | 0:ec559500a63f | 1163 | TCP_STATS_INC(tcp.xmit); |
andrewbonney | 0:ec559500a63f | 1164 | |
andrewbonney | 0:ec559500a63f | 1165 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: Before ip_out seqno = %"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 1166 | htonl(seg->tcphdr->seqno))); //DG |
andrewbonney | 0:ec559500a63f | 1167 | |
andrewbonney | 0:ec559500a63f | 1168 | #if LWIP_NETIF_HWADDRHINT |
andrewbonney | 0:ec559500a63f | 1169 | ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos, |
andrewbonney | 0:ec559500a63f | 1170 | IP_PROTO_TCP, &(pcb->addr_hint)); |
andrewbonney | 0:ec559500a63f | 1171 | #else /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 1172 | ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos, |
andrewbonney | 0:ec559500a63f | 1173 | IP_PROTO_TCP); |
andrewbonney | 0:ec559500a63f | 1174 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 1175 | |
andrewbonney | 0:ec559500a63f | 1176 | LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: After ip_out seqno = %"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 1177 | htonl(seg->tcphdr->seqno))); //DG |
andrewbonney | 0:ec559500a63f | 1178 | |
andrewbonney | 0:ec559500a63f | 1179 | |
andrewbonney | 0:ec559500a63f | 1180 | } |
andrewbonney | 0:ec559500a63f | 1181 | |
andrewbonney | 0:ec559500a63f | 1182 | /** |
andrewbonney | 0:ec559500a63f | 1183 | * Send a TCP RESET packet (empty segment with RST flag set) either to |
andrewbonney | 0:ec559500a63f | 1184 | * abort a connection or to show that there is no matching local connection |
andrewbonney | 0:ec559500a63f | 1185 | * for a received segment. |
andrewbonney | 0:ec559500a63f | 1186 | * |
andrewbonney | 0:ec559500a63f | 1187 | * Called by tcp_abort() (to abort a local connection), tcp_input() (if no |
andrewbonney | 0:ec559500a63f | 1188 | * matching local pcb was found), tcp_listen_input() (if incoming segment |
andrewbonney | 0:ec559500a63f | 1189 | * has ACK flag set) and tcp_process() (received segment in the wrong state) |
andrewbonney | 0:ec559500a63f | 1190 | * |
andrewbonney | 0:ec559500a63f | 1191 | * Since a RST segment is in most cases not sent for an active connection, |
andrewbonney | 0:ec559500a63f | 1192 | * tcp_rst() has a number of arguments that are taken from a tcp_pcb for |
andrewbonney | 0:ec559500a63f | 1193 | * most other segment output functions. |
andrewbonney | 0:ec559500a63f | 1194 | * |
andrewbonney | 0:ec559500a63f | 1195 | * @param seqno the sequence number to use for the outgoing segment |
andrewbonney | 0:ec559500a63f | 1196 | * @param ackno the acknowledge number to use for the outgoing segment |
andrewbonney | 0:ec559500a63f | 1197 | * @param local_ip the local IP address to send the segment from |
andrewbonney | 0:ec559500a63f | 1198 | * @param remote_ip the remote IP address to send the segment to |
andrewbonney | 0:ec559500a63f | 1199 | * @param local_port the local TCP port to send the segment from |
andrewbonney | 0:ec559500a63f | 1200 | * @param remote_port the remote TCP port to send the segment to |
andrewbonney | 0:ec559500a63f | 1201 | */ |
andrewbonney | 0:ec559500a63f | 1202 | void |
andrewbonney | 0:ec559500a63f | 1203 | tcp_rst(u32_t seqno, u32_t ackno, |
andrewbonney | 0:ec559500a63f | 1204 | ip_addr_t *local_ip, ip_addr_t *remote_ip, |
andrewbonney | 0:ec559500a63f | 1205 | u16_t local_port, u16_t remote_port) |
andrewbonney | 0:ec559500a63f | 1206 | { |
andrewbonney | 0:ec559500a63f | 1207 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 1208 | struct tcp_hdr *tcphdr; |
andrewbonney | 0:ec559500a63f | 1209 | p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM); |
andrewbonney | 0:ec559500a63f | 1210 | if (p == NULL) { |
andrewbonney | 0:ec559500a63f | 1211 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n")); |
andrewbonney | 0:ec559500a63f | 1212 | return; |
andrewbonney | 0:ec559500a63f | 1213 | } |
andrewbonney | 0:ec559500a63f | 1214 | LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr", |
andrewbonney | 0:ec559500a63f | 1215 | (p->len >= sizeof(struct tcp_hdr))); |
andrewbonney | 0:ec559500a63f | 1216 | |
andrewbonney | 0:ec559500a63f | 1217 | tcphdr = (struct tcp_hdr *)p->payload; |
andrewbonney | 0:ec559500a63f | 1218 | tcphdr->src = htons(local_port); |
andrewbonney | 0:ec559500a63f | 1219 | tcphdr->dest = htons(remote_port); |
andrewbonney | 0:ec559500a63f | 1220 | tcphdr->seqno = htonl(seqno); |
andrewbonney | 0:ec559500a63f | 1221 | tcphdr->ackno = htonl(ackno); |
andrewbonney | 0:ec559500a63f | 1222 | TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK); |
andrewbonney | 0:ec559500a63f | 1223 | tcphdr->wnd = PP_HTONS(TCP_WND); |
andrewbonney | 0:ec559500a63f | 1224 | tcphdr->chksum = 0; |
andrewbonney | 0:ec559500a63f | 1225 | tcphdr->urgp = 0; |
andrewbonney | 0:ec559500a63f | 1226 | |
andrewbonney | 0:ec559500a63f | 1227 | #if CHECKSUM_GEN_TCP |
andrewbonney | 0:ec559500a63f | 1228 | tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip, |
andrewbonney | 0:ec559500a63f | 1229 | IP_PROTO_TCP, p->tot_len); |
andrewbonney | 0:ec559500a63f | 1230 | #endif |
andrewbonney | 0:ec559500a63f | 1231 | TCP_STATS_INC(tcp.xmit); |
andrewbonney | 0:ec559500a63f | 1232 | snmp_inc_tcpoutrsts(); |
andrewbonney | 0:ec559500a63f | 1233 | /* Send output with hardcoded TTL since we have no access to the pcb */ |
andrewbonney | 0:ec559500a63f | 1234 | ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP); |
andrewbonney | 0:ec559500a63f | 1235 | pbuf_free(p); |
andrewbonney | 0:ec559500a63f | 1236 | LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno)); |
andrewbonney | 0:ec559500a63f | 1237 | } |
andrewbonney | 0:ec559500a63f | 1238 | |
andrewbonney | 0:ec559500a63f | 1239 | /** |
andrewbonney | 0:ec559500a63f | 1240 | * Requeue all unacked segments for retransmission |
andrewbonney | 0:ec559500a63f | 1241 | * |
andrewbonney | 0:ec559500a63f | 1242 | * Called by tcp_slowtmr() for slow retransmission. |
andrewbonney | 0:ec559500a63f | 1243 | * |
andrewbonney | 0:ec559500a63f | 1244 | * @param pcb the tcp_pcb for which to re-enqueue all unacked segments |
andrewbonney | 0:ec559500a63f | 1245 | */ |
andrewbonney | 0:ec559500a63f | 1246 | void |
andrewbonney | 0:ec559500a63f | 1247 | tcp_rexmit_rto(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 1248 | { |
andrewbonney | 0:ec559500a63f | 1249 | struct tcp_seg *seg; |
andrewbonney | 0:ec559500a63f | 1250 | |
andrewbonney | 0:ec559500a63f | 1251 | if (pcb->unacked == NULL) { |
andrewbonney | 0:ec559500a63f | 1252 | return; |
andrewbonney | 0:ec559500a63f | 1253 | } |
andrewbonney | 0:ec559500a63f | 1254 | |
andrewbonney | 0:ec559500a63f | 1255 | /* Move all unacked segments to the head of the unsent queue */ |
andrewbonney | 0:ec559500a63f | 1256 | for (seg = pcb->unacked; seg->next != NULL; seg = seg->next); |
andrewbonney | 0:ec559500a63f | 1257 | /* concatenate unsent queue after unacked queue */ |
andrewbonney | 0:ec559500a63f | 1258 | seg->next = pcb->unsent; |
andrewbonney | 0:ec559500a63f | 1259 | /* unsent queue is the concatenated queue (of unacked, unsent) */ |
andrewbonney | 0:ec559500a63f | 1260 | pcb->unsent = pcb->unacked; |
andrewbonney | 0:ec559500a63f | 1261 | /* unacked queue is now empty */ |
andrewbonney | 0:ec559500a63f | 1262 | pcb->unacked = NULL; |
andrewbonney | 0:ec559500a63f | 1263 | |
andrewbonney | 0:ec559500a63f | 1264 | /* increment number of retransmissions */ |
andrewbonney | 0:ec559500a63f | 1265 | ++pcb->nrtx; |
andrewbonney | 0:ec559500a63f | 1266 | |
andrewbonney | 0:ec559500a63f | 1267 | /* Don't take any RTT measurements after retransmitting. */ |
andrewbonney | 0:ec559500a63f | 1268 | pcb->rttest = 0; |
andrewbonney | 0:ec559500a63f | 1269 | |
andrewbonney | 0:ec559500a63f | 1270 | /* Do the actual retransmission */ |
andrewbonney | 0:ec559500a63f | 1271 | tcp_output(pcb); |
andrewbonney | 0:ec559500a63f | 1272 | } |
andrewbonney | 0:ec559500a63f | 1273 | |
andrewbonney | 0:ec559500a63f | 1274 | /** |
andrewbonney | 0:ec559500a63f | 1275 | * Requeue the first unacked segment for retransmission |
andrewbonney | 0:ec559500a63f | 1276 | * |
andrewbonney | 0:ec559500a63f | 1277 | * Called by tcp_receive() for fast retramsmit. |
andrewbonney | 0:ec559500a63f | 1278 | * |
andrewbonney | 0:ec559500a63f | 1279 | * @param pcb the tcp_pcb for which to retransmit the first unacked segment |
andrewbonney | 0:ec559500a63f | 1280 | */ |
andrewbonney | 0:ec559500a63f | 1281 | void |
andrewbonney | 0:ec559500a63f | 1282 | tcp_rexmit(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 1283 | { |
andrewbonney | 0:ec559500a63f | 1284 | struct tcp_seg *seg; |
andrewbonney | 0:ec559500a63f | 1285 | struct tcp_seg **cur_seg; |
andrewbonney | 0:ec559500a63f | 1286 | |
andrewbonney | 0:ec559500a63f | 1287 | if (pcb->unacked == NULL) { |
andrewbonney | 0:ec559500a63f | 1288 | return; |
andrewbonney | 0:ec559500a63f | 1289 | } |
andrewbonney | 0:ec559500a63f | 1290 | |
andrewbonney | 0:ec559500a63f | 1291 | /* Move the first unacked segment to the unsent queue */ |
andrewbonney | 0:ec559500a63f | 1292 | /* Keep the unsent queue sorted. */ |
andrewbonney | 0:ec559500a63f | 1293 | seg = pcb->unacked; |
andrewbonney | 0:ec559500a63f | 1294 | pcb->unacked = seg->next; |
andrewbonney | 0:ec559500a63f | 1295 | |
andrewbonney | 0:ec559500a63f | 1296 | cur_seg = &(pcb->unsent); |
andrewbonney | 0:ec559500a63f | 1297 | while (*cur_seg && |
andrewbonney | 0:ec559500a63f | 1298 | TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) { |
andrewbonney | 0:ec559500a63f | 1299 | cur_seg = &((*cur_seg)->next ); |
andrewbonney | 0:ec559500a63f | 1300 | } |
andrewbonney | 0:ec559500a63f | 1301 | seg->next = *cur_seg; |
andrewbonney | 0:ec559500a63f | 1302 | *cur_seg = seg; |
andrewbonney | 0:ec559500a63f | 1303 | |
andrewbonney | 0:ec559500a63f | 1304 | ++pcb->nrtx; |
andrewbonney | 0:ec559500a63f | 1305 | |
andrewbonney | 0:ec559500a63f | 1306 | /* Don't take any rtt measurements after retransmitting. */ |
andrewbonney | 0:ec559500a63f | 1307 | pcb->rttest = 0; |
andrewbonney | 0:ec559500a63f | 1308 | |
andrewbonney | 0:ec559500a63f | 1309 | /* Do the actual retransmission. */ |
andrewbonney | 0:ec559500a63f | 1310 | snmp_inc_tcpretranssegs(); |
andrewbonney | 0:ec559500a63f | 1311 | /* No need to call tcp_output: we are always called from tcp_input() |
andrewbonney | 0:ec559500a63f | 1312 | and thus tcp_output directly returns. */ |
andrewbonney | 0:ec559500a63f | 1313 | } |
andrewbonney | 0:ec559500a63f | 1314 | |
andrewbonney | 0:ec559500a63f | 1315 | |
andrewbonney | 0:ec559500a63f | 1316 | /** |
andrewbonney | 0:ec559500a63f | 1317 | * Handle retransmission after three dupacks received |
andrewbonney | 0:ec559500a63f | 1318 | * |
andrewbonney | 0:ec559500a63f | 1319 | * @param pcb the tcp_pcb for which to retransmit the first unacked segment |
andrewbonney | 0:ec559500a63f | 1320 | */ |
andrewbonney | 0:ec559500a63f | 1321 | void |
andrewbonney | 0:ec559500a63f | 1322 | tcp_rexmit_fast(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 1323 | { |
andrewbonney | 0:ec559500a63f | 1324 | if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) { |
andrewbonney | 0:ec559500a63f | 1325 | /* This is fast retransmit. Retransmit the first unacked segment. */ |
andrewbonney | 0:ec559500a63f | 1326 | LWIP_DEBUGF(TCP_FR_DEBUG, |
andrewbonney | 0:ec559500a63f | 1327 | ("tcp_receive: dupacks %"U16_F" (%"U32_F |
andrewbonney | 0:ec559500a63f | 1328 | "), fast retransmit %"U32_F"\n", |
andrewbonney | 0:ec559500a63f | 1329 | (u16_t)pcb->dupacks, pcb->lastack, |
andrewbonney | 0:ec559500a63f | 1330 | ntohl(pcb->unacked->tcphdr->seqno))); |
andrewbonney | 0:ec559500a63f | 1331 | tcp_rexmit(pcb); |
andrewbonney | 0:ec559500a63f | 1332 | |
andrewbonney | 0:ec559500a63f | 1333 | /* Set ssthresh to half of the minimum of the current |
andrewbonney | 0:ec559500a63f | 1334 | * cwnd and the advertised window */ |
andrewbonney | 0:ec559500a63f | 1335 | if (pcb->cwnd > pcb->snd_wnd) { |
andrewbonney | 0:ec559500a63f | 1336 | pcb->ssthresh = pcb->snd_wnd / 2; |
andrewbonney | 0:ec559500a63f | 1337 | } else { |
andrewbonney | 0:ec559500a63f | 1338 | pcb->ssthresh = pcb->cwnd / 2; |
andrewbonney | 0:ec559500a63f | 1339 | } |
andrewbonney | 0:ec559500a63f | 1340 | |
andrewbonney | 0:ec559500a63f | 1341 | /* The minimum value for ssthresh should be 2 MSS */ |
andrewbonney | 0:ec559500a63f | 1342 | if (pcb->ssthresh < 2*pcb->mss) { |
andrewbonney | 0:ec559500a63f | 1343 | LWIP_DEBUGF(TCP_FR_DEBUG, |
andrewbonney | 0:ec559500a63f | 1344 | ("tcp_receive: The minimum value for ssthresh %"U16_F |
andrewbonney | 0:ec559500a63f | 1345 | " should be min 2 mss %"U16_F"...\n", |
andrewbonney | 0:ec559500a63f | 1346 | pcb->ssthresh, 2*pcb->mss)); |
andrewbonney | 0:ec559500a63f | 1347 | pcb->ssthresh = 2*pcb->mss; |
andrewbonney | 0:ec559500a63f | 1348 | } |
andrewbonney | 0:ec559500a63f | 1349 | |
andrewbonney | 0:ec559500a63f | 1350 | pcb->cwnd = pcb->ssthresh + 3 * pcb->mss; |
andrewbonney | 0:ec559500a63f | 1351 | pcb->flags |= TF_INFR; |
andrewbonney | 0:ec559500a63f | 1352 | } |
andrewbonney | 0:ec559500a63f | 1353 | } |
andrewbonney | 0:ec559500a63f | 1354 | |
andrewbonney | 0:ec559500a63f | 1355 | |
andrewbonney | 0:ec559500a63f | 1356 | /** |
andrewbonney | 0:ec559500a63f | 1357 | * Send keepalive packets to keep a connection active although |
andrewbonney | 0:ec559500a63f | 1358 | * no data is sent over it. |
andrewbonney | 0:ec559500a63f | 1359 | * |
andrewbonney | 0:ec559500a63f | 1360 | * Called by tcp_slowtmr() |
andrewbonney | 0:ec559500a63f | 1361 | * |
andrewbonney | 0:ec559500a63f | 1362 | * @param pcb the tcp_pcb for which to send a keepalive packet |
andrewbonney | 0:ec559500a63f | 1363 | */ |
andrewbonney | 0:ec559500a63f | 1364 | void |
andrewbonney | 0:ec559500a63f | 1365 | tcp_keepalive(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 1366 | { |
andrewbonney | 0:ec559500a63f | 1367 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 1368 | struct tcp_hdr *tcphdr; |
andrewbonney | 0:ec559500a63f | 1369 | |
andrewbonney | 0:ec559500a63f | 1370 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
andrewbonney | 0:ec559500a63f | 1371 | ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), |
andrewbonney | 0:ec559500a63f | 1372 | ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip))); |
andrewbonney | 0:ec559500a63f | 1373 | |
andrewbonney | 0:ec559500a63f | 1374 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n", |
andrewbonney | 0:ec559500a63f | 1375 | tcp_ticks, pcb->tmr, pcb->keep_cnt_sent)); |
andrewbonney | 0:ec559500a63f | 1376 | |
andrewbonney | 0:ec559500a63f | 1377 | p = tcp_output_alloc_header(pcb, 0, 0, htonl(pcb->snd_nxt - 1)); |
andrewbonney | 0:ec559500a63f | 1378 | if(p == NULL) { |
andrewbonney | 0:ec559500a63f | 1379 | LWIP_DEBUGF(TCP_DEBUG, |
andrewbonney | 0:ec559500a63f | 1380 | ("tcp_keepalive: could not allocate memory for pbuf\n")); |
andrewbonney | 0:ec559500a63f | 1381 | return; |
andrewbonney | 0:ec559500a63f | 1382 | } |
andrewbonney | 0:ec559500a63f | 1383 | tcphdr = (struct tcp_hdr *)p->payload; |
andrewbonney | 0:ec559500a63f | 1384 | |
andrewbonney | 0:ec559500a63f | 1385 | #if CHECKSUM_GEN_TCP |
andrewbonney | 0:ec559500a63f | 1386 | tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip, |
andrewbonney | 0:ec559500a63f | 1387 | IP_PROTO_TCP, p->tot_len); |
andrewbonney | 0:ec559500a63f | 1388 | #endif |
andrewbonney | 0:ec559500a63f | 1389 | TCP_STATS_INC(tcp.xmit); |
andrewbonney | 0:ec559500a63f | 1390 | |
andrewbonney | 0:ec559500a63f | 1391 | /* Send output to IP */ |
andrewbonney | 0:ec559500a63f | 1392 | #if LWIP_NETIF_HWADDRHINT |
andrewbonney | 0:ec559500a63f | 1393 | ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP, |
andrewbonney | 0:ec559500a63f | 1394 | &(pcb->addr_hint)); |
andrewbonney | 0:ec559500a63f | 1395 | #else /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 1396 | ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP); |
andrewbonney | 0:ec559500a63f | 1397 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 1398 | |
andrewbonney | 0:ec559500a63f | 1399 | pbuf_free(p); |
andrewbonney | 0:ec559500a63f | 1400 | |
andrewbonney | 0:ec559500a63f | 1401 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n", |
andrewbonney | 0:ec559500a63f | 1402 | pcb->snd_nxt - 1, pcb->rcv_nxt)); |
andrewbonney | 0:ec559500a63f | 1403 | } |
andrewbonney | 0:ec559500a63f | 1404 | |
andrewbonney | 0:ec559500a63f | 1405 | |
andrewbonney | 0:ec559500a63f | 1406 | /** |
andrewbonney | 0:ec559500a63f | 1407 | * Send persist timer zero-window probes to keep a connection active |
andrewbonney | 0:ec559500a63f | 1408 | * when a window update is lost. |
andrewbonney | 0:ec559500a63f | 1409 | * |
andrewbonney | 0:ec559500a63f | 1410 | * Called by tcp_slowtmr() |
andrewbonney | 0:ec559500a63f | 1411 | * |
andrewbonney | 0:ec559500a63f | 1412 | * @param pcb the tcp_pcb for which to send a zero-window probe packet |
andrewbonney | 0:ec559500a63f | 1413 | */ |
andrewbonney | 0:ec559500a63f | 1414 | void |
andrewbonney | 0:ec559500a63f | 1415 | tcp_zero_window_probe(struct tcp_pcb *pcb) |
andrewbonney | 0:ec559500a63f | 1416 | { |
andrewbonney | 0:ec559500a63f | 1417 | struct pbuf *p; |
andrewbonney | 0:ec559500a63f | 1418 | struct tcp_hdr *tcphdr; |
andrewbonney | 0:ec559500a63f | 1419 | struct tcp_seg *seg; |
andrewbonney | 0:ec559500a63f | 1420 | u16_t len; |
andrewbonney | 0:ec559500a63f | 1421 | u8_t is_fin; |
andrewbonney | 0:ec559500a63f | 1422 | |
andrewbonney | 0:ec559500a63f | 1423 | LWIP_DEBUGF(TCP_DEBUG, |
andrewbonney | 0:ec559500a63f | 1424 | ("tcp_zero_window_probe: sending ZERO WINDOW probe to %" |
andrewbonney | 0:ec559500a63f | 1425 | U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
andrewbonney | 0:ec559500a63f | 1426 | ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip), |
andrewbonney | 0:ec559500a63f | 1427 | ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip))); |
andrewbonney | 0:ec559500a63f | 1428 | |
andrewbonney | 0:ec559500a63f | 1429 | LWIP_DEBUGF(TCP_DEBUG, |
andrewbonney | 0:ec559500a63f | 1430 | ("tcp_zero_window_probe: tcp_ticks %"U32_F |
andrewbonney | 0:ec559500a63f | 1431 | " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n", |
andrewbonney | 0:ec559500a63f | 1432 | tcp_ticks, pcb->tmr, pcb->keep_cnt_sent)); |
andrewbonney | 0:ec559500a63f | 1433 | |
andrewbonney | 0:ec559500a63f | 1434 | seg = pcb->unacked; |
andrewbonney | 0:ec559500a63f | 1435 | |
andrewbonney | 0:ec559500a63f | 1436 | if(seg == NULL) { |
andrewbonney | 0:ec559500a63f | 1437 | seg = pcb->unsent; |
andrewbonney | 0:ec559500a63f | 1438 | } |
andrewbonney | 0:ec559500a63f | 1439 | if(seg == NULL) { |
andrewbonney | 0:ec559500a63f | 1440 | return; |
andrewbonney | 0:ec559500a63f | 1441 | } |
andrewbonney | 0:ec559500a63f | 1442 | |
andrewbonney | 0:ec559500a63f | 1443 | is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0); |
andrewbonney | 0:ec559500a63f | 1444 | /* we want to send one seqno: either FIN or data (no options) */ |
andrewbonney | 0:ec559500a63f | 1445 | len = is_fin ? 0 : 1; |
andrewbonney | 0:ec559500a63f | 1446 | |
andrewbonney | 0:ec559500a63f | 1447 | p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno); |
andrewbonney | 0:ec559500a63f | 1448 | if(p == NULL) { |
andrewbonney | 0:ec559500a63f | 1449 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n")); |
andrewbonney | 0:ec559500a63f | 1450 | return; |
andrewbonney | 0:ec559500a63f | 1451 | } |
andrewbonney | 0:ec559500a63f | 1452 | tcphdr = (struct tcp_hdr *)p->payload; |
andrewbonney | 0:ec559500a63f | 1453 | |
andrewbonney | 0:ec559500a63f | 1454 | if (is_fin) { |
andrewbonney | 0:ec559500a63f | 1455 | /* FIN segment, no data */ |
andrewbonney | 0:ec559500a63f | 1456 | TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN); |
andrewbonney | 0:ec559500a63f | 1457 | } else { |
andrewbonney | 0:ec559500a63f | 1458 | /* Data segment, copy in one byte from the head of the unacked queue */ |
andrewbonney | 0:ec559500a63f | 1459 | *((char *)p->payload + TCP_HLEN) = *(char *)seg->dataptr; |
andrewbonney | 0:ec559500a63f | 1460 | } |
andrewbonney | 0:ec559500a63f | 1461 | |
andrewbonney | 0:ec559500a63f | 1462 | #if CHECKSUM_GEN_TCP |
andrewbonney | 0:ec559500a63f | 1463 | tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip, |
andrewbonney | 0:ec559500a63f | 1464 | IP_PROTO_TCP, p->tot_len); |
andrewbonney | 0:ec559500a63f | 1465 | #endif |
andrewbonney | 0:ec559500a63f | 1466 | TCP_STATS_INC(tcp.xmit); |
andrewbonney | 0:ec559500a63f | 1467 | |
andrewbonney | 0:ec559500a63f | 1468 | /* Send output to IP */ |
andrewbonney | 0:ec559500a63f | 1469 | #if LWIP_NETIF_HWADDRHINT |
andrewbonney | 0:ec559500a63f | 1470 | ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP, |
andrewbonney | 0:ec559500a63f | 1471 | &(pcb->addr_hint)); |
andrewbonney | 0:ec559500a63f | 1472 | #else /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 1473 | ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP); |
andrewbonney | 0:ec559500a63f | 1474 | #endif /* LWIP_NETIF_HWADDRHINT*/ |
andrewbonney | 0:ec559500a63f | 1475 | |
andrewbonney | 0:ec559500a63f | 1476 | pbuf_free(p); |
andrewbonney | 0:ec559500a63f | 1477 | |
andrewbonney | 0:ec559500a63f | 1478 | LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F |
andrewbonney | 0:ec559500a63f | 1479 | " ackno %"U32_F".\n", |
andrewbonney | 0:ec559500a63f | 1480 | pcb->snd_nxt - 1, pcb->rcv_nxt)); |
andrewbonney | 0:ec559500a63f | 1481 | } |
andrewbonney | 0:ec559500a63f | 1482 | #endif /* LWIP_TCP */ |