1 | /*
|
---|
2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup dnsrsrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <async.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <ipc/dnsr.h>
|
---|
41 | #include <ipc/services.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <task.h>
|
---|
47 |
|
---|
48 | #include "dns_msg.h"
|
---|
49 | #include "dns_std.h"
|
---|
50 | #include "query.h"
|
---|
51 | #include "transport.h"
|
---|
52 |
|
---|
53 | #define NAME "dnsres"
|
---|
54 |
|
---|
55 | static void dnsr_client_conn(ipc_call_t *, void *);
|
---|
56 |
|
---|
57 | static errno_t dnsr_init(void)
|
---|
58 | {
|
---|
59 | loc_srv_t *srv;
|
---|
60 | errno_t rc;
|
---|
61 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
|
---|
62 |
|
---|
63 | rc = transport_init();
|
---|
64 | if (rc != EOK) {
|
---|
65 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing transport.");
|
---|
66 | return EIO;
|
---|
67 | }
|
---|
68 |
|
---|
69 | async_set_fallback_port_handler(dnsr_client_conn, NULL);
|
---|
70 |
|
---|
71 | rc = loc_server_register(NAME, &srv);
|
---|
72 | if (rc != EOK) {
|
---|
73 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
|
---|
74 | transport_fini();
|
---|
75 | return EEXIST;
|
---|
76 | }
|
---|
77 |
|
---|
78 | service_id_t sid;
|
---|
79 | rc = loc_service_register(srv, SERVICE_NAME_DNSR, fallback_port_id,
|
---|
80 | &sid);
|
---|
81 | if (rc != EOK) {
|
---|
82 | loc_server_unregister(srv);
|
---|
83 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
|
---|
84 | transport_fini();
|
---|
85 | return EEXIST;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return EOK;
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void dnsr_name2host_srv(dnsr_client_t *client, ipc_call_t *icall)
|
---|
92 | {
|
---|
93 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
|
---|
94 |
|
---|
95 | ip_ver_t ver = ipc_get_arg1(icall);
|
---|
96 |
|
---|
97 | char *name;
|
---|
98 | errno_t rc = async_data_write_accept((void **) &name, true, 0,
|
---|
99 | DNS_NAME_MAX_SIZE, 0, NULL);
|
---|
100 | if (rc != EOK) {
|
---|
101 | async_answer_0(icall, rc);
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | dns_host_info_t *hinfo;
|
---|
106 | rc = dns_name2host(name, &hinfo, ver);
|
---|
107 | if (rc != EOK) {
|
---|
108 | async_answer_0(icall, rc);
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | ipc_call_t call;
|
---|
113 | size_t size;
|
---|
114 | if (!async_data_read_receive(&call, &size)) {
|
---|
115 | async_answer_0(&call, EREFUSED);
|
---|
116 | async_answer_0(icall, EREFUSED);
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (size != sizeof(inet_addr_t)) {
|
---|
121 | async_answer_0(&call, EINVAL);
|
---|
122 | async_answer_0(icall, EINVAL);
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | rc = async_data_read_finalize(&call, &hinfo->addr, size);
|
---|
127 | if (rc != EOK) {
|
---|
128 | async_answer_0(&call, rc);
|
---|
129 | async_answer_0(icall, rc);
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (!async_data_read_receive(&call, &size)) {
|
---|
134 | async_answer_0(&call, EREFUSED);
|
---|
135 | async_answer_0(icall, EREFUSED);
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 | size_t act_size = str_size(hinfo->cname);
|
---|
140 | if (act_size > size) {
|
---|
141 | async_answer_0(&call, EINVAL);
|
---|
142 | async_answer_0(icall, EINVAL);
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | rc = async_data_read_finalize(&call, hinfo->cname, act_size);
|
---|
147 | if (rc != EOK)
|
---|
148 | async_answer_0(&call, rc);
|
---|
149 |
|
---|
150 | async_answer_0(icall, rc);
|
---|
151 |
|
---|
152 | dns_hostinfo_destroy(hinfo);
|
---|
153 | }
|
---|
154 |
|
---|
155 | static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_call_t *icall)
|
---|
156 | {
|
---|
157 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
|
---|
158 |
|
---|
159 | ipc_call_t call;
|
---|
160 | size_t size;
|
---|
161 | if (!async_data_read_receive(&call, &size)) {
|
---|
162 | async_answer_0(&call, EREFUSED);
|
---|
163 | async_answer_0(icall, EREFUSED);
|
---|
164 | return;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (size != sizeof(inet_addr_t)) {
|
---|
168 | async_answer_0(&call, EINVAL);
|
---|
169 | async_answer_0(icall, EINVAL);
|
---|
170 | return;
|
---|
171 | }
|
---|
172 |
|
---|
173 | // FIXME locking
|
---|
174 |
|
---|
175 | errno_t rc = async_data_read_finalize(&call, &dns_server_addr, size);
|
---|
176 | if (rc != EOK)
|
---|
177 | async_answer_0(&call, rc);
|
---|
178 |
|
---|
179 | async_answer_0(icall, rc);
|
---|
180 | }
|
---|
181 |
|
---|
182 | static void dnsr_set_srvaddr_srv(dnsr_client_t *client, ipc_call_t *icall)
|
---|
183 | {
|
---|
184 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
|
---|
185 |
|
---|
186 | ipc_call_t call;
|
---|
187 | size_t size;
|
---|
188 | if (!async_data_write_receive(&call, &size)) {
|
---|
189 | async_answer_0(&call, EREFUSED);
|
---|
190 | async_answer_0(icall, EREFUSED);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (size != sizeof(inet_addr_t)) {
|
---|
195 | async_answer_0(&call, EINVAL);
|
---|
196 | async_answer_0(icall, EINVAL);
|
---|
197 | return;
|
---|
198 | }
|
---|
199 |
|
---|
200 | // FIXME locking
|
---|
201 |
|
---|
202 | errno_t rc = async_data_write_finalize(&call, &dns_server_addr, size);
|
---|
203 | if (rc != EOK) {
|
---|
204 | async_answer_0(&call, rc);
|
---|
205 | async_answer_0(icall, rc);
|
---|
206 | return;
|
---|
207 | }
|
---|
208 |
|
---|
209 | async_answer_0(icall, rc);
|
---|
210 | }
|
---|
211 |
|
---|
212 | static void dnsr_client_conn(ipc_call_t *icall, void *arg)
|
---|
213 | {
|
---|
214 | dnsr_client_t client;
|
---|
215 |
|
---|
216 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
|
---|
217 |
|
---|
218 | /* Accept the connection */
|
---|
219 | async_accept_0(icall);
|
---|
220 |
|
---|
221 | while (true) {
|
---|
222 | ipc_call_t call;
|
---|
223 | async_get_call(&call);
|
---|
224 | sysarg_t method = ipc_get_imethod(&call);
|
---|
225 |
|
---|
226 | if (!method) {
|
---|
227 | /* The other side has hung up */
|
---|
228 | async_answer_0(&call, EOK);
|
---|
229 | return;
|
---|
230 | }
|
---|
231 |
|
---|
232 | switch (method) {
|
---|
233 | case DNSR_NAME2HOST:
|
---|
234 | dnsr_name2host_srv(&client, &call);
|
---|
235 | break;
|
---|
236 | case DNSR_GET_SRVADDR:
|
---|
237 | dnsr_get_srvaddr_srv(&client, &call);
|
---|
238 | break;
|
---|
239 | case DNSR_SET_SRVADDR:
|
---|
240 | dnsr_set_srvaddr_srv(&client, &call);
|
---|
241 | break;
|
---|
242 | default:
|
---|
243 | async_answer_0(&call, EINVAL);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | int main(int argc, char *argv[])
|
---|
249 | {
|
---|
250 | errno_t rc;
|
---|
251 |
|
---|
252 | printf("%s: DNS Resolution Service\n", NAME);
|
---|
253 |
|
---|
254 | if (log_init(NAME) != EOK) {
|
---|
255 | printf(NAME ": Failed to initialize logging.\n");
|
---|
256 | return 1;
|
---|
257 | }
|
---|
258 |
|
---|
259 | rc = dnsr_init();
|
---|
260 | if (rc != EOK)
|
---|
261 | return 1;
|
---|
262 |
|
---|
263 | printf(NAME ": Accepting connections.\n");
|
---|
264 | task_retval(0);
|
---|
265 | async_manager();
|
---|
266 |
|
---|
267 | return 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /** @}
|
---|
271 | */
|
---|