http://www.rfc-editor.org/rfc/rfc791.txt
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
struct ip
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ip_hl:4; /* header length */
unsigned int ip_v:4; /* version */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned int ip_v:4; /* version */
unsigned int ip_hl:4; /* header length */
#endif
u_int8_t ip_tos; /* type of service */
u_short ip_len; /* total length */
u_short ip_id; /* identification */
u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_int8_t ip_ttl; /* time to live */
u_int8_t ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src, ip_dst; /* source and dest address */
};
TCP/IP教程:http://www.rfc-editor.org/rfc/rfc1180.txt
TCP/IP协议校验和算法:http://www.rfc-editor.org/rfc/rfc1071.txt
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#inlcude <netinet/ip_icmp.h>
#include <netdb.h>
#include <setjmp.h>
#inlcude <errno.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int datalen = 56;
int nsend = 0, nreceived = 0;
int sockfd;
struct sockaddr_in dest_addr, from;
struct timeval tvrecv;
pid_t pid;
void statistics(int signo);
unsigned short cal_checksum(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);
void statistics(int signo)
{
printf("\n-------------PING statistics---------------\n");
printf("%d packet transmitted , %d received, %%%d lost\n",nsend, nreceived, \
(nsend-nreceived)/nsend*100);
close(sockfd);
exit(1);
}
/*-------校验和算法--------*/
unsigned short cal_checksum(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字节数据的高字节,低字节为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)
{
int i, packsize;
struct icmp *icmp;
struct timeval *tval;
icmp = (struct icmp*)sendpacket;
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_seq = pack_no;
icmp->icmp_id = pid;
packsize = 8 + datalen;
tval = (struct timeval *)icmp->icmp_data;
gettimeofday(tval,NULL);/*记录发送时间*/
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, packsize);/*校验算法*/
return packsize;
}
/*---发送三个报文---*/
void send_packet()
{
int packetsize;
while( nsend < MAX_NO_PACKETS )
{
nsend++;
packetsize = pack(nsend);/*设置ICMP报头*/
if( sendto(sockfd, sendpacket, packetsize, 0,
(struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0 )
{
perror("sendto error");
continue;
}
sleep(1); /*每隔一秒发送一个ICMP报文*/
}
}
void recv_packet()
{
int n, fromlen;
extern int errno;
signal(SIGALRM,statistics);
fromlen = sizeof(from);
while(nreceived < nsend)
{
alarm(MAX_WAIT_TIME);
if((n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0,
(struct sockaddr *)&from, &fromlen)) < 0)
{
if(errno == EINTR)
continue;
perror(recvfrom error");
continue;
}
gettimeofday(&tvrecv,NULL); /*记录接收时间*/
if(unpack(recvpacket,n) == -1)
continue;
nreceived++;
}
}
int unpack(char *buf, int len)
{
int i, iphdrlen;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
double rtt;
ip = (struct ip *)buf;
iphdrlen = ip->ip_hl<<2; /*求ip报头长度,即ip报头的长度标志乘4*/
icmp = (struct icmp *)(buf + iphdrlen); /*越过ip报头,指向ICMP报头*/
len -= iphdrlen; /*ICMP报头及ICMP数据报的总长度*/
if( len < 8 )
{
printf("ICMP packet\'s length is less than 8\n");
return -1;
}
/*确保所收到的是我所发的ICMP的回应*/
if(( icmp->icmp_type == ICMP_ECHOREPLY ) && (icmp->icmp_id == pid))
{
tvsend = (struct timeval *)icmp->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 ms\n",
len,
inet_ntoa(from.sin_addr),
icmp->icmp_seq,
ip->ttl,
rtt);
}
else
return -1;
}
int main(int argc, char *argv[])
{
struct hostent *host;
struct protoent *protocol;
unsigned long inaddr = 0L;
int waittime = MAX_WAIT_TIME;
int size = 50 * 1024;
if( argc < 2 )
{
printf("usage:%s hostname / IP address\n", argv[0]);
exit(1);
}
if((protocol = getprotobyname("icmp")) == NULL)
{
perror("getprotobyname");
exit(1);
}
/*生成使用ICMP的原始套接字,这种套接字只有root才能生成*/
if(( sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
{
perror("socket error");
exit(1);
}
/*收回root权限,设置当前用户权限*/
setuid(getuid());
/*扩大套接字接收缓冲区到50K,这样做主要为了减小接收缓冲区溢出的
可能性,若无意中ping一个广播地址或多播地址,将会引来大量应答*/
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
/*判断是主机名还是IP地址*/
if( inaddr = inet_addr(argv[1]) == INADDR_NONE)
{
if((host = gethostbyname(argv[1])) == NULL) /*主机名*/
{
perror("gethostbyname error");
exit(1);
}
memcpy((char *)&dest_addr.sin_addr, host->h_addr, host->h_length);
}
else /*是IP地址*/
memcpy((char *)&dest_addr, (char *)&inaddr, host->h_length);
/*获取main的进程id,用于设置ICMP的标志符*/
pid = getid();
printf("PING %s(%s):%d bytes data in ICMP packets.\n", argv[1], inet_ntoa(dest_addr.sin_addr),datalen);
send_packet();
recv_packet();
statistics(SIGALRM);
return 0;
}
void tv_sub(struct timeval *out, struct timeval *in)
{
if((out->tv_usec -= in->tv_usec) < 0 )
{
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
sudo gcc -o ping ping.c
sudo chmod u+s ping #目的是把ping程序设置成SUID的属性
使用SCTP优化网络:http://www.ibm.com/developerworks/cn/linux/l-sctp/