寻求一个 ping的源程序。

wyb_45 2003-11-11 10:54:01
找了很多,都不是很满意
有shell的有的只能ping 127.0.0.1
wyb45@sohu.com
...全文
62 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyb_45 2003-11-29
  • 打赏
  • 举报
回复
先谢谢你
mosaic 2003-11-28
  • 打赏
  • 举报
回复
哦,忘了说一声,里面有c++的注释 //
去了就行了。
或者cc编译的时候加上
-qcpluscmt (IBM)
compaq,hp上缺省应该是支持c++的注释形式的

mosaic 2003-11-28
  • 打赏
  • 举报
回复
给你一个.原来是用在linux上的,我稍微改了一下,去掉了getopt相关的内容.
在hp,ibm,compaq上都应该没有问题。不过值得注意的是这是"TCP"的ping,
跟一般使用icmp协议的ping有所不同.

/* poink.c

Nosuid TCP/IP ping 1.6 beta by Michal Zalewski <lcamtuf@coredump.cx>
--------------------------------------------------------------------

The Nosuid TCP/IP ping and related utilities are free software; you
can redistribute it and/or modify it under the terms of the GNU Library
General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

*/

#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
/*#include <getopt.h>*/
#include <stdlib.h>
#include <string.h>

#define DEFAULT_PORT 1
#define DEFAULT_TMOUT 4
#define DEFAULT_REP 0
#define DEFAULT_DELAY 1
#define MS_THRES 500


#define debug(x) fprintf(stderr,x)

#define fatal(x) do { \
debug(x); \
exit(1); \
} while (0)

#define pfatal(x) do { \
perror(x); \
exit(1); \
} while (0)


static char iptext[20],got_alrm;
static unsigned int received,sent,max_rtt,total_rtt;
static unsigned int min_rtt=(unsigned int)-1;


static void sig_alarm(int dummy) {
got_alrm=1;
}


static void do_stats(int x) {

printf("\n-- %s ping statistics --\n",iptext);
printf("%d packet(s) transmitted, %d packet(s) "
"received, %d%% packets lost.\n",sent,
received,(sent-received)*100/sent);

printf("round-trip statistics min/avg/max: ");

if (received) {

printf("%.1f/%.1f/%.1f ms",(float)min_rtt/1000,
(float)total_rtt/received/1000,(float)max_rtt/1000);

if (min_rtt < MS_THRES) printf(" (%d/%d/%d usec)",min_rtt,
total_rtt/received,max_rtt);
puts(".");

} else printf("(not available due to 100%% packets lost).\n");

exit(0);
}


static void usage(void) {
fatal("Usage: poink [ -i delay ] [ -c count ] [ -t timeout ] host\n");
}


