免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2919 | 回复: 4
打印 上一主题 下一主题

ping的源程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-10-29 11:50 |只看该作者 |倒序浏览
// 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(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >;>; 16) + (cksum & 0xffff);
cksum += (cksum >;>;16);
return (USHORT)(~cksum);
}
//
// Function: DecodeIPOptions
//
// Description:
// If the IP option header is present, find the IP options
// within the IP header and print the record route option
// values
//
void DecodeIPOptions(char *buf, int bytes)
{
IpOptionHeader *ipopt = NULL;
IN_ADDR inaddr;
int i;
HOSTENT *host = NULL;

ipopt = (IpOptionHeader *)(buf + 20);

printf("RR: ";
for(i = 0; i < (ipopt->;ptr / 4) - 1; i++)
{
inaddr.S_un.S_addr = ipopt->;addr;
if (i != 0)
printf(" ";
host = gethostbyaddr((char *)&inaddr.S_un.S_addr,
sizeof(inaddr.S_un.S_addr), AF_INET);
if (host)
printf("(%-15s) %s\n", inet_ntoa(inaddr), host->;h_name);
else
printf("(%-15s)\n", inet_ntoa(inaddr));
}
return;
}

//
// Function: DecodeICMPHeader
//
// Description:
// The response is an IP packet. We must decode the IP header to
// locate the ICMP data.
//
void DecodeICMPHeader(char *buf, int bytes,
struct sockaddr_in *from)
{
IpHeader *iphdr = NULL;
IcmpHeader *icmphdr = NULL;
unsigned short iphdrlen;
DWORD tick;
static int icmpcount = 0;

iphdr = (IpHeader *)buf;
// Number of 32-bit words * 4 = bytes
iphdrlen = iphdr->;h_len * 4;
tick = GetTickCount();

if ((iphdrlen == MAX_IP_HDR_SIZE) && (!icmpcount))
DecodeIPOptions(buf, 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)
{
printf("nonecho type %d recvd\n", icmphdr->;i_type);
return;
}
// Make sure this is an ICMP reply to something we sent!
//
if (icmphdr->;i_id != (USHORT)GetCurrentProcessId())
{
printf("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", tick - icmphdr->;timestamp);
printf("\n";

icmpcount++;
return;
}

void ValidateArgs(int argc, char **argv)
{
int i;

bRecordRoute = FALSE;
lpdest = NULL;
datasize = DEF_PACKET_SIZE;

for(i = 1; i < argc; i++)
{
if ((argv[0] == '-') || (argv[0] == '/'))
{
switch (tolower(argv[1]))
{
case 'r': // Record route option
bRecordRoute = TRUE;
break;
default:
usage(argv[0]);
break;
}
}
else if (isdigit(argv[0]))
datasize = atoi(argv);
else
lpdest = argv;
}
}
//
// Function: main
//
// Description:
// Setup the ICMP raw socket, and create the ICMP header. Add
// the appropriate IP option header, and start sending ICMP
// echo requests to the endpoint. For each send and receive,
// we set a timeout value so that we don't wait forever for a
// response in case the endpoint is not responding. When we
// receive a packet decode it.
//
int main(int argc, char **argv)
{

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


if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
ValidateArgs(argc, argv);

//
// WSA_FLAG_OVERLAPPED flag is required for SO_RCVTIMEO,
// SO_SNDTIMEO option. If NULL is used as last param for
// WSASocket, all I/O on the socket is synchronous, the
// internal user mode wait code never gets a chance to
// execute, and therefore kernel-mode I/O blocks forever.
// A socket created via the socket function has the over-
// lapped I/O attribute set internally. But here we need
// to use WSASocket to specify a raw socket.
//
// If you want to use timeout with a synchronous
// nonoverlapped socket created by WSASocket with last
// param set to NULL, you can set the timeout by using
// the select function, or you can use WSAEventSelect and
// set the timeout in the WSAWaitForMultipleEvents
// function.
//
sockRaw = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,
WSA_FLAG_OVERLAPPED);
if (sockRaw == INVALID_SOCKET)
{
printf("WSASocket() failed: %d\n", WSAGetLastError());
return -1;
}
if (bRecordRoute)
{
// Setup the IP option header to go out on every ICMP packet
//
ZeroMemory(&ipopt, sizeof(ipopt));
ipopt.code = IP_RECORD_ROUTE; // Record route option
ipopt.ptr = 4; // Point to the first addr offset
ipopt.len = 39; // Length of option header

ret = setsockopt(sockRaw, IPPROTO_IP, IP_OPTIONS,
(char *)&ipopt, sizeof(ipopt));
if (ret == SOCKET_ERROR)
{
printf("setsockopt(IP_OPTIONS) failed: %d\n",
WSAGetLastError());
}
}
// Set the send/recv timeout values
//
bread = setsockopt(sockRaw, SOL_SOCKET, SO_RCVTIMEO,
(char*)&timeout, sizeof(timeout));
if(bread == SOCKET_ERROR)
{
printf("setsockopt(SO_RCVTIMEO) failed: %d\n",
WSAGetLastError());
return -1;
}
timeout = 1000;
bread = setsockopt(sockRaw, SOL_SOCKET, SO_SNDTIMEO,
(char*)&timeout, sizeof(timeout));
if (bread == SOCKET_ERROR)
{
printf("setsockopt(SO_SNDTIMEO) failed: %d\n",
WSAGetLastError());
return -1;
}
memset(&dest, 0, sizeof(dest));
//
// Resolve the endpoint's name if necessary
//
dest.sin_family = AF_INET;
if ((dest.sin_addr.s_addr = inet_addr(lpdest)) == INADDR_NONE)
{
if ((hp = gethostbyname(lpdest)) != NULL)
{
memcpy(&(dest.sin_addr), hp->;h_addr, hp->;h_length);
dest.sin_family = hp->;h_addrtype;
printf("dest.sin_addr = %s\n", inet_ntoa(dest.sin_addr));
}
else
{
printf("gethostbyname() failed: %d\n",
WSAGetLastError());
return -1;
}
}
//
// Create the ICMP packet
//
datasize += sizeof(IcmpHeader);

icmp_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
MAX_PACKET);
recvbuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
MAX_PACKET);
if (!icmp_data)
{
printf("HeapAlloc() failed: %d\n", GetLastError());
return -1;
}
memset(icmp_data,0,MAX_PACKET);
FillICMPData(icmp_data,datasize);
//
// Start sending/receiving ICMP packets
//
while(1)
{
static int nCount = 0;
int bwrote;

if (nCount++ == 4)
break;

((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;
}
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
if (bwrote < datasize)
{
printf("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;
}
printf("recvfrom() failed: %d\n", WSAGetLastError());
return -1;
}
DecodeICMPHeader(recvbuf, bread, &from);

Sleep(1000);
}
// Cleanup
//
if (sockRaw != INVALID_SOCKET)
closesocket(sockRaw);
HeapFree(GetProcessHeap(), 0, recvbuf);
HeapFree(GetProcessHeap(), 0, icmp_data);

WSACleanup();
return 0;
}





论坛徽章:
0
2 [报告]
发表于 2003-10-29 12:02 |只看该作者

ping的源程序

美国Borland软件公司简介  

自1983年成立以来, Borland   软件公司(纳斯达克代码:BORL)就一直是平台独立的软件开发与分发解决方案领域的领导者,致力于加速整个应用程序开发周期。作为开发领域的先驱, Borland是首批发布DOS开发环境下的开发工具厂商,其产品Turbo Pascal?使PC应用程序的商用开发成为可能。

多年以来, Borland一直保持着提供卓越产品的传统,满足全世界范围上百万软件专业人员的需求,在技术上不断精益求精,以满足商务环境不断发展的要求。Borland认识到,当今全球的公司都需要可以帮助他们比以往更快速、更有效工作的解决方案,而提供这些公司所需要完整的应用周期解决方案,帮助他们以更快的速度开发出更好的软件,是Borland一贯的承诺。

更好的软件来源于与管理员,测试人员,设计人员,开发者以及实时环境中的操作人员无缝连接。Borland引领着Java和 Linux开发解决方案的市场,作为首个获得Microsoft .NET Framework 软件开发工具包许可的公司,Borland被公认为是通向.NET的首选独立通道。. Borland致力于开放标准与平台的独立性,实现可以跨所有主流架构进行工作,其中包括: Solaris,IBM, JAVA, MAC,Palm, Windows以及 Microsoft .NET Framework。

在全球范围内,从财富1000到Borland开发者网络 有几百万开发者,用户使用Borland产品满足他们的需求,帮助他们在不放弃过去成果的同时移向未来。这些公司之所以采用Borland技术,是因为他们信赖Borland这一品牌,尤其是信赖该公司在加速应用软件开发领域中提供卓越产品的传统。

合作伙伴:Apple, BEA, 爱立信,日立,Hewlett-Packard, IBM, 英特尔,微软,Netscape, 诺基亚,Novell, 甲骨文,SAP, 西门子,以及 Sun Microsystems


应用程序周期管理技术解决方案

设计
Star Team&amp;
Caliber RM™
Borland&amp; Together

开发
JAVA
Borland Enterprise Studio for Java™
JBuilder&amp;
Optimizeit™ ServerTrace
OptimizeIt™ Suite

.NET
Kylix™
Delphi™
C++ Mobile
Optimizeit™ Profiler

分发
Borland Enterprise Server (AppServer™, VisiBroker&amp;, Team Editions)
Borland JDataStore™
Borland Interbase&amp;

专业服务
咨询,支持与培训

欲了解有关产品的更多信息请访问http://www.borland.com/products/index.html


联系Borland
美国Borland软件公司北京代表处
地址:北京市东城区东中街39号东环广场写字楼A座9层A-B号
邮编:100027
电话:8610-64183766
传真:8610-64183768
电子邮件:china-support@borland.com(技术服务)
     saleschina@borland.com(销售咨询)

论坛徽章:
0
3 [报告]
发表于 2003-10-29 13:05 |只看该作者

ping的源程序

???

论坛徽章:
0
4 [报告]
发表于 2003-10-29 19:41 |只看该作者

ping的源程序

我感觉steven的<UNIX网络编程>;中的Ping的源程序,要比这个好,作的说明简单明了,而且还兼容IPV6标准。

论坛徽章:
0
5 [报告]
发表于 2003-10-30 08:50 |只看该作者

ping的源程序

同感。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP