//================================================
//文件名:MNetWorkPKT.h
//常见的网络数据包的数据结构
//================================================
#ifndef _NETWORK_PACKET_H
#define _NETWORD_PACKET_H
#pragma once
//地址定义
//================================================
//IP地址定义
typedef struct _ip_addr
{
unsigned char ucByte1;
unsigned char ucByte2;
unsigned char ucByte3;
unsigned char ucByte4;
}ip_addr,IP_ADDR,* PIP_ADDR;
//MAC地址定义
typedef struct _mac_addr
{
unsigned char ucByte1;
unsigned char ucByte2;
unsigned char ucByte3;
unsigned char ucByte4;
unsigned char ucByte5;
unsigned char ucByte6;
}mac_addr,MAC_ADDR,* PMAC_ADDR;
//================================================
//数据格式定义
//================================================
//IP分组的首部
typedef struct _ip_header
{
unsigned char ucVerHeadLen; //版本4位 + 首部长度4位
unsigned char ucTos; //区分服务
unsigned short usTlen; //总长度
unsigned short usId; //惟一标识
unsigned short usFlagOffset; //标志与偏移量
unsigned char ucTTL; //TTL
unsigned char ucProtocol; //协议
unsigned short usCheckSum; //校验和
IP_ADDR ipaSaddr; //源地址
IP_ADDR ipaDaddr; //目的地址
}ip_header,IP_HEADER,* PIP_HEADER;
//MAC帧首部格式
typedef struct _eth_header
{
MAC_ADDR maDes; //目地MAC地址
MAC_ADDR maSrc; //源MAC地址
unsigned short usEthType; //数据部分类型协议:ARP、IP、RARP
}eth_header,ETH_HEADER,* PETH_HEADER;
//ARP数据包格式
typedef struct _arp_packet
{
unsigned short usHardWareAddrType; //硬件地址类型
unsigned short usProtocolAddrType; //协议地址类型
unsigned char ucHardWareAddrLength; //硬件地址长度
unsigned char ucProtocolAddrLength; //协议地址长度
unsigned short usOptionCode; //选项码(请求、应答)
MAC_ADDR maSenderHDAddr; //发送者的硬件地址
IP_ADDR ipSenderIPAddr; //发送者的IP地址
MAC_ADDR maTargetHDAddr; //目标的硬件地址
IP_ADDR ipTargetIPAddr; //目标的协议地址
unsigned char ucPaddingCode[18]; //填充码(大于18个字节)
}arp_packet,ARP_PACKET,* PARP_PACKET;
//ARP数据帧格式
typedef struct _arp_frame
{
ETH_HEADER ethHeader; //以太网帧头
ARP_PACKET arpPacket; //ARP数据包
}arp_frame,ARP_FRAME,*PARP_FRAME;
//ICMP数据报的首部
typedef struct _icmp_header
{
unsigned char ucType; //类型
unsigned char ucCode; //代码
unsigned short usChkSum; //检验和
}icmp_header,ICMP_HEADER,* PICMP_HEADER;
//ICMP数据报的三种常见格式
//ICMP报文一:请求与回复报文
typedef struct _icmp_echo_message
{
ICMP_HEADER icmpHeader; //ICMP报头
unsigned short usIdentifier; //ICMP标识符
unsigned short usSeqNumber; //ICMP序号
//TimeSend并不RFC文档的标准,在标准中只有data域
unsigned long ulTimeSend; //发送请求时间,用于计算往返时间
}icmp_echo_message,ICMP_ECHO_MESSAGE,*PICMP_ECHO_MESSAGE;
//ICMP报文二:超时差错报文
typedef struct _icmp_exceeded_message
{
ICMP_HEADER icmpHeader; //ICMP报头
unsigned char ucUnused[4]; //不使用,但必须清零
IP_HEADER iphReIPHeader; //产生差错的IP首部
unsigned char strOriData[8]; //原始的8字节的数据
}icmp_exceeded_message,ICMP_EXCEEDED_MESSAGE,*PICMP_EXCEEDED_MESSAGE;
//ICMP报文三:时间戳与时间戳回复报文
//时间戳:距离某一时刻逝去的时间间隔
typedef struct _icmp_timestamp_message
{
ICMP_HEADER icmpHeader; //ICMP报头
unsigned short usIdentifier; //ICMP标识符
unsigned short usSeqNumber; //ICMP序号
unsigned long ulOriTimeStamp; //原始时间戳
unsigned long ulRecTimeStamp; //接收者时间戳
unsigned long ulTransmitTimeStamp;//传送时间戳
}icmp_timestamp_message,ICMP_TIMESTAMP_MESSAGE,*PICMP_TIMESTAMP_MESSAGE;
//UDP数据包头的格式
typedef struct _udp_header
{
unsigned short usSrcPort; //源端口
unsigned short usDesPort; //目的端口
unsigned short usDGramLength; //数据报长度
unsigned short usChkSum; //检验和
}udp_header,UDP_HEADER,*PUDP_HEADER;
//伪首部(UDP / TCP都适用)
typedef struct _pseudo_header
{
IP_ADDR ipaSrcAddr; //源地址
IP_ADDR ipaDesAddr; //目的地址
unsigned char ucZero; //0域
unsigned char ucProtocol; //协议
unsigned short usPktLength; //数据报长度
}pseudo_header,PSEUDE_HEADER,*PPSEUDE_HEADER;
//TCP数据基本报头格式
typedef struct _tcp_header
{
unsigned short usSrcPort; //源端口
unsigned short usDesPort; //目的端口
unsigned long ulOrder; //序号
unsigned long ulAckNext; //确认号
unsigned short usTCPOpt; //数据偏移(4 bit)、保留(6 bit)、URG、ACK、PSH、RST、SYN、FIN
unsigned short usRWnd; //窗口值
unsigned short usChkSum; //检验和
unsigned short usUrgent; //紧急指针(当URG = 1时才有作用)
}tcp_header,TCP_HEADER,* PTCP_HEADER;
#endif
//================================================
//文件名:LocalInfo.h
//获取本地适配器的一些信息
//1.获取本地IP、MAC地址
//2.格式化地打印IP、MAC地址
//3.IP串转化,如“192.168.1.1”这个串与实际表示的4字节IP相互转化
//4.比较两个IP地址是否相同
//================================================
#include "MNetWorkPKT.h"
int GetLocalIPAddr(PIP_ADDR );
int GetMACAddr(PMAC_ADDR);
int PrintIPAddr(PIP_ADDR);
int PrintMACAddr(PMAC_ADDR);
int CharTOIP(const char * szIP,PIP_ADDR pIPAddr);
int IPTOChar(char * szIP,const PIP_ADDR pIPAddr);
int IsIPSame(PIP_ADDR,PIP_ADDR);
//================================================
//================================================
//文件名:LocalInfo.cpp
//简介:对LocalInfo.h的函数的实现
//================================================
#include "LocalInfo.h"
#include
#include
#include
//================================================
int GetLocalIPAddr(PIP_ADDR pIPAddr)
{
PIP_ADAPTER_INFO pAdapter = 0;
unsigned long ulBuf = 0;
DWORD dwRet;
dwRet = GetAdaptersInfo(pAdapter,&ulBuf);
if(dwRet == ERROR_BUFFER_OVERFLOW)
{
pAdapter = (PIP_ADAPTER_INFO)malloc(ulBuf);
dwRet = GetAdaptersInfo(pAdapter,&ulBuf);
if (dwRet == ERROR_SUCCESS)
{
CharTOIP(pAdapter->IpAddressList.IpAddress.String,pIPAddr);
}
}
free(pAdapter);
return dwRet;
}
//================================================
int GetMACAddr(PMAC_ADDR pMACAddr)
{
PIP_ADAPTER_INFO pAdapter = 0;
unsigned long ulBuf = 0;
DWORD dwRet;
dwRet = GetAdaptersInfo(pAdapter,&ulBuf);
if(dwRet == ERROR_BUFFER_OVERFLOW)
{
pAdapter = (PIP_ADAPTER_INFO)malloc(ulBuf);
dwRet = GetAdaptersInfo(pAdapter,&ulBuf);
if (dwRet == ERROR_SUCCESS)
{
memcpy(pMACAddr,pAdapter->Address,6);
}
}
free(pAdapter);
return dwRet;
}
//================================================
int PrintIPAddr(PIP_ADDR p)
{//格式输出IP地址:如,192.168.1.101
if (p!= 0)
{
printf("%u.%u.%u.%u", p->ucByte1,
p->ucByte2,
p->ucByte3,
p->ucByte4);
return 1;
}
return 0;
}
//================================================
int PrintMACAddr(PMAC_ADDR p)
{//16进制格式输出MAC地址:如,00-0C-29-D2-6B-A5
if (p != 0)
{
printf("%02X-%02X-%02X-%02X-%02X-%02X",
p->ucByte1,
p->ucByte2,
p->ucByte3,
p->ucByte4,
p->ucByte5,
p->ucByte6);
return 1;
}
return 0;
}
//================================================
int CharTOIP(const char * szIP,PIP_ADDR pIPAddr)
{
if (pIPAddr == 0) return 0;
unsigned char nByte[4];
char * p = (char *)szIP;
char Buf[4] = {0};
for (int i = 0;i < 4;i++)
{
memset(Buf,0,sizeof(Buf));
int j = 0;
while(*p != '.' && *p)
{
Buf[j++] = *p++;
}
if (*p == '.') p++;
nByte[i] = atoi(Buf);
}
memcpy(pIPAddr,nByte,sizeof(IP_ADDR));
return 1;
}
int IPTOChar(char * szIP,const PIP_ADDR pIPAddr)
{
if (szIP == 0) return 0;
sprintf(szIP,"%d.%d.%d.%d", pIPAddr->ucByte1,
pIPAddr->ucByte2,
pIPAddr->ucByte3,
pIPAddr->ucByte4);
return 1;
}
int IsIPSame(PIP_ADDR p1,PIP_ADDR p2)
{
return memcmp(p1,p2,sizeof(IP_ADDR)) == 0;
}
//================================================
共享一个网络数据格式的头文件(更新)
2009-09-09 10:27
//================================================
//文件名:MNetWorkPKT.h
//常见的网络数据包的数据结构
//================================================
//前言说明
//================================================
//1个字节 = 8 位
//unsigned char -- 1个字节
//unsigned short -- 2个字节
//unsigned long -- 4个字节
//
//专业术语:报、消息、包、帧
//
//data :原始的用户数据
//segment:报文分段(包含控制信息),位于传输层的数据组
//packet:分组或者包,由应用层传来的数据,将其分组成包
//frame :帧,负责将包(分组)装成数据链路层数据格式
//bit :位流
//================================================
#ifndef _NETWORK_PACKET_H
#define _NETWORD_PACKET_H
#pragma once
//地址定义
//------------------------------------------------
//IP地址定义
typedef struct _ip_addr
{
unsigned char ucByte1;
unsigned char ucByte2;
unsigned char ucByte3;
unsigned char ucByte4;
}ip_addr,IP_ADDR,* PIP_ADDR;
//MAC地址定义
typedef struct _mac_addr
{
unsigned char ucByte1;
unsigned char ucByte2;
unsigned char ucByte3;
unsigned char ucByte4;
unsigned char ucByte5;
unsigned char ucByte6;
}mac_addr,MAC_ADDR,* PMAC_ADDR;
//------------------------------------------------
//数据格式定义
//------------------------------------------------
//IP分组的首部
typedef struct _ip_header
{
unsigned char ucVerHeadLen; //版本4位 + 首部长度4位
unsigned char ucTos; //区分服务
unsigned short usTlen; //总长度
unsigned short usId; //惟一标识
unsigned short usFlagOffset; //标志与偏移量
unsigned char ucTTL; //TTL
unsigned char ucProtocol; //协议
unsigned short usCheckSum; //校验和
IP_ADDR ipaSaddr; //源地址
IP_ADDR ipaDaddr; //目的地址
}ip_header,IP_HEADER,* PIP_HEADER;
//MAC帧首部格式
typedef struct _eth_header
{
MAC_ADDR maDes; //目地MAC地址
MAC_ADDR maSrc; //源MAC地址
unsigned short usEthType; //数据部分类型协议:ARP、IP、RARP
}eth_header,ETH_HEADER,* PETH_HEADER;
//ARP数据包格式
typedef struct _arp_packet
{
unsigned short usHardWareAddrType; //硬件地址类型
unsigned short usProtocolAddrType; //协议地址类型
unsigned char ucHardWareAddrLength; //硬件地址长度
unsigned char ucProtocolAddrLength; //协议地址长度
unsigned short usOptionCode; //选项码(请求、应答)
MAC_ADDR maSenderHDAddr; //发送者的硬件地址
IP_ADDR maSenderIPAddr; //发送者的IP地址
MAC_ADDR maTargetHDAddr; //目标的硬件地址
IP_ADDR maTargetIPAddr; //目标的协议地址
unsigned char ucPaddingCode[18]; //填充码(大于18个字节)
}arp_packet,ARP_PACKET,* PARP_PACKET;
//ARP数据帧格式
typedef struct _arp_frame
{
ETH_HEADER ethHeader; //以太网帧头
ARP_PACKET arpPacket; //ARP数据包
}arp_frame,ARP_FRAME,*PARP_FRAME;
//ICMP数据报的首部
typedef struct _icmp_header
{
unsigned char ucType; //类型
unsigned char ucCode; //代码
unsigned short usChkSum; //检验和
}icmp_header,ICMP_HEADER,* PICMP_HEADER;
//ICMP数据报的三种常见格式
//ICMP报文一:请求与回复报文
typedef struct _icmp_echo_message
{
ICMP_HEADER icmpHeader; //ICMP报头
unsigned short usIdentifier; //ICMP标识符
unsigned short usSeqNumber; //ICMP序号
//TimeSend并不RFC文档的标准,在标准中只有data域
unsigned long ulTimeSend; //发送请求时间,用于计算往返时间
}icmp_echo_message,ICMP_ECHO_MESSAGE,*PICMP_ECHO_MESSAGE;
//ICMP报文二:超时差错报文
typedef struct _icmp_exceeded_message
{
ICMP_HEADER icmpHeader; //ICMP报头
unsigned char ucUnused[4]; //不使用,但必须清零
IP_HEADER iphReIPHeader; //产生差错的IP首部
unsigned char strOriData[8]; //原始的8字节的数据
}icmp_exceeded_message,ICMP_EXCEEDED_MESSAGE,*PICMP_EXCEEDED_MESSAGE;
//ICMP报文三:时间戳与时间戳回复报文
//时间戳:距离某一时刻逝去的时间间隔
typedef struct _icmp_timestamp_message
{
ICMP_HEADER icmpHeader; //ICMP报头
unsigned short usIdentifier; //ICMP标识符
unsigned short usSeqNumber; //ICMP序号
unsigned long ulOriTimeStamp; //原始时间戳
unsigned long ulRecTimeStamp; //接收者时间戳
unsigned long ulTransmitTimeStamp;//传送时间戳
}icmp_timestamp_message,ICMP_TIMESTAMP_MESSAGE,*PICMP_TIMESTAMP_MESSAGE;
//UDP数据包头的格式
typedef struct _upd_header
{
unsigned short usSrcPort; //源端口
unsigned short usDesPort; //目的端口
unsigned short usDGramLength; //数据报长度
unsigned short usChkSum; //检验和
}upd_header,UPD_HEADER,*PUDP_HEADER;
//伪首部(UDP / TCP都适用)
typedef struct _pseudo_header
{
IP_ADDR ipaSrcAddr; //源地址
IP_ADDR ipaDesAddr; //目的地址
unsigned char ucZero; //0域
unsigned char ucProtocol; //协议
unsigned short usPktLength; //数据报长度
}pseudo_header,PSEUDE_HEADER,*PPSEUDE_HEADER;
//------------------------------------------------
//简单的输出格式(控制台)
#ifdef _CONSOLE
#include
//------------------------------------------------
void PrintIPAddr(const IP_ADDR * p)
{//格式输出IP地址:如,192.168.1.101
if (p!= 0)
{
printf("%u.%u.%u.%u", p->ucByte1,
p->ucByte2,
p->ucByte3,
p->ucByte4);
}
}
//------------------------------------------------
void PrintMACAddr(const MAC_ADDR * p)
{//16进制格式输出MAC地址:如,00-0C-29-D2-6B-A5
if (p != 0)
{
printf("%02X-%02X-%02X-%02X-%02X-%02X",
p->ucByte1,
p->ucByte2,
p->ucByte3,
p->ucByte4,
p->ucByte5,
p->ucByte6);
}
}
//------------------------------------------------
#endif
#endif
//编程规约,保留勿删
//=================================================
//
//变 量 名:类型 + 名字(首字大写)
// 如,整型数据,int nRecv, char * pSend;
//
//函 数 名:名字(首字大写)
// 如,函数void DeleteData();
//
//数据类型名:全小写或者全大写,全小写取下划线分隔,
// 如果是涉及自定义指针一律用P+全大写,
// 如ip_header,IP_HEADER,PIP_HEADER;
//
//注 释 风格:一律用"//"和中文,少数情况可以用英文;
// 大模块之间用"="分隔,小模块用"-"分隔。
//
//2009年9月11日
//=============