]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sbin/iscontrol/pdu.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sbin / iscontrol / pdu.c
1 /*-
2  * Copyright (c) 2005-2010 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 <dev/iscsi_initiator/iscsi.h>
47 #include "iscontrol.h"
48
49 static void     pukeText(char *it, pdu_t *pp);
50
51 int
52 xmitpdu(isess_t *sess, pdu_t *pp)
53 {
54      if(ioctl(sess->fd, ISCSISEND, pp)) {
55           perror("xmitpdu");
56           return -1;
57      }
58      if(vflag)
59           pukeText("I-", pp);
60
61      return 0;
62 }
63
64 int
65 recvpdu(isess_t *sess, pdu_t *pp)
66 {
67      if(ioctl(sess->fd, ISCSIRECV, pp)) {
68           perror("recvpdu");
69           return -1;
70      }
71      // XXX: return error if truncated via
72      // the FUDGE factor.
73      if(vflag)
74           pukeText("T-", pp);
75
76      return 0;
77 }
78
79 int
80 sendPDU(isess_t *sess, pdu_t *pp, handler_t *hdlr)
81 {
82      if(xmitpdu(sess, pp))
83           return 0;
84      if(hdlr) {
85           int res;
86
87           pp->ahs_size = 8 * 1024;
88           if((pp->ahs_addr = malloc(pp->ahs_size)) == NULL) {
89                fprintf(stderr, "out of mem!");
90                return -1;
91           }
92           pp->ds_size = 0;
93           if((res = recvpdu(sess, pp)) != 0) {
94                fprintf(stderr, "recvpdu failed\n");
95                return res;
96           }
97           res = hdlr(sess, pp);
98           freePDU(pp);
99           return res;
100      }
101      return 1;
102 }
103
104
105 #define FUDGE (512 * 8)
106 /*
107  | We use the same memory for the response
108  | so make enough room ...
109  | XXX: must find a better way.
110  */
111 int
112 addText(pdu_t *pp, char *fmt, ...)
113 {
114      u_int      len;
115      char       *str;
116      va_list    ap;
117
118      va_start(ap, fmt);
119      len = vasprintf(&str, fmt, ap) + 1;
120      if((pp->ds_len + len) > 0xffffff) {
121           printf("ds overflow\n");
122           free(str);
123           return 0;
124      }
125
126      if((pp->ds_len + len) > pp->ds_size) {
127           u_char        *np;
128
129           np = realloc(pp->ds_addr, pp->ds_size + len + FUDGE);
130           if(np == NULL) {
131                free(str);
132                //XXX: out of memory!
133                return -1;
134           }
135           pp->ds_addr = np;
136           pp->ds_size += len + FUDGE;
137      }
138      memcpy(pp->ds_addr + pp->ds_len, str, len);
139      pp->ds_len += len;
140      free(str);
141      return len;
142 }
143
144 void
145 freePDU(pdu_t *pp)
146 {
147      if(pp->ahs_size)
148           free(pp->ahs_addr);
149      if(pp->ds_size)
150           free(pp->ds_addr);
151      bzero(&pp->ipdu, sizeof(union ipdu_u));
152      pp->ahs_addr = NULL;
153      pp->ds_addr = NULL;
154      pp->ahs_size = 0;
155      pp->ds_size = pp->ds_len = 0;
156 }
157
158 static void
159 pukeText(char *it, pdu_t *pp)
160 {
161      char       *ptr;
162      int        cmd;
163      size_t     len, n;
164
165      len = pp->ds_len;
166      ptr = (char *)pp->ds_addr;
167      cmd = pp->ipdu.bhs.opcode;
168
169      printf("%s: cmd=0x%x len=%d\n", it, cmd, (int)len);
170      while(len > 0) {
171           printf("\t%s\n", ptr);
172           n = strlen(ptr) + 1;
173           len -= n;
174           ptr += n;
175      }
176 }