[Mono-list] UDP and ICMP

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


------=_20040205070451_12014
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

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
----------------------------------------
------=_20040205070451_12014
Content-Type: application/x-gzip; name="icmp.tar.gz"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="icmp.tar.gz"

H4sIABTaIUAAA+1YbW/bNhDOZ/8KQhgGeXUVSVGcbrFTLG5TGEgzo86wD0U+KNLZ1iKLrkg59oLs
t+/4Ipl+idsOzYoBOgSwdLyX507H4zFJNJ0dHjwvuUgnrou/ge8H/sGJpLbkazrwXNdzj46Dk7aH
/ONjXCbHz4yrpoODRHz/GEZhkXLntkjS+Nv7+Mz399wAn5HRPjrxj05QznN97+iAfHskNW1S5/Vi
mpI55CyhWdfyHNd6fdZodGY5/RMi3iAkC6fQtWZJNrbwTZdK15K1YqEoIR0e5mMQsqW0WpSMGFiU
JzMuzZ8LPiN8AiQq8hwyTiKajZJxkYdCAu0JnU7EIvlAiDLdtWABlmbFcFuMuxbPi4pFCz4rUOqH
hyzMuKPBOwLMoyNUz7Rgh9Eij4CRUZikNIPpjC+1qVIEhZIsSgsErqP5yYmYRQ4rG4faiAZ7iGjF
Y+dQgX0iJ1EKYbYjJz3B/4KcxJAChwrEKEmBASe3IYM4ybuWszcCkQQjAq1dRrCybQTROdR5PGt8
7yqt6blI9v9+7/0Aa/y5fHym//v+ia/6f4AnfyDk2m2vXff//4IKho2dDJeMw/S0Yb45V8A3ONew
QFYjSkPGiCiaxgO2jFlxmyYRuV1yINfLGZxu8Ho0NnkFm9Cck94EojtWTI2VBFvfe2AsHMMw+WvT
zsebcpF0SQb3iomjQnBz2liJClh2kz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