source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c@ 3f932a7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3f932a7e 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
Line 
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>
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
55static void dnsr_client_conn(cap_call_handle_t, ipc_call_t *, void *);
56
57static errno_t dnsr_init(void)
58{
59 errno_t rc;
60 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
61
62 rc = transport_init();
63 if (rc != EOK) {
64 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing transport.");
65 return EIO;
66 }
67
68 async_set_fallback_port_handler(dnsr_client_conn, NULL);
69
70 rc = loc_server_register(NAME);
71 if (rc != EOK) {
72 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
73 transport_fini();
74 return EEXIST;
75 }
76
77 service_id_t sid;
78 rc = loc_service_register(SERVICE_NAME_DNSR, &sid);
79 if (rc != EOK) {
80 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
81 transport_fini();
82 return EEXIST;
83 }
84
85 return EOK;
86}
87
88static void dnsr_name2host_srv(dnsr_client_t *client, cap_call_handle_t icall_handle,
89 ipc_call_t *icall)
90{
91 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
92
93 ip_ver_t ver = IPC_GET_ARG1(*icall);
94
95 char *name;
96 errno_t rc = async_data_write_accept((void **) &name, true, 0,
97 DNS_NAME_MAX_SIZE, 0, NULL);
98 if (rc != EOK) {
99 async_answer_0(icall_handle, rc);
100 return;
101 }
102
103 dns_host_info_t *hinfo;
104 rc = dns_name2host(name, &hinfo, ver);
105 if (rc != EOK) {
106 async_answer_0(icall_handle, rc);
107 return;
108 }
109
110 cap_call_handle_t chandle;
111 size_t size;
112 if (!async_data_read_receive(&chandle, &size)) {
113 async_answer_0(chandle, EREFUSED);
114 async_answer_0(icall_handle, EREFUSED);
115 return;
116 }
117
118 if (size != sizeof(inet_addr_t)) {
119 async_answer_0(chandle, EINVAL);
120 async_answer_0(icall_handle, EINVAL);
121 return;
122 }
123
124 rc = async_data_read_finalize(chandle, &hinfo->addr, size);
125 if (rc != EOK) {
126 async_answer_0(chandle, rc);
127 async_answer_0(icall_handle, rc);
128 return;
129 }
130
131 if (!async_data_read_receive(&chandle, &size)) {
132 async_answer_0(chandle, EREFUSED);
133 async_answer_0(icall_handle, EREFUSED);
134 return;
135 }
136
137 size_t act_size = str_size(hinfo->cname);
138 if (act_size > size) {
139 async_answer_0(chandle, EINVAL);
140 async_answer_0(icall_handle, EINVAL);
141 return;
142 }
143
144 rc = async_data_read_finalize(chandle, hinfo->cname, act_size);
145 if (rc != EOK)
146 async_answer_0(chandle, rc);
147
148 async_answer_0(icall_handle, rc);
149
150 dns_hostinfo_destroy(hinfo);
151}
152
153static void dnsr_get_srvaddr_srv(dnsr_client_t *client, cap_call_handle_t icall_handle,
154 ipc_call_t *icall)
155{
156 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
157
158 cap_call_handle_t chandle;
159 size_t size;
160 if (!async_data_read_receive(&chandle, &size)) {
161 async_answer_0(chandle, EREFUSED);
162 async_answer_0(icall_handle, EREFUSED);
163 return;
164 }
165
166 if (size != sizeof(inet_addr_t)) {
167 async_answer_0(chandle, EINVAL);
168 async_answer_0(icall_handle, EINVAL);
169 return;
170 }
171
172 // FIXME locking
173
174 errno_t rc = async_data_read_finalize(chandle, &dns_server_addr, size);
175 if (rc != EOK)
176 async_answer_0(chandle, rc);
177
178 async_answer_0(icall_handle, rc);
179}
180
181static void dnsr_set_srvaddr_srv(dnsr_client_t *client, cap_call_handle_t icall_handle,
182 ipc_call_t *icall)
183{
184 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
185
186 cap_call_handle_t chandle;
187 size_t size;
188 if (!async_data_write_receive(&chandle, &size)) {
189 async_answer_0(chandle, EREFUSED);
190 async_answer_0(icall_handle, EREFUSED);
191 return;
192 }
193
194 if (size != sizeof(inet_addr_t)) {
195 async_answer_0(chandle, EINVAL);
196 async_answer_0(icall_handle, EINVAL);
197 return;
198 }
199
200 // FIXME locking
201
202 errno_t rc = async_data_write_finalize(chandle, &dns_server_addr, size);
203 if (rc != EOK) {
204 async_answer_0(chandle, rc);
205 async_answer_0(icall_handle, rc);
206 }
207
208 async_answer_0(icall_handle, rc);
209}
210
211static void dnsr_client_conn(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
212{
213 dnsr_client_t client;
214
215 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
216
217 /* Accept the connection */
218 async_answer_0(icall_handle, EOK);
219
220 while (true) {
221 ipc_call_t call;
222 cap_call_handle_t chandle = async_get_call(&call);
223 sysarg_t method = IPC_GET_IMETHOD(call);
224
225 if (!method) {
226 /* The other side has hung up */
227 async_answer_0(chandle, EOK);
228 return;
229 }
230
231 switch (method) {
232 case DNSR_NAME2HOST:
233 dnsr_name2host_srv(&client, chandle, &call);
234 break;
235 case DNSR_GET_SRVADDR:
236 dnsr_get_srvaddr_srv(&client, chandle, &call);
237 break;
238 case DNSR_SET_SRVADDR:
239 dnsr_set_srvaddr_srv(&client, chandle, &call);
240 break;
241 default:
242 async_answer_0(chandle, EINVAL);
243 }
244 }
245}
246
247int main(int argc, char *argv[])
248{
249 errno_t rc;
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.