ping的源程序

来源:网络收集 责任编辑:栏目编辑 发表时间:2013-07-01 14:30 点击:

网上收集
如侵犯您的权力,请来信告知

// Module Name: Ping.c
//
// Description:
// This sample illustrates how an ICMP ping app can be written
// using the SOCK_RAW socket type and IPPROTO_ICMP protocol.
// By creating a raw socket, the underlying layer does not change
// the protocol header so that when we submit the ICMP header
// nothing is changed so that the receiving end will see an
// ICMP packet. Additionally, we use the record route IP option
// to get a round trip path to the endpoint. Note that the size
// of the IP option header that records the route is limited to
// nine IP addresses.
//
// Compile:
// cl -o Ping Ping.c ws2_32.lib /Zp1
//
// Command Line Options/Parameters:
// Ping [host] [packet-size]
//
// host String name of host to ping
// packet-size Integer size of packet to send
// (smaller than 1024 bytes)
//
//#pragma pack(1)

#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include

#define IP_RECORD_ROUTE 0x7
//
// IP header structure
//
typedef struct _iphdr
{
unsigned int h_len:4; // Length of the header
unsigned int version:4; // Version of IP
unsigned char tos; // Type of service
unsigned short total_len; // Total length of the packet
unsigned short ident; // Unique identifier
unsigned short frag_and_flags; // Flags
unsigned char ttl; // Time to live
unsigned char proto; // Protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum

unsigned int sourceIP;
unsigned int destIP;
} IpHeader;

#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0
#define ICMP_MIN 8 // Minimum 8-byte ICMP packet (header)

//
// ICMP header structure
//
typedef struct _icmphdr
{
BYTE i_type;
BYTE i_code; // Type sub code
USHORT i_cksum;
USHORT i_id;
USHORT i_seq;
// This is not the standard header, but we reserve space for time
ULONG timestamp;
} IcmpHeader;


//
// IP option header - use with socket option IP_OPTIONS
//
typedef struct _ipoptionhdr
{
unsigned char code; // Option type
unsigned char len; // Length of option hdr
unsigned char ptr; // Offset into options
unsigned long addr[9]; // List of IP addrs
} IpOptionHeader;

#define DEF_PACKET_SIZE 32 // Default packet size
#define MAX_PACKET 1024 // Max ICMP packet size
#define MAX_IP_HDR_SIZE 60 // Max IP header size w/options

BOOL bRecordRoute;
int datasize;
char *lpdest;

//
// Function: usage
//
// Description:
// Print usage information
//
void usage(char *progname)
{
printf("usage: ping -r [data size]\n");
printf(" -r record route\n");
printf(" host remote machine to ping\n");
printf(" datasize can be up to 1KB\n");
ExitProcess(-1);
}

//
// Function: FillICMPData
//
// Description:
// Helper function to fill in various fields for our ICMP request
//
void FillICMPData(char *icmp_data, int datasize)
{
IcmpHeader *icmp_hdr = NULL;
char *datapart = NULL;

icmp_hdr = (IcmpHeader*)icmp_data;
icmp_hdr->i_type = ICMP_ECHO; // Request an ICMP echo
icmp_hdr->i_code = 0;
icmp_hdr->i_id = (USHORT)GetCurrentProcessId();
icmp_hdr->i_cksum = 0;
icmp_hdr->i_seq = 0;

datapart = icmp_data + sizeof(IcmpHeader);
//
// Place some junk in the buffer
//
memset(datapart,'E', datasize - sizeof(IcmpHeader));
}


//
// Function: checksum
//
// Description:
// This function calculates the 16-bit one's complement sum
// of the supplied buffer (ICMP) header
//
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;

while (size > 1)
{
cksum += *buffer++;
size -= sizeof(

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • 用C#制作屏幕捕获程序
      • .NET程序员项目开发必知必会—Dev环境中的集成测试用例执行时上
      • 遍历ArrayList易犯错误
      • C#对XML操作:一个处理XML文件的类(1)
      • .NET简谈反射(动态调用)
      • 使用C#编写LED样式时钟控件
      • DataList嵌套问题 如何删除内层子DataList的记录
      • 怎样用C#实现完整文档打印功能
      • .NET简谈自定义事务资源管理器
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1