int main(int argc, char **argv) { int c; struct addrinfo *ai; char *h; opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "m:v")) != -1) { switch (c) { case 'm': if ( (max_ttl = atoi(optarg)) <= 1) err_quit("invalid -m value"); break; case 'v': verbose++; break; case '?': err_quit("unrecognized option: %c", c); } } if (optind != argc-1) err_quit("usage: traceroute [ -m <maxttl> -v ] <hostname>"); host = argv[optind]; pid = getpid(); Signal(SIGALRM, sig_alrm); ai = Host_serv(host, NULL, 0, 0); h = Sock_ntop_host(ai->ai_addr, ai->ai_addrlen); printf("traceroute to %s (%s): %d hops max, %d data bytes\n", ai->ai_canonname ? ai->ai_canonname : h, h, max_ttl, datalen); /* initialize according to protocol */ if (ai->ai_family == AF_INET) { pr = &proto_v4; #ifdef IPV6 } else if (ai->ai_family == AF_INET6) { pr = &proto_v6; if (IN6_IS_ADDR_V4MAPPED(&(((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr))) err_quit("cannot traceroute IPv4-mapped IPv6 address"); #endif } else err_quit("unknown address family %d", ai->ai_family); pr->sasend = ai->ai_addr; /* contains destination address */ pr->sarecv = Calloc(1, ai->ai_addrlen); pr->salast = Calloc(1, ai->ai_addrlen); pr->sabind = Calloc(1, ai->ai_addrlen); pr->salen = ai->ai_addrlen; traceloop(); exit(0); }
int main(int argc, char **argv) { struct addrinfo hints, *res; struct sigaction act; char *h; int errcode; if(argc != 2){ fprintf(stderr, "usage: traceroute <hostname>\n"); exit(1); } host = argv[1]; pid = getpid(); act.sa_handler = sig_alrm; sigemptyset(&act.sa_mask); act.sa_flags = SA_INTERRUPT; if(sigaction(SIGALRM, &act, NULL) != 0){ perror("sigaction error"); exit(42); } bzero(&hints, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_flags = AI_CANONNAME; if((errcode=getaddrinfo(argv[1], NULL, &hints, &res)) != 0){ fprintf(stderr,"getaddrinfo error: %s\n", gai_strerror(errcode)); exit(5); } if((h = inet_ntoa(((struct sockaddr_in *)res->ai_addr)->sin_addr)) == NULL){ fprintf(stderr, "inet_ntoa error\n"); exit(45); } printf("traceroute to %s (%s): %d hops max, %d data bytes\n", res->ai_canonname ? res->ai_canonname : argv[1], h, max_ttl, datalen); salen = res->ai_addrlen; sasend = *(res->ai_addr); traceloop(); exit(0); }
int main(int argc, char *argv[]) { int c; struct addrinfo *hints; opterr = 0; while ((c = getopt(argc,argv,"t:v")) != -1) { switch(c) { case 't': if (atoi(optarg) > 255) max_ttl=255; else max_ttl=atoi(optarg); #ifdef DEBUG printf("TTL is %d\n", max_ttl); #endif break; case 'v': #ifdef DEBUG printf("Verbose is selected\n"); #endif verbose = 1; break; case '?': err_quit("unrecognized option: %c\n",c); break; } } if (optind != argc-1) err_quit("Usage: traceroute <-t ttl> <-v> hostname\n"); hostname = argv[optind]; #ifdef DEBUG printf("Hostname is %s\n",hostname); #endif catchsignals(); hostname = "www.cern.ch"; bzero(hints,sizeof(struct addrinfo)); hints->ai_flags = AI_CANONNAME; hints->ai_family = 0; hints->ai_socktype = 0; if ( ( c = getaddrinfo(hostname, NULL, hints, &hints)) != 0) err_quit(gai_strerror(c)); printf("traceroute to %s (%s): %d hops max, %d data bytes\n",hints->ai_canonname,hostntop(hints->ai_addr,hints->ai_addrlen),max_ttl,datalen); if(hints->ai_family == AF_INET) pr = &proto_v4; else err_quit("unknown address family %d",hints->ai_family); pr->sasend = hints->ai_addr; /* contains destination address */ pr->sarecv = calloc(1,hints->ai_addrlen); pr->salast = calloc(1, hints->ai_addrlen); pr->sabind = calloc(1,hints->ai_addrlen); pr->salen = hints->ai_addrlen; traceloop(); exit(0); }