From 558eabc13c64bdbd2d380e9520d56107d60a13cd Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 4 Mar 2022 14:15:44 +0100 Subject: [PATCH] map: move dns host based lookup code to a separate function Signed-off-by: Felix Fietkau --- map.c | 54 +++++++++++++++++++++++++++++++++--------------------- qosify.h | 1 + 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/map.c b/map.c index fe11aa5..c9c75aa 100644 --- a/map.c +++ b/map.c @@ -710,31 +710,18 @@ void qosify_map_gc(void) uloop_timeout_set(&qosify_map_timer, timeout * 1000); } - -int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int ttl) +int qosify_map_lookup_dns_entry(char *host, uint8_t *dscp) { struct qosify_map_data data = { .id = CL_MAP_DNS, .addr.dns.pattern = "", }; struct qosify_map_entry *e; - int prev_timeout = qosify_map_timeout; + bool ret = -1; char *c; e = avl_find_ge_element(&map_data, &data, e, avl); if (!e) - return 0; - - memset(&data, 0, sizeof(data)); - data.user = true; - if (!strcmp(type, "A")) - data.id = CL_MAP_IPV4_ADDR; - else if (!strcmp(type, "AAAA")) - data.id = CL_MAP_IPV6_ADDR; - else - return 0; - - if (qosify_map_fill_ip(&data, addr)) return -1; for (c = host; *c; c++) @@ -744,7 +731,7 @@ int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int regex_t *regex = &e->data.addr.dns.regex; if (e->data.id != CL_MAP_DNS) - return 0; + break; if (e->data.addr.dns.pattern[0] == '/') { if (regexec(regex, host, 0, NULL, 0) != 0) @@ -754,13 +741,38 @@ int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int continue; } - if (ttl) - qosify_map_timeout = ttl; - data.dscp = e->data.dscp; - __qosify_map_set_entry(&data); - qosify_map_timeout = prev_timeout; + *dscp = e->data.dscp; + ret = 0; } + return ret; +} + + +int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int ttl) +{ + struct qosify_map_data data = {}; + int prev_timeout = qosify_map_timeout; + + if (qosify_map_lookup_dns_entry(host, &data.dscp)) + return 0; + + data.user = true; + if (!strcmp(type, "A")) + data.id = CL_MAP_IPV4_ADDR; + else if (!strcmp(type, "AAAA")) + data.id = CL_MAP_IPV6_ADDR; + else + return 0; + + if (qosify_map_fill_ip(&data, addr)) + return -1; + + if (ttl) + qosify_map_timeout = ttl; + __qosify_map_set_entry(&data); + qosify_map_timeout = prev_timeout; + return 0; } diff --git a/qosify.h b/qosify.h index 791cd16..52edd9b 100644 --- a/qosify.h +++ b/qosify.h @@ -86,6 +86,7 @@ void qosify_map_set_dscp_default(enum qosify_map_id id, uint8_t val); void qosify_map_reset_config(void); void qosify_map_update_config(void); void qosify_map_set_classes(struct blob_attr *val); +int qosify_map_lookup_dns_entry(char *host, uint8_t *dscp); int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int ttl); int map_parse_flow_config(struct qosify_flow_config *cfg, struct blob_attr *attr, bool reset); -- 2.30.2