msantos / procket

Erlang interface to low level socket operations

Home Page:http://blog.listincomprehension.com/search/label/procket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strange behavior while spawning icmp:ping from examples

infospacer opened this issue · comments

I'm trying to ping many hosts by spawning icmp:ping from examples
For example two hosts
-192.168.20.9 (I know it is on)
-192.168.20.10 (I know it is off)

But I get answers as if they both are "on" and their "id" and "checksum" totally coincide.

11> [ spawn(icmp, ping ,[H]) || H <- ["192.168.20.9","192.168.20.10"]].
[<0.121.0>,<0.122.0>]
12>
=INFO REPORT==== 1-Jun-2013::09:48:40 ===
type: 0
code: 0
checksum: 23085
id: 16563
sequence: 0
payload: <<" !"#$%&'()*+,-./0123456789:;<=>?@abcdefghijk">>
time: 13438

=INFO REPORT==== 1-Jun-2013::09:48:40 ===
type: 0
code: 0
checksum: 23085
id: 16563
sequence: 0
payload: <<" !"#$%&'()*+,-./0123456789:;<=>?@abcdefghijk">>
time: 13461

One oddity of ICMP sockets (at least compared to TCP and UDP sockets) is that each socket receives all the ICMP packets destined for the host. The example code isn't very sophisticated. So what you are seeing is: the ICMP echo reply comes back from 192.168.20.9, both sockets receive it, both report ok.

If you are only interested in ICMP echo reply packets, then you could modify the code to check the source address. The problem with doing this is that you might miss other replies. For example, if there are too many hops between you and the destination, an intermediate router might send back an ICMP time exceeded in transit message.

gen_icmp might give you more ideas on how to handle these conditions:

https://github.com/msantos/gen_icmp

Note: if you are going to use gen_icmp, I am right in the middle of adding RFC3542 support, so the master branch is a bit unstable. In particular, I might change the reply of gen_icmp:ping/1,2,3 to be mirror gen_udp:recv/2.

Thank you for your answer.
I will try gen_icmp.