Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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