📄 tcpip.h
字号:
///
// Copyright (c) 2003, Wolver Wang, MinShan Inc. R&D Center
// wolver@minshan-inc.com
// 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. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// This product includes software developed by Wolver Wang.
// 4. 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.
///
///
// Copyright (c) 2003, Wolver Wang, MinShan Inc. R&D Center
// wolver@minshan-inc.com
// 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. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// This product includes software developed by Wolver Wang.
// 4. 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.
///
///
// 版权 2003, 王卫无,四川绵阳岷山集团有限公司--研究开发中心
// wolver@minshan-inc.com
// 保留一切权利
//
// 如果符合以下条件,则无论是以原代码或非原代码代码形式,且不论是否修改,
// 再分发和使用本软件都是被允许的。
// 1. 原代码的再分发必须保留上述的版权声明、本条件说明和以下免责声明。
// 2. 非原代码形式的再分发,必须在证明文件和(或)其它一同提供的材料中重新
// 作上述的版权声明、本条件说明和以下免责声明。
// 3. 一切提及本软件和使用的广告材料必须显示以下致谢:
// 本产品包含王卫无(四川绵阳岷山集团有限公司--研究开发中心)开发的软件。
// 4. 如果没有预先得到特定的书面许可,不能用作者的名字来宣传推广基于本软件得到
// 的产品。
//
// 免责声明:
// 本软件是由某某作者提供,如果出现以下情况,作者都不承担任何责任。
// 1. 因作者的说明以及任何明确的或暗示的保证(包括但不限于表达某种商业性和适合某一
// 特定目的暗示性保证)而产生的损失。
// 2. 无论在何种情况下,对使用本软件造成的任何直接的、间接的、偶然的、特定的、可预
// 见性的和连带产生的损失(包括但不限于获取产品和服务、作用丧失、数据遗失、利益损
// 失或商业干预),无论这些损失是怎样造成的,并且是以何种方式阐释责任。
// 3. 任何因使用本软件而相关的合同、严格赔偿责任和侵权行为(包含:疏忽或其它)的损失,
// 甚至即便是可能的此类已经明示或暗示的损失。
///
// 所有这些定义适用于vIP4版本的TCP/IP协议
#ifndef __TCPIP_H__
#define __TCPIP_H__
#include "system.h"
#include "drivers.h"
// 定义前缀 -----------------------------------------------------------------------------------
// c <-> Constant
// g <-> Global
// uw <-> Unsigned Word
// p <-> Point
// st <-> Struct
// pt <-> Point of Struct
// 定义TCP/IP模式 ------------------------------------------------------------------------------
#define TCP_DHCP 0 // 没有DHCP协议
// 系统工作于服务模式。定义为:0
// 允许TCP/IP主动激活(客户模式)。定义为:1
// 服务模式和客户模式同时存在。定义为:1
#define TCP_ACTIVE_OPEN 1
// 预置本机IP地址。例如: IP = 192.168.0.30 ------------------------------------------------------
#if TCP_DHCP == 0
#define cIpAddr1 192 // Byte Length
#define cIpAddr2 168 // Byte Length
#define cIpAddr3 0 // Byte Length
#define cIpAddr4 60 // Byte Length
extern const UINT16 guwIpAddr[2];
#else
extern UINT16 guwIpAddr[2];
#endif
// 预置本机IP的掩码。例如: NETMASK = 255.255.255.0 -----------------------------------------------
#if TCP_DHCP == 0
#define cNetMask1 255 // Byte Length
#define cNetMask2 255 // Byte Length
#define cNetMask3 255 // Byte Length
#define cNetMask4 0 // Byte Length
extern const UINT16 guwNetMask[2];
#else
extern UINT16 guwNetMask[2];
#endif
// 预置本机的网关。例如: GateWay = 192.168.0.1 ---------------------------------------------------
#if TCP_DHCP == 0
#define cDR_IpAddr1 192 // Byte Length
#define cDR_IpAddr2 168 // Byte Length
#define cDR_IpAddr3 0 // Byte Length
#define cDR_IpAddr4 1 // Byte Length
extern const UINT16 guwDR_IpAddr[2];
#else
extern UINT16 guwDR_IpAddr[2];
#endif
// 预置本机的以太物理地址。例如: MAC = 52H-54H-ABH-1D-A4-F9H ------------------------------------
#define cEthAddr1 0x52 // Byte Length
#define cEthAddr2 0x54 // Byte Length
#define cEthAddr3 0xab // Byte Length
#define cEthAddr4 0x1d // Byte Length
#define cEthAddr5 0xa4 // Byte Length
#define cEthAddr6 0xfa // Byte Length
extern const UINT16 guwEthAddr[3];
//---------------------------------------------------------------------------------
// 定义TCP应用数据长度最大缓冲区,单位:字(Word)
#define cTcpDataSize (1460/2)
//---------------------------------------------------------------------------------
// 定义TCP/IP协议的相关结构长度,单位:字(Word),参考结构定义
#define cEthHdrLen 7 // 以太头结构长度
#define cArpHdrLen 14 // ARP头结构长度
#define cIpHdrLen 10 // IP头结构长度(不含扩展选项)
#define cIcmpHdrLen 4 // ICMP中Ping协议头结构长度
#define cTcpHdrLen 10 // TCP头结构长度(不含扩展选项)
//---------------------------------------------------------------------------------
// 以太头结构
typedef struct{
UINT16 DestEthAddr[3];
UINT16 SrcEthAddr[3];
UINT16 EthType;
// 以太头的包类型。EthHdr_Stru.EthType
#define cEthType_Arp 0x0806 // ARP Packet
#define cEthType_Ip 0x0800 // IP Packet
}EthHdr_Stru;
// 最大允许的以太包长度,单位:字(Word)
#define cEthBufSize (cEthHdrLen + cIpHdrLen + cTcpHdrLen + cTcpDataSize)
// 分配允许的以太包处理缓冲区,单位:字(Word)
extern UINT16 guwEthBuf[cEthBufSize];
// 以太头的常数指针。
#define cpEthHdrBuf ((UINT16 *)&guwEthBuf[0]) // 指向Word的buffer
#define cptEthHdrBuf ((EthHdr_Stru *)&guwEthBuf[0]) // 指向结构
//---------------------------------------------------------------------------------
// ARP头结构
typedef struct{
UINT16 HwType;
// ARP包硬件类型。 ArpHdr_Stru.HwType
#define cHwType_Eth 0x01 // 以太模式
UINT16 ProtoType;
// #define cEthType_Ip 0x0800 // IP Packet
UINT16 Hw8Proto8;
// ARP协议类型 ArpHdr_Stru.Hw8Proto8
#define c_vIP4 0x0604 // vIP4
UINT16 OpCode;
// ARP操作类型 ArpHdr_Stru.OpCode
#define cArpRequest 0x01 // ARP请求
#define cArpReply 0x02 // ARP回应
UINT16 SndEthAddr[3];
UINT16 SndIpAddr[2];
UINT16 RcvEthAddr[3];
UINT16 RcvIpAddr[2];
}ArpHdr_Stru;
// ARP头的常数指针。
#define cptArpHdrBuf ((ArpHdr_Stru *)&guwEthBuf[cEthHdrLen]) // 指向结构
//---------------------------------------------------------------------------------
// IP头结构,不含扩展选项
typedef struct{
UINT16 Tos:8, Vhl:8;
// IpHdr_Stru.Tos = 0x00 // 服务类型0x00允许:TCP和ICMP查询、ICMP差错、BOOTP
// IP头格式 IpHdr_Stru.Vhl
#define cIP_VER_HLEN 0x45 // vIP4
UINT16 Len;
UINT16 IpId;
UINT16 Offset:13, Flags:3;
// IpHdr_Stru.offset = 0
// IpHdr_Stru.Flags 分片帧
#define cIP_DF 0x2 // 不分片帧(Do not Fragment)
#define cIP_MF 0x1 // 非最后帧,分片帧(More Fragment)
UINT16 Proto:8, Ttl:8;
// IP头的存活时间与协议类型 IpHdr_Stru.Proto
#define cIP_PROTO_ICMP 0x01 // IP属于ICMP类
#define cIP_PROTO_TCP 0x06 // IP属于TCP类
// IP头的存活时间与协议类型 IpHdr_Stru.Ttl
#define cIP_TTL 0xff // 存活时间
UINT16 IpChkSum;
UINT16 SrcIpAddr[2];
UINT16 DestIpAddr[2];
}IpHdr_Stru;
// IP头的常数指针。
#define cpIpHdrBuf ((UINT16 *)&guwEthBuf[cEthHdrLen]) // 指向Word的buffer
#define cptIpHdrBuf ((IpHdr_Stru *)&guwEthBuf[cEthHdrLen]) // 指向结构
// 记录本地IP序号
extern volatile UINT16 guwIpId;
//---------------------------------------------------------------------------------
// ICMP头结构,不含扩展选项
typedef struct{
UINT16 Code:8, Type:8;
// IcmpHdr_Stru.Code = 0x00 only for PING relpy!
// ICMP操作类型 IcmpHdr_Stru.Type
#define cICMP_ECHO_REPLY 0x00 // ICMP回应
#define cICMP_ECHO 0x08 // ICMP呼叫
UINT16 ChkSum;
UINT16 Id;
UINT16 SeqNum;
}IcmpHdr_Stru;
// ICMP头的常数指针。
#define cpIcmpHdrBuf ((UINT16 *)&guwEthBuf[cEthHdrLen + cIpHdrLen]) // 指向Word的buffer
#define cptIcmpHdrBuf ((IcmpHdr_Stru *)&guwEthBuf[cEthHdrLen + cIpHdrLen]) // 指向结构
//---------------------------------------------------------------------------------
// TCP头结构,不含扩展选项
typedef struct{
UINT16 SrcPort; // 本地端口
UINT16 DestPort; // 远端端口
UINT16 SeqNum[2]; // 本地32位序号
UINT16 AckNum[2]; // 远端期待的32位序号
UINT16 Flags:6, Reserve:6, HdrLen:4;
// TCP保留 TcpHdr_Stru.Reserve = 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -