debugging for datagram sockets

Patrick Tullmann tullmann at cs.utah.edu
Thu Oct 14 21:41:30 PDT 1999


The attached patch adds NATIVENET debugging support to datagram socket 
operations, just like already exist on stream sockets.

Here's a ChangeLog entry (if necessary):

1999-10-14  Patrick A Tullmann  <tullmann at cs.utah.edu>

	* libraries/clib/net/PlainDatagramSocketImpl.c: add
	NATIVENET debugging support.

-Pat

----- ----- ---- ---  ---  --   -    -      -         -               -
Pat Tullmann                                       tullmann at cs.utah.edu

Index: libraries/clib/net/PlainDatagramSocketImpl.c
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/clib/net/PlainDatagramSocketImpl.c,v
retrieving revision 1.18
diff -u -b -r1.18 PlainDatagramSocketImpl.c
--- PlainDatagramSocketImpl.c	1999/09/10 13:41:41	1.18
+++ PlainDatagramSocketImpl.c	1999/10/15 04:51:42
@@ -23,6 +23,7 @@
 #include "java_net_SocketOptions.h"
 #include "nets.h"
 #include <jsyscall.h>
+#include "../../../kaffe/kaffevm/debug.h"
 
 /*
  * Supported socket options
@@ -43,6 +44,41 @@
 #endif
   };
 
+#ifdef DEBUG
+ static const struct {
+   int opt;
+   char *name;
+ } optionNames[] = {
+#ifdef SO_SNDBUF
+   { java_net_SocketOptions_SO_SNDBUF, "SO_SNDBUF" },
+#endif
+#ifdef SO_RCVBUF
+   { java_net_SocketOptions_SO_RCVBUF, "SO_RCVBUF" },
+#endif
+#ifdef SO_REUSEADDR
+   { java_net_SocketOptions_SO_REUSEADDR, "SO_REUSEADDR" },
+#endif
+ };
+#endif /*DEBUG*/
+
+#ifdef DEBUG
+/* Generate a string for an inet addr (in host form). */
+static char *
+ip2str(jint addr) 
+{
+	static char addrbuf[(4 * 3) + 3];
+	unsigned int top = (addr >> 24) & 0xFF;
+	unsigned int tmid = (addr >> 16) & 0xFF;
+	unsigned int bmid = (addr >> 8) & 0xFF;
+	unsigned int bottom = addr & 0xFF;
+
+	sprintf(addrbuf, "%u.%u.%u.%u", top, tmid, bmid, bottom);
+	return addrbuf;
+}
+#endif /* DEBUG */
+
+
+
 /*
  * Create a datagram socket.
  */
@@ -52,12 +88,22 @@
 	int fd;
 	int rc;
 
+	DBG(NATIVENET,
+	    dprintf("datagram_create(%p)\n", this);
+	    )
+
 	rc = KSOCKET(AF_INET, SOCK_DGRAM, 0, &fd);
 	if (rc) {
 		unhand(unhand(this)->fd)->fd = -1;
 		SignalError("java.net.SocketException", SYS_ERROR(rc));
 	}
+
 	unhand(unhand(this)->fd)->fd = fd;
+
+	DBG(NATIVENET,
+	    dprintf("datagram_create(%p) -> fd=%d\n", this, fd);
+	    )
+
 #if defined(SOL_SOCKET) && defined(SO_BROADCAST)
 	/* On some systems broadcasting is off by default - enable it here */
 	{
@@ -79,6 +125,11 @@
 	int alen;
 	const int fd = unhand(unhand(this)->fd)->fd;
 
+	DBG(NATIVENET,
+	    dprintf("datagram_bind(%p, %s, %d)\n", 
+		    this, ip2str(unhand(laddr)->address), port);
+	    )
+
 	memset(&addr, 0, sizeof(addr));
 #if defined(BSD44)
 	addr.sin_len = sizeof(addr);
@@ -101,6 +152,11 @@
 		port = ntohs(addr.sin_port);
 	}
 	unhand(this)->localPort = port;
