透析ICMP协议(四): 应用篇ping(RAW Socket) …

2008-02-22 12:38:04来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


int allocate_buffers(ICMPHeader*& send_buf, IPHeader*& recv_buf,
int packet_size)
{
// First the send buffer
send_buf = (ICMPHeader*)new char[packet_size];
if (send_buf == 0) {
cerr << "Failed to allocate output buffer." << endl;
return -1;
}

// And then the receive buffer
recv_buf = (IPHeader*)new char[MAX_PING_PACKET_SIZE];
if (recv_buf == 0) {
cerr << "Failed to allocate output buffer." << endl;
return -1;
}

return 0;
}

/*
* 程序名: rawping.h
* 说明:
* 主要函数库头文件
*/

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>

// ICMP 包类型, 具体参见本文的第一节
#define ICMP_ECHO_REPLY 0
#define ICMP_DEST_UNREACH 3
#define ICMP_TTL_EXPIRE 11
#define ICMP_ECHO_REQUEST 8

// 最小的ICMP包大小
#define ICMP_MIN 8


// IP 包头
struct IPHeader {
BYTE h_len:4; // Length of the header in dwords
BYTE version:4; // Version of IP
BYTE tos; // Type of service
USHORT total_len; // Length of the packet in dwords
USHORT ident; // unique identifier
USHORT flags; // Flags
BYTE ttl; // Time to live, 这个字段我在下一节中用来实现Tracert功能
BYTE proto; // Protocol number (TCP, UDP etc)
USHORT checksum; // IP checksum
ULONG source_ip;
ULONG dest_ip;
};

// ICMP 包头(实际的包不包括timestamp字段,
// 作者用来计算包的回应时间,其实完全没有必要这样做)
struct ICMPHeader {
BYTE type; // ICMP packet type
BYTE code; // Type sub code
USHORT checksum;
USHORT id;
USHORT seq;
ULONG timestamp; // not part of ICMP, but we need it
};


extern USHORT ip_checksum(USHORT* buffer, int size);
extern int setup_for_ping(char* host, int ttl, SOCKET& sd, sockaddr_in& dest);
extern int send_ping(SOCKET sd, const sockaddr_in& dest, ICMPHeader* send_buf, int packet_size);
extern int recv_ping(SOCKET sd, sockaddr_in& source, IPHeader* recv_buf,
int packet_size);
extern int decode_reply(IPHeader* reply, int bytes, sockaddr_in* from);
extern void init_ping_packet(ICMPHeader* icmp_hdr, int packet_size, int seq_no);

/*
* 程序名: rawping.cpp
* 说明:
* 主要函数库实现部分
*/

include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream.h>
#include "rawping.h"

// 计算ICMP包的校验和的简单算法, 很多地方都有说明, 这里没有必要详细将
// 只是一点要提, 做校验之前, 务必将ICMP包头的checksum字段置为0
USHORT ip_checksum(USHORT* buffer, int size)
{
unsigned long cksum = 0;

// Sum all the words together, adding the final byte if size is odd
while (size > 1) {
cksum = *buffer ;
size -= sizeof(USHORT);
}
if (size) {
cksum = *(UCHAR*)buffer;
}

// Do a little shuffling
cksum = (cksum >> 16) (cksum & 0xffff);
cksum = (cksum >> 16);

// Return the bitwise complement of the resulting mishmash
return (USHORT)(~cksum);
}

//初试化RAW Socket, 设置ttl, 初试化dest
// 返回值 <0 表失败

int setup_for_ping(char* host, int ttl, SOCKET& sd, sockaddr_in& dest)
{
// Create the socket
sd = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, 0, 0);
if (sd == INVALID_SOCKET) {
cerr << "Failed to create raw socket: " << WSAGetLastError() <<
endl;
return -1;
}

if (setsockopt(sd, IPPROTO_IP, IP_TTL, (const char*)&ttl,
sizeof(ttl)) == SOCKET_ERROR) {
cerr << "TTL setsockopt failed: " << WSAGetLastError() << endl;
return -1;
}

// Initialize the destination host info block
memset(&dest, 0, sizeof(dest));

// Turn first passed parameter into an IP address to ping
unsigned int addr = inet_addr(host);
if (addr != INADDR_NONE) {
// It was a dotted quad number, so save result
dest.sin_addr.s_addr = addr;
dest.sin_family = AF_INET;
}
else {
// Not in dotted quad form, so try and look it up
hostent* hp = gethostbyname(host);
if (hp != 0) {
// Found an address for that host, so save it
memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);
dest.sin_family = hp->h_addrtype;
}
else {
// Not a recognized hostname either!
cerr << "Failed to resolve " << host << endl;
return -1;
}
}

return 0;
}

//初试化ICMP的包头, 给data部分填充数据, 最后计算整个包的校验和

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:透析ICMP协议(三): 应用篇ping(ICMP.dll)

下一篇:透析ICMP协议(五): 应用篇路由追踪