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

📄 tcp.c.svn-base

📁 realtek的8186芯片ADSL路由AP源代码
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
/* * 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.c,v 1.1.1.1 2006/06/07 05:27:50 kaohj Exp $ *//*-----------------------------------------------------------------------------------*//* tcp.c * * This file contains common functions for the TCP implementation, such as functinos * for manipulating the data structures and the TCP timer functions. TCP functions * related to input and output is found in tcp_input.c and tcp_output.c respectively. * *//*-----------------------------------------------------------------------------------*/#include "debug.h"#include "def.h"#include "mem.h"#include "memp.h"#include "tcp.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__ */







/* Incremented every coarse grained timer shot   (typically every 500 ms, determined by TCP_COARSE_TIMEOUT). */u32_t tcp_ticks;const u8_t tcp_backoff[13] =    { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };/* The TCP PCB lists. */struct tcp_pcb_listen *tcp_listen_pcbs;  /* List of all TCP PCBs in LISTEN state. */struct tcp_pcb *tcp_active_pcbs;  /* List of all TCP PCBs that are in a				 state in which they accept or send				 data. */struct tcp_pcb *tcp_tw_pcbs;      /* List of all TCP PCBs in TIME-WAIT. */struct tcp_pcb *tcp_tmp_pcb;#define MIN(x,y) (x) < (y)? (x): (y)#if MEMP_RECLAIMstatic u8_t tcp_memp_reclaim(void *arg, memp_t type);#endif#if MEM_RECLAIMstatic mem_size_t tcp_mem_reclaim(void *arg, mem_size_t size);#endifstatic u8_t tcp_timer;/*-----------------------------------------------------------------------------------*//* * tcp_init(): * * Initializes the TCP layer. *//*-----------------------------------------------------------------------------------*/voidtcp_init(void){  /* Clear globals. */  tcp_listen_pcbs = NULL;  tcp_active_pcbs = NULL;  tcp_tw_pcbs = NULL;  tcp_tmp_pcb = NULL;    /* Register memory reclaim function */#if MEM_RECLAIM  mem_register_reclaim((mem_reclaim_func)tcp_mem_reclaim, NULL);#endif /* MEM_RECLAIM */#if MEMP_RECLAIM  memp_register_reclaim(MEMP_PBUF, (memp_reclaim_func)tcp_memp_reclaim, NULL);  memp_register_reclaim(MEMP_TCP_SEG, (memp_reclaim_func)tcp_memp_reclaim, NULL);  memp_register_reclaim(MEMP_TCP_PCB, (memp_reclaim_func)tcp_memp_reclaim, NULL);#endif /* MEMP_RECLAIM */  /* initialize timer */  tcp_ticks = 0;  tcp_timer = 0;  }/*-----------------------------------------------------------------------------------*//* * tcp_tmr(): * * Called periodically to dispatch TCP timers. * *//*-----------------------------------------------------------------------------------*/
#if 0voidtcp_tmr(){  ++tcp_timer;  if(tcp_timer == 10) {    tcp_timer = 0;  }    if(tcp_timer & 1) {    /* Call tcp_fasttmr() every 200 ms, i.e., every other timer       tcp_tmr() is called. */    tcp_fasttmr();  }  if(tcp_timer == 0 || tcp_timer == 5) {    /* Call tcp_slowtmr() every 500 ms, i.e., every fifth timer       tcp_tmr() is called. */    tcp_slowtmr();  }}
#endif/*-----------------------------------------------------------------------------------*//* * tcp_close(): * * Closes the connection held by the PCB. * *//*-----------------------------------------------------------------------------------*/err_ttcp_close(struct tcp_pcb *pcb){  err_t err;#if TCP_DEBUG  DEBUGF(TCP_DEBUG, ("tcp_close: closing in state "));  tcp_debug_print_state(pcb->state);  DEBUGF(TCP_DEBUG, ("\n"));#endif /* TCP_DEBUG */  switch(pcb->state) {  case LISTEN:    err = ERR_OK;    tcp_pcb_remove((struct tcp_pcb **)&tcp_listen_pcbs, pcb);    memp_free(MEMP_TCP_PCB_LISTEN, pcb);    pcb = NULL;    break;  case SYN_SENT:    err = ERR_OK;    tcp_pcb_remove(&tcp_active_pcbs, pcb);    memp_free(MEMP_TCP_PCB, pcb);    pcb = NULL;    break;  case SYN_RCVD:    err = tcp_send_ctrl(pcb, TCP_FIN);    if(err == ERR_OK) {      pcb->state = FIN_WAIT_1;    }    break;  case ESTABLISHED:
//	  printf("State is ESTABLISHED.\n");    err = tcp_send_ctrl(pcb, TCP_FIN);    if(err == ERR_OK) {      pcb->state = FIN_WAIT_1;    }    break;  case CLOSE_WAIT:    err = tcp_send_ctrl(pcb, TCP_FIN);    if(err == ERR_OK) {      pcb->state = LAST_ACK;    }    break;  default:    /* Has already been closed, do nothing. */    err = ERR_OK;    pcb = NULL;    break;  }  if(pcb != NULL && err == ERR_OK) {    err = tcp_output(pcb);  }  return err;}/*-----------------------------------------------------------------------------------*//* * tcp_abort() * * Aborts a connection by sending a RST to the remote host and deletes * the local protocol control block. This is done when a connection is * killed because of shortage of memory. * *//*-----------------------------------------------------------------------------------*/voidtcp_abort(struct tcp_pcb *pcb){  u32_t seqno, ackno;  u16_t remote_port, local_port;//  struct ip_addr remote_ip, local_ip;
  u32_t remote_ip, local_ip;  void (* errf)(void *arg, err_t err);  void *errf_arg;    /* Figure out on which TCP PCB list we are, and remove us. If we     are in an active state, call the receive function associated with     the PCB with a NULL argument, and send an RST to the remote end. */  if(pcb->state == TIME_WAIT) {    tcp_pcb_remove(&tcp_tw_pcbs, pcb);    memp_free(MEMP_TCP_PCB, pcb);  } else if(pcb->state == LISTEN) {    tcp_pcb_remove((struct tcp_pcb **)&tcp_listen_pcbs, pcb);    memp_free(MEMP_TCP_PCB_LISTEN, pcb);  } else {    seqno = pcb->snd_nxt;    ackno = pcb->rcv_nxt;    ip_addr_set(&local_ip, &(pcb->local_ip));    ip_addr_set(&remote_ip, &(pcb->remote_ip));    local_port = pcb->local_port;    remote_port = pcb->remote_port;    errf = pcb->errf;    errf_arg = pcb->callback_arg;    tcp_pcb_remove(&tcp_active_pcbs, pcb);    memp_free(MEMP_TCP_PCB, pcb);    if(errf != NULL) {      errf(errf_arg, ERR_ABRT);    }    DEBUGF(TCP_RST_DEBUG, ("tcp_abort: sending RST\n"));    tcp_rst(seqno, ackno, &local_ip, &remote_ip, local_port, remote_port);  }}/*-----------------------------------------------------------------------------------*//* * tcp_bind(): * * Binds the connection to a local portnumber and IP address. If the * IP address is not given (i.e., ipaddr == NULL), the IP address of * the outgoing network interface is used instead. * *//*-----------------------------------------------------------------------------------*/err_t//tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
