1 | /* $Id: resolv_conf_parser.h 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * resolv_conf_parser.h - interface to parser of resolv.conf resolver(5)
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef __RESOLV_CONF_PARSER_H__
|
---|
29 | #define __RESOLV_CONF_PARSER_H__
|
---|
30 |
|
---|
31 | #include <iprt/cdefs.h>
|
---|
32 | #include <iprt/net.h>
|
---|
33 |
|
---|
34 | RT_C_DECLS_BEGIN
|
---|
35 |
|
---|
36 | #define RCPS_MAX_NAMESERVERS 3
|
---|
37 | #define RCPS_MAX_SEARCHLIST 10
|
---|
38 | #define RCPS_BUFFER_SIZE 256
|
---|
39 | #define RCPS_IPVX_SIZE 47
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * RESOLV_CONF_FILE can be defined in external tests for verification of Slirp behaviour.
|
---|
43 | */
|
---|
44 | #ifndef RESOLV_CONF_FILE
|
---|
45 | # ifndef RT_OS_OS2
|
---|
46 | # define RESOLV_CONF_FILE "/etc/resolv.conf"
|
---|
47 | # else
|
---|
48 | # define RESOLV_CONF_FILE "\\MPTN\\ETC\\RESOLV2"
|
---|
49 | # endif
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * In Slirp we don't need IPv6 for general case (only for dnsproxy mode
|
---|
54 | * it's potentially acceptable)
|
---|
55 | */
|
---|
56 | #define RCPSF_IGNORE_IPV6 RT_BIT(0)
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * This flag used to request just the strings in rcps_str_nameserver,
|
---|
60 | * but no addresses in rcps_nameserver. This is not very useful,
|
---|
61 | * since we need to validate addresses anyway. This flag is ignored
|
---|
62 | * now.
|
---|
63 | */
|
---|
64 | #define RCPSF_NO_STR2IPCONV RT_BIT(1)
|
---|
65 |
|
---|
66 |
|
---|
67 | struct rcp_state
|
---|
68 | {
|
---|
69 | uint16_t rcps_port;
|
---|
70 | /**
|
---|
71 | * Filling of this array ommited iff RCPSF_NO_STR2IPCONF in rcp_state::rcps_flags set.
|
---|
72 | */
|
---|
73 | RTNETADDR rcps_nameserver[RCPS_MAX_NAMESERVERS];
|
---|
74 | /**
|
---|
75 | * this array contains non-NULL (pointing to rcp_state::rcps_nameserver_str_buffer) iff
|
---|
76 | * RCPSF_NO_STR2IPCONF in rcp_state::rcps_flags set.
|
---|
77 | */
|
---|
78 | char *rcps_str_nameserver[RCPS_MAX_NAMESERVERS];
|
---|
79 | unsigned rcps_num_nameserver;
|
---|
80 | /**
|
---|
81 | * Shortcuts to storage, note that domain is optional
|
---|
82 | * and if it's missed in resolv.conf rcps_domain should be equal
|
---|
83 | * to rcps_search_list[0]
|
---|
84 | */
|
---|
85 | char *rcps_domain;
|
---|
86 | char *rcps_searchlist[RCPS_MAX_SEARCHLIST];
|
---|
87 | unsigned rcps_num_searchlist;
|
---|
88 |
|
---|
89 | uint32_t rcps_flags;
|
---|
90 |
|
---|
91 | char rcps_domain_buffer[RCPS_BUFFER_SIZE];
|
---|
92 | char rcps_searchlist_buffer[RCPS_BUFFER_SIZE];
|
---|
93 | char rcps_nameserver_str_buffer[RCPS_MAX_NAMESERVERS * RCPS_IPVX_SIZE];
|
---|
94 | };
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * This function parses specified file (expected to conform resolver (5) Mac OSX or resolv.conf (3) Linux)
|
---|
99 | * and fills the structure.
|
---|
100 | * @return 0 - on success
|
---|
101 | * -1 - on fail.
|
---|
102 | * <code>
|
---|
103 | * struct rcp_state state;
|
---|
104 | * int rc;
|
---|
105 | *
|
---|
106 | * rc = rcp_parse(&state, "/etc/resolv.conf");
|
---|
107 | * for(i = 0; rc == 0 && i != state.rcps_num_nameserver; ++i)
|
---|
108 | * {
|
---|
109 | * if ((state.rcps_flags & RCPSF_NO_STR2IPCONV) == 0)
|
---|
110 | * {
|
---|
111 | * const RTNETADDR *addr = &state.rcps_nameserver[i];
|
---|
112 | *
|
---|
113 | * switch (state.rcps_nameserver[i].enmType)
|
---|
114 | * {
|
---|
115 | * case RTNETADDRTYPE_IPV4:
|
---|
116 | * RTPrintf("nameserver[%d]: [%RTnaipv4]:%d\n", i, addr->uAddr.IPv4, addr->uPort);
|
---|
117 | * break;
|
---|
118 | * case RTNETADDRTYPE_IPV6:
|
---|
119 | * RTPrintf("nameserver[%d]: [%RTnaipv6]:%d\n", i, &addr->uAddr.IPv6, addr->uPort);
|
---|
120 | * break;
|
---|
121 | * default:
|
---|
122 | * break;
|
---|
123 | * }
|
---|
124 | * }
|
---|
125 | * else
|
---|
126 | * RTPrintf("nameserver[%d]: %s\n", i, state.rcps_str_nameserver[i]);
|
---|
127 | * }
|
---|
128 | * </code>
|
---|
129 | *
|
---|
130 | */
|
---|
131 | int rcp_parse(struct rcp_state *, const char *);
|
---|
132 |
|
---|
133 | RT_C_DECLS_END
|
---|
134 |
|
---|
135 | #endif
|
---|