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/>.
23 #include <sys/types.h>
27 #include "iscsi-private.h"
28 #include "dlinklist.h"
31 struct iscsi_context *iscsi_create_context(const char *initiator_name)
33 struct iscsi_context *iscsi;
35 iscsi = malloc(sizeof(struct iscsi_context));
37 printf("Failed to allocate iscsi context\n");
41 bzero(iscsi, sizeof(struct iscsi_context));
43 iscsi->initiator_name = strdup(initiator_name);
44 if (iscsi->initiator_name == NULL) {
45 printf("Failed to allocate initiator name context\n");
52 /* use a "random" isid */
53 srandom(getpid() ^ time(NULL));
54 iscsi_set_random_isid(iscsi);
59 int iscsi_set_random_isid(struct iscsi_context *iscsi)
61 iscsi->isid[0] = 0x80;
62 iscsi->isid[1] = random()&0xff;
63 iscsi->isid[2] = random()&0xff;
64 iscsi->isid[3] = random()&0xff;
71 int iscsi_set_alias(struct iscsi_context *iscsi, const char *alias)
74 printf("Context is NULL when adding alias\n");
77 if (iscsi->is_loggedin != 0) {
78 printf("Already logged in when adding alias\n");
82 if (iscsi->alias != NULL) {
83 free(discard_const(iscsi->alias));
87 iscsi->alias = strdup(alias);
88 if (iscsi->alias == NULL) {
89 printf("Failed to allocate alias name\n");
96 int iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name)
99 printf("Context is NULL when adding targetname\n");
102 if (iscsi->is_loggedin != 0) {
103 printf("Already logged in when adding targetname\n");
107 if (iscsi->target_name != NULL) {
108 free(discard_const(iscsi->target_name));
109 iscsi->target_name= NULL;
112 iscsi->target_name = strdup(target_name);
113 if (iscsi->target_name == NULL) {
114 printf("Failed to allocate target name\n");
121 int iscsi_destroy_context(struct iscsi_context *iscsi)
123 struct iscsi_pdu *pdu;
128 if (iscsi->initiator_name != NULL) {
129 free(discard_const(iscsi->initiator_name));
130 iscsi->initiator_name = NULL;
132 if (iscsi->alias != NULL) {
133 free(discard_const(iscsi->alias));
136 if (iscsi->is_loggedin != 0) {
137 printf("deswtroying context while logged in\n");
139 if (iscsi->fd != -1) {
140 iscsi_disconnect(iscsi);
143 if (iscsi->inbuf != NULL) {
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);
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);