2018-02-27 19:20:58 +00:00
|
|
|
/*-
|
2015-02-24 18:19:20 +00:00
|
|
|
* SSLsplit - transparent SSL/TLS interception
|
2018-02-27 19:20:58 +00:00
|
|
|
* https://www.roe.ch/SSLsplit
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2018, Daniel Roethlisberger <daniel@roe.ch>.
|
2012-04-13 12:47:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2018-02-27 19:20:58 +00:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
2012-04-13 12:47:30 +00:00
|
|
|
*
|
2018-02-27 19:20:58 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "proxy.h"
|
|
|
|
|
2014-11-24 21:01:52 +00:00
|
|
|
#include "privsep.h"
|
2012-04-13 12:47:30 +00:00
|
|
|
#include "pxythrmgr.h"
|
|
|
|
#include "pxyconn.h"
|
|
|
|
#include "cachemgr.h"
|
|
|
|
#include "opts.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "attrib.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/listener.h>
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
#include <event2/bufferevent_ssl.h>
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/thread.h>
|
|
|
|
|
2017-07-25 13:07:39 +00:00
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Proxy engine, built around libevent 2.x.
|
|
|
|
*/
|
|
|
|
|
2017-07-25 13:07:39 +00:00
|
|
|
static int signals[] = { SIGTERM, SIGQUIT, SIGHUP, SIGINT, SIGPIPE, SIGUSR1 };
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
struct proxy_ctx {
|
|
|
|
pxy_thrmgr_ctx_t *thrmgr;
|
|
|
|
struct event_base *evbase;
|
|
|
|
struct event *sev[sizeof(signals)/sizeof(int)];
|
|
|
|
struct event *gcev;
|
|
|
|
struct proxy_listener_ctx *lctx;
|
|
|
|
opts_t *opts;
|
|
|
|
};
|
|
|
|
|
|
|
|
static proxy_listener_ctx_t *
|
|
|
|
proxy_listener_ctx_new(pxy_thrmgr_ctx_t *thrmgr, proxyspec_t *spec,
|
|
|
|
opts_t *opts) MALLOC;
|
|
|
|
static proxy_listener_ctx_t *
|
|
|
|
proxy_listener_ctx_new(pxy_thrmgr_ctx_t *thrmgr, proxyspec_t *spec,
|
|
|
|
opts_t *opts)
|
|
|
|
{
|
|
|
|
proxy_listener_ctx_t *ctx = malloc(sizeof(proxy_listener_ctx_t));
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
|
|
|
memset(ctx, 0, sizeof(proxy_listener_ctx_t));
|
|
|
|
ctx->thrmgr = thrmgr;
|
|
|
|
ctx->spec = spec;
|
|
|
|
ctx->opts = opts;
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:17:45 +00:00
|
|
|
static void
|
|
|
|
proxy_listener_ctx_free(proxy_listener_ctx_t *ctx) NONNULL(1);
|
2012-04-13 12:47:30 +00:00
|
|
|
static void
|
|
|
|
proxy_listener_ctx_free(proxy_listener_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->evcl) {
|
|
|
|
evconnlistener_free(ctx->evcl);
|
|
|
|
}
|
|
|
|
if (ctx->next) {
|
|
|
|
proxy_listener_ctx_free(ctx->next);
|
|
|
|
}
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
2017-07-07 14:18:01 +00:00
|
|
|
/*
|
|
|
|
* Callback for accept events on the socket listener bufferevent.
|
|
|
|
*/
|
2012-04-13 12:47:30 +00:00
|
|
|
static void
|
2017-05-29 09:22:23 +00:00
|
|
|
proxy_listener_acceptcb(UNUSED struct evconnlistener *listener,
|
|
|
|
evutil_socket_t fd,
|
|
|
|
struct sockaddr *peeraddr, int peeraddrlen,
|
|
|
|
void *arg)
|
2012-04-13 12:47:30 +00:00
|
|
|
{
|
2017-05-29 09:22:23 +00:00
|
|
|
proxy_listener_ctx_t *lctx = arg;
|
|
|
|
|
2017-08-13 01:36:33 +00:00
|
|
|
#ifdef DEBUG_PROXY
|
2018-10-17 13:28:21 +00:00
|
|
|
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "proxy_listener_acceptcb: ENTER, fd=%d\n", fd);
|
2017-08-13 01:36:33 +00:00
|
|
|
#endif /* DEBUG_PROXY */
|
|
|
|
pxy_conn_setup(fd, peeraddr, peeraddrlen, lctx->thrmgr, lctx->spec, lctx->opts, lctx->clisock);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 13:07:39 +00:00
|
|
|
/*
|
|
|
|
* Callback for error events on the socket listener bufferevent.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
proxy_listener_errorcb(struct evconnlistener *listener, UNUSED void *arg)
|
|
|
|
{
|
|
|
|
struct event_base *evbase = evconnlistener_get_base(listener);
|
|
|
|
int err = EVUTIL_SOCKET_ERROR();
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error %d on listener: %s\n", err,
|
2017-07-25 13:07:39 +00:00
|
|
|
evutil_socket_error_to_string(err));
|
2018-11-29 23:49:37 +00:00
|
|
|
/* Do not break the event loop if out of fds:
|
|
|
|
* Too many open files (24) */
|
|
|
|
if (err == 24) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-25 13:07:39 +00:00
|
|
|
event_base_loopbreak(evbase);
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Dump a description of an evbase to debugging code.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
proxy_debug_base(const struct event_base *ev_base)
|
|
|
|
{
|
|
|
|
log_dbg_printf("Using libevent backend '%s'\n",
|
|
|
|
event_base_get_method(ev_base));
|
|
|
|
|
|
|
|
enum event_method_feature f;
|
|
|
|
f = event_base_get_features(ev_base);
|
|
|
|
log_dbg_printf("Event base supports: edge %s, O(1) %s, anyfd %s\n",
|
|
|
|
((f & EV_FEATURE_ET) ? "yes" : "no"),
|
|
|
|
((f & EV_FEATURE_O1) ? "yes" : "no"),
|
|
|
|
((f & EV_FEATURE_FDS) ? "yes" : "no"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the listener for a single proxyspec and add it to evbase.
|
|
|
|
* Returns the proxy_listener_ctx_t pointer if successful, NULL otherwise.
|
|
|
|
*/
|
|
|
|
static proxy_listener_ctx_t *
|
|
|
|
proxy_listener_setup(struct event_base *evbase, pxy_thrmgr_ctx_t *thrmgr,
|
2017-07-11 22:45:15 +00:00
|
|
|
proxyspec_t *spec, opts_t *opts, evutil_socket_t clisock)
|
2012-04-13 12:47:30 +00:00
|
|
|
{
|
2017-08-13 01:36:33 +00:00
|
|
|
#ifdef DEBUG_PROXY
|
|
|
|
log_dbg_level_printf(LOG_DBG_MODE_FINEST, "proxy_listener_setup: ENTER\n");
|
|
|
|
#endif /* DEBUG_PROXY */
|
2017-05-29 09:22:23 +00:00
|
|
|
|
|
|
|
proxy_listener_ctx_t *lctx;
|
2014-11-24 21:01:52 +00:00
|
|
|
int fd;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
2014-11-24 21:01:52 +00:00
|
|
|
if ((fd = privsep_client_opensock(clisock, spec)) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error opening socket: %s (%i)\n",
|
2014-11-24 21:01:52 +00:00
|
|
|
strerror(errno), errno);
|
2012-04-13 12:47:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-29 09:22:23 +00:00
|
|
|
lctx = proxy_listener_ctx_new(thrmgr, spec, opts);
|
|
|
|
if (!lctx) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error creating listener context\n");
|
2012-04-13 12:47:30 +00:00
|
|
|
evutil_closesocket(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-29 09:22:23 +00:00
|
|
|
lctx->clisock = clisock;
|
|
|
|
|
2017-07-01 15:08:28 +00:00
|
|
|
// @todo Should we enable threadsafe event structs?
|
2017-07-15 10:04:13 +00:00
|
|
|
// @attention Do not pass NULL as user-supplied pointer
|
2017-05-29 09:22:23 +00:00
|
|
|
lctx->evcl = evconnlistener_new(evbase, proxy_listener_acceptcb,
|
|
|
|
lctx, LEV_OPT_CLOSE_ON_FREE, 1024, fd);
|
2017-07-01 15:08:28 +00:00
|
|
|
// lctx, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_THREADSAFE, 1024, fd);
|
2017-05-29 09:22:23 +00:00
|
|
|
if (!lctx->evcl) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error creating evconnlistener: %s\n",
|
2012-04-13 12:47:30 +00:00
|
|
|
strerror(errno));
|
2017-05-29 09:22:23 +00:00
|
|
|
proxy_listener_ctx_free(lctx);
|
2012-04-13 12:47:30 +00:00
|
|
|
evutil_closesocket(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-29 09:22:23 +00:00
|
|
|
evconnlistener_set_error_cb(lctx->evcl, proxy_listener_errorcb);
|
|
|
|
return lctx;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-25 13:07:39 +00:00
|
|
|
* Signal handler for SIGTERM, SIGQUIT, SIGINT, SIGHUP, SIGPIPE and SIGUSR1.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
proxy_signal_cb(evutil_socket_t fd, UNUSED short what, void *arg)
|
|
|
|
{
|
|
|
|
proxy_ctx_t *ctx = arg;
|
|
|
|
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(ctx->opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("Received signal %i\n", fd);
|
|
|
|
}
|
|
|
|
|
2014-11-25 22:45:40 +00:00
|
|
|
switch(fd) {
|
2017-07-25 13:07:39 +00:00
|
|
|
case SIGTERM:
|
2014-11-25 22:45:40 +00:00
|
|
|
case SIGQUIT:
|
|
|
|
case SIGINT:
|
2016-03-25 14:56:42 +00:00
|
|
|
proxy_loopbreak(ctx);
|
2014-11-25 22:45:40 +00:00
|
|
|
break;
|
2018-11-03 15:23:31 +00:00
|
|
|
case SIGHUP:
|
2014-11-25 22:45:40 +00:00
|
|
|
case SIGUSR1:
|
|
|
|
if (log_reopen() == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_WARNING, "Failed to reopen logs\n");
|
2014-11-25 22:45:40 +00:00
|
|
|
} else {
|
|
|
|
log_dbg_printf("Reopened log files\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SIGPIPE:
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_WARNING, "Received SIGPIPE; ignoring.\n");
|
2014-11-25 22:45:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_WARNING, "Received unexpected signal %i\n", fd);
|
2014-11-25 22:45:40 +00:00
|
|
|
break;
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-04-03 16:06:01 +00:00
|
|
|
* Garbage collection handler.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
proxy_gc_cb(UNUSED evutil_socket_t fd, UNUSED short what, void *arg)
|
|
|
|
{
|
|
|
|
proxy_ctx_t *ctx = arg;
|
|
|
|
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(ctx->opts))
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("Garbage collecting caches started.\n");
|
|
|
|
|
|
|
|
cachemgr_gc();
|
|
|
|
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(ctx->opts))
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("Garbage collecting caches done.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the core event loop.
|
2014-11-24 21:01:52 +00:00
|
|
|
* Socket clisock is the privsep client socket used for binding to ports.
|
2012-04-13 12:47:30 +00:00
|
|
|
* Returns ctx on success, or NULL on error.
|
|
|
|
*/
|
|
|
|
proxy_ctx_t *
|
2014-11-24 21:01:52 +00:00
|
|
|
proxy_new(opts_t *opts, int clisock)
|
2012-04-13 12:47:30 +00:00
|
|
|
{
|
|
|
|
proxy_listener_ctx_t *head;
|
|
|
|
proxy_ctx_t *ctx;
|
2014-01-15 09:33:43 +00:00
|
|
|
struct evdns_base *dnsbase;
|
|
|
|
int rc;
|
2012-04-13 12:47:30 +00:00
|
|
|
|
|
|
|
/* adds locking, only required if accessed from separate threads */
|
|
|
|
evthread_use_pthreads();
|
|
|
|
|
|
|
|
#ifndef PURIFY
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
event_enable_debug_mode();
|
|
|
|
}
|
|
|
|
#endif /* PURIFY */
|
|
|
|
|
|
|
|
ctx = malloc(sizeof(proxy_ctx_t));
|
|
|
|
if (!ctx) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error allocating memory\n");
|
2012-04-13 12:47:30 +00:00
|
|
|
goto leave0;
|
|
|
|
}
|
|
|
|
memset(ctx, 0, sizeof(proxy_ctx_t));
|
|
|
|
|
|
|
|
ctx->opts = opts;
|
|
|
|
ctx->evbase = event_base_new();
|
|
|
|
if (!ctx->evbase) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error getting event base\n");
|
2012-04-13 12:47:30 +00:00
|
|
|
goto leave1;
|
|
|
|
}
|
|
|
|
|
2015-09-27 14:39:24 +00:00
|
|
|
if (opts_has_dns_spec(opts)) {
|
|
|
|
/* create a dnsbase here purely for being able to test parsing
|
|
|
|
* resolv.conf while we can still alert the user about it. */
|
|
|
|
dnsbase = evdns_base_new(ctx->evbase, 0);
|
|
|
|
if (!dnsbase) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error creating dns event base\n");
|
2015-09-27 14:39:24 +00:00
|
|
|
goto leave1b;
|
|
|
|
}
|
|
|
|
rc = evdns_base_resolv_conf_parse(dnsbase, DNS_OPTIONS_ALL,
|
|
|
|
"/etc/resolv.conf");
|
|
|
|
evdns_base_free(dnsbase, 0);
|
|
|
|
if (rc != 0) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "evdns cannot parse resolv.conf: "
|
2015-09-27 14:39:24 +00:00
|
|
|
"%s (%d)\n",
|
|
|
|
rc == 1 ? "failed to open file" :
|
|
|
|
rc == 2 ? "failed to stat file" :
|
|
|
|
rc == 3 ? "file too large" :
|
|
|
|
rc == 4 ? "out of memory" :
|
|
|
|
rc == 5 ? "short read from file" :
|
|
|
|
rc == 6 ? "no nameservers in file" :
|
|
|
|
"unknown error", rc);
|
|
|
|
goto leave1b;
|
|
|
|
}
|
2014-01-15 09:33:43 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
proxy_debug_base(ctx->evbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->thrmgr = pxy_thrmgr_new(opts);
|
|
|
|
if (!ctx->thrmgr) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Error creating thread manager\n");
|
2012-04-13 12:47:30 +00:00
|
|
|
goto leave1b;
|
|
|
|
}
|
|
|
|
|
|
|
|
head = ctx->lctx = NULL;
|
|
|
|
for (proxyspec_t *spec = opts->spec; spec; spec = spec->next) {
|
|
|
|
head = proxy_listener_setup(ctx->evbase, ctx->thrmgr,
|
2014-11-24 21:01:52 +00:00
|
|
|
spec, opts, clisock);
|
2012-04-13 12:47:30 +00:00
|
|
|
if (!head)
|
|
|
|
goto leave2;
|
|
|
|
head->next = ctx->lctx;
|
|
|
|
ctx->lctx = head;
|
2017-05-29 09:22:23 +00:00
|
|
|
|
|
|
|
char *specstr = proxyspec_str(spec);
|
|
|
|
if (!specstr) {
|
|
|
|
fprintf(stderr, "out of memory\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-08-13 01:36:33 +00:00
|
|
|
log_dbg_printf("proxy_listener_setup: %s\n", specstr);
|
2017-05-29 09:22:23 +00:00
|
|
|
free(specstr);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < (sizeof(signals) / sizeof(int)); i++) {
|
|
|
|
ctx->sev[i] = evsignal_new(ctx->evbase, signals[i],
|
|
|
|
proxy_signal_cb, ctx);
|
|
|
|
if (!ctx->sev[i])
|
|
|
|
goto leave3;
|
|
|
|
evsignal_add(ctx->sev[i], NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timeval gc_delay = {60, 0};
|
|
|
|
ctx->gcev = event_new(ctx->evbase, -1, EV_PERSIST, proxy_gc_cb, ctx);
|
|
|
|
if (!ctx->gcev)
|
|
|
|
goto leave4;
|
|
|
|
evtimer_add(ctx->gcev, &gc_delay);
|
|
|
|
|
2019-03-15 12:39:15 +00:00
|
|
|
// @attention Do not close privsep sock, we open new sockets for child conns
|
2017-05-29 09:22:23 +00:00
|
|
|
//privsep_client_close(clisock);
|
2012-04-13 12:47:30 +00:00
|
|
|
return ctx;
|
|
|
|
|
|
|
|
leave4:
|
|
|
|
if (ctx->gcev) {
|
|
|
|
event_free(ctx->gcev);
|
|
|
|
}
|
|
|
|
|
|
|
|
leave3:
|
|
|
|
for (size_t i = 0; i < (sizeof(ctx->sev) / sizeof(ctx->sev[0])); i++) {
|
|
|
|
if (ctx->sev[i]) {
|
|
|
|
event_free(ctx->sev[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
leave2:
|
|
|
|
if (ctx->lctx) {
|
|
|
|
proxy_listener_ctx_free(ctx->lctx);
|
|
|
|
}
|
|
|
|
pxy_thrmgr_free(ctx->thrmgr);
|
|
|
|
leave1b:
|
|
|
|
event_base_free(ctx->evbase);
|
|
|
|
leave1:
|
|
|
|
free(ctx);
|
|
|
|
leave0:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-03 15:23:31 +00:00
|
|
|
* Run the event loop. Returns when the event loop is canceled by a signal
|
2013-07-02 13:54:46 +00:00
|
|
|
* or on failure.
|
2012-04-13 12:47:30 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
proxy_run(proxy_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->opts->detach) {
|
|
|
|
event_reinit(ctx->evbase);
|
|
|
|
}
|
|
|
|
#ifndef PURIFY
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(ctx->opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
event_base_dump_events(ctx->evbase, stderr);
|
|
|
|
}
|
|
|
|
#endif /* PURIFY */
|
2013-07-02 13:54:46 +00:00
|
|
|
if (pxy_thrmgr_run(ctx->thrmgr) == -1) {
|
2017-10-14 22:39:30 +00:00
|
|
|
log_err_level_printf(LOG_CRIT, "Failed to start thread manager\n");
|
2013-07-02 13:54:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(ctx->opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("Starting main event loop.\n");
|
|
|
|
}
|
|
|
|
event_base_dispatch(ctx->evbase);
|
2012-05-13 13:24:50 +00:00
|
|
|
if (OPTS_DEBUG(ctx->opts)) {
|
2012-04-13 12:47:30 +00:00
|
|
|
log_dbg_printf("Main event loop stopped.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 14:56:42 +00:00
|
|
|
/*
|
|
|
|
* Break the loop of the proxy, causing the proxy_run to return.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
proxy_loopbreak(proxy_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
event_base_loopbreak(ctx->evbase);
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:47:30 +00:00
|
|
|
/*
|
|
|
|
* Free the proxy data structures.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
proxy_free(proxy_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->gcev) {
|
|
|
|
event_free(ctx->gcev);
|
|
|
|
}
|
|
|
|
if (ctx->lctx) {
|
|
|
|
proxy_listener_ctx_free(ctx->lctx);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < (sizeof(ctx->sev) / sizeof(ctx->sev[0])); i++) {
|
2013-08-23 14:56:12 +00:00
|
|
|
if (ctx->sev[i]) {
|
|
|
|
event_free(ctx->sev[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ctx->thrmgr) {
|
|
|
|
pxy_thrmgr_free(ctx->thrmgr);
|
|
|
|
}
|
|
|
|
if (ctx->evbase) {
|
|
|
|
event_base_free(ctx->evbase);
|
2012-04-13 12:47:30 +00:00
|
|
|
}
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set noet ft=c: */
|