TNT/include/bootstrap.h
m1ngsama 3b8a1d18d8 refactor: extract input module (PR2-M5)
Move the interactive session — username read, vim-style key dispatcher,
main poll/redraw/keepalive/idle-timeout loop, the @mention bell, and
the tear-down path — out of ssh_server.c into a dedicated module.

New API (include/input.h):
- input_init()                 -- read TNT_IDLE_TIMEOUT once at startup
- input_run_session(client_t*) -- run the interactive session for an
                                  already-bootstrapped client_t,
                                  including exec_dispatch() short-circuit
                                  when client->exec_command is set
- notify_mentions(content, sender) -- moved from ssh_server.h since
                                  the INSERT-mode send path lives here
                                  too

Renamed: client_handle_session(void *arg) -> input_run_session(client_t *).
The old void* signature was a fossil from when this was a pthread entry;
bootstrap.c calls it synchronously inside its own detached thread.
bootstrap_run() now ends with `input_run_session(client); return NULL;`.

g_idle_timeout is now private to input.c; ssh_server_init() calls
input_init() instead of reading the env directly.

ssh_server.c shrinks from 1026 to 402 lines (-624) -- now down to just
the accept loop, ssh_server_init / ssh_server_start, host-key setup,
client_t I/O API (send/printf/addref/release), and the post-bootstrap
client_channel_* callbacks.  M6 will give those a proper home.

Behaviour is preserved: all moved code is byte-for-byte identical.
2026-05-17 10:01:48 +08:00

39 lines
1.5 KiB
C

#ifndef BOOTSTRAP_H
#define BOOTSTRAP_H
#include "ssh_server.h" /* for client_t and the libssh / arpa includes */
/* Hand-off envelope between the accept loop and the bootstrap thread.
* The accept loop allocates one of these per accepted session, fills it,
* and pthread_create()s a detached bootstrap_run() with this pointer.
* bootstrap_run() owns the struct and the embedded ssh_session, and frees
* both before returning. */
typedef struct {
ssh_session session;
char client_ip[INET6_ADDRSTRLEN];
} accepted_session_t;
/* Read TNT_ACCESS_TOKEN from the environment. Idempotent. Call once
* during startup, before bootstrap_run() can fire on any accepted
* session. */
void bootstrap_init(void);
/* Read the peer IP off an accepted ssh_session into ip_buf. Sets ip_buf
* to "unknown" when the address family is unrecognised or getpeername()
* fails. ip_buf must be at least INET6_ADDRSTRLEN bytes. */
void bootstrap_peer_ip(ssh_session session, char *ip_buf, size_t buf_size);
/* pthread entry point for the per-connection bootstrap thread.
*
* Steps performed before handing control to input_run_session():
* 1. SSH key exchange
* 2. auth (password / none / pubkey, with rate-limit feedback)
* 3. channel open + PTY/shell-or-exec request
* 4. construct a client_t and install its lifetime channel callbacks
*
* On any failure path the connection is torn down and ratelimit /
* connection counters are released; input_run_session() is never
* invoked. Always returns NULL. */
void *bootstrap_run(void *arg);
#endif /* BOOTSTRAP_H */