112 lines
3.7 KiB
C++
112 lines
3.7 KiB
C++
/*
|
|
* montauk_io.cpp
|
|
* MontaukOS transport for the SMB client library
|
|
* Copyright (c) 2026 Daniel Hammer
|
|
*/
|
|
|
|
#include <smb/montauk.hpp>
|
|
#include <montauk/syscall.h>
|
|
|
|
namespace smb {
|
|
|
|
static constexpr uint64_t kSendStallMs = 15000;
|
|
|
|
static bool parse_ipv4(const char* s, uint32_t* out) {
|
|
uint32_t parts[4] = {0, 0, 0, 0};
|
|
int part = 0;
|
|
while (*s) {
|
|
if (part >= 4 || *s < '0' || *s > '9') return false;
|
|
uint32_t value = 0;
|
|
while (*s >= '0' && *s <= '9') {
|
|
value = value * 10u + (uint32_t)(*s - '0');
|
|
if (value > 255u) return false;
|
|
s++;
|
|
}
|
|
parts[part++] = value;
|
|
if (*s == '.') { s++; continue; }
|
|
if (*s != '\0') return false;
|
|
}
|
|
if (part != 4) return false;
|
|
*out = parts[0] | (parts[1] << 8) | (parts[2] << 16) | (parts[3] << 24);
|
|
return true;
|
|
}
|
|
|
|
int link_open(MontaukLink* link, const char* host, int port) {
|
|
link->fd = -1;
|
|
uint32_t ip = 0;
|
|
if (!parse_ipv4(host, &ip)) ip = montauk::resolve(host);
|
|
if (ip == 0) return ERR_NOT_FOUND;
|
|
|
|
int fd = montauk::socket(montauk::abi::SOCK_TCP);
|
|
if (fd < 0) return ERR_IO;
|
|
if (montauk::connect(fd, ip, (uint16_t)(port > 0 ? port : 445)) < 0) {
|
|
montauk::closesocket(fd);
|
|
return ERR_IO;
|
|
}
|
|
link->fd = fd;
|
|
return OK;
|
|
}
|
|
|
|
void link_close(MontaukLink* link) {
|
|
if (link->fd >= 0) montauk::closesocket(link->fd);
|
|
link->fd = -1;
|
|
}
|
|
|
|
static int io_send(void* ctx, const void* data, uint32_t len) {
|
|
int fd = ((MontaukLink*)ctx)->fd;
|
|
if (fd < 0) return -1;
|
|
const uint8_t* p = (const uint8_t*)data;
|
|
uint32_t sent = 0;
|
|
uint64_t deadline = montauk::get_milliseconds() + kSendStallMs;
|
|
while (sent < len) {
|
|
int r = montauk::send(fd, p + sent, len - sent);
|
|
if (r > 0) {
|
|
sent += (uint32_t)r;
|
|
deadline = montauk::get_milliseconds() + kSendStallMs;
|
|
continue;
|
|
}
|
|
if (r < 0) return -1;
|
|
uint64_t now = montauk::get_milliseconds();
|
|
if (now >= deadline) return -1;
|
|
uint32_t signals = montauk::wait_handle(
|
|
fd, montauk::abi::IPC_SIGNAL_WRITABLE | montauk::abi::IPC_SIGNAL_PEER_CLOSED,
|
|
deadline - now);
|
|
if (signals == (uint32_t)-1 || (signals & montauk::abi::IPC_SIGNAL_PEER_CLOSED)) return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int io_recv(void* ctx, void* buf, uint32_t len, uint32_t timeoutMs) {
|
|
int fd = ((MontaukLink*)ctx)->fd;
|
|
if (fd < 0) return -1;
|
|
uint64_t deadline = montauk::get_milliseconds() + timeoutMs;
|
|
for (;;) {
|
|
int r = montauk::recv(fd, buf, len);
|
|
if (r > 0) return r;
|
|
if (r < 0) return -1;
|
|
uint64_t now = montauk::get_milliseconds();
|
|
if (now >= deadline) return 0;
|
|
uint32_t signals = montauk::wait_handle(
|
|
fd, montauk::abi::IPC_SIGNAL_READABLE | montauk::abi::IPC_SIGNAL_PEER_CLOSED,
|
|
deadline - now);
|
|
if (signals == (uint32_t)-1) return -1;
|
|
if ((signals & montauk::abi::IPC_SIGNAL_PEER_CLOSED) &&
|
|
!(signals & montauk::abi::IPC_SIGNAL_READABLE)) return -1;
|
|
}
|
|
}
|
|
|
|
static void io_random(void* ctx, void* buf, uint32_t len) {
|
|
montauk::getrandom(buf, len);
|
|
}
|
|
|
|
Io link_io(MontaukLink* link) {
|
|
Io io;
|
|
io.ctx = link;
|
|
io.send = io_send;
|
|
io.recv = io_recv;
|
|
io.random = io_random;
|
|
return io;
|
|
}
|
|
|
|
}
|