-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//--------------------------------------------------------------------------- | ||
// | ||
// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi | ||
// | ||
// Copyright (C) 2023-2025 Uwe Seimet | ||
// | ||
//--------------------------------------------------------------------------- | ||
|
||
#include "s2p_server.h" | ||
#include <cassert> | ||
#include <csignal> | ||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
#include <sys/socket.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
using namespace spdlog; | ||
|
||
string S2pServer::Init(int port) | ||
{ | ||
assert(server_socket == -1); | ||
|
||
if (port <= 0 || port > 65535) { | ||
return fmt::format("Invalid port number: {}", port); | ||
} | ||
|
||
server_socket = socket(PF_INET, SOCK_STREAM, 0); | ||
if (server_socket == -1) { | ||
return fmt::format("Can't create server socket: {}", strerror(errno)); | ||
} | ||
|
||
if (const int enable = 1; setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) { | ||
Stop(); | ||
return fmt::format("Can't reuse socket: {}", strerror(errno)); | ||
} | ||
|
||
sockaddr_in server = { }; | ||
server.sin_family = PF_INET; | ||
server.sin_port = htons((uint16_t)port); | ||
server.sin_addr.s_addr = INADDR_ANY; | ||
if (bind(server_socket, reinterpret_cast<const sockaddr*>(&server), // NOSONAR bit_cast is not supported by the bullseye compiler | ||
static_cast<socklen_t>(sizeof(sockaddr_in))) < 0) { | ||
Stop(); | ||
return fmt::format("Port {} is in use, s2p may already be running", port); | ||
} | ||
|
||
if (listen(server_socket, 2) == -1) { | ||
Stop(); | ||
return "Can't listen to service socket: " + string(strerror(errno)); | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
void S2pServer::Stop() | ||
{ | ||
if (server_socket != -1) { | ||
shutdown(server_socket, SHUT_RD); | ||
close(server_socket); | ||
|
||
server_socket = -1; | ||
} | ||
} | ||
|
||
int S2pServer::Accept() const | ||
{ | ||
return accept(server_socket, nullptr, nullptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//--------------------------------------------------------------------------- | ||
// | ||
// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi | ||
// | ||
// Copyright (C) 2023-2025 Uwe Seimet | ||
// | ||
//--------------------------------------------------------------------------- | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class S2pServer | ||
{ | ||
|
||
public: | ||
|
||
string Init(int); | ||
|
||
void Stop(); | ||
|
||
int Accept() const; | ||
|
||
bool IsRunning() const | ||
{ | ||
return server_socket != -1; | ||
} | ||
|
||
private: | ||
|
||
int server_socket = -1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters