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