source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c@ 9c1841b

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

Do not answer a call twice

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