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