IPプロトコルでICMPソケットを使用することは可能ですか?多分次のようなものです:
socket(PF_INET, <type>, IPPROTO_ICMP)?
<type>
フィールドに何を入れるべきですか?を使用していくつかの例を見ましSOCK_RAW
たが、それはOSがIPプロトコルを処理する仕事をするのを妨げませんか?
そして別のこと。プロトコルに関係するポートがないため、OSはどのプロセスにICMPデータグラムを送信する必要があるかをどのように知ることができますか?
Linuxには、以下で使用できる特別なICMPソケットタイプがあります。
socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP);
これにより、ICMPエコー要求のみを送信できます。カーネルはそれを特別に処理します(要求/応答の一致、チェックサムの入力)。
これは、特別なsysctlが設定されている場合にのみ機能します。デフォルトでは、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ヘッダーの余地がない場合、カーネルはsendto()呼び出しを拒否して失敗し、ICMPtype
は8(ICMP_ECHO)であり、ICMPコードは0である必要があることに注意してください。
はい、それは可能ping
です。コマンドはICMPを実行するからです。
関連するシステムコールを見つけるには、strace
そのコマンドを(ルートの下で)実行できます。
また、 Debianのpingなど、そのコマンドのソースコードを一瞥することもできます。
そして、あなたを助けるためのlibopingライブラリがあります...