2013-02-07 44 views
1

我试图实现一个Traceroute程序,但我遇到了两个问题,一个是TTL和RTT打印出错;尽管它们在作为一个ping程序实施时是正确的。最后,我的主要问题是,当我递增TTL时,它将增加2而不是1.为什么我的TTL值每次增加2? (C Socket编程)

我只包括我认为必要的代码,谢谢。

感谢提前:)

void 
respond (int signum) { 
    struct sockaddr_storage peer_addr; 
    socklen_t    peer_addrlen; 
    struct sockaddr_in  addr; 
    struct sockaddr_in  dstaddr; 
    struct iphdr *  ip; 
    struct icmphdr *  icmp; 
    struct timeval *  sent; 
    int skt; 
    int sequence; 
    long int length; 
    fd_set rdfds; 
    int ready; 
    int rtt; 
    char buff [BUF_SIZE]; 

    /* Create and check Socket Number */ 
    skt = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); 


    int ttl = 0; 
    setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0; 


    /* START SEND LOOP*/ 
    int i; 
    for (i = 0; i < 4; i++){ 
     ttl+=1; 
     setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 


    /* Check Socket */ 
    if (skt < 0) { 
     perror ("socket()"); 
     exit (1); 
    } 

    /* Set IP Addresses */ 
    addr.sin_family  = AF_INET; 
    addr.sin_port  = 0; 
    addr.sin_addr.s_addr = INADDR_ANY; 


    /* Check Socket Bind */ 
    if (bind (skt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) { 
     perror ("Can't bind socket"); 
     exit (1); 
    } 

    /* IP Buffer */ 
    ip = (struct iphdr *)buff; 
    peer_addrlen = (socklen_t) sizeof (struct sockaddr_storage); 
    memset (&dstaddr, 0, sizeof(struct sockaddr_in)); 
    dstaddr.sin_addr.s_addr = inet_addr(HOSTADDR); 
    dstaddr.sin_family = AF_INET; 

    ip->ttl=(ttl++); 

    /* ICMP Buffer */ 
    memset (buff, 0, sizeof(buff)); 
    icmp = (struct icmphdr *) buff; 
    icmp->type = ECHO_REQ; 
    icmp->id  = htons(getpid() & 0xffff); 
    icmp->seqNum = htons(sequence++); 


    /* Check Send Time */ 
    if (gettimeofday ((struct timeval *)icmp->data, NULL)) { 
     perror ("Can't establish send time"); 
     exit (1); 
    } 

    /*Calculating packet size*/ 
    length = sizeof(struct icmphdr) + sizeof(struct timeval); 
    icmp->checksum = ~(sum (0, buff, length)); 



    /* Packet too small, ERROR 
    SEND Request    */ 
    if (sendto (skt, buff, length, 0, 
     (struct sockaddr *) &dstaddr, sizeof(struct sockaddr_in)) <= 0) { 
     perror ("sendto()"); 
     exit (1); 
} 

    /* Define File Descriptor */ 
    timeout.tv_sec = 2; 
    timeout.tv_usec = 0; 
    FD_ZERO(&rdfds); 
    FD_SET (skt, &rdfds); 

    /* Select Data from File Descriptor */ 
    ready = select (skt + 1, &rdfds, NULL, NULL, &timeout); 
    if (ready < 0) { 
     perror ("Select()"); 
     exit (1); 
    } 

/* Recieve Reply */ 
memset (buff, 0, sizeof(buff)); 
    if (recvfrom (skt, buff, sizeof(buff), 0, 
     (struct sockaddr *) &peer_addr, &peer_addrlen) <= 0) exit (1); 



    /* Check Time Stamp */ 
    if (gettimeofday (&end, NULL)) { // Timestamp reception 
     perror ("Can't establish time of receipt"); 
     exit (1); 
    } 


    /* Check IP Protocol */ 
    if (ip->version != 4 || 
     sum (0, buff, sizeof(struct iphdr)) != 0xffff || 
     ip->protocol != ICMP) 
     exit(1); 


    /* Get IP Payload legth and ICMP Address*/ 
    length = ntohs(ip->length) - ip->hdrlen * 4;  // Length of IP payload 
    icmp = (struct icmphdr *)((uint32_t *)ip + ip->hdrlen); // Find ICMP hdr 


    /* Check ICMP response type*/ 
    if (icmp->type == 11){ 
     printf("Type 11: ICMP...."); 
     } 

    /* if (icmp->type != ECHO_REPL || sum (0, icmp, length) != 0xffff) { 
     fprintf (stderr, "Received %s\n", messages[icmp->type]); 
     //exit (1); 
    } */ 

    /* Find the difference between sent and end times in 10s of ms */ 
    sent = (struct timeval *)icmp->data; 
    if ((rtt = (end.tv_usec - sent->tv_usec)/100) < 0) 
     rtt += 10000; // We've cycled to a new second 
    rtt += (end.tv_sec - sent->tv_sec) * 10000; // Add any seconds 

    /* PRINT ICMP REPLY*/ 
    printf ("%ld bytes from %s: icmp_req=%d ttl=%d time=%0.1f ms\n", 
     length, 
     iptos(ntohl(ip->srcip)), 
     ntohs(icmp->seqNum), 
     /*Set initial TTL */ 
     ip->ttl, 
     ((float)rtt)/10); 


    } /*END SEND LOOP 

    /* Invalid Signal returned */ 
    if (signum == SIGINT) { 
     printf ("\nGoodbye!\n"); 
     exit(0); 
    } 

    /* 3 Second Probe */ 
    alarm (3); 
} 

回答

1
/* START SEND LOOP*/ 
int i; 
for (i = 0; i < 4; i++){ 
    ttl+=1; 
    setsockopt(skt, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 

在这里,您通过每次一个循环迭代增加ttl

ip->ttl=(ttl++); 

然后在这里再增加一个。所以如果是1,在这一行之后将是2。然而,一旦迭代循环,你再次增加它,所以它现在将是3.这就是为什么你的ttl在每次迭代时增加两个。

线ip->ttl=(ttl++)表示:请采取的ttl值,并将其复制到ip->ttl,那么这样做后,加一ttl

+0

首先你赢了:P –

1

你正在循环中递增ttl两次。一旦当for循环开始了:

ttl+=1; 

话又说回来,同时将其分配到结构

ip->ttl=(ttl++); 

对于RTT,以及代码只是看起来我错了。

+0

谢谢,我完全错过了那个(之前它留在那里)! 当这个程序正在ping时,RTT正在工作,只是不再:/ –

+0

对于我来说关于rtt的看起来很奇怪的是,你正在减去一个应该大于(或等于)的数字,然后除以100总是会导致> = 0的值。然后,您会检查结果是否小于等于0并将10,000加到它(这又似乎是不正确的,因为您已经除以100,所以您不应该只添加在10?)然后在下一行你再次将其转换为useconds而不是十分之一。 – Eric