158 lines
6.8 KiB
Plaintext
158 lines
6.8 KiB
Plaintext
import t, c
|
||
from openssl.types import *
|
||
|
||
# ============================================================
|
||
# openssl/bio.vp — BIO(内存 / 文件 / Socket / Connect 抽象 IO)
|
||
# 对应头文件: bio.h
|
||
#
|
||
# 含 BIO_ADDR/BIO_ADDRINFO:主机名解析(getaddrinfo 封装)+ TCP 连接/读写。
|
||
# ============================================================
|
||
|
||
# ---- BIO_METHOD 获取 ----
|
||
extern def BIO_s_mem() -> BioMethodPtr
|
||
extern def BIO_s_socket() -> BioMethodPtr
|
||
extern def BIO_s_connect() -> BioMethodPtr
|
||
extern def BIO_s_accept() -> BioMethodPtr
|
||
extern def BIO_s_fd() -> BioMethodPtr
|
||
extern def BIO_s_file() -> BioMethodPtr
|
||
extern def BIO_s_null() -> BioMethodPtr
|
||
extern def BIO_f_buffer() -> BioMethodPtr
|
||
|
||
# ---- 创建 / 释放 ----
|
||
# BIO *BIO_new(const BIO_METHOD *type)
|
||
extern def BIO_new(type: BioMethodPtr) -> BioPtr
|
||
# BIO *BIO_new_socket(int sock, int close_flag)
|
||
extern def BIO_new_socket(sock: int, close_flag: int) -> BioPtr
|
||
# BIO *BIO_new_connect(const char *host_port)
|
||
extern def BIO_new_connect(host_port: str) -> BioPtr
|
||
# BIO *BIO_new_accept(const char *host_port)
|
||
extern def BIO_new_accept(host_port: str) -> BioPtr
|
||
# BIO *BIO_new_fd(int fd, int close_flag)
|
||
extern def BIO_new_fd(fd: int, close_flag: int) -> BioPtr
|
||
# BIO *BIO_new_mem_buf(const void *buf, int len)
|
||
extern def BIO_new_mem_buf(buf: bytes, len: int) -> BioPtr
|
||
# BIO *BIO_new_file(const char *filename, const char *mode)
|
||
extern def BIO_new_file(filename: str, mode: str) -> BioPtr
|
||
# int BIO_free(BIO *a) / void BIO_free_all(BIO *a)
|
||
extern def BIO_free(a: BioPtr) -> int
|
||
extern def BIO_free_all(a: BioPtr)
|
||
extern def BIO_vfree(a: BioPtr)
|
||
|
||
# ---- 读写 ----
|
||
# int BIO_read(BIO *b, void *data, int dlen)
|
||
extern def BIO_read(b: BioPtr, data: bytes, dlen: int) -> int
|
||
# int BIO_write(BIO *b, const void *data, int dlen)
|
||
extern def BIO_write(b: BioPtr, data: bytes, dlen: int) -> int
|
||
# int BIO_gets(BIO *bp, char *buf, int size)
|
||
extern def BIO_gets(bp: BioPtr, buf: bytes, size: int) -> int
|
||
# int BIO_puts(BIO *bp, const char *buf)
|
||
extern def BIO_puts(bp: BioPtr, buf: str) -> int
|
||
# size_t BIO_ctrl_pending(BIO *b)
|
||
extern def BIO_ctrl_pending(b: BioPtr) -> t.CSizeT
|
||
# size_t BIO_ctrl_wpending(BIO *b)
|
||
extern def BIO_ctrl_wpending(b: BioPtr) -> t.CSizeT
|
||
|
||
# ---- BIO 控制(宏的底层 ctrl;连接/非阻塞等由它实现)----
|
||
# long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg)
|
||
extern def BIO_ctrl(bp: BioPtr, cmd: int, larg: t.CInt32T, parg: bytes) -> t.CInt32T
|
||
# int BIO_do_connect(BIO *b) —— 宏:BIO_ctrl(b, BIO_C_DO_STATE_MACHINE, 0, NULL)
|
||
BioCDoStateMachine: t.CDefine = 101
|
||
BioCSetConnect: t.CDefine = 100
|
||
BioCSetNbio: t.CDefine = 102
|
||
BioCSetFd: t.CDefine = 104
|
||
BioCGetFd: t.CDefine = 105
|
||
BioNoclose: t.CDefine = 0x00
|
||
BioClose: t.CDefine = 0x01
|
||
# BIO 链
|
||
# BIO *BIO_push(BIO *b, BIO *append) / BIO *BIO_pop(BIO *b)
|
||
extern def BIO_push(b: BioPtr, append: BioPtr) -> BioPtr
|
||
extern def BIO_pop(b: BioPtr) -> BioPtr
|
||
|
||
# ---- 便捷控制封装(对应 bio.h 宏,避免 Viper 端裸用 BIO_ctrl)----
|
||
def bio_set_conn_hostname(b: BioPtr, name: str):
|
||
BIO_ctrl(b, BioCSetConnect, 0, name)
|
||
|
||
|
||
def bio_set_conn_port(b: BioPtr, port: str):
|
||
BIO_ctrl(b, BioCSetConnect, 1, port)
|
||
|
||
|
||
def bio_set_nbio(b: BioPtr, n: int):
|
||
BIO_ctrl(b, BioCSetNbio, n, None)
|
||
|
||
|
||
def bio_do_connect(b: BioPtr) -> int:
|
||
return BIO_ctrl(b, BioCDoStateMachine, 0, None)
|
||
|
||
|
||
# ============================================================
|
||
# Socket 原语(BIO 层直接管 fd)
|
||
# ============================================================
|
||
BioSockReuseaddr: t.CDefine = 0x01
|
||
BioSockV6Only: t.CDefine = 0x02
|
||
BioSockKeepalive: t.CDefine = 0x04
|
||
BioSockNonblock: t.CDefine = 0x08
|
||
BioSockNodelay: t.CDefine = 0x10
|
||
|
||
# int BIO_socket(int domain, int socktype, int protocol, int options)
|
||
extern def BIO_socket(domain: int, socktype: int, protocol: int, options: int) -> int
|
||
# int BIO_connect(int sock, const BIO_ADDR *addr, int options)
|
||
extern def BIO_connect(sock: int, addr: BioAddrPtr, options: int) -> int
|
||
# int BIO_bind(int sock, const BIO_ADDR *addr, int options)
|
||
extern def BIO_bind(sock: int, addr: BioAddrPtr, options: int) -> int
|
||
# int BIO_listen(int sock, const BIO_ADDR *addr, int options)
|
||
extern def BIO_listen(sock: int, addr: BioAddrPtr, options: int) -> int
|
||
# int BIO_accept_ex(int accept_sock, BIO_ADDR *addr, int options)
|
||
extern def BIO_accept_ex(accept_sock: int, addr: BioAddrPtr, options: int) -> int
|
||
# int BIO_closesocket(int sock)
|
||
extern def BIO_closesocket(sock: int) -> int
|
||
# int BIO_sock_init(void)
|
||
extern def BIO_sock_init() -> int
|
||
# int BIO_socket_nbio(int fd, int mode)
|
||
extern def BIO_socket_nbio(fd: int, mode: int) -> int
|
||
# int BIO_set_tcp_ndelay(int sock, int turn_on)
|
||
extern def BIO_set_tcp_ndelay(sock: int, turn_on: int) -> int
|
||
# int BIO_sock_should_retry(int i)
|
||
extern def BIO_sock_should_retry(i: int) -> int
|
||
|
||
# ============================================================
|
||
# BIO_ADDR / BIO_ADDRINFO(网络地址,TCP 解析用)
|
||
# ============================================================
|
||
# BIO_ADDR *BIO_ADDR_new(void) / dup
|
||
extern def BIO_ADDR_new() -> BioAddrPtr
|
||
extern def BIO_ADDR_dup(ap: BioAddrPtr) -> BioAddrPtr
|
||
extern def BIO_ADDR_free(ap: BioAddrPtr)
|
||
# int BIO_ADDR_family(const BIO_ADDR *ap)
|
||
extern def BIO_ADDR_family(ap: BioAddrPtr) -> int
|
||
# unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
|
||
extern def BIO_ADDR_rawport(ap: BioAddrPtr) -> t.CUInt16T
|
||
# int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l)
|
||
extern def BIO_ADDR_rawaddress(ap: BioAddrPtr, p: bytes, l: bytes) -> int
|
||
# char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric) → 返回需 CRYPTO_free
|
||
extern def BIO_ADDR_hostname_string(ap: BioAddrPtr, numeric: int) -> str
|
||
# char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric)
|
||
extern def BIO_ADDR_service_string(ap: BioAddrPtr, numeric: int) -> str
|
||
# char *BIO_ADDR_path_string(const BIO_ADDR *ap)
|
||
extern def BIO_ADDR_path_string(ap: BioAddrPtr) -> str
|
||
|
||
# BIO_ADDRINFO 遍历
|
||
extern def BIO_ADDRINFO_free(bai: BioAddrInfoPtr)
|
||
# const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai)
|
||
extern def BIO_ADDRINFO_next(bai: BioAddrInfoPtr) -> BioAddrInfoPtr
|
||
# int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)
|
||
extern def BIO_ADDRINFO_family(bai: BioAddrInfoPtr) -> int
|
||
# int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)
|
||
extern def BIO_ADDRINFO_socktype(bai: BioAddrInfoPtr) -> int
|
||
# int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)
|
||
extern def BIO_ADDRINFO_protocol(bai: BioAddrInfoPtr) -> int
|
||
# const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai)
|
||
extern def BIO_ADDRINFO_address(bai: BioAddrInfoPtr) -> BioAddrPtr
|
||
|
||
# 主机名解析(getaddrinfo 封装)
|
||
BioLookupClient: t.CDefine = 0
|
||
BioLookupServer: t.CDefine = 1
|
||
# int BIO_lookup(const char *host, const char *service, int lookup_type,
|
||
# int family, int socktype, BIO_ADDRINFO **res)
|
||
extern def BIO_lookup(host: str, service: str, lookup_type: int,
|
||
family: int, socktype: int, res: bytes) -> int
|