1*e25c779eSMatthew Dillon /*-
2*e25c779eSMatthew Dillon * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3*e25c779eSMatthew Dillon * All rights reserved.
4*e25c779eSMatthew Dillon *
5*e25c779eSMatthew Dillon * Redistribution and use in source and binary forms, with or without
6*e25c779eSMatthew Dillon * modification, are permitted provided that the following conditions
7*e25c779eSMatthew Dillon * are met:
8*e25c779eSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
9*e25c779eSMatthew Dillon * notice, this list of conditions and the following disclaimer.
10*e25c779eSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
11*e25c779eSMatthew Dillon * notice, this list of conditions and the following disclaimer in the
12*e25c779eSMatthew Dillon * documentation and/or other materials provided with the distribution.
13*e25c779eSMatthew Dillon *
14*e25c779eSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*e25c779eSMatthew Dillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*e25c779eSMatthew Dillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*e25c779eSMatthew Dillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*e25c779eSMatthew Dillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*e25c779eSMatthew Dillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*e25c779eSMatthew Dillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*e25c779eSMatthew Dillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*e25c779eSMatthew Dillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*e25c779eSMatthew Dillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*e25c779eSMatthew Dillon * SUCH DAMAGE.
25*e25c779eSMatthew Dillon *
26*e25c779eSMatthew Dillon */
27*e25c779eSMatthew Dillon /*
28*e25c779eSMatthew Dillon | $Id: pdu.c,v 2.2 2006/12/01 09:11:56 danny Exp danny $
29*e25c779eSMatthew Dillon */
30*e25c779eSMatthew Dillon
31*e25c779eSMatthew Dillon #include <sys/types.h>
32*e25c779eSMatthew Dillon #include <sys/time.h>
33*e25c779eSMatthew Dillon #include <sys/uio.h>
34*e25c779eSMatthew Dillon #include <sys/ioctl.h>
35*e25c779eSMatthew Dillon #include <unistd.h>
36*e25c779eSMatthew Dillon #include <stdlib.h>
37*e25c779eSMatthew Dillon #include <string.h>
38*e25c779eSMatthew Dillon #include <errno.h>
39*e25c779eSMatthew Dillon #include <stdio.h>
40*e25c779eSMatthew Dillon #include <stdarg.h>
41*e25c779eSMatthew Dillon #include <camlib.h>
42*e25c779eSMatthew Dillon
43*e25c779eSMatthew Dillon #include "iscsi.h"
44*e25c779eSMatthew Dillon #include "iscontrol.h"
45*e25c779eSMatthew Dillon
46*e25c779eSMatthew Dillon int
xmitpdu(isess_t * sess,pdu_t * pp)47*e25c779eSMatthew Dillon xmitpdu(isess_t *sess, pdu_t *pp)
48*e25c779eSMatthew Dillon {
49*e25c779eSMatthew Dillon if(ioctl(sess->fd, ISCSISEND, pp)) {
50*e25c779eSMatthew Dillon perror("xmitpdu");
51*e25c779eSMatthew Dillon return -1;
52*e25c779eSMatthew Dillon }
53*e25c779eSMatthew Dillon if(vflag)
54*e25c779eSMatthew Dillon pukeText("I-", pp);
55*e25c779eSMatthew Dillon
56*e25c779eSMatthew Dillon return 0;
57*e25c779eSMatthew Dillon }
58*e25c779eSMatthew Dillon
59*e25c779eSMatthew Dillon int
recvpdu(isess_t * sess,pdu_t * pp)60*e25c779eSMatthew Dillon recvpdu(isess_t *sess, pdu_t *pp)
61*e25c779eSMatthew Dillon {
62*e25c779eSMatthew Dillon if(ioctl(sess->fd, ISCSIRECV, pp)) {
63*e25c779eSMatthew Dillon perror("recvpdu");
64*e25c779eSMatthew Dillon return -1;
65*e25c779eSMatthew Dillon }
66*e25c779eSMatthew Dillon // XXX: return error if truncated via
67*e25c779eSMatthew Dillon // the FUDGE factor.
68*e25c779eSMatthew Dillon if(vflag)
69*e25c779eSMatthew Dillon pukeText("T-", pp);
70*e25c779eSMatthew Dillon
71*e25c779eSMatthew Dillon return 0;
72*e25c779eSMatthew Dillon }
73*e25c779eSMatthew Dillon
74*e25c779eSMatthew Dillon int
sendPDU(isess_t * sess,pdu_t * pp,handler_t * hdlr)75*e25c779eSMatthew Dillon sendPDU(isess_t *sess, pdu_t *pp, handler_t *hdlr)
76*e25c779eSMatthew Dillon {
77*e25c779eSMatthew Dillon if(xmitpdu(sess, pp))
78*e25c779eSMatthew Dillon return 0;
79*e25c779eSMatthew Dillon if(hdlr) {
80*e25c779eSMatthew Dillon int res;
81*e25c779eSMatthew Dillon
82*e25c779eSMatthew Dillon pp->ahs_size = 8 * 1024;
83*e25c779eSMatthew Dillon if((pp->ahs = malloc(pp->ahs_size)) == NULL) {
84*e25c779eSMatthew Dillon fprintf(stderr, "out of mem!");
85*e25c779eSMatthew Dillon return -1;
86*e25c779eSMatthew Dillon }
87*e25c779eSMatthew Dillon pp->ds_size = 0;
88*e25c779eSMatthew Dillon if((res = recvpdu(sess, pp)) != 0) {
89*e25c779eSMatthew Dillon fprintf(stderr, "recvpdu failed\n");
90*e25c779eSMatthew Dillon return res;
91*e25c779eSMatthew Dillon }
92*e25c779eSMatthew Dillon res = hdlr(sess, pp);
93*e25c779eSMatthew Dillon freePDU(pp);
94*e25c779eSMatthew Dillon return res;
95*e25c779eSMatthew Dillon }
96*e25c779eSMatthew Dillon return 1;
97*e25c779eSMatthew Dillon }
98*e25c779eSMatthew Dillon
99*e25c779eSMatthew Dillon
100*e25c779eSMatthew Dillon #define FUDGE (512 * 8)
101*e25c779eSMatthew Dillon /*
102*e25c779eSMatthew Dillon | We use the same memory for the response
103*e25c779eSMatthew Dillon | so make enough room ...
104*e25c779eSMatthew Dillon | XXX: must find a better way.
105*e25c779eSMatthew Dillon */
106*e25c779eSMatthew Dillon int
addText(pdu_t * pp,char * fmt,...)107*e25c779eSMatthew Dillon addText(pdu_t *pp, char *fmt, ...)
108*e25c779eSMatthew Dillon {
109*e25c779eSMatthew Dillon u_int len;
110*e25c779eSMatthew Dillon char *str;
111*e25c779eSMatthew Dillon va_list ap;
112*e25c779eSMatthew Dillon
113*e25c779eSMatthew Dillon va_start(ap, fmt);
114*e25c779eSMatthew Dillon len = vasprintf(&str, fmt, ap) + 1;
115*e25c779eSMatthew Dillon if((pp->ds_len + len) > 0xffffff) {
116*e25c779eSMatthew Dillon printf("ds overflow\n");
117*e25c779eSMatthew Dillon free(str);
118*e25c779eSMatthew Dillon return 0;
119*e25c779eSMatthew Dillon }
120*e25c779eSMatthew Dillon
121*e25c779eSMatthew Dillon if((pp->ds_len + len) > pp->ds_size) {
122*e25c779eSMatthew Dillon u_char *np;
123*e25c779eSMatthew Dillon
124*e25c779eSMatthew Dillon np = realloc(pp->ds, pp->ds_size + len + FUDGE);
125*e25c779eSMatthew Dillon if(np == NULL) {
126*e25c779eSMatthew Dillon free(str);
127*e25c779eSMatthew Dillon //XXX: out of memory!
128*e25c779eSMatthew Dillon return -1;
129*e25c779eSMatthew Dillon }
130*e25c779eSMatthew Dillon pp->ds = np;
131*e25c779eSMatthew Dillon pp->ds_size += len + FUDGE;
132*e25c779eSMatthew Dillon }
133*e25c779eSMatthew Dillon memcpy(pp->ds + pp->ds_len, str, len);
134*e25c779eSMatthew Dillon pp->ds_len += len;
135*e25c779eSMatthew Dillon free(str);
136*e25c779eSMatthew Dillon return len;
137*e25c779eSMatthew Dillon }
138*e25c779eSMatthew Dillon
139*e25c779eSMatthew Dillon void
freePDU(pdu_t * pp)140*e25c779eSMatthew Dillon freePDU(pdu_t *pp)
141*e25c779eSMatthew Dillon {
142*e25c779eSMatthew Dillon if(pp->ahs_size)
143*e25c779eSMatthew Dillon free(pp->ahs);
144*e25c779eSMatthew Dillon if(pp->ds_size)
145*e25c779eSMatthew Dillon free(pp->ds);
146*e25c779eSMatthew Dillon bzero(&pp->ipdu, sizeof(union ipdu_u));
147*e25c779eSMatthew Dillon pp->ahs = NULL;
148*e25c779eSMatthew Dillon pp->ds = NULL;
149*e25c779eSMatthew Dillon pp->ahs_size = 0;
150*e25c779eSMatthew Dillon pp->ds_size = pp->ds_len = 0;
151*e25c779eSMatthew Dillon }
152*e25c779eSMatthew Dillon
153*e25c779eSMatthew Dillon void
pukeText(char * it,pdu_t * pp)154*e25c779eSMatthew Dillon pukeText(char *it, pdu_t *pp)
155*e25c779eSMatthew Dillon {
156*e25c779eSMatthew Dillon char *ptr;
157*e25c779eSMatthew Dillon int cmd;
158*e25c779eSMatthew Dillon size_t len, n;
159*e25c779eSMatthew Dillon
160*e25c779eSMatthew Dillon len = pp->ds_len;
161*e25c779eSMatthew Dillon ptr = (char *)pp->ds;
162*e25c779eSMatthew Dillon cmd = pp->ipdu.bhs.opcode;
163*e25c779eSMatthew Dillon
164*e25c779eSMatthew Dillon printf("%s: cmd=0x%x len=%d\n", it, cmd, (int)len);
165*e25c779eSMatthew Dillon while(len > 0) {
166*e25c779eSMatthew Dillon printf("\t%s\n", ptr);
167*e25c779eSMatthew Dillon n = strlen(ptr) + 1;
168*e25c779eSMatthew Dillon len -= n;
169*e25c779eSMatthew Dillon ptr += n;
170*e25c779eSMatthew Dillon }
171*e25c779eSMatthew Dillon }
172