文件

74 行
3.0 KiB
C

/* ssh_tcpip.h —— SSH 端口转发引擎 (-L 本地 / -R 远程 / -D 动态 SOCKS5)
*
* 用法:
* fwd = ssh_fwd_new();
* ssh_fwd_add_local(fwd, s, SSH_FWD_L, "127.0.0.1", 8080, "web", 80);
* ssh_fwd_add_remote(fwd, s, "127.0.0.1", 9000, "db", 5432);
* ssh_fwd_add_local(fwd, s, SSH_FWD_D, "127.0.0.1", 1080, NULL, 0);
* 事件循环:
* ssh_fwd_prepare(fwd, &rfds, &wfds, &maxfd); // 注册 fd
* select(...);
* ssh_fwd_pump(fwd, s, &rfds, &wfds); // 处理本地事件
* // SSH 数据由调用方 ssh_channel_recv_data 取出后:
* ssh_fwd_channel_data(fwd, rid, buf, n); // 分发到本地 socket
* // 服务器主动发来 forwarded-tcpip 时:
* ssh_channel_accept_forwarded(...) → ssh_fwd_remote_connect(fwd, s, ch, host, port)
*/
#ifndef PAZE_SSH_TCPIP_H
#define PAZE_SSH_TCPIP_H
/* fd_set / SOCKET 平台类型:必须先于本头文件使用处定义 */
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
#include "paze/ssh.h"
#ifdef __cplusplus
extern "C" {
#endif
#define SSH_FWD_L 0 /* 本地转发 direct-tcpip */
#define SSH_FWD_D 1 /* 动态转发 SOCKS5 */
#define SSH_FWD_R 2 /* 远程转发 forwarded-tcpip */
typedef struct ssh_fwd ssh_fwd_t;
ssh_fwd_t *ssh_fwd_new(void);
void ssh_fwd_free(ssh_fwd_t *f);
/* -L/-D:本地监听 bind:port。kind=SSH_FWD_L 需 target/target_port;
* kind=SSH_FWD_D 忽略 target。返回 0 成功。 */
int ssh_fwd_add_local(ssh_fwd_t *f, ssh_session_t *s, int kind,
const char *bind, int port,
const char *target, int target_port);
/* -R:请求远端监听 bind:port,连接转发到本地 target:target_port。
* actual_port 输出服务器实际绑定端口(请求 0 时)。 */
int ssh_fwd_add_remote(ssh_fwd_t *f, ssh_session_t *s,
const char *bind, int port,
const char *target, int target_port,
uint32_t *actual_port);
/* 事件循环:把本层 fd 加入集合。maxfd 更新为最大 fd (POSIX)。 */
void ssh_fwd_prepare(ssh_fwd_t *f, fd_set *r, fd_set *w, int *maxfd);
/* 处理监听 accept / 连接可读(转发数据到通道) / SOCKS5 握手。 */
int ssh_fwd_pump(ssh_fwd_t *f, ssh_session_t *s, fd_set *r, fd_set *w);
/* 通道数据分发到本地 socket。返回写入字节数;0=非本层通道。 */
int ssh_fwd_channel_data(ssh_fwd_t *f, uint32_t rid,
const uint8_t *data, size_t n);
/* -R:服务器 pushed forwarded-tcpip 通道,建立到本地 target 的连接。
* 返回 0 成功,-1 未匹配(调用方应关闭通道)。 */
int ssh_fwd_remote_connect(ssh_fwd_t *f, ssh_session_t *s, uint32_t ch,
const char *host, uint32_t port);
/* 通道被对端关闭(EOF/CLOSE)时清理对应本地连接。rid 为本地通道 id。 */
void ssh_fwd_channel_closed(ssh_fwd_t *f, uint32_t rid);
/* 当前活动转发连接数 */
int ssh_fwd_active_count(const ssh_fwd_t *f);
#ifdef __cplusplus
}
#endif
#endif /* PAZE_SSH_TCPIP_H */