int main(int argc,char* argv[]) {

unsigned int count=DEFAULT_REP,
delay=DEFAULT_DELAY,
timeout=DEFAULT_TMOUT;

unsigned int n=0,port=DEFAULT_PORT;

char ch;
unsigned char *iaddr;
struct hostent* hp;
struct sockaddr_in s;
struct timeval tv1,tv2;

printf("Nosuid TCP/IP ping 1.6 by <lcamtuf@coredump.cx>\n");

/* while ((ch=getopt(argc,argv,"i:t:c:")) != EOF)
switch(ch) {

case 'c':
if (sscanf(optarg,"%u",&count)!=1)
fatal("poink: invalid number of pings.\n");
break;

case 'i':
if (sscanf(optarg,"%u",&delay)!=1 || delay < 1 || delay > 1000)
fatal("poink: invalid ping delay.\n");
break;

case 't':
if (sscanf(optarg,"%u",&timeout)!=1 || timeout < 1 || timeout > 100)
fatal("poink: invalid ping timeout.\n");
break;

default:
usage();
}

if (argc-optind!=1) usage();*/

hp=gethostbyname(argv[1]);

if (!hp) fatal("ping: host not found.\n");

iaddr=(unsigned char*)hp->h_addr;

sprintf(iptext,"%u.%u.%u.%u",iaddr[0],iaddr[1],iaddr[2],iaddr[3]);

signal(SIGINT,do_stats);

if (count)
printf("Pinging %s (%s) - %d packets, delay %d sec(s), "
"timeout %d sec(s).\n",hp->h_name,iptext,count,delay,timeout);
else
printf("Pinging %s (%s) - delay %d sec(s), timeout %d "
"sec(s).\n",hp->h_name,iptext,delay,timeout);

while (!count || n++ < count) {
int csock;
char skew=0;
struct sigaction siga;
unsigned int delayed;

// First, let's get a socket.//
memcpy((void*)&s.sin_addr,hp->h_addr,hp->h_length);
s.sin_family=hp->h_addrtype;
s.sin_port=htons(port);
if ((csock=socket(AF_INET,SOCK_STREAM,0))<0) pfatal("socket");

// Get ready for SIGALRM... We need this signal not to restart
// the syscall, and this seems to be a good way to do it
siga.sa_handler=sig_alarm;
siga.sa_flags=0;
sigaction(SIGALRM,&siga,NULL);
alarm(timeout);

// Get time of day...
gettimeofday(&tv1,NULL);

sent++;

if (connect(csock,(struct sockaddr*)&s,sizeof(s))) {
if (got_alrm) {
printf("No ping reply from %s within %d second(s)...\n",iptext,
timeout);
got_alrm=0;
shutdown(csock,2);
close(csock);
continue;
} else {
if (errno != ECONNREFUSED) pfatal("connect");
received++;
}
} else {
received++;
skew=1;
port--;
}

alarm(0);

gettimeofday(&tv2,NULL);

// Can overflow with extremely large delays, but c'mon...
delayed=(tv2.tv_sec-tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);

if (delayed >= MS_THRES)
printf("Ping reply from %s: seq=%d, time=%.1f ms",iptext,sent-1,
(float)delayed/1000);
else
printf("Ping reply from %s: seq=%d, time<0.1 ms (%d usec)",iptext,
sent-1,delayed);

if (skew) printf(" (skewed!)\n"); else puts("");

total_rtt += delayed;
if (delayed > max_rtt) max_rtt = delayed;
if (delayed < min_rtt) min_rtt = delayed;

// Be nice.
shutdown(csock,2);
close(csock);

if (!count || n != count) usleep(delay*1000000);

}

do_stats(0);
return 0;

}

fierygnu 2003-11-27
  • 打赏
  • 举报
回复
不会的,好象只是支持的不是非常好。编译是比较费劲,但终归是可以的。到网上,比如HP的网站、GCC的位置等地方去看看能不能下到可执行包。
另外HP自己的编译器编译不行吗?出什么问题?
wyb_45 2003-11-27
  • 打赏
  • 举报
回复
HP-UX的gcc安装不上去
fierygnu 2003-11-14
  • 打赏
  • 举报
回复
用gcc编译。
wyb_45 2003-11-14
  • 打赏
  • 举报
回复
Unix下没有/linux/下的头文件呀
salaciouswolf 2003-11-12
  • 打赏
  • 举报
回复
mark
t0mychen 2003-11-11
  • 打赏
  • 举报
回复

for win
//
// PingI.c -- Simple ping program using the proprietary
// Microsoft ICMP API
//

#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>

typedef struct tagIPINFO
{
u_char Ttl; // Time To Live
u_char Tos; // Type Of Service
u_char IPFlags; // IP flags
u_char OptSize; // Size of options data
u_char FAR *Options; // Options data buffer
}IPINFO, *PIPINFO;

typedef struct tagICMPECHO
{
u_long Source; // Source address
u_long Status; // IP status
u_long RTTime; // Round trip time in milliseconds
u_short DataSize; // Reply data size
u_short Reserved; // Unknown
void FAR *pData; // Reply data buffer
IPINFO ipInfo; // Reply options
}ICMPECHO, *PICMPECHO;


//ICMP API
//HANDLE WINAPI IcmpCreateFile (VOID);
//BOOL WINAPI IcmpCloseHandle (HANDLE IcmpHandle);
//DWORD WINAPI IcmpSendEcho (HANDLE IcmpHandle,IPAddr DestinationAddress,
// LPVOID RequestData,WORD RequestSize,
// PIP_OPTION_INFORMATION RequestOptions,
// LPVOID ReplyBuffer,DWORD ReplySize, DWORD Timeout);

