main.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "trace.h"
  2. struct proto proto_v4 = { icmpcode_v4, recv_v4, NULL, NULL, NULL, NULL, 0,
  3. IPPROTO_ICMP, IPPROTO_IP, IP_TTL };
  4. #ifdef IPV6
  5. struct proto proto_v6 = { icmpcode_v6, recv_v6, NULL, NULL, NULL, NULL, 0,
  6. IPPROTO_ICMPV6, IPPROTO_IPV6, IPV6_UNICAST_HOPS };
  7. #endif
  8. int datalen = sizeof(struct rec); /* defaults */
  9. int max_ttl = 30;
  10. int nprobes = 3;
  11. u_short dport = 32768 + 666;
  12. int
  13. main(int argc, char **argv)
  14. {
  15. int c;
  16. struct addrinfo *ai;
  17. char *h;
  18. opterr = 0; /* don't want getopt() writing to stderr */
  19. while ( (c = getopt(argc, argv, "m:v")) != -1) {
  20. switch (c) {
  21. case 'm':
  22. if ( (max_ttl = atoi(optarg)) <= 1)
  23. err_quit("invalid -m value");
  24. break;
  25. case 'v':
  26. verbose++;
  27. break;
  28. case '?':
  29. err_quit("unrecognized option: %c", c);
  30. }
  31. }
  32. if (optind != argc-1)
  33. err_quit("usage: traceroute [ -m <maxttl> -v ] <hostname>");
  34. host = argv[optind];
  35. pid = getpid();
  36. Signal(SIGALRM, sig_alrm);
  37. ai = Host_serv(host, NULL, 0, 0);
  38. h = Sock_ntop_host(ai->ai_addr, ai->ai_addrlen);
  39. printf("traceroute to %s (%s): %d hops max, %d data bytes\n",
  40. ai->ai_canonname ? ai->ai_canonname : h,
  41. h, max_ttl, datalen);
  42. /* initialize according to protocol */
  43. if (ai->ai_family == AF_INET) {
  44. pr = &proto_v4;
  45. #ifdef IPV6
  46. } else if (ai->ai_family == AF_INET6) {
  47. pr = &proto_v6;
  48. if (IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr)))
  49. err_quit("cannot traceroute IPv4-mapped IPv6 address");
  50. #endif
  51. } else
  52. err_quit("unknown address family %d", ai->ai_family);
  53. pr->sasend = ai->ai_addr; /* contains destination address */
  54. pr->sarecv = Calloc(1, ai->ai_addrlen);
  55. pr->salast = Calloc(1, ai->ai_addrlen);
  56. pr->sabind = Calloc(1, ai->ai_addrlen);
  57. pr->salen = ai->ai_addrlen;
  58. traceloop();
  59. exit(0);
  60. }