C语言实现ping

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>

#define PACKET_SIZE 64  // ICMP报文大小
#define MAX_WAIT_TIME 2 // 最大等待时间(秒)

// 计算校验和
unsigned short calculateChecksum(unsigned short *addr, int len) {
    unsigned int sum = 0;
    while (len > 1) {
        sum += *addr++;
        len -= 2;
    }
    if (len == 1) {
        sum += *(unsigned char *)addr;
    }
    sum = (sum >> 16) + (sum & 0xFFFF);
    sum += (sum >> 16);
    return (unsigned short)(~sum);
}

// 发送ICMP Echo请求
void sendICMPEchoRequest(int sockfd, struct sockaddr_in *dest_addr, int seq) {
    struct icmp send_pkt;
    int pkt_size = sizeof(struct icmp);

    memset(&send_pkt, 0, pkt_size); // 清空结构体
    send_pkt.icmp_type = ICMP_ECHO;
    send_pkt.icmp_code = 0;
    send_pkt.icmp_id = getpid();
    send_pkt.icmp_seq = seq;
    send_pkt.icmp_cksum = calculateChecksum((unsigned short *)&send_pkt, pkt_size);

    // 发送报文
    ssize_t bytes_sent = sendto(sockfd, &send_pkt, pkt_size, 0, (struct sockaddr *)dest_addr, sizeof(struct sockaddr));
    if (bytes_sent <= 0) {
        perror("sendto");
        exit(1);
    }
}

// 接收ICMP Echo响应
void receiveICMPEchoResponse(int sockfd, struct sockaddr_in *src_addr) {
    char recv_buf[PACKET_SIZE];
    struct sockaddr_in from_addr;
    socklen_t from_addr_len = sizeof(from_addr);
    ssize_t bytes_received = recvfrom(sockfd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&from_addr, &from_addr_len);

    if (bytes_received <= 0) {
        perror("recvfrom");
        exit(1);
    }

    struct ip *ip_hdr = (struct ip *)recv_buf;
    struct icmp *icmp_hdr = (struct icmp *)(recv_buf + (ip_hdr->ip_hl << 2));

    if (icmp_hdr->icmp_type == ICMP_ECHOREPLY) {
        printf("Reply from %s: seq=%d\n", inet_ntoa(from_addr.sin_addr), icmp_hdr->icmp_seq);
    }
}

int main(int argc, char **argv) {
    if (argc != 2) {
        printf("Usage: %s <host>\n", argv[0]);
        exit(1);
    }

    const char *host = argv[1];

    struct hostent *hostent = gethostbyname(host);
    if (!hostent) {
        printf("Couldn't resolve host: %s\n", host);
        exit(1);
    }

    struct sockaddr_in dest_addr;
    memset(&dest_addr, 0, sizeof(dest_addr));
    dest_addr.sin_family = AF_INET;
    dest_addr.sin_addr = *((struct in_addr *)hostent->h_addr);

    // 创建原始套接字
    int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if (sockfd < 0) {
        perror("socket");
        exit(1);
    }

    int seq = 0;

    while (seq < 4) {
        sendICMPEchoRequest(sockfd, &dest_addr, seq);
        receiveICMPEchoResponse(sockfd, &dest_addr);
        seq++;
        sleep(1);
    }

    close(sockfd);

    return 0;
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值