traceroute 程序
时间:2010-11-30 来源:空灵静世
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
typedef struct mesg_hdr{
unsigned short magic;
unsigned char version;
unsigned char mesg_type;
unsigned char owner;
unsigned char net_flag;
unsigned short content_len;
unsigned int seq;
}MESG_HDR,*PMESG_HDR;
#define MEG_HDR_LEN sizeof(MESG_HDR) //12
#define SM_NAME_PING_IP "destIp"
int traceroute(char *to_ip,char *from_ip,int port,unsigned int * icmp_type,int ttl){
struct ip *ip;
struct icmp *icmp;
struct sockaddr_in addr;
struct sockaddr_in dst_addr;
struct timeval timeout;
socklen_t len = sizeof(struct sockaddr_in);
int hops = ttl;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(to_ip);
addr.sin_port = port;
char buf[128]={0};
int ret = 0;
int icmp_sock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
int udp_sock = socket(AF_INET,SOCK_DGRAM,0);
setsockopt(udp_sock,IPPROTO_IP,IP_TTL,&hops,sizeof(int));
setsockopt( icmp_sock, SOL_SOCKET, SO_RCVTIMEO,&timeout, sizeof(struct timeval));
ret = sendto(udp_sock,buf,6,0,(struct sockaddr *)&addr,len);
if (ret < 0 ){
close(icmp_sock);
close(udp_sock);
printf("sendto failed errno:[%d] errmsg:[%s]\n",errno,strerror(errno));
return -1;
}else {
}
ret = recvfrom(icmp_sock,buf,128,0,(struct sockaddr *)&dst_addr,&len);
if (ret < 0 ){
close(icmp_sock);
close(udp_sock);
printf("recvfrom failed errno:[%d] errmsg:[%s]\n",errno,strerror(errno));
return -1;
}
ip=(struct ip *)buf;
icmp = (struct icmp *)(buf+(ip->ip_hl << 2));
strcpy(from_ip,inet_ntoa(dst_addr.sin_addr));
*icmp_type= icmp->icmp_type;
close(icmp_sock);
close(udp_sock);
return 0;
}
int main(int argc,char *argv[]){
struct timeval begin_time;
struct timeval end_time;
int ret =0;
int index = 0;
int ttl = 0;
int port =65001;
int used_time = 0;
int traceflag = 0;
unsigned int icmp_type;
char dest_ip[20] = {0};
char from_ip[20] = {0};
char tmp_time[20]={0};
strcpy(dest_ip,argv[1]);
printf("traceroute to %s (%s), 30 hops max, 38 byte packets\n",dest_ip,dest_ip);
gettimeofday(&begin_time,(struct timezone *)NULL);
for (index = 0;(index < 60) && (ttl = (index/2 +1));index++){
gettimeofday(&end_time,(struct timezone *)NULL);
if ((used_time = (end_time.tv_sec - begin_time.tv_sec)*1000000+(end_time.tv_usec-begin_time.tv_usec)) >3000000 ){
break;
}
memset(&icmp_type,0,sizeof(icmp_type));
if (traceroute(dest_ip,from_ip,port++,&icmp_type,ttl) == -1){
break;
}
if ((icmp_type != ICMP_TIME_EXCEEDED) && (icmp_type != ICMP_DEST_UNREACH)){
printf("icmp_type:[%d] != ICMP_SOURCE_QUENCH\n",icmp_type);
index--;
continue;
}
tmp_time[0]= 0;
sprintf(tmp_time,"%d.%03d",used_time/1000,used_time%1000);
if ((index %2) != 0){
printf(" %s\n",tmp_time);
}else {
printf(" %d %s %s",ttl,from_ip,tmp_time);
}
if ((strcmp(dest_ip,from_ip) == 0) && (traceflag++ >=1)){
break;
}
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
typedef struct mesg_hdr{
unsigned short magic;
unsigned char version;
unsigned char mesg_type;
unsigned char owner;
unsigned char net_flag;
unsigned short content_len;
unsigned int seq;
}MESG_HDR,*PMESG_HDR;
#define MEG_HDR_LEN sizeof(MESG_HDR) //12
#define SM_NAME_PING_IP "destIp"
int traceroute(char *to_ip,char *from_ip,int port,unsigned int * icmp_type,int ttl){
struct ip *ip;
struct icmp *icmp;
struct sockaddr_in addr;
struct sockaddr_in dst_addr;
struct timeval timeout;
socklen_t len = sizeof(struct sockaddr_in);
int hops = ttl;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(to_ip);
addr.sin_port = port;
char buf[128]={0};
int ret = 0;
int icmp_sock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);
int udp_sock = socket(AF_INET,SOCK_DGRAM,0);
setsockopt(udp_sock,IPPROTO_IP,IP_TTL,&hops,sizeof(int));
setsockopt( icmp_sock, SOL_SOCKET, SO_RCVTIMEO,&timeout, sizeof(struct timeval));
ret = sendto(udp_sock,buf,6,0,(struct sockaddr *)&addr,len);
if (ret < 0 ){
close(icmp_sock);
close(udp_sock);
printf("sendto failed errno:[%d] errmsg:[%s]\n",errno,strerror(errno));
return -1;
}else {
}
ret = recvfrom(icmp_sock,buf,128,0,(struct sockaddr *)&dst_addr,&len);
if (ret < 0 ){
close(icmp_sock);
close(udp_sock);
printf("recvfrom failed errno:[%d] errmsg:[%s]\n",errno,strerror(errno));
return -1;
}
ip=(struct ip *)buf;
icmp = (struct icmp *)(buf+(ip->ip_hl << 2));
strcpy(from_ip,inet_ntoa(dst_addr.sin_addr));
*icmp_type= icmp->icmp_type;
close(icmp_sock);
close(udp_sock);
return 0;
}
int main(int argc,char *argv[]){
struct timeval begin_time;
struct timeval end_time;
int ret =0;
int index = 0;
int ttl = 0;
int port =65001;
int used_time = 0;
int traceflag = 0;
unsigned int icmp_type;
char dest_ip[20] = {0};
char from_ip[20] = {0};
char tmp_time[20]={0};
strcpy(dest_ip,argv[1]);
printf("traceroute to %s (%s), 30 hops max, 38 byte packets\n",dest_ip,dest_ip);
gettimeofday(&begin_time,(struct timezone *)NULL);
for (index = 0;(index < 60) && (ttl = (index/2 +1));index++){
gettimeofday(&end_time,(struct timezone *)NULL);
if ((used_time = (end_time.tv_sec - begin_time.tv_sec)*1000000+(end_time.tv_usec-begin_time.tv_usec)) >3000000 ){
break;
}
memset(&icmp_type,0,sizeof(icmp_type));
if (traceroute(dest_ip,from_ip,port++,&icmp_type,ttl) == -1){
break;
}
if ((icmp_type != ICMP_TIME_EXCEEDED) && (icmp_type != ICMP_DEST_UNREACH)){
printf("icmp_type:[%d] != ICMP_SOURCE_QUENCH\n",icmp_type);
index--;
continue;
}
tmp_time[0]= 0;
sprintf(tmp_time,"%d.%03d",used_time/1000,used_time%1000);
if ((index %2) != 0){
printf(" %s\n",tmp_time);
}else {
printf(" %d %s %s",ttl,from_ip,tmp_time);
}
if ((strcmp(dest_ip,from_ip) == 0) && (traceflag++ >=1)){
break;
}
}
return 0;
}
相关阅读 更多 +