V1

Dependents:   EthernetInterface

Fork of lwip by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcp_out.c Source File

tcp_out.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * Transmission Control Protocol, outgoing traffic
00004  *
00005  * The output functions of TCP.
00006  *
00007  */
00008 
00009 /*
00010  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  * 2. Redistributions in binary form must reproduce the above copyright notice,
00019  *    this list of conditions and the following disclaimer in the documentation
00020  *    and/or other materials provided with the distribution.
00021  * 3. The name of the author may not be used to endorse or promote products
00022  *    derived from this software without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00025  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00026  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00027  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00029  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00032  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00033  * OF SUCH DAMAGE.
00034  *
00035  * This file is part of the lwIP TCP/IP stack.
00036  *
00037  * Author: Adam Dunkels <adam@sics.se>
00038  *
00039  */
00040 
00041 #include "lwip/opt.h"
00042 
00043 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
00044 
00045 #include "lwip/tcp_impl.h"
00046 #include "lwip/def.h"
00047 #include "lwip/mem.h"
00048 #include "lwip/memp.h"
00049 #include "lwip/sys.h"
00050 #include "lwip/ip_addr.h"
00051 #include "lwip/netif.h"
00052 #include "lwip/inet_chksum.h"
00053 #include "lwip/stats.h"
00054 #include "lwip/snmp.h"
00055 
00056 #include <string.h>
00057 
00058 /* Define some copy-macros for checksum-on-copy so that the code looks
00059    nicer by preventing too many ifdef's. */
00060 #if TCP_CHECKSUM_ON_COPY
00061 #define TCP_DATA_COPY(dst, src, len, seg) do { \
00062   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
00063                      len, &seg->chksum, &seg->chksum_swapped); \
00064   seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
00065 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped)  \
00066   tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
00067 #else /* TCP_CHECKSUM_ON_COPY*/
00068 #define TCP_DATA_COPY(dst, src, len, seg)                     MEMCPY(dst, src, len)
00069 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
00070 #endif /* TCP_CHECKSUM_ON_COPY*/
00071 
00072 /** Define this to 1 for an extra check that the output checksum is valid
00073  * (usefule when the checksum is generated by the application, not the stack) */
00074 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
00075 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK   0
00076 #endif
00077 
00078 /* Forward declarations.*/
00079 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
00080 
00081 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
00082  * functions other than the default tcp_output -> tcp_output_segment
00083  * (e.g. tcp_send_empty_ack, etc.)
00084  *
00085  * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
00086  * @param optlen length of header-options
00087  * @param datalen length of tcp data to reserve in pbuf
00088  * @param seqno_be seqno in network byte order (big-endian)
00089  * @return pbuf with p->payload being the tcp_hdr
00090  */
00091 static struct pbuf *
00092 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
00093                       u32_t seqno_be /* already in network byte order */)
00094 {
00095   struct tcp_hdr *tcphdr;
00096   struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
00097   if (p != NULL) {
00098     LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
00099                  (p->len >= TCP_HLEN + optlen));
00100     tcphdr = (struct tcp_hdr *)p->payload;
00101     tcphdr->src = htons(pcb->local_port);
00102     tcphdr->dest = htons(pcb->remote_port);
00103     tcphdr->seqno = seqno_be;
00104     tcphdr->ackno = htonl(pcb->rcv_nxt);
00105     TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
00106     tcphdr->wnd = htons(pcb->rcv_ann_wnd);
00107     tcphdr->chksum = 0;
00108     tcphdr->urgp = 0;
00109 
00110     /* If we're sending a packet, update the announced right window edge */
00111     pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
00112   }
00113   return p;
00114 }
00115 
00116 /**
00117  * Called by tcp_close() to send a segment including FIN flag but not data.
00118  *
00119  * @param pcb the tcp_pcb over which to send a segment
00120  * @return ERR_OK if sent, another err_t otherwise
00121  */
00122 err_t
00123 tcp_send_fin(struct tcp_pcb *pcb)
00124 {
00125   /* first, try to add the fin to the last unsent segment */
00126   if (pcb->unsent != NULL) {
00127     struct tcp_seg *last_unsent;
00128     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
00129          last_unsent = last_unsent->next);
00130 
00131     if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
00132       /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
00133       TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
00134       return ERR_OK;
00135     }
00136   }
00137   /* no data, no length, flags, copy=1, no optdata */
00138   return tcp_enqueue_flags(pcb, TCP_FIN);
00139 }
00140 
00141 /**
00142  * Create a TCP segment with prefilled header.
00143  *
00144  * Called by tcp_write and tcp_enqueue_flags.
00145  *
00146  * @param pcb Protocol control block for the TCP connection.
00147  * @param p pbuf that is used to hold the TCP header.
00148  * @param flags TCP flags for header.
00149  * @param seqno TCP sequence number of this packet
00150  * @param optflags options to include in TCP header
00151  * @return a new tcp_seg pointing to p, or NULL.
00152  * The TCP header is filled in except ackno and wnd.
00153  * p is freed on failure.
00154  */
00155 static struct tcp_seg *
00156 tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
00157 {
00158   struct tcp_seg *seg;
00159   u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
00160 
00161   if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
00162     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no memory.\n"));
00163     pbuf_free(p);
00164     return NULL;
00165   }
00166   seg->flags = optflags;
00167   seg->next = NULL;
00168   seg->p = p;
00169   seg->len = p->tot_len - optlen;
00170 #if TCP_OVERSIZE_DBGCHECK
00171   seg->oversize_left = 0;
00172 #endif /* TCP_OVERSIZE_DBGCHECK */
00173 #if TCP_CHECKSUM_ON_COPY
00174   seg->chksum = 0;
00175   seg->chksum_swapped = 0;
00176   /* check optflags */
00177   LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
00178               (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
00179 #endif /* TCP_CHECKSUM_ON_COPY */
00180 
00181   /* build TCP header */
00182   if (pbuf_header(p, TCP_HLEN)) {
00183     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
00184     TCP_STATS_INC(tcp.err);
00185     tcp_seg_free(seg);
00186     return NULL;
00187   }
00188   seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
00189   seg->tcphdr->src = htons(pcb->local_port);
00190   seg->tcphdr->dest = htons(pcb->remote_port);
00191   seg->tcphdr->seqno = htonl(seqno);
00192   /* ackno is set in tcp_output */
00193   TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
00194   /* wnd and chksum are set in tcp_output */
00195   seg->tcphdr->urgp = 0;
00196   return seg;
00197 } 
00198 
00199 /**
00200  * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
00201  *
00202  * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
00203  * there may be extra bytes available at the end.
00204  *
00205  * @param layer flag to define header size.
00206  * @param length size of the pbuf's payload.
00207  * @param max_length maximum usable size of payload+oversize.
00208  * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
00209  * @param pcb The TCP connection that willo enqueue the pbuf.
00210  * @param apiflags API flags given to tcp_write.
00211  * @param first_seg true when this pbuf will be used in the first enqueued segment.
00212  * @param 
00213  */
00214 #if TCP_OVERSIZE
00215 static struct pbuf *
00216 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
00217                   u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
00218                   u8_t first_seg)
00219 {
00220   struct pbuf *p;
00221   u16_t alloc = length;
00222 
00223 #if LWIP_NETIF_TX_SINGLE_PBUF
00224   LWIP_UNUSED_ARG(max_length);
00225   LWIP_UNUSED_ARG(pcb);
00226   LWIP_UNUSED_ARG(apiflags);
00227   LWIP_UNUSED_ARG(first_seg);
00228   /* always create MSS-sized pbufs */
00229   alloc = TCP_MSS;
00230 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
00231   if (length < max_length) {
00232     /* Should we allocate an oversized pbuf, or just the minimum
00233      * length required? If tcp_write is going to be called again
00234      * before this segment is transmitted, we want the oversized
00235      * buffer. If the segment will be transmitted immediately, we can
00236      * save memory by allocating only length. We use a simple
00237      * heuristic based on the following information:
00238      *
00239      * Did the user set TCP_WRITE_FLAG_MORE?
00240      *
00241      * Will the Nagle algorithm defer transmission of this segment?
00242      */
00243     if ((apiflags & TCP_WRITE_FLAG_MORE) ||
00244         (!(pcb->flags & TF_NODELAY) &&
00245          (!first_seg ||
00246           pcb->unsent != NULL ||
00247           pcb->unacked != NULL))) {
00248       alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(length + TCP_OVERSIZE));
00249     }
00250   }
00251 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
00252   p = pbuf_alloc(layer, alloc, PBUF_RAM);
00253   if (p == NULL) {
00254     return NULL;
00255   }
00256   LWIP_ASSERT("need unchained pbuf", p->next == NULL);
00257   *oversize = p->len - length;
00258   /* trim p->len to the currently used size */
00259   p->len = p->tot_len = length;
00260   return p;
00261 }
00262 #else /* TCP_OVERSIZE */
00263 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
00264 #endif /* TCP_OVERSIZE */
00265 
00266 #if TCP_CHECKSUM_ON_COPY
00267 /** Add a checksum of newly added data to the segment */
00268 static void
00269 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
00270                    u8_t *seg_chksum_swapped)
00271 {
00272   u32_t helper;
00273   /* add chksum to old chksum and fold to u16_t */
00274   helper = chksum + *seg_chksum;
00275   chksum = FOLD_U32T(helper);
00276   if ((len & 1) != 0) {
00277     *seg_chksum_swapped = 1 - *seg_chksum_swapped;
00278     chksum = SWAP_BYTES_IN_WORD(chksum);
00279   }
00280   *seg_chksum = chksum;
00281 }
00282 #endif /* TCP_CHECKSUM_ON_COPY */
00283 
00284 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
00285  *
00286  * @param pcb the tcp pcb to check for
00287  * @param len length of data to send (checked agains snd_buf)
00288  * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
00289  */
00290 static err_t
00291 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
00292 {
00293   /* connection is in invalid state for data transmission? */
00294   if ((pcb->state != ESTABLISHED) &&
00295       (pcb->state != CLOSE_WAIT) &&
00296       (pcb->state != SYN_SENT) &&
00297       (pcb->state != SYN_RCVD)) {
00298     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
00299     return ERR_CONN;
00300   } else if (len == 0) {
00301     return ERR_OK;
00302   }
00303 
00304   /* fail on too much data */
00305   if (len > pcb->snd_buf) {
00306     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n",
00307       len, pcb->snd_buf));
00308     pcb->flags |= TF_NAGLEMEMERR;
00309     return ERR_MEM;
00310   }
00311 
00312   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
00313 
00314   /* If total number of pbufs on the unsent/unacked queues exceeds the
00315    * configured maximum, return an error */
00316   /* check for configured max queuelen and possible overflow */
00317   if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
00318     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
00319       pcb->snd_queuelen, TCP_SND_QUEUELEN));
00320     TCP_STATS_INC(tcp.memerr);
00321     pcb->flags |= TF_NAGLEMEMERR;
00322     return ERR_MEM;
00323   }
00324   if (pcb->snd_queuelen != 0) {
00325     LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
00326       pcb->unacked != NULL || pcb->unsent != NULL);
00327   } else {
00328     LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
00329       pcb->unacked == NULL && pcb->unsent == NULL);
00330   }
00331   return ERR_OK;
00332 }
00333 
00334 /**
00335  * Write data for sending (but does not send it immediately).
00336  *
00337  * It waits in the expectation of more data being sent soon (as
00338  * it can send them more efficiently by combining them together).
00339  * To prompt the system to send data now, call tcp_output() after
00340  * calling tcp_write().
00341  *
00342  * @param pcb Protocol control block for the TCP connection to enqueue data for.
00343  * @param arg Pointer to the data to be enqueued for sending.
00344  * @param len Data length in bytes
00345  * @param apiflags combination of following flags :
00346  * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
00347  * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
00348  * @return ERR_OK if enqueued, another err_t on error
00349  */
00350 err_t
00351 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
00352 {
00353   struct pbuf *concat_p = NULL;
00354   struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
00355   u16_t pos = 0; /* position in 'arg' data */
00356   u16_t queuelen;
00357   u8_t optlen = 0;
00358   u8_t optflags = 0;
00359 #if TCP_OVERSIZE
00360   u16_t oversize = 0;
00361   u16_t oversize_used = 0;
00362 #endif /* TCP_OVERSIZE */
00363 #if TCP_CHECKSUM_ON_COPY
00364   u16_t concat_chksum = 0;
00365   u8_t concat_chksum_swapped = 0;
00366   u16_t concat_chksummed = 0;
00367 #endif /* TCP_CHECKSUM_ON_COPY */
00368   err_t err;
00369 
00370 #if LWIP_NETIF_TX_SINGLE_PBUF
00371   /* Always copy to try to create single pbufs for TX */
00372   apiflags |= TCP_WRITE_FLAG_COPY;
00373 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
00374 
00375   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
00376     (void *)pcb, arg, len, (u16_t)apiflags));
00377   LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)", 
00378              arg != NULL, return ERR_ARG;);
00379 
00380   err = tcp_write_checks(pcb, len);
00381   if (err != ERR_OK) {
00382     return err;
00383   }
00384   queuelen = pcb->snd_queuelen;
00385 
00386 #if LWIP_TCP_TIMESTAMPS
00387   if ((pcb->flags & TF_TIMESTAMP)) {
00388     optflags = TF_SEG_OPTS_TS;
00389     optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
00390   }
00391 #endif /* LWIP_TCP_TIMESTAMPS */
00392 
00393 
00394   /*
00395    * TCP segmentation is done in three phases with increasing complexity:
00396    *
00397    * 1. Copy data directly into an oversized pbuf.
00398    * 2. Chain a new pbuf to the end of pcb->unsent.
00399    * 3. Create new segments.
00400    *
00401    * We may run out of memory at any point. In that case we must
00402    * return ERR_MEM and not change anything in pcb. Therefore, all
00403    * changes are recorded in local variables and committed at the end
00404    * of the function. Some pcb fields are maintained in local copies:
00405    *
00406    * queuelen = pcb->snd_queuelen
00407    * oversize = pcb->unsent_oversize
00408    *
00409    * These variables are set consistently by the phases:
00410    *
00411    * seg points to the last segment tampered with.
00412    *
00413    * pos records progress as data is segmented.
00414    */
00415 
00416   /* Find the tail of the unsent queue. */
00417   if (pcb->unsent != NULL) {
00418     u16_t space;
00419     u16_t unsent_optlen;
00420 
00421     /* @todo: this could be sped up by keeping last_unsent in the pcb */
00422     for (last_unsent = pcb->unsent; last_unsent->next != NULL;
00423          last_unsent = last_unsent->next);
00424 
00425     /* Usable space at the end of the last unsent segment */
00426     unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
00427     space = pcb->mss - (last_unsent->len + unsent_optlen);
00428 
00429     /*
00430      * Phase 1: Copy data directly into an oversized pbuf.
00431      *
00432      * The number of bytes copied is recorded in the oversize_used
00433      * variable. The actual copying is done at the bottom of the
00434      * function.
00435      */
00436 #if TCP_OVERSIZE
00437 #if TCP_OVERSIZE_DBGCHECK
00438     /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */
00439     LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
00440                 pcb->unsent_oversize == last_unsent->oversize_left);
00441 #endif /* TCP_OVERSIZE_DBGCHECK */
00442     oversize = pcb->unsent_oversize;
00443     if (oversize > 0) {
00444       LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space);
00445       seg = last_unsent;
00446       oversize_used = oversize < len ? oversize : len;
00447       pos += oversize_used;
00448       oversize -= oversize_used;
00449       space -= oversize_used;
00450     }
00451     /* now we are either finished or oversize is zero */
00452     LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
00453 #endif /* TCP_OVERSIZE */
00454 
00455     /*
00456      * Phase 2: Chain a new pbuf to the end of pcb->unsent.
00457      *
00458      * We don't extend segments containing SYN/FIN flags or options
00459      * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
00460      * the end.
00461      */
00462     if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
00463       u16_t seglen = space < len - pos ? space : len - pos;
00464       seg = last_unsent;
00465 
00466       /* Create a pbuf with a copy or reference to seglen bytes. We
00467        * can use PBUF_RAW here since the data appears in the middle of
00468        * a segment. A header will never be prepended. */
00469       if (apiflags & TCP_WRITE_FLAG_COPY) {
00470         /* Data is copied */
00471         if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
00472           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
00473                       ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
00474                        seglen));
00475           goto memerr;
00476         }
00477 #if TCP_OVERSIZE_DBGCHECK
00478         last_unsent->oversize_left = oversize;
00479 #endif /* TCP_OVERSIZE_DBGCHECK */
00480         TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
00481 #if TCP_CHECKSUM_ON_COPY
00482         concat_chksummed += seglen;
00483 #endif /* TCP_CHECKSUM_ON_COPY */
00484       } else {
00485         /* Data is not copied */
00486         if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
00487           LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
00488                       ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
00489           goto memerr;
00490         }
00491 #if TCP_CHECKSUM_ON_COPY
00492         /* calculate the checksum of nocopy-data */
00493         tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen,
00494           &concat_chksum, &concat_chksum_swapped);
00495         concat_chksummed += seglen;
00496 #endif /* TCP_CHECKSUM_ON_COPY */
00497         /* reference the non-volatile payload data */
00498         concat_p->payload = (u8_t*)arg + pos;
00499       }
00500 
00501       pos += seglen;
00502       queuelen += pbuf_clen(concat_p);
00503     }
00504   } else {
00505 #if TCP_OVERSIZE
00506     LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
00507                 pcb->unsent_oversize == 0);
00508 #endif /* TCP_OVERSIZE */
00509   }
00510 
00511   /*
00512    * Phase 3: Create new segments.
00513    *
00514    * The new segments are chained together in the local 'queue'
00515    * variable, ready to be appended to pcb->unsent.
00516    */
00517   while (pos < len) {
00518     struct pbuf *p;
00519     u16_t left = len - pos;
00520     u16_t max_len = pcb->mss - optlen;
00521     u16_t seglen = left > max_len ? max_len : left;
00522 #if TCP_CHECKSUM_ON_COPY
00523     u16_t chksum = 0;
00524     u8_t chksum_swapped = 0;
00525 #endif /* TCP_CHECKSUM_ON_COPY */
00526 
00527     if (apiflags & TCP_WRITE_FLAG_COPY) {
00528       /* If copy is set, memory should be allocated and data copied
00529        * into pbuf */
00530       if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, pcb->mss, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
00531         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
00532         goto memerr;
00533       }
00534       LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
00535                   (p->len >= seglen));
00536       TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
00537     } else {
00538       /* Copy is not set: First allocate a pbuf for holding the data.
00539        * Since the referenced data is available at least until it is
00540        * sent out on the link (as it has to be ACKed by the remote
00541        * party) we can safely use PBUF_ROM instead of PBUF_REF here.
00542        */
00543       struct pbuf *p2;
00544 #if TCP_OVERSIZE
00545       LWIP_ASSERT("oversize == 0", oversize == 0);
00546 #endif /* TCP_OVERSIZE */
00547       if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
00548         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
00549         goto memerr;
00550       }
00551 #if TCP_CHECKSUM_ON_COPY
00552       /* calculate the checksum of nocopy-data */
00553       chksum = ~inet_chksum((u8_t*)arg + pos, seglen);
00554 #endif /* TCP_CHECKSUM_ON_COPY */
00555       /* reference the non-volatile payload data */
00556       p2->payload = (u8_t*)arg + pos;
00557 
00558       /* Second, allocate a pbuf for the headers. */
00559       if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
00560         /* If allocation fails, we have to deallocate the data pbuf as
00561          * well. */
00562         pbuf_free(p2);
00563         LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for header pbuf\n"));
00564         goto memerr;
00565       }
00566       /* Concatenate the headers and data pbufs together. */
00567       pbuf_cat(p/*header*/, p2/*data*/);
00568     }
00569 
00570     queuelen += pbuf_clen(p);
00571 
00572     /* Now that there are more segments queued, we check again if the
00573      * length of the queue exceeds the configured maximum or
00574      * overflows. */
00575     if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
00576       LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
00577       pbuf_free(p);
00578       goto memerr;
00579     }
00580 
00581     if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
00582       goto memerr;
00583     }
00584 #if TCP_OVERSIZE_DBGCHECK
00585     seg->oversize_left = oversize;
00586 #endif /* TCP_OVERSIZE_DBGCHECK */
00587 #if TCP_CHECKSUM_ON_COPY
00588     seg->chksum = chksum;
00589     seg->chksum_swapped = chksum_swapped;
00590     seg->flags |= TF_SEG_DATA_CHECKSUMMED;
00591 #endif /* TCP_CHECKSUM_ON_COPY */
00592 
00593     /* first segment of to-be-queued data? */
00594     if (queue == NULL) {
00595       queue = seg;
00596     } else {
00597       /* Attach the segment to the end of the queued segments */
00598       LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
00599       prev_seg->next = seg;
00600     }
00601     /* remember last segment of to-be-queued data for next iteration */
00602     prev_seg = seg;
00603 
00604     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
00605       ntohl(seg->tcphdr->seqno),
00606       ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
00607 
00608     pos += seglen;
00609   }
00610 
00611   /*
00612    * All three segmentation phases were successful. We can commit the
00613    * transaction.
00614    */
00615 
00616   /*
00617    * Phase 1: If data has been added to the preallocated tail of
00618    * last_unsent, we update the length fields of the pbuf chain.
00619    */
00620 #if TCP_OVERSIZE
00621   if (oversize_used > 0) {
00622     struct pbuf *p;
00623     /* Bump tot_len of whole chain, len of tail */
00624     for (p = last_unsent->p; p; p = p->next) {
00625       p->tot_len += oversize_used;
00626       if (p->next == NULL) {
00627         TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
00628         p->len += oversize_used;
00629       }
00630     }
00631     last_unsent->len += oversize_used;
00632 #if TCP_OVERSIZE_DBGCHECK
00633     last_unsent->oversize_left -= oversize_used;
00634 #endif /* TCP_OVERSIZE_DBGCHECK */
00635   }
00636   pcb->unsent_oversize = oversize;
00637 #endif /* TCP_OVERSIZE */
00638 
00639   /*
00640    * Phase 2: concat_p can be concatenated onto last_unsent->p
00641    */
00642   if (concat_p != NULL) {
00643     LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
00644       (last_unsent != NULL));
00645     pbuf_cat(last_unsent->p, concat_p);
00646     last_unsent->len += concat_p->tot_len;
00647 #if TCP_CHECKSUM_ON_COPY
00648     if (concat_chksummed) {
00649       tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
00650         &last_unsent->chksum_swapped);
00651       last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
00652     }
00653 #endif /* TCP_CHECKSUM_ON_COPY */
00654   }
00655 
00656   /*
00657    * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
00658    * is harmless
00659    */
00660   if (last_unsent == NULL) {
00661     pcb->unsent = queue;
00662   } else {
00663     last_unsent->next = queue;
00664   }
00665 
00666   /*
00667    * Finally update the pcb state.
00668    */
00669   pcb->snd_lbb += len;
00670   pcb->snd_buf -= len;
00671   pcb->snd_queuelen = queuelen;
00672 
00673   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
00674     pcb->snd_queuelen));
00675   if (pcb->snd_queuelen != 0) {
00676     LWIP_ASSERT("tcp_write: valid queue length",
00677                 pcb->unacked != NULL || pcb->unsent != NULL);
00678   }
00679 
00680   /* Set the PSH flag in the last segment that we enqueued. */
00681   if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
00682     TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
00683   }
00684 
00685   return ERR_OK;
00686 memerr:
00687   pcb->flags |= TF_NAGLEMEMERR;
00688   TCP_STATS_INC(tcp.memerr);
00689 
00690   if (concat_p != NULL) {
00691     pbuf_free(concat_p);
00692   }
00693   if (queue != NULL) {
00694     tcp_segs_free(queue);
00695   }
00696   if (pcb->snd_queuelen != 0) {
00697     LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
00698       pcb->unsent != NULL);
00699   }
00700   LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
00701   return ERR_MEM;
00702 }
00703 
00704 /**
00705  * Enqueue TCP options for transmission.
00706  *
00707  * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
00708  *
00709  * @param pcb Protocol control block for the TCP connection.
00710  * @param flags TCP header flags to set in the outgoing segment.
00711  * @param optdata pointer to TCP options, or NULL.
00712  * @param optlen length of TCP options in bytes.
00713  */
00714 err_t
00715 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
00716 {
00717   struct pbuf *p;
00718   struct tcp_seg *seg;
00719   u8_t optflags = 0;
00720   u8_t optlen = 0;
00721 
00722   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
00723 
00724   LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
00725               (flags & (TCP_SYN | TCP_FIN)) != 0);
00726 
00727   /* check for configured max queuelen and possible overflow */
00728   if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
00729     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
00730                                        pcb->snd_queuelen, TCP_SND_QUEUELEN));
00731     TCP_STATS_INC(tcp.memerr);
00732     pcb->flags |= TF_NAGLEMEMERR;
00733     return ERR_MEM;
00734   }
00735 
00736   if (flags & TCP_SYN) {
00737     optflags = TF_SEG_OPTS_MSS;
00738   }
00739 #if LWIP_TCP_TIMESTAMPS
00740   if ((pcb->flags & TF_TIMESTAMP)) {
00741     optflags |= TF_SEG_OPTS_TS;
00742   }
00743 #endif /* LWIP_TCP_TIMESTAMPS */
00744   optlen = LWIP_TCP_OPT_LENGTH(optflags);
00745 
00746   /* tcp_enqueue_flags is always called with either SYN or FIN in flags.
00747    * We need one available snd_buf byte to do that.
00748    * This means we can't send FIN while snd_buf==0. A better fix would be to
00749    * not include SYN and FIN sequence numbers in the snd_buf count. */
00750   if (pcb->snd_buf == 0) {
00751     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: no send buffer available\n"));
00752     TCP_STATS_INC(tcp.memerr);
00753     return ERR_MEM;
00754   }
00755 
00756   /* Allocate pbuf with room for TCP header + options */
00757   if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
00758     pcb->flags |= TF_NAGLEMEMERR;
00759     TCP_STATS_INC(tcp.memerr);
00760     return ERR_MEM;
00761   }
00762   LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
00763               (p->len >= optlen));
00764 
00765   /* Allocate memory for tcp_seg, and fill in fields. */
00766   if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
00767     pcb->flags |= TF_NAGLEMEMERR;
00768     TCP_STATS_INC(tcp.memerr);
00769     return ERR_MEM;
00770   }
00771   LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % MEM_ALIGNMENT) == 0);
00772   LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
00773 
00774   LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
00775               ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
00776                ntohl(seg->tcphdr->seqno),
00777                ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
00778                (u16_t)flags));
00779 
00780   /* Now append seg to pcb->unsent queue */
00781   if (pcb->unsent == NULL) {
00782     pcb->unsent = seg;
00783   } else {
00784     struct tcp_seg *useg;
00785     for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
00786     useg->next = seg;
00787   }
00788 #if TCP_OVERSIZE
00789   /* The new unsent tail has no space */
00790   pcb->unsent_oversize = 0;
00791 #endif /* TCP_OVERSIZE */
00792 
00793   /* SYN and FIN bump the sequence number */
00794   if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
00795     pcb->snd_lbb++;
00796     /* optlen does not influence snd_buf */
00797     pcb->snd_buf--;
00798   }
00799   if (flags & TCP_FIN) {
00800     pcb->flags |= TF_FIN;
00801   }
00802 
00803   /* update number of segments on the queues */
00804   pcb->snd_queuelen += pbuf_clen(seg->p);
00805   LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
00806   if (pcb->snd_queuelen != 0) {
00807     LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
00808       pcb->unacked != NULL || pcb->unsent != NULL);
00809   }
00810 
00811   return ERR_OK;
00812 }
00813  
00814 
00815 #if LWIP_TCP_TIMESTAMPS
00816 /* Build a timestamp option (12 bytes long) at the specified options pointer)
00817  *
00818  * @param pcb tcp_pcb
00819  * @param opts option pointer where to store the timestamp option
00820  */
00821 static void
00822 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
00823 {
00824   /* Pad with two NOP options to make everything nicely aligned */
00825   opts[0] = PP_HTONL(0x0101080A);
00826   opts[1] = htonl(sys_now());
00827   opts[2] = htonl(pcb->ts_recent);
00828 }
00829 #endif
00830 
00831 /** Send an ACK without data.
00832  *
00833  * @param pcb Protocol control block for the TCP connection to send the ACK
00834  */
00835 err_t
00836 tcp_send_empty_ack(struct tcp_pcb *pcb)
00837 {
00838   struct pbuf *p;
00839   struct tcp_hdr *tcphdr;
00840   u8_t optlen = 0;
00841 
00842 #if LWIP_TCP_TIMESTAMPS
00843   if (pcb->flags & TF_TIMESTAMP) {
00844     optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
00845   }
00846 #endif
00847 
00848   p = tcp_output_alloc_header(pcb, optlen, 0, htonl(pcb->snd_nxt));
00849   if (p == NULL) {
00850     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
00851     return ERR_BUF;
00852   }
00853   tcphdr = (struct tcp_hdr *)p->payload;
00854   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, 
00855               ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
00856   /* remove ACK flags from the PCB, as we send an empty ACK now */
00857   pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
00858 
00859   /* NB. MSS option is only sent on SYNs, so ignore it here */
00860 #if LWIP_TCP_TIMESTAMPS
00861   pcb->ts_lastacksent = pcb->rcv_nxt;
00862 
00863   if (pcb->flags & TF_TIMESTAMP) {
00864     tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
00865   }
00866 #endif 
00867 
00868 #if CHECKSUM_GEN_TCP
00869   tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
00870         IP_PROTO_TCP, p->tot_len);
00871 #endif
00872 #if LWIP_NETIF_HWADDRHINT
00873   ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
00874       IP_PROTO_TCP, &(pcb->addr_hint));
00875 #else /* LWIP_NETIF_HWADDRHINT*/
00876   ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
00877       IP_PROTO_TCP);
00878 #endif /* LWIP_NETIF_HWADDRHINT*/
00879   pbuf_free(p);
00880 
00881   return ERR_OK;
00882 }
00883 
00884 /**
00885  * Find out what we can send and send it
00886  *
00887  * @param pcb Protocol control block for the TCP connection to send data
00888  * @return ERR_OK if data has been sent or nothing to send
00889  *         another err_t on error
00890  */
00891 err_t
00892 tcp_output(struct tcp_pcb *pcb)
00893 {
00894   struct tcp_seg *seg, *useg;
00895   u32_t wnd, snd_nxt;
00896 #if TCP_CWND_DEBUG
00897   s16_t i = 0;
00898 #endif /* TCP_CWND_DEBUG */
00899 
00900   /* First, check if we are invoked by the TCP input processing
00901      code. If so, we do not output anything. Instead, we rely on the
00902      input processing code to call us when input processing is done
00903      with. */
00904   if (tcp_input_pcb == pcb) {
00905     return ERR_OK;
00906   }
00907 
00908   wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
00909 
00910   seg = pcb->unsent;
00911 
00912   /* If the TF_ACK_NOW flag is set and no data will be sent (either
00913    * because the ->unsent queue is empty or because the window does
00914    * not allow it), construct an empty ACK segment and send it.
00915    *
00916    * If data is to be sent, we will just piggyback the ACK (see below).
00917    */
00918   if (pcb->flags & TF_ACK_NOW &&
00919      (seg == NULL ||
00920       ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
00921      return tcp_send_empty_ack(pcb);
00922   }
00923 
00924   /* useg should point to last segment on unacked queue */
00925   useg = pcb->unacked;
00926   if (useg != NULL) {
00927     for (; useg->next != NULL; useg = useg->next);
00928   }
00929 
00930 #if TCP_OUTPUT_DEBUG
00931   if (seg == NULL) {
00932     LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
00933                                    (void*)pcb->unsent));
00934   }
00935 #endif /* TCP_OUTPUT_DEBUG */
00936 #if TCP_CWND_DEBUG
00937   if (seg == NULL) {
00938     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
00939                                  ", cwnd %"U16_F", wnd %"U32_F
00940                                  ", seg == NULL, ack %"U32_F"\n",
00941                                  pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
00942   } else {
00943     LWIP_DEBUGF(TCP_CWND_DEBUG, 
00944                 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
00945                  ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
00946                  pcb->snd_wnd, pcb->cwnd, wnd,
00947                  ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
00948                  ntohl(seg->tcphdr->seqno), pcb->lastack));
00949   }
00950 #endif /* TCP_CWND_DEBUG */
00951   /* data available and window allows it to be sent? */
00952   while (seg != NULL &&
00953          ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
00954     LWIP_ASSERT("RST not expected here!", 
00955                 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
00956     /* Stop sending if the nagle algorithm would prevent it
00957      * Don't stop:
00958      * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
00959      * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
00960      *   either seg->next != NULL or pcb->unacked == NULL;
00961      *   RST is no sent using tcp_write/tcp_output.
00962      */
00963     if((tcp_do_output_nagle(pcb) == 0) &&
00964       ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
00965       break;
00966     }
00967 #if TCP_CWND_DEBUG
00968     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",
00969                             pcb->snd_wnd, pcb->cwnd, wnd,
00970                             ntohl(seg->tcphdr->seqno) + seg->len -
00971                             pcb->lastack,
00972                             ntohl(seg->tcphdr->seqno), pcb->lastack, i));
00973     ++i;
00974 #endif /* TCP_CWND_DEBUG */
00975 
00976     pcb->unsent = seg->next;
00977 
00978     if (pcb->state != SYN_SENT) {
00979       TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
00980       pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
00981     }
00982 
00983     tcp_output_segment(seg, pcb);
00984     snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
00985     if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
00986       pcb->snd_nxt = snd_nxt;
00987     }
00988     /* put segment on unacknowledged list if length > 0 */
00989     if (TCP_TCPLEN(seg) > 0) {
00990       seg->next = NULL;
00991       /* unacked list is empty? */
00992       if (pcb->unacked == NULL) {
00993         pcb->unacked = seg;
00994         useg = seg;
00995       /* unacked list is not empty? */
00996       } else {
00997         /* In the case of fast retransmit, the packet should not go to the tail
00998          * of the unacked queue, but rather somewhere before it. We need to check for
00999          * this case. -STJ Jul 27, 2004 */
01000         if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) {
01001           /* add segment to before tail of unacked list, keeping the list sorted */
01002           struct tcp_seg **cur_seg = &(pcb->unacked);
01003           while (*cur_seg &&
01004             TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
01005               cur_seg = &((*cur_seg)->next );
01006           }
01007           seg->next = (*cur_seg);
01008           (*cur_seg) = seg;
01009         } else {
01010           /* add segment to tail of unacked list */
01011           useg->next = seg;
01012           useg = useg->next;
01013         }
01014       }
01015     /* do not queue empty segments on the unacked list */
01016     } else {
01017       tcp_seg_free(seg);
01018     }
01019     seg = pcb->unsent;
01020   }
01021 #if TCP_OVERSIZE
01022   if (pcb->unsent == NULL) {
01023     /* last unsent has been removed, reset unsent_oversize */
01024     pcb->unsent_oversize = 0;
01025   }
01026 #endif /* TCP_OVERSIZE */
01027 
01028   if (seg != NULL && pcb->persist_backoff == 0 && 
01029       ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
01030     /* prepare for persist timer */
01031     pcb->persist_cnt = 0;
01032     pcb->persist_backoff = 1;
01033   }
01034 
01035   pcb->flags &= ~TF_NAGLEMEMERR;
01036   return ERR_OK;
01037 }
01038 
01039 /**
01040  * Called by tcp_output() to actually send a TCP segment over IP.
01041  *
01042  * @param seg the tcp_seg to send
01043  * @param pcb the tcp_pcb for the TCP connection used to send the segment
01044  */
01045 static void
01046 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
01047 {
01048   u16_t len;
01049   struct netif *netif;
01050   u32_t *opts;
01051 
01052   /** @bug Exclude retransmitted segments from this count. */
01053   snmp_inc_tcpoutsegs();
01054 
01055   /* The TCP header has already been constructed, but the ackno and
01056    wnd fields remain. */
01057   seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
01058 
01059   /* advertise our receive window size in this TCP segment */
01060   seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
01061 
01062   pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
01063 
01064   /* Add any requested options.  NB MSS option is only set on SYN
01065      packets, so ignore it here */
01066   LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)seg->tcphdr % MEM_ALIGNMENT) == 0);
01067   opts = (u32_t *)(void *)(seg->tcphdr + 1);
01068   if (seg->flags & TF_SEG_OPTS_MSS) {
01069     TCP_BUILD_MSS_OPTION(*opts);
01070     opts += 1;
01071   }
01072 #if LWIP_TCP_TIMESTAMPS
01073   pcb->ts_lastacksent = pcb->rcv_nxt;
01074 
01075   if (seg->flags & TF_SEG_OPTS_TS) {
01076     tcp_build_timestamp_option(pcb, opts);
01077     opts += 3;
01078   }
01079 #endif
01080 
01081   /* Set retransmission timer running if it is not currently enabled 
01082      This must be set before checking the route. */
01083   if (pcb->rtime == -1) {
01084     pcb->rtime = 0;
01085   }
01086 
01087   /* If we don't have a local IP address, we get one by
01088      calling ip_route(). */
01089   if (ip_addr_isany(&(pcb->local_ip))) {
01090     netif = ip_route(&(pcb->remote_ip));
01091     if (netif == NULL) {
01092       return;
01093     }
01094     ip_addr_copy(pcb->local_ip, netif->ip_addr);
01095   }
01096 
01097   if (pcb->rttest == 0) {
01098     pcb->rttest = tcp_ticks;
01099     pcb->rtseq = ntohl(seg->tcphdr->seqno);
01100 
01101     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
01102   }
01103   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
01104           htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
01105           seg->len));
01106 
01107   len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
01108 
01109   seg->p->len -= len;
01110   seg->p->tot_len -= len;
01111 
01112   seg->p->payload = seg->tcphdr;
01113 
01114   seg->tcphdr->chksum = 0;
01115 #if CHECKSUM_GEN_TCP
01116 #if TCP_CHECKSUM_ON_COPY
01117   {
01118     u32_t acc;
01119 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
01120     u16_t chksum_slow = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
01121            &(pcb->remote_ip),
01122            IP_PROTO_TCP, seg->p->tot_len);
01123 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
01124     if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
01125       LWIP_ASSERT("data included but not checksummed",
01126         seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
01127     }
01128 
01129     /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
01130     acc = inet_chksum_pseudo_partial(seg->p, &(pcb->local_ip),
01131              &(pcb->remote_ip),
01132              IP_PROTO_TCP, seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4);
01133     /* add payload checksum */
01134     if (seg->chksum_swapped) {
01135       seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
01136       seg->chksum_swapped = 0;
01137     }
01138     acc += (u16_t)~(seg->chksum);
01139     seg->tcphdr->chksum = FOLD_U32T(acc);
01140 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
01141     if (chksum_slow != seg->tcphdr->chksum) {
01142       LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING,
01143                   ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
01144                   seg->tcphdr->chksum, chksum_slow));
01145       seg->tcphdr->chksum = chksum_slow;
01146     }
01147 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
01148   }
01149 #else /* TCP_CHECKSUM_ON_COPY */
01150   seg->tcphdr->chksum = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
01151          &(pcb->remote_ip),
01152          IP_PROTO_TCP, seg->p->tot_len);
01153 #endif /* TCP_CHECKSUM_ON_COPY */
01154 #endif /* CHECKSUM_GEN_TCP */
01155   TCP_STATS_INC(tcp.xmit);
01156 
01157 #if LWIP_NETIF_HWADDRHINT
01158   ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
01159       IP_PROTO_TCP, &(pcb->addr_hint));
01160 #else /* LWIP_NETIF_HWADDRHINT*/
01161   ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
01162       IP_PROTO_TCP);
01163 #endif /* LWIP_NETIF_HWADDRHINT*/
01164 }
01165 
01166 /**
01167  * Send a TCP RESET packet (empty segment with RST flag set) either to
01168  * abort a connection or to show that there is no matching local connection
01169  * for a received segment.
01170  *
01171  * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
01172  * matching local pcb was found), tcp_listen_input() (if incoming segment
01173  * has ACK flag set) and tcp_process() (received segment in the wrong state)
01174  *
01175  * Since a RST segment is in most cases not sent for an active connection,
01176  * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
01177  * most other segment output functions.
01178  *
01179  * @param seqno the sequence number to use for the outgoing segment
01180  * @param ackno the acknowledge number to use for the outgoing segment
01181  * @param local_ip the local IP address to send the segment from
01182  * @param remote_ip the remote IP address to send the segment to
01183  * @param local_port the local TCP port to send the segment from
01184  * @param remote_port the remote TCP port to send the segment to
01185  */
01186 void
01187 tcp_rst(u32_t seqno, u32_t ackno,
01188   ip_addr_t *local_ip, ip_addr_t *remote_ip,
01189   u16_t local_port, u16_t remote_port)
01190 {
01191   struct pbuf *p;
01192   struct tcp_hdr *tcphdr;
01193   p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
01194   if (p == NULL) {
01195       LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
01196       return;
01197   }
01198   LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
01199               (p->len >= sizeof(struct tcp_hdr)));
01200 
01201   tcphdr = (struct tcp_hdr *)p->payload;
01202   tcphdr->src = htons(local_port);
01203   tcphdr->dest = htons(remote_port);
01204   tcphdr->seqno = htonl(seqno);
01205   tcphdr->ackno = htonl(ackno);
01206   TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
01207   tcphdr->wnd = PP_HTONS(TCP_WND);
01208   tcphdr->chksum = 0;
01209   tcphdr->urgp = 0;
01210 
01211 #if CHECKSUM_GEN_TCP
01212   tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
01213               IP_PROTO_TCP, p->tot_len);
01214 #endif
01215   TCP_STATS_INC(tcp.xmit);
01216   snmp_inc_tcpoutrsts();
01217    /* Send output with hardcoded TTL since we have no access to the pcb */
01218   ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
01219   pbuf_free(p);
01220   LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
01221 }
01222 
01223 /**
01224  * Requeue all unacked segments for retransmission
01225  *
01226  * Called by tcp_slowtmr() for slow retransmission.
01227  *
01228  * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
01229  */
01230 void
01231 tcp_rexmit_rto(struct tcp_pcb *pcb)
01232 {
01233   struct tcp_seg *seg;
01234 
01235   if (pcb->unacked == NULL) {
01236     return;
01237   }
01238 
01239   /* Move all unacked segments to the head of the unsent queue */
01240   for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
01241   /* concatenate unsent queue after unacked queue */
01242   seg->next = pcb->unsent;
01243   /* unsent queue is the concatenated queue (of unacked, unsent) */
01244   pcb->unsent = pcb->unacked;
01245   /* unacked queue is now empty */
01246   pcb->unacked = NULL;
01247 
01248   /* increment number of retransmissions */
01249   ++pcb->nrtx;
01250 
01251   /* Don't take any RTT measurements after retransmitting. */
01252   pcb->rttest = 0;
01253 
01254   /* Do the actual retransmission */
01255   tcp_output(pcb);
01256 }
01257 
01258 /**
01259  * Requeue the first unacked segment for retransmission
01260  *
01261  * Called by tcp_receive() for fast retramsmit.
01262  *
01263  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
01264  */
01265 void
01266 tcp_rexmit(struct tcp_pcb *pcb)
01267 {
01268   struct tcp_seg *seg;
01269   struct tcp_seg **cur_seg;
01270 
01271   if (pcb->unacked == NULL) {
01272     return;
01273   }
01274 
01275   /* Move the first unacked segment to the unsent queue */
01276   /* Keep the unsent queue sorted. */
01277   seg = pcb->unacked;
01278   pcb->unacked = seg->next;
01279 
01280   cur_seg = &(pcb->unsent);
01281   while (*cur_seg &&
01282     TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
01283       cur_seg = &((*cur_seg)->next );
01284   }
01285   seg->next = *cur_seg;
01286   *cur_seg = seg;
01287 
01288   ++pcb->nrtx;
01289 
01290   /* Don't take any rtt measurements after retransmitting. */
01291   pcb->rttest = 0;
01292 
01293   /* Do the actual retransmission. */
01294   snmp_inc_tcpretranssegs();
01295   /* No need to call tcp_output: we are always called from tcp_input()
01296      and thus tcp_output directly returns. */
01297 }
01298 
01299 
01300 /**
01301  * Handle retransmission after three dupacks received
01302  *
01303  * @param pcb the tcp_pcb for which to retransmit the first unacked segment
01304  */
01305 void 
01306 tcp_rexmit_fast(struct tcp_pcb *pcb)
01307 {
01308   if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
01309     /* This is fast retransmit. Retransmit the first unacked segment. */
01310     LWIP_DEBUGF(TCP_FR_DEBUG, 
01311                 ("tcp_receive: dupacks %"U16_F" (%"U32_F
01312                  "), fast retransmit %"U32_F"\n",
01313                  (u16_t)pcb->dupacks, pcb->lastack,
01314                  ntohl(pcb->unacked->tcphdr->seqno)));
01315     tcp_rexmit(pcb);
01316 
01317     /* Set ssthresh to half of the minimum of the current
01318      * cwnd and the advertised window */
01319     if (pcb->cwnd > pcb->snd_wnd) {
01320       pcb->ssthresh = pcb->snd_wnd / 2;
01321     } else {
01322       pcb->ssthresh = pcb->cwnd / 2;
01323     }
01324     
01325     /* The minimum value for ssthresh should be 2 MSS */
01326     if (pcb->ssthresh < 2*pcb->mss) {
01327       LWIP_DEBUGF(TCP_FR_DEBUG, 
01328                   ("tcp_receive: The minimum value for ssthresh %"U16_F
01329                    " should be min 2 mss %"U16_F"...\n",
01330                    pcb->ssthresh, 2*pcb->mss));
01331       pcb->ssthresh = 2*pcb->mss;
01332     }
01333     
01334     pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
01335     pcb->flags |= TF_INFR;
01336   } 
01337 }
01338 
01339 
01340 /**
01341  * Send keepalive packets to keep a connection active although
01342  * no data is sent over it.
01343  *
01344  * Called by tcp_slowtmr()
01345  *
01346  * @param pcb the tcp_pcb for which to send a keepalive packet
01347  */
01348 void
01349 tcp_keepalive(struct tcp_pcb *pcb)
01350 {
01351   struct pbuf *p;
01352   struct tcp_hdr *tcphdr;
01353 
01354   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
01355                           ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
01356                           ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
01357 
01358   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F"   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n", 
01359                           tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
01360    
01361   p = tcp_output_alloc_header(pcb, 0, 0, htonl(pcb->snd_nxt - 1));
01362   if(p == NULL) {
01363     LWIP_DEBUGF(TCP_DEBUG, 
01364                 ("tcp_keepalive: could not allocate memory for pbuf\n"));
01365     return;
01366   }
01367   tcphdr = (struct tcp_hdr *)p->payload;
01368 
01369 #if CHECKSUM_GEN_TCP
01370   tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
01371                                       IP_PROTO_TCP, p->tot_len);
01372 #endif
01373   TCP_STATS_INC(tcp.xmit);
01374 
01375   /* Send output to IP */
01376 #if LWIP_NETIF_HWADDRHINT
01377   ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
01378     &(pcb->addr_hint));
01379 #else /* LWIP_NETIF_HWADDRHINT*/
01380   ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
01381 #endif /* LWIP_NETIF_HWADDRHINT*/
01382 
01383   pbuf_free(p);
01384 
01385   LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
01386                           pcb->snd_nxt - 1, pcb->rcv_nxt));
01387 }
01388 
01389 
01390 /**
01391  * Send persist timer zero-window probes to keep a connection active
01392  * when a window update is lost.
01393  *
01394  * Called by tcp_slowtmr()
01395  *
01396  * @param pcb the tcp_pcb for which to send a zero-window probe packet
01397  */
01398 void
01399 tcp_zero_window_probe(struct tcp_pcb *pcb)
01400 {
01401   struct pbuf *p;
01402   struct tcp_hdr *tcphdr;
01403   struct tcp_seg *seg;
01404   u16_t len;
01405   u8_t is_fin;
01406 
01407   LWIP_DEBUGF(TCP_DEBUG, 
01408               ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
01409                U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
01410                ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
01411                ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
01412 
01413   LWIP_DEBUGF(TCP_DEBUG, 
01414               ("tcp_zero_window_probe: tcp_ticks %"U32_F
01415                "   pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n", 
01416                tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
01417 
01418   seg = pcb->unacked;
01419 
01420   if(seg == NULL) {
01421     seg = pcb->unsent;
01422   }
01423   if(seg == NULL) {
01424     return;
01425   }
01426 
01427   is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
01428   /* we want to send one seqno: either FIN or data (no options) */
01429   len = is_fin ? 0 : 1;
01430 
01431   p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
01432   if(p == NULL) {
01433     LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
01434     return;
01435   }
01436   tcphdr = (struct tcp_hdr *)p->payload;
01437 
01438   if (is_fin) {
01439     /* FIN segment, no data */
01440     TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
01441   } else {
01442     /* Data segment, copy in one byte from the head of the unacked queue */
01443     struct tcp_hdr *thdr = (struct tcp_hdr *)seg->p->payload;
01444     char *d = ((char *)p->payload + TCP_HLEN);
01445     pbuf_copy_partial(seg->p, d, 1, TCPH_HDRLEN(thdr) * 4);
01446   }
01447 
01448 #if CHECKSUM_GEN_TCP
01449   tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
01450                                       IP_PROTO_TCP, p->tot_len);
01451 #endif
01452   TCP_STATS_INC(tcp.xmit);
01453 
01454   /* Send output to IP */
01455 #if LWIP_NETIF_HWADDRHINT
01456   ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
01457     &(pcb->addr_hint));
01458 #else /* LWIP_NETIF_HWADDRHINT*/
01459   ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
01460 #endif /* LWIP_NETIF_HWADDRHINT*/
01461 
01462   pbuf_free(p);
01463 
01464   LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
01465                           " ackno %"U32_F".\n",
01466                           pcb->snd_nxt - 1, pcb->rcv_nxt));
01467 }
01468 #endif /* LWIP_TCP */