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);
gethostname_strict(char ** ret)17 static inline int gethostname_strict(char **ret) {
18 return gethostname_full(0, ret);
19 }
20
gethostname_malloc(void)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
gethostname_short_malloc(void)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
is_gateway_hostname(const char * hostname)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
is_outbound_hostname(const char * hostname)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 int get_pretty_hostname(char **ret);
64