我找到了一个实现icmp协议的源代码,但是有些地方看不懂,请帮忙解答!

liszt 2004-03-01 08:44:57
这段代码是一个老外在1998年用Microsoft VisualC++ 4.2 and MFC实现的。(下面贴的就是icmp.h的代码)我要问的问题:在icmp.H文件中,第一行可执行语句#pragma pack (4)是什么意思?
在定义IP数据报头的文件里,“unsigned int HeaderLength:4;”中HeaderLength:4是一个整形吗?这是什么意思?而且我看该.H文件并没有包含其他的文件呀。
//////////////////////////////////////////////////////////////////////////////////

#pragma pack (4)

//////////////////////////////////////////////////////////////////
// //
// //
//////////////////////////////////////////////////////////////////

#define MAXBFRSIZE 2048

#define MAX_HOPS 30

typedef SOCKET * LPSOCKET;

typedef in_addr * LPINADDR;

//////////////////////////////////////////////////////////////////
// //
// IP Header //
// Implementation of RFC791 IP Header //
// //
//////////////////////////////////////////////////////////////////

typedef struct _IpHeader
{
unsigned int HeaderLength:4; // length of the header
unsigned int Version:4; // Version of IP
unsigned char TypeOfService; // Type of service
unsigned short TotalLength; // total length of the packet
unsigned short Identification; // unique identifier
unsigned short FragmentationFlags; // flags
unsigned char TTL; // Time To Live
unsigned char Protocol; // protocol (TCP, UDP etc)
unsigned short CheckSum; // IP Header checksum

unsigned int sourceIPAddress; // Source address
unsigned int destIPAddress; // Destination Address

} IpHeader;

typedef IpHeader FAR * LPIpHeader;

#define IpHeaderLength sizeof(IpHeader)

//////////////////////////////////////////////////////////////////
// //
// ICMP header //
// Implementation of RFC792 ICMP Header //
// //
//////////////////////////////////////////////////////////////////
typedef struct _IcmpHeader
{
BYTE IcmpType;
BYTE IcmpCode; // Type sub code
USHORT IcmpChecksum;
USHORT IcmpId;
USHORT IcmpSeq;
ULONG IcmpTimestamp; // Not standard field in header, but reserved nonetheless
} IcmpHeader;

typedef IcmpHeader FAR * LPIcmpHeader;

#define IcmpHeaderLength sizeof(IcmpHeader)

//////////////////////////////////////////////////////////////////

#define MAX_PACKET 2000 + IcmpHeaderLength

#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0

#define ICMP_MIN 8 // minimum 8 byte icmp packet (just header)

/////////////////////////////////////////////////////////////////////////////
// CIcmp command target

class CIcmp : public CSocket
{
// Attributes
public:

BOOL OpenNewSocket(HWND hWnd, unsigned int NotificationMessage, long NotifyEvents);
BOOL OpenNewSocket(HWND hWnd, unsigned int NotificationMessage, long NotifyEvents, int AFamily, int AType, int AProtocol);

int CloseIcmpSocket(void);

BOOL Connect(int ReceiveTimeout, int SendTimeout);
BOOL Connect(LPINT ReceiveTimeout, LPINT SendTimeout, int AFamily, int AType, int AProtocol);

int SetTTL(int TTL);

int SetAsynchNotification(HWND hWnd, unsigned int Message, long Events);

int Receive(LPSTR pIcmpBuffer, int IcmpBufferSize);

unsigned long GetIPAddress (LPSTR iHostName);

int Ping (LPSTR pIcmpBuffer, int IcmpBufferSize);

unsigned short IcmpChecksum(unsigned short FAR *lpBuf, int Len);

void DisplayError(CString ErrorType, CString FunctionName);

// Operations
public:
CIcmp(void);
CIcmp(CIcmp ©);
~CIcmp(void);

// CIcmp(CIcmp NewIcmp);

// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CIcmp)
//}}AFX_VIRTUAL

// Generated message map functions
//{{AFX_MSG(CIcmp)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG

// Implementation
private:

public:
//
// I/O Buffer Pointers
//

LPIcmpHeader pIcmpHeader;
LPIpHeader pIpHeader;

SOCKET icmpSocket;

SOCKADDR_IN icmpSockAddr;
SOCKADDR_IN rcvSockAddr;

DWORD icmpRoundTripTime;

DWORD icmpPingSentAt;
DWORD icmpPingReceivedAt;

int icmpRcvLen;

int icmpHops;
int icmpMaxHops;

int icmpCurSeq;
int icmpCurId;

int icmpPingTimer;

int icmpSocketError;
int icmpSocketErrorMod;

unsigned long icmpHostAddress;

protected:
};

typedef CIcmp FAR * LPIcmp;
//////////////////////////////////////////////////////////////////////////////////////
...全文
84 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