source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c

Last change on this file was ca48672, checked in by Jiri Svoboda <jiri@…>, 9 days ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[31e9fe0]1/*
[ca48672]2 * Copyright (c) 2025 Jiri Svoboda
[31e9fe0]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
[a63966d]29/** @addtogroup dnsrsrv
[31e9fe0]30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <async.h>
37#include <errno.h>
[c1694b6b]38#include <str_error.h>
[31e9fe0]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>
[1d6dd2a]45#include <str.h>
[31e9fe0]46#include <task.h>
47
48#include "dns_msg.h"
49#include "dns_std.h"
50#include "query.h"
[48171fc4]51#include "transport.h"
[31e9fe0]52
53#define NAME "dnsres"
54
[984a9ba]55static void dnsr_client_conn(ipc_call_t *, void *);
[31e9fe0]56
[b7fd2a0]57static errno_t dnsr_init(void)
[31e9fe0]58{
[4c6fd56]59 loc_srv_t *srv;
[b7fd2a0]60 errno_t rc;
[31e9fe0]61 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
62
[48171fc4]63 rc = transport_init();
64 if (rc != EOK) {
[695b6ff]65 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing transport.");
[48171fc4]66 return EIO;
67 }
68
[b688fd8]69 async_set_fallback_port_handler(dnsr_client_conn, NULL);
[31e9fe0]70
[4c6fd56]71 rc = loc_server_register(NAME, &srv);
[31e9fe0]72 if (rc != EOK) {
[c1694b6b]73 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
[48171fc4]74 transport_fini();
[31e9fe0]75 return EEXIST;
76 }
77
78 service_id_t sid;
[ca48672]79 rc = loc_service_register(srv, SERVICE_NAME_DNSR, fallback_port_id,
80 &sid);
[31e9fe0]81 if (rc != EOK) {
[4c6fd56]82 loc_server_unregister(srv);
[c1694b6b]83 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
[48171fc4]84 transport_fini();
[31e9fe0]85 return EEXIST;
86 }
87
88 return EOK;
89}
90
[984a9ba]91static void dnsr_name2host_srv(dnsr_client_t *client, ipc_call_t *icall)
[31e9fe0]92{
93 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
[a35b458]94
[fafb8e5]95 ip_ver_t ver = ipc_get_arg1(icall);
[a35b458]96
[02a09ed]97 char *name;
[b7fd2a0]98 errno_t rc = async_data_write_accept((void **) &name, true, 0,
[31e9fe0]99 DNS_NAME_MAX_SIZE, 0, NULL);
100 if (rc != EOK) {
[984a9ba]101 async_answer_0(icall, rc);
[31e9fe0]102 return;
103 }
[a35b458]104
[02a09ed]105 dns_host_info_t *hinfo;
[e948fde]106 rc = dns_name2host(name, &hinfo, ver);
[02a09ed]107 if (rc != EOK) {
[984a9ba]108 async_answer_0(icall, rc);
[02a09ed]109 return;
110 }
[a35b458]111
[984a9ba]112 ipc_call_t call;
[02a09ed]113 size_t size;
[984a9ba]114 if (!async_data_read_receive(&call, &size)) {
115 async_answer_0(&call, EREFUSED);
116 async_answer_0(icall, EREFUSED);
[959d2ec]117 return;
118 }
[a35b458]119
[02a09ed]120 if (size != sizeof(inet_addr_t)) {
[984a9ba]121 async_answer_0(&call, EINVAL);
122 async_answer_0(icall, EINVAL);
[31e9fe0]123 return;
124 }
[a35b458]125
[984a9ba]126 rc = async_data_read_finalize(&call, &hinfo->addr, size);
[3e66428]127 if (rc != EOK) {
[984a9ba]128 async_answer_0(&call, rc);
129 async_answer_0(icall, rc);
[3e66428]130 return;
131 }
[a35b458]132
[984a9ba]133 if (!async_data_read_receive(&call, &size)) {
134 async_answer_0(&call, EREFUSED);
135 async_answer_0(icall, EREFUSED);
[02a09ed]136 return;
137 }
[a35b458]138
[02a09ed]139 size_t act_size = str_size(hinfo->cname);
[959d2ec]140 if (act_size > size) {
[984a9ba]141 async_answer_0(&call, EINVAL);
142 async_answer_0(icall, EINVAL);
[959d2ec]143 return;
144 }
[a35b458]145
[984a9ba]146 rc = async_data_read_finalize(&call, hinfo->cname, act_size);
[02a09ed]147 if (rc != EOK)
[984a9ba]148 async_answer_0(&call, rc);
[a35b458]149
[984a9ba]150 async_answer_0(icall, rc);
[a35b458]151
[31e9fe0]152 dns_hostinfo_destroy(hinfo);
153}
154
[984a9ba]155static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_call_t *icall)
[31e9fe0]156{
157 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
[a35b458]158
[984a9ba]159 ipc_call_t call;
[3e66428]160 size_t size;
[984a9ba]161 if (!async_data_read_receive(&call, &size)) {
162 async_answer_0(&call, EREFUSED);
163 async_answer_0(icall, EREFUSED);
[3e66428]164 return;
165 }
[a35b458]166
[a2e3ee6]167 if (size != sizeof(inet_addr_t)) {
[984a9ba]168 async_answer_0(&call, EINVAL);
169 async_answer_0(icall, EINVAL);
[3e66428]170 return;
171 }
[a35b458]172
[3e66428]173 // FIXME locking
[a35b458]174
[984a9ba]175 errno_t rc = async_data_read_finalize(&call, &dns_server_addr, size);
[02a09ed]176 if (rc != EOK)
[984a9ba]177 async_answer_0(&call, rc);
[a35b458]178
[984a9ba]179 async_answer_0(icall, rc);
[31e9fe0]180}
181
[984a9ba]182static void dnsr_set_srvaddr_srv(dnsr_client_t *client, ipc_call_t *icall)
[31e9fe0]183{
184 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
[a35b458]185
[984a9ba]186 ipc_call_t call;
[3e66428]187 size_t size;
[984a9ba]188 if (!async_data_write_receive(&call, &size)) {
189 async_answer_0(&call, EREFUSED);
190 async_answer_0(icall, EREFUSED);
[3e66428]191 return;
192 }
[a35b458]193
[a2e3ee6]194 if (size != sizeof(inet_addr_t)) {
[984a9ba]195 async_answer_0(&call, EINVAL);
196 async_answer_0(icall, EINVAL);
[3e66428]197 return;
198 }
[a35b458]199
[3e66428]200 // FIXME locking
[a35b458]201
[984a9ba]202 errno_t rc = async_data_write_finalize(&call, &dns_server_addr, size);
[d8b47eca]203 if (rc != EOK) {
[984a9ba]204 async_answer_0(&call, rc);
205 async_answer_0(icall, rc);
[9c1841b]206 return;
[d8b47eca]207 }
[a35b458]208
[984a9ba]209 async_answer_0(icall, rc);
[31e9fe0]210}
211
[984a9ba]212static void dnsr_client_conn(ipc_call_t *icall, void *arg)
[31e9fe0]213{
214 dnsr_client_t client;
215
216 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
217
218 /* Accept the connection */
[beb83c1]219 async_accept_0(icall);
[31e9fe0]220
221 while (true) {
222 ipc_call_t call;
[984a9ba]223 async_get_call(&call);
[fafb8e5]224 sysarg_t method = ipc_get_imethod(&call);
[31e9fe0]225
226 if (!method) {
227 /* The other side has hung up */
[984a9ba]228 async_answer_0(&call, EOK);
[31e9fe0]229 return;
230 }
231
232 switch (method) {
233 case DNSR_NAME2HOST:
[984a9ba]234 dnsr_name2host_srv(&client, &call);
[31e9fe0]235 break;
236 case DNSR_GET_SRVADDR:
[984a9ba]237 dnsr_get_srvaddr_srv(&client, &call);
[31e9fe0]238 break;
239 case DNSR_SET_SRVADDR:
[984a9ba]240 dnsr_set_srvaddr_srv(&client, &call);
[31e9fe0]241 break;
242 default:
[984a9ba]243 async_answer_0(&call, EINVAL);
[31e9fe0]244 }
245 }
246}
247
248int main(int argc, char *argv[])
249{
[b7fd2a0]250 errno_t rc;
[31e9fe0]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 */
Note: See TracBrowser for help on using the repository browser.