⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp_out.c

📁 最新的lwip 1.3.0版本在ucos平台上的移植
💻 C
📖 第 1 页 / 共 3 页
字号:
/** * @file * Transmission Control Protocol, outgoing traffic * * The output functions of TCP. * *//* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, *    this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, *    this list of conditions and the following disclaimer in the documentation *    and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * */#include "lwip/opt.h"#if LWIP_TCP /* don't build if not configured for use in lwipopts.h */#include "lwip/tcp.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/memp.h"#include "lwip/sys.h"#include "lwip/ip_addr.h"#include "lwip/netif.h"#include "lwip/inet.h"#include "lwip/inet_chksum.h"#include "lwip/stats.h"#include "lwip/snmp.h"#include <string.h>/* Forward declarations.*/static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);/** * Called by tcp_close() to send a segment including flags but not data. * * @param pcb the tcp_pcb over which to send a segment * @param flags the flags to set in the segment header * @return ERR_OK if sent, another err_t otherwise */err_ttcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags){  /* no data, no length, flags, copy=1, no optdata, no optdatalen */  return tcp_enqueue(pcb, NULL, 0, flags, TCP_WRITE_FLAG_COPY, NULL, 0);}/** * Write data for sending (but does not send it immediately). * * It waits in the expectation of more data being sent soon (as * it can send them more efficiently by combining them together). * To prompt the system to send data now, call tcp_output() after * calling tcp_write(). *  * @param pcb Protocol control block of the TCP connection to enqueue data for. * @param data pointer to the data to send * @param len length (in bytes) of the data to send * @param apiflags combination of following flags : * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent, * @return ERR_OK if enqueued, another err_t on error *  * @see tcp_write() */err_ttcp_write(struct tcp_pcb *pcb, const void *data, u16_t len, u8_t apiflags){  LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n", (void *)pcb,    data, len, (u16_t)apiflags));  /* connection is in valid state for data transmission? */  if (pcb->state == ESTABLISHED ||     pcb->state == CLOSE_WAIT ||     pcb->state == SYN_SENT ||     pcb->state == SYN_RCVD) {    if (len > 0) {      return tcp_enqueue(pcb, (void *)data, len, 0, apiflags, NULL, 0);    }    return ERR_OK;  } else {    LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | 3, ("tcp_write() called in invalid state\n"));    return ERR_CONN;  }}/** * Enqueue either data or TCP options (but not both) for tranmission * * Called by tcp_connect(), tcp_listen_input(), tcp_send_ctrl() and tcp_write(). * * @param pcb Protocol control block for the TCP connection to enqueue data for. * @param arg Pointer to the data to be enqueued for sending. * @param len Data length in bytes * @param flags tcp header flags to set in the outgoing segment * @param apiflags combination of following flags : * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent, * @param optdata * @param optlen */err_ttcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len,  u8_t flags, u8_t apiflags,  u8_t *optdata, u8_t optlen){  struct pbuf *p;  struct tcp_seg *seg, *useg, *queue;  u32_t seqno;  u16_t left, seglen;  void *ptr;  u16_t queuelen;  LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue(pcb=%p, arg=%p, len=%"U16_F", flags=%"X16_F", apiflags=%"U16_F")\n",    (void *)pcb, arg, len, (u16_t)flags, (u16_t)apiflags));  LWIP_ERROR("tcp_enqueue: len == 0 || optlen == 0 (programmer violates API)",      ((len == 0) || (optlen == 0)), return ERR_ARG;);  LWIP_ERROR("tcp_enqueue: arg == NULL || optdata == NULL (programmer violates API)",      ((arg == NULL) || (optdata == NULL)), return ERR_ARG;);  /* fail on too much data */  if (len > pcb->snd_buf) {    LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n", len, pcb->snd_buf));    pcb->flags |= TF_NAGLEMEMERR;    return ERR_MEM;  }  left = len;  ptr = arg;  /* seqno will be the sequence number of the first segment enqueued   * by the call to this function. */  seqno = pcb->snd_lbb;  LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));  /* If total number of pbufs on the unsent/unacked queues exceeds the   * configured maximum, return an error */  queuelen = pcb->snd_queuelen;  /* check for configured max queuelen and possible overflow */  if ((queuelen >= TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {    LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too long queue %"U16_F" (max %"U16_F")\n", queuelen, TCP_SND_QUEUELEN));    TCP_STATS_INC(tcp.memerr);    pcb->flags |= TF_NAGLEMEMERR;    return ERR_MEM;  }  if (queuelen != 0) {    LWIP_ASSERT("tcp_enqueue: pbufs on queue => at least one queue non-empty",      pcb->unacked != NULL || pcb->unsent != NULL);  } else {    LWIP_ASSERT("tcp_enqueue: no pbufs on queue => both queues empty",      pcb->unacked == NULL && pcb->unsent == NULL);  }  /* First, break up the data into segments and tuck them together in   * the local "queue" variable. */  useg = queue = seg = NULL;  seglen = 0;  while (queue == NULL || left > 0) {    /* The segment length should be the MSS if the data to be enqueued     * is larger than the MSS. */    seglen = left > pcb->mss? pcb->mss: left;    /* Allocate memory for tcp_seg, and fill in fields. */    seg = memp_malloc(MEMP_TCP_SEG);    if (seg == NULL) {      LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for tcp_seg\n"));      goto memerr;    }    seg->next = NULL;    seg->p = NULL;    /* first segment of to-be-queued data? */    if (queue == NULL) {      queue = seg;    }    /* subsequent segments of to-be-queued data */    else {      /* Attach the segment to the end of the queued segments */      LWIP_ASSERT("useg != NULL", useg != NULL);      useg->next = seg;    }    /* remember last segment of to-be-queued data for next iteration */    useg = seg;    /* If copy is set, memory should be allocated     * and data copied into pbuf, otherwise data comes from     * ROM or other static memory, and need not be copied. If     * optdata is != NULL, we have options instead of data. */         /* options? */    if (optdata != NULL) {      if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {        goto memerr;      }      LWIP_ASSERT("check that first pbuf can hold optlen",                  (seg->p->len >= optlen));      queuelen += pbuf_clen(seg->p);      seg->dataptr = seg->p->payload;    }    /* copy from volatile memory? */    else if (apiflags & TCP_WRITE_FLAG_COPY) {      if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_RAM)) == NULL) {        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));        goto memerr;      }      LWIP_ASSERT("check that first pbuf can hold the complete seglen",                  (seg->p->len >= seglen));      queuelen += pbuf_clen(seg->p);      if (arg != NULL) {        MEMCPY(seg->p->payload, ptr, seglen);      }      seg->dataptr = seg->p->payload;    }    /* do not copy data */    else {      /* First, allocate a pbuf for holding the data.       * since the referenced data is available at least until it is sent out on the       * link (as it has to be ACKed by the remote party) we can safely use PBUF_ROM       * instead of PBUF_REF here.       */      if ((p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for zero-copy pbuf\n"));        goto memerr;      }      ++queuelen;      /* reference the non-volatile payload data */      p->payload = ptr;      seg->dataptr = ptr;      /* Second, allocate a pbuf for the headers. */      if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_RAM)) == NULL) {        /* If allocation fails, we have to deallocate the data pbuf as         * well. */        pbuf_free(p);        LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: could not allocate memory for header pbuf\n"));        goto memerr;      }      queuelen += pbuf_clen(seg->p);      /* Concatenate the headers and data pbufs together. */      pbuf_cat(seg->p/*header*/, p/*data*/);      p = NULL;    }    /* Now that there are more segments queued, we check again if the    length of the queue exceeds the configured maximum or overflows. */    if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {      LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));      goto memerr;    }    seg->len = seglen;    /* build TCP header */    if (pbuf_header(seg->p, TCP_HLEN)) {      LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: no room for TCP header in pbuf.\n"));      TCP_STATS_INC(tcp.err);      goto memerr;    }    seg->tcphdr = seg->p->payload;    seg->tcphdr->src = htons(pcb->local_port);    seg->tcphdr->dest = htons(pcb->remote_port);    seg->tcphdr->seqno = htonl(seqno);    seg->tcphdr->urgp = 0;    TCPH_FLAGS_SET(seg->tcphdr, flags);    /* don't fill in tcphdr->ackno and tcphdr->wnd until later */    /* Copy the options into the header, if they are present. */    if (optdata == NULL) {      TCPH_HDRLEN_SET(seg->tcphdr, 5);    }    else {      TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));      /* Copy options into data portion of segment.       Options can thus only be sent in non data carrying       segments such as SYN|ACK. */      SMEMCPY(seg->dataptr, optdata, optlen);    }    LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_enqueue: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",      ntohl(seg->tcphdr->seqno),      ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),      (u16_t)flags));    left -= seglen;    seqno += seglen;    ptr = (void *)((u8_t *)ptr + seglen);  }  /* Now that the data to be enqueued has been broken up into TCP  segments in the queue variable, we add them to the end of the  pcb->unsent queue. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -