tlsgate

TLS reverse proxy
git clone git://git.akobets.xyz/tlsgate
Log | Files | Refs | README | LICENSE

commit 9760ff09a3ba8f0aaf91722317f29c01e14acd8c
parent 9e09a6a11c484eb9f92888c4692eb742891a4dee
Author: Artem Kobets <artem@akobets.xyz>
Date:   Sat, 22 Aug 2020 15:51:45 +0300

add socket timeout

Diffstat:
Mmain.c | 7+++++++
Msock.c | 17+++++++++++++++++
Msock.h | 1+
3 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/main.c b/main.c @@ -16,6 +16,8 @@ #include "sock.h" #include "util.h" +#define SOCK_TIMEOUT_SECS 30 + char *argv0; void sigchld(int unused); @@ -151,6 +153,9 @@ main(int argc, char **argv) case 0: close(fd); + if (sock_set_timeout(clientfd, SOCK_TIMEOUT_SECS) == -1) + goto cleanup; + /* start tls */ if (tls_accept_socket(ctx, &cctx, cfd) < 0) { warn("tls_accept_socket: %s\n", strerror(errno)); @@ -167,6 +172,8 @@ main(int argc, char **argv) : sock_client_ips(client_host, client_port); if (clientfd < 0) goto cleanup; + if (sock_set_timeout(clientfd, SOCK_TIMEOUT_SECS) == -1) + goto cleanup; serve(cctx, clientfd); diff --git a/sock.c b/sock.c @@ -2,6 +2,7 @@ #include <stdio.h> #include <string.h> #include <unistd.h> +#include <sys/time.h> #include <sys/socket.h> #include <arpa/inet.h> @@ -114,3 +115,19 @@ sock_client_uds(const char *file) return fd; } + +int +sock_set_timeout(int fd, int sec) +{ + struct timeval time; + + time.tv_sec = sec; + time.tv_usec = 0; + if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &time, sizeof(time)) == -1 || + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &time, sizeof(time)) == -1) { + warn("failed to set socket timeout: %s\n", strerror(errno)); + return -1; + } + + return 0; +} diff --git a/sock.h b/sock.h @@ -1,3 +1,4 @@ int sock_server_ips(const char *host, const char *port); int sock_client_ips(const char *host, const char *port); int sock_client_uds(const char *file); +int sock_set_timeout(int fd, int sec);