HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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