📄 461.htm
字号:
(struct sockaddr *)&dst, <br>
sizeof(dst)) < 0) { <br>
perror("sendto"); <br>
} <br>
if (delay) <br>
usleep(delay); <br>
/* Don't flood the pipeline..kind of arbitrary */ <br>
head = head->next; <br>
} <br>
} <br>
<br>
void myexit(int whatsig) <br>
{ <br>
exit(0); <br>
exit(0); <br>
} <br>
<br>
/* <br>
* Listen for 'hostcount' pings, print out the information, and <br>
* then exit. <br>
*/ <br>
<br>
void recvpings(int s, int querytype, struct hostdesc *head, int hostcount) <br>
{ <br>
char buf[1500]; <br>
struct ip *ip = (struct ip *)buf; <br>
struct icmp *icmp; <br>
int err = 0; <br>
long int fromlen = 0; <br>
int hlen; <br>
struct timeval *tp; <br>
struct timeval tv; <br>
int recvd = 0; <br>
char *hostto; <br>
char hostbuf[128]; /* We'll print the address here otherwise */ <br>
struct hostdesc *foundhost; <br>
<br>
<br>
gettimeofday(&tv, NULL); <br>
<br>
while (recvd < hostcount) { <br>
if ((err = recvfrom(s, buf, sizeof buf, 0, NULL, <br>
(int *)&fromlen)) < 0) <br>
{ <br>
perror("icmpquery: recvfrom"); <br>
} <br>
<br>
hlen = ip->ip_hl << 2; <br>
icmp = (struct icmp *)(buf + hlen); <br>
<br>
/* Find the host */ <br>
hostto = 0; <br>
for (foundhost = head; foundhost != NULL; <br>
foundhost = foundhost->next) { <br>
if (foundhost->hostaddr.s_addr == ip->ip_src.s_addr) { <br>
hostto = foundhost->hostname; <br>
break; <br>
} <br>
} <br>
<br>
<br>
if (!hostto) { <br>
sprintf(hostbuf, "unknown (%s)", <br>
inet_ntoa(ip->ip_src)); <br>
hostto = hostbuf; <br>
} <br>
<br>
/* For time */ <br>
switch(icmp->icmp_type) { <br>
case ICMP_TSTAMPREPLY: <br>
fromlen = ntohl(icmp->icmp_ttime); <br>
/* ms since midnight. yuch. */ <br>
tv.tv_usec = fromlen; <br>
tv.tv_usec = 0; <br>
tv.tv_sec -= tv.tv_sec%(24*60*60); <br>
tv.tv_sec += (fromlen/1000); <br>
tv.tv_usec = (fromlen%1000); <br>
printf("%-40.40s: %s", hostto, ctime(&(tv.tv_sec))); <br>
break; <br>
<br>
case ICMP_MASKREPLY: <br>
fromlen = ntohl(icmp->icmp_dun.id_mask); <br>
printf("%-40.40s: 0x%lX\n", hostto, fromlen); <br>
break; <br>
<br>
default: <br>
printf("Unknown ICMP message received (type %d)\n", <br>
icmp->icmp_type); <br>
} <br>
recvd++; <br>
} <br>
} <br>
<br>
int <br>
main(int argc, char **argv) <br>
{ <br>
int s; <br>
<br>
char *progname; <br>
extern char *optarg; /* getopt variable declarations */ <br>
char *hostfrom = NULL; <br>
extern int optind; <br>
extern int optopt; <br>
extern int opterr; <br>
char ch; /* Holds the getopt result */ <br>
int on = 1; <br>
int hostcount; <br>
int delay = 0; <br>
int querytype = ICMP_TSTAMP; <br>
struct in_addr fromaddr; <br>
int timeout = 5; /* Default to 5 seconds */ <br>
<br>
fromaddr.s_addr = 0; <br>
<br>
progname = argv[0]; <br>
<br>
while ((ch = getopt(argc, argv, "tmf:d:T:")) != EOF) <br>
switch(ch) <br>
{ <br>
case 'd': <br>
delay = (int) strtol(optarg, NULL, 10); <br>
break; <br>
case 't': /* timestamp request */ <br>
querytype = ICMP_TSTAMP; <br>
break; <br>
case 'm': /* address mask request */ <br>
querytype = ICMP_MASKREQ; <br>
break; <br>
case 'f': <br>
hostfrom = optarg; <br>
resolv_from(hostfrom, &fromaddr); <br>
break; <br>
case 'T': <br>
timeout = (int) strtol(optarg, NULL, 10); <br>
break; <br>
default: <br>
usage(progname); <br>
exit(-1); <br>
} <br>
argc -= optind; <br>
argv += optind; <br>
<br>
if (!argv[0] || !strlen(argv[0])) <br>
{ <br>
usage(progname); <br>
exit(-1); <br>
} <br>
<br>
hostcount = makehosts(argv); <br>
<br>
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { <br>
perror("socket"); <br>
exit(1); <br>
} <br>
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) { <br>
perror("IP_HDRINCL"); <br>
exit(1); <br>
} <br>
<br>
signal(SIGALRM, myexit); <br>
alarm(timeout); <br>
sendpings(s, querytype, hostnames, delay, fromaddr); <br>
recvpings(s, querytype, hostnames, hostcount); <br>
} <br>
<br>
/* <br>
* in_cksum -- <br>
* Checksum routine for Internet Protocol family headers (C Version) <br>
* From FreeBSD's ping.c <br>
*/ <br>
<br>
<br>
in_cksum(addr, len) <br>
u_short *addr; <br>
int len; <br>
{ <br>
register int nleft = len; <br>
register u_short *w = addr; <br>
register int sum = 0; <br>
u_short answer = 0; <br>
<br>
/* <br>
* Our algorithm is simple, using a 32 bit accumulator (sum), we add <br>
* sequential 16 bit words to it, and at the end, fold back all the <br>
* carry bits from the top 16 bits into the lower 16 bits. <br>
*/ <br>
while (nleft > 1) { <br>
sum += *w++; <br>
nleft -= 2; <br>
} <br>
<br>
/* mop up an odd byte, if necessary */ <br>
if (nleft == 1) { <br>
*(u_char *)(&answer) = *(u_char *)w ; <br>
sum += answer; <br>
} <br>
<br>
/* add back carry outs from top 16 bits to low 16 bits */ <br>
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ <br>
sum += (sum >> 16); /* add carry */ <br>
answer = ~sum; /* truncate to 16 bits */ <br>
return(answer); <br>
} <br>
<br>
<br>
/* <br>
* Copyright (c) 1989, 1993 <br>
* The Regents of the University of California. All rights reserved. <br>
* <br>
* This code is derived from software contributed to Berkeley by <br>
* Mike Muuss. <br>
* <br>
* Redistribution and use in source and binary forms, with or without <br>
* modification, are permitted provided that the following conditions <br>
* are met: <br>
* 1. Redistributions of source code must retain the above copyright <br>
* notice, this list of conditions and the following disclaimer. <br>
* 2. Redistributions in binary form must reproduce the above copyright <br>
* notice, this list of conditions and the following disclaimer in the <br>
* documentation and/or other materials provided with the distribution. <br>
* 3. All advertising materials mentioning features or use of this software <br>
* must display the following acknowledgement: <br>
* This product includes software developed by the University of <br>
* California, Berkeley and its contributors. <br>
* 4. Neither the name of the University nor the names of its contributors <br>
* may be used to endorse or promote products derived from this software <br>
* without specific prior written permission. <br>
* <br>
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND <br>
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE <br>
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE <br>
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE <br>
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL <br>
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS <br>
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) <br>
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT <br>
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY <br>
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF <br>
* SUCH DAMAGE. <br>
*/ <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="321.htm">上一层</a>][<a href="462.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -