Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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