summaryrefslogtreecommitdiff
path: root/ccan/iscsi/init.c
blob: e428c941b814d487061925b0299515259fa52704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
   Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include "iscsi.h"
#include "iscsi-private.h"
#include "dlinklist.h"


struct iscsi_context *iscsi_create_context(const char *initiator_name)
{
	struct iscsi_context *iscsi;

	iscsi = malloc(sizeof(struct iscsi_context));
	if (iscsi == NULL) {
		printf("Failed to allocate iscsi context\n");
		return NULL;
	}

	bzero(iscsi, sizeof(struct iscsi_context));

	iscsi->initiator_name = strdup(initiator_name);
	if (iscsi->initiator_name == NULL) {
		printf("Failed to allocate initiator name context\n");
		free(iscsi);
		return NULL;
	}

	iscsi->fd = -1;

	/* use a "random" isid */
	srandom(getpid() ^ time(NULL));
	iscsi_set_random_isid(iscsi);

	return iscsi;
}

int iscsi_set_random_isid(struct iscsi_context *iscsi)
{
	iscsi->isid[0] = 0x80;
	iscsi->isid[1] = random()&0xff;
	iscsi->isid[2] = random()&0xff;
	iscsi->isid[3] = random()&0xff;
	iscsi->isid[4] = 0;
	iscsi->isid[5] = 0;

	return 0;
}

int iscsi_set_alias(struct iscsi_context *iscsi, const char *alias)
{
	if (iscsi == NULL) {
		printf("Context is NULL when adding alias\n");
		return -1;
	}
	if (iscsi->is_loggedin != 0) {
		printf("Already logged in when adding alias\n");
		return -2;
	}

	if (iscsi->alias != NULL) {
		free(discard_const(iscsi->alias));
		iscsi->alias = NULL;
	}

	iscsi->alias = strdup(alias);
	if (iscsi->alias == NULL) {
		printf("Failed to allocate alias name\n");
		return -3;
	}

	return 0;
}

int iscsi_set_targetname(struct iscsi_context *iscsi, const char *target_name)
{
	if (iscsi == NULL) {
		printf("Context is NULL when adding targetname\n");
		return -1;
	}
	if (iscsi->is_loggedin != 0) {
		printf("Already logged in when adding targetname\n");
		return -2;
	}

	if (iscsi->target_name != NULL) {
		free(discard_const(iscsi->target_name));
		iscsi->target_name= NULL;
	}

	iscsi->target_name = strdup(target_name);
	if (iscsi->target_name == NULL) {
		printf("Failed to allocate target name\n");
		return -3;
	}

	return 0;
}

int iscsi_destroy_context(struct iscsi_context *iscsi)
{
	struct iscsi_pdu *pdu;

	if (iscsi == NULL) {
		return 0;
	}
	if (iscsi->initiator_name != NULL) {
		free(discard_const(iscsi->initiator_name));
		iscsi->initiator_name = NULL;
	}
	if (iscsi->alias != NULL) {
		free(discard_const(iscsi->alias));
		iscsi->alias = NULL;
	}
	if (iscsi->is_loggedin != 0) {
		printf("deswtroying context while logged in\n");
	}
	if (iscsi->fd != -1) {
		iscsi_disconnect(iscsi);
	}

	if (iscsi->inbuf != NULL) {
		free(iscsi->inbuf);
		iscsi->inbuf = NULL;
		iscsi->insize = 0;
		iscsi->inpos = 0;
	}

	while ((pdu = iscsi->outqueue)) {
	      	DLIST_REMOVE(iscsi->outqueue, pdu);
		pdu->callback(iscsi, ISCSI_STATUS_CANCELLED, NULL, pdu->private_data);
		iscsi_free_pdu(iscsi, pdu);
	}
	while ((pdu = iscsi->waitpdu)) {
	      	DLIST_REMOVE(iscsi->waitpdu, pdu);
		pdu->callback(iscsi, ISCSI_STATUS_CANCELLED, NULL, pdu->private_data);
		iscsi_free_pdu(iscsi, pdu);
	}

	free(iscsi);

	return 0;
}