为什么我的ICMP Ping TTL参数在我的C代码中不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我的ICMP Ping TTL参数在我的C代码中不起作用相关的知识,希望对你有一定的参考价值。
我写了一个C程序来实现Traceroute,因为我需要有效地获取ip路径数据。我希望当我ping IP时,我可以得到src和dst IP之间的所有Ips。因此我使用TTL来获取中间IP,我想每次增加TTL时,我就可以逐渐获取中间IP。该代码段如下:
for(;ttl_val<20;ttl_val++)
setsockopt(ping_sockfd, SOL_IP, IP_TTL,&ttl_val, sizeof(ttl_val)
但是当我使用recvfrom获取中间Ips时,我发现它是不正确的,中间Ips总是在变化,并且与我用linux traceroute命令获得的Ips不同。
摘要代码如下:
struct sockaddr_in r_addr;
addr_len=sizeof(r_addr);
if ( recvfrom(ping_sockfd, &pckt, sizeof(pckt), 0,(struct sockaddr*)&r_addr, &addr_len) <= 0)
printf("\nPacket receive failed!\n");
else
if(!(pckt.hdr.type ==69 && pckt.hdr.code==0))
printf("Error..Packet \n");
else
//THE MIDDLE IPs were not correct!!!!!!
printf("=%s\n", inet_ntoa(r_addr.sin_addr));
例如,我的IP为1.1.1.1,我要对目标IP 5.5.5.5进行ping操作,中间IP为:2.2.2.2、3.3.3.3、4.4.4.4,即这些是IP中的路由IP路径。我希望当我设置TTL = 1时我可以得到2.2.2.2,当我设置TTL = 2时我可以得到3.3.3.3,以此类推。我不知道为什么上面的方法不起作用,当我设置TTL时,代码返回的是随机IP,而不是真正的中间IP,因为它们与linux traceroute命令获得的Ips不同。我的整个代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/ip_icmp.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
// Define the Packet Constants
// ping packet size
#define PING_PKT_S 64
// Automatic port number
#define PORT_NO 0
// Automatic port number
#define PING_SLEEP_RATE 1000000
// Gives the timeout delay for receiving packets
// in seconds
#define RECV_TIMEOUT 1
// Define the Ping Loop
int pingloop=1;
// ping packet structure
struct ping_pkt
struct icmphdr hdr;
char msg[PING_PKT_S-sizeof(struct icmphdr)];
;
// Calculating the Check Sum
unsigned short checksum(void *b, int len)
unsigned short *buf = b;
unsigned int sum=0;
unsigned short result;
for ( sum = 0; len > 1; len -= 2 )
sum += *buf++;
if ( len == 1 )
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
int atoint(char s[])
int i,n=0;
for(i=0;s[i]>='0' && s[i]<='9';i++)
n=10*n+(s[i]-'0');
return n;
// Interrupt handler
void intHandler(int dummy)
pingloop=0;
// Performs a DNS lookup
char *dns_lookup(char *addr_host, struct sockaddr_in *addr_con)
printf("\nResolving DNS..\n");
struct hostent *host_entity;
char *ip=(char*)malloc(NI_MAXHOST*sizeof(char));
int i;
if ((host_entity = gethostbyname(addr_host)) == NULL)
// No ip found for hostname
return NULL;
//filling up address structure
strcpy(ip, inet_ntoa(*(struct in_addr *)
host_entity->h_addr));
(*addr_con).sin_family = host_entity->h_addrtype;
(*addr_con).sin_port = htons (PORT_NO);
(*addr_con).sin_addr.s_addr = *(long*)host_entity->h_addr;
return ip;
// Resolves the reverse lookup of the hostname
char* reverse_dns_lookup(char *ip_addr)
struct sockaddr_in temp_addr;
socklen_t len;
char buf[NI_MAXHOST], *ret_buf;
temp_addr.sin_family = AF_INET;
temp_addr.sin_addr.s_addr = inet_addr(ip_addr);
len = sizeof(struct sockaddr_in);
if (getnameinfo((struct sockaddr *) &temp_addr, len, buf,
sizeof(buf), NULL, 0, NI_NAMEREQD))
printf("Could not resolve reverse lookup of hostname\n");
return NULL;
ret_buf = (char*)malloc((strlen(buf) +1)*sizeof(char) );
strcpy(ret_buf, buf);
return ret_buf;
// make a ping request
void send_ping(int ping_sockfd, struct sockaddr_in *ping_addr,
char *ping_dom, char *ping_ip, char *rev_host, int ttl_val)
int msg_count=0, i, addr_len, flag=1,msg_received_count=0;
struct ping_pkt pckt;
struct timespec time_start, time_end, tfs, tfe;
long double rtt_msec=0, total_msec=0;
struct timeval tv_out;
tv_out.tv_sec = RECV_TIMEOUT;
tv_out.tv_usec = 0;
clock_gettime(CLOCK_MONOTONIC, &tfs);
// set socket options at ip to TTL and value to 64,
// change to what you want by setting ttl_val
if (setsockopt(ping_sockfd, SOL_IP, IP_TTL,&ttl_val, sizeof(ttl_val)) != 0)
printf("\nSetting socket options to TTL failed!\n");
return;
else
printf("\nSocket set to TTL..\n");
// setting timeout of recv setting
// setsockopt(ping_sockfd, SOL_SOCKET, SO_RCVTIMEO,(const char*)&tv_out, sizeof tv_out);
// send icmp packet in an infinite loop
while(pingloop)
// flag is whether packet was sent or not
flag=1;
//filling packet
bzero(&pckt, sizeof(pckt));
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = getpid();
for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
pckt.msg[i] = i+'0';
pckt.msg[i] = 0;
pckt.hdr.un.echo.sequence = msg_count++;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
usleep(PING_SLEEP_RATE);
//send packet
clock_gettime(CLOCK_MONOTONIC, &time_start);
if ( sendto(ping_sockfd, &pckt, sizeof(pckt), 0,
(struct sockaddr*) ping_addr,
sizeof(*ping_addr)) <= 0)
printf("\nPacket Sending Failed!\n");
flag=0;
struct sockaddr_in r_addr;
//receive packet
addr_len=sizeof(r_addr);
// struct sockaddr_in from;
if ( recvfrom(ping_sockfd, &pckt, sizeof(pckt), 0,(struct sockaddr*)&r_addr, &addr_len) <= 0 && msg_count>1)
printf("\nPacket receive failed!\n");
else
clock_gettime(CLOCK_MONOTONIC, &time_end);
double timeElapsed = ((double)(time_end.tv_nsec - time_start.tv_nsec))/1000000.0;
rtt_msec = (time_end.tv_sec-time_start.tv_sec) * 1000.0+ timeElapsed;
// if packet was not sent, don't receive
if(flag)
if(!(pckt.hdr.type ==69 && pckt.hdr.code==0))
printf("Error..Packet received with ICMP type %d code %d\n", pckt.hdr.type, pckt.hdr.code);
else
printf("%d bytes from %s (h: %s) (%s) msg_seq=%d ttl=%d rtt = %Lf ms.\n", PING_PKT_S, ping_dom, rev_host,ping_ip, msg_count,ttl_val, rtt_msec);
printf("saddr = %d, %s: %d\n", r_addr.sin_family, inet_ntoa(r_addr.sin_addr), r_addr.sin_port);
msg_received_count++;
clock_gettime(CLOCK_MONOTONIC, &tfe);
double timeElapsed = ((double)(tfe.tv_nsec - tfs.tv_nsec))/1000000.0;
total_msec = (tfe.tv_sec-tfs.tv_sec)*1000.0 + timeElapsed;
printf("\n===%s ping statistics===\n", ping_ip);
printf("\n%d packets sent, %d packets received, %f percent packet loss. Total time: %Lf ms.\n\n", msg_count, msg_received_count, ((msg_count - msg_received_count)/msg_count) * 100.0, total_msec);
// Driver Code
int main(int argc, char *argv[])
int sockfd;
char *ip_addr, *reverse_hostname;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
char net_buf[NI_MAXHOST];
if(argc!=3)
printf("\nFormat %s <address> ttl \n", argv[0]);
return 0;
ip_addr = dns_lookup(argv[1], &addr_con);
if(ip_addr==NULL)
printf("\nDNS lookup failed! Could not resolve hostname!\n");
return 0;
reverse_hostname = reverse_dns_lookup(ip_addr);
printf("\nTrying to connect to '%s' IP: %s\n",argv[1], ip_addr);
printf("\nReverse Lookup domain: %s",reverse_hostname);
//socket()
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(sockfd<0)
printf("\nSocket file descriptor not received!!\n");
return 0;
else
printf("\nSocket file descriptor %d received\n", sockfd);
signal(SIGINT, intHandler);//catching interrupt
int ttl = atoint(argv[2]);
//send pings continuously
send_ping(sockfd, &addr_con, reverse_hostname,ip_addr, argv[1],ttl);
return 0;
您的TTL效果很好。用tcmpdump -vn ICMP
观察。
您的代码结果:
$ sudo ./test7 www.baidu.com 3
...
64 bytes from (null) (h: www.baidu.com) (104.193.88.123) msg_seq=25 ttl=3 rtt = 0.196290 ms.
saddr = 2, 162.151.78.85: 0
64 bytes from (null) (h: www.baidu.com) (104.193.88.123) msg_seq=26 ttl=3 rtt = 0.103414 ms.
saddr = 2, 162.151.78.85: 0
^C64 bytes from (null) (h: www.baidu.com) (104.193.88.123) msg_seq=27 ttl=3 rtt = 0.166377 ms.
saddr = 2, 162.151.78.85: 0
traceroute
:
$ traceroute -n www.baidu.com
traceroute to www.baidu.com (104.193.88.123), 30 hops max, 60 byte packets
1 *.*.*.* 15.001 ms 15.138 ms 15.121 ms
2 *.*.*.* 14.828 ms 14.873 ms 15.047 ms
3 162.151.78.85 14.469 ms 14.941 ms 14.590 ms
...
您缺少的是过滤收到您自己的ICMP ID和所需的ICMP类型。
没有过滤,您将收到其他不需要的数据(其他ICMP),并且您将需要再次丢弃(就像您对类型69和代码0所做的那样)和recvfrom
。 Linux过滤示例:
struct sock_fprog filter;
// set filter with your ID
setsockopt(ping_sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof filter);
struct icmp_filter filter;
// set filter.data with ICMP types (bitmask)
setsockopt(ping_sockfd, SOL_RAW, ICMP_FILTER, &filter, sizeof filter);
以上是关于为什么我的ICMP Ping TTL参数在我的C代码中不起作用的主要内容,如果未能解决你的问题,请参考以下文章