]> git.ozlabs.org Git - ccan/blob - ccan/iscsi/login.c
ccan/io: flatten debug callchain further.
[ccan] / ccan / iscsi / login.c
1 /*
2    Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3    
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.
8    
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.
13    
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/>.
16 */
17
18 #include "config.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <arpa/inet.h>
23 #include <ccan/compiler/compiler.h>
24 #include "iscsi.h"
25 #include "iscsi-private.h"
26
27 int iscsi_login_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data)
28 {
29         struct iscsi_pdu *pdu;
30         const char *str;
31         char *astr;
32         int ret;
33
34         if (iscsi == NULL) {
35                 printf("trying to login on NULL context\n");
36                 return -1;
37         }
38
39         if (iscsi->is_loggedin != 0) {
40                 printf("trying to login while already logged in\n");
41                 return -2;
42         }
43
44         switch (iscsi->session_type) {
45         case ISCSI_SESSION_DISCOVERY:
46         case ISCSI_SESSION_NORMAL:
47                 break;
48         default:
49                 printf("trying to login without setting session type\n");
50                 return -3;
51         }
52
53         pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_LOGIN_REQUEST, ISCSI_PDU_LOGIN_RESPONSE);
54         if (pdu == NULL) {
55                 printf("Failed to allocate login pdu\n");
56                 return -4;
57         }
58
59         /* login request */
60         iscsi_pdu_set_immediate(pdu);
61
62         /* flags */
63         iscsi_pdu_set_pduflags(pdu, ISCSI_PDU_LOGIN_TRANSIT|ISCSI_PDU_LOGIN_CSG_OPNEG|ISCSI_PDU_LOGIN_NSG_FF);
64
65
66         /* initiator name */
67         if (asprintf(&astr, "InitiatorName=%s", iscsi->initiator_name) == -1) {
68                 printf("asprintf failed\n");
69                 iscsi_free_pdu(iscsi, pdu);
70                 return -5;
71         }
72         ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)astr, strlen(astr)+1);
73         free(astr);
74         if (ret != 0) {
75                 printf("pdu add data failed\n");
76                 iscsi_free_pdu(iscsi, pdu);
77                 return -6;
78         }
79
80         /* optional alias */
81         if (iscsi->alias) {
82                 if (asprintf(&astr, "InitiatorAlias=%s", iscsi->alias) == -1) {
83                         printf("asprintf failed\n");
84                         iscsi_free_pdu(iscsi, pdu);
85                         return -7;
86                 }
87                 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)astr, strlen(astr)+1);
88                 free(astr);
89                 if (ret != 0) {
90                         printf("pdu add data failed\n");
91                         iscsi_free_pdu(iscsi, pdu);
92                         return -8;
93                 }
94         }
95
96         /* target name */
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);
101                         return -9;
102                 }
103
104                 if (asprintf(&astr, "TargetName=%s", iscsi->target_name) == -1) {
105                         printf("asprintf failed\n");
106                         iscsi_free_pdu(iscsi, pdu);
107                         return -10;
108                 }
109                 ret = iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)astr, strlen(astr)+1);
110                 free(astr);
111                 if (ret != 0) {
112                         printf("pdu add data failed\n");
113                         iscsi_free_pdu(iscsi, pdu);
114                         return -11;
115                 }
116         }
117
118         /* session type */
119         switch (iscsi->session_type) {
120         case ISCSI_SESSION_DISCOVERY:
121                 str = "SessionType=Discovery";
122                 break;
123         case ISCSI_SESSION_NORMAL:
124                 str = "SessionType=Normal";
125                 break;
126         default:
127                 printf("can not handle sessions %d yet\n", iscsi->session_type);
128                 return -12;
129         }
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);
133                 return -13;
134         }
135
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);
140                 return -14;
141         }
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);
146                 return -15;
147         }
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);
152                 return -16;
153         }
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);
158                 return -17;
159         }
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);
164                 return -18;
165         }
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);
170                 return -19;
171         }
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);
176                 return -20;
177         }
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);
182                 return -21;
183         }
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);
188                 return -22;
189         }
190
191
192         pdu->callback     = cb;
193         pdu->private_data = private_data;
194
195         if (iscsi_queue_pdu(iscsi, pdu) != 0) {
196                 printf("failed to queue iscsi login pdu\n");
197                 iscsi_free_pdu(iscsi, pdu);
198                 return -23;
199         }
200
201         return 0;
202 }
203
204 int iscsi_process_login_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
205 {
206         int status;
207
208         if (size < ISCSI_HEADER_SIZE) {
209                 printf("don't have enough data to read status from login reply\n");
210                 return -1;
211         }
212
213         /* XXX here we should parse the data returned in case the target renegotiated some
214          *     some parameters.
215          *     we should also do proper handshaking if the target is not yet prepared to transition
216          *     to the next stage
217          */
218         status = ntohs(*(uint16_t *)&hdr[36]);
219         if (status != 0) {
220                 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
221                 return 0;
222         }
223
224         iscsi->statsn = ntohs(*(uint16_t *)&hdr[24]);
225
226         iscsi->is_loggedin = 1;
227         pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
228
229         return 0;
230 }
231
232
233 int iscsi_logout_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data)
234 {
235         struct iscsi_pdu *pdu;
236
237         if (iscsi == NULL) {
238                 printf("trying to logout on NULL context\n");
239                 return -1;
240         }
241
242         if (iscsi->is_loggedin == 0) {
243                 printf("trying to logout while not logged in\n");
244                 return -2;
245         }
246
247         pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_LOGOUT_REQUEST, ISCSI_PDU_LOGOUT_RESPONSE);
248         if (pdu == NULL) {
249                 printf("Failed to allocate logout pdu\n");
250                 return -3;
251         }
252
253         /* logout request has the immediate flag set */
254         iscsi_pdu_set_immediate(pdu);
255
256         /* flags : close the session */
257         iscsi_pdu_set_pduflags(pdu, 0x80);
258
259
260         pdu->callback     = cb;
261         pdu->private_data = private_data;
262
263         if (iscsi_queue_pdu(iscsi, pdu) != 0) {
264                 printf("failed to queue iscsi logout pdu\n");
265                 iscsi_free_pdu(iscsi, pdu);
266                 return -4;
267         }
268
269         return 0;
270 }
271
272 int iscsi_process_logout_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size UNUSED)
273 {
274         iscsi->is_loggedin = 0;
275         pdu->callback(iscsi, ISCSI_STATUS_GOOD, NULL, pdu->private_data);
276
277         return 0;
278 }
279
280 int iscsi_set_session_type(struct iscsi_context *iscsi, enum iscsi_session_type session_type)
281 {
282         if (iscsi == NULL) {
283                 printf("Trying to set sesssion type on NULL context\n");
284                 return -1;
285         }
286         if (iscsi->is_loggedin) {
287                 printf("trying to set session type while logged in\n");
288                 return -2;
289         }
290         
291         iscsi->session_type = session_type;
292
293         return 0;
294 }