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