void print_report ()
{
  int stat;
  /* Removed to compile cleanly with -Wall */
  //double rtt, krtt;
  char str[NI_MAXHOST];
      
  stat = sock_cmp_addr (sarecv, salast, salen);

  /* if this reply comes from source different from the previous
     one, print the full host information */

  if (stat != 0) {
    stat = getnameinfo (sarecv, salen, str, sizeof(str), NULL, 0, 0);
    if (stat == 0) {
      printf (" %s (%s)", str, Sock_ntop_host (sarecv, salen));
    } else {
      printf (" %s", Sock_ntop_host (sarecv, salen));
    }
    memcpy (salast, sarecv, salen);
  }

  /* calculate and print the round trip time using user-level timestamps */

  sub_tv (recvtv, sendtv, difftv);
  /* Removed to compile cleanly with -Wall */
  //rtt = time_to_double (difftv);

  printf ("  %.3f", time_to_double (difftv));
}
/*
 * The function that compares two sockaddr structures.
 * \param addr1 first sockaddr struct
 * \param addr1_len length of first sockaddr struct
 * \param addr2 second sockaddr struct
 * \param addr1_len length of second sockaddr struct
 * \return 0 if the two structires are egual, otherwise an error reflecting the
 * first difference encountered.
 */
int sockaddr_cmp(struct sockaddr *addr1, socklen_t addr1_len, struct sockaddr *addr2, socklen_t addr2_len)
{
        if (addr1_len != addr2_len)
                return WSOCK_ERRSIZE;
        if (addr1->sa_family != addr1->sa_family)
                return WSOCK_ERRFAMILY;
        if (sock_cmp_addr(addr1, addr2 /*, addr1_len */ ))
                return WSOCK_ERRADDR;
        if (sock_cmp_port(addr1, addr2 /*, addr1_len */ ))
                return WSOCK_ERRPORT;

        return 0;
}
void
traceloop(void)
{
	int					seq, code, done;
	double				rtt;
	struct rec			*rec;
	struct timeval		tvrecv;

	recvfd = Socket(pr->sasend->sa_family, SOCK_RAW, pr->icmpproto);
	setuid(getuid());		/* don't need special permissions any more */

	sendfd = Socket(pr->sasend->sa_family, SOCK_DGRAM, 0);

	pr->sabind->sa_family = pr->sasend->sa_family;
	sport = (getpid() & 0xffff) | 0x8000;	/* our source UDP port# */
	sock_set_port(pr->sabind, pr->salen, htons(sport));
	Bind(sendfd, pr->sabind, pr->salen);

	sig_alrm(SIGALRM);

	seq = 0;
	done = 0;
	for (ttl = 1; ttl <= max_ttl && done == 0; ttl++) {
		Setsockopt(sendfd, pr->ttllevel, pr->ttloptname, &ttl, sizeof(int));
		bzero(pr->salast, pr->salen);

		printf("%2d  ", ttl);
		fflush(stdout);

		for (probe = 0; probe < nprobes; probe++) {
			rec = (struct rec *) sendbuf;
			rec->rec_seq = ++seq;
			rec->rec_ttl = ttl;
			Gettimeofday(&rec->rec_tv, NULL);

			sock_set_port(pr->sasend, pr->salen, htons(dport + seq));
			Sendto(sendfd, sendbuf, datalen, 0, pr->sasend, pr->salen);

			if ( (code = (*pr->recv)(seq, &tvrecv)) == -3)
				printf(" *");		/* timeout, no reply */
			else {
				char	str[NI_MAXHOST];

				if (sock_cmp_addr(pr->sarecv, pr->salast, pr->salen) != 0) {
					if (getnameinfo(pr->sarecv, pr->salen, str, sizeof(str),
									NULL, 0, 0) == 0)
						printf(" %s (%s)", str,
								Sock_ntop_host(pr->sarecv, pr->salen));
					else
						printf(" %s",
								Sock_ntop_host(pr->sarecv, pr->salen));
					memcpy(pr->salast, pr->sarecv, pr->salen);
				}
				tv_sub(&tvrecv, &rec->rec_tv);
				rtt = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0;
				printf("  %.3f ms", rtt);

				if (code == -1)		/* port unreachable; at destination */
					done++;
				else if (code >= 0)
					printf(" (ICMP %s)", (*pr->icmpcode)(code));
			}
			fflush(stdout);
		}
		printf("\n");
	}
}
Exemple #4
0
void traceloop(void)
{ 
  int seq,code,done;
  double rtt;
  struct rec *rec;
  struct timeval tvrecv;

 
  if (( recvfd = socket(pr->sasend->sa_family,SOCK_RAW,pr->icmpproto)) < 0)
    err_sys("socket error");

  if (( sendfd = socket(pr->sasend->sa_family,SOCK_DGRAM,0)) < 0)
    err_sys("socket error");

  pr->sabind->sa_family = pr->sasend->sa_family;
  /* source port */
  sport = (getpid() & 0xffff) | 0x8000;
  sock_set_port(pr->sabind,pr->salen,htons(sport));
  if (bind(sendfd,pr->sabind,pr->salen) < 0)
    err_sys("bind error");
  
  seq = 0;
  done = 0;

  for (ttl = 1; ttl <= max_ttl && done == 0; ttl++)
  {
    if (setsockopt(sendfd,pr->ttllevel,pr->ttloptname, &ttl,sizeof(int)) < 0)
      err_sys("socket error");
    bzero(pr->salast,pr->salen);
  
    printf("%2d  ", ttl);
    fflush(stdout);

    for (probe = 0; probe < nprobes; probe++)
    {
      rec = (struct rec *) sendbuf;
      rec->rec_seq = ++seq;
      rec->rec_ttl = ttl;
      gettimeofday(&rec->rec_tv,NULL);

      sock_set_port(pr->sasend,pr->salen,htons(dport + seq));
      sendto(sendfd,sendbuf,datalen,0,pr->sasend,pr->salen);
  
      if ( (code = (*pr->recv)(seq, &tvrecv))  == -3 )
        printf(" *");
      else 
      {
        char str[NI_MAXHOST];

        if (sock_cmp_addr(pr->sarecv,pr->salast,pr->salen) != 0)
        {
          if(getnameinfo(pr->sarecv,pr->salen,str,sizeof(str),NULL,0,0) == 0)
            printf(" %s (%s)", str, hostntop(pr->sarecv,pr->salen));
          else
            printf(" %s", hostntop(pr->sarecv,pr->salen));
          memcpy(pr->salast,pr->sarecv,pr->salen);
        }
        tv_sub(&tvrecv,&rec->rec_tv);
        rtt = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0;
        printf(" %.3f ms",rtt);

        /* port unreachable at destination */
        if (code == -1)
          done++;
        else if (code >= 0)
          printf(" (ICMP %s)",(*pr->icmpcode)(code));
      }
      fflush(stdout);
    }
    printf("\n");
  }
}