+
+	DBG(NATIVENET,
+	    dprintf("  datagram_bind(%p, %s, -) -> (localPort: %d)\n", this,
+		    ip2str(unhand(laddr)->address), port);
+	    );
 }
 
 void
@@ -110,6 +166,11 @@
 	ssize_t bsent;
 	struct sockaddr_in addr;
 
+	DBG(NATIVENET,
+	    dprintf("datagram_send(%p, %p [%d bytes])\n",
+		    this, pkt, unhand(pkt)->length);
+	    );
+
 	memset(&addr, 0, sizeof(addr));
 #if defined(BSD44)
 	addr.sin_len = sizeof(addr);
@@ -118,7 +179,19 @@
 	addr.sin_port = htons(unhand(pkt)->port);
 	addr.sin_addr.s_addr = htonl(unhand(unhand(pkt)->address)->address);
 
-	rc = KSENDTO(unhand(unhand(this)->fd)->fd, unhand_array(unhand(pkt)->buf)->body, unhand(pkt)->length, 0, (struct sockaddr*)&addr, sizeof(addr), &bsent);
+	DBG(NATIVENET,
+	    dprintf("  datagram_send() to %s:%d\n",
+		    ip2str(unhand(unhand(pkt)->address)->address), unhand(pkt)->port);
+	    );
+
+	rc = KSENDTO(unhand(unhand(this)->fd)->fd,
+		     unhand_array(unhand(pkt)->buf)->body, unhand(pkt)->length,
+		     0, (struct sockaddr*)&addr, sizeof(addr), &bsent);
+
+	DBG(NATIVENET,
+	    dprintf("  datagram_send() -> rc=%d bsent=%d\n", rc, bsent);
+	    );
+
 	if (rc) {
 		SignalError("java.net.SocketException", SYS_ERROR(rc));
 	}
@@ -150,10 +223,22 @@
 	struct sockaddr_in addr;
 	int alen = sizeof(addr);
 
+	assert(this);
+	assert(pkt);
+
+	DBG(NATIVENET,
+	    dprintf("datagram_receive(%p, %p [%d bytes])\n",
+		    this, pkt, unhand(pkt)->length);
+	    );
+
 	/* Which port am I receiving from */
 	addr.sin_port = htons(unhand(this)->localPort);
 
-	rc = KRECVFROM(unhand(unhand(this)->fd)->fd, unhand_array(unhand(pkt)->buf)->body, unhand(pkt)->length, 0, (struct sockaddr*)&addr, &alen, unhand(this)->timeout, &r);
+	/* XXX should assert (unhand(pkt)->length <= unhand_array(unhand(pkt)->buf)->length), no? */
+
+	rc = KRECVFROM(unhand(unhand(this)->fd)->fd, unhand_array(unhand(pkt)->buf)->body,
+		       unhand(pkt)->length,
+		       0, (struct sockaddr*)&addr, &alen, unhand(this)->timeout, &r);
 	if (rc) {
 		SignalError("java.net.SocketException", SYS_ERROR(rc));
 	}
@@ -165,6 +250,13 @@
 	 * the new address from which the packet came.
 	 */
 	unhand(unhand(pkt)->address)->hostName = 0;
+
+	DBG(NATIVENET,
+ 	    dprintf("  datagram_receive(%p, %p) -> from %s:%d; brecv=%d\n",
+		    this, pkt,
+		    ip2str(ntohl(addr.sin_addr.s_addr)),
+		    ntohs(addr.sin_port), r);
+	    )
 }
 
 /*
@@ -174,6 +266,10 @@
 java_net_PlainDatagramSocketImpl_datagramSocketClose(struct Hjava_net_PlainDatagramSocketImpl* this)
 {
 	int r;
+
+	DBG(NATIVENET,
+	    dprintf("datagram_close(%p)\n", this);
+	    );
 
 	if (unhand(unhand(this)->fd)->fd != -1) {
 		r = KSOCKCLOSE(unhand(unhand(this)->fd)->fd);


More information about the kaffe mailing list