main.c 1.4 KB

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