pdu.c revision 171568
1171568Sscottl/*-
2171568Sscottl * Copyright (c) 2005 Daniel Braniss <danny@cs.huji.ac.il>
3171568Sscottl * All rights reserved.
4171568Sscottl *
5171568Sscottl * Redistribution and use in source and binary forms, with or without
6171568Sscottl * modification, are permitted provided that the following conditions
7171568Sscottl * are met:
8171568Sscottl * 1. Redistributions of source code must retain the above copyright
9171568Sscottl *    notice, this list of conditions and the following disclaimer.
10171568Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11171568Sscottl *    notice, this list of conditions and the following disclaimer in the
12171568Sscottl *    documentation and/or other materials provided with the distribution.
13171568Sscottl *
14171568Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15171568Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16171568Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17171568Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18171568Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19171568Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20171568Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21171568Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22171568Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23171568Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24171568Sscottl * SUCH DAMAGE.
25171568Sscottl *
26171568Sscottl */
27171568Sscottl/*
28171568Sscottl | $Id: pdu.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
29171568Sscottl */
30171568Sscottl
31171568Sscottl#include <sys/cdefs.h>
32171568Sscottl__FBSDID("$FreeBSD: head/sbin/iscontrol/pdu.c 171568 2007-07-24 15:35:02Z scottl $");
33171568Sscottl
34171568Sscottl#include <sys/types.h>
35171568Sscottl#include <sys/time.h>
36171568Sscottl#include <sys/uio.h>
37171568Sscottl#include <sys/ioctl.h>
38171568Sscottl#include <unistd.h>
39171568Sscottl#include <stdlib.h>
40171568Sscottl#include <string.h>
41171568Sscottl#include <errno.h>
42171568Sscottl#include <stdio.h>
43171568Sscottl#include <stdarg.h>
44171568Sscottl#include <camlib.h>
45171568Sscottl
46171568Sscottl#include "iscsi.h"
47171568Sscottl#include "iscontrol.h"
48171568Sscottl#include "pdu.h"
49171568Sscottl
50171568Sscottlint
51171568Sscottlxmitpdu(isess_t *sess, pdu_t *pp)
52171568Sscottl{
53171568Sscottl     if(ioctl(sess->fd, ISCSISEND, pp)) {
54171568Sscottl	  perror("xmitpdu");
55171568Sscottl	  return -1;
56171568Sscottl     }
57171568Sscottl     if(vflag)
58171568Sscottl	  pukeText("I-", pp);
59171568Sscottl
60171568Sscottl     return 0;
61171568Sscottl}
62171568Sscottl
63171568Sscottlint
64171568Sscottlrecvpdu(isess_t *sess, pdu_t *pp)
65171568Sscottl{
66171568Sscottl     if(ioctl(sess->fd, ISCSIRECV, pp)) {
67171568Sscottl	  perror("recvpdu");
68171568Sscottl	  return -1;
69171568Sscottl     }
70171568Sscottl     // XXX: return error if truncated via
71171568Sscottl     // the FUDGE factor.
72171568Sscottl     if(vflag)
73171568Sscottl	  pukeText("T-", pp);
74171568Sscottl
75171568Sscottl     return 0;
76171568Sscottl}
77171568Sscottl
78171568Sscottlint
79171568SscottlsendPDU(isess_t *sess, pdu_t *pp, handler_t *hdlr)
80171568Sscottl{
81171568Sscottl     if(xmitpdu(sess, pp))
82171568Sscottl	  return 0;
83171568Sscottl     if(hdlr) {
84171568Sscottl	  int res;
85171568Sscottl
86171568Sscottl	  pp->ahs_size = 8 * 1024;
87171568Sscottl	  if((pp->ahs = malloc(pp->ahs_size)) == NULL) {
88171568Sscottl	       fprintf(stderr, "out of mem!");
89171568Sscottl	       return -1;
90171568Sscottl	  }
91171568Sscottl	  pp->ds_size = 0;
92171568Sscottl	  if((res = recvpdu(sess, pp)) != 0) {
93171568Sscottl	       fprintf(stderr, "recvpdu failed\n");
94171568Sscottl	       return res;
95171568Sscottl	  }
96171568Sscottl	  res = hdlr(sess, pp);
97171568Sscottl	  freePDU(pp);
98171568Sscottl	  return res;
99171568Sscottl     }
100171568Sscottl     return 1;
101171568Sscottl}
102171568Sscottl
103171568Sscottl
104171568Sscottl#define FUDGE (512 * 8)
105171568Sscottl/*
106171568Sscottl | We use the same memory for the response
107171568Sscottl | so make enough room ...
108171568Sscottl | XXX: must find a better way.
109171568Sscottl */
110171568Sscottlint
111171568SscottladdText(pdu_t *pp, char *fmt, ...)
112171568Sscottl{
113171568Sscottl     u_int	len;
114171568Sscottl     char	*str;
115171568Sscottl     va_list	ap;
116171568Sscottl
117171568Sscottl     va_start(ap, fmt);
118171568Sscottl     len = vasprintf(&str, fmt, ap) + 1;
119171568Sscottl     if((pp->ds_len + len) > 0xffffff) {
120171568Sscottl	  printf("ds overflow\n");
121171568Sscottl	  free(str);
122171568Sscottl	  return 0;
123171568Sscottl     }
124171568Sscottl
125171568Sscottl     if((pp->ds_len + len) > pp->ds_size) {
126171568Sscottl	  u_char	*np;
127171568Sscottl
128171568Sscottl	  np = realloc(pp->ds, pp->ds_size + len + FUDGE);
129171568Sscottl	  if(np == NULL) {
130171568Sscottl	       free(str);
131171568Sscottl	       //XXX: out of memory!
132171568Sscottl	       return -1;
133171568Sscottl	  }
134171568Sscottl	  pp->ds = np;
135171568Sscottl	  pp->ds_size += len + FUDGE;
136171568Sscottl     }
137171568Sscottl     memcpy(pp->ds + pp->ds_len, str, len);
138171568Sscottl     pp->ds_len += len;
139171568Sscottl     free(str);
140171568Sscottl     return len;
141171568Sscottl}
142171568Sscottl
143171568Sscottlvoid
144171568SscottlfreePDU(pdu_t *pp)
145171568Sscottl{
146171568Sscottl     if(pp->ahs_size)
147171568Sscottl	  free(pp->ahs);
148171568Sscottl     if(pp->ds_size)
149171568Sscottl	  free(pp->ds);
150171568Sscottl     bzero(&pp->ipdu, sizeof(union ipdu_u));
151171568Sscottl     pp->ahs = NULL;
152171568Sscottl     pp->ds = NULL;
153171568Sscottl     pp->ahs_size = 0;
154171568Sscottl     pp->ds_size = pp->ds_len = 0;
155171568Sscottl}
156171568Sscottl
157171568Sscottlvoid
158171568SscottlpukeText(char *it, pdu_t *pp)
159171568Sscottl{
160171568Sscottl     char	*ptr;
161171568Sscottl     int	cmd;
162171568Sscottl     size_t	len, n;
163171568Sscottl
164171568Sscottl     len = pp->ds_len;
165171568Sscottl     ptr = (char *)pp->ds;
166171568Sscottl     cmd = pp->ipdu.bhs.opcode;
167171568Sscottl
168171568Sscottl     printf("%s: cmd=0x%x len=%d\n", it, cmd, (int)len);
169171568Sscottl     while(len > 0) {
170171568Sscottl	  printf("\t%s\n", ptr);
171171568Sscottl	  n = strlen(ptr) + 1;
172171568Sscottl	  len -= n;
173171568Sscottl	  ptr += n;
174171568Sscottl     }
175171568Sscottl}
176