Files
mguinhosandClaude Opus 5.5 066bb295ed tinyblocks-socket, -ssl, -http e -archive: internet e arquivos compactados em blocos
socket: conexao TCP como TbStream (fluxo com classe read/write/close) e
blocos para conectar, enviar, receber e resolver endereco.
ssl: embrulha um TbStream TCP em TLS (OpenSSL), com SNI e certificado
conferido pelo sistema e pelo nome do host.
http: cliente HTTP/1.1 so sobre TbStream; https:// quando a ssl esta
carregada; Content-Length, chunked e redirecionamentos; GET e POST de
texto e download em segundo plano (.parte ate terminar) com progresso.
archive: lista e extrai .tar e .tar.gz (zlib), nomes longos GNU/pax;
recusa entradas com "../" ou caminho absoluto.

As camadas se acham pelas tabelas no lib_userdata, sem linkar umas com as
outras. test-http usa um servidor local (e a internet, se houver);
test-archive cria os arquivos com tar e Python.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 23:27:08 -03:00

66 lines
2.7 KiB
C

/*
* TinyBlocks - tb_socket.h
* Library tinyblocks-socket: conexoes TCP (sockets POSIX). Registra a
* categoria "Socket".
*
* Define a interface TbStream, um fluxo de bytes com classe (read, write,
* close). A conexao TCP e um TbStream; a tinyblocks-openssl embrulha um
* TbStream TCP num TbStream TLS; a tinyblocks-http so fala com TbStream, sem
* saber se ha TLS por baixo.
*
* Como as outras libraries chegam aqui sem linkar: a tabela TbSocketApi fica
* no lib_userdata da library (tb_socket_api(rt)).
*
* Blocos: "conecte a (host) porta (p)" devolve a conexao (um bloco-handle);
* "envie", "receba uma linha", "receba ate N bytes", "feche" e
* "endereco de (host)".
*/
#ifndef TINYBLOCKS_SOCKET_H
#define TINYBLOCKS_SOCKET_H
#include <stdbool.h>
#include <stddef.h>
#include <tinyblocks/tinyblocks.h>
#define TB_SOCKET_LIB "tinyblocks-socket"
#define TB_SOCKET_API_VERSION 1
typedef struct TbStream TbStream;
/* Classe de um fluxo: read devolve bytes lidos (0 = fim, -1 = erro) */
typedef struct TbStreamClass {
const char* name; /* "tcp", "tls" */
long (*read)(TbStream* s, void* buf, size_t cap);
long (*write)(TbStream* s, const void* buf, size_t n);
void (*close)(TbStream* s); /* fecha e libera s */
} TbStreamClass;
struct TbStream {
const TbStreamClass* cls;
int fd; /* socket por baixo (-1 se nenhum) */
void* impl; /* dados da classe (ex.: o SSL*) */
char err[160]; /* ultimo erro deste fluxo */
};
typedef struct TbSocketApi {
int version; /* TB_SOCKET_API_VERSION */
/* TCP: NULL em erro (mensagem em err). timeout_ms <= 0 = 30 s. */
TbStream* (*tcp_connect)(const char* host, int port, int timeout_ms, char* err, size_t cap);
/* Escreve tudo (repete o write ate acabar); false em erro */
bool (*write_all)(TbStream* s, const void* buf, size_t n);
} TbSocketApi;
TB_API const TinyBlocks_LibraryDescriptor* tb_socket_get_library(void);
static inline const TbSocketApi* tb_socket_api(TinyBlocks* rt) {
TinyBlocks_LibraryBlock* lib = tb_rt_find_library(rt, TB_SOCKET_LIB);
const TbSocketApi* api = lib ? (const TbSocketApi*)lib->lib_userdata : NULL;
return api && api->version == TB_SOCKET_API_VERSION ? api : NULL;
}
static inline long tb_stream_read(TbStream* s, void* buf, size_t cap) { return s->cls->read(s, buf, cap); }
static inline long tb_stream_write(TbStream* s, const void* buf, size_t n) { return s->cls->write(s, buf, n); }
static inline void tb_stream_close(TbStream* s) { if (s) s->cls->close(s); }
#endif /* TINYBLOCKS_SOCKET_H */