tests: hold OS-assigned HTTP and SOCKS listener ports
This commit is contained in:
1 parent
08865b2909
commit
4e2cc0e35a
4 files changed
+53
-51
No files matched your search
+14
-3
@@ -909,8 +909,12 @@ struct listen_ctx* socks_proxy_init_listen(struct UASYNC* ua, const char* addr_s
|
||||
size_t ip_len = (size_t)(colon - addr_str);
|
||||
if (ip_len > sizeof(ip) - 1) ip_len = sizeof(ip) - 1;
|
||||
memcpy(ip, addr_str, ip_len); ip[ip_len] = '\0';
|
||||
int port = atoi(colon + 1);
|
||||
if (port <= 0 || port > 65535) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: bad port %d", port); return NULL; }
|
||||
char* end;
|
||||
long parsed = strtol(colon + 1, &end, 10);
|
||||
if (end == colon + 1 || *end || parsed < 0 || parsed > 65535) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: bad port '%s'", colon + 1); return NULL;
|
||||
}
|
||||
int port = (int)parsed;
|
||||
|
||||
struct listen_ctx* ctx = u_calloc(1, sizeof(struct listen_ctx));
|
||||
if (!ctx) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: u_calloc failed"); return NULL; }
|
||||
@@ -928,16 +932,23 @@ struct listen_ctx* socks_proxy_init_listen(struct UASYNC* ua, const char* addr_s
|
||||
if (inet_pton(AF_INET, ip, &addr.sin_addr) != 1) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: inet_pton(%s) failed", ip); socket_close_wrapper(sock); u_free(ctx); return NULL; }
|
||||
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: bind(%s:%d) failed errno=%d", ip, port, errno); socket_close_wrapper(sock); u_free(ctx); return NULL; }
|
||||
if (listen(sock, 32) < 0) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: listen() failed errno=%d", errno); socket_close_wrapper(sock); u_free(ctx); return NULL; }
|
||||
uint16_t bound_port = socket_get_bound_port(sock);
|
||||
if (!bound_port) { socket_close_wrapper(sock); u_free(ctx); return NULL; }
|
||||
|
||||
ctx->listen_sock = sock;
|
||||
ctx->socket_id = uasync_add_socket_t(ua, sock, on_accept_cb, NULL, NULL, "socks_proxy_listen", ctx);
|
||||
if (!ctx->socket_id) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: uasync_add_socket_t failed"); socket_close_wrapper(sock); u_free(ctx); return NULL; }
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_PROXY, "socks_proxy: %s listening on %s:%d sock=%d",
|
||||
is_http ? "HTTP" : "SOCKS", ip, port, (int)sock);
|
||||
is_http ? "HTTP" : "SOCKS", ip, bound_port, (int)sock);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
uint16_t socks_proxy_get_port(const struct listen_ctx* ctx) {
|
||||
if (!ctx) { DEBUG_ERROR(DEBUG_CATEGORY_PROXY, "socks_proxy: missing listener"); return 0; }
|
||||
return socket_get_bound_port(ctx->listen_sock);
|
||||
}
|
||||
|
||||
void socks_proxy_close_listen(struct UASYNC* ua, struct listen_ctx* ctx, socket_t* sock_out) {
|
||||
if (!ctx) return;
|
||||
if (ctx->socket_id) uasync_remove_socket_t(ua, ctx->listen_sock);
|
||||
|
||||
@@ -83,6 +83,8 @@ struct listen_ctx* socks_proxy_init_listen(struct UASYNC* ua, const char* addr_s
|
||||
// Закрыть слушающий сокет и освободить listen_ctx.
|
||||
// Если sock_out задан, в него записывается SOCKET_INVALID: сокет уже закрыт.
|
||||
void socks_proxy_close_listen(struct UASYNC* ua, struct listen_ctx* ctx, socket_t* sock_out);
|
||||
// Фактический порт активного listener (IP:0 выбирает ОС); 0 при ошибке.
|
||||
uint16_t socks_proxy_get_port(const struct listen_ctx* ctx);
|
||||
|
||||
// Найти соединение по stream_id
|
||||
struct socks_proxy_conn* socks_proxy_find_conn(struct socks_proxy_conn* head, uint32_t stream_id);
|
||||
|
||||
Reference in new issue
Block a user