]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hostname-util.h
hexdecoct: make unbase64mem and unhexmem always use SIZE_MAX
[thirdparty/systemd.git] / src / basic / hostname-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <stdio.h>
6
7 #include "macro.h"
8 #include "strv.h"
9
10 typedef enum GetHostnameFlags {
11 GET_HOSTNAME_ALLOW_LOCALHOST = 1 << 0, /* accepts "localhost" or friends. */
12 GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 1, /* use default hostname if no hostname is set. */
13 GET_HOSTNAME_SHORT = 1 << 2, /* kills the FQDN part if present. */
14 } GetHostnameFlags;
15
16 int gethostname_full(GetHostnameFlags flags, char **ret);
17 static inline int gethostname_strict(char **ret) {
18 return gethostname_full(0, ret);
19 }
20
21 static inline char* gethostname_malloc(void) {
22 char *s;
23
24 if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT, &s) < 0)
25 return NULL;
26
27 return s;
28 }
29
30 static inline char* gethostname_short_malloc(void) {
31 char *s;
32
33 if (gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST | GET_HOSTNAME_FALLBACK_DEFAULT | GET_HOSTNAME_SHORT, &s) < 0)
34 return NULL;
35
36 return s;
37 }
38
39 char* get_default_hostname(void);
40
41 bool valid_ldh_char(char c) _const_;
42
43 typedef enum ValidHostnameFlags {
44 VALID_HOSTNAME_TRAILING_DOT = 1 << 0, /* Accept trailing dot on multi-label names */
45 VALID_HOSTNAME_DOT_HOST = 1 << 1, /* Accept ".host" as valid hostname */
46 } ValidHostnameFlags;
47
48 bool hostname_is_valid(const char *s, ValidHostnameFlags flags) _pure_;
49 char* hostname_cleanup(char *s);
50
51 bool is_localhost(const char *hostname);
52
53 static inline bool is_gateway_hostname(const char *hostname) {
54 /* This tries to identify the valid syntaxes for the our synthetic "gateway" host. */
55 return STRCASE_IN_SET(hostname, "_gateway", "_gateway.");
56 }
57
58 static inline bool is_outbound_hostname(const char *hostname) {
59 /* This tries to identify the valid syntaxes for the our synthetic "outbound" host. */
60 return STRCASE_IN_SET(hostname, "_outbound", "_outbound.");
61 }
62
63 static inline bool is_dns_stub_hostname(const char *hostname) {
64 return STRCASE_IN_SET(hostname, "_localdnsstub", "_localdnsstub.");
65 }
66
67 static inline bool is_dns_proxy_stub_hostname(const char *hostname) {
68 return STRCASE_IN_SET(hostname, "_localdnsproxy", "_localdnsproxy.");
69 }
70
71 int get_pretty_hostname(char **ret);