1171568Sscottl/*-
2211095Sdes * Copyright (c) 2005-2010 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$");
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
46211095Sdes#include <dev/iscsi/initiator/iscsi.h>
47171568Sscottl#include "iscontrol.h"
48171568Sscottl
49203460Sdelphijstatic void	pukeText(char *it, pdu_t *pp);
50203460Sdelphij
51171568Sscottlint
52171568Sscottlxmitpdu(isess_t *sess, pdu_t *pp)
53171568Sscottl{
54171568Sscottl     if(ioctl(sess->fd, ISCSISEND, pp)) {
55171568Sscottl	  perror("xmitpdu");
56171568Sscottl	  return -1;
57171568Sscottl     }
58171568Sscottl     if(vflag)
59171568Sscottl	  pukeText("I-", pp);
60171568Sscottl
61171568Sscottl     return 0;
62171568Sscottl}
63171568Sscottl
64171568Sscottlint
65171568Sscottlrecvpdu(isess_t *sess, pdu_t *pp)
66171568Sscottl{
67171568Sscottl     if(ioctl(sess->fd, ISCSIRECV, pp)) {
68171568Sscottl	  perror("recvpdu");
69171568Sscottl	  return -1;
70171568Sscottl     }
71171568Sscottl     // XXX: return error if truncated via
72171568Sscottl     // the FUDGE factor.
73171568Sscottl     if(vflag)
74171568Sscottl	  pukeText("T-", pp);
75171568Sscottl
76171568Sscottl     return 0;
77171568Sscottl}
78171568Sscottl
79171568Sscottlint
80171568SscottlsendPDU(isess_t *sess, pdu_t *pp, handler_t *hdlr)
81171568Sscottl{
82171568Sscottl     if(xmitpdu(sess, pp))
83171568Sscottl	  return 0;
84171568Sscottl     if(hdlr) {
85171568Sscottl	  int res;
86171568Sscottl
87171568Sscottl	  pp->ahs_size = 8 * 1024;
88211095Sdes	  if((pp->ahs_addr = malloc(pp->ahs_size)) == NULL) {
89171568Sscottl	       fprintf(stderr, "out of mem!");
90171568Sscottl	       return -1;
91171568Sscottl	  }
92171568Sscottl	  pp->ds_size = 0;
93171568Sscottl	  if((res = recvpdu(sess, pp)) != 0) {
94171568Sscottl	       fprintf(stderr, "recvpdu failed\n");
95171568Sscottl	       return res;
96171568Sscottl	  }
97171568Sscottl	  res = hdlr(sess, pp);
98171568Sscottl	  freePDU(pp);
99171568Sscottl	  return res;
100171568Sscottl     }
101171568Sscottl     return 1;
102171568Sscottl}
103171568Sscottl
104171568Sscottl
105171568Sscottl#define FUDGE (512 * 8)
106171568Sscottl/*
107171568Sscottl | We use the same memory for the response
108171568Sscottl | so make enough room ...
109171568Sscottl | XXX: must find a better way.
110171568Sscottl */
111171568Sscottlint
112171568SscottladdText(pdu_t *pp, char *fmt, ...)
113171568Sscottl{
114171568Sscottl     u_int	len;
115171568Sscottl     char	*str;
116171568Sscottl     va_list	ap;
117171568Sscottl
118171568Sscottl     va_start(ap, fmt);
119171568Sscottl     len = vasprintf(&str, fmt, ap) + 1;
120171568Sscottl     if((pp->ds_len + len) > 0xffffff) {
121171568Sscottl	  printf("ds overflow\n");
122171568Sscottl	  free(str);
123171568Sscottl	  return 0;
124171568Sscottl     }
125171568Sscottl
126171568Sscottl     if((pp->ds_len + len) > pp->ds_size) {
127171568Sscottl	  u_char	*np;
128171568Sscottl
129211095Sdes	  np = realloc(pp->ds_addr, pp->ds_size + len + FUDGE);
130171568Sscottl	  if(np == NULL) {
131171568Sscottl	       free(str);
132171568Sscottl	       //XXX: out of memory!
133171568Sscottl	       return -1;
134171568Sscottl	  }
135211095Sdes	  pp->ds_addr = np;
136171568Sscottl	  pp->ds_size += len + FUDGE;
137171568Sscottl     }
138211095Sdes     memcpy(pp->ds_addr + pp->ds_len, str, len);
139171568Sscottl     pp->ds_len += len;
140171568Sscottl     free(str);
141171568Sscottl     return len;
142171568Sscottl}
143171568Sscottl
144171568Sscottlvoid
145171568SscottlfreePDU(pdu_t *pp)
146171568Sscottl{
147171568Sscottl     if(pp->ahs_size)
148211095Sdes	  free(pp->ahs_addr);
149171568Sscottl     if(pp->ds_size)
150211095Sdes	  free(pp->ds_addr);
151171568Sscottl     bzero(&pp->ipdu, sizeof(union ipdu_u));
152211095Sdes     pp->ahs_addr = NULL;
153211095Sdes     pp->ds_addr = NULL;
154171568Sscottl     pp->ahs_size = 0;
155171568Sscottl     pp->ds_size = pp->ds_len = 0;
156171568Sscottl}
157171568Sscottl
158203460Sdelphijstatic void
159171568SscottlpukeText(char *it, pdu_t *pp)
160171568Sscottl{
161171568Sscottl     char	*ptr;
162171568Sscottl     int	cmd;
163171568Sscottl     size_t	len, n;
164171568Sscottl
165171568Sscottl     len = pp->ds_len;
166211095Sdes     ptr = (char *)pp->ds_addr;
167171568Sscottl     cmd = pp->ipdu.bhs.opcode;
168171568Sscottl
169171568Sscottl     printf("%s: cmd=0x%x len=%d\n", it, cmd, (int)len);
170171568Sscottl     while(len > 0) {
171171568Sscottl	  printf("\t%s\n", ptr);
172171568Sscottl	  n = strlen(ptr) + 1;
173171568Sscottl	  len -= n;
174171568Sscottl	  ptr += n;
175171568Sscottl     }
176171568Sscottl}
177