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