📄 461.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center"> ● UNIX网络编程 (BM: clown) </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="321.htm">上一层</a>][<a href="462.htm">下一篇</a>]
<hr><p align="left"><small>/* <br>
* icmpquery.c - send and receive ICMP queries for address mask <br>
* and current time. <br>
* <br>
* Version 1.0 <br>
* <br>
* Copyright 1998, 1998 David G. Andersen <angio@pobox.com> <br>
* <danderse@cs.utah.edu> <br>
* http://www.angio.net/ <br>
* <br>
* Verified to work on: <br>
* FreeBSD (2.x, 3.x) <br>
* Linux 2.0.x <br>
* NetBSD 1.3 <br>
* <br>
* Should work on Solaris and other platforms with BSD-ish stacks. <br>
* <br>
* If you compile it somewhere else, or it doesn't work somewhere, <br>
* please let me know. <br>
* <br>
* Compilation: gcc icmpquery.c -o icmpquery <br>
*/ <br>
<br>
<br>
<br>
/* Some portions of this code are taken from FreeBSD's ping source. <br>
* Those portions are subject to the BSD copyright, which is appended <br>
* at the end of this file. <br>
*/ <br>
<br>
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ <br>
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ <br>
#define SET(bit) (A(bit) |= B(bit)) <br>
#define CLR(bit) (A(bit) &= (~B(bit))) <br>
#define TST(bit) (A(bit) & B(bit)) <br>
<br>
#include <time.h> <br>
#include <sys/time.h> <br>
#include <stdio.h> <br>
#include <sys/types.h> <br>
#include <sys/socket.h> <br>
#include <netdb.h> <br>
#include <netinet/in.h> <br>
#include <netinet/in_systm.h> <br>
#include <netinet/ip.h> <br>
#include <netinet/ip_icmp.h> <br>
#include <errno.h> <br>
#include <string.h> <br>
#include <signal.h> <br>
<br>
/* <br>
* We perform lookups on the hosts, and then store them in a chain <br>
* here. <br>
* <br>
* Yes, it's linear. No, I don't care. <br>
*/ <br>
<br>
struct hostdesc { <br>
char *hostname; <br>
struct in_addr hostaddr; <br>
struct hostdesc *next; <br>
}; <br>
<br>
struct hostdesc *hostnames; <br>
struct hostdesc *hosttail; <br>
<br>
void resolv_from(char *hostfrom, struct in_addr *fromaddr) <br>
{ <br>
{ <br>
struct hostent *hp; <br>
if (hostfrom == NULL) { <br>
fromaddr->s_addr = 0; <br>
return; <br>
} <br>
<br>
if ((hp = gethostbyname(hostfrom)) == NULL) { <br>
if ((fromaddr->s_addr = inet_addr(hostfrom)) == -1) { <br>
fprintf(stderr, "could not resolve from address\n"); <br>
exit(0); <br>
} <br>
} else { <br>
bcopy(hp->h_addr_list[0], &fromaddr->s_addr, hp->h_length); <br>
} <br>
} <br>
<br>
/* <br>
* Set up the list of hosts. Return the count. <br>
*/ <br>
<br>
int makehosts(char **hostlist) <br>
{ <br>
{ <br>
int i; <br>
struct hostent *hp; <br>
struct in_addr tmpaddr; <br>
int hostcount = 0; <br>
<br>
for (i = 0; hostlist[i]; i++) { <br>
#ifdef DEBUG <br>
printf("Resolving %s\n", hostlist[i]); <br>
#endif <br>
if ((hp = gethostbyname(hostlist[i])) == NULL) { <br>
if (tmpaddr.s_addr = inet_addr(hostlist[i])) { <br>
/* Could not resolve it. Skip it. */ <br>
fprintf(stderr, "%s: unknown host\n", <br>
hostlist[i]); <br>
continue; <br>
} <br>
} else { <br>
bcopy(hp->h_addr_list[0], <br>
&tmpaddr.s_addr, hp->h_length); <br>
} <br>
<br>
/* The host has been resolved. Put it in the chain */ <br>
/* We want to stick it on the end. */ <br>
if (hostnames == NULL) { <br>
hostnames = (struct hostdesc *) <br>
malloc(sizeof(*hostnames)); <br>
if (hostnames == NULL) { <br>
perror("hostnames malloc failed"); <br>
exit(-1); <br>
} <br>
hosttail = hostnames; <br>
} else { <br>
hosttail->next = (struct hostdesc *) <br>
malloc(sizeof(*hostnames)); <br>
if (hosttail->next == NULL) { <br>
perror("hosttail->next malloc failed"); <br>
exit(-1); <br>
} <br>
hosttail = hosttail->next; <br>
} <br>
hosttail->hostname = strdup(hostlist[i]); <br>
if (hosttail->hostname == NULL) { <br>
perror("strdup failed"); <br>
exit(-1); <br>
exit(-1); <br>
} <br>
hosttail->hostaddr = tmpaddr; <br>
hosttail->next = NULL; <br>
hostcount++; <br>
} <br>
return hostcount; <br>
} <br>
<br>
void usage(char *prog) <br>
{ <br>
fprintf(stderr, <br>
"%s <-querytype> [-f fromhost] [-d delay] [-T time] destination list\n" <br>
" where <querytype> is one of:\n" <br>
" -t : icmp timestamp request\n" <br>
" -m : icmp address mask request\n" <br>
" The delay is in microseconds to sleep between packets.\n" <br>
" The destination list is a list of hostnames or addresses\n" <br>
" -T specifies the number of seconds to wait for a host to\n" <br>
" respond. The default is 5.\n" <br>
" If you're on a modem, you may wish to use a larger -d and -T\n" <br>
, prog); <br>
} <br>
} <br>
<br>
/* <br>
* Set up a packet. Returns the length of the ICMP portion. <br>
*/ <br>
<br>
int initpacket(char *buf, int querytype, struct in_addr fromaddr) <br>
{ <br>
struct ip *ip = (struct ip *)buf; <br>
struct icmp *icmp = (struct icmp *)(ip + 1); <br>
int on = 1; <br>
<br>
/* things we customize */ <br>
int icmplen = 0; <br>
int icmptype = 0; <br>
<br>
int offset; <br>
int typevar; <br>
int subtype; <br>
<br>
ip->ip_src = fromaddr; /* if 0, have kernel fill in */ <br>
ip->ip_v = 4; /* Always use ipv4 for now */ <br>
ip->ip_hl = sizeof *ip >> 2; <br>
ip->ip_tos = 0; <br>
ip->ip_id = htons(4321); <br>
ip->ip_ttl = 255; <br>
ip->ip_p = 1; <br>
ip->ip_sum = 0; /* kernel fills in */ <br>
<br>
icmp->icmp_seq = 1; <br>
icmp->icmp_cksum = 0; <br>
icmp->icmp_type = querytype; <br>
icmp->icmp_code = 0; <br>
<br>
switch(querytype) { <br>
case ICMP_TSTAMP: <br>
gettimeofday( (struct timeval *)(icmp+8), NULL); <br>
bzero( icmp+12, 8); <br>
icmplen = 20; <br>
break; <br>
case ICMP_MASKREQ: <br>
*((char *)(icmp+8)) = 255; <br>
icmplen = 12; <br>
break; <br>
default: <br>
default: <br>
fprintf(stderr, "eek: unknown query type\n"); <br>
exit(0); <br>
} <br>
ip->ip_len = sizeof(struct ip) + icmplen; <br>
return icmplen; <br>
} <br>
<br>
void sendpings(int s, int querytype, struct hostdesc *head, int delay, <br>
struct in_addr fromaddr) <br>
<br>
{ <br>
char buf[1500]; <br>
struct ip *ip = (struct ip *)buf; <br>
struct icmp *icmp = (struct icmp *)(ip + 1); <br>
struct sockaddr_in dst; <br>
int icmplen; <br>
<br>
bzero(buf, 1500); <br>
icmplen = initpacket(buf, querytype, fromaddr); <br>
dst.sin_family = AF_INET; <br>
<br>
while (head != NULL) { <br>
#ifdef DEBUG <br>
printf("pinging %s\n", head->hostname); <br>
#endif <br>
ip->ip_dst.s_addr = head->hostaddr.s_addr; <br>
dst.sin_addr = head->hostaddr; <br>
icmp->icmp_cksum = 0; <br>
icmp->icmp_cksum = in_cksum(icmp, icmplen); <br>
if (sendto(s, buf, ip->ip_len, 0, <br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -