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;
34 printf("trying to login on NULL context\n");
38 if (iscsi->is_loggedin != 0) {
39 printf("trying to login while already logged in\n");
43 switch (iscsi->session_type) {
44 case ISCSI_SESSION_DISCOVERY:
45 case ISCSI_SESSION_NORMAL:
48 printf("trying to login without setting session type\n");
52 pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_LOGIN_REQUEST, ISCSI_PDU_LOGIN_RESPONSE);
54 printf("Failed to allocate login pdu\n");
59 iscsi_pdu_set_immediate(pdu);
62 iscsi_pdu_set_pduflags(pdu, ISCSI_PDU_LOGIN_TRANSIT|ISCSI_PDU_LOGIN_CSG_OPNEG|ISCSI_PDU_LOGIN_NSG_FF);
66 if (asprintf(&str, "InitiatorName=%s", iscsi->initiator_name) == -1) {
67 printf("asprintf failed\n");
68 iscsi_free_pdu(iscsi, pdu);
71 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1);
74 printf("pdu add data failed\n");
75 iscsi_free_pdu(iscsi, pdu);
81 if (asprintf(&str, "InitiatorAlias=%s", iscsi->alias) == -1) {
82 printf("asprintf failed\n");
83 iscsi_free_pdu(iscsi, pdu);
86 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1);
89 printf("pdu add data failed\n");
90 iscsi_free_pdu(iscsi, pdu);
96 if (iscsi->session_type == ISCSI_SESSION_NORMAL) {
97 if (iscsi->target_name == NULL) {
98 printf("trying normal connect but target name not set\n");
99 iscsi_free_pdu(iscsi, pdu);
103 if (asprintf(&str, "TargetName=%s", iscsi->target_name) == -1) {
104 printf("asprintf failed\n");
105 iscsi_free_pdu(iscsi, pdu);
108 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1);
111 printf("pdu add data failed\n");
112 iscsi_free_pdu(iscsi, pdu);
118 switch (iscsi->session_type) {
119 case ISCSI_SESSION_DISCOVERY:
120 str = "SessionType=Discovery";
122 case ISCSI_SESSION_NORMAL:
123 str = "SessionType=Normal";
126 printf("can not handle sessions %d yet\n", iscsi->session_type);
129 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
130 printf("pdu add data failed\n");
131 iscsi_free_pdu(iscsi, pdu);
135 str = "HeaderDigest=None";
136 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
137 printf("pdu add data failed\n");
138 iscsi_free_pdu(iscsi, pdu);
141 str = "DataDigest=None";
142 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
143 printf("pdu add data failed\n");
144 iscsi_free_pdu(iscsi, pdu);
147 str = "InitialR2T=Yes";
148 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
149 printf("pdu add data failed\n");
150 iscsi_free_pdu(iscsi, pdu);
153 str = "ImmediateData=Yes";
154 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
155 printf("pdu add data failed\n");
156 iscsi_free_pdu(iscsi, pdu);
159 str = "MaxBurstLength=262144";
160 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
161 printf("pdu add data failed\n");
162 iscsi_free_pdu(iscsi, pdu);
165 str = "FirstBurstLength=262144";
166 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
167 printf("pdu add data failed\n");
168 iscsi_free_pdu(iscsi, pdu);
171 str = "MaxRecvDataSegmentLength=262144";
172 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
173 printf("pdu add data failed\n");
174 iscsi_free_pdu(iscsi, pdu);
177 str = "DataPDUInOrder=Yes";
178 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
179 printf("pdu add data failed\n");
180 iscsi_free_pdu(iscsi, pdu);
183 str = "DataSequenceInOrder=Yes";
184 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
185 printf("pdu add data failed\n");
186 iscsi_free_pdu(iscsi, pdu);
192 pdu->private_data = private_data;
194 if (iscsi_queue_pdu(iscsi, pdu) != 0) {
195 printf("failed to queue iscsi login pdu\n");
196 iscsi_free_pdu(iscsi, pdu);
203 int iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
207 if (size < ISCSI_HEADER_SIZE) {
208 printf("don't have enough data to read status from login reply\n");
212 /* XXX here we should parse the data returned in case the target renegotiated some
214 * we should also do proper handshaking if the target is not yet prepared to transition
217 status = ntohs(*(uint16_t *)&hdr[36]);
219 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
223 iscsi->statsn = ntohs(*(uint16_t *)&hdr[24]);
225 iscsi->is_loggedin = 1;
226 pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
232 int iscsi_logout_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data)
234 struct iscsi_pdu *pdu;
237 printf("trying to logout on NULL context\n");
241 if (iscsi->is_loggedin == 0) {
242 printf("trying to logout while not logged in\n");
246 pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_LOGOUT_REQUEST, ISCSI_PDU_LOGOUT_RESPONSE);
248 printf("Failed to allocate logout pdu\n");
252 /* logout request has the immediate flag set */
253 iscsi_pdu_set_immediate(pdu);
255 /* flags : close the session */
256 iscsi_pdu_set_pduflags(pdu, 0x80);
260 pdu->private_data = private_data;
262 if (iscsi_queue_pdu(iscsi, pdu) != 0) {
263 printf("failed to queue iscsi logout pdu\n");
264 iscsi_free_pdu(iscsi, pdu);
271 int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size UNUSED)
273 iscsi->is_loggedin = 0;
274 pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
279 int iscsi_set_session_type(struct iscsi_context *iscsi, enum iscsi_session_type session_type)
282 printf("Trying to set sesssion type on NULL context\n");
285 if (iscsi->is_loggedin) {
286 printf("trying to set session type while logged in\n");
290 iscsi->session_type = session_type;