1 #include	"ping.h"
2 
3 struct proto	proto_v4 = { proc_v4, send_v4, NULL, NULL, 0, IPPROTO_ICMP };
4 
5 #ifdef	IPV6
6 struct proto	proto_v6 = { proc_v6, send_v6, NULL, NULL, 0, IPPROTO_ICMPV6 };
7 #endif
8 
9 int	datalen = 56;		/* data that goes with ICMP echo request */
10 
11 int
main(int argc,char ** argv)12 main(int argc, char **argv)
13 {
14 	int				c;
15 	struct addrinfo	*ai;
16 
17 	opterr = 0;		/* don't want getopt() writing to stderr */
18 	while ( (c = getopt(argc, argv, "v")) != -1) {
19 		switch (c) {
20 		case 'v':
21 			verbose++;
22 			break;
23 
24 		case '?':
25 			err_quit("unrecognized option: %c", c);
26 		}
27 	}
28 
29 	if (optind != argc-1)
30 		err_quit("usage: ping [ -v ] <hostname>");
31 	host = argv[optind];
32 
33 	pid = getpid();
34 	Signal(SIGALRM, sig_alrm);
35 
36 	ai = Host_serv(host, NULL, 0, 0);
37 
38 	printf("PING %s (%s): %d data bytes\n", ai->ai_canonname,
39 		   Sock_ntop_host(ai->ai_addr, ai->ai_addrlen), datalen);
40 
41 		/* 4initialize according to protocol */
42 	if (ai->ai_family == AF_INET) {
43 		pr = &proto_v4;
44 #ifdef	IPV6
45 	} else if (ai->ai_family == AF_INET6) {
46 		pr = &proto_v6;
47 		if (IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)
48 								 ai->ai_addr)->sin6_addr)))
49 			err_quit("cannot ping IPv4-mapped IPv6 address");
50 #endif
51 	} else
52 		err_quit("unknown address family %d", ai->ai_family);
53 
54 	pr->sasend = ai->ai_addr;
55 	pr->sarecv = Calloc(1, ai->ai_addrlen);
56 	pr->salen = ai->ai_addrlen;
57 
58 	readloop();
59 
60 	exit(0);
61 }
62