Deleted Added
full compact
pdu.c (203460) pdu.c (211095)
1/*-
1/*-
2 * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
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>
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: head/sbin/iscontrol/pdu.c 203460 2010-02-03 23:58:00Z delphij $");
32__FBSDID("$FreeBSD: head/sbin/iscontrol/pdu.c 211095 2010-08-09 12:36:36Z des $");
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
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"
46#include <dev/iscsi/initiator/iscsi.h>
47#include "iscontrol.h"
48
49static void pukeText(char *it, pdu_t *pp);
50
51int
52xmitpdu(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
64int
65recvpdu(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
79int
80sendPDU(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;
47#include "iscontrol.h"
48
49static void pukeText(char *it, pdu_t *pp);
50
51int
52xmitpdu(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
64int
65recvpdu(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
79int
80sendPDU(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 = malloc(pp->ahs_size)) == NULL) {
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 */
111int
112addText(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
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 */
111int
112addText(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, pp->ds_size + len + FUDGE);
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 }
130 if(np == NULL) {
131 free(str);
132 //XXX: out of memory!
133 return -1;
134 }
135 pp->ds = np;
135 pp->ds_addr = np;
136 pp->ds_size += len + FUDGE;
137 }
136 pp->ds_size += len + FUDGE;
137 }
138 memcpy(pp->ds + pp->ds_len, str, len);
138 memcpy(pp->ds_addr + pp->ds_len, str, len);
139 pp->ds_len += len;
140 free(str);
141 return len;
142}
143
144void
145freePDU(pdu_t *pp)
146{
147 if(pp->ahs_size)
139 pp->ds_len += len;
140 free(str);
141 return len;
142}
143
144void
145freePDU(pdu_t *pp)
146{
147 if(pp->ahs_size)
148 free(pp->ahs);
148 free(pp->ahs_addr);
149 if(pp->ds_size)
149 if(pp->ds_size)
150 free(pp->ds);
150 free(pp->ds_addr);
151 bzero(&pp->ipdu, sizeof(union ipdu_u));
151 bzero(&pp->ipdu, sizeof(union ipdu_u));
152 pp->ahs = NULL;
153 pp->ds = NULL;
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
158static void
159pukeText(char *it, pdu_t *pp)
160{
161 char *ptr;
162 int cmd;
163 size_t len, n;
164
165 len = pp->ds_len;
154 pp->ahs_size = 0;
155 pp->ds_size = pp->ds_len = 0;
156}
157
158static void
159pukeText(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;
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}
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}