📄 myprotocol.h
字号:
// MyProtocol.h : header file
//
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef BPMAXB
#define BPMAXB 2048 /* Maximum buffer length */
#endif
/* Ethernet definitions */
#define EP_MINLEN 60 /* minimum packet length */
#define EP_DLEN 1500 /* length of data field ep */
#define EP_HLEN 24 /* size of (extended) Ether header */
#define EP_CRC 4 /* ether CRC (trailer) */
#define EP_MAXLEN EP_HLEN+EP_DLEN
#define EP_ALEN 6 /* number of octets in physical address */
/* a physical Ethernet address */
typedef unsigned char Eaddr[EP_ALEN];
/* macro to compute a datagram's header length (in bytes) */
#define IP_MINHLEN 5 /* minimum IP header length (in longs) */
#define IP_HLEN(pip) ((pip->ip_verlen & 0xf)<<2)
#define IPMHLEN 20 /* minimum IP header length (in bytes) */
#define IP_MAXLEN BPMAXB-EP_HLEN /* Maximum IP datagram length */
/* TCP Control Bits */
#define TCPF_URG 0x20 /* urgent pointer is valid */
#define TCPF_ACK 0x10 /* acknowledgement field is valid */
#define TCPF_PSH 0x08 /* this segment requests a push */
#define TCPF_RST 0x04 /* reset the connection */
#define TCPF_SYN 0x02 /* synchronize sequence numbers */
#define TCPF_FIN 0x01 /* sender has reached end of its stream */
#define TCPMHLEN 20 /* minimum TCP header length */
#define TCPHOFFSET 0x50 /* tcp_offset value for TCPMHLEN */
#define TCP_HLEN(ptcp) (((ptcp)->tcp_offset & 0xf0)>>2)
#define TCP_IF_URG(ptcp) ((ptcp)->tcp_flags & TCPF_URG)
#define TCP_IF_ACK(ptcp) ((ptcp)->tcp_flags & TCPF_ACK)
#define TCP_IF_PSH(ptcp) ((ptcp)->tcp_flags & TCPF_PSH)
#define TCP_IF_RST(ptcp) ((ptcp)->tcp_flags & TCPF_RST)
#define TCP_IF_SYN(ptcp) ((ptcp)->tcp_flags & TCPF_SYN)
#define TCP_IF_FIN(ptcp) ((ptcp)->tcp_flags & TCPF_FIN)
/* TCP Options */
#define TPO_EOOL 0 /* end Of Option List */
#define TPO_NOOP 1 /* no Operation */
#define TPO_MSS 2 /* maximum Segment Size */
/* UDP header length in bytes */
#define U_HLEN 8
/* maximum data in UDP packet */
#define U_MAXLEN (IP_MAXLEN-(IP_MINHLEN<<2)-U_HLEN)
/************************************************/
/* PROTOCOL DATA STRUCTURE */
/************************************************/
/* IP头格式
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
]Version] IHL ]Type of Service] Total Length ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Identification ]Flags] Fragment Offset ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Time to Live ] Protocol ] Header Checksum ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Source Address ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Destination Address ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] TCP header, then your data ...... ]
] ]
*/
// IP头
typedef struct _IP_HEADER
{
unsigned char ip_verlen; // 4位IP版本号+4位首部长度
unsigned char ip_tos; // 服务类型 : 8
unsigned short ip_len; // 总长 : 16
unsigned short ip_id; // 标识 : 16
unsigned short ip_frags; // 标志 include "Flags/Fragment Offset" : 16 (3+13)
unsigned char ip_ttl; // 生命期 Time to Live : 8
unsigned char ip_proto; // 协议 ( IP , TCP, UDP etc) : 8
unsigned short ip_chksum; // IP头校验和 : 16
unsigned int ip_srcIP; // 源地址 : 32 // unsigned long
unsigned int ip_dstIP; // 目的地址 : 32 // unsigned long
unsigned char ip_data[1]; // variable length data
//BYTE options; // 选项
}IP_HEADER;
/*
typedef struct _iphdr
{
unsigned char h_lenver; // 4位IP版本号+4位首部长度
unsigned char tos; // 8位服务类型TOS
unsigned short total_len; // 16位总长度(字节)
unsigned short ident; // 16位标识
unsigned short frags; // 3位标志位 include "Flags/Fragment Offset"
unsigned char ttl; // 8位生存时间 TTL
unsigned char proto; // 8位协议 (TCP, UDP 或其他)
unsigned short chksum; // 16位IP首部校验和
unsigned int srcIP; // 32位源IP地址
unsigned int dstIP; // 32位目的IP地址
}IP_HEADER;
*/
/* TCP头格式
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Source Port ] Destination Port ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Sequence Number ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Acknowledgment Number ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Data ] ]U]A]P]R]S]F] ]
] Offset] Reserved ]R]C]S]S]Y]I] Window ]
] ] ]G]K]H]T]N]N] ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Checksum ] Urgent Pointer ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] your data ... next 500 octets ]
] ...... ]
*/
// TCP头
typedef struct _TCP_HEADER
{
unsigned short tcp_SrcPort; // 源端口 : 16
unsigned short tcp_DstPort; // 目的端口 : 16
unsigned int tcp_SeqNo; // 序列号 : 32 // 或用long
unsigned int tcp_AckNo; // 确认号 : 32 // 或用long
unsigned char tcp_lenres; // 4位首部长度+ 6位保留字中的前4位 // tcp_offset
unsigned char tcp_flags; // 余下的2位保留字+6位标志(URG,ACK,PSH,RST,SYN,FIN)
unsigned short tcp_WndSize; // 滑动窗口大小 : 16
unsigned short tcp_ChkSum; // 校验和 : 16
unsigned short tcp_UrgPtr; // 紧急指针 : 16
unsigned char tcp_data[1];
// 可选项(0或更多的32位字)
}TCP_HEADER;
// 其后是数据(可选项)
/*
typedef struct _tcphdr //定义TCP首部
{
USHORT th_sport; //16位源端口
USHORT th_dport; //16位目的端口
unsigned int th_seq; //32位序列号
unsigned int th_ack; //32位确认号
unsigned char th_lenres; //4位首部长度/6位保留字
unsigned char th_flag; //6位标志位
USHORT th_win; //16位窗口大小
USHORT th_sum; //16位校验和
USHORT th_urp; //16位紧急数据偏移量
}TCP_HEADER;
struct TCPPacketHead {
WORD SourPort; // 源端口
WORD DestPort; // 目的端口
DWORD SeqNo; // 顺序号
DWORD AckNo; // 确认号
BYTE HLen; // TCP头长
BYTE Flag; // 标志(...)
WORD WndSize; // 滑动窗口大小
WORD ChkSum; // 校验和
WORD UrgPtr; // 紧急指针
}TCP_HEADER;
*/
// UDP头
typedef struct _UDP_HEADER
{
unsigned short udp_Srcport; //16位源端口
unsigned short udp_Dstport; //16位目的端口
unsigned short udp_Len; //16位UDP长度(包括UDP头+UDP数据)
unsigned short udp_ChkSum; //16位UDP校验和
char udp_data[U_MAXLEN]; //data in UDP message
}UDP_HEADER;
/*
typedef struct _UDP_HEADER
{
WORD SrcPort; // 源端口
WORD DstPort; // 目的端口
WORD Len; // UDP头长度
WORD ChkSum; // UDP校验和
}UDP_HEADER;
*/
// ICMP头
typedef struct _ICMP_HEADER
{
unsigned char ic_type; //8位类型
unsigned char ic_code; //8位代码
unsigned short ic_cksum; //16位校验和
unsigned short ic_id; //识别号(一般用进程号作识别号)
unsigned short ic_seq; //报文序列号
unsigned long timestamp; //时间戳
}ICMP_HEADER;
/*
struct icmp
{
char ic_type; // type of message (ICT_* above)
char ic_code; // code (ICC_* above)
short ic_cksum; // checksum of ICMP header+data
union {
struct {
int ic1_id:16; // echo type, a message id
int ic1_seq:16; // echo type, a seq. number
} ic1;
unsigned long ic2_gw; // for redirect, gateway //IPaddr ic2_gw;
struct {
char ic3_ptr; // pointer, for ICT_PARAMP
char ic3_pad[IC_PADLEN];
} ic3;
int ic4_mbz; // must be zero
} icu;
char ic_data[1]; // data area of ICMP message
};
// format 1
#define ic_id icu.ic1.ic1_id
#define ic_seq icu.ic1.ic1_seq
// format 2
#define ic_gw icu.ic2_gw
// format 3
#define ic_ptr icu.ic3.ic3_ptr
#define ic_pad icu.ic3.ic3_pad
// format 4
#define ic_mbz icu.ic4_mbz
typedef struct _ICMP_HEADER
{
BYTE Type;
BYTE Code;
WORD ChkSum;
}ICMP_HEADER;
*/
//以太网头
/* Frame type field must be "8"
| 6 Bytes | 6 Bytes | 2 Bytes |
---------------------------------------------------------------
|Destination address| Source address |Frame type | Frame data
---------------------------------------------------------------
*/ /*
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Ethernet destination address (first 32 bits) ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Ethernet dest (last 16 bits) ]Ethernet source (first 16 bits)]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Ethernet source address (last 32 bits) ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Type code ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] IP header, then TCP header, then your data ]
...
] end of your data ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
] Ethernet Checksum ]
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/*// 以太网头
typedef struct _ETH_HEADER
{
unsigned char eh_dst[6]; // destination ethernet addrress
unsigned char eh_src[6]; // source ethernet addresss
unsigned short eh_type; // ethernet pachet type
}ETH_HEADER; */
/*
struct eh
{
Eaddr eh_dst;
Eaddr eh_src;
unsigned short eh_type;
}; */
/*// 完整的以太网数据包
typedef struct _ETHER_PACKET
{
struct ETH_HEADER ETH;
struct IP_HEADER IP;
struct TCP_HEADER TCP;
char buff[8192];
}ETHER_PACKET; */
/* complete structure of Ethernet packet*/ /*
struct ep
{
u_long ep_nexthop; // niput() uses this
short ep_ifn; // originating interface number
short ep_len; // length of the packet
short ep_order; // byte order mask (for debugging)
struct eh ep_eh; // the ethernet header
char ep_data[EP_DLEN]; // data in the packet
};
*/
#define IG_HLEN 8 // IGMP header length
#define IG_VER(pig) (((pig)->ig_vertyp>>4) & 0xf)
#define IG_TYP(pig) ((pig)->ig_vertyp & 0xf)
typedef struct _IGMP_HEADER
{
unsigned char ig_vertyp; // version and type field
char ig_unused; // not used by IGMP
unsigned short ig_cksum; // compl. of 1's compl. sum
unsigned long ig_gaddr; // host group IP address //IPaddr ig_gaddr;
}IGMP_HEADER;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -