ICMP套接字(linux)

时间:2011-11-27 23:46:25

标签: linux sockets icmp

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

socket(PF_INET, <type>, IPPROTO_ICMP)?

我应该把什么放在&lt;类型&gt;领域?我看到了一些使用SOCK_RAW的例子,但这不会阻止操作系统处理IP协议的工作吗?

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

2 个答案:

答案 0 :(得分:20)

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标头空间,并且ICMP type必须为8(ICMP_ECHO)并且ICMP代码必须为,则内核将拒绝并失败sendto()调用。 0

答案 1 :(得分:8)

是的,因为ping命令可以执行ICMP。

要找出所涉及的系统调用,您可以strace该命令(在根目录下)。

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

还有liboping库来帮助你...

相关问题