Add sock_set_timeout()

This commit is contained in:
2022-12-31 01:29:36 +01:00
parent 96f3225f51
commit 1547805e4c
4 changed files with 25 additions and 15 deletions

View File

@ -47,6 +47,7 @@ void proxy_unload(void) {
} }
static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) { static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) {
// TODO locking void *proxies
int n = 0; int n = 0;
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) { for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
host_config_t *hc = &config.hosts[i]; host_config_t *hc = &config.hosts[i];
@ -222,7 +223,6 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
const char *connection, *upgrade, *ws_version; const char *connection, *upgrade, *ws_version;
long ret; long ret;
int tries = 0, retry = 0; int tries = 0, retry = 0;
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
proxy_ctx_t *proxy = proxy_get_by_conf(conf); proxy_ctx_t *proxy = proxy_get_by_conf(conf);
proxy->in_use = 1; proxy->in_use = 1;
@ -247,11 +247,7 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
return NULL; return NULL;
} }
server_timeout.tv_sec = SERVER_TIMEOUT_INIT; if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT_INIT) != 0)
server_timeout.tv_usec = 0;
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
goto proxy_timeout_err;
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
goto proxy_timeout_err; goto proxy_timeout_err;
struct hostent *host_ent = gethostbyname2(conf->proxy.hostname, AF_INET6); struct hostent *host_ent = gethostbyname2(conf->proxy.hostname, AF_INET6);
@ -294,11 +290,7 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
goto proxy_err; goto proxy_err;
} }
server_timeout.tv_sec = SERVER_TIMEOUT; if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT) != 0) {
server_timeout.tv_usec = 0;
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_RCVTIMEO, &server_timeout, sizeof(server_timeout)) < 0)
goto proxy_timeout_err;
if (setsockopt(proxy->proxy.socket, SOL_SOCKET, SO_SNDTIMEO, &server_timeout, sizeof(server_timeout)) < 0) {
proxy_timeout_err: proxy_timeout_err:
res->status = http_get_status(500); res->status = http_get_status(500);
ctx->origin = INTERNAL; ctx->origin = INTERNAL;

View File

@ -57,6 +57,23 @@ const char *sock_strerror(sock *s) {
} }
} }
int sock_set_timeout_micros(sock *s, long recv_micros, long send_micros) {
struct timeval recv_to = {.tv_sec = recv_micros / 1000000, .tv_usec = recv_micros % 1000000},
send_to = {.tv_sec = send_micros / 1000000, .tv_usec = send_micros % 1000000};
if (setsockopt(s->socket, SOL_SOCKET, SO_RCVTIMEO, &recv_to, sizeof(recv_to)) != 0)
return -1;
if (setsockopt(s->socket, SOL_SOCKET, SO_SNDTIMEO, &send_to, sizeof(send_to)) != 0)
return -1;
return 0;
}
int sock_set_timeout(sock *s, int sec) {
return sock_set_timeout_micros(s, sec * 1000000L, sec * 1000000L);
}
long sock_send(sock *s, void *buf, unsigned long len, int flags) { long sock_send(sock *s, void *buf, unsigned long len, int flags) {
long ret; long ret;
if (s->enc) { if (s->enc) {

View File

@ -32,6 +32,10 @@ int sock_enc_error(sock *s);
const char *sock_strerror(sock *s); const char *sock_strerror(sock *s);
int sock_set_timeout_micros(sock *s, long recv_micros, long send_micros);
int sock_set_timeout(sock *s, int sec);
long sock_send(sock *s, void *buf, unsigned long len, int flags); long sock_send(sock *s, void *buf, unsigned long len, int flags);
long sock_recv(sock *s, void *buf, unsigned long len, int flags); long sock_recv(sock *s, void *buf, unsigned long len, int flags);

View File

@ -92,10 +92,7 @@ static int tcp_acceptor(client_ctx_t *ctx) {
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "", ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
ctx->cc[0] != 0 ? ctx->cc : "N/A"); ctx->cc[0] != 0 ? ctx->cc : "N/A");
struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0}; if (sock_set_timeout(client, CLIENT_TIMEOUT)) {
if (setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO, &client_timeout, sizeof(client_timeout)) == -1 ||
setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &client_timeout, sizeof(client_timeout)) == -1)
{
error("Unable to set timeout for socket"); error("Unable to set timeout for socket");
return -1; return -1;
} }