/* * scorpiox-scorpioplayer-remote-control-protocol — SPRC wire protocol. * * UDP datagram, payload = zlib( JSON( SPRCPayload ) ). Pure C99. * See SPEC.md for the decisions and the C# spec sources. * * ⚠️ SECURITY — READ THIS FIRST * ----------------------------- * The legacy SPRC protocol has NO authentication, NO encryption and NO origin * check. With this library's security exports OFF (the default, for * byte-compatibility with the legacy fleet), **anyone who can send a UDP * datagram to the listening port can control the player and read its state** * (play arbitrary URIs, load arbitrary subtitle files, read playlists, * favourites, current IMDb id, ...). * SPRC_API * sprc_set_shared_secret() — HMAC-SHA256 over the compressed payload, * verified BEFORE inflation (a bomb then costs * one HMAC, never an allocation). SPRC_API * sprc_require_auth(1) — refuse datagrams without a valid tag. SPRC_API * sprc_set_peer_filter() — the origin check the C# commented out. SPRC_API * sprc_arm_uri_commands(1) — refuse PlayUri / LoadSubtitle / Open unless * armed. These three take a path/URL from the * network. * * OFF BY DEFAULT MEANS UNAUTHENTICATED. * * ⚠️ The wire field is misspelled "DeivceCode" (sic) — load-bearing, do not fix. * * All string results point into internal static buffers, valid until the next * call to the same function. Copy immediately. */ #ifndef SCORPIOX_SCORPIOPLAYER_REMOTE_CONTROL_PROTOCOL_H #define SCORPIOX_SCORPIOPLAYER_REMOTE_CONTROL_PROTOCOL_H #include #include /* Windows build: the version script is replaced by a /DEF file and * dllexport is not needed for MSVC/MinGW shared libs with explicit exports. * On Windows define SPRC_BUILD_DLL to add dllexport to every declaration. */ #ifndef SPRC_API #ifdef SPRC_BUILD_DLL #define SPRC_API __declspec(dllexport) #else #define SPRC_API #endif #endif #ifdef __cplusplus extern "C" { #endif /* ------------------------------ status ---------------------------------- */ #define SPRC_OK 0 #define SPRC_ERR_INVALID_ARG 1 #define SPRC_ERR_INFLATE 2 #define SPRC_ERR_BOMB 3 #define SPRC_ERR_JSON 4 #define SPRC_ERR_UNKNOWN_COMMAND 5 #define SPRC_ERR_TOO_LARGE 6 #define SPRC_ERR_UNAUTHENTICATED 7 #define SPRC_ERR_PEER_REJECTED 8 #define SPRC_ERR_NOT_ARMED 9 #define SPRC_ERR_BIND 10 #define SPRC_ERR_TIMEOUT 11 #define SPRC_ERR_OOM 12 /* v1.1.0 additions (D29) */ #define SPRC_ERR_PROTOCOL 13 /* v2 envelope at a v0 listener / v0 at a v2 * listener without allow_legacy / unknown * key_id / bad version byte / bad header */ #define SPRC_ERR_REPLAY 14 /* counter inside the per-sender 64-window */ #define SPRC_ERR_FRAGMENT 15 /* reassembly overflow / timeout */ #define SPRC_ERR_PAIRING 16 /* bad code / expired / wrong-code rate limit */ #define SPRC_MAX_DATAGRAM 65536u /* max UDP payload */ #define SPRC_MAX_INFLATE (4u * 1024u * 1024u) /* default absolute cap */ #define SPRC_MAX_RATIO 100 /* default inflate ratio */ #define SPRC_MAX_JSON (16u * 1024u * 1024u) /* decoded-JSON parse cap */ /* v1.1.0 auth v2 (D22-D27) */ #define SPRC_V2_VERSION 2 #define SPRC_V2_HEADER_LEN 29u /* version..frag_len, big-endian (D23) */ #define SPRC_V2_TAG_LEN 32u /* HMAC-SHA256 over header+fragment */ #define SPRC_V2_OVERHEAD (SPRC_V2_HEADER_LEN + SPRC_V2_TAG_LEN) #define SPRC_V2_MTU 1472u /* max bytes per v2 datagram (D27) */ #define SPRC_V2_FRAG_MAX (SPRC_V2_MTU - SPRC_V2_OVERHEAD) /* 1,411 B */ #define SPRC_V2_REASSEMBLY_MAX (128u * 1024u) /* per-message reassembly cap */ #define SPRC_V2_MSG_MAX 4 /* in-flight messages */ #define SPRC_V2_MSG_TIMEOUT_MS 10000 #define SPRC_V2_REPLAY_WINDOW 64u #define SPRC_V2_KEY_ID_CURRENT 0u #define SPRC_V2_SENDER_PLAYER 1 #define SPRC_V2_SENDER_PHONE 2 #define SPRC_V2_PAIRING_TTL_S 300 #define SPRC_V2_PAIRING_MAX_WRONG 5 /* within 60 s (D25) */ #define SPRC_V2_PAIRING_SKEW_S 120 /* v1.2.0: clock skew tolerated between devices */ /* --------------------------- command table ------------------------------ */ /* 41 commands, ordinals = declaration order in Core/SPRC/SPRCCommand.cs. * The wire format is the NUMBER (SPEC D2). Inserting a command anywhere but * the end of this table breaks compatibility. */ enum { SPRC_CMD_TOGGLE_PLAY_PAUSE = 0, SPRC_CMD_PLAY, SPRC_CMD_PAUSE, SPRC_CMD_STOP, SPRC_CMD_RECORD, SPRC_CMD_FAST_FORWARD, SPRC_CMD_REWIND, SPRC_CMD_NEXT, SPRC_CMD_PREVIOUS, SPRC_CMD_CHANNEL_UP, SPRC_CMD_CHANNEL_DOWN, SPRC_CMD_PLAY_LIST, SPRC_CMD_FAVORITES_FOLDERS, SPRC_CMD_FAVORITES_FILES, SPRC_CMD_RECENT_FILES, SPRC_CMD_OPEN_ID, SPRC_CMD_OPEN_FOLDER, SPRC_CMD_PING, SPRC_CMD_GO_BACK, SPRC_CMD_CURRENT_STATE, SPRC_CMD_PROGRESS, SPRC_CMD_SEEK, SPRC_CMD_AUDIO_TRACKS, SPRC_CMD_SUBTITLE_TRACKS, SPRC_CMD_VOLUME, SPRC_CMD_SET_SUBTITLE_TRACK, SPRC_CMD_SET_AUDIO_TRACK, SPRC_CMD_SET_VOLUME, SPRC_CMD_TOGGLE_MUTED, SPRC_CMD_RESTART, SPRC_CMD_OPEN, SPRC_CMD_LOCK, SPRC_CMD_RATE, SPRC_CMD_FULL_SCREEN, SPRC_CMD_DETAIL_VIEW, SPRC_CMD_DECODE, SPRC_CMD_EFFECT, SPRC_CMD_START_CAST, SPRC_CMD_STOP_CAST, SPRC_CMD_LOAD_SUBTITLE, SPRC_CMD_PLAY_URI, SPRC_CMD_COUNT /* = 41 */ }; /* SPRCMediaPlaybackState (Core/SPRC/SPRCMediaPlaybackState.cs) */ enum { SPRC_STATE_NONE = 0, SPRC_STATE_OPENING, SPRC_STATE_BUFFERING, SPRC_STATE_PLAYING, SPRC_STATE_PAUSED }; /* ------------------------------- errors --------------------------------- */ SPRC_API const char *sprc_last_error(void); /* e.g. "ERR_BOMB" */ SPRC_API int sprc_warning_count(void); SPRC_API const char *sprc_warning(int i); /* "bz2-refused", ... */ SPRC_API void sprc_warnings_clear(void); SPRC_API const char *sprc_result_json(void); /* last decoded payload JSON */ SPRC_API int sprc_last_peer_count(void); /* opaque peer tokens (transport) */ /* --------------------- codec: THE PRIMARY SURFACE ----------------------- */ /* Encode: payload_json (UTF-8, an object) → zlib datagram bytes. * If a shared secret is set, the output is 32-byte HMAC tag || zlib bytes; * with no secret the output is byte-identical to the legacy sender. * Returns 0 = OK (len set), -1 on error. */ SPRC_API int sprc_encode(const char *payload_json, unsigned char *out, size_t cap, size_t *len); /* Decode: one datagram → parse. Bounded (SPEC D8). Returns status (D10). */ SPRC_API int sprc_decode(const unsigned char *datagram, size_t len); /* After a successful sprc_decode: */ SPRC_API int sprc_command(void); /* 0-40, SPRC_ERR_UNKNOWN_COMMAND on failure */ SPRC_API int sprc_command_id(const char *name);/* case-insensitive name → id, -1 if unknown */ SPRC_API const char *sprc_command_name(int id); /* NULL if id out of range */ SPRC_API int sprc_command_count(void); /* 41 */ SPRC_API int sprc_contract_level(void); SPRC_API const char *sprc_device_code(void); /* wire key "DeivceCode" (sic) */ SPRC_API int sprc_state(void); /* 0-4 or -1 absent */ SPRC_API unsigned long long sprc_position(void); SPRC_API int sprc_volume_permille(void); /* Volume*1000, -1 absent, -2 not finite */ SPRC_API int sprc_is_muted(void); /* 1/0, -1 absent */ SPRC_API int sprc_is_locked(void); /* 1/0, -1 absent */ SPRC_API int sprc_is_fullscreen(void); /* 1/0, -1 absent */ SPRC_API int sprc_unlock_tristate(void); /* -1 absent, 0 false, 1 true */ SPRC_API int sprc_rate_permille(void); /* Rate*1000, -1 absent, -2 not finite */ SPRC_API int sprc_open_id(void); /* Open enum value or -1 absent */ SPRC_API int sprc_effect_id(void); /* Effect enum value or -1 absent */ SPRC_API int sprc_decode_id(void); /* Decode enum value or -1 absent */ SPRC_API const char *sprc_uri(void); /* NULL if absent */ SPRC_API const char *sprc_imdb_id(void); /* NULL if absent */ SPRC_API const char *sprc_poster(void); /* NULL if absent */ /* Progress: */ SPRC_API int sprc_progress_present(void); /* 1/0 */ SPRC_API long long sprc_progress_duration_ms(void); /* -1 absent/unparseable */ SPRC_API long long sprc_progress_current_ms(void); SPRC_API const char *sprc_progress_current_text(void); /* NULL if absent */ SPRC_API const char *sprc_progress_remain_text(void); /* arrays: */ SPRC_API int sprc_playlist_count(void); /* field of PlayList[i]: "DisplayName","ContentType","MusicArtist","MusicTitle", * "Name","Id","SeasonEpisode","MetaString" (string); "Size" (int64 text); * "NaturalDuration","Current","DateModified" (raw wire text); * "IMDb.imdbID","IMDb.Title","IMDb.Year","IMDb.Poster", ... (any kept IMDb field). * Returns NULL if item/field absent. */ SPRC_API const char *sprc_playlist_field(int i, const char *field); SPRC_API int sprc_recents_count(void); SPRC_API const char *sprc_recents_field(int i, const char *field); SPRC_API int sprc_folders_count(void); SPRC_API const char *sprc_folder_name(int i); SPRC_API int sprc_folder_has_subfolder(int i); /* 1/0, -1 absent */ SPRC_API int sprc_audio_track_count(void); SPRC_API const char *sprc_audio_track_name(int i); SPRC_API int sprc_audio_track_index(int i); SPRC_API int sprc_subtitle_track_count(void); SPRC_API const char *sprc_subtitle_track_name(int i); SPRC_API int sprc_subtitle_track_index(int i); /* single Item / Folder / Track accessors: */ SPRC_API const char *sprc_item_field(const char *field); SPRC_API const char *sprc_folder_field(const char *field); SPRC_API const char *sprc_audio_track_field(const char *field); SPRC_API const char *sprc_subtitle_track_field(const char *field); SPRC_API const char *sprc_effect_status_field(int i, const char *field); /* "Effect","IsOn" */ SPRC_API int sprc_effect_status_count(void); SPRC_API int sprc_decode_status_field(const char *field); /* 1/0, -1 absent */ /* ------------------------------- limits --------------------------------- */ SPRC_API void sprc_set_max_inflate(size_t bytes); /* absolute cap, default 4 MiB */ SPRC_API void sprc_set_max_ratio(int ratio); /* default 100 */ /* ------------------------------- builders ------------------------------- */ /* Build a payload JSON object, then sprc_encode it. SPRC_API * sprc_build_begin(command) starts a new build (resets). Fields are set in * call order; unknown field names are an error (typo guard). */ SPRC_API int sprc_build_begin(int command); SPRC_API int sprc_build_set_str(const char *field, const char *v); /* NULL v = omit */ SPRC_API int sprc_build_set_i64(const char *field, long long v); SPRC_API int sprc_build_set_bool(const char *field, int v); SPRC_API int sprc_build_set_unlock(int tristate); /* -1 omit, 0, 1 */ SPRC_API int sprc_build_add_playlist_item(const char *item_json); /* applies IMDb slimming */ SPRC_API int sprc_build_add_recents_item(const char *item_json); /* as-is */ SPRC_API int sprc_build_add_folder(const char *name, int has_subfolder); SPRC_API int sprc_build_add_audio_track(const char *display_name, int index); SPRC_API int sprc_build_add_subtitle_track(const char *display_name, int index); SPRC_API int sprc_build_set_progress(long long duration_ms, long long current_ms, const char *current_text, const char *remain_text); SPRC_API int sprc_build_set_item_json(const char *item_json); /* Item, as-is */ SPRC_API int sprc_build_set_folder_json(const char *folder_json); /* Folder, as-is */ SPRC_API int sprc_build_add_effect_status(int effect, int is_on); SPRC_API int sprc_build_set_decode_status(int force_video, int force_audio, int system); SPRC_API int sprc_build_set_audio_track(const char *display_name, int index); /* single AudioTrack */ SPRC_API int sprc_build_set_subtitle_track(const char *display_name, int index); /* single SubtitleTrack */ SPRC_API int sprc_build_finish(char *buf, size_t cap, size_t *len); /* → JSON */ SPRC_API size_t sprc_build_size_estimate(void); /* MTU check before send */ /* --------------------- transport (optional, injectable) ------------------ */ typedef struct sprc_transport { void *ctx; int (*bind)(void *ctx, int port, int *bound_port); /* 0 = OK */ int (*send_to)(void *ctx, int peer, const unsigned char *d, size_t n); int (*recv_from)(void *ctx, int timeout_ms, unsigned char *buf, size_t cap, size_t *n, int *peer); void (*close)(void *ctx); } sprc_transport; typedef int (*sprc_peer_filter_fn)(int peer, void *ctx); /* 0 = accept */ SPRC_API int sprc_set_transport(const sprc_transport *t); /* NULL = built-in UDP */ SPRC_API int sprc_listen(int port); /* port 0 = ephemeral */ SPRC_API int sprc_listen_bound_port(void); SPRC_API int sprc_poll(int timeout_ms); /* OK = one decoded datagram */ SPRC_API int sprc_last_peer(void); /* opaque token, NOT an address */ SPRC_API int sprc_send_to(int peer, const unsigned char *datagram, size_t len); SPRC_API int sprc_stop(void); /* --------------------- security (OFF = unauthenticated) ------------------ */ SPRC_API void sprc_set_shared_secret(const char *secret); /* NULL/"" = off */ SPRC_API void sprc_require_auth(int on); /* only with a secret */ SPRC_API void sprc_set_peer_filter(sprc_peer_filter_fn fn, void *ctx); SPRC_API void sprc_arm_uri_commands(int armed); /* PlayUri/LoadSubtitle/Open */ /* ============================ v1.1.0 — auth v2 =========================== */ /* * SPRC v2 = a real default-on auth mode for a phone <-> player pair. * Envelope: ver(1)|key_id(4 BE)|sender_id(4 BE)|counter(8 BE)|msg_id(4 BE)| * frag_idx(2 BE)|frag_count(2 BE)|frag_len(4 BE)|fragment|HMAC(32) * tag = HMAC-SHA256(direction key, header || fragment); verified before * reassembly/inflation. 64-position sliding counter window per sender * (replay -> ERR_REPLAY). Keys derive via HKDF-SHA256 from one 256-bit * pairing secret, direction-separated (D26). Fragments at <= 1,472 B per * datagram (D27). Legacy v0 = ContractLevel 0, byte-identical to 2017; * a v2 listener refuses it unless sprc_allow_legacy(1) (D22/D28). * * Roles: sender_id 1 = player, 2 = phone. A device registers ONE slot with * its own sender_id (the pairing secret derives both direction keys); * sprc_v2_send(payload, receiver_id, cb, ctx) tags the envelope with the * device's own sender_id and is refused if it would address itself. A * reflected packet therefore fails at slot selection AND at the HMAC. * * v2 is HMAC-only, NOT encrypted (D26, documented decision). */ /* one datagram of a v2 send, handed to the shell (D27) */ typedef void (*sprc_v2_fragment_fn)(void *ctx, const unsigned char *datagram, size_t len, int frag_idx, int frag_count); /* counter persistence: shell stores (sender_id, counter) so a reboot resumes * (D23). Engine keeps in-process state only. */ typedef void (*sprc_v2_counter_fn)(void *ctx, unsigned int sender_id, unsigned long long counter); /* deterministic clock for tests/fuzz (D30): milliseconds, monotonic ok */ typedef unsigned long long (*sprc_v2_clock_fn)(void); /* register a pairing secret for this device. Derives both direction keys * (HKDF-SHA256) and marks this device as the given sender_id (1 = player, * 2 = phone). Returns SPRC_OK. */ SPRC_API int sprc_v2_register_key(unsigned int sender_id, const char *secret_b64, unsigned int key_id); SPRC_API void sprc_v2_reset_keys(void); /* wipe all keys */ SPRC_API int sprc_v2_key_registered(void); /* any v2 key? */ /* version negotiation (D22/D28) */ SPRC_API void sprc_allow_legacy(int on); /* accept v0 on a v2 listener */ SPRC_API int sprc_negotiated_level(void); /* 0 or 2, last decoded */ /* pairing (D25) */ SPRC_API int sprc_v2_begin_pairing(const char *device_id, char *out_json, size_t cap); SPRC_API int sprc_v2_accept_pairing(const char *json, unsigned int role_sender_id); /* v1.2.0 — CROSS-DEVICE pairing. * "expires" is UNIX seconds (v1.1.0 wrote CLOCK_MONOTONIC seconds, which the * other device cannot interpret). * accept_pairing in a process that did NOT begin the pairing (the phone) * verifies the JSON on its own: 32-byte secret, code == the code the secret * derives, expires in the future within SPRC_V2_PAIRING_SKEW_S. In the * process that began it, the v1.1.0 exact match still applies. * sprc_v2_pairing_code: the 6 digits a secret derives (out7 >= 7 bytes). */ SPRC_API int sprc_v2_pairing_code(const char *secret_b64, char *out7, size_t cap); /* sender side (D27): compress once, fragment, hand every datagram to cb. * receiver_id = the peer's sender_id (1 = player, 2 = phone): the envelope * is tagged with this device's own sender_id, and the slot is the one that * does NOT claim receiver_id as its own. */ SPRC_API int sprc_v2_send(const char *payload_json, unsigned int receiver_id, sprc_v2_fragment_fn cb, void *ctx); SPRC_API unsigned long long sprc_v2_next_counter(void); /* sender-side counter */ /* replay window / reassembly / counter persistence */ SPRC_API void sprc_v2_set_counter_sink(sprc_v2_counter_fn fn, void *ctx); SPRC_API void sprc_v2_set_clock(sprc_v2_clock_fn fn); SPRC_API int sprc_v2_reassembly_pending(void); /* in-flight messages */ /* raw envelope construction/verification (for tests, harnesses, fuzz): * build one datagram (fragment i of n) from a compressed stream slice, * or verify an existing one. tag in/out = 32 bytes. */ SPRC_API int sprc_v2_build_datagram(unsigned int key_id, unsigned int sender_id, unsigned long long counter, unsigned int msg_id, unsigned int frag_idx, unsigned int frag_count, const unsigned char *zlib_fragment, size_t frag_len, const unsigned char *key32, unsigned char *out, size_t cap, size_t *len); SPRC_API int sprc_v2_verify_datagram(const unsigned char *datagram, size_t len, const unsigned char *key32); /* 0 ok, else -1 */ #ifdef __cplusplus } #endif #endif /* SCORPIOX_SCORPIOPLAYER_REMOTE_CONTROL_PROTOCOL_H */