2018-05-03 13:47:57 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI (*^..^*)
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Powered by XM6 TypeG Technology.
|
2020-07-04 14:57:44 +00:00
|
|
|
// Copyright (C) 2016-2020 GIMONS
|
2020-07-05 16:47:17 +00:00
|
|
|
// [ Send Control Command ]
|
2018-05-03 13:47:57 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-07-19 23:41:00 +00:00
|
|
|
#include <netdb.h>
|
2021-07-24 00:13:05 +00:00
|
|
|
#include "google/protobuf/message_lite.h"
|
2018-05-03 13:47:57 +00:00
|
|
|
#include "os.h"
|
2021-01-25 16:07:30 +00:00
|
|
|
#include "rascsi_version.h"
|
2021-07-18 22:15:13 +00:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "rasutil.h"
|
2021-07-22 12:47:08 +00:00
|
|
|
#include "rascsi_interface.pb.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2021-07-22 12:47:08 +00:00
|
|
|
using namespace rascsi_interface;
|
2018-05-03 13:47:57 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-07-05 16:47:17 +00:00
|
|
|
// Send Command
|
2018-05-03 13:47:57 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
2021-07-24 00:13:05 +00:00
|
|
|
int SendCommand(const string& hostname, const PbCommand& command)
|
2018-05-03 13:47:57 +00:00
|
|
|
{
|
2021-07-24 00:30:56 +00:00
|
|
|
int fd = -1;
|
2018-05-03 13:47:57 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
try {
|
|
|
|
struct hostent *host = gethostbyname(hostname.c_str());
|
|
|
|
if (!host) {
|
|
|
|
throw ioexception("Can't resolve hostname '" + hostname + "'");
|
|
|
|
}
|
2021-07-18 22:15:13 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
throw ioexception("Can't create socket");
|
|
|
|
}
|
2018-05-03 13:47:57 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
struct sockaddr_in server;
|
|
|
|
memset(&server, 0, sizeof(server));
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(6868);
|
|
|
|
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
memcpy(&server.sin_addr.s_addr, host->h_addr, host->h_length);
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
throw ioexception("Can't connect to rascsi process on host '" + hostname + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializeMessage(fd, command);
|
2021-07-22 12:47:08 +00:00
|
|
|
}
|
|
|
|
catch(const ioexception& e) {
|
2021-07-24 00:13:05 +00:00
|
|
|
cerr << "Error: " << e.getmsg() << endl;
|
2021-07-22 12:47:08 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2021-07-19 23:41:00 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
return -1;
|
2021-07-22 12:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
return fd;
|
2021-07-22 12:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Receive command result
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool ReceiveResult(int fd) {
|
|
|
|
bool status = true;
|
2021-07-24 00:13:05 +00:00
|
|
|
|
2021-07-22 12:47:08 +00:00
|
|
|
try {
|
2021-07-24 00:13:05 +00:00
|
|
|
PbResult result;
|
|
|
|
DeserializeMessage(fd, result);
|
2021-07-18 22:15:13 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
status = result.status();
|
2021-07-22 12:47:08 +00:00
|
|
|
if (status) {
|
|
|
|
cerr << result.msg();
|
|
|
|
}
|
|
|
|
else {
|
2021-07-18 22:15:13 +00:00
|
|
|
cout << result.msg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const ioexception& e) {
|
2021-07-22 12:47:08 +00:00
|
|
|
cerr << "Error: " << e.getmsg() << endl;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
// Fall through
|
2021-07-22 12:47:08 +00:00
|
|
|
|
|
|
|
status = false;
|
2021-07-18 22:15:13 +00:00
|
|
|
}
|
2018-05-03 13:47:57 +00:00
|
|
|
|
2021-07-19 23:41:00 +00:00
|
|
|
close(fd);
|
2018-05-03 13:47:57 +00:00
|
|
|
|
2021-07-22 12:47:08 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Command implementations
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CommandList(const string& hostname)
|
|
|
|
{
|
|
|
|
PbCommand command;
|
|
|
|
command.set_cmd(LIST);
|
2021-07-22 12:47:08 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
int fd = SendCommand(hostname.c_str(), command);
|
|
|
|
if (fd < 0) {
|
|
|
|
exit(ENOTCONN);
|
|
|
|
}
|
|
|
|
|
|
|
|
PbDevices devices;
|
|
|
|
try {
|
|
|
|
DeserializeMessage(fd, devices);
|
2021-07-22 12:47:08 +00:00
|
|
|
}
|
2021-07-24 00:13:05 +00:00
|
|
|
catch(const ioexception& e) {
|
|
|
|
cerr << "Error: " << e.getmsg() << endl;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
exit(-1);
|
2021-07-22 12:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
close (fd);
|
2021-07-22 12:47:08 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
cout << ListDevices(devices) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandLogLevel(const string& hostname, const string& log_level)
|
|
|
|
{
|
|
|
|
PbCommand command;
|
|
|
|
command.set_cmd(LOG_LEVEL);
|
|
|
|
command.set_params(log_level);
|
|
|
|
|
|
|
|
int fd = SendCommand(hostname.c_str(), command);
|
|
|
|
if (fd < 0) {
|
|
|
|
exit(ENOTCONN);
|
2021-07-22 12:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
ReceiveResult(fd);
|
2021-07-22 12:47:08 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
close(fd);
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-07-05 16:47:17 +00:00
|
|
|
// Main processing
|
2018-05-03 13:47:57 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2021-07-19 23:41:00 +00:00
|
|
|
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// Display help
|
2018-05-03 13:47:57 +00:00
|
|
|
if (argc < 2) {
|
2021-07-18 22:15:13 +00:00
|
|
|
cerr << "SCSI Target Emulator RaSCSI Controller" << endl;
|
|
|
|
cerr << "version " << rascsi_get_version_string() << " (" << __DATE__ << ", " << __TIME__ << ")" << endl;
|
2021-07-24 00:13:05 +00:00
|
|
|
cerr << "Usage: " << argv[0] << " -i ID [-u UNIT] [-c CMD] [-t TYPE] [-f FILE] [-h HOSTNAME] [-g LOG_LEVEL]" << endl;
|
2021-07-18 22:15:13 +00:00
|
|
|
cerr << " where ID := {0|1|2|3|4|5|6|7}" << endl;
|
|
|
|
cerr << " UNIT := {0|1} default setting is 0." << endl;
|
|
|
|
cerr << " CMD := {attach|detach|insert|eject|protect}" << endl;
|
|
|
|
cerr << " TYPE := {hd|mo|cd|bridge|daynaport}" << endl;
|
|
|
|
cerr << " FILE := image file path" << endl;
|
2021-07-19 23:41:00 +00:00
|
|
|
cerr << " HOSTNAME := rascsi host to connect to, default is 'localhost'" << endl;
|
|
|
|
cerr << " LOG_LEVEL := log level {trace|debug|info|warn|err|critical|off}, default is 'trace'" << endl;
|
2021-07-18 22:15:13 +00:00
|
|
|
cerr << " If CMD is 'attach' or 'insert' the FILE parameter is required." << endl;
|
|
|
|
cerr << "Usage: " << argv[0] << " -l" << endl;
|
|
|
|
cerr << " Print device list." << endl;
|
|
|
|
|
2018-05-03 13:47:57 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// Parse the arguments
|
2021-07-18 22:15:13 +00:00
|
|
|
int opt;
|
|
|
|
int id = -1;
|
|
|
|
int un = 0;
|
2021-07-24 00:13:05 +00:00
|
|
|
PbOperation cmd = LIST;
|
|
|
|
PbDeviceType type = UNDEFINED;
|
2021-07-19 23:41:00 +00:00
|
|
|
const char *hostname = "localhost";
|
2021-07-18 22:15:13 +00:00
|
|
|
string params;
|
2018-05-03 13:47:57 +00:00
|
|
|
opterr = 0;
|
2021-07-24 00:13:05 +00:00
|
|
|
while ((opt = getopt(argc, argv, "i:u:c:t:f:h:g:l")) != -1) {
|
2018-05-03 13:47:57 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'i':
|
|
|
|
id = optarg[0] - '0';
|
|
|
|
break;
|
|
|
|
|
2020-07-04 14:57:44 +00:00
|
|
|
case 'u':
|
|
|
|
un = optarg[0] - '0';
|
|
|
|
break;
|
|
|
|
|
2018-05-03 13:47:57 +00:00
|
|
|
case 'c':
|
2021-07-18 22:15:13 +00:00
|
|
|
switch (tolower(optarg[0])) {
|
|
|
|
case 'a':
|
|
|
|
cmd = ATTACH;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'd':
|
|
|
|
cmd = DETACH;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'i':
|
|
|
|
cmd = INSERT;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'e':
|
|
|
|
cmd = EJECT;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'p':
|
|
|
|
cmd = PROTECT;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
2021-07-18 22:15:13 +00:00
|
|
|
switch (tolower(optarg[0])) {
|
|
|
|
case 's':
|
|
|
|
type = SASI_HD;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'h':
|
|
|
|
type = SCSI_HD;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'm':
|
|
|
|
type = MO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
type = CD;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'b':
|
|
|
|
type = BR;
|
2021-02-07 19:00:48 +00:00
|
|
|
break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
// case 'n':
|
|
|
|
// type = NUVOLINK;
|
2021-02-07 19:00:48 +00:00
|
|
|
// break;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
case 'd':
|
|
|
|
type = DAYNAPORT;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
2021-07-18 22:15:13 +00:00
|
|
|
params = optarg;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
2021-07-18 22:15:13 +00:00
|
|
|
cmd = LIST;
|
|
|
|
break;
|
|
|
|
|
2021-07-19 23:41:00 +00:00
|
|
|
case 'h':
|
|
|
|
hostname = optarg;
|
|
|
|
break;
|
|
|
|
|
2021-07-22 12:47:08 +00:00
|
|
|
case 'g':
|
2021-07-18 22:15:13 +00:00
|
|
|
cmd = LOG_LEVEL;
|
|
|
|
params = optarg;
|
2018-05-03 13:47:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
PbCommand command;
|
2021-07-18 22:15:13 +00:00
|
|
|
|
|
|
|
if (cmd == LOG_LEVEL) {
|
2021-07-24 00:13:05 +00:00
|
|
|
CommandLogLevel(hostname, params);
|
2021-07-18 22:15:13 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// List display only
|
2021-07-18 22:15:13 +00:00
|
|
|
if (cmd == LIST || (id < 0 && type == UNDEFINED && params.empty())) {
|
2021-07-24 00:13:05 +00:00
|
|
|
CommandList(hostname);
|
2018-05-03 13:47:57 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// Check the ID number
|
2018-05-03 13:47:57 +00:00
|
|
|
if (id < 0 || id > 7) {
|
2021-07-18 22:15:13 +00:00
|
|
|
cerr << __PRETTY_FUNCTION__ << " Error : Invalid ID " << id << endl;
|
2018-05-03 13:47:57 +00:00
|
|
|
exit(EINVAL);
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// Check the unit number
|
2020-07-04 14:57:44 +00:00
|
|
|
if (un < 0 || un > 1) {
|
2021-07-18 22:15:13 +00:00
|
|
|
cerr << __PRETTY_FUNCTION__ << " Error : Invalid UNIT " << un << endl;
|
2020-07-04 14:57:44 +00:00
|
|
|
exit(EINVAL);
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// Type Check
|
2021-07-18 22:15:13 +00:00
|
|
|
if (cmd == ATTACH && type == UNDEFINED) {
|
2020-07-05 16:47:17 +00:00
|
|
|
// Try to determine the file type from the extension
|
2021-07-18 22:15:13 +00:00
|
|
|
int len = params.length();
|
|
|
|
if (len > 4 && params[len - 4] == '.') {
|
|
|
|
string ext = params.substr(len - 3);
|
|
|
|
if (ext == "hdf" || ext == "hds" || ext == "hdn" || ext == "hdi" || ext == "nhd" || ext == "hda") {
|
|
|
|
type = SASI_HD;
|
|
|
|
} else if (ext == "mos") {
|
|
|
|
type = MO;
|
|
|
|
} else if (ext == "iso") {
|
|
|
|
type = CD;
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 22:15:13 +00:00
|
|
|
// File check (command is ATTACH and type is HD, for CD and MO the medium (=file) may be inserted later)
|
|
|
|
if (cmd == ATTACH && (type == SASI_HD || type == SCSI_HD) && params.empty()) {
|
|
|
|
cerr << "Error : Invalid file path" << endl;
|
|
|
|
exit(EINVAL);
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// File check (command is INSERT)
|
2021-07-18 22:15:13 +00:00
|
|
|
if (cmd == INSERT && params.empty()) {
|
|
|
|
cerr << "Error : Invalid file path" << endl;
|
|
|
|
exit(EINVAL);
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// Generate the command and send it
|
2021-07-18 22:15:13 +00:00
|
|
|
command.set_id(id);
|
|
|
|
command.set_un(un);
|
|
|
|
command.set_cmd(cmd);
|
|
|
|
command.set_type(type);
|
|
|
|
if (!params.empty()) {
|
|
|
|
command.set_params(params);
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|
2021-07-22 12:47:08 +00:00
|
|
|
|
2021-07-24 00:13:05 +00:00
|
|
|
int fd = SendCommand(hostname, command);
|
|
|
|
if (fd < 0) {
|
2021-07-18 22:15:13 +00:00
|
|
|
exit(ENOTCONN);
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:47:08 +00:00
|
|
|
bool status = ReceiveResult(fd);
|
|
|
|
close(fd);
|
|
|
|
|
2020-07-05 16:47:17 +00:00
|
|
|
// All done!
|
2021-07-22 12:47:08 +00:00
|
|
|
exit(status ? 0 : -1);
|
2018-05-03 13:47:57 +00:00
|
|
|
}
|