/*
* Copyright (c) 2003 Electric Application Laboratory of NAN KAI University
* 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.
*
* Author: Li Zhanglin <wzzlin@nankai.edu.cn>
*
*/
#include "..\GloblDef\GloblDef.h"
#include "..\TCPIP\TCPIPmem.h"
#include "..\TCPIP\IP.h"
#include "..\TCPIP\Netif.h"
#include "..\TCPIP\TCP.h"
struct STCB DT_XDATA TCBPool[TCP_CONNECTION_MAX_NUM];
struct STCB DT_XDATA * DT_XDATA TCBFreeList; /* free list */
struct STCB DT_XDATA * DT_XDATA TCBList; /* tcb in use */
struct SPacketQueue DT_XDATA QPool[TCP_QUEUE_MAX_NUM];
struct SPacketQueue DT_XDATA * DT_XDATA QFreeList;
struct STCB DT_XDATA *TCPGetTCB() REENTRANT_SIG
{
struct STCB DT_XDATA * pTCB;
if((pTCB = TCBFreeList) != NULL)
{
TCBFreeList = TCBFreeList->pNext;
}
return pTCB;
}
void TCPInsertTCB(struct STCB DT_XDATA *pTCB) REENTRANT_SIG
{
pTCB->pNext = TCBList;
TCBList = pTCB;
}
struct SPacketQueue DT_XDATA * TCPGetQ() REENTRANT_SIG
{
struct SPacketQueue DT_XDATA * pQ;
if((pQ = QFreeList) != NULL)
{
QFreeList = QFreeList->pNext;
}
return pQ;
}
/* insert to the head of *ppQ. Q is a double link chain */
BOOL TCPInsertQ(struct SPacketQueue DT_XDATA * DT_XDATA * ppQ,struct SMemHead DT_XDATA *MemHead,
DWORD Seq) REENTRANT_SIG
{
struct SPacketQueue DT_XDATA *pNewQ;
struct SPacketQueue DT_XDATA *pQ;
/* allocate a queue to it */
if((pNewQ = TCPGetQ()) == NULL)
return FALSE;
/* write content */
pNewQ->Seq = Seq;
pNewQ->MemHead = MemHead;
/*
* link in the queue
*/
/* if is a empty queue */
if(*ppQ == NULL)
{
*ppQ = pNewQ;
/* pNext pPre point to itself when no others in queue */
pNewQ->pNext = pNewQ;
pNewQ->pPre = pNewQ;
}
else
{
pQ = *ppQ;
/* pNext link */
pNewQ->pNext = pQ->pNext;
pQ->pNext = pNewQ;
/* pPre link */
pNewQ->pNext->pPre = pNewQ;
pNewQ->pPre = pQ;
}
return TRUE;
}
/* move the last unit in queue outof queue,if the queue
is empty return FALSE.actrually last unit is *ppQ */
struct SPacketQueue DT_XDATA * TCPOutQ(struct SPacketQueue DT_XDATA * DT_XDATA * ppQ) REENTRANT_SIG
{
struct SPacketQueue DT_XDATA *pQ;
/* a empty queue? */
if((pQ = *ppQ) == NULL)
return NULL;
/* after remove it, the queue is empty? */
if(pQ->pNext == pQ)
*ppQ = NULL;
else
{
/* relink */
pQ->pPre->pNext = pQ->pNext;
pQ->pNext->pPre = pQ->pPre;
/* and the queue head *ppQ point to pQ->pPre */
*ppQ = pQ->pPre;
}
/* relaim it. link to QFreeList */
pQ->pNext = QFreeList;
QFreeList = pQ;
return pQ;
}
void TCPInit() REENTRANT_MUL
{
WORD i;
/* move TCBPool to TCBFreeList */
for(i = 0, TCBFreeList = NULL; i<TCP_CONNECTION_MAX_NUM; i++)
{
TCBPool[i].pNext = TCBFreeList;
TCBFreeList = &TCBPool[i];
}
/* move QPool to QFreeList */
for(i = 0, QFreeList = NULL; i<TCP_QUEUE_MAX_NUM; i++)
{
QPool[i].pNext = QFreeList;
QFreeList = &QPool[i];
}
TCBList = NULL;
}
/* tcp check sum. return check sum. TCPSize = TCPDataSize + TCPHeadSize*/
WORD TCPCheckSum(struct SIPHead DT_XDATA * pIPHead,WORD TCPSize) REENTRANT_SIG
{
DWORD sum = 0;
WORD DT_XDATA * p;
BYTE i;
/* clac pseudo-header CheckSum. pseudo-header is:
source ip, destination ip, pad 8 bits, protocol, TCP lendth */
sum = 0;
/* source ip and dest ip */
p = (WORD DT_XDATA *)(&(pIPHead->IPScr));
for(i=0; i < sizeof(DWORD)/sizeof(WORD)*2; i++,p++)
sum += *p;
/* pad 8 and protocol */
sum += pIPHead->Protocol;
/* tcp lendth */
sum += TCPSize;
return CheckSum((WORD DT_XDATA *)((BYTE DT_XDATA *)pIPHead + IP_HEAD_MIN_LEN),TCPSize,sum);
}
/* this funtion should be called periodically */
void TCPTimer() REENTRANT_MUL
{
struct STCB DT_XDATA *pTCB;
/* go through all tcbs to see if any time out */
for(pTCB = TCBList; pTCB != NULL; pTCB = pTCB->pNext)
{
/* delayed ack need send now? */
if(pTCB->bNeedAck == TRUE)
{
if(pTCB->DelayAckTimer == 0)
{
/* send a ack. bNeedAck will set FLASE in TCPOutput*/
TCPSendSeg(pTCB,TCPAllocate(0),TCP_ACK);
}
else
pTCB->DelayAckTimer--;
}
/* TCP_STATE_LASTACK TCP_STATE_TIMEWAIT state time out? */
if(pTCB->TCPState == TCP_STATE_LASTACK ||
pTCB->TCPState == TCP_STATE_TIMEWAIT)
{
if(pTCB->LastAckTimer == 0)
{
pTCB->TCPState = TCP_STATE_CLOSED;
/* release buf queue and call user close */
TCPRelease(pTCB);
/* let pTCB->close(); to be call when they send a fin when we at established */
}
else
pTCB->LastAckTimer--;
}
/* if retransmit timer out? */
if(pTCB->QUnacked != NULL)
{
if(pTCB->RetranTimer == 0)
{
/* retransmit,pStart set to tcpdata */
IPOutput(pTCB->QUnacked->MemHead);
/* timer restart and retransmit times inc */
if(pTCB->RetranTimes == TCP_RETRAN_MAX_TIME)
{
pTCB->TCPState = TCP_STATE_CLOSED;
/* closed by countpart shut down */
TCPRelease(pTCB);
}
else
{
pTCB->RetranTimes++;
pTCB->RetranTimer = TCP_RETRAN_TIME_OUT;
}
}
else
pTCB->RetranTimer--;
}
}
}
/* when a TCP close, send too much packet but no replay,
connection fail. TCPIP will call TCPRelease to release packet
and queue, but will not reclaim TCB. in other word user
can use this socket again. */
void TCPRelease(struct STCB DT_XDATA *pTCB) REENTRANT_SIG
{
struct SPacketQueue DT_XDATA *pQ;
/* reclaim Q, and free packet in queue */
while(pQ = TCPOutQ(&(pTCB->QExceedSeq)))
MemFree(pQ->MemHead);
while(pQ = TCPOutQ(&(pTCB->QUnacked)))
MemFree(pQ->MemHead);
while(pQ = TCPOutQ(&(pTCB->QUnSend)))
MemFree(pQ->MemHead);
}
/* fill a segment and send it,NOTE MemHead->pStart point to TCPData */
BOOL TCPSendSeg(struct STCB DT_XDATA *pTCB,struct SMemHead DT_XDATA *MemHead,BYTE TCPFlag) REENTRANT_SIG
{
struct STCPHead DT_XDATA *pTCPHead;
struct SIPHead DT_XDATA *pIPHead;
WORD SeqInc;
/* mem insufficient? */
if(MemHead == NULL)
return FALSE;
/* SeqMine increasment */
if((TCPFlag & TCP_SYN) || (TCPFlag & TCP_FIN))
{
SeqInc = MemHead->pEnd - MemHead->pStart + 1;
}
else
{
SeqInc = MemHead->pEnd - MemHead->pStart;
}
pTCPHead = (struct STCPHead DT_XDATA *)(MemHead->pStart - sizeof(struct STCPHead));
/* fill tcp header */
pTCPHead->PortDest = pTCB->PortDest;
pTCPHead->PortScr = pTCB->PortScr;
pTCPHead->Seq = htonl(pTCB->SeqMine);
pTCPHead->AckSeq = htonl(pTCB->SeqHis);
pTCPHead->TCPH
没有合适的资源?快使用搜索试试~ 我知道了~
单片机以太网上网原代码(keil 工程,用RTS8019)--转载
共58个文件
c:12个
h:11个
obj:10个
4星 · 超过85%的资源 需积分: 31 26 下载量 46 浏览量
2009-04-26
14:37:57
上传
评论 2
收藏 249KB RAR 举报
温馨提示
1)精简TCP/IP 协议栈,以减小代码量。ZLIP 目前没有支持UDP 协议,ICMP 协议 也只支持其中的echo 协议(响应ping 数据包)。lwIP 是一个功能全面的TCP/IP 协 议栈,但是相对51 来说代码量较大。 2) 应用层接口简单,以兼容通用的socket 接口。uIP 有很小的代码量和减小代码量 (选择AVR 为目标器件时,代码为5K 左右)和RAM 使用量(100 字节左右)。uIP 采用了不保存需要应答的数据包的RAM 使用方案,没有和BSD的套接字接口兼容, 应用层接口较复杂。 3) 针对KeilC51 编译器设计。所有的外部变量都使用了xdata 类型,全部指针都为 明确存储类型的指针,需要重入的函数已经声明为reentant,使用KeilC 的小 模式下编译。
资源推荐
资源详情
资源评论
收起资源包目录
zlIP0.2.rar (58个子文件)
zlIP0.2
KeilC
Ex1.plg 243B
Ex1.lnp 187B
Ex1.M51 80KB
NetIf.lst 8KB
TCP.obj 66KB
NetIf.obj 15KB
Ex1.Uv2 2KB
Ex1 208KB
icmp.obj 9KB
Ex1.Opt 3KB
Ex1_Uv2.Bak 2KB
GloblDef.obj 4KB
Ex1.hex 42KB
TCPIPmem.obj 10KB
RTL8019.lst 19KB
ARP.obj 23KB
TCPIPmem.lst 10KB
GloblDef.lst 4KB
main.obj 35KB
Ex1_Opt.Bak 3KB
RTL8019.obj 23KB
main.lst 9KB
etherif.lst 9KB
ARP.lst 13KB
IP.lst 11KB
TCP.lst 50KB
icmp.lst 6KB
etherif.obj 19KB
IP.obj 22KB
App
main.h 2KB
Main.c 5KB
packet_fill.c 3KB
test.c 3KB
Netif
ARP.c 7KB
ARP.h 3KB
etherif.c 5KB
etherif.h 3KB
RTL8019.c 11KB
RTL8019.h 9KB
TCPIP
TCPIPmem.h 2KB
TCPIPmem.c 6KB
icmp.h 2KB
IP.h 3KB
TCP.h 6KB
icmp.c 3KB
NetIf.h 3KB
IP.c 6KB
TCP.c 25KB
NetIf.c 4KB
MSVC6
zlIP.opt 53KB
zlIP.dsw 531B
Debug
zlIP.ncb 249KB
zlIP.positions 135B
zlIP.plg 920B
zlIP.dsp 6KB
GloblDef
GloblDef.h 3KB
GloblDef.c 2KB
MCU
AT89X52.H 6KB
共 58 条
- 1
资源评论
- Yakoo1111112015-04-07临时改了方案,没用上楼主的代码,不过还是十分感谢
- gonglh2012-09-02最近在改DOS下的网络协议,这个资料正好可以用上,谢谢
Sir王
- 粉丝: 2
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 用Rust实现仿nginx,力争实现一个可替代方案,http/https代理, socks5代理, 负载均衡, 反向代理, 静态文件服务器,四层TCP/UDP转发,websocket转发, 内网穿透N
- 计算机二级考试选择题练习模拟题70道及答案.doc
- 企业账户分析情况表_hive_20241118.sql
- 数据中台(大数据平台)数据建模存储标准规范.pdf
- Linux 平台下基于 Rust + GTK 开发的网易云音乐播放器
- 基于Rust语言的新一代组装式应用开发框架,它强调 简单性、可扩展性和生产力
- 数据中台(大数据平台)数据共享标准规范.pdf
- StratoVirt 基于Rust 编程语言 StratoVirt 轻量级、高效且安全 它还具有 Full Sence Support 和 Modules Flexible Splitting 等功能
- 微信小程序开发游戏2048
- 中小学,大学,职业院校专用的免费教务排课管理系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功