tlsgate

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

commit a6266e5699566ff9f65089760148beeb7b5877fd
parent ab2ab6e3e97078dc249ab6b839fd4b69ae53221d
Author: Artem Kobets <artem@akobets.xyz>
Date:   Thu,  3 Sep 2020 01:19:24 +0300

err/warn utils

Diffstat:
Mmain.c | 32+++++++++++++++-----------------
Mserve.c | 6++----
Msock.c | 26++++++++++++--------------
Mutil.c | 46++++++++++++++++++++++++++++++++++++++++++++--
Mutil.h | 4+++-
5 files changed, 76 insertions(+), 38 deletions(-)

diff --git a/main.c b/main.c @@ -1,9 +1,7 @@ -#include <err.h> #include <errno.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <sys/resource.h> #include <sys/wait.h> #include <unistd.h> @@ -20,7 +18,7 @@ static void sigchld(int unused); static void usage(void); -static char *argv0; +char *argv0; static void sigchld(int unused) @@ -31,10 +29,10 @@ sigchld(int unused) static void usage(void) { - die( + errx( "usage: %s -c cert -k key [-C ca]\n" " [-h host] -p port [-H host] [-P port] [-U file]\n" - " [-n proc-num]\n", + " [-n proc-num]", argv0 ); } @@ -113,23 +111,23 @@ main(int argc, char **argv) /* process limit */ rlim.rlim_cur = rlim.rlim_max = maxnprocs; if (setrlimit(RLIMIT_NPROC, &rlim) == -1) - die("setrlimit RLIMIT_NPROC: %s\n", strerror(errno)); + err("setrlimit RLIMIT_NPROC"); /* setup tls */ if ((ctx = tls_server()) == NULL) - die("tls_server: %s\n", strerror(errno)); + err("tls_server"); if ((config = tls_config_new()) == NULL) - die("tls_config_new: %s\n", strerror(errno)); + err("tls_config_new"); if (tls_config_set_cert_file(config, cert_file) == -1) - die("tls_config_set_cert_file: %s\n", strerror(errno)); + err("tls_config_set_cert_file"); if (tls_config_set_key_file(config, key_file) == -1) - die("tls_config_set_key_file: %s\n", strerror(errno)); + err("tls_config_set_key_file"); if (ca_file != NULL) { if (tls_config_set_ca_file(config, ca_file) == -1) - die("tls_config_set_ca_file: %s\n", strerror(errno)); + err("tls_config_set_ca_file"); } if (tls_configure(ctx, config) == -1) - die("tls_configure: %s\n", strerror(errno)); + err("tls_configure"); /* setup server socket */ fd = sock_server_ips(server_host, server_port); @@ -148,7 +146,7 @@ main(int argc, char **argv) if ((cfd = accept(fd, NULL, NULL)) == -1) { /* can be interrupted with SIGCHLD */ if (errno != EINTR) - warn("accept: %s\n", strerror(errno)); + warn("accept"); continue; } @@ -164,11 +162,11 @@ main(int argc, char **argv) /* start tls */ if (tls_accept_socket(ctx, &cctx, cfd) == -1) { - warn("tls_accept_socket: %s\n", strerror(errno)); + warn("tls_accept_socket"); goto cleanup; } if (tls_handshake(cctx) == -1) { - warn("tls_handshake: %s\n", tls_error(cctx)); + warnx("tls_handshake: %s", tls_error(cctx)); goto cleanup; } @@ -192,11 +190,11 @@ cleanup: close(cfd); if (clientfd != -1) close(clientfd); - _exit(EXIT_SUCCESS); + _exit(0); break; } case -1: - warn("fork: %s\n", strerror(errno)); + warn("fork"); /* fallthrough */ default: /* close connection in parent */ diff --git a/serve.c b/serve.c @@ -1,7 +1,5 @@ -#include <errno.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <sys/wait.h> #include <unistd.h> @@ -19,7 +17,7 @@ serve(struct tls *ctx, int fd) int pid; pid = fork(); if (pid == -1) { - warn("fork: %s\n", strerror(errno)); + warn("fork"); tls_close(ctx); shutdown(fd, SHUT_RDWR); return; @@ -52,7 +50,7 @@ serve(struct tls *ctx, int fd) } tls_close(ctx); shutdown(fd, SHUT_RDWR); - _exit(EXIT_SUCCESS); + _exit(0); } else { while (1) { char buf[BUFMAX]; diff --git a/sock.c b/sock.c @@ -1,8 +1,6 @@ -#include <errno.h> #include <stdio.h> -#include <string.h> -#include <unistd.h> #include <sys/time.h> +#include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> @@ -27,14 +25,14 @@ sock_server_ips(const char *host, const char *port) int optval = 1; if ((ret = getaddrinfo(host, port, &hints, &ai)) != 0) - die("getaddrinfo: %s\n", gai_strerror(ret)); + errx("getaddrinfo: %s", gai_strerror(ret)); for (p = ai; p != NULL; p = p->ai_next) { fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (fd == -1) continue; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) - die("setsockopt: %s\n", strerror(errno)); + err("setsockopt"); if (bind(fd, p->ai_addr, p->ai_addrlen) == -1) { close(fd); } else { @@ -43,14 +41,14 @@ sock_server_ips(const char *host, const char *port) } freeaddrinfo(ai); if (p == NULL) - die( - "bind: Can not bind to address: %s:%s\n", + errx( + "bind: Can not bind to address: %s:%s", host, port ); if (listen(fd, SOMAXCONN) == -1) - die("listen: %s\n", strerror(errno)); + err("listen"); return fd; } @@ -68,7 +66,7 @@ sock_client_ips(const char *host, const char *port) int fd = -1; if ((ret = getaddrinfo(host, port, &hints, &ai)) != 0) { - warn("getaddrinfo: %s\n", gai_strerror(ret)); + warnx("getaddrinfo: %s", gai_strerror(ret)); return -1; } @@ -84,8 +82,8 @@ sock_client_ips(const char *host, const char *port) } freeaddrinfo(ai); if (p == NULL) { - warn( - "connect: Can not connect to address: %s:%s\n", + warnx( + "connect: Can not connect to address: %s:%s", host, port ); @@ -102,7 +100,7 @@ sock_client_uds(const char *file) struct sockaddr_un addr; if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - warn("socket: %s\n", strerror(errno)); + warn("socket"); return -1; } @@ -110,7 +108,7 @@ sock_client_uds(const char *file) snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", file); if (connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) { - warn("connect: %s\n", strerror(errno)); + warn("connect"); return -1; } @@ -126,7 +124,7 @@ sock_set_timeout(int fd, int 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)); + warn("failed to set socket timeout"); return -1; } diff --git a/util.c b/util.c @@ -1,27 +1,69 @@ +#include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "util.h" +extern char *argv0; + +void +err(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "%s: ", argv0); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fprintf(stderr, ": %s\n", strerror(errno)); + + exit(1); +} + +void +errx(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "%s: ", argv0); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fputc('\n', stderr); + + exit(1); +} + void warn(const char *fmt, ...) { va_list ap; + fprintf(stderr, "%s: ", argv0); + va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); + + fprintf(stderr, ": %s\n", strerror(errno)); } void -die(const char *fmt, ...) +warnx(const char *fmt, ...) { va_list ap; + fprintf(stderr, "%s: ", argv0); + va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - exit(EXIT_FAILURE); + fputc('\n', stderr); } diff --git a/util.h b/util.h @@ -1,2 +1,4 @@ +void err(const char *fmt, ...); +void errx(const char *fmt, ...); void warn(const char *fmt, ...); -void die(const char *fmt, ...); +void warnx(const char *fmt, ...);