#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include <string.h>
#include <netinet/in.h>
#include "iscsid.h"
#include "iscsi_proto.h"
static struct pdu *
logout_receive(struct connection *conn)
{
struct pdu *response;
struct iscsi_bhs_logout_response *bhslr;
response = pdu_new(conn);
pdu_receive(response);
if (response->pdu_bhs->bhs_opcode != ISCSI_BHS_OPCODE_LOGOUT_RESPONSE)
log_errx(1, "protocol error: received invalid opcode 0x%x",
response->pdu_bhs->bhs_opcode);
bhslr = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
if (ntohs(bhslr->bhslr_response) != BHSLR_RESPONSE_CLOSED_SUCCESSFULLY)
log_warnx("received Logout Response with reason %d",
ntohs(bhslr->bhslr_response));
if (ntohl(bhslr->bhslr_statsn) != conn->conn_statsn + 1) {
log_errx(1, "received Logout PDU with wrong StatSN: "
"is %u, should be %u", ntohl(bhslr->bhslr_statsn),
conn->conn_statsn + 1);
}
conn->conn_statsn = ntohl(bhslr->bhslr_statsn);
return (response);
}
static struct pdu *
logout_new_request(struct connection *conn)
{
struct pdu *request;
struct iscsi_bhs_logout_request *bhslr;
request = pdu_new(conn);
bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
bhslr->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_REQUEST |
ISCSI_BHS_OPCODE_IMMEDIATE;
bhslr->bhslr_reason = BHSLR_REASON_CLOSE_SESSION;
bhslr->bhslr_reason |= 0x80;
bhslr->bhslr_initiator_task_tag = 0;
bhslr->bhslr_cmdsn = 0;
bhslr->bhslr_expstatsn = htonl(conn->conn_statsn + 1);
return (request);
}
static void
kernel_add(const struct iscsid_connection *conn, const char *target)
{
struct iscsi_session_add isa;
int error;
memset(&isa, 0, sizeof(isa));
memcpy(&isa.isa_conf, &conn->conn_conf, sizeof(isa.isa_conf));
strlcpy(isa.isa_conf.isc_target, target,
sizeof(isa.isa_conf.isc_target));
isa.isa_conf.isc_discovery = 0;
error = ioctl(conn->conn_iscsi_fd, ISCSISADD, &isa);
if (error != 0)
log_warn("failed to add %s: ISCSISADD", target);
}
static void
kernel_remove(const struct iscsid_connection *conn)
{
struct iscsi_session_remove isr;
int error;
memset(&isr, 0, sizeof(isr));
isr.isr_session_id = conn->conn_session_id;
error = ioctl(conn->conn_iscsi_fd, ISCSISREMOVE, &isr);
if (error != 0)
log_warn("ISCSISREMOVE");
}
void
discovery(struct iscsid_connection *conn)
{
struct pdu *request, *response;
struct keys *request_keys, *response_keys;
int i;
log_debugx("beginning discovery session");
request_keys = keys_new();
keys_add(request_keys, "SendTargets", "All");
text_send_request(&conn->conn, request_keys);
keys_delete(request_keys);
request_keys = NULL;
log_debugx("waiting for Text Response");
response_keys = text_read_response(&conn->conn);
for (i = 0; i < KEYS_MAX; i++) {
if (response_keys->keys_names[i] == NULL)
break;
if (strcmp(response_keys->keys_names[i], "TargetName") != 0)
continue;
log_debugx("adding target %s", response_keys->keys_values[i]);
kernel_add(conn, response_keys->keys_values[i]);
}
keys_delete(response_keys);
log_debugx("removing temporary discovery session");
kernel_remove(conn);
#ifdef ICL_KERNEL_PROXY
if (conn->conn_conf.isc_iser == 1) {
log_debugx("discovery session done");
return;
}
#endif
log_debugx("discovery done; logging out");
request = logout_new_request(&conn->conn);
pdu_send(request);
pdu_delete(request);
request = NULL;
log_debugx("waiting for Logout Response");
response = logout_receive(&conn->conn);
pdu_delete(response);
log_debugx("discovery session done");
}