[Mono-list] UDP and ICMP

Giuseppe Greco giuseppe.greco@agamura.com
Thu, 5 Feb 2004 07:04:21 +0100 (CET)


Oscar,

If I've well understood, you are looking for a way
to handle ICMP packets...

Well, you have to relay on raw sockets. Here below
is how you can create a raw socket for ICMP:

  Socket socket = new Socket(
      AddressFamily.InterNetwork,
      SocketType.Raw,
      ProtocolType.Icmp);

Then, you should set a receive timeout like this

  socket.SetSocketOption(
      SocketOptionLevel.Socket,
      SocketOptionName.ReceiveTimeout,
      3000); // e.g. 3 seconds

... and finally, you have to build the ICMP packet and send it:

  socket.SendTo(...);

Note that when receiving an ICMP packet, the first 20
bytes (or the first 40 bytes if you are using IPv6),
correspond to the IP header and you should skip them
when parsing the received data.

By the way, attached to this email you will find a
zipped tarball that contains a full example of how
to use ICMP with raw sockets.

I hope that helps,

j3d.

> Hello All.
>
> I am using using mono to send udp packets between two hosts.  At some
> point, one host may go offline.  If I watch the network (using ethereal)
> I see the UDP packet go from A->B, and host B responds with a ICMP
> destination unreachable packet.  However, I don't see how I can get
> access to that packet in my application.
>
> I was hoping that SendTo or ReceiveFrom could throw exceptions, but I
> don't believe that is how they work.
>
> I also tried to make a separate socket to listen for the ICMP packets,
> but under Linux and Mono 0.30, I get an exception when I try to create
> the socket to listen for ICMP messages:
>
> Unhandled Exception: System.Net.Sockets.SocketException: Access denied
> in (unmanaged) /usr/local/lib/libmono.so.0(mono_raise_exception+0x1f)
> [0x4009176f]
> in (unmanaged) /usr/local/lib/libmono.so.0 [0x400b1d7d]
> in <0x00054> System.Net.Sockets.Socket:.ctor
> (System.Net.Sockets.AddressFamily,System.Net.Sockets.SocketType,System.Net.Sockets.ProtocolType)
> in <0x000d3> Gnucla.UdpEdgeListener:.ctor (int)
> in <0x001b0> Gnucla.EchoTest:Main (string[])
>
> Can anyone suggest a method that host A can see that host B is no longer
> listening without doing it at the application layer?  I just want the
> ICMP packet to have an effect.
>
> Thanks.
> POB
> --
> boykin@pobox.com    http://pobox.com/~boykin    jabber:
> johnynek@jabber.org
> fingerprint=D250 4AD9 4544 B7D2 A17C  911D D608 D387 6718 D75F
> fix copyright:
> http://cyberlaw.stanford.edu/lessig/blog/archives/EAFAQ.html
>


----------------------------------------
Giuseppe Greco

::agamura::

phone:  +41 (0)91 604 67 65
mobile: +41 (0)76 390 60 32
email:  giuseppe.greco@agamura.com
web:    www.agamura.com
----------------------------------------