[Mono-list] UDP and ICMP

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


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


--+SfteS7bOf3dGlBC
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
>=20
> For instance, in Java there is the PortUnreachableException:
> http://klomp.org/mark/classpath/doc/api/html/java/net/PortUnreachableExce=
ption.html
>=20
> 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
it.

POB.
--=20
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/

--+SfteS7bOf3dGlBC
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 "127.0.0.1"

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 )
  {
    perror("sendto");
    exit(1);
  }
  //Now get the response:
  resp = recvfrom(sockfd, buf, 1024, 0, (struct sockaddr *)&dest_addr, &size);
  if( resp == -1 )
  {
    perror("recvfrom");
    exit(1);
  }
  return 0;
}

--+SfteS7bOf3dGlBC
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]),
		                    System.Int16.Parse(arg[1]));
	 =20
    Socket s =3D new Socket(AddressFamily.InterNetwork,
		          SocketType.Dgram,
			  ProtocolType.Udp);
    byte[] buf =3D new byte[1024];
    s.Bind(new IPEndPoint(IPAddress.Any, 23456));
    s.Connect(end);
    s.SendTo(buf, end);
    s.ReceiveFrom(buf, ref end);

  }
 =20
}

--+SfteS7bOf3dGlBC--

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAItGL1gjTh2cY118RAutcAJ9ay428dwzHx//kvMgtfj70v3ZC1wCeN2xa
Qd5KwSTYkNIzFOv1NJFxke8=
=CLH1
-----END PGP SIGNATURE-----

--Bqc0IY4JZZt50bUr--