⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ping6.c

📁 IPv6环境中的ping实现
💻 C
📖 第 1 页 / 共 5 页
字号:
			    (mtuctl->ip6m_addr.sin6_scope_id &&			     dst.sin6_scope_id &&			     mtuctl->ip6m_addr.sin6_scope_id !=			     dst.sin6_scope_id)) {				if ((options & F_VERBOSE) != 0) {					printf("path MTU for %s is notified. "					       "(ignored)\n",					   pr_addr((struct sockaddr *)&mtuctl->ip6m_addr,					   sizeof(mtuctl->ip6m_addr)));				}				return(0);			}			/*			 * Ignore an invalid MTU. XXX: can we just believe			 * the kernel check?			 */			if (mtuctl->ip6m_mtu < IPV6_MMTU)				return(0);			/* notification for our destination. return the MTU. */			return((int)mtuctl->ip6m_mtu);		}	}#endif	return(0);}/* * tvsub -- *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to * be >= in. */voidtvsub(out, in)	struct timeval *out, *in;{	if ((out->tv_usec -= in->tv_usec) < 0) {		--out->tv_sec;		out->tv_usec += 1000000;	}	out->tv_sec -= in->tv_sec;}/* * onint -- *	SIGINT handler. *//* ARGSUSED */voidonint(notused)	int notused;{	summary();	(void)signal(SIGINT, SIG_DFL);	(void)kill(getpid(), SIGINT);	/* NOTREACHED */	exit(1);}/* * summary -- *	Print out statistics. */voidsummary(){	(void)printf("\n--- %s ping6 statistics ---\n", hostname);	(void)printf("%ld packets transmitted, ", ntransmitted);	(void)printf("%ld packets received, ", nreceived);	if (nrepeats)		(void)printf("+%ld duplicates, ", nrepeats);	if (ntransmitted) {		if (nreceived > ntransmitted)			(void)printf("-- somebody's duplicating packets!");		else			(void)printf("%d%% packet loss",			    (int) (((ntransmitted - nreceived) * 100) /			    ntransmitted));	}	(void)putchar('\n');	if (nreceived && timing) {		/* Only display average to microseconds */		double num = nreceived + nrepeats;		double avg = tsum / num;#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)		double dev = sqrt(tsumsq / num - avg * avg);		(void)printf(		    "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",		    tmin, avg, tmax, dev);#else		(void)printf(		    "round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",		    tmin, avg, tmax);#endif		(void)fflush(stdout);	}	(void)fflush(stdout);}/*subject type*/static const char *niqcode[] = {	"IPv6 address",	"DNS label",	/*or empty*/	"IPv4 address",};/*result code*/static const char *nircode[] = {	"Success", "Refused", "Unknown",};/* * pr_icmph -- *	Print a descriptive string about an ICMP header. */voidpr_icmph(icp, end)	struct icmp6_hdr *icp;	u_char *end;{	char ntop_buf[INET6_ADDRSTRLEN];	struct nd_redirect *red;	struct icmp6_nodeinfo *ni;	char dnsname[MAXDNAME + 1];	const u_char *cp;	size_t l;	switch (icp->icmp6_type) {	case ICMP6_DST_UNREACH:		switch (icp->icmp6_code) {		case ICMP6_DST_UNREACH_NOROUTE:			(void)printf("No Route to Destination\n");			break;		case ICMP6_DST_UNREACH_ADMIN:			(void)printf("Destination Administratively "			    "Unreachable\n");			break;		case ICMP6_DST_UNREACH_BEYONDSCOPE:			(void)printf("Destination Unreachable Beyond Scope\n");			break;		case ICMP6_DST_UNREACH_ADDR:			(void)printf("Destination Host Unreachable\n");			break;		case ICMP6_DST_UNREACH_NOPORT:			(void)printf("Destination Port Unreachable\n");			break;		default:			(void)printf("Destination Unreachable, Bad Code: %d\n",			    icp->icmp6_code);			break;		}		/* Print returned IP header information */		pr_retip((struct ip6_hdr *)(icp + 1), end);		break;	case ICMP6_PACKET_TOO_BIG:		(void)printf("Packet too big mtu = %d\n",		    (int)ntohl(icp->icmp6_mtu));		pr_retip((struct ip6_hdr *)(icp + 1), end);		break;	case ICMP6_TIME_EXCEEDED:		switch (icp->icmp6_code) {		case ICMP6_TIME_EXCEED_TRANSIT:			(void)printf("Time to live exceeded\n");			break;		case ICMP6_TIME_EXCEED_REASSEMBLY:			(void)printf("Frag reassembly time exceeded\n");			break;		default:			(void)printf("Time exceeded, Bad Code: %d\n",			    icp->icmp6_code);			break;		}		pr_retip((struct ip6_hdr *)(icp + 1), end);		break;	case ICMP6_PARAM_PROB:		(void)printf("Parameter problem: ");		switch (icp->icmp6_code) {		case ICMP6_PARAMPROB_HEADER:			(void)printf("Erroneous Header ");			break;		case ICMP6_PARAMPROB_NEXTHEADER:			(void)printf("Unknown Nextheader ");			break;		case ICMP6_PARAMPROB_OPTION:			(void)printf("Unrecognized Option ");			break;		default:			(void)printf("Bad code(%d) ", icp->icmp6_code);			break;		}		(void)printf("pointer = 0x%02x\n",		    (u_int32_t)ntohl(icp->icmp6_pptr));		pr_retip((struct ip6_hdr *)(icp + 1), end);		break;	case ICMP6_ECHO_REQUEST:		(void)printf("Echo Request");		/* XXX ID + Seq + Data */		break;	case ICMP6_ECHO_REPLY:		(void)printf("Echo Reply");		/* XXX ID + Seq + Data */		break;	case ICMP6_MEMBERSHIP_QUERY:		(void)printf("Listener Query");		break;	case ICMP6_MEMBERSHIP_REPORT:		(void)printf("Listener Report");		break;	case ICMP6_MEMBERSHIP_REDUCTION:		(void)printf("Listener Done");		break;	case ND_ROUTER_SOLICIT:		(void)printf("Router Solicitation");		break;	case ND_ROUTER_ADVERT:		(void)printf("Router Advertisement");		break;	case ND_NEIGHBOR_SOLICIT:		(void)printf("Neighbor Solicitation");		break;	case ND_NEIGHBOR_ADVERT:		(void)printf("Neighbor Advertisement");		break;	case ND_REDIRECT:		red = (struct nd_redirect *)icp;		(void)printf("Redirect\n");		if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf,		    sizeof(ntop_buf)))			strncpy(ntop_buf, "?", sizeof(ntop_buf));		(void)printf("Destination: %s", ntop_buf);		if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf,		    sizeof(ntop_buf)))			strncpy(ntop_buf, "?", sizeof(ntop_buf));		(void)printf(" New Target: %s", ntop_buf);		break;	case ICMP6_NI_QUERY:		(void)printf("Node Information Query");		/* XXX ID + Seq + Data */		ni = (struct icmp6_nodeinfo *)icp;		l = end - (u_char *)(ni + 1);		printf(", ");		switch (ntohs(ni->ni_qtype)) {		case NI_QTYPE_NOOP:			(void)printf("NOOP");			break;		case NI_QTYPE_SUPTYPES:			(void)printf("Supported qtypes");			break;		case NI_QTYPE_FQDN:			(void)printf("DNS name");			break;		case NI_QTYPE_NODEADDR:			(void)printf("nodeaddr");			break;		case NI_QTYPE_IPV4ADDR:			(void)printf("IPv4 nodeaddr");			break;		default:			(void)printf("unknown qtype");			break;		}		if (options & F_VERBOSE) {			switch (ni->ni_code) {			case ICMP6_NI_SUBJ_IPV6:				if (l == sizeof(struct in6_addr) &&				    inet_ntop(AF_INET6, ni + 1, ntop_buf,				    sizeof(ntop_buf)) != NULL) {					(void)printf(", subject=%s(%s)",					    niqcode[ni->ni_code], ntop_buf);				} else {#if 1					/* backward compat to -W */					(void)printf(", oldfqdn");#else					(void)printf(", invalid");#endif				}				break;			case ICMP6_NI_SUBJ_FQDN:				if (end == (u_char *)(ni + 1)) {					(void)printf(", no subject");					break;				}				printf(", subject=%s", niqcode[ni->ni_code]);				cp = (const u_char *)(ni + 1);				if (dnsdecode(&cp, end, NULL, dnsname,				    sizeof(dnsname)) != NULL)					printf("(%s)", dnsname);				else					printf("(invalid)");				break;			case ICMP6_NI_SUBJ_IPV4:				if (l == sizeof(struct in_addr) &&				    inet_ntop(AF_INET, ni + 1, ntop_buf,				    sizeof(ntop_buf)) != NULL) {					(void)printf(", subject=%s(%s)",					    niqcode[ni->ni_code], ntop_buf);				} else					(void)printf(", invalid");				break;			default:				(void)printf(", invalid");				break;			}		}		break;	case ICMP6_NI_REPLY:		(void)printf("Node Information Reply");		/* XXX ID + Seq + Data */		ni = (struct icmp6_nodeinfo *)icp;		printf(", ");		switch (ntohs(ni->ni_qtype)) {		case NI_QTYPE_NOOP:			(void)printf("NOOP");			break;		case NI_QTYPE_SUPTYPES:			(void)printf("Supported qtypes");			break;		case NI_QTYPE_FQDN:			(void)printf("DNS name");			break;		case NI_QTYPE_NODEADDR:			(void)printf("nodeaddr");			break;		case NI_QTYPE_IPV4ADDR:			(void)printf("IPv4 nodeaddr");			break;		default:			(void)printf("unknown qtype");			break;		}		if (options & F_VERBOSE) {			if (ni->ni_code > sizeof(nircode) / sizeof(nircode[0]))				printf(", invalid");			else				printf(", %s", nircode[ni->ni_code]);		}		break;	default:		(void)printf("Bad ICMP type: %d", icp->icmp6_type);	}}/* * pr_iph -- *	Print an IP6 header. */voidpr_iph(ip6)	struct ip6_hdr *ip6;{	u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;	u_int8_t tc;	char ntop_buf[INET6_ADDRSTRLEN];	tc = *(&ip6->ip6_vfc + 1); /* XXX */	tc = (tc >> 4) & 0x0f;	tc |= (ip6->ip6_vfc << 4);	printf("Vr TC  Flow Plen Nxt Hlim\n");	printf(" %1x %02x %05x %04x  %02x   %02x\n",	    (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow),	    ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim);	if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))		strncpy(ntop_buf, "?", sizeof(ntop_buf));	printf("%s->", ntop_buf);	if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))		strncpy(ntop_buf, "?", sizeof(ntop_buf));	printf("%s\n", ntop_buf);}/* * pr_addr -- *	Return an ascii host address as a dotted quad and optionally with * a hostname. */const char *pr_addr(addr, addrlen)	struct sockaddr *addr;	int addrlen;{	static char buf[NI_MAXHOST];	int flag = 0;	if ((options & F_HOSTNAME) == 0)		flag |= NI_NUMERICHOST;	if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0)		return (buf);	else		return "?";}/* * pr_retip -- *	Dump some info on a returned (via ICMPv6) IPv6 packet. */voidpr_retip(ip6, end)	struct ip6_hdr *ip6;	u_char *end;{	u_char *cp = (u_char *)ip6, nh;	int hlen;	if (end - (u_char *)ip6 < sizeof(*ip6)) {		printf("IP6");		goto trunc;	}	pr_iph(ip6);	hlen = sizeof(*ip6);	nh = ip6->ip6_nxt;	cp += hlen;	while (end - cp >= 8) {		switch (nh) {		case IPPROTO_HOPOPTS:			printf("HBH ");			hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;			nh = ((struct ip6_hbh *)cp)->ip6h_nxt;			break;		case IPPROTO_DSTOPTS:			printf("DSTOPT ");			hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;			nh = ((struct ip6_dest *)cp)->ip6d_nxt;			break;		case IPPROTO_FRAGMENT:			printf("FRAG ");			hlen = sizeof(struct ip6_frag);			nh = ((struct ip6_frag *)cp)->ip6f_nxt;			break;		case IPPROTO_ROUTING:			printf("RTHDR ");			hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;			nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;			break;#ifdef IPSEC		case IPPROTO_AH:			printf("AH ");			hlen = (((struct ah *)cp)->ah_len+2) << 2;			nh = ((struct ah *)cp)->ah_nxt;			break;#endif		case IPPROTO_ICMPV6:			printf("ICMP6: type = %d, code = %d\n",			    *cp, *(cp + 1));			return;		case IPPROTO_ESP:			printf("ESP\n");			return;		case IPPROTO_TCP:			printf("TCP: from port %u, to port %u (decimal)\n",			    (*cp * 256 + *(cp + 1)),			    (*(cp + 2) * 256 + *(cp + 3)));			return;		case IPPROTO_UDP:			printf("UDP: from port %u, to port %u (decimal)\n",			    (*cp * 256 + *(cp + 1)),			    (*(cp + 2) * 256 + *(cp + 3)));			return;		default:			printf("Unknown Header(%d)\n", nh);			return;		}		if ((cp += hlen) >= end)			goto trunc;	}	if (end - cp < 8)		goto trunc;	putchar('\n');	return;  trunc:	printf("...\n");	return;}voidfill(bp, patp)	char *bp, *patp;{	int ii, jj, kk;	int pat[16];	char *cp;	for (cp = patp; *cp; cp++)		if (!isxdigit(*cp))			errx(1, "patterns must be specified as hex digits");	ii = sscanf(patp,	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],	    &pat[13], &pat[14], &pat[15]);/* xxx */	if (ii > 0)		for (kk = 0;		    kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii);		    kk += ii)			for (jj = 0; jj < ii; ++jj)				bp[jj + kk] = pat[jj];	if (!(options & F_QUIET)) {		(void)printf("PATTERN: 0x");		for (jj = 0; jj < ii; ++jj)			(void)printf("%02x", bp[jj] & 0xFF);		(void)printf("\n");	}}#ifdef IPSEC#ifdef IPSEC_POLICY_IPSECintsetpolicy(so, policy)	int so;	char *policy;{	char *buf;	if (policy == NULL)		re

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -