当前位置: 技术问答>linux和unix
IP头问题
来源: 互联网 发布时间:2016-06-04
本文导语: 想做一个linux下的tracert,拿了网上现成的ping代码自己改装: #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PACKET_SIZE 4096 #d...
想做一个linux下的tracert,拿了网上现成的ping代码自己改装:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd,datalen=56;
int nsend=0,nreceived=0;
struct sockaddr_in dest_addr;
pid_t pid;
struct sockaddr_in from;
struct timeval tvrecv;
void statisticsexit(int signo);
unsigned short cal_chksum(unsigned short *addr,int len);
int pack(int pack_no);
void send_packet(void);
void recv_packet(void);
int unpack(char *buf,int len);
void tv_sub(struct timeval *out,struct timeval *in);
short TTL=0;
struct protoent *protocol;
using namespace std;
#include
//void statistics(int signo)
//{
// printf("n--------------------PING statistics-------------------n");
// printf("%d packets transmitted, %d received , %%%d lostn",nsend,nreceived,(nsend-nreceived)/nsend*100);
// close(sockfd);
//}
/*校验和算法*/
unsigned short cal_chksum(unsigned short *addr,int len)
{
int nleft=len;
int sum=0;
unsigned short *w=addr;
unsigned short answer=0;
/*把ICMP报头二进制数据以2字节为单位累加起来*/
while(nleft>1)
{
sum+=*w++;
nleft-=2;
}
/*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/
if( nleft==1)
{
*(unsigned char *)(&answer)=*(unsigned char *)w;
sum+=answer;
}
sum=(sum>>16)+(sum&0xffff);
sum+=(sum>>16);
answer=~sum;
return answer;
}
/*设置ICMP报头*/
int pack(int pack_no)
{
setuid(getuid());
struct ip *ip;
struct icmp *icmp;
struct timeval *tval;
int packsize=8+datalen+sizeof(struct ip);
memset(sendpacket,0,PACKET_SIZE);
ip=(struct ip *)sendpacket;
ip->ip_v=IPVERSION;
ip->ip_hl=sizeof(struct ip)>>2;
ip->ip_tos=0;
ip->ip_len=htons(packsize);
ip->ip_id=0;
ip->ip_off=0;
ip->ip_ttl=TTL;
couticmp_cksum=cal_chksum( (unsigned short *)icmp,packsize); /*校验算法*/
return packsize;
}
/*发送三个ICMP报文*/
void send_packet()
{
int packetsize;
nsend++;
packetsize=pack(nsend); /*设置ICMP报头*/
if(sendto(sockfd,sendpacket,packetsize,0,(struct sockaddr *)&dest_addr,sizeof(dest_addr) )icmp_data;
tv_sub(&tvrecv,tvsend); /*接收和发送的时间差*/
rtt=tvrecv.tv_sec*1000+tvrecv.tv_usec/1000; /*以毫秒为单位计算rtt*/
/*显示相关信息*/
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f msn",len,inet_ntoa(from.sin_addr),icmp->icmp_seq,ip->ip_ttl,rtt);
}
//else return -1;
return 0;
}
void tracing()
{
while(TTLp_proto) )h_addr,host->h_length);
// }
// else /*是ip地址*/
// memcpy((char *)&dest_addr,(char *)&inaddr,host->h_length);
inet_pton(AF_INET,argv,&dest_addr.sin_addr);
/*获取main的进程id,用于设置ICMP的标志符*/
pid=getpid();
printf("PING %s(%s): %d bytes data in ICMP packets.n",argv,inet_ntoa(dest_addr.sin_addr),datalen);
tracing();
//statistics(SIGALRM); /*进行统计*/
return 0;
}
/*两个timeval结构相减*/
void tv_sub(struct timeval *out,struct timeval *in)
{
if((out->tv_usec-=in->tv_usec)tv_sec;
out->tv_usec+=1000000;
}
out->tv_sec-=in->tv_sec;
}
现在运行结果是:
hi0
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=1 ttl=0 rtt=0.000 ms
hi1
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=1 ttl=64 rtt=0.000 ms
hi2
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=2 ttl=1 rtt=0.000 ms
hi3
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=2 ttl=64 rtt=0.000 ms
hi4
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=3 ttl=2 rtt=0.000 ms
hi5
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=3 ttl=64 rtt=0.000 ms
hi6
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=4 ttl=3 rtt=0.000 ms
hi7
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=4 ttl=64 rtt=0.000 ms
怎么会是这样。。。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd,datalen=56;
int nsend=0,nreceived=0;
struct sockaddr_in dest_addr;
pid_t pid;
struct sockaddr_in from;
struct timeval tvrecv;
void statisticsexit(int signo);
unsigned short cal_chksum(unsigned short *addr,int len);
int pack(int pack_no);
void send_packet(void);
void recv_packet(void);
int unpack(char *buf,int len);
void tv_sub(struct timeval *out,struct timeval *in);
short TTL=0;
struct protoent *protocol;
using namespace std;
#include
//void statistics(int signo)
//{
// printf("n--------------------PING statistics-------------------n");
// printf("%d packets transmitted, %d received , %%%d lostn",nsend,nreceived,(nsend-nreceived)/nsend*100);
// close(sockfd);
//}
/*校验和算法*/
unsigned short cal_chksum(unsigned short *addr,int len)
{
int nleft=len;
int sum=0;
unsigned short *w=addr;
unsigned short answer=0;
/*把ICMP报头二进制数据以2字节为单位累加起来*/
while(nleft>1)
{
sum+=*w++;
nleft-=2;
}
/*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/
if( nleft==1)
{
*(unsigned char *)(&answer)=*(unsigned char *)w;
sum+=answer;
}
sum=(sum>>16)+(sum&0xffff);
sum+=(sum>>16);
answer=~sum;
return answer;
}
/*设置ICMP报头*/
int pack(int pack_no)
{
setuid(getuid());
struct ip *ip;
struct icmp *icmp;
struct timeval *tval;
int packsize=8+datalen+sizeof(struct ip);
memset(sendpacket,0,PACKET_SIZE);
ip=(struct ip *)sendpacket;
ip->ip_v=IPVERSION;
ip->ip_hl=sizeof(struct ip)>>2;
ip->ip_tos=0;
ip->ip_len=htons(packsize);
ip->ip_id=0;
ip->ip_off=0;
ip->ip_ttl=TTL;
couticmp_cksum=cal_chksum( (unsigned short *)icmp,packsize); /*校验算法*/
return packsize;
}
/*发送三个ICMP报文*/
void send_packet()
{
int packetsize;
nsend++;
packetsize=pack(nsend); /*设置ICMP报头*/
if(sendto(sockfd,sendpacket,packetsize,0,(struct sockaddr *)&dest_addr,sizeof(dest_addr) )icmp_data;
tv_sub(&tvrecv,tvsend); /*接收和发送的时间差*/
rtt=tvrecv.tv_sec*1000+tvrecv.tv_usec/1000; /*以毫秒为单位计算rtt*/
/*显示相关信息*/
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f msn",len,inet_ntoa(from.sin_addr),icmp->icmp_seq,ip->ip_ttl,rtt);
}
//else return -1;
return 0;
}
void tracing()
{
while(TTLp_proto) )h_addr,host->h_length);
// }
// else /*是ip地址*/
// memcpy((char *)&dest_addr,(char *)&inaddr,host->h_length);
inet_pton(AF_INET,argv,&dest_addr.sin_addr);
/*获取main的进程id,用于设置ICMP的标志符*/
pid=getpid();
printf("PING %s(%s): %d bytes data in ICMP packets.n",argv,inet_ntoa(dest_addr.sin_addr),datalen);
tracing();
//statistics(SIGALRM); /*进行统计*/
return 0;
}
/*两个timeval结构相减*/
void tv_sub(struct timeval *out,struct timeval *in)
{
if((out->tv_usec-=in->tv_usec)tv_sec;
out->tv_usec+=1000000;
}
out->tv_sec-=in->tv_sec;
}
现在运行结果是:
hi0
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=1 ttl=0 rtt=0.000 ms
hi1
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=1 ttl=64 rtt=0.000 ms
hi2
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=2 ttl=1 rtt=0.000 ms
hi3
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=2 ttl=64 rtt=0.000 ms
hi4
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=3 ttl=2 rtt=0.000 ms
hi5
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=3 ttl=64 rtt=0.000 ms
hi6
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=4 ttl=3 rtt=0.000 ms
hi7
dest addr is 169.254.8.242
64 byte from 169.254.8.242: icmp_seq=4 ttl=64 rtt=0.000 ms
怎么会是这样。。。
|
tracert的源码?没写过。
再去pudn下个看看。
http://www.pudn.com/downloads25/sourcecode/unix_linux/network/detail79050.html
再去pudn下个看看。
http://www.pudn.com/downloads25/sourcecode/unix_linux/network/detail79050.html
|
贴你的问题出来,代码太长了,没时间分析
IP的头结构可以去看RFC 791
IP的头结构可以去看RFC 791
|
unix网络编程里有ping和tracert的源码
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。