日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

四 透析ICMP協(xié)議: 應(yīng)用篇ping(RAW Socket)( 二 )

<
0) {
// Pull the sequence number out of the ICMP header.; If
// it"s bad, we just complain, but otherwise we take
// off, because the read failed for some reason.
unsigned short header_len = recv_buf->h_len * 4;
ICMPHeader* icmphdr = (ICMPHeader*)
((char*)recv_bufheader_len);
if (icmphdr->seq != seq_no) {
cerr << "bad sequence number!" << endl;
continue;
}
else {
break;
}
}
if (decode_reply(recv_buf, packet_size, &source) != -2) {
// SUCcess or fatal error (as opposed to a minor error)
// so take off.
break;
}
}
}

cleanup:
delete[]send_buf//釋放分配的內(nèi)存
delete[]recv_buf;
WSACleanup(); // 清理winsock
return 0;
}
// 為send_buf 和 recv_buf的內(nèi)存分配. 太簡(jiǎn)單, 我略過
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
* 說明:;
*;;;;主要函數(shù)庫頭文件
*/
#define WIN32_LEAN_AND_MEAN
#include
// ICMP 包類型, 具體參見本文的第一節(jié)
#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, 這個(gè)字段我在下一節(jié)中用來實(shí)現(xiàn)Tracert功能
BYTE proto;;;// Protocol number (TCP, UDP etc)
USHORT checksum // IP checksum
ULONG source_ip;
ULONG dest_ip;
};
// ICMP 包頭(實(shí)際的包不包括timestamp字段,
// 作者用來計(jì)算包的回應(yīng)時(shí)間,其實(shí)完全沒有必要這樣做)
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
* 說明:;
*;;;;主要函數(shù)庫實(shí)現(xiàn)部分
*/
include
#include
#include
#include "rawping.h"
// 計(jì)算ICMP包的校驗(yàn)和的簡(jiǎn)單算法, 很多地方都有說明, 這里沒有必要具體將
// 只是一點(diǎn)要提, 做校驗(yàn)之前, 務(wù)必將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) {

推薦閱讀