// ICMP.DLL Export Function Pointers
HANDLE (WINAPI *pIcmpCreateFile)(VOID);
BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
DWORD (WINAPI *pIcmpSendEcho)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);

//
//
void main(int argc, char **argv)
{
WSADATA wsaData; // WSADATA
ICMPECHO icmpEcho; // ICMP Echo reply buffer
HANDLE hndlIcmp; // LoadLibrary() handle to ICMP.DLL
HANDLE hndlFile; // Handle for IcmpCreateFile()
LPHOSTENT pHost; // Pointer to host entry structure
struct in_addr iaDest; // Internet address structure
DWORD *dwAddress; // IP Address
IPINFO ipInfo; // IP Options structure
int nRet; // General use return code
DWORD dwRet; // DWORD return code
int x;

// Check arguments
if (argc != 2)
{
fprintf(stderr,"\nSyntax: pingi HostNameOrIPAddress\n");
return;
}

// Dynamically load the ICMP.DLL
hndlIcmp = LoadLibrary("ICMP.DLL");
if (hndlIcmp == NULL)
{
fprintf(stderr,"\nCould not load ICMP.DLL\n");
return;
}
// Retrieve ICMP function pointers
pIcmpCreateFile = (HANDLE (WINAPI *)(void))
GetProcAddress(hndlIcmp,"IcmpCreateFile");
pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
GetProcAddress(hndlIcmp,"IcmpCloseHandle");
pIcmpSendEcho = (DWORD (WINAPI *)
(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
GetProcAddress(hndlIcmp,"IcmpSendEcho");
// Check all the function pointers
if (pIcmpCreateFile == NULL ||
pIcmpCloseHandle == NULL ||
pIcmpSendEcho == NULL)
{
fprintf(stderr,"\nError getting ICMP proc address\n");
FreeLibrary(hndlIcmp);
return;
}

// Init WinSock
nRet = WSAStartup(0x0101, &wsaData );
if (nRet)
{
fprintf(stderr,"\nWSAStartup() error: %d\n", nRet);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}
// Check WinSock version
if (0x0101 != wsaData.wVersion)
{
fprintf(stderr,"\nWinSock version 1.1 not supported\n");
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}

// Lookup destination
// Use inet_addr() to determine if we're dealing with a name
// or an address
iaDest.s_addr = inet_addr(argv[1]);
if (iaDest.s_addr == INADDR_NONE)
pHost = gethostbyname(argv[1]);
else
pHost = gethostbyaddr((const char *)&iaDest,
sizeof(struct in_addr), AF_INET);
if (pHost == NULL)
{
fprintf(stderr, "\n%s not found\n", argv[1]);
WSACleanup();
FreeLibrary(hndlIcmp);
return;
}

// Tell the user what we're doing
printf("\nPinging %s [%s]", pHost->h_name,
inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));

// Copy the IP address
dwAddress = (DWORD *)(*pHost->h_addr_list);

// Get an ICMP echo request handle
hndlFile = pIcmpCreateFile();
for (x = 0; x < 4; x++)
{
// Set some reasonable default values
ipInfo.Ttl = 255;
ipInfo.Tos = 0;
ipInfo.IPFlags = 0;
ipInfo.OptSize = 0;
ipInfo.Options = NULL;
//icmpEcho.ipInfo.Ttl = 256;
// Reqest an ICMP echo
dwRet = pIcmpSendEcho(
hndlFile, // Handle from IcmpCreateFile()
*dwAddress, // Destination IP address
NULL, // Pointer to buffer to send
0, // Size of buffer in bytes
&ipInfo, // Request options
&icmpEcho, // Reply buffer
sizeof(struct tagICMPECHO),
5000); // Time to wait in milliseconds
// Print the results
iaDest.s_addr = icmpEcho.Source;
printf("\nReply from %s Time=%ldms TTL=%d",
inet_ntoa(iaDest),
icmpEcho.RTTime,
icmpEcho.ipInfo.Ttl);
if (icmpEcho.Status)
{
printf("\nError: icmpEcho.Status=%ld",
icmpEcho.Status);
break;
}
}
printf("\n");
// Close the echo request file handle
pIcmpCloseHandle(hndlFile);
FreeLibrary(hndlIcmp);
WSACleanup();
}

fierygnu 2003-11-11
  • 打赏
  • 举报
回复
GNU的inetutils包里有ping:
ftp://ftp.gnu.org/gnu/inetutils/
fierygnu 2003-11-11
  • 打赏
  • 举报
回复
可以用。是不是编译有些错误?改了就行了。
wyb_45 2003-11-11
  • 打赏
  • 举报
回复
我需要unix下的
在linux下我找到源码包了,
可是基本无法在unix下使用
HP-UX 还有sun的
《C语言课程设计案例精编》光盘内容及使用说明 1. 内容及使用方法 (1) 本光盘提供了书中案例的C语言源代码文件、可执行文件及所需的图形驱动文件。 (2) 读者可以使用Win-TC、Turbo C、文本编辑工具(例如Windows中的记事本)来打开和编辑C语言源代码文件。 (3) 文件夹图标后的数字编号为章号。例如Chap12,为第12章的内容。 (4) 本光盘中,除第1章和第2章外,每章均含有案例的源代码及相关文件。 (5) 本光盘中除了第4篇中的3个程序(Ping、TCP、UDP)在Visual C++ 6.0中调试通过以外(因为TC和Win-TC中均没有需要的头文件),其余所有程序都在Win-TC和Turbo C 2.0中调试通过。 (6) 建议读者使用Win-TC来编辑和调试案例中的源代码,因为在Win-TC中可以充分利用Windows剪贴板和支持中文显示等特点, 故可大大提高学习效率。 2. 书中案例运行的配置要求 (1) 硬件配置 CPU:≥Pentium III 600 内存:≥128MB 硬盘剩余空间:≥512MB (2) 软件环境 安装Win-tc191或Turbo C 2.0和Visual C++ 6.0。 3. 特别提示 睁大你们的眼睛是代码是代码 !免的有些人渣开始骂人 考虑到网络编程篇中的3个程序(Ping、TCP、UDP)在Visual C++ 6.0中调试通过的特点,下面介绍一下调试这3个程序的注意事项。 (1) Ping程序 Ping程序运行方式为:“开始”->“运行”->“cmd”->“程序名+参数”。这里的程序名为ping\Debug下的ping.exe,参数设置可参见教材第9章。 (2) TCP程序 TCP程序的运行需要有服务器端和客户端,即需要两台主机。 主机1(服务器):“开始”->“运行”->“cmd”->“服务器端程序名+参数”。这里的服务器端程序名为tcp\Debug下的tcp.exe,参数设置可参见教材第10章。 主机2(客户端):“开始”->“运行”->“cmd”->“客户端程序名+参数”。这里的客户端程序名为tcp_client\Debug下的tcp_client.exe,参数设置可参见教材第10章。 (3) UDP程序设计: UDP程序的运行需要两台主机,两台主机上运行相同的程序,即udp\Debug下的udp.exe。 主机1:“开始”->“运行”->“cmd”->“程序名+参数”。这里的程序名为udp\Debug下的udp.exe,参数设置可参见程序。 主机2:“开始”->“运行”->“cmd”->“程序名+参数”。这里的程序名为udp\Debug下的udp.exe,参数设置可参见教材第11章。 4. 注意事项 (1) 建议读者将光盘中的所有文件备份到硬盘上运行。 (2) 在练习C语言时,在Turbo C 2.0中为了避免每次都修改Options的Directories选项的麻烦,建议修改好Options的Directories后,单击Options最下面的Save Options选项,然后单击ok,重启Turbo C即可。 5. 特别声明 本光盘中的文件仅可作为学习和欣赏之用,未经许可不得用于任何商业或其他用途。 6. 技术支持 关于本书的相关技术支持和软件问题请发电子邮件到bookforc@163.com寻求帮助。 7. 作者信息 作者(技术支持及相关问题探讨) 姓 名:刘勇、姜灵芝 电子邮件:bookforc@163.com

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