📄 tcp_output.c.svn-base
字号:
/* * Copyright (c) 2001, 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. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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> * * $Id: tcp_output.c,v 1.1.1.1 2006/06/07 05:27:50 kaohj Exp $ *//*-----------------------------------------------------------------------------------*//* tcp_output.c * * The output functions of TCP. * *//*-----------------------------------------------------------------------------------*/#include "debug.h"#include "def.h"#include "opt.h"//#include "arch/lib.h"
#ifndef __ARCH_LIB_H__
#define __ARCH_LIB_H__
#ifndef _STRING_H_
#ifndef _STRING_H
int strlen(const char *str);
int strncmp(const char *str1, const char *str2, int len);
//void bcopy(const void *src, void *dest, int len);
//void xzero(void *data, int n);
#endif /* _STRING_H */
#endif /* _STRING_H_ */
#endif /* __ARCH_LIB_H__ */
#include "mem.h"#include "memp.h"//#include "lwip/sys.h"//#include "lwip/netif.h"//#include "lwip/inet.h"
#ifndef __LWIP_INET_H__
#define __LWIP_INET_H__
#include "arch.h"
#include "opt.h"
#include "pbuf.h"
//#include "lwip/ip_addr.h"
//u16_t inet_chksum(void *dataptr, u16_t len);
//u16_t inet_chksum_pbuf(struct pbuf *p);
//u16_t inet_chksum_pseudo(struct pbuf *p,
// struct ip_addr *src, struct ip_addr *dest,
// u8_t proto, u16_t proto_len);
u16_t inet_chksum_pseudo(struct pbuf *p,
u32_t *src, u32_t *dest,
u8_t proto, u32_t proto_len);
#ifdef HTONS
#undef HTONS
#endif /* HTONS */
#ifdef NTOHS
#undef NTOHS
#endif /* NTOHS */
#ifdef HTONL
#undef HTONL
#endif /* HTONL */
#ifdef NTOHL
#undef NTOHL
#endif /* NTOHL */
#ifndef HTONS
# if BYTE_ORDER == BIG_ENDIAN
# define HTONS(n) (n)
# define htons(n) HTONS(n)
# else /* BYTE_ORDER == BIG_ENDIAN */
# define HTONS(n) (((((u16_t)(n) & 0xff)) << 8) | (((u16_t)(n) & 0xff00) >> 8))
# endif /* BYTE_ORDER == BIG_ENDIAN */
#endif /* HTONS */
#ifdef NTOHS
#undef NTOHS
#endif /* NTOHS */
#ifdef ntohs
#undef ntohs
#endif /* ntohs */
#define NTOHS HTONS
#define ntohs htons
#ifndef HTONL
# if BYTE_ORDER == BIG_ENDIAN
# define HTONL(n) (n)
# define htonl(n) HTONL(n)
# else /* BYTE_ORDER == BIG_ENDIAN */
# define HTONL(n) (((((u32_t)(n) & 0xff)) << 24) | \
((((u32_t)(n) & 0xff00)) << 8) | \
((((u32_t)(n) & 0xff0000)) >> 8) | \
((((u32_t)(n) & 0xff000000)) >> 24))
# endif /* BYTE_ORDER == BIG_ENDIAN */
#endif /* HTONL */
#ifdef ntohl
#undef ntohl
#endif /* ntohl */
#ifdef NTOHL
#undef NTOHL
#endif /* NTOHL */
#define NTOHL HTONL
#define ntohl htonl
#ifndef _MACHINE_ENDIAN_H_
#ifndef _NETINET_IN_H
#ifndef _LINUX_BYTEORDER_GENERIC_H
#if BYTE_ORDER == LITTLE_ENDIAN
u16_t htons(u16_t n);
u32_t htonl(u32_t n);
#else
#endif /* BYTE_ORDER == LITTLE_ENDIAN */
#endif /* _LINUX_BYTEORDER_GENERIC_H */
#endif /* _NETINET_IN_H */
#endif /* _MACHINE_ENDIAN_H_ */
#endif /* __LWIP_INET_H__ */
#include "tcp.h"#include "stats.h"#define MIN(x,y) (x) < (y)? (x): (y)/* Forward declarations.*/static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
static err_t tcp_enqueue2(struct tcp_pcb *pcb, void *arg, u16_t len,
u8_t flags, u8_t copy,
u8_t *optdata, u8_t optlen);
/*-----------------------------------------------------------------------------------*/err_ttcp_send_ctrl(struct tcp_pcb *pcb, u8_t flags){ return tcp_enqueue(pcb, NULL, 0, flags, 1, NULL, 0);}/*-----------------------------------------------------------------------------------*/err_ttcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t copy){ if(pcb->state == SYN_SENT || pcb->state == SYN_RCVD || pcb->state == ESTABLISHED || pcb->state == CLOSE_WAIT) {
// printf("tcp_write here. length is %d.\n", len); if(len > 0) { return tcp_enqueue(pcb, (void *)arg, len, 0, copy, NULL, 0); } return ERR_OK; } else { 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; left = len; ptr = arg; if(len > pcb->snd_buf) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: too much data %d\n", len));
// printf("error here 0\n"); return ERR_MEM; } seqno = pcb->snd_lbb; queue = NULL; DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue: %d\n", pcb->snd_queuelen)); queuelen = pcb->snd_queuelen; if(queuelen >= TCP_SND_QUEUELEN) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: too long queue %d (max %d)\n", queuelen, TCP_SND_QUEUELEN)); goto memerr; } #ifdef LWIP_DEBUG if(pcb->snd_queuelen != 0) { ASSERT("tcp_enqueue: valid queue length", pcb->unacked != NULL || pcb->unsent != NULL); }#endif /* LWIP_DEBUG */ seg = NULL; seglen = 0; while(queue == NULL || left > 0) { 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) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: could not allocate memory for tcp_seg\n")); goto memerr; } seg->next = NULL; seg->p = NULL; if(queue == NULL) { queue = seg; } else { for(useg = queue; useg->next != NULL; useg = useg->next); useg->next = 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) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: could not allocate memory for pbuf copy\n")); goto memerr; } ++queuelen; if(arg != NULL) { bcopy(ptr, seg->p->payload, seglen); } seg->dataptr = seg->p->payload; } else { /* Do not copy the data. */ if((p = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: could not allocate memory for pbuf non-copy\n")); goto memerr; } ++queuelen; p->payload = ptr; seg->dataptr = ptr; if((seg->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_RAM)) == NULL) { pbuf_free(p); DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: could not allocate memory for header pbuf\n")); goto memerr; } ++queuelen; pbuf_chain(seg->p, p); } if(queuelen > TCP_SND_QUEUELEN) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: queue too long %d (%d)\n", queuelen, TCP_SND_QUEUELEN)); goto memerr; } seg->len = seglen; /* if((flags & TCP_SYN) || (flags & TCP_FIN)) { ++seg->len; }*/ /* build TCP header */ if(pbuf_header(seg->p, TCP_HLEN)) { DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_enqueue: no room for TCP header in pbuf.\n")); #ifdef TCP_STATS ++stats.tcp.err;#endif /* TCP_STATS */ 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);
// printf("tcp src is %d, tcp dst is %d.\n", seg->tcphdr->src, seg->tcphdr->dest); /* don't fill in tcphdr->ackno and tcphdr->wnd until later */ if(optdata == NULL) { TCPH_OFFSET_SET(seg->tcphdr, 5 << 4);
// printf("enqueue opt is zero.\n"); } else { TCPH_OFFSET_SET(seg->tcphdr, (5 + optlen / 4) << 4);
// printf("enqueue opt is not zero.\n"); /* Copy options into data portion of segment. Options can thus only be sent in non data carrying segments such as SYN|ACK. */ bcopy(optdata, seg->dataptr, optlen); } DEBUGF(TCP_OUTPUT_DEBUG, ("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); } /* Go to the last segment on the ->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. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -