diff --git a/src/lib/proxy.c b/src/lib/proxy.c index ad383aa..4abd84f 100644 --- a/src/lib/proxy.c +++ b/src/lib/proxy.c @@ -47,6 +47,7 @@ void proxy_unload(void) { } static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) { + // TODO locking void *proxies int n = 0; for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; 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; long ret; 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->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; } - server_timeout.tv_sec = SERVER_TIMEOUT_INIT; - 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) + if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT_INIT) != 0) goto proxy_timeout_err; 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; } - server_timeout.tv_sec = SERVER_TIMEOUT; - 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) { + if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT) != 0) { proxy_timeout_err: res->status = http_get_status(500); ctx->origin = INTERNAL; diff --git a/src/lib/sock.c b/src/lib/sock.c index 32b7c9f..dba1510 100644 --- a/src/lib/sock.c +++ b/src/lib/sock.c @@ -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 ret; if (s->enc) { diff --git a/src/lib/sock.h b/src/lib/sock.h index 0898e97..bf256ed 100644 --- a/src/lib/sock.h +++ b/src/lib/sock.h @@ -32,6 +32,10 @@ int sock_enc_error(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_recv(sock *s, void *buf, unsigned long len, int flags); diff --git a/src/worker/tcp_acceptor.c b/src/worker/tcp_acceptor.c index baecf35..32dc5d9 100644 --- a/src/worker/tcp_acceptor.c +++ b/src/worker/tcp_acceptor.c @@ -92,10 +92,7 @@ static int tcp_acceptor(client_ctx_t *ctx) { ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "", ctx->cc[0] != 0 ? ctx->cc : "N/A"); - struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0}; - 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) - { + if (sock_set_timeout(client, CLIENT_TIMEOUT)) { error("Unable to set timeout for socket"); return -1; }