本文介绍了如何接收与原始套接字用C ICMP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

。这是一项任务,它必须与原始套接字。我想编写一个简单的ICMP PING。
我用这个作为基础。在线路127是为wlan0,进出口使用的eth0。在290线I codeD这样的:

.This is an assignment, it has to be with raw sockets. I need to program a simple icmp ping.I used this as a base http://www.pdbuchan.com/rawsock/icmp4.c . At line 127 is wlan0, Im using eth0. In line 290 I coded this:

`

 struct sockaddr_in rec;

  unsigned char * pkt = (unsigned char *) malloc (IP_MAXPACKET * sizeof (unsigned char));

  if (recvfrom (sd, (void*)pkt, IP4_HDRLEN + ICMP_HDRLEN+datalen , 0, NULL, (socklen_t*)sizeof (struct sockaddr)) < 0)  {
    perror ("recvfrom() failed ");
    exit (EXIT_FAILURE);
  }
  struct ip *ip = (struct ip *)pkt;
  struct icmphdr *icmp = (struct icmphdr *)(pkt + IP4_HDRLEN);

  printf("%s %s %d\n",(char*)inet_ntoa(*(struct in_addr*)&ip->ip_dst),
            (char*)inet_ntoa(*(struct in_addr*)&ip->ip_src),
        icmp->type);
  free (pkt);

`
问题是,ip_dst和ip_src被示为我的机器的IP和ICMP类型为0并且不8. Wireshark的同时显示ICMP回应和请求。
也许我recvfrom的是错的,但我听说过一些关于Linux自己的TCP / IP可能被处理的数据包。如果这是真的,有什么解决办法是什么?

`The problem is that ip_dst and ip_src are being shown as my machines's IP, and icmp type as 0 and not 8. Wireshark shows both icmp reply and request.Probably my recvfrom is wrong, but I heard something about linux own TCP/IP might be handling the packets. If that's true, what is the workaround for that?

编辑:我检查这个但它并没有解决我的问题。

edit: I checked this raw socket listener but it did not solve my problem.

推荐答案

我不认为你可以使用IPPROTO_RAW时得到答复。

I don't think you can get a reply when using IPPROTO_RAW.

您必须使用

socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);

使用IPPROTO_ICMP必须只发送一个ICMP数据包,而不是整个IP包。

With IPPROTO_ICMP you must only send an ICMP packet, not the whole IP packet.

在接收但是你会得到整个IP数据包,并要提取的ICMP回复。请注意,您将获得向主机发送ICMP包的副本,所以你必须进行过滤。

When receiving however you'll get the whole IP packet and have to extract the ICMP reply. Note that you will get a copy of all ICMP packets sent to the host so you must filter them.

看。

这篇关于如何接收与原始套接字用C ICMP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:56