`
izuoyan
  • 浏览: 9223966 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ping 命令

阅读更多

/******************************************************************************\
* ping.c - Simple ping utility using SOCK_RAW
*
* This is a part of the Microsoft Source Code Samples.
* Copyright 1996-1997 Microsoft Corporation.
* All rights reserved.
* This source code is only intended as a supplement to
* Microsoft Development Tools and/or WinHelp documentation.
* See these sources for detailed information regarding the
* Microsoft samples programs.
\******************************************************************************/

#pragma pack(4)

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>

#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0

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

/* The IP header */
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;
unsigned char proto; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum

unsigned int sourceIP;
unsigned int destIP;

}IpHeader;

//
// ICMP header
//
typedef struct _ihdr {
BYTE i_type;
BYTE i_code; /* type sub code */
USHORT i_cksum;
USHORT i_id;
USHORT i_seq;
/* This is not the std header, but we reserve space for time */
ULONG timestamp;
}IcmpHeader;

#define STATUS_FAILED 0xFFFF
#define DEF_PACKET_SIZE 32
#define MAX_PACKET 1024

#define xmalloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))
#define xfree(p) HeapFree (GetProcessHeap(),0,(p))

void fill_icmp_data(char *, int);
USHORT checksum(USHORT *, int);
void decode_resp(char *,int ,struct sockaddr_in *);

void Usage(char *progname){

fprintf(stderr,"Usage:\n");
fprintf(stderr,"%s <host> [data_size]\n",progname);
fprintf(stderr,"datasize can be up to 1Kb\n");
ExitProcess(STATUS_FAILED);

}
int main(int argc, char **argv){

WSADATA wsaData;
SOCKET sockRaw;
struct sockaddr_in dest,from;
struct hostent * hp;
int bread,datasize;
int fromlen = sizeof(from);
int timeout = 1000;
char *dest_ip;
char *icmp_data;
char *recvbuf;
unsigned int addr=0;
USHORT seq_no = 0;

if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0){
fprintf(stderr,"WSAStartup failed: %d\n",GetLastError());
ExitProcess(STATUS_FAILED);
}

if (argc <2 ) {
Usage(argv[0]);
}
sockRaw = WSASocket (AF_INET,
SOCK_RAW,
IPPROTO_ICMP,
NULL, 0,0);

if (sockRaw == INVALID_SOCKET) {
fprintf(stderr,"WSASocket() failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
bread = setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,
sizeof(timeout));
if(bread == SOCKET_ERROR) {
fprintf(stderr,"failed to set recv timeout: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
timeout = 1000;
bread = setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,
sizeof(timeout));
if(bread == SOCKET_ERROR) {
fprintf(stderr,"failed to set send timeout: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
memset(&dest,0,sizeof(dest));

hp = gethostbyname(argv[1]);

if (!hp){
addr = inet_addr(argv[1]);
}
if ((!hp) && (addr == INADDR_NONE) ) {
fprintf(stderr,"Unable to resolve %s\n",argv[1]);
ExitProcess(STATUS_FAILED);
}

if (hp != NULL)
memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
else
dest.sin_addr.s_addr = addr;

if (hp)
dest.sin_family = hp->h_addrtype;
else
dest.sin_family = AF_INET;

dest_ip = inet_ntoa(dest.sin_addr);

if (argc >2) {
datasize = atoi(argv[2]);
if (datasize == 0)
datasize = DEF_PACKET_SIZE;

}
else
datasize = DEF_PACKET_SIZE;

datasize += sizeof(IcmpHeader);

icmp_data = xmalloc(MAX_PACKET);
recvbuf = xmalloc(MAX_PACKET);

if (!icmp_data) {
fprintf(stderr,"HeapAlloc failed %d\n",GetLastError());
ExitProcess(STATUS_FAILED);
}


memset(icmp_data,0,MAX_PACKET);
fill_icmp_data(icmp_data,datasize);

while(1) {
int bwrote;

((IcmpHeader*)icmp_data)->i_cksum = 0;
((IcmpHeader*)icmp_data)->timestamp = GetTickCount();

((IcmpHeader*)icmp_data)->i_seq = seq_no++;
((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data,
datasize);

bwrote = sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)&dest,
sizeof(dest));
if (bwrote == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("timed out\n");
continue;
}
fprintf(stderr,"sendto failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
if (bwrote < datasize ) {
fprintf(stdout,"Wrote %d bytes\n",bwrote);
}
bread = recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(struct sockaddr*)&from,
&fromlen);
if (bread == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("timed out\n");
continue;
}
fprintf(stderr,"recvfrom failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
decode_resp(recvbuf,bread,&from);
Sleep(1000);

}
return 0;

}
/*
The response is an IP packet. We must decode the IP header to locate
the ICMP data
*/
void decode_resp(char *buf, int bytes,struct sockaddr_in *from) {

IpHeader *iphdr;
IcmpHeader *icmphdr;
unsigned short iphdrlen;

iphdr = (IpHeader *)buf;

iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes

if (bytes < iphdrlen + ICMP_MIN) {
printf("Too few bytes from %s\n",inet_ntoa(from->sin_addr));
}

icmphdr = (IcmpHeader*)(buf + iphdrlen);

if (icmphdr->i_type != ICMP_ECHOREPLY) {
fprintf(stderr,"non-echo type %d recvd\n",icmphdr->i_type);
return;
}
if (icmphdr->i_id != (USHORT)GetCurrentProcessId()) {
fprintf(stderr,"someone else's packet!\n");
return ;
}
printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr));
printf(" icmp_seq = %d. ",icmphdr->i_seq);
printf(" time: %d ms ",GetTickCount()-icmphdr->timestamp);
printf("\n");

}


USHORT checksum(USHORT *buffer, int size) {

unsigned long cksum=0;

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

if(size ) {
cksum += *(UCHAR*)buffer;
}

cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
/*
Helper function to fill in various stuff in our ICMP request.
*/
void fill_icmp_data(char * icmp_data, int datasize){

IcmpHeader *icmp_hdr;
char *datapart;

icmp_hdr = (IcmpHeader*)icmp_data;

icmp_hdr->i_type = 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));
}

可以研究的东西

分享到:
评论

相关推荐

    tnsping命令解析

    ### tnsping命令详解 #### 一、tnsping命令简介 tnsping是一个由Oracle提供的网络诊断工具,主要用于测试客户端能否成功连接到远程数据库。它通过模拟客户端发送请求到数据库监听器(Listener),以此来验证数据库...

    java中ping命令ping工具类(循环ping)

    java中ping命令ping工具类(循环ping) java ping ip ping命令 ping工具类 支持linux和windows等所有平台 Ping是Windows下的一个命令 在Unix和Linux下也有这个命令。 ping也属于一个通信协议,是TCP/IP协议的一部分 ...

    Ping命令实现的扩充

    实验内容:Ping命令实现的扩充,在给定的Ping程序的基础上做如下功能扩充: ? -h 显示帮助信息 ? -b 允许ping一个广播地址,只用于IPv4 ? -t 设置ttl值,只用于IPv4 ? -q 安静模式。不显示每个收到的包的分析结果,...

    增强版ping命令(带时间戳)

    在IT领域,网络诊断是日常运维中的重要环节,而ping命令是其中最基础且实用的工具之一。它用于检查网络连接是否畅通,以及测量数据包从发送到接收所需的时间。然而,标准的ping命令并不记录时间戳,这在进行长时间的...

    ping命令带时间,便于分析网络异常

    ping命令是网络诊断中最常用的工具之一,主要用于检测网络连接的连通性、延迟以及丢包情况。在标题和描述中提到的"ping命令带时间",实际上是指在执行ping命令时,显示每个回应数据包的发送和接收时间,这对于分析...

    ping命令详解-学习ping命令

    ### ping命令详解 #### 一、ping命令简介与作用 ping命令是一种常用的网络诊断工具,用于测试网络连接是否可达及网络延迟情况。它通过发送ICMP(Internet Control Message Protocol)报文来检测目标主机是否响应,...

    web页面实现ping 命令

    **ping命令** 是一个网络诊断工具,用于检查网络连接是否通畅或测量数据包从发送到接收所需的时间。它通过发送ICMP(Internet Control Message Protocol)回显请求报文到目标主机,然后接收响应来确定网络的连通性和...

    操作系统课程设计——Ping命令模拟实现

    操作系统课程设计——Ping命令模拟实现 本章节主要讲述了操作系统课程设计中Ping命令的模拟实现,使用ICMP协议来实现Ping命令的基本功能。 2.1 Ping命令的基本概念 Ping命令是网络诊断工具,用于测试网络连接...

    pb实现ping命令代码

    在PB(PowerBuilder)开发环境中实现ping命令的功能,是一个典型的网络编程示例,涉及到了操作系统提供的网络库函数的调用,以及对网络协议的理解。以下是对该代码的关键知识点的详细解析: ### 1. ICMP协议简介 ...

    ping命令的模拟实现

    【ping命令的模拟实现】 ping命令是网络诊断中不可或缺的工具,它基于Internet控制消息协议(ICMP)来检查网络连接的可达性。在本文中,我们将深入理解ping命令的工作原理,并探讨如何模拟实现这一功能。 ICMP是...

    ping 命令的高级用法

    Ping 命令的高级用法 Ping 命令是网络诊断和故障排除中最基本和最常用的工具之一。它可以用来检测本地主机和远程主机之间的网络连接是否正常,检查 TCP/IP 参数是否设置正确,从而帮助网络管理员和用户排除网络故障...

    正确使用Windows中的Ping命令

    使用 Windows 中的 Ping 命令 Ping 命令是 Windows 操作系统中非常实用的网络诊断工具,它可以帮助用户检测网络连接是否正常、网络延迟、 packet loss 等问题。然而,很多人可能不知道如何正确使用 Ping 命令来发挥...

    Ping命令的自动实现

    【标题】"Ping命令的自动实现"涉及到网络诊断与监控中的基本工具——Ping。Ping是Internet控制报文协议(ICMP)的一部分,用于测试网络连接的可达性和响应时间。通过发送ICMP回显请求数据包到目标主机并接收回应,...

    ping命令

    ping命令

    C语言实现PING命令

    标题 "C语言实现PING命令" 涉及到的是使用C语言编程来创建一个功能类似于Linux系统中的`ping`命令的程序。`ping`命令是网络诊断工具,它通过发送Internet控制消息协议(ICMP)回显请求报文来检查网络连接是否可达。...

Global site tag (gtag.js) - Google Analytics