Commit Diff


commit - de1c918bdb355ca73dcb897d862081210a8967a7
commit + 64c19d90dad12b169e42f2c019f1662a4a00fab7
blob - 562c5d49b33de6220b4128a0b2c2286238ba308e
blob + 27c12f77ce32b6470060ab6849edbc13b5ba261a
--- client.c
+++ client.c
@@ -18,6 +18,7 @@
 
 #include <sys/stat.h>
 
+#include <ctype.h>
 #include <endian.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -107,6 +108,8 @@ static void	tclunk(struct np_msg_header *, const uint8
 static void	tflush(struct np_msg_header *, const uint8_t *, size_t);
 static void	handle_message(struct imsg *, size_t);
 
+void		client_hexdump(const char *, uint8_t *data, size_t len);
+
 ATTR_DEAD void
 client(int debug, int verbose)
 {
@@ -476,11 +479,16 @@ np_qid(struct qid *qid)
 static void
 do_send(void)
 {
-	size_t len;
+	size_t	 len;
+	void	*data;
 
 	len = EVBUFFER_LENGTH(evb);
-	log_debug("sending a packet long %zu bytes", len);
-	client_send_listener(IMSG_BUF, EVBUFFER_DATA(evb), len);
+	data = EVBUFFER_DATA(evb);
+
+#if DEBUG_PACKETS
+	client_hexdump("outgoing packet", data, len);
+#endif
+	client_send_listener(IMSG_BUF, data, len);
 	evbuffer_drain(evb, len);
 }
 
@@ -755,6 +763,10 @@ handle_message(struct imsg *imsg, size_t len)
 	struct np_msg_header	 hdr;
 	size_t			 i;
 	uint8_t			*data;
+
+#if DEBUG_PACKETS
+	client_hexdump("incoming packet", imsg->data, len);
+#endif
 
 	parse_message(imsg->data, len, &hdr, &data);
 	len -= HEADERSIZE;
@@ -762,18 +774,6 @@ handle_message(struct imsg *imsg, size_t len)
 	log_debug("got request: len=%d type=%d[%s] tag=%d",
 	    hdr.len, hdr.type, pp_msg_type(hdr.type), hdr.tag);
 
-#if DEBUG_PACKETS
-	printf("\nhexdump:\n");
-	for (i = 0; i < len; ++i) {
-		if (i != 0 && i % 8 == 0)
-			printf(" ");
-		if (i != 0 && i % 16 == 0)
-			printf("\n");
-		printf("%02x ", data[i]);
-	}
-	printf("\n\n");
-#endif
-
 	if (!handshaked && hdr.type != Tversion) {
 		client_send_listener(IMSG_CLOSE, NULL, 0);
 		client_shutdown();
@@ -789,4 +789,60 @@ handle_message(struct imsg *imsg, size_t len)
 	}
 
 	np_error(hdr.tag, "Not supported.");
+}
+
+static void
+hexdump_ppline(int x, uint8_t *data, size_t len)
+{
+	for (; x < 50; x++)
+		printf(" ");
+
+	printf("|");
+
+	for (x = 0; x < (int)len; ++x) {
+		if (isgraph(data[x]))
+			printf("%c", data[x]);
+		else
+			printf(".");
+	}
+
+	printf("|\n");
 }
+
+void
+client_hexdump(const char *label, uint8_t *data, size_t len)
+{
+	size_t	i;
+	int	x, n;
+
+	/*
+	 * Layout:
+	 * === first block === == second block ==  |........|\n
+	 * first and second block are 8 bytes long (for a total of 48
+	 * columns), plus two separator plus two | plus 16 chars, for
+	 * a total of 68 characters.
+	 */
+
+	printf("\nhexdump \"%s\": (%zu bytes)\n", label, len);
+	for (x = 0, n = 0, i = 0; i < len; ++i) {
+		if (i != 0 && i % 8 == 0) {
+			printf(" ");
+			x++;
+		}
+
+		if (n == 16) {
+			hexdump_ppline(x, &data[i - 16], 16);
+			x = 0;
+			n = 0;
+		}
+
+		printf("%02x ", data[i]);
+		x += 3;
+		n++;
+	}
+
+	if (n != 0)
+                hexdump_ppline(x, &data[i - n], n);
+
+	printf("\n");
+}