]> git.ozlabs.org Git - ccan/blob - ccan/iscsi/init.c
e428c941b814d487061925b0299515259fa52704
[ccan] / ccan / iscsi / init.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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <strings.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include "iscsi.h"
27 #include "iscsi-private.h"
28 #include "dlinklist.h"
29
30
31 struct iscsi_context *iscsi_create_context(const char *initiator_name)
32 {
33         struct iscsi_context *iscsi;
34
35         iscsi = malloc(sizeof(struct iscsi_context));
36         if (iscsi == NULL) {
37                 printf("Failed to allocate iscsi context\n");
38                 return NULL;
39         }
40
41         bzero(iscsi, sizeof(struct iscsi_context));
42
43         iscsi->initiator_name = strdup(initiator_name);
44         if (iscsi->initiator_name == NULL) {
45                 printf("Failed to allocate initiator name context\n");
46                 free(iscsi);
47                 return NULL;
48         }
49
50         iscsi->fd = -1;
51
52         /* use a "random" isid */
53         srandom(getpid() ^ time(NULL));
54         iscsi_set_random_isid(iscsi);
55
56         return iscsi;
57 }
58
59 int iscsi_set_random_isid(struct iscsi_context *iscsi)
60 {
61         iscsi->isid[0] = 0x80;
62         iscsi->isid[1] = random()&0xff;
63         iscsi->isid[2] = random()&0xff;
64         iscsi->isid[3] = random()&0xff;
65         iscsi->isid[4] = 0;
66         iscsi->isid[5] = 0;
67
68         return 0;
69 }
70
71 int iscsi_set_alias(struct iscsi_context *iscsi, const char *alias)
72 {
73         if (iscsi == NULL) {
74                 printf("Context is NULL when adding alias\n");
75                 return -1;
76         }
77         if (iscsi->is_loggedin != 0) {
78                 printf("Already logged in when adding alias\n");
79                 return -2;
80         }
81
82         if (iscsi->alias != NULL) {
83                 free(discard_const(iscsi->alias));
84                 iscsi->alias = NULL;
85         }
86
87         iscsi->alias = strdup(alias);
88         if (iscsi->alias == NULL) {
89                 printf("Failed to allocate alias name\n");
90                 return -3;
91         }
92
93         return 0;
94 }
95
96 int iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name)
97 {
98         if (iscsi == NULL) {
99                 printf("Context is NULL when adding targetname\n");
100                 return -1;
101         }
102         if (iscsi->is_loggedin != 0) {
103                 printf("Already logged in when adding targetname\n");
104                 return -2;
105         }
106
107         if (iscsi->target_name != NULL) {
108                 free(discard_const(iscsi->target_name));
109                 iscsi->target_name= NULL;
110         }
111
112         iscsi->target_name = strdup(target_name);
113         if (iscsi->target_name == NULL) {
114                 printf("Failed to allocate target name\n");
115                 return -3;
116         }
117
118         return 0;
119 }
120
121 int iscsi_destroy_context(struct iscsi_context *iscsi)
122 {
123         struct iscsi_pdu *pdu;
124
125         if (iscsi == NULL) {
126                 return 0;
127         }
128         if (iscsi->initiator_name != NULL) {
129                 free(discard_const(iscsi->initiator_name));
130                 iscsi->initiator_name = NULL;
131         }
132         if (iscsi->alias != NULL) {
133                 free(discard_const(iscsi->alias));
134                 iscsi->alias = NULL;
135         }
136         if (iscsi->is_loggedin != 0) {
137                 printf("deswtroying context while logged in\n");
138         }
139         if (iscsi->fd != -1) {
140                 iscsi_disconnect(iscsi);
141         }
142
143         if (iscsi->inbuf != NULL) {
144                 free(iscsi->inbuf);
145                 iscsi->inbuf = NULL;
146                 iscsi->insize = 0;
147                 iscsi->inpos = 0;
148         }
149
150         while ((pdu = iscsi->outqueue)) {
151                 DLIST_REMOVE(iscsi->outqueue, pdu);
152                 pdu->callback(iscsi, ISCSI_STATUS_CANCELLED, NULL, pdu->private_data);
153                 iscsi_free_pdu(iscsi, pdu);
154         }
155         while ((pdu = iscsi->waitpdu)) {
156                 DLIST_REMOVE(iscsi->waitpdu, pdu);
157                 pdu->callback(iscsi, ISCSI_STATUS_CANCELLED, NULL, pdu->private_data);
158                 iscsi_free_pdu(iscsi, pdu);
159         }
160
161         free(iscsi);
162
163         return 0;
164 }