linux - ICMP 套接字 (Linux)

标签 linux sockets icmp

是否可以在IP协议(protocol)下使用ICMP套接字?也许是这样的:

socket(PF_INET, <type>, IPPROTO_ICMP)?

我应该在 <type> 中放什么? field ?我看到了一些使用 SOCK_RAW 的示例,但这不会阻止操作系统处理 IP 协议(protocol)吗?

还有一件事。操作系统如何知道他应该将 ICMP 数据报发送到哪个进程,因为协议(protocol)没有涉及端口?

最佳答案

Linux 有一个特殊的 ICMP 套接字类型,您可以使用:

  socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP);

这允许您只发送 ICMP 回显请求内核将对其进行特殊处理(匹配请求/响应,填写校验和)。

这只适用于 special sysctl已设置。默认情况下,甚至 root 也不能​​使用这种套接字。您指定可以访问它的用户组。要允许 root(组 0)使用 ICMP 套接字,请执行以下操作:

 sysctl -w net.ipv4.ping_group_range="0 0"

这是一个示例程序,用于演示发送 ICMP 回显请求的最基本用法:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <sys/select.h>

//note, to allow root to use icmp sockets, run:
//sysctl -w net.ipv4.ping_group_range="0 0"

void ping_it(struct in_addr *dst)
{
    struct icmphdr icmp_hdr;
    struct sockaddr_in addr;
    int sequence = 0;
    int sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_ICMP);
    if (sock < 0) {
        perror("socket");
        return ;
    }

    memset(&addr, 0, sizeof addr);
    addr.sin_family = AF_INET;
    addr.sin_addr = *dst;

    memset(&icmp_hdr, 0, sizeof icmp_hdr);
    icmp_hdr.type = ICMP_ECHO;
    icmp_hdr.un.echo.id = 1234;//arbitrary id

    for (;;) {
        unsigned char data[2048];
        int rc;
        struct timeval timeout = {3, 0}; //wait max 3 seconds for a reply
        fd_set read_set;
        socklen_t slen;
        struct icmphdr rcv_hdr;

        icmp_hdr.un.echo.sequence = sequence++;
        memcpy(data, &icmp_hdr, sizeof icmp_hdr);
        memcpy(data + sizeof icmp_hdr, "hello", 5); //icmp payload
        rc = sendto(sock, data, sizeof icmp_hdr + 5,
                        0, (struct sockaddr*)&addr, sizeof addr);
        if (rc <= 0) {
            perror("Sendto");
            break;
        }
        puts("Sent ICMP\n");

        memset(&read_set, 0, sizeof read_set);
        FD_SET(sock, &read_set);

        //wait for a reply with a timeout
        rc = select(sock + 1, &read_set, NULL, NULL, &timeout);
        if (rc == 0) {
            puts("Got no reply\n");
            continue;
        } else if (rc < 0) {
            perror("Select");
            break;
        }

        //we don't care about the sender address in this example..
        slen = 0;
        rc = recvfrom(sock, data, sizeof data, 0, NULL, &slen);
        if (rc <= 0) {
            perror("recvfrom");
            break;
        } else if (rc < sizeof rcv_hdr) {
            printf("Error, got short ICMP packet, %d bytes\n", rc);
            break;
        }
        memcpy(&rcv_hdr, data, sizeof rcv_hdr);
        if (rcv_hdr.type == ICMP_ECHOREPLY) {
            printf("ICMP Reply, id=0x%x, sequence =  0x%x\n",
                            icmp_hdr.un.echo.id, icmp_hdr.un.echo.sequence);
        } else {
            printf("Got ICMP packet with type 0x%x ?!?\n", rcv_hdr.type);
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("usage: %s destination_ip\n", argv[0]);
        return 1;
    }
    struct in_addr dst;

    if (inet_aton(argv[1], &dst) == 0) {

        perror("inet_aton");
        printf("%s isn't a valid IP address\n", argv[1]);
        return 1;
    }

    ping_it(&dst);
    return 0;
}

请注意,如果发送的数据没有适当的 ICMP header 空间,并且 ICMP type 必须为 8 (ICMP_ECHO) 并且 ICMP代码必须为0。

关于linux - ICMP 套接字 (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290046/

相关文章:

linux - flock 和 NFS——意外关机时会发生什么?

linux - Tmux .tmux.conf 无法正确加载

java - 在 Java 聊天中实现 SSL

javascript - Net.Socket 实例不会在 NodeJS 中消失

windows - 如何判断计算机是否打开并在网络上运行

Linux Bash 脚本 : Declare var name from user or file input

linux - 需要确定 RHEL 7 的兼容 java 版本

sockets - Linux内核TCP Socket修改

ubuntu - Ubuntu 18.04 中未调用 Netfilter Hook - 内核 4.18

azure - 如何在 Azure 上启用 Ping(ICMP)