summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-19 19:11:48 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-19 19:11:48 +0200
commit91a7ca50f3f8a2c7bb01113fa3849cb5e153a70f (patch)
tree18c5b7c76f4ec3069e9033a1c222eefd2c945da6 /main.c
parent445647adc4475c0b8264ce8b6c97d748eec69e7b (diff)
Add support for async DNS
Diffstat (limited to 'main.c')
-rw-r--r--main.c86
1 files changed, 52 insertions, 34 deletions
diff --git a/main.c b/main.c
index e748ff6..b2accc1 100644
--- a/main.c
+++ b/main.c
@@ -255,7 +255,7 @@ cfg_read(struct cfg *cfg)
while (true) {
int key;
const char *keyname;
- union cfg_value value;
+ struct cfg_value value;
if (!config_parse_line(cfg, path, &pos, mcfg_key_map,
&key, &keyname, &value))
@@ -276,6 +276,7 @@ cfg_read(struct cfg *cfg)
cfg->igmp_iface = xstrdup(value.str);
if (!cfg->igmp_iface)
perrordie("xstrdup");
+
break;
case MCFG_KEY_INVALID:
@@ -291,40 +292,46 @@ const struct {
} debug_category_str[] = {
{
.name = "config",
- .val = DBG_CFG
+ .val = DBG_CFG,
},{
.name = "refcount",
- .val = DBG_REF
+ .val = DBG_REF,
},{
.name = "malloc",
- .val = DBG_MALLOC
+ .val = DBG_MALLOC,
},{
.name = "announce",
- .val = DBG_ANN
+ .val = DBG_ANN,
},{
- .name = "signals",
- .val = DBG_SIG
+ .name = "signal",
+ .val = DBG_SIG,
},{
.name = "uring",
- .val = DBG_UR
+ .val = DBG_UR,
},{
.name = "server",
- .val = DBG_SRV
+ .val = DBG_SRV,
},{
.name = "proxy",
- .val = DBG_PROXY
+ .val = DBG_PROXY,
},{
.name = "rcon",
- .val = DBG_RCON
+ .val = DBG_RCON,
},{
.name = "idle",
- .val = DBG_IDLE
+ .val = DBG_IDLE,
},{
.name = "igmp",
- .val = DBG_IGMP
+ .val = DBG_IGMP,
+ },{
+ .name = "systemd",
+ .val = DBG_SYSD,
+ },{
+ .name = "dns",
+ .val = DBG_DNS,
},{
.name = NULL,
- .val = 0
+ .val = 0,
}
};
@@ -561,24 +568,19 @@ dump_tree(struct cfg *cfg)
debug(DBG_REF, "\n\n\n\n");
}
+static struct dns_async *hack_dns = NULL;
+
static void
signalfd_read(struct cfg *cfg, struct uring_task *task, int res)
{
struct signalfd_ev *sev = container_of(task, struct signalfd_ev, task);
struct server *server, *stmp;
- static int count = 0;
if (task->dead) {
debug(DBG_SIG, "task dead\n");
return;
}
- count++;
- if (count > 5) {
- error("Max signal count exceeded, force quitting\n");
- exit(EXIT_FAILURE);
- }
-
if (res != sizeof(sev->buf))
die("error in signalfd (%i)", res);
@@ -586,7 +588,7 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res)
verbose("got a signal to quit\n");
sd_notifyf(0, "STOPPING=1\nSTATUS=Received signal, exiting");
exit(EXIT_SUCCESS);
- } else {
+ } else if (sev->buf < 10000) {
verbose("got a signal to dump tree\n");
sd_notifyf(0, "STOPPING=1\nSTATUS=Received signal, exiting");
dump_tree(cfg);
@@ -598,25 +600,45 @@ signalfd_read(struct cfg *cfg, struct uring_task *task, int res)
server_delete(cfg, server);
uring_delete(cfg);
return;
+ } else {
+ debug(DBG_DNS, "DNS lookup complete, dns: %p, dns->cb: %p\n",
+ hack_dns,
+ hack_dns ? hack_dns->callback : NULL);
+
+ if (!hack_dns || !hack_dns->callback) {
+ error("DNS callback not set\n");
+ goto out;
+ }
+
+ if (hack_dns && hack_dns->callback)
+ hack_dns->callback(hack_dns);
}
+out:
uring_read(cfg, &sev->task, &sev->buf, sizeof(sev->buf), signalfd_read);
}
static int hack_efd = -1;
static void
-hack_handler(int signum)
+hack_handler(int signum, siginfo_t *info, void *ucontext)
{
uint64_t val;
- static int count = 0;
switch (signum) {
+ case SIGUSR1:
+ debug(DBG_SIG, "Got a SIGUSR1\n");
+ if (info->si_code != SI_ASYNCNL || info->si_signo != SIGUSR1 || !info->si_ptr) {
+ debug(DBG_SIG, "unexpected values in siginfo\n");
+ return;
+ }
+ debug(DBG_SIG, "SIGUSR1 struct dns_async: %p\n", info->si_ptr);
+ hack_dns = info->si_ptr;
+ val = 10000;
+ break;
case SIGINT:
debug(DBG_SIG, "Got a SIGINT\n");
val = 1000;
- if (count > 3)
- dump_tree(cfghack);
break;
case SIGHUP:
debug(DBG_SIG, "Got a SIGHUP\n");
@@ -632,13 +654,7 @@ hack_handler(int signum)
break;
}
- if (count > 5) {
- error("Max signal count exceeded, force quitting\n");
- exit(EXIT_FAILURE);
- }
-
write(hack_efd, &val, sizeof(val));
- count++;
}
static void
@@ -664,14 +680,16 @@ signalfd_init(struct cfg *cfg)
struct sigaction action;
sigfillset(&action.sa_mask);
- action.sa_handler = hack_handler;
- action.sa_flags = 0;
+ action.sa_sigaction = hack_handler;
+ action.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &action, NULL);
sigaction(SIGHUP, &action, NULL);
sigaction(SIGTERM, &action, NULL);
+ sigaction(SIGUSR1, &action, NULL);
action.sa_handler = SIG_IGN;
+ action.sa_flags = 0;
sigaction(SIGPIPE, &action, NULL);
sfd = eventfd(0, EFD_CLOEXEC);