ICMP套接字(linux)

Kei*_*vky 9 sockets linux icmp

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

socket(PF_INET, <type>, IPPROTO_ICMP)?

我应该在<type>字段中放入什么?我看到了一些使用SOCK_RAW的例子,但这不会阻止操作系统处理IP协议的工作吗?

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

nos*_*nos 21

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

  socket(PF_INET, SOCK_DGRAM IPPROTO_ICMP);
Run Code Online (Sandbox Code Playgroud)

这允许您只发送ICMP回应请求内核将专门处理它(匹配请求/响应,填写校验和).

这仅在设置了特殊sysctl时才有效.默认情况下,甚至root都不能使用这种套接字.您可以指定可以访问它的用户组.要允许root(组0)使用ICMP套接字,请执行以下操作:

 sysctl -w net.ipv4.ping_group_range="0 0"
Run Code Online (Sandbox Code Playgroud)

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果发送的数据没有适当的ICMP标头空间,并且ICMP type必须为8(ICMP_ECHO)且ICMP代码必须为0 ,则内核将拒绝并失败sendto()调用.

  • @Florida_Jake SOCK_DGRAM 与第 7 层无关。Unix 套接字编程书没有描述如何使用 Linux 特定的 ICMP 回显套接字,这里的示例有效。发送/接收 ICMP 消息的传统方式确实是使用 SOCK_RAW,并自己构建 ICMP 消息。我发布的代码使用另一种 Linux 特定功能来发送和接收 ICMP 回显消息。 (3认同)
  • 我刚刚将此代码改编为更高级的语言,并且效果很好。我要补充的唯一说明是,无需将“icmp_hdr.un.echo.id”分配给任何内容。Linux 将自行选择一个标识符。根据[补丁](https://lwn.net/Articles/420800/):“id被设置为套接字的编号(本地端口),校验和总是被重新计算。” (2认同)

Bas*_*tch 8

是的,这是可能的,因为该ping命令执行ICMP.

要找出所涉及的系统调用,您可以使用strace该命令(在root下).

您还可以浏览该命令的源代码,例如Debian的ping

有一个liboping库可以帮助你......