comparison network.c @ 434:691400f1c9bb

Add function for dumping current network buffer.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 25 May 2012 20:18:24 +0300
parents edd67b882271
children 3be600124da8
comparison
equal deleted inserted replaced
433:edd67b882271 434:691400f1c9bb
548 tmp = th_strdup_vprintf(fmt, ap); 548 tmp = th_strdup_vprintf(fmt, ap);
549 va_end(ap); 549 va_end(ap);
550 550
551 return nn_conn_send_msg(conn, user, tmp); 551 return nn_conn_send_msg(conn, user, tmp);
552 } 552 }
553
554
555 void nn_conn_dump_buffer(FILE *f, nn_conn_t *conn)
556 {
557 char *p;
558 size_t offs, left;
559
560 fprintf(f,
561 "\n--------------------------------------------------------------\n"
562 "err=%d, status=%d, got_bytes=%d, total_bytes=%d\n"
563 "buf=0x%p, in_ptr=0x%04x, ptr=0x%04x\n",
564 conn->err, conn->status, conn->got_bytes, conn->total_bytes,
565 conn->buf, conn->in_ptr - conn->buf, conn->ptr - conn->buf);
566
567 // Dump buffer contents as a hexdump
568 for (offs = 0, left = conn->total_bytes, p = conn->buf; p < conn->in_ptr;)
569 {
570 char buf[NN_DUMP_BYTES + 1];
571 size_t bufoffs, amount = left < NN_DUMP_BYTES ? left : NN_DUMP_BYTES;
572 left -= amount;
573
574 // Dump offs | xx xx xx xx | and fill string
575 fprintf(f, "%04x | ", offs);
576 for (bufoffs = 0; bufoffs < amount; offs++, bufoffs++, p++)
577 {
578 fprintf(f, "%02x ", *p);
579 buf[bufoffs] = th_isprint(*p) ? *p : '.';
580 }
581 buf[bufoffs] = 0;
582
583 // Add padding
584 for (; bufoffs < NN_DUMP_BYTES; bufoffs++)
585 fprintf(f, " ");
586
587 // Print string
588 fprintf(f, "| %s\n", buf);
589 }
590 }