diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2024-09-11 21:36:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-11 21:36:24 +0200 |
commit | 783a15081e2c34eb9d7865bcc24457cdabdd6bb8 (patch) | |
tree | 25f9a2fbb16bfec32fdfb41373f42f511228802a /src/resolve | |
parent | resolved: simplify dns_scope_get_n_dns_servers(), don't count each time (diff) | |
parent | resolved: move dnssd parsers to resolved-dnssd.c (diff) | |
download | systemd-783a15081e2c34eb9d7865bcc24457cdabdd6bb8.tar.xz systemd-783a15081e2c34eb9d7865bcc24457cdabdd6bb8.zip |
Merge pull request #34373 from poettering/resolved-dnssd-move-out
move dnssd configuration file parsing from generic code into dnssd source files
Diffstat (limited to 'src/resolve')
-rw-r--r-- | src/resolve/resolved-conf.c | 221 | ||||
-rw-r--r-- | src/resolve/resolved-conf.h | 5 | ||||
-rw-r--r-- | src/resolve/resolved-dnssd-gperf.gperf | 1 | ||||
-rw-r--r-- | src/resolve/resolved-dnssd.c | 222 | ||||
-rw-r--r-- | src/resolve/resolved-dnssd.h | 7 |
5 files changed, 229 insertions, 227 deletions
diff --git a/src/resolve/resolved-conf.c b/src/resolve/resolved-conf.c index 65a5611d09..9d70d98d1d 100644 --- a/src/resolve/resolved-conf.c +++ b/src/resolve/resolved-conf.c @@ -209,227 +209,6 @@ int config_parse_search_domains( return 0; } -int config_parse_dnssd_service_name( - const char *unit, - const char *filename, - unsigned line, - const char *section, - unsigned section_line, - const char *lvalue, - int ltype, - const char *rvalue, - void *data, - void *userdata) { - - static const Specifier specifier_table[] = { - { 'a', specifier_architecture, NULL }, - { 'b', specifier_boot_id, NULL }, - { 'B', specifier_os_build_id, NULL }, - { 'H', specifier_hostname, NULL }, /* We will use specifier_dnssd_hostname(). */ - { 'm', specifier_machine_id, NULL }, - { 'o', specifier_os_id, NULL }, - { 'v', specifier_kernel_release, NULL }, - { 'w', specifier_os_version_id, NULL }, - { 'W', specifier_os_variant_id, NULL }, - {} - }; - DnssdService *s = ASSERT_PTR(userdata); - _cleanup_free_ char *name = NULL; - int r; - - assert(filename); - assert(lvalue); - assert(rvalue); - - if (isempty(rvalue)) { - s->name_template = mfree(s->name_template); - return 0; - } - - r = specifier_printf(rvalue, DNS_LABEL_MAX, specifier_table, NULL, NULL, &name); - if (r < 0) { - log_syntax(unit, LOG_WARNING, filename, line, r, - "Invalid service instance name template '%s', ignoring assignment: %m", rvalue); - return 0; - } - - if (!dns_service_name_is_valid(name)) { - log_syntax(unit, LOG_WARNING, filename, line, 0, - "Service instance name template '%s' renders to invalid name '%s'. Ignoring assignment.", - rvalue, name); - return 0; - } - - return free_and_strdup_warn(&s->name_template, rvalue); -} - -int config_parse_dnssd_service_type( - const char *unit, - const char *filename, - unsigned line, - const char *section, - unsigned section_line, - const char *lvalue, - int ltype, - const char *rvalue, - void *data, - void *userdata) { - - DnssdService *s = ASSERT_PTR(userdata); - int r; - - assert(filename); - assert(lvalue); - assert(rvalue); - - if (isempty(rvalue)) { - s->type = mfree(s->type); - return 0; - } - - if (!dnssd_srv_type_is_valid(rvalue)) { - log_syntax(unit, LOG_WARNING, filename, line, 0, "Service type is invalid. Ignoring."); - return 0; - } - - r = free_and_strdup(&s->type, rvalue); - if (r < 0) - return log_oom(); - - return 0; -} - -int config_parse_dnssd_service_subtype( - const char *unit, - const char *filename, - unsigned line, - const char *section, - unsigned section_line, - const char *lvalue, - int ltype, - const char *rvalue, - void *data, - void *userdata) { - - DnssdService *s = ASSERT_PTR(userdata); - - assert(filename); - assert(lvalue); - assert(rvalue); - - if (isempty(rvalue)) { - s->subtype = mfree(s->subtype); - return 0; - } - - if (!dns_subtype_name_is_valid(rvalue)) { - log_syntax(unit, LOG_WARNING, filename, line, 0, "Service subtype is invalid. Ignoring."); - return 0; - } - - return free_and_strdup_warn(&s->subtype, rvalue); -} - -int config_parse_dnssd_txt( - const char *unit, - const char *filename, - unsigned line, - const char *section, - unsigned section_line, - const char *lvalue, - int ltype, - const char *rvalue, - void *data, - void *userdata) { - - _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL; - DnssdService *s = ASSERT_PTR(userdata); - DnsTxtItem *last = NULL; - - assert(filename); - assert(lvalue); - assert(rvalue); - - if (isempty(rvalue)) { - /* Flush out collected items */ - s->txt_data_items = dnssd_txtdata_free_all(s->txt_data_items); - return 0; - } - - txt_data = new0(DnssdTxtData, 1); - if (!txt_data) - return log_oom(); - - for (;;) { - _cleanup_free_ char *word = NULL, *key = NULL, *value = NULL; - _cleanup_free_ void *decoded = NULL; - size_t length = 0; - DnsTxtItem *i; - int r; - - r = extract_first_word(&rvalue, &word, NULL, - EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX); - if (r == 0) - break; - if (r == -ENOMEM) - return log_oom(); - if (r < 0) { - log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); - return 0; - } - - r = split_pair(word, "=", &key, &value); - if (r == -ENOMEM) - return log_oom(); - if (r == -EINVAL) - key = TAKE_PTR(word); - - if (!ascii_is_valid(key)) { - log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid key, ignoring: %s", key); - continue; - } - - switch (ltype) { - - case DNS_TXT_ITEM_DATA: - if (value) { - r = unbase64mem(value, &decoded, &length); - if (r == -ENOMEM) - return log_oom(); - if (r < 0) { - log_syntax(unit, LOG_WARNING, filename, line, r, - "Invalid base64 encoding, ignoring: %s", value); - continue; - } - } - - r = dnssd_txt_item_new_from_data(key, decoded, length, &i); - if (r < 0) - return log_oom(); - break; - - case DNS_TXT_ITEM_TEXT: - r = dnssd_txt_item_new_from_string(key, value, &i); - if (r < 0) - return log_oom(); - break; - - default: - assert_not_reached(); - } - - LIST_INSERT_AFTER(items, txt_data->txts, last, i); - last = i; - } - - if (txt_data->txts) { - LIST_PREPEND(items, s->txt_data_items, txt_data); - TAKE_PTR(txt_data); - } - - return 0; -} - int config_parse_dns_stub_listener_extra( const char *unit, const char *filename, diff --git a/src/resolve/resolved-conf.h b/src/resolve/resolved-conf.h index 5eea6bd54b..ce280cb0a3 100644 --- a/src/resolve/resolved-conf.h +++ b/src/resolve/resolved-conf.h @@ -19,13 +19,8 @@ int manager_parse_search_domains_and_warn(Manager *m, const char *string); int manager_parse_dns_server_string_and_warn(Manager *m, DnsServerType type, const char *string); const struct ConfigPerfItem* resolved_gperf_lookup(const char *key, GPERF_LEN_TYPE length); -const struct ConfigPerfItem* resolved_dnssd_gperf_lookup(const char *key, GPERF_LEN_TYPE length); CONFIG_PARSER_PROTOTYPE(config_parse_dns_servers); CONFIG_PARSER_PROTOTYPE(config_parse_search_domains); CONFIG_PARSER_PROTOTYPE(config_parse_dns_stub_listener_mode); -CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_service_name); -CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_service_subtype); -CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_service_type); -CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_txt); CONFIG_PARSER_PROTOTYPE(config_parse_dns_stub_listener_extra); diff --git a/src/resolve/resolved-dnssd-gperf.gperf b/src/resolve/resolved-dnssd-gperf.gperf index e78573bec0..a5158390c9 100644 --- a/src/resolve/resolved-dnssd-gperf.gperf +++ b/src/resolve/resolved-dnssd-gperf.gperf @@ -2,7 +2,6 @@ %{ #include <stddef.h> #include "conf-parser.h" -#include "resolved-conf.h" #include "resolved-dnssd.h" %} struct ConfigPerfItem; diff --git a/src/resolve/resolved-dnssd.c b/src/resolve/resolved-dnssd.c index 5f66e3c501..00fd725044 100644 --- a/src/resolve/resolved-dnssd.c +++ b/src/resolve/resolved-dnssd.c @@ -3,6 +3,7 @@ #include "conf-files.h" #include "conf-parser.h" #include "constants.h" +#include "hexdecoct.h" #include "path-util.h" #include "resolved-conf.h" #include "resolved-dns-rr.h" @@ -402,3 +403,224 @@ int dnssd_signal_conflict(Manager *manager, const char *name) { return 0; } + +int config_parse_dnssd_service_name( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + static const Specifier specifier_table[] = { + { 'a', specifier_architecture, NULL }, + { 'b', specifier_boot_id, NULL }, + { 'B', specifier_os_build_id, NULL }, + { 'H', specifier_hostname, NULL }, /* We will use specifier_dnssd_hostname(). */ + { 'm', specifier_machine_id, NULL }, + { 'o', specifier_os_id, NULL }, + { 'v', specifier_kernel_release, NULL }, + { 'w', specifier_os_version_id, NULL }, + { 'W', specifier_os_variant_id, NULL }, + {} + }; + DnssdService *s = ASSERT_PTR(userdata); + _cleanup_free_ char *name = NULL; + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + + if (isempty(rvalue)) { + s->name_template = mfree(s->name_template); + return 0; + } + + r = specifier_printf(rvalue, DNS_LABEL_MAX, specifier_table, NULL, NULL, &name); + if (r < 0) { + log_syntax(unit, LOG_WARNING, filename, line, r, + "Invalid service instance name template '%s', ignoring assignment: %m", rvalue); + return 0; + } + + if (!dns_service_name_is_valid(name)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, + "Service instance name template '%s' renders to invalid name '%s'. Ignoring assignment.", + rvalue, name); + return 0; + } + + return free_and_strdup_warn(&s->name_template, rvalue); +} + +int config_parse_dnssd_service_type( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + DnssdService *s = ASSERT_PTR(userdata); + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + + if (isempty(rvalue)) { + s->type = mfree(s->type); + return 0; + } + + if (!dnssd_srv_type_is_valid(rvalue)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, "Service type is invalid. Ignoring."); + return 0; + } + + r = free_and_strdup(&s->type, rvalue); + if (r < 0) + return log_oom(); + + return 0; +} + +int config_parse_dnssd_service_subtype( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + DnssdService *s = ASSERT_PTR(userdata); + + assert(filename); + assert(lvalue); + assert(rvalue); + + if (isempty(rvalue)) { + s->subtype = mfree(s->subtype); + return 0; + } + + if (!dns_subtype_name_is_valid(rvalue)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, "Service subtype is invalid. Ignoring."); + return 0; + } + + return free_and_strdup_warn(&s->subtype, rvalue); +} + +int config_parse_dnssd_txt( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL; + DnssdService *s = ASSERT_PTR(userdata); + DnsTxtItem *last = NULL; + + assert(filename); + assert(lvalue); + assert(rvalue); + + if (isempty(rvalue)) { + /* Flush out collected items */ + s->txt_data_items = dnssd_txtdata_free_all(s->txt_data_items); + return 0; + } + + txt_data = new0(DnssdTxtData, 1); + if (!txt_data) + return log_oom(); + + for (;;) { + _cleanup_free_ char *word = NULL, *key = NULL, *value = NULL; + _cleanup_free_ void *decoded = NULL; + size_t length = 0; + DnsTxtItem *i; + int r; + + r = extract_first_word(&rvalue, &word, NULL, + EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_RELAX); + if (r == 0) + break; + if (r == -ENOMEM) + return log_oom(); + if (r < 0) { + log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue); + return 0; + } + + r = split_pair(word, "=", &key, &value); + if (r == -ENOMEM) + return log_oom(); + if (r == -EINVAL) + key = TAKE_PTR(word); + + if (!ascii_is_valid(key)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid key, ignoring: %s", key); + continue; + } + + switch (ltype) { + + case DNS_TXT_ITEM_DATA: + if (value) { + r = unbase64mem(value, &decoded, &length); + if (r == -ENOMEM) + return log_oom(); + if (r < 0) { + log_syntax(unit, LOG_WARNING, filename, line, r, + "Invalid base64 encoding, ignoring: %s", value); + continue; + } + } + + r = dnssd_txt_item_new_from_data(key, decoded, length, &i); + if (r < 0) + return log_oom(); + break; + + case DNS_TXT_ITEM_TEXT: + r = dnssd_txt_item_new_from_string(key, value, &i); + if (r < 0) + return log_oom(); + break; + + default: + assert_not_reached(); + } + + LIST_INSERT_AFTER(items, txt_data->txts, last, i); + last = i; + } + + if (txt_data->txts) { + LIST_PREPEND(items, s->txt_data_items, txt_data); + TAKE_PTR(txt_data); + } + + return 0; +} diff --git a/src/resolve/resolved-dnssd.h b/src/resolve/resolved-dnssd.h index 84f7853b81..8aaa9da748 100644 --- a/src/resolve/resolved-dnssd.h +++ b/src/resolve/resolved-dnssd.h @@ -66,3 +66,10 @@ int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtIte int dnssd_txt_item_new_from_data(const char *key, const void *value, const size_t size, DnsTxtItem **ret_item); int dnssd_update_rrs(DnssdService *s); int dnssd_signal_conflict(Manager *manager, const char *name); + +const struct ConfigPerfItem* resolved_dnssd_gperf_lookup(const char *key, GPERF_LEN_TYPE length); + +CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_service_name); +CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_service_subtype); +CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_service_type); +CONFIG_PARSER_PROTOTYPE(config_parse_dnssd_txt); |