?? 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 + -