📄 tcp_out.c
字号:
/** * @file * * Transmission Control Protocol, outgoing traffic *//* * 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> * *//* tcp_output.c * * The output functions of TCP. * */#include "lwip/def.h"#include "lwip/opt.h"#include "lwip/mem.h"#include "lwip/memp.h"#include "lwip/sys.h"#include "lwip/netif.h"#include "lwip/inet.h"#include "lwip/tcp.h"#include "lwip/stats.h"#if LWIP_TCP/* Forward declarations.*/static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);err_ttcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags){ return tcp_enqueue(pcb, NULL, 0, flags, 1, NULL, 0);}/* * NB. tcp_write() enqueues data for sending, but does not send it * straight away. 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(). */err_ttcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t copy){ LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, arg=%p, len=%u, copy=%d)\n", (void *)pcb, arg, len, (unsigned int)copy)); if (pcb->state == SYN_SENT || pcb->state == SYN_RCVD || pcb->state == ESTABLISHED || pcb->state == CLOSE_WAIT) { if (len > 0) { return tcp_enqueue(pcb, (void *)arg, len, 0, copy, NULL, 0); } return ERR_OK; } else { LWIP_DEBUGF(TCP_OUTPUT_DEBUG | DBG_STATE | 3, ("tcp_write() called in invalid state\n")); return ERR_CONN; }}err_ttcp_enqueue(struct tcp_pcb *pcb, void *arg, u16_t len, u8_t flags, u8_t copy, u8_t *optdata, u8_t optlen){ struct pbuf *p; struct tcp_seg *seg, *useg, *queue; u32_t left, seqno; u16_t seglen; void *ptr; u8_t queuelen; LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue(pcb=%p, arg=%p, len=%u, flags=%x, copy=%u)\n", (void *)pcb, arg, len, (unsigned int)flags, (unsigned int)copy)); left = len; ptr = arg; /* fail on too much data */ if (len > pcb->snd_buf) { LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too much data (len=%u > snd_buf=%u)\n", len, pcb->snd_buf)); return ERR_MEM; } /* seqno will be the sequence number of the first segment enqueued * by the call to this function. */ seqno = pcb->snd_lbb; queue = NULL; LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: queuelen: %u\n", (unsigned int)pcb->snd_queuelen)); /* Check if the queue length exceeds the configured maximum queue * length. If so, we return an error. */ queuelen = pcb->snd_queuelen; if (queuelen >= TCP_SND_QUEUELEN) { LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue: too long queue %u (max %u)\n", queuelen, TCP_SND_QUEUELEN)); goto memerr; } if (pcb->snd_queuelen != 0) { LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL || pcb->unsent != NULL); } seg = useg = NULL; seglen = 0; /* First, break up the data into segments and tuck them together in * the local "queue" variable. */ 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; if (queue == NULL) { useg = queue = seg; } else { /* Attach the segment to the end of the queued segments. */ LWIP_ASSERT("useg != NULL", useg != NULL); useg->next = seg; 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. */ if (optdata != NULL) { if ((seg->p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) { goto memerr; } ++queuelen; seg->dataptr = seg->p->payload; } else if (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 %u\n", seglen)); goto memerr; } ++queuelen; 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; 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; /* Concatenate the headers and data pbufs together. */ pbuf_cat(seg->p, p); p = NULL; } /* Now that there are more segments queued, we check again if the length of the queue exceeds the configured maximum. */ if (queuelen > TCP_SND_QUEUELEN) { LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_enqueue: queue too long %u (%u)\n", queuelen, TCP_SND_QUEUELEN)); goto memerr; } seg->len = seglen;#if 0 /* Was commented out. TODO: can someone say why this is here? */ if ((flags & TCP_SYN) || (flags & TCP_FIN)) { ++seg->len; }#endif /* 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. */ memcpy(seg->dataptr, optdata, optlen); } LWIP_DEBUGF(TCP_OUTPUT_DEBUG | DBG_TRACE, ("tcp_enqueue: queueing %lu:%lu (0x%x)\n", ntohl(seg->tcphdr->seqno), ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg), flags)); left -= seglen; seqno += seglen; ptr = (void *)((char *)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. */ if (pcb->unsent == NULL) { useg = NULL; } else { for (useg = pcb->unsent; useg->next != NULL; useg = useg->next); } /* If there is room in the last pbuf on the unsent queue, chain the first pbuf on the queue together with that. */ if (useg != NULL && TCP_TCPLEN(useg) != 0 && !(TCPH_FLAGS(useg->tcphdr) & (TCP_SYN | TCP_FIN)) && !(flags & (TCP_SYN | TCP_FIN)) && useg->len + queue->len <= pcb->mss) { /* Remove TCP header from first segment. */ pbuf_header(queue->p, -TCP_HLEN); pbuf_cat(useg->p, queue->p); useg->len += queue->len; useg->next = queue->next; LWIP_DEBUGF(TCP_OUTPUT_DEBUG | DBG_TRACE | DBG_STATE, ("tcp_enqueue: chaining, new len %u\n", useg->len)); if (seg == queue) { seg = NULL; } memp_free(MEMP_TCP_SEG, queue); } else { if (useg == NULL) { pcb->unsent = queue; } else { useg->next = queue; } } if ((flags & TCP_SYN) || (flags & TCP_FIN)) { ++len; } pcb->snd_lbb += len; pcb->snd_buf -= len; pcb->snd_queuelen = queuelen; LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: %d (after enqueued)\n", pcb->snd_queuelen)); if (pcb->snd_queuelen != 0) { LWIP_ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL || pcb->unsent != NULL); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -