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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d73d992 was a46e56b, checked in by Jakub Jermar <jakub@…>, 8 years ago

Prefer handle over ID in naming handle variables

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[31e9fe0]1/*
2 * Copyright (c) 2013 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 dnsres
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
[3be9d10]55static void dnsr_client_conn(cap_call_handle_t, ipc_call_t *, void *);
[31e9fe0]56
[b7fd2a0]57static errno_t dnsr_init(void)
[31e9fe0]58{
[b7fd2a0]59 errno_t rc;
[31e9fe0]60 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
61
[48171fc4]62 rc = transport_init();
63 if (rc != EOK) {
[695b6ff]64 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing transport.");
[48171fc4]65 return EIO;
66 }
67
[b688fd8]68 async_set_fallback_port_handler(dnsr_client_conn, NULL);
[31e9fe0]69
[48171fc4]70 rc = loc_server_register(NAME);
[31e9fe0]71 if (rc != EOK) {
[c1694b6b]72 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
[48171fc4]73 transport_fini();
[31e9fe0]74 return EEXIST;
75 }
76
77 service_id_t sid;
78 rc = loc_service_register(SERVICE_NAME_DNSR, &sid);
79 if (rc != EOK) {
[c1694b6b]80 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
[48171fc4]81 transport_fini();
[31e9fe0]82 return EEXIST;
83 }
84
85 return EOK;
86}
87
[a46e56b]88static void dnsr_name2host_srv(dnsr_client_t *client, cap_call_handle_t icall_handle,
[02a09ed]89 ipc_call_t *icall)
[31e9fe0]90{
91 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
[a35b458]92
[e948fde]93 ip_ver_t ver = IPC_GET_ARG1(*icall);
[a35b458]94
[02a09ed]95 char *name;
[b7fd2a0]96 errno_t rc = async_data_write_accept((void **) &name, true, 0,
[31e9fe0]97 DNS_NAME_MAX_SIZE, 0, NULL);
98 if (rc != EOK) {
[a46e56b]99 async_answer_0(icall_handle, rc);
[31e9fe0]100 return;
101 }
[a35b458]102
[02a09ed]103 dns_host_info_t *hinfo;
[e948fde]104 rc = dns_name2host(name, &hinfo, ver);
[02a09ed]105 if (rc != EOK) {
[a46e56b]106 async_answer_0(icall_handle, rc);
[02a09ed]107 return;
108 }
[a35b458]109
[a46e56b]110 cap_call_handle_t chandle;
[02a09ed]111 size_t size;
[a46e56b]112 if (!async_data_read_receive(&chandle, &size)) {
113 async_answer_0(chandle, EREFUSED);
114 async_answer_0(icall_handle, EREFUSED);
[959d2ec]115 return;
116 }
[a35b458]117
[02a09ed]118 if (size != sizeof(inet_addr_t)) {
[a46e56b]119 async_answer_0(chandle, EINVAL);
120 async_answer_0(icall_handle, EINVAL);
[31e9fe0]121 return;
122 }
[a35b458]123
[a46e56b]124 rc = async_data_read_finalize(chandle, &hinfo->addr, size);
[3e66428]125 if (rc != EOK) {
[a46e56b]126 async_answer_0(chandle, rc);
127 async_answer_0(icall_handle, rc);
[3e66428]128 return;
129 }
[a35b458]130
[a46e56b]131 if (!async_data_read_receive(&chandle, &size)) {
132 async_answer_0(chandle, EREFUSED);
133 async_answer_0(icall_handle, EREFUSED);
[02a09ed]134 return;
135 }
[a35b458]136
[02a09ed]137 size_t act_size = str_size(hinfo->cname);
[959d2ec]138 if (act_size > size) {
[a46e56b]139 async_answer_0(chandle, EINVAL);
140 async_answer_0(icall_handle, EINVAL);
[959d2ec]141 return;
142 }
[a35b458]143
[a46e56b]144 rc = async_data_read_finalize(chandle, hinfo->cname, act_size);
[02a09ed]145 if (rc != EOK)
[a46e56b]146 async_answer_0(chandle, rc);
[a35b458]147
[a46e56b]148 async_answer_0(icall_handle, rc);
[a35b458]149
[31e9fe0]150 dns_hostinfo_destroy(hinfo);
151}
152
[a46e56b]153static void dnsr_get_srvaddr_srv(dnsr_client_t *client, cap_call_handle_t icall_handle,
[3e66428]154 ipc_call_t *icall)
[31e9fe0]155{
156 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
[a35b458]157
[a46e56b]158 cap_call_handle_t chandle;
[3e66428]159 size_t size;
[a46e56b]160 if (!async_data_read_receive(&chandle, &size)) {
161 async_answer_0(chandle, EREFUSED);
162 async_answer_0(icall_handle, EREFUSED);
[3e66428]163 return;
164 }
[a35b458]165
[a2e3ee6]166 if (size != sizeof(inet_addr_t)) {
[a46e56b]167 async_answer_0(chandle, EINVAL);
168 async_answer_0(icall_handle, EINVAL);
[3e66428]169 return;
170 }
[a35b458]171
[3e66428]172 // FIXME locking
[a35b458]173
[a46e56b]174 errno_t rc = async_data_read_finalize(chandle, &dns_server_addr, size);
[02a09ed]175 if (rc != EOK)
[a46e56b]176 async_answer_0(chandle, rc);
[a35b458]177
[a46e56b]178 async_answer_0(icall_handle, rc);
[31e9fe0]179}
180
[a46e56b]181static void dnsr_set_srvaddr_srv(dnsr_client_t *client, cap_call_handle_t icall_handle,
[3e66428]182 ipc_call_t *icall)
[31e9fe0]183{
184 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
[a35b458]185
[a46e56b]186 cap_call_handle_t chandle;
[3e66428]187 size_t size;
[a46e56b]188 if (!async_data_write_receive(&chandle, &size)) {
189 async_answer_0(chandle, EREFUSED);
190 async_answer_0(icall_handle, EREFUSED);
[3e66428]191 return;
192 }
[a35b458]193
[a2e3ee6]194 if (size != sizeof(inet_addr_t)) {
[a46e56b]195 async_answer_0(chandle, EINVAL);
196 async_answer_0(icall_handle, EINVAL);
[3e66428]197 return;
198 }
[a35b458]199
[3e66428]200 // FIXME locking
[a35b458]201
[a46e56b]202 errno_t rc = async_data_write_finalize(chandle, &dns_server_addr, size);
[d8b47eca]203 if (rc != EOK) {
[a46e56b]204 async_answer_0(chandle, rc);
205 async_answer_0(icall_handle, rc);
[d8b47eca]206 }
[a35b458]207
[a46e56b]208 async_answer_0(icall_handle, rc);
[31e9fe0]209}
210
[a46e56b]211static void dnsr_client_conn(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
[31e9fe0]212{
213 dnsr_client_t client;
214
215 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
216
217 /* Accept the connection */
[a46e56b]218 async_answer_0(icall_handle, EOK);
[31e9fe0]219
220 while (true) {
221 ipc_call_t call;
[a46e56b]222 cap_call_handle_t chandle = async_get_call(&call);
[31e9fe0]223 sysarg_t method = IPC_GET_IMETHOD(call);
224
225 if (!method) {
226 /* The other side has hung up */
[a46e56b]227 async_answer_0(chandle, EOK);
[31e9fe0]228 return;
229 }
230
231 switch (method) {
232 case DNSR_NAME2HOST:
[a46e56b]233 dnsr_name2host_srv(&client, chandle, &call);
[31e9fe0]234 break;
235 case DNSR_GET_SRVADDR:
[a46e56b]236 dnsr_get_srvaddr_srv(&client, chandle, &call);
[31e9fe0]237 break;
238 case DNSR_SET_SRVADDR:
[a46e56b]239 dnsr_set_srvaddr_srv(&client, chandle, &call);
[31e9fe0]240 break;
241 default:
[a46e56b]242 async_answer_0(chandle, EINVAL);
[31e9fe0]243 }
244 }
245}
246
247int main(int argc, char *argv[])
248{
[b7fd2a0]249 errno_t rc;
[31e9fe0]250
251 printf("%s: DNS Resolution Service\n", NAME);
252
253 if (log_init(NAME) != EOK) {
254 printf(NAME ": Failed to initialize logging.\n");
255 return 1;
256 }
257
258 rc = dnsr_init();
259 if (rc != EOK)
260 return 1;
261
262 printf(NAME ": Accepting connections.\n");
263 task_retval(0);
264 async_manager();
265
266 return 0;
267}
268
269/** @}
270 */
Note: See TracBrowser for help on using the repository browser.