]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dns-domain.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 # include <stringprep.h>
11 #include <netinet/in.h>
13 #include <sys/socket.h>
15 #include "alloc-util.h"
16 #include "dns-domain.h"
18 #include "hexdecoct.h"
19 #include "hostname-util.h"
20 #include "in-addr-util.h"
22 #include "parse-util.h"
23 #include "string-util.h"
27 int dns_label_unescape(const char **name
, char *dest
, size_t sz
, DNSLabelFlags flags
) {
29 char *d
, last_char
= 0;
39 if (IN_SET(*n
, 0, '.')) {
40 if (FLAGS_SET(flags
, DNS_LABEL_LDH
) && last_char
== '-')
49 if (r
>= DNS_LABEL_MAX
)
56 /* Escaped character */
57 if (FLAGS_SET(flags
, DNS_LABEL_NO_ESCAPES
))
66 else if (IN_SET(*n
, '\\', '.')) {
67 /* Escaped backslash or dot */
69 if (FLAGS_SET(flags
, DNS_LABEL_LDH
))
79 } else if (n
[0] >= '0' && n
[0] <= '9') {
82 /* Escaped literal ASCII character */
84 if (!(n
[1] >= '0' && n
[1] <= '9') ||
85 !(n
[2] >= '0' && n
[2] <= '9'))
88 k
= ((unsigned) (n
[0] - '0') * 100) +
89 ((unsigned) (n
[1] - '0') * 10) +
90 ((unsigned) (n
[2] - '0'));
92 /* Don't allow anything that doesn't
93 * fit in 8bit. Note that we do allow
94 * control characters, as some servers
95 * (e.g. cloudflare) are happy to
96 * generate labels with them
101 if (FLAGS_SET(flags
, DNS_LABEL_LDH
) &&
102 !valid_ldh_char((char) k
))
105 last_char
= (char) k
;
115 } else if ((uint8_t) *n
>= (uint8_t) ' ' && *n
!= 127) {
117 /* Normal character */
119 if (FLAGS_SET(flags
, DNS_LABEL_LDH
)) {
120 if (!valid_ldh_char(*n
))
122 if (r
== 0 && *n
== '-')
137 /* Empty label that is not at the end? */
141 /* More than one trailing dot? */
152 /* @label_terminal: terminal character of a label, updated to point to the terminal character of
153 * the previous label (always skipping one dot) or to NULL if there are no more
155 int dns_label_unescape_suffix(const char *name
, const char **label_terminal
, char *dest
, size_t sz
) {
156 const char *terminal
;
160 assert(label_terminal
);
164 if (!*label_terminal
) {
171 terminal
= *label_terminal
;
172 assert(IN_SET(*terminal
, 0, '.'));
174 /* Skip current terminal character (and accept domain names ending it ".") */
177 if (terminal
>= name
&& *terminal
== '.')
180 /* Point name to the last label, and terminal to the preceding terminal symbol (or make it a NULL pointer) */
182 if (terminal
< name
) {
183 /* Reached the first label, so indicate that there are no more */
188 /* Find the start of the last label */
189 if (*terminal
== '.') {
191 unsigned slashes
= 0;
193 for (y
= terminal
- 1; y
>= name
&& *y
== '\\'; y
--)
196 if (slashes
% 2 == 0) {
197 /* The '.' was not escaped */
209 r
= dns_label_unescape(&name
, dest
, sz
, 0);
213 *label_terminal
= terminal
;
218 int dns_label_escape(const char *p
, size_t l
, char *dest
, size_t sz
) {
221 /* DNS labels must be between 1 and 63 characters long. A
222 * zero-length label does not exist. See RFC 2182, Section
225 if (l
<= 0 || l
> DNS_LABEL_MAX
)
236 if (IN_SET(*p
, '.', '\\')) {
238 /* Dot or backslash */
248 } else if (IN_SET(*p
, '_', '-') ||
249 (*p
>= '0' && *p
<= '9') ||
250 (*p
>= 'a' && *p
<= 'z') ||
251 (*p
>= 'A' && *p
<= 'Z')) {
253 /* Proper character */
263 /* Everything else */
269 *(q
++) = '0' + (char) ((uint8_t) *p
/ 100);
270 *(q
++) = '0' + (char) (((uint8_t) *p
/ 10) % 10);
271 *(q
++) = '0' + (char) ((uint8_t) *p
% 10);
281 return (int) (q
- dest
);
284 int dns_label_escape_new(const char *p
, size_t l
, char **ret
) {
285 _cleanup_free_
char *s
= NULL
;
291 if (l
<= 0 || l
> DNS_LABEL_MAX
)
294 s
= new(char, DNS_LABEL_ESCAPED_MAX
);
298 r
= dns_label_escape(p
, l
, s
, DNS_LABEL_ESCAPED_MAX
);
308 int dns_label_apply_idna(const char *encoded
, size_t encoded_size
, char *decoded
, size_t decoded_max
) {
309 _cleanup_free_
uint32_t *input
= NULL
;
310 size_t input_size
, l
;
312 bool contains_8bit
= false;
313 char buffer
[DNS_LABEL_MAX
+1];
318 /* Converts an U-label into an A-label */
320 if (encoded_size
<= 0)
323 for (p
= encoded
; p
< encoded
+ encoded_size
; p
++)
324 if ((uint8_t) *p
> 127)
325 contains_8bit
= true;
327 if (!contains_8bit
) {
328 if (encoded_size
> DNS_LABEL_MAX
)
334 input
= stringprep_utf8_to_ucs4(encoded
, encoded_size
, &input_size
);
338 if (idna_to_ascii_4i(input
, input_size
, buffer
, 0) != 0)
343 /* Verify that the result is not longer than one DNS label. */
344 if (l
<= 0 || l
> DNS_LABEL_MAX
)
349 memcpy(decoded
, buffer
, l
);
351 /* If there's room, append a trailing NUL byte, but only then */
358 int dns_label_undo_idna(const char *encoded
, size_t encoded_size
, char *decoded
, size_t decoded_max
) {
359 size_t input_size
, output_size
;
360 _cleanup_free_
uint32_t *input
= NULL
;
361 _cleanup_free_
char *result
= NULL
;
362 uint32_t *output
= NULL
;
365 /* To be invoked after unescaping. Converts an A-label into an U-label. */
370 if (encoded_size
<= 0 || encoded_size
> DNS_LABEL_MAX
)
373 if (!memory_startswith(encoded
, encoded_size
, IDNA_ACE_PREFIX
))
376 input
= stringprep_utf8_to_ucs4(encoded
, encoded_size
, &input_size
);
380 output_size
= input_size
;
381 output
= newa(uint32_t, output_size
);
383 idna_to_unicode_44i(input
, input_size
, output
, &output_size
, 0);
385 result
= stringprep_ucs4_to_utf8(output
, output_size
, NULL
, &w
);
393 memcpy(decoded
, result
, w
);
395 /* Append trailing NUL byte if there's space, but only then. */
403 int dns_name_concat(const char *a
, const char *b
, DNSLabelFlags flags
, char **_ret
) {
404 _cleanup_free_
char *ret
= NULL
;
405 size_t n
= 0, allocated
= 0;
418 char label
[DNS_LABEL_MAX
];
420 r
= dns_label_unescape(&p
, label
, sizeof label
, flags
);
428 /* Now continue with the second string, if there is one */
437 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
440 r
= dns_label_escape(label
, r
, ret
+ n
+ !first
, DNS_LABEL_ESCAPED_MAX
);
447 char escaped
[DNS_LABEL_ESCAPED_MAX
];
449 r
= dns_label_escape(label
, r
, escaped
, sizeof(escaped
));
463 if (n
> DNS_HOSTNAME_MAX
)
468 /* Nothing appended? If so, generate at least a single dot, to indicate the DNS root domain */
469 if (!GREEDY_REALLOC(ret
, allocated
, 2))
474 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
479 *_ret
= TAKE_PTR(ret
);
485 void dns_name_hash_func(const char *p
, struct siphash
*state
) {
491 char label
[DNS_LABEL_MAX
+1];
493 r
= dns_label_unescape(&p
, label
, sizeof label
, 0);
499 ascii_strlower_n(label
, r
);
500 siphash24_compress(label
, r
, state
);
501 siphash24_compress_byte(0, state
); /* make sure foobar and foo.bar result in different hashes */
504 /* enforce that all names are terminated by the empty label */
505 string_hash_func("", state
);
508 int dns_name_compare_func(const char *a
, const char *b
) {
519 char la
[DNS_LABEL_MAX
], lb
[DNS_LABEL_MAX
];
521 if (x
== NULL
&& y
== NULL
)
524 r
= dns_label_unescape_suffix(a
, &x
, la
, sizeof(la
));
525 q
= dns_label_unescape_suffix(b
, &y
, lb
, sizeof(lb
));
529 r
= ascii_strcasecmp_nn(la
, r
, lb
, q
);
535 DEFINE_HASH_OPS(dns_name_hash_ops
, char, dns_name_hash_func
, dns_name_compare_func
);
537 int dns_name_equal(const char *x
, const char *y
) {
544 char la
[DNS_LABEL_MAX
], lb
[DNS_LABEL_MAX
];
546 r
= dns_label_unescape(&x
, la
, sizeof la
, 0);
550 q
= dns_label_unescape(&y
, lb
, sizeof lb
, 0);
559 if (ascii_strcasecmp_n(la
, lb
, r
) != 0)
564 int dns_name_endswith(const char *name
, const char *suffix
) {
565 const char *n
, *s
, *saved_n
= NULL
;
575 char ln
[DNS_LABEL_MAX
], ls
[DNS_LABEL_MAX
];
577 r
= dns_label_unescape(&n
, ln
, sizeof ln
, 0);
584 q
= dns_label_unescape(&s
, ls
, sizeof ls
, 0);
588 if (r
== 0 && q
== 0)
590 if (r
== 0 && saved_n
== n
)
593 if (r
!= q
|| ascii_strcasecmp_n(ln
, ls
, r
) != 0) {
595 /* Not the same, let's jump back, and try with the next label again */
597 n
= TAKE_PTR(saved_n
);
602 int dns_name_startswith(const char *name
, const char *prefix
) {
613 char ln
[DNS_LABEL_MAX
], lp
[DNS_LABEL_MAX
];
615 r
= dns_label_unescape(&p
, lp
, sizeof lp
, 0);
621 q
= dns_label_unescape(&n
, ln
, sizeof ln
, 0);
627 if (ascii_strcasecmp_n(ln
, lp
, r
) != 0)
632 int dns_name_change_suffix(const char *name
, const char *old_suffix
, const char *new_suffix
, char **ret
) {
633 const char *n
, *s
, *saved_before
= NULL
, *saved_after
= NULL
, *prefix
;
645 char ln
[DNS_LABEL_MAX
], ls
[DNS_LABEL_MAX
];
650 r
= dns_label_unescape(&n
, ln
, sizeof ln
, 0);
657 q
= dns_label_unescape(&s
, ls
, sizeof ls
, 0);
661 if (r
== 0 && q
== 0)
663 if (r
== 0 && saved_after
== n
) {
664 *ret
= NULL
; /* doesn't match */
668 if (r
!= q
|| ascii_strcasecmp_n(ln
, ls
, r
) != 0) {
670 /* Not the same, let's jump back, and try with the next label again */
672 n
= TAKE_PTR(saved_after
);
677 /* Found it! Now generate the new name */
678 prefix
= strndupa(name
, saved_before
- name
);
680 r
= dns_name_concat(prefix
, new_suffix
, 0, ret
);
687 int dns_name_between(const char *a
, const char *b
, const char *c
) {
688 /* Determine if b is strictly greater than a and strictly smaller than c.
689 We consider the order of names to be circular, so that if a is
690 strictly greater than c, we consider b to be between them if it is
691 either greater than a or smaller than c. This is how the canonical
692 DNS name order used in NSEC records work. */
694 if (dns_name_compare_func(a
, c
) < 0)
696 a and c are properly ordered:
699 return dns_name_compare_func(a
, b
) < 0 &&
700 dns_name_compare_func(b
, c
) < 0;
703 a and c are equal or 'reversed':
708 return dns_name_compare_func(b
, c
) < 0 ||
709 dns_name_compare_func(a
, b
) < 0;
712 int dns_name_reverse(int family
, const union in_addr_union
*a
, char **ret
) {
719 p
= (const uint8_t*) a
;
721 if (family
== AF_INET
)
722 r
= asprintf(ret
, "%u.%u.%u.%u.in-addr.arpa", p
[3], p
[2], p
[1], p
[0]);
723 else if (family
== AF_INET6
)
724 r
= asprintf(ret
, "%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.%c.ip6.arpa",
725 hexchar(p
[15] & 0xF), hexchar(p
[15] >> 4), hexchar(p
[14] & 0xF), hexchar(p
[14] >> 4),
726 hexchar(p
[13] & 0xF), hexchar(p
[13] >> 4), hexchar(p
[12] & 0xF), hexchar(p
[12] >> 4),
727 hexchar(p
[11] & 0xF), hexchar(p
[11] >> 4), hexchar(p
[10] & 0xF), hexchar(p
[10] >> 4),
728 hexchar(p
[ 9] & 0xF), hexchar(p
[ 9] >> 4), hexchar(p
[ 8] & 0xF), hexchar(p
[ 8] >> 4),
729 hexchar(p
[ 7] & 0xF), hexchar(p
[ 7] >> 4), hexchar(p
[ 6] & 0xF), hexchar(p
[ 6] >> 4),
730 hexchar(p
[ 5] & 0xF), hexchar(p
[ 5] >> 4), hexchar(p
[ 4] & 0xF), hexchar(p
[ 4] >> 4),
731 hexchar(p
[ 3] & 0xF), hexchar(p
[ 3] >> 4), hexchar(p
[ 2] & 0xF), hexchar(p
[ 2] >> 4),
732 hexchar(p
[ 1] & 0xF), hexchar(p
[ 1] >> 4), hexchar(p
[ 0] & 0xF), hexchar(p
[ 0] >> 4));
734 return -EAFNOSUPPORT
;
741 int dns_name_address(const char *p
, int *family
, union in_addr_union
*address
) {
748 r
= dns_name_endswith(p
, "in-addr.arpa");
755 for (i
= 0; i
< ELEMENTSOF(a
); i
++) {
756 char label
[DNS_LABEL_MAX
+1];
758 r
= dns_label_unescape(&p
, label
, sizeof label
, 0);
766 r
= safe_atou8(label
, &a
[i
]);
771 r
= dns_name_equal(p
, "in-addr.arpa");
776 address
->in
.s_addr
= htobe32(((uint32_t) a
[3] << 24) |
777 ((uint32_t) a
[2] << 16) |
778 ((uint32_t) a
[1] << 8) |
784 r
= dns_name_endswith(p
, "ip6.arpa");
791 for (i
= 0; i
< ELEMENTSOF(a
.s6_addr
); i
++) {
792 char label
[DNS_LABEL_MAX
+1];
795 r
= dns_label_unescape(&p
, label
, sizeof label
, 0);
800 x
= unhexchar(label
[0]);
804 r
= dns_label_unescape(&p
, label
, sizeof label
, 0);
809 y
= unhexchar(label
[0]);
813 a
.s6_addr
[ELEMENTSOF(a
.s6_addr
) - i
- 1] = (uint8_t) y
<< 4 | (uint8_t) x
;
816 r
= dns_name_equal(p
, "ip6.arpa");
828 bool dns_name_is_root(const char *name
) {
832 /* There are exactly two ways to encode the root domain name:
833 * as empty string, or with a single dot. */
835 return STR_IN_SET(name
, "", ".");
838 bool dns_name_is_single_label(const char *name
) {
843 r
= dns_name_parent(&name
);
847 return dns_name_is_root(name
);
850 /* Encode a domain name according to RFC 1035 Section 3.1, without compression */
851 int dns_name_to_wire_format(const char *domain
, uint8_t *buffer
, size_t len
, bool canonical
) {
852 uint8_t *label_length
, *out
;
861 /* Reserve a byte for label length */
868 /* Convert and copy a single label. Note that
869 * dns_label_unescape() returns 0 when it hits the end
870 * of the domain name, which we rely on here to encode
871 * the trailing NUL byte. */
872 r
= dns_label_unescape(&domain
, (char *) out
, len
, 0);
876 /* Optionally, output the name in DNSSEC canonical
877 * format, as described in RFC 4034, section 6.2. Or
878 * in other words: in lower-case. */
880 ascii_strlower_n((char*) out
, (size_t) r
);
882 /* Fill label length, move forward */
889 /* Verify the maximum size of the encoded name. The trailing
890 * dot + NUL byte account are included this time, hence
891 * compare against DNS_HOSTNAME_MAX + 2 (which is 255) this
893 if (out
- buffer
> DNS_HOSTNAME_MAX
+ 2)
899 static bool srv_type_label_is_valid(const char *label
, size_t n
) {
904 if (n
< 2) /* Label needs to be at least 2 chars long */
907 if (label
[0] != '_') /* First label char needs to be underscore */
910 /* Second char must be a letter */
911 if (!(label
[1] >= 'A' && label
[1] <= 'Z') &&
912 !(label
[1] >= 'a' && label
[1] <= 'z'))
915 /* Third and further chars must be alphanumeric or a hyphen */
916 for (k
= 2; k
< n
; k
++) {
917 if (!(label
[k
] >= 'A' && label
[k
] <= 'Z') &&
918 !(label
[k
] >= 'a' && label
[k
] <= 'z') &&
919 !(label
[k
] >= '0' && label
[k
] <= '9') &&
927 bool dns_srv_type_is_valid(const char *name
) {
935 char label
[DNS_LABEL_MAX
];
937 /* This more or less implements RFC 6335, Section 5.1 */
939 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
948 if (!srv_type_label_is_valid(label
, r
))
954 return c
== 2; /* exactly two labels */
957 bool dnssd_srv_type_is_valid(const char *name
) {
958 return dns_srv_type_is_valid(name
) &&
959 ((dns_name_endswith(name
, "_tcp") > 0) ||
960 (dns_name_endswith(name
, "_udp") > 0)); /* Specific to DNS-SD. RFC 6763, Section 7 */
963 bool dns_service_name_is_valid(const char *name
) {
966 /* This more or less implements RFC 6763, Section 4.1.1 */
971 if (!utf8_is_valid(name
))
974 if (string_has_cc(name
, NULL
))
986 int dns_service_join(const char *name
, const char *type
, const char *domain
, char **ret
) {
987 char escaped
[DNS_LABEL_ESCAPED_MAX
];
988 _cleanup_free_
char *n
= NULL
;
995 if (!dns_srv_type_is_valid(type
))
999 return dns_name_concat(type
, domain
, 0, ret
);
1001 if (!dns_service_name_is_valid(name
))
1004 r
= dns_label_escape(name
, strlen(name
), escaped
, sizeof(escaped
));
1008 r
= dns_name_concat(type
, domain
, 0, &n
);
1012 return dns_name_concat(escaped
, n
, 0, ret
);
1015 static bool dns_service_name_label_is_valid(const char *label
, size_t n
) {
1020 if (memchr(label
, 0, n
))
1023 s
= strndupa(label
, n
);
1024 return dns_service_name_is_valid(s
);
1027 int dns_service_split(const char *joined
, char **_name
, char **_type
, char **_domain
) {
1028 _cleanup_free_
char *name
= NULL
, *type
= NULL
, *domain
= NULL
;
1029 const char *p
= joined
, *q
= NULL
, *d
= NULL
;
1030 char a
[DNS_LABEL_MAX
], b
[DNS_LABEL_MAX
], c
[DNS_LABEL_MAX
];
1036 /* Get first label from the full name */
1037 an
= dns_label_unescape(&p
, a
, sizeof(a
), 0);
1044 /* If there was a first label, try to get the second one */
1045 bn
= dns_label_unescape(&p
, b
, sizeof(b
), 0);
1052 /* If there was a second label, try to get the third one */
1054 cn
= dns_label_unescape(&p
, c
, sizeof(c
), 0);
1065 if (x
>= 2 && srv_type_label_is_valid(b
, bn
)) {
1067 if (x
>= 3 && srv_type_label_is_valid(c
, cn
)) {
1069 if (dns_service_name_label_is_valid(a
, an
)) {
1070 /* OK, got <name> . <type> . <type2> . <domain> */
1072 name
= strndup(a
, an
);
1076 type
= strjoin(b
, ".", c
);
1084 } else if (srv_type_label_is_valid(a
, an
)) {
1086 /* OK, got <type> . <type2> . <domain> */
1090 type
= strjoin(a
, ".", b
);
1104 r
= dns_name_normalize(d
, 0, &domain
);
1109 *_domain
= TAKE_PTR(domain
);
1112 *_type
= TAKE_PTR(type
);
1115 *_name
= TAKE_PTR(name
);
1120 static int dns_name_build_suffix_table(const char *name
, const char *table
[]) {
1130 if (n
> DNS_N_LABELS_MAX
)
1134 r
= dns_name_parent(&p
);
1146 int dns_name_suffix(const char *name
, unsigned n_labels
, const char **ret
) {
1147 const char* labels
[DNS_N_LABELS_MAX
+1];
1153 n
= dns_name_build_suffix_table(name
, labels
);
1157 if ((unsigned) n
< n_labels
)
1160 *ret
= labels
[n
- n_labels
];
1161 return (int) (n
- n_labels
);
1164 int dns_name_skip(const char *a
, unsigned n_labels
, const char **ret
) {
1170 for (; n_labels
> 0; n_labels
--) {
1171 r
= dns_name_parent(&a
);
1184 int dns_name_count_labels(const char *name
) {
1193 r
= dns_name_parent(&p
);
1199 if (n
>= DNS_N_LABELS_MAX
)
1208 int dns_name_equal_skip(const char *a
, unsigned n_labels
, const char *b
) {
1214 r
= dns_name_skip(a
, n_labels
, &a
);
1218 return dns_name_equal(a
, b
);
1221 int dns_name_common_suffix(const char *a
, const char *b
, const char **ret
) {
1222 const char *a_labels
[DNS_N_LABELS_MAX
+1], *b_labels
[DNS_N_LABELS_MAX
+1];
1223 int n
= 0, m
= 0, k
= 0, r
, q
;
1229 /* Determines the common suffix of domain names a and b */
1231 n
= dns_name_build_suffix_table(a
, a_labels
);
1235 m
= dns_name_build_suffix_table(b
, b_labels
);
1240 char la
[DNS_LABEL_MAX
], lb
[DNS_LABEL_MAX
];
1243 if (k
>= n
|| k
>= m
) {
1244 *ret
= a_labels
[n
- k
];
1248 x
= a_labels
[n
- 1 - k
];
1249 r
= dns_label_unescape(&x
, la
, sizeof la
, 0);
1253 y
= b_labels
[m
- 1 - k
];
1254 q
= dns_label_unescape(&y
, lb
, sizeof lb
, 0);
1258 if (r
!= q
|| ascii_strcasecmp_n(la
, lb
, r
) != 0) {
1259 *ret
= a_labels
[n
- k
];
1267 int dns_name_apply_idna(const char *name
, char **ret
) {
1268 /* Return negative on error, 0 if not implemented, positive on success. */
1272 _cleanup_free_
char *t
= NULL
;
1277 r
= idn2_lookup_u8((uint8_t*) name
, (uint8_t**) &t
,
1278 IDN2_NFC_INPUT
| IDN2_NONTRANSITIONAL
);
1279 log_debug("idn2_lookup_u8: %s → %s", name
, t
);
1281 if (!startswith(name
, "xn--")) {
1282 _cleanup_free_
char *s
= NULL
;
1284 r
= idn2_to_unicode_8z8z(t
, &s
, 0);
1286 log_debug("idn2_to_unicode_8z8z(\"%s\") failed: %d/%s",
1287 t
, r
, idn2_strerror(r
));
1291 if (!streq_ptr(name
, s
)) {
1292 log_debug("idn2 roundtrip failed: \"%s\" → \"%s\" → \"%s\", ignoring.",
1300 return 1; /* *ret has been written */
1303 log_debug("idn2_lookup_u8(\"%s\") failed: %d/%s", name
, r
, idn2_strerror(r
));
1304 if (r
== IDN2_2HYPHEN
)
1305 /* The name has two hyphens — forbidden by IDNA2008 in some cases */
1307 if (IN_SET(r
, IDN2_TOO_BIG_DOMAIN
, IDN2_TOO_BIG_LABEL
))
1311 _cleanup_free_
char *buf
= NULL
;
1312 size_t n
= 0, allocated
= 0;
1320 char label
[DNS_LABEL_MAX
];
1322 r
= dns_label_unescape(&name
, label
, sizeof label
, 0);
1328 q
= dns_label_apply_idna(label
, r
, label
, sizeof label
);
1334 if (!GREEDY_REALLOC(buf
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1337 r
= dns_label_escape(label
, r
, buf
+ n
+ !first
, DNS_LABEL_ESCAPED_MAX
);
1349 if (n
> DNS_HOSTNAME_MAX
)
1352 if (!GREEDY_REALLOC(buf
, allocated
, n
+ 1))
1356 *ret
= TAKE_PTR(buf
);
1364 int dns_name_is_valid_or_address(const char *name
) {
1365 /* Returns > 0 if the specified name is either a valid IP address formatted as string or a valid DNS name */
1370 if (in_addr_from_string_auto(name
, NULL
, NULL
) >= 0)
1373 return dns_name_is_valid(name
);