From 472f2f06be419bdf6bfbe794db9db0a53955fd85 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 21 Aug 2020 08:03:58 -0400 Subject: Allow wildcards in rules The hostname "*" is treated as a wildcard. Port 0 is treated as a wildcard. Closes https://github.com/gjedeer/tuntox/issues/56 --- main.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 2a230e0..749f38b 100644 --- a/main.c +++ b/main.c @@ -108,14 +108,13 @@ int allowed_toxid_cmp(allowed_toxid *a, allowed_toxid *b) return memcmp(a->toxid, b->toxid, TOX_PUBLIC_KEY_SIZE); } -/* Comparison function for rule objects */ -int rule_match(rule *a, rule *b) +/* Match rule r against candidate host, port. Returns 0 for match. */ +int rule_match(rule *r, rule *candidate) { - //log_printf(L_INFO, "Comparison result: %d %d\n", strcmp(a->host, b->host), (a->port == b->port)); - if ((strcmp(a->host, b->host)==0) && (a->port == b->port)) - return 0; - else - return -1; + bool host_match = !strcmp(r->host, "*") || !strcmp(r->host, candidate->host); + bool port_match = r->port == 0 || r->port == candidate->port; + + return port_match && host_match ? 0 : -1; } void update_select_nfds(int fd) -- cgit v1.2.3