From 56f1d99d4cd5bd4ce686f632153aef710f8e9516 Mon Sep 17 00:00:00 2001 From: Uwe Seimet <48174652+uweseimet@users.noreply.github.com> Date: Fri, 23 Jul 2021 15:40:01 +0200 Subject: [PATCH] Made port selectable (#145) * Made port selectable * Added port number error handling * Added checks for negative port --- src/raspberrypi/rascsi.cpp | 33 +++++++++++++++++++++------------ src/raspberrypi/rasctl.cpp | 26 ++++++++++++++++++-------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index ab675ea7..d5495871 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -119,7 +119,7 @@ void Banner(int argc, char* argv[]) // Initialization // //--------------------------------------------------------------------------- -BOOL Init() +BOOL Init(int port) { struct sockaddr_in server; int yes, result; @@ -134,7 +134,7 @@ BOOL Init() monsocket = socket(PF_INET, SOCK_STREAM, 0); memset(&server, 0, sizeof(server)); server.sin_family = PF_INET; - server.sin_port = htons(6868); + server.sin_port = htons(port); server.sin_addr.s_addr = htonl(INADDR_ANY); // allow address reuse @@ -723,7 +723,7 @@ bool has_suffix(const string& filename, const string& suffix) { // Argument Parsing // //--------------------------------------------------------------------------- -bool ParseArgument(int argc, char* argv[]) +bool ParseArgument(int argc, char* argv[], int& port) { int id = -1; bool is_sasi = false; @@ -731,7 +731,7 @@ bool ParseArgument(int argc, char* argv[]) string log_level = "trace"; int opt; - while ((opt = getopt(argc, argv, "-IiHhG:g:D:d:")) != -1) { + while ((opt = getopt(argc, argv, "-IiHhG:g:D:d:p:")) != -1) { switch (tolower(opt)) { case 'i': is_sasi = false; @@ -759,6 +759,14 @@ bool ParseArgument(int argc, char* argv[]) continue; } + case 'p': + port = atoi(optarg); + if (port <= 0 || port > 65535) { + cerr << "Invalid port " << optarg << ", port must be between 1 and 65535" << endl; + return false; + } + continue; + default: return false; @@ -944,9 +952,16 @@ int main(int argc, char* argv[]) // Output the Banner Banner(argc, argv); - // Initialize + // Argument parsing int ret = 0; - if (!Init()) { + int port = 6868; + if (!ParseArgument(argc, argv, port)) { + ret = EINVAL; + goto err_exit; + } + + // Initialize + if (!Init(port)) { ret = EPERM; goto init_exit; } @@ -954,12 +969,6 @@ int main(int argc, char* argv[]) // Reset Reset(); - // Argument parsing - if (!ParseArgument(argc, argv)) { - ret = EINVAL; - goto err_exit; - } - // Set the affinity to a specific processor core FixCpu(3); diff --git a/src/raspberrypi/rasctl.cpp b/src/raspberrypi/rasctl.cpp index fa922770..afab1bb0 100644 --- a/src/raspberrypi/rasctl.cpp +++ b/src/raspberrypi/rasctl.cpp @@ -26,14 +26,14 @@ using namespace rascsi_interface; // Send Command // //--------------------------------------------------------------------------- -int SendCommand(const char *hostname, const Command& command) +int SendCommand(const char *hostname, int port, const Command& command) { // Create a socket to send the command int fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; - server.sin_port = htons(6868); + server.sin_port = htons(port); server.sin_addr.s_addr = htonl(INADDR_LOOPBACK); struct hostent *host = gethostbyname(hostname); @@ -45,7 +45,7 @@ int SendCommand(const char *hostname, const Command& command) // Connect if (connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) { - cerr << "Error: Can't connect to rascsi process on host '" << hostname << "'" << endl; + cerr << "Error: Can't connect to rascsi process on '" << hostname << ":" << port << "'" << endl; return -1; } @@ -136,13 +136,14 @@ int main(int argc, char* argv[]) if (argc < 2) { cerr << "SCSI Target Emulator RaSCSI Controller" << endl; cerr << "version " << rascsi_get_version_string() << " (" << __DATE__ << ", " << __TIME__ << ")" << endl; - cerr << "Usage: " << argv[0] << " -i ID [-u UNIT] [-c CMD] [-t TYPE] [-f FILE] [-h HOSTNAME] [-g LOG_LEVEL]" << endl; + cerr << "Usage: " << argv[0] << " -i ID [-u UNIT] [-c CMD] [-t TYPE] [-f FILE] [-h HOSTNAME] [-p PORT] [-g LOG_LEVEL]" << endl; 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; cerr << " HOSTNAME := rascsi host to connect to, default is 'localhost'" << endl; + cerr << " PORT := rascsi port to connect to, default is 6868" << endl; cerr << " LOG_LEVEL := log level {trace|debug|info|warn|err|critical|off}, default is 'trace'" << endl; cerr << " If CMD is 'attach' or 'insert' the FILE parameter is required." << endl; cerr << "Usage: " << argv[0] << " -l" << endl; @@ -158,9 +159,10 @@ int main(int argc, char* argv[]) Operation cmd = LIST; DeviceType type = UNDEFINED; const char *hostname = "localhost"; + int port = 6868; string params; opterr = 0; - while ((opt = getopt(argc, argv, "i:u:c:t:f:h:g:l")) != -1) { + while ((opt = getopt(argc, argv, "i:u:c:t:f:h:p:g:l")) != -1) { switch (opt) { case 'i': id = optarg[0] - '0'; @@ -238,6 +240,14 @@ int main(int argc, char* argv[]) hostname = optarg; break; + case 'p': + port = atoi(optarg); + if (port <= 0 || port > 65535) { + cerr << "Invalid port " << optarg << ", port must be between 1 and 65535" << endl; + exit(-1); + } + break; + case 'g': cmd = LOG_LEVEL; params = optarg; @@ -250,7 +260,7 @@ int main(int argc, char* argv[]) if (cmd == LOG_LEVEL) { command.set_cmd(LOG_LEVEL); command.set_params(params); - int fd = SendCommand(hostname, command); + int fd = SendCommand(hostname, port, command); if (fd < 0) { exit(ENOTCONN); } @@ -262,7 +272,7 @@ int main(int argc, char* argv[]) // List display only if (cmd == LIST || (id < 0 && type == UNDEFINED && params.empty())) { command.set_cmd(LIST); - int fd = SendCommand(hostname, command); + int fd = SendCommand(hostname, port, command); if (fd < 0) { exit(ENOTCONN); } @@ -335,7 +345,7 @@ int main(int argc, char* argv[]) command.set_params(params); } - int fd = SendCommand(hostname, command); + int fd = SendCommand(hostname, port, command); if (fd == -1) { exit(ENOTCONN); }