1 /* $OpenBSD: logmsg.c,v 1.1 2016/09/02 16:22:31 benno Exp $ */
2
3 /*
4 * Copyright (c) 2009 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <arpa/inet.h>
21 #include <endian.h>
22 #include <event.h>
23 #include <scsi/iscsi.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/queue.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29
30 #include "iscsid.h"
31 #include "log.h"
32
33 extern int debug;
34 extern int verbose;
35
36 void
log_hexdump(void * buf,size_t len)37 log_hexdump(void *buf, size_t len)
38 {
39 u_char b[16];
40 size_t i, j, l;
41
42 if (!debug)
43 return;
44
45 for (i = 0; i < len; i += l) {
46 fprintf(stderr, "%4zi:", i);
47 l = sizeof(b) < len - i ? sizeof(b) : len - i;
48 memcpy(b, (char *)buf + i, l);
49
50 for (j = 0; j < sizeof(b); j++) {
51 if (j % 2 == 0)
52 fprintf(stderr, " ");
53 if (j % 8 == 0)
54 fprintf(stderr, " ");
55 if (j < l)
56 fprintf(stderr, "%02x", (int)b[j]);
57 else
58 fprintf(stderr, " ");
59 }
60 fprintf(stderr, " |");
61 for (j = 0; j < l; j++) {
62 if (b[j] >= 0x20 && b[j] <= 0x7e)
63 fprintf(stderr, "%c", b[j]);
64 else
65 fprintf(stderr, ".");
66 }
67 fprintf(stderr, "|\n");
68 }
69 }
70
71 void
log_pdu(struct pdu * p,int all)72 log_pdu(struct pdu *p, int all)
73 {
74 struct iscsi_pdu *pdu;
75 void *b;
76 size_t s;
77
78 if (!debug)
79 return;
80
81 if (!(pdu = pdu_getbuf(p, NULL, PDU_HEADER))) {
82 log_debug("empty pdu");
83 return;
84 }
85
86 fprintf(stderr, "PDU: op %x%s flags %02x%02x%02x ahs %d len %d\n",
87 ISCSI_PDU_OPCODE(pdu->opcode), ISCSI_PDU_I(pdu) ? " I" : "",
88 pdu->flags, pdu->_reserved1[0], pdu->_reserved1[1],
89 pdu->ahslen, pdu->datalen[0] << 16 | pdu->datalen[1] << 8 |
90 pdu->datalen[2]);
91 fprintf(stderr, " lun %02x%02x%02x%02x%02x%02x%02x%02x itt %u "
92 "cmdsn %u expstatsn %u\n", pdu->lun[0], pdu->lun[1], pdu->lun[2],
93 pdu->lun[3], pdu->lun[4], pdu->lun[5], pdu->lun[6], pdu->lun[7],
94 ntohl(pdu->itt), ntohl(pdu->cmdsn), ntohl(pdu->expstatsn));
95 log_hexdump(pdu, sizeof(*pdu));
96
97 if (all && (b = pdu_getbuf(p, &s, PDU_DATA)))
98 log_hexdump(b, s);
99 }
100