Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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