240 lines
7.0 KiB
C++
240 lines
7.0 KiB
C++
#include "attach_linux/usbip_server.h"
|
|
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
namespace attach::linux_attach {
|
|
namespace {
|
|
|
|
using nui::protocol::TransferStatus;
|
|
using nui::protocol::UsbFunction;
|
|
|
|
bool read_exact(int fd, void* buffer, std::size_t length) {
|
|
auto* cursor = static_cast<uint8_t*>(buffer);
|
|
std::size_t remaining = length;
|
|
while (remaining > 0) {
|
|
const ssize_t got = ::recv(fd, cursor, remaining, 0);
|
|
if (got <= 0) {
|
|
return false;
|
|
}
|
|
cursor += got;
|
|
remaining -= static_cast<std::size_t>(got);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool write_exact(int fd, const void* buffer, std::size_t length) {
|
|
const auto* cursor = static_cast<const uint8_t*>(buffer);
|
|
std::size_t remaining = length;
|
|
while (remaining > 0) {
|
|
const ssize_t sent = ::send(fd, cursor, remaining, MSG_NOSIGNAL);
|
|
if (sent <= 0) {
|
|
return false;
|
|
}
|
|
cursor += sent;
|
|
remaining -= static_cast<std::size_t>(sent);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int32_t status_for(TransferStatus status) {
|
|
switch (status) {
|
|
case TransferStatus::completed:
|
|
return 0;
|
|
case TransferStatus::stall:
|
|
return usbip::kErrnoPipeStall;
|
|
case TransferStatus::idle:
|
|
return 0;
|
|
case TransferStatus::invalid_frame:
|
|
return usbip::kErrnoNoDevice;
|
|
}
|
|
return usbip::kErrnoNoDevice;
|
|
}
|
|
|
|
uint8_t endpoint_address(const usbip::HeaderBasic& basic) {
|
|
return static_cast<uint8_t>(basic.ep | (basic.direction == usbip::kDirIn ? 0x80 : 0x00));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
UsbipServer::UsbipServer(nui::protocol::UsbDeviceBackend& backend, ExportSet devices)
|
|
: backend_(backend), devices_(devices) {}
|
|
|
|
void UsbipServer::stop() {
|
|
stop_requested_ = true;
|
|
if (listen_fd_ >= 0) {
|
|
::shutdown(listen_fd_, SHUT_RDWR);
|
|
}
|
|
}
|
|
|
|
int UsbipServer::run(uint16_t port) {
|
|
const int listen_fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
if (listen_fd < 0) {
|
|
return 1;
|
|
}
|
|
const int reuse = 1;
|
|
::setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
|
|
|
sockaddr_in address{};
|
|
address.sin_family = AF_INET;
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
|
address.sin_port = htons(port);
|
|
if (::bind(listen_fd, reinterpret_cast<sockaddr*>(&address), sizeof(address)) != 0) {
|
|
::close(listen_fd);
|
|
return 2;
|
|
}
|
|
if (::listen(listen_fd, 4) != 0) {
|
|
::close(listen_fd);
|
|
return 3;
|
|
}
|
|
listen_fd_ = listen_fd;
|
|
const int result = accept_loop(listen_fd);
|
|
::close(listen_fd);
|
|
listen_fd_ = -1;
|
|
return result;
|
|
}
|
|
|
|
int UsbipServer::accept_loop(int listen_fd) {
|
|
while (!stop_requested_) {
|
|
const int client_fd = ::accept(listen_fd, nullptr, nullptr);
|
|
if (client_fd < 0) {
|
|
if (stop_requested_) {
|
|
return 0;
|
|
}
|
|
continue;
|
|
}
|
|
const int nodelay = 1;
|
|
::setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay));
|
|
serve_connection(client_fd);
|
|
::close(client_fd);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void UsbipServer::serve_connection(int client_fd) {
|
|
UsbFunction function = UsbFunction::camera;
|
|
std::string busid;
|
|
if (!handle_control_phase(client_fd, function, busid)) {
|
|
return;
|
|
}
|
|
session_loop(client_fd, function);
|
|
}
|
|
|
|
bool UsbipServer::handle_control_phase(int client_fd, UsbFunction& function, std::string& busid) {
|
|
uint8_t header[8];
|
|
if (!read_exact(client_fd, header, sizeof(header))) {
|
|
return false;
|
|
}
|
|
const auto op = usbip::decode_op_header(usbip::Bytes(header, header + sizeof(header)));
|
|
if (!op) {
|
|
return false;
|
|
}
|
|
|
|
if (op->code == usbip::kOpReqDevlist) {
|
|
const auto reply = usbip::encode_rep_devlist({devices_.devices.begin(), devices_.devices.end()});
|
|
write_exact(client_fd, reply.data(), reply.size());
|
|
return false; // OP_REQ_DEVLIST closes the connection per protocol.
|
|
}
|
|
|
|
if (op->code != usbip::kOpReqImport) {
|
|
return false;
|
|
}
|
|
uint8_t body[32];
|
|
if (!read_exact(client_fd, body, sizeof(body))) {
|
|
return false;
|
|
}
|
|
const auto requested_busid = usbip::decode_req_import_busid(usbip::Bytes(body, body + sizeof(body)));
|
|
const auto* device = requested_busid ? device_for_busid(devices_, *requested_busid) : nullptr;
|
|
if (!device) {
|
|
const auto reply = usbip::encode_rep_import_error();
|
|
write_exact(client_fd, reply.data(), reply.size());
|
|
return false;
|
|
}
|
|
const auto reply = usbip::encode_rep_import_ok(*device);
|
|
if (!write_exact(client_fd, reply.data(), reply.size())) {
|
|
return false;
|
|
}
|
|
busid = *requested_busid;
|
|
function = function_for_busid(devices_, busid);
|
|
return true;
|
|
}
|
|
|
|
void UsbipServer::session_loop(int client_fd, UsbFunction function) {
|
|
while (!stop_requested_) {
|
|
uint8_t header[usbip::kSubmitHeaderSize];
|
|
if (!read_exact(client_fd, header, usbip::kHeaderBasicSize)) {
|
|
return;
|
|
}
|
|
const auto basic = usbip::decode_header_basic(usbip::Bytes(header, header + usbip::kHeaderBasicSize));
|
|
if (!basic) {
|
|
return;
|
|
}
|
|
|
|
if (basic->command == usbip::kCmdUnlink) {
|
|
if (!read_exact(client_fd, header + usbip::kHeaderBasicSize,
|
|
usbip::kUnlinkHeaderSize - usbip::kHeaderBasicSize)) {
|
|
return;
|
|
}
|
|
const auto unlink = usbip::decode_cmd_unlink(
|
|
usbip::Bytes(header, header + usbip::kUnlinkHeaderSize));
|
|
if (!unlink) {
|
|
return;
|
|
}
|
|
const auto reply = usbip::encode_ret_unlink(unlink->basic.seqnum, usbip::kErrnoConnReset);
|
|
if (!write_exact(client_fd, reply.data(), reply.size())) {
|
|
return;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (basic->command != usbip::kCmdSubmit) {
|
|
return;
|
|
}
|
|
if (!read_exact(client_fd, header + usbip::kHeaderBasicSize,
|
|
usbip::kSubmitHeaderSize - usbip::kHeaderBasicSize)) {
|
|
return;
|
|
}
|
|
usbip::Bytes full(header, header + usbip::kSubmitHeaderSize);
|
|
const auto peek = usbip::decode_cmd_submit(full);
|
|
if (!peek) {
|
|
return;
|
|
}
|
|
if (peek->basic.direction == usbip::kDirOut && peek->transfer_buffer_length > 0) {
|
|
usbip::Bytes out_payload(peek->transfer_buffer_length);
|
|
if (!read_exact(client_fd, out_payload.data(), out_payload.size())) {
|
|
return;
|
|
}
|
|
full.insert(full.end(), out_payload.begin(), out_payload.end());
|
|
}
|
|
const auto submit = usbip::decode_cmd_submit(full);
|
|
if (!submit) {
|
|
return;
|
|
}
|
|
|
|
const uint8_t endpoint = endpoint_address(submit->basic);
|
|
nui::protocol::TransferResult result;
|
|
if (submit->basic.ep == 0) {
|
|
const auto setup = usbip::decode_setup(submit->setup);
|
|
const nui::protocol::ControlSetup control_setup{
|
|
setup.request_type, setup.request, setup.value, setup.index, setup.length};
|
|
result = backend_.answer_control_transfer(function, control_setup, submit->transfer_buffer);
|
|
} else {
|
|
result = backend_.next_packet(function, endpoint);
|
|
}
|
|
|
|
const auto reply = usbip::encode_ret_submit(
|
|
submit->basic.seqnum, status_for(result.status),
|
|
submit->basic.direction == usbip::kDirIn ? result.data : usbip::Bytes{});
|
|
if (!write_exact(client_fd, reply.data(), reply.size())) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace attach::linux_attach
|