/* * spsftpc.h — scorpiox-scorpioplayer-sftp-file-client * * SCORPIO PLAYER 2027 engine, row 41. Pure C99, zero external deps (vendored * Monocypher + yyjson + the row-49 SSH-2 client transport only). An SFTP v3 * CLIENT that rides the vendored `spsshc` transport's session channels * ("subsystem sftp"): REALPATH, OPENDIR/READDIR (all batches), LSTAT/STAT, * OPEN read-only, ranged READs pipelined N-deep over ONE channel, CLOSE. * Nothing writes, deletes or renames. * * It replaces Renci.SshNet 2020.0.0's SftpClient (the legacy corpora all * connect with `new ConnectionInfo(host, port, user, PasswordAuthenticationMethod)`). * Every failure names its cause via spsftpc_last_error(): auth failure vs * not-found vs network vs protocol are DISTINCT codes (the legacy * Connect+Login returned null with no reason). * * Also implements the row-54 `sprch_handler` boundary (RuntimeType "SFTP") and * the #26 `spffa_backend` storage vtable (id "sftp"). See SPEC.md for every * decision and its legacy citation. * * Threading: one global session (fleet style). Not thread-safe; call from one * thread. Strings returned by spsftpc_* are owned by the library and valid * until the next call that changes the same state. Copy them. */ #ifndef SPSFTPC_H #define SPSFTPC_H #include #ifdef __cplusplus extern "C" { #endif #if defined(SPSFTPC_STATIC) # define SPSFTPC_API #elif defined(_WIN32) # if defined(SPSFTPC_BUILD) # define SPSFTPC_API __declspec(dllexport) # else # define SPSFTPC_API __declspec(dllimport) # endif #else # define SPSFTPC_API __attribute__((visibility("default"))) #endif #define SPSFTPC_VERSION "1.0.0" #define SPSFTPC_PIPELINE 16 /* default in-flight READs */ #define SPSFTPC_READ_MAX (32u * 1024u) /* max bytes one READ asks for (the 32 KiB max-packet shape) */ /* ---- error taxonomy (SPEC section 11). Values frozen. ---- * Distinct classes for auth / not-found / network / protocol / host-key — * a network failure is never "empty folder" and never an auth failure. */ #define SPSFTPC_OK 0 #define SPSFTPC_E_NETWORK -1 /* TCP refused / reset / EOF / timeout */ #define SPSFTPC_E_HOSTKEY -2 /* pin mismatch (HOSTKEY_CHANGED) or bad pin */ #define SPSFTPC_E_AUTH -3 /* all offered auth methods refused */ #define SPSFTPC_E_PROTOCOL -4 /* malformed SSH or SFTP reply */ #define SPSFTPC_E_NOTFOUND -5 /* NO_SUCH_FILE / NO_SUCH_PATH / no such dir */ #define SPSFTPC_E_PERM -6 /* PERMISSION_DENIED / unreadable */ #define SPSFTPC_E_NO_SESSION -7 /* not connected / not initialised */ #define SPSFTPC_E_INVALID_ARG -8 #define SPSFTPC_E_LIMIT -9 /* a bounded decode limit was exceeded */ #define SPSFTPC_E_UNSUPPORTED -10 /* server refused sftp / an unsupported packet */ #define SPSFTPC_E_BUFFER -11 /* out buffer too small: retry with a larger cap */ #define SPSFTPC_E_EOF -12 /* read at/after EOF (bytes read still returned) */ #define SPSFTPC_E_OOM -13 /* short aliases (the vendored server-side names, for the shared tests) */ #define SPSFTPC_E_ARG SPSFTPC_E_INVALID_ARG #define SPSFTPC_E_STATE SPSFTPC_E_NO_SESSION /* ---- handle semantics ---- * spsftpc_open returns a handle >= 1, or < 0 on error. Handles live until * spsftpc_close(h) / spsftpc_disconnect(). spsftpc_read_at is EOF-capped and * never pads: it returns the bytes read (>= 0) or < 0 on error; a read * starting at/after EOF returns 0 with SPSFTPC_OK. A short read at `size` * ends the file exactly there (the stream ends at the REALPATH'd size). */ /* ---- connect + authenticate (the legacy SftpClient.Connect path) ---- * host: name or IP; port 1..65535, 0 = 22 (the legacy default). * pinned_fp: "SHA256:..." from a previous connect, NULL/"" = trust-on-first-use * (the legacy had NO host-key check at all — `git grep HostKeyReceived` = 0 hits * across all three corpora — so the fingerprint is REPORTED on every connect * and the caller pins it; a mismatch is a hard SPSFTPC_E_HOSTKEY). * user: required (non-empty). pass: password auth; NULL/"" with key_pem set * falls back to publickey. */ SPSFTPC_API int spsftpc_connect(const char *host, int port, const char *user, const char *pass, const char *pinned_fp, const char *key_pem, int timeout_ms); /* legacy RuntimeSettings keys unchanged: HostName, ServerPort (default 22), * UserName, Password, Path, ID (SFTPRuntimeConfigHandler.cs:17-40). */ SPSFTPC_API int spsftpc_connect_json(const char *runtime_settings_json); SPSFTPC_API void spsftpc_disconnect(void); SPSFTPC_API int spsftpc_is_connected(void); SPSFTPC_API const char *spsftpc_host_fingerprint(void); /* "SHA256:..." (TOFU) */ SPSFTPC_API const char *spsftpc_negotiated(void); /* kex/hostkey/ciphers */ SPSFTPC_API const char *spsftpc_session_json(void); /* host/port/user/auth/fp */ /* ---- the pooled SFTP session (the legacy `if (!_client.IsConnected) Connect()`) ---- */ SPSFTPC_API int spsftpc_reconnect(void); /* reconnect + re-auth, same settings */ /* ---- listing (OPENDIR + READDIR, all batches drained) ---- * path NULL/"" = the connection's root (the legacy `Path` setting, "/"). * Count is AFTER the legacy hidden filter ($ . @ — NFS2FolderFileAccesssor.cs:109, * SMBFolderFileAccesssor, SFTPPlayListFolder.cs:66 `!StartsWith(".")`). */ SPSFTPC_API int spsftpc_list(const char *path); SPSFTPC_API const char *spsftpc_item_json(int i); SPSFTPC_API int spsftpc_list_count(void); /* {"path":..,"items":[{name,path,folder,size,mtime_ms,hidden}..]} (#26 shape) */ SPSFTPC_API const char *spsftpc_list_json(void); SPSFTPC_API int spsftpc_list_hidden_count(void); /* entries dropped by the filter */ SPSFTPC_API int spsftpc_is_hidden_name(const char *name); /* ---- stat / realpath / symlink target ---- */ SPSFTPC_API int spsftpc_stat(const char *path, char *out_json, int cap); SPSFTPC_API int spsftpc_realpath(const char *path, char *out, int cap); SPSFTPC_API int spsftpc_readlink(const char *path, char *out, int cap); /* ---- file reads (OPEN read-only + pipelined READ) ---- */ SPSFTPC_API int spsftpc_open(const char *path); /* handle >= 1 */ SPSFTPC_API long long spsftpc_size(int h); /* LONG_BYTE count, <0 error */ SPSFTPC_API int spsftpc_read_at(int h, long long off, void *buf, int len); /* EOF-capped, bytes or <0 */ SPSFTPC_API void spsftpc_close(int h); /* in-flight READ requests used by the last read_at (0..SPSFTPC_PIPELINE) */ SPSFTPC_API int spsftpc_pipeline_depth(void); /* pipelining depth for read_at: 1 = one-at-a-time (the legacy shape), * default SPSFTPC_PIPELINE (16). 1..64. */ SPSFTPC_API int spsftpc_set_pipeline(int depth); SPSFTPC_API long long spsftpc_frame_count(void); /* SFTP packets sent since connect */ /* ---- whole-file convenience (the CLI + tests): reads a whole file through * the pipelined path into out, capping at cap bytes; returns bytes or <0. * The SHA-256 over the bytes is hex into out_sha (33+ bytes). NULL skips. */ SPSFTPC_API long long spsftpc_read_file(const char *path, void *out, int cap, char *out_sha, int sha_cap); /* ---- pure, channel-free helpers ---- */ /* split "host:port" and normalise the SFTP path ("//x" -> "/x", trailing slashes) */ SPSFTPC_API int spsftpc_split_path(const char *path, char *out, int cap); SPSFTPC_API const char *spsftpc_status_name(unsigned code); /* "NO_SUCH_FILE"... */ SPSFTPC_API int spsftpc_selftest(void); /* 0 = codec + KAT vectors pass */ /* ---- transports (SPEC section 4) ---- */ SPSFTPC_API int spsftpc_use_ssh(void); /* default: the vendored spsshc transport */ /* the in-process fake SFTP server (tests, console --test). tree_json: SPEC §13. * NO socket: the client's SFTP packets are answered by C code in-process. */ SPSFTPC_API int spsftpc_use_fake(const char *tree_json); SPSFTPC_API int spsftpc_fake_set_fault(const char *what, int value); SPSFTPC_API long long spsftpc_fake_calls(int pkt); /* SFTP packets the client sent */ /* ---- results ---- */ SPSFTPC_API const char *spsftpc_last_error(void); SPSFTPC_API int spsftpc_last_code(void); SPSFTPC_API unsigned spsftpc_last_status(void); /* the server's SSH_FX_* code */ SPSFTPC_API const char *spsftpc_strerror(int code); SPSFTPC_API const char *spsftpc_version(void); SPSFTPC_API const void *spsftpc_sprch_handler(void); /* const sprch_handler * (row 54) */ SPSFTPC_API const void *spsftpc_spffa_backend(void); /* const spffa_backend * (#26) */ #ifdef __cplusplus } #endif #endif /* SPSFTPC_H */