void discoverylogout_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data) { struct client_state *clnt = (struct client_state *)private_data; printf("discovery session logged out, Message from main() was:[%s]\n", clnt->message); printf("disconnect socket\n"); if (iscsi_disconnect(iscsi) != 0) { printf("Failed to disconnect old socket\n"); exit(10); } printf("reconnect with normal login to [%s]\n", clnt->target_address); printf("Use targetname [%s] when connecting\n", clnt->target_name); if (iscsi_set_targetname(iscsi, clnt->target_name)) { printf("Failed to set target name\n"); exit(10); } if (iscsi_set_alias(iscsi, "ronnie") != 0) { printf("Failed to add alias\n"); exit(10); } if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) { printf("Failed to set settion type to normal\n"); exit(10); } if (iscsi_connect_async(iscsi, clnt->target_address, normalconnect_cb, clnt) != 0) { printf("iscsi_connect failed\n"); exit(10); } }
int main(int argc, char *argv[]) { struct iscsi_context *iscsi; struct pollfd pfd; struct client_state clnt; printf("iscsi client\n"); iscsi = iscsi_create_context("iqn.2002-10.com.ronnie:client"); if (iscsi == NULL) { printf("Failed to create context\n"); exit(10); } if (iscsi_set_alias(iscsi, "ronnie") != 0) { printf("Failed to add alias\n"); exit(10); } clnt.message = "Hello iSCSI"; clnt.has_discovered_target = 0; if (iscsi_connect_async(iscsi, TARGET, discoveryconnect_cb, &clnt) != 0) { printf("iscsi_connect failed\n"); exit(10); } for (;;) { pfd.fd = iscsi_get_fd(iscsi); pfd.events = iscsi_which_events(iscsi); if (poll(&pfd, 1, -1) < 0) { printf("Poll failed"); exit(10); } if (iscsi_service(iscsi, pfd.revents) < 0) { printf("iscsi_service failed\n"); break; } } printf("STOP\n"); exit(10); printf("ok\n"); return 0; }
/* * Connect to the target. Must set a target and address before calling this */ void iSCSILibWrapper::iSCSIConnect(void) { if (mIscsi) { mError = true; mErrorString.Format("%s: Cannot connect! Already connected!", __func__); throw CException(mErrorString); } /* * Create a reasonable initiator name if one does not exist */ if (mInitiator.size() == 0) mInitiator.append("iqn.2011-07.com.scsitest:scsitest0"); mIscsi = iscsi_create_context(mInitiator.c_str()); if (!mIscsi) { mError = true; mErrorString.Format("%s: Creation of iscsi context for %s failed: %s", __func__, mTarget.c_str(), iscsi_get_error(mIscsi)); throw CException(mErrorString); } if (iscsi_set_alias(mIscsi, "scqadleader") != 0) { mError = true; mErrorString.Format("%s: Failed to add an alias for target %s: %s", __func__, mTarget.c_str(), iscsi_get_error(mIscsi)); throw CException(mErrorString); } std::string target = std::string(mAddress); // If it does not have the port number, add it on. This is actually not // The best test ... but we will often only be given strings from discovery. if (target.find(":3260") == std::string::npos) target.append(":3260"); mClient.message = "Hello Server"; mClient.has_discovered_target = 0; mClient.finished = 0; if (iscsi_connect_async(mIscsi, target.c_str(), connect_cb, this) != 0) { mError = true; mErrorString.Format("%s : iscsi_connect_async to target %s failed: %s", __func__, target.c_str(), iscsi_get_error(mIscsi)); throw CException(mErrorString); } ServiceISCSIEvents(); // Add to the background task iSCSIBackGround::GetInstance().AddConnection(*this); }