feat: add SMB networking support
This commit is contained in:
1 parent
6665750528
commit
2af2e90606
55 files changed
+7456
-158
No files matched your search
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* mount.hpp
|
||||
* Launching and managing smbfs mounts from other programs
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <montauk/syscall.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Shares are mounted by the smbfs.elf provider process (src/smbfs). These
|
||||
// helpers start one without blocking the caller -- a GUI polls the job from
|
||||
// its event loop -- and find or eject the drives that are already mounted.
|
||||
namespace smb {
|
||||
|
||||
struct MountRequest {
|
||||
const char* host;
|
||||
const char* share;
|
||||
const char* user; // "" or null for a guest logon
|
||||
const char* domain;
|
||||
const char* password;
|
||||
const char* label; // "" or null for the default "share on host"
|
||||
int port; // 0 = 445
|
||||
bool readOnly;
|
||||
};
|
||||
|
||||
struct MountJob {
|
||||
int pid; // smbfs process, -1 when idle
|
||||
uint64_t started;
|
||||
char line[200];
|
||||
int lineLen;
|
||||
};
|
||||
|
||||
// How long smbfs gets to report success or failure.
|
||||
static constexpr uint64_t MOUNT_TIMEOUT_MS = 60000;
|
||||
|
||||
// Characters that would break smbfs's argument splitting.
|
||||
inline bool mount_arg_ok(const char* s) {
|
||||
if (s == nullptr) return true;
|
||||
for (; *s; s++) {
|
||||
if (*s == '"' || (unsigned char)*s < 0x20) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Start mounting. Returns false if smbfs could not be launched or an
|
||||
// argument contains characters that cannot be passed.
|
||||
inline bool mount_begin(MountJob* job, const MountRequest& rq) {
|
||||
job->pid = -1;
|
||||
job->lineLen = 0;
|
||||
if (rq.host == nullptr || rq.share == nullptr || !rq.host[0] || !rq.share[0]) return false;
|
||||
if (!mount_arg_ok(rq.host) || !mount_arg_ok(rq.share) || !mount_arg_ok(rq.user) ||
|
||||
!mount_arg_ok(rq.domain) || !mount_arg_ok(rq.label)) return false;
|
||||
|
||||
char args[900];
|
||||
int n = snprintf(args, sizeof(args), "\"//%s/%s\"", rq.host, rq.share);
|
||||
if (rq.user && rq.user[0]) n += snprintf(args + n, sizeof(args) - (size_t)n, " --user \"%s\"", rq.user);
|
||||
if (rq.domain && rq.domain[0]) n += snprintf(args + n, sizeof(args) - (size_t)n, " --domain \"%s\"", rq.domain);
|
||||
if (rq.label && rq.label[0]) n += snprintf(args + n, sizeof(args) - (size_t)n, " --label \"%s\"", rq.label);
|
||||
if (rq.port > 0) n += snprintf(args + n, sizeof(args) - (size_t)n, " --port %d", rq.port);
|
||||
if (rq.readOnly) n += snprintf(args + n, sizeof(args) - (size_t)n, " --ro");
|
||||
if (n >= (int)sizeof(args) - 1) return false;
|
||||
|
||||
// The child inherits this process's environment at spawn, which is
|
||||
// how the password reaches it without appearing in its arguments.
|
||||
setenv("SMB_PASSWORD", rq.password ? rq.password : "", 1);
|
||||
int pid = montauk::spawn_redir("0:/os/smbfs.elf", args);
|
||||
unsetenv("SMB_PASSWORD");
|
||||
if (pid < 0) return false;
|
||||
|
||||
job->pid = pid;
|
||||
job->started = montauk::get_milliseconds();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Poll a job started with mount_begin. Returns 0 while still mounting,
|
||||
// 1 once mounted (*drive is the drive number), or -1 on failure with a
|
||||
// message in `error` and smbfs's smb::Status in *status.
|
||||
inline int mount_poll(MountJob* job, int* drive, int* status, char* error, int errorMax) {
|
||||
if (job->pid < 0) return -1;
|
||||
char buf[128];
|
||||
int n;
|
||||
while ((n = montauk::childio_read(job->pid, buf, sizeof(buf))) > 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (buf[i] != '\n') {
|
||||
if (buf[i] != '\r' && job->lineLen < (int)sizeof(job->line) - 1)
|
||||
job->line[job->lineLen++] = buf[i];
|
||||
continue;
|
||||
}
|
||||
job->line[job->lineLen] = '\0';
|
||||
job->lineLen = 0;
|
||||
if (!strncmp(job->line, "OK ", 3)) {
|
||||
if (drive) *drive = atoi(job->line + 3);
|
||||
job->pid = -1;
|
||||
return 1;
|
||||
}
|
||||
if (!strncmp(job->line, "ERR ", 4)) {
|
||||
const char* p = job->line + 4;
|
||||
if (status) *status = atoi(p);
|
||||
while (*p && *p != ' ') p++;
|
||||
while (*p == ' ') p++;
|
||||
if (error && errorMax > 0) snprintf(error, (size_t)errorMax, "%s", p);
|
||||
job->pid = -1;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// A negative read means the process is gone without having answered.
|
||||
bool timedOut = montauk::get_milliseconds() - job->started > MOUNT_TIMEOUT_MS;
|
||||
if (n < 0 || timedOut) {
|
||||
if (timedOut) montauk::kill(job->pid);
|
||||
if (status) *status = -1;
|
||||
if (error && errorMax > 0)
|
||||
snprintf(error, (size_t)errorMax, "%s", timedOut ? "the server did not respond"
|
||||
: "the mount helper stopped unexpectedly");
|
||||
job->pid = -1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Give up on a mount in progress.
|
||||
inline void mount_cancel(MountJob* job) {
|
||||
if (job->pid >= 0) montauk::kill(job->pid);
|
||||
job->pid = -1;
|
||||
}
|
||||
|
||||
// Find the drive an "smb://host/share" source is mounted on, or -1.
|
||||
// Host and share compare without regard to case.
|
||||
inline int find_mount(const char* host, const char* share) {
|
||||
char want[160];
|
||||
snprintf(want, sizeof(want), "smb://%s/%s", host, share);
|
||||
int drives[64];
|
||||
int n = montauk::drivelist(drives, 64);
|
||||
for (int i = 0; i < n; i++) {
|
||||
montauk::abi::UserFsInfo info;
|
||||
if (montauk::userfs_info(drives[i], &info) != 0) continue;
|
||||
const char* a = info.source;
|
||||
const char* b = want;
|
||||
for (; *a && *b; a++, b++) {
|
||||
char ca = (*a >= 'A' && *a <= 'Z') ? (char)(*a + 32) : *a;
|
||||
char cb = (*b >= 'A' && *b <= 'Z') ? (char)(*b + 32) : *b;
|
||||
if (ca != cb) break;
|
||||
}
|
||||
if (*a == '\0' && *b == '\0') return drives[i];
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Ask the provider of `drive` to disconnect and exit. Returns 0 if the
|
||||
// request was delivered; the drive disappears shortly afterwards.
|
||||
inline int unmount(int drive) {
|
||||
return montauk::userfs_unmount(drive);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user