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/>.
22 #include <arpa/inet.h>
24 #include "iscsi-private.h"
26 int iscsi_discovery_async(struct iscsi_context *iscsi, iscsi_command_cb cb, void *private_data)
28 struct iscsi_pdu *pdu;
32 printf("trying to send text on NULL context\n");
36 if (iscsi->session_type != ISCSI_SESSION_DISCOVERY) {
37 printf("Trying to do discovery on non-discovery session\n");
41 pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_TEXT_REQUEST, ISCSI_PDU_TEXT_RESPONSE);
43 printf("Failed to allocate text pdu\n");
48 iscsi_pdu_set_immediate(pdu);
51 iscsi_pdu_set_pduflags(pdu, ISCSI_PDU_TEXT_FINAL);
53 /* target transfer tag */
54 iscsi_pdu_set_ttt(pdu, 0xffffffff);
57 str = "SendTargets=All";
58 if (iscsi_pdu_add_data(iscsi, pdu, (unsigned char *)str, strlen(str)+1) != 0) {
59 printf("pdu add data failed\n");
60 iscsi_free_pdu(iscsi, pdu);
65 pdu->private_data = private_data;
67 if (iscsi_queue_pdu(iscsi, pdu) != 0) {
68 printf("failed to queue iscsi text pdu\n");
69 iscsi_free_pdu(iscsi, pdu);
76 static void iscsi_free_discovery_addresses(struct iscsi_discovery_address *addresses)
78 while (addresses != NULL) {
79 struct iscsi_discovery_address *next = addresses->next;
81 if (addresses->target_name != NULL) {
82 free(discard_const(addresses->target_name));
83 addresses->target_name = NULL;
85 if (addresses->target_address != NULL) {
86 free(discard_const(addresses->target_address));
87 addresses->target_address = NULL;
89 addresses->next = NULL;
95 int iscsi_process_text_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
97 struct iscsi_discovery_address *targets = NULL;
99 /* verify the response looks sane */
100 if (hdr[1] != ISCSI_PDU_TEXT_FINAL) {
101 printf("unsupported flags in text reply %02x\n", hdr[1]);
102 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
106 /* skip past the header */
107 hdr += ISCSI_HEADER_SIZE;
108 size -= ISCSI_HEADER_SIZE;
113 len = strlen((char *)hdr);
120 printf("len > size when parsing discovery data %d>%d\n", len, size);
121 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
122 iscsi_free_discovery_addresses(targets);
126 /* parse the strings */
127 if (!strncmp((char *)hdr, "TargetName=", 11)) {
128 struct iscsi_discovery_address *target;
130 target = malloc(sizeof(struct iscsi_discovery_address));
131 if (target == NULL) {
132 printf("Failed to allocate data for new discovered target\n");
133 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
134 iscsi_free_discovery_addresses(targets);
137 bzero(target, sizeof(struct iscsi_discovery_address));
138 target->target_name = strdup((char *)hdr+11);
139 if (target->target_name == NULL) {
140 printf("Failed to allocate data for new discovered target name\n");
141 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
144 iscsi_free_discovery_addresses(targets);
147 target->next = targets;
149 } else if (!strncmp((char *)hdr, "TargetAddress=", 14)) {
150 targets->target_address = strdup((char *)hdr+14);
151 if (targets->target_address == NULL) {
152 printf("Failed to allocate data for new discovered target address\n");
153 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
154 iscsi_free_discovery_addresses(targets);
158 printf("Don't know how to handle discovery string : %s\n", hdr);
159 pdu->callback(iscsi, ISCSI_STATUS_ERROR, NULL, pdu->private_data);
160 iscsi_free_discovery_addresses(targets);
168 pdu->callback(iscsi, ISCSI_STATUS_GOOD, targets, pdu->private_data);
169 iscsi_free_discovery_addresses(targets);