c语言实现ping命令
时间: 2023-11-22 22:37:58 浏览: 94
C语言实现PING命令
实现ping命令需要使用系统底层的网络API,这里给出一个基于Linux系统的C语言实现示例:
```c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 4
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd, datalen = 56;
int nsend = 0, nreceived = 0;
struct sockaddr_in dest_addr;
struct sockaddr_in from;
struct timeval tvrecv;
unsigned short cal_chksum(unsigned short *addr, int len)
{
int sum = 0;
unsigned short res = 0;
while(len > 1)
{
sum += *addr++;
len -= 2;
}
if(len == 1)
{
*((unsigned char *)(&res)) = *((unsigned char *)addr);
sum += res;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
res = ~sum;
return res;
}
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 = getpid();
packsize = 8 + datalen;
tval = (struct timeval *)icmp->icmp_data;
gettimeofday(tval, NULL);
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, packsize);
return packsize;
}
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;
icmp = (struct icmp *)(buf + iphdrlen);
len -= iphdrlen;
if(len < 8)
{
printf("ICMP packets\'s length is less than 8\n");
return -1;
}
if((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == getpid()))
{
tvsend = (struct timeval *)icmp->icmp_data;
gettimeofday(&tvrecv, NULL);
tv_sub(&tvrecv, tvsend);
rtt = tvrecv.tv_sec * 1000 + tvrecv.tv_usec / 1000.0;
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f ms\n",
len, inet_ntoa(from.sin_addr), icmp->icmp_seq, ip->ip_ttl, rtt);
return 0;
}
else
{
return -1;
}
}
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;
}
void ping()
{
int i, packsize;
struct timeval tv;
tv.tv_sec = MAX_WAIT_TIME;
tv.tv_usec = 0;
for (i = 0; i < MAX_NO_PACKETS; i++)
{
nsend++;
packsize = pack(nsend);
if (sendto(sockfd, sendpacket, packsize, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0)
{
printf("sendto error: %s\n", strerror(errno));
continue;
}
if (recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen) < 0)
{
printf("recvfrom error: %s\n", strerror(errno));
continue;
}
if (unpack(recvpacket, sizeof(recvpacket)) == -1)
{
printf("unpack error\n");
continue;
}
nreceived++;
usleep(1000000);
}
}
int main(int argc, char *argv[])
{
struct hostent *host;
struct protoent *protocol;
char protoname[] = "icmp";
int size = 50 * 1024;
if (argc < 2)
{
printf("usage: ping hostname/IP address\n");
return 0;
}
if ((protocol = getprotobyname(protoname)) == NULL)
{
printf("getprotobyname() error\n");
return 0;
}
if ((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
{
printf("socket() error\n");
return 0;
}
setuid(getuid());
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
if ((dest_addr.sin_addr.s_addr = inet_addr(argv[1])) == INADDR_NONE)
{
if ((host = gethostbyname(argv[1])) == NULL)
{
printf("gethostbyname() error\n");
return 0;
}
memcpy((char *)&dest_addr.sin_addr, host->h_addr, host->h_length);
}
printf("PING %s(%s) %d bytes of data.\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen);
ping();
printf("--- %s ping statistics ---\n", argv[1]);
printf("%d packets transmitted, %d received, %%%.2f packet loss\n",
nsend, nreceived, (nsend - nreceived) * 100.0 / nsend);
close(sockfd);
return 0;
}
```
这个实现使用了ICMP协议来模拟ping命令。需要注意的是,这个实现中使用了系统底层的网络API,因此需要在Linux系统下编译运行。如果需要在其他系统上运行,请根据具体系统的网络API进行调整。
阅读全文