tcp_bind(struct tcp_pcb *pcb, u32_t *ipaddr, u16_t port){  struct tcp_pcb *cpcb;
  /* Check if the address already is in use. */  for(cpcb = (struct tcp_pcb *)tcp_listen_pcbs;      cpcb != NULL; cpcb = cpcb->next) {    if(cpcb->local_port == port) {      if(ip_addr_isany(&(cpcb->local_ip)) ||	 ip_addr_isany(ipaddr) ||	 ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {	return ERR_USE;      }    }  }  for(cpcb = tcp_active_pcbs;      cpcb != NULL; cpcb = cpcb->next) {    if(cpcb->local_port == port) {      if(ip_addr_isany(&(cpcb->local_ip)) ||	 ip_addr_isany(ipaddr) ||	 ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {	return ERR_USE;      }    }  }  if(!ip_addr_isany(ipaddr)) {
    pcb->local_ip = *ipaddr;  } else {
  }  pcb->local_port = port;  DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %d\n", port));  return ERR_OK;}/*-----------------------------------------------------------------------------------*//* * tcp_listen(): * * Set the state of the connection to be LISTEN, which means that it * is able to accept incoming connections. The protocol control block * is reallocated in order to consume less memory. Setting the * connection to LISTEN is an irreversible process. * *//*-----------------------------------------------------------------------------------*/struct tcp_pcb *tcp_listen(struct tcp_pcb *pcb){  pcb->state = LISTEN;//  pcb = memp_realloc(MEMP_TCP_PCB, MEMP_TCP_PCB_LISTEN, pcb);  if(pcb == NULL) {
//	  printf("error in tcp_listen.\n");    return NULL;  }  TCP_REG((struct tcp_pcb **)&tcp_listen_pcbs, pcb);
//  printf("pcb ip is 0x%x, pcb port is %d.\n", pcb->local_ip, pcb->local_port);
  return pcb;}/*-----------------------------------------------------------------------------------*//* * tcp_recved(): * * This function should be called by the application when it has * processed the data. The purpose is to advertise a larger window * when the data has been processed. * */

⌨️ 快捷键说明

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