2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <arpa/inet.h>
23 #include <ccan/compiler/compiler.h>
25 #include "iscsi-private.h"
27 int iscsi_login_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data)
29 struct iscsi_pdu *pdu;
35 printf("trying to login on NULL context\n");
39 if (iscsi->is_loggedin != 0) {
40 printf("trying to login while already logged in\n");
44 switch (iscsi->session_type) {
45 case ISCSI_SESSION_DISCOVERY:
46 case ISCSI_SESSION_NORMAL:
49 printf("trying to login without setting session type\n");
53 pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_LOGIN_REQUEST, ISCSI_PDU_LOGIN_RESPONSE);
55 printf("Failed to allocate login pdu\n");
60 iscsi_pdu_set_immediate(pdu);
63 iscsi_pdu_set_pduflags(pdu, ISCSI_PDU_LOGIN_TRANSIT|ISCSI_PDU_LOGIN_CSG_OPNEG|ISCSI_PDU_LOGIN_NSG_FF);
67 if (asprintf(&astr, "InitiatorName=%s", iscsi->initiator_name) == -1) {
68 printf("asprintf failed\n");
69 iscsi_free_pdu(iscsi, pdu);
72 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)astr, strlen(astr)+1);
75 printf("pdu add data failed\n");
76 iscsi_free_pdu(iscsi, pdu);
82 if (asprintf(&astr, "InitiatorAlias=%s", iscsi->alias) == -1) {
83 printf("asprintf failed\n");
84 iscsi_free_pdu(iscsi, pdu);
87 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)astr, strlen(astr)+1);
90 printf("pdu add data failed\n");
91 iscsi_free_pdu(iscsi, pdu);
97 if (iscsi->session_type == ISCSI_SESSION_NORMAL) {
98 if (iscsi->target_name == NULL) {
99 printf("trying normal connect but target name not set\n");
100 iscsi_free_pdu(iscsi, pdu);
104 if (asprintf(&astr, "TargetName=%s", iscsi->target_name) == -1) {
105 printf("asprintf failed\n");
106 iscsi_free_pdu(iscsi, pdu);
109 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)astr, strlen(astr)+1);
112 printf("pdu add data failed\n");
113 iscsi_free_pdu(iscsi, pdu);
119 switch (iscsi->session_type) {
120 case ISCSI_SESSION_DISCOVERY:
121 str = "SessionType=Discovery";
123 case ISCSI_SESSION_NORMAL:
124 str = "SessionType=Normal";
127 printf("can not handle sessions %d yet\n", iscsi->session_type);
130 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
131 printf("pdu add data failed\n");
132 iscsi_free_pdu(iscsi, pdu);
136 str = "HeaderDigest=None";
137 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
138 printf("pdu add data failed\n");
139 iscsi_free_pdu(iscsi, pdu);
142 str = "DataDigest=None";
143 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
144 printf("pdu add data failed\n");
145 iscsi_free_pdu(iscsi, pdu);
148 str = "InitialR2T=Yes";
149 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
150 printf("pdu add data failed\n");
151 iscsi_free_pdu(iscsi, pdu);
154 str = "ImmediateData=Yes";
155 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
156 printf("pdu add data failed\n");
157 iscsi_free_pdu(iscsi, pdu);
160 str = "MaxBurstLength=262144";
161 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
162 printf("pdu add data failed\n");
163 iscsi_free_pdu(iscsi, pdu);
166 str = "FirstBurstLength=262144";
167 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
168 printf("pdu add data failed\n");
169 iscsi_free_pdu(iscsi, pdu);
172 str = "MaxRecvDataSegmentLength=262144";
173 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
174 printf("pdu add data failed\n");
175 iscsi_free_pdu(iscsi, pdu);
178 str = "DataPDUInOrder=Yes";
179 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
180 printf("pdu add data failed\n");
181 iscsi_free_pdu(iscsi, pdu);
184 str = "DataSequenceInOrder=Yes";
185 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
186 printf("pdu add data failed\n");
187 iscsi_free_pdu(iscsi, pdu);
193 pdu->private_data = private_data;
195 if (iscsi_queue_pdu(iscsi, pdu) != 0) {
196 printf("failed to queue iscsi login pdu\n");
197 iscsi_free_pdu(iscsi, pdu);
204 int iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
208 if (size < ISCSI_HEADER_SIZE) {
209 printf("don't have enough data to read status from login reply\n");
213 /* XXX here we should parse the data returned in case the target renegotiated some
215 * we should also do proper handshaking if the target is not yet prepared to transition
218 status = ntohs(*(uint16_t *)&hdr[36]);
220 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
224 iscsi->statsn = ntohs(*(uint16_t *)&hdr[24]);
226 iscsi->is_loggedin = 1;
227 pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
233 int iscsi_logout_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data)
235 struct iscsi_pdu *pdu;
238 printf("trying to logout on NULL context\n");
242 if (iscsi->is_loggedin == 0) {
243 printf("trying to logout while not logged in\n");
247 pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_LOGOUT_REQUEST, ISCSI_PDU_LOGOUT_RESPONSE);
249 printf("Failed to allocate logout pdu\n");
253 /* logout request has the immediate flag set */
254 iscsi_pdu_set_immediate(pdu);
256 /* flags : close the session */
257 iscsi_pdu_set_pduflags(pdu, 0x80);
261 pdu->private_data = private_data;
263 if (iscsi_queue_pdu(iscsi, pdu) != 0) {
264 printf("failed to queue iscsi logout pdu\n");
265 iscsi_free_pdu(iscsi, pdu);
272 int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size UNUSED)
274 iscsi->is_loggedin = 0;
275 pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
280 int iscsi_set_session_type(struct iscsi_context *iscsi, enum iscsi_session_type session_type)
283 printf("Trying to set sesssion type on NULL context\n");
286 if (iscsi->is_loggedin) {
287 printf("trying to set session type while logged in\n");
291 iscsi->session_type = session_type;