[Mono-list] UDP and ICMP

P Oscar Boykin boykin@pobox.com
Thu, 5 Feb 2004 15:28:12 -0800

Content-Type: multipart/mixed; boundary="+SfteS7bOf3dGlBC"
Content-Disposition: inline

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Sorry for replying to myself...

On Thu, Feb 05, 2004 at 10:45:16AM -0800, P Oscar Boykin wrote:
> There are APIs that throw errors when the ICMP message comes in. =20
> For instance, in Java there is the PortUnreachableException:
> http://klomp.org/mark/classpath/doc/api/html/java/net/PortUnreachableExce=
> It seems like people are saying that they think that A simply ignores
> the ICMP message and the .Net framework has no way of passing that
> information to the user (without being root and listening for the raw
> Icmp packets)

It seems like here is the problem, if you send a udp packet to a host
which is not listening on that port, nothing will happen, *BUT* if you
Connect first, the next ReceiveFrom will throw an exception.

You can find example code both in c and C# attached to this email.

I would prefer not to have to connect the socket to get this
functionality, but if that is the Socket API, I guess I am stuck with

boykin@pobox.com    http://pobox.com/~boykin    jabber: johnynek@jabber.org
fingerprint=3DD250 4AD9 4544 B7D2 A17C  911D D608 D387 6718 D75F
code is speech.  support freedom on the net.  http://www.eff.org/

Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="udptest.c"

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MYPORT 3490    // the port users will be connecting to
#define DEST_PORT 4240    // the port we try sending to (no one listens there)
#define DEST_IP ""

int main(int argc, char* argv[])
  int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
  struct sockaddr_in my_addr;    // my address information
  struct sockaddr_in dest_addr; // connector's address information
  int sin_size;
  int resp, size;
  char buf[1024];

  sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // do some error checking!

  my_addr.sin_family = AF_INET;         // host byte order
  my_addr.sin_port = htons(MYPORT);     // short, network byte order
  my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
  memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

  //Bind to the address:
  bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));

  dest_addr.sin_family = AF_INET;          // host byte order
  dest_addr.sin_port = htons(DEST_PORT);   // short, network byte order
  dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
  memset(&(dest_addr.sin_zero), '\0', 8);  // zero the rest of the struct

  //If I connect, then recvfrom will give an error, else not
  resp = connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
  resp = sendto(sockfd, "hello udp",8, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
  if( resp == -1 )
  //Now get the response:
  resp = recvfrom(sockfd, buf, 1024, 0, (struct sockaddr *)&dest_addr, &size);
  if( resp == -1 )
  return 0;

Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="test2.cs"
Content-Transfer-Encoding: quoted-printable

using System.Net.Sockets;
using System.Net;

public class Test {

  public static void Main(string[] arg)
    EndPoint end =3D new IPEndPoint(IPAddress.Parse(arg[0]),
    Socket s =3D new Socket(AddressFamily.InterNetwork,
    byte[] buf =3D new byte[1024];
    s.Bind(new IPEndPoint(IPAddress.Any, 23456));
    s.SendTo(buf, end);
    s.ReceiveFrom(buf, ref end);



Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.2.2 (GNU/Linux)