source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c@ 9359aae

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9359aae was e948fde, checked in by Jiri Svoboda <jiri@…>, 12 years ago

dnsr_name2host should use ip_ver_t instead of AF.

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