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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a5756bc was 0aa70f4, checked in by Martin Decky <martin@…>, 12 years ago

support for IPv6 DNS name resolution (AAAA)
if the desired address family of the DNS query is not explicitly specified, then IPv6 addresses take precendece over IPv4 addresses

  • Property mode set to 100644
File size: 6.1 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 <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"
49#include "transport.h"
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{
57 int rc;
58 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
59
60 rc = transport_init();
61 if (rc != EOK) {
62 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing tarnsport.");
63 return EIO;
64 }
65
66 async_set_client_connection(dnsr_client_conn);
67
68 rc = loc_server_register(NAME);
69 if (rc != EOK) {
70 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
71 transport_fini();
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);
79 transport_fini();
80 return EEXIST;
81 }
82
83 return EOK;
84}
85
86static void dnsr_name2host_srv(dnsr_client_t *client, ipc_callid_t iid,
87 ipc_call_t *icall)
88{
89 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
90
91 uint16_t af = IPC_GET_ARG1(*icall);
92
93 char *name;
94 int rc = async_data_write_accept((void **) &name, true, 0,
95 DNS_NAME_MAX_SIZE, 0, NULL);
96 if (rc != EOK) {
97 async_answer_0(iid, rc);
98 return;
99 }
100
101 dns_host_info_t *hinfo;
102 rc = dns_name2host(name, &hinfo, af);
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)) {
111 async_answer_0(callid, EREFUSED);
112 async_answer_0(iid, EREFUSED);
113 return;
114 }
115
116 if (size != sizeof(inet_addr_t)) {
117 async_answer_0(callid, EINVAL);
118 async_answer_0(iid, EINVAL);
119 return;
120 }
121
122 rc = async_data_read_finalize(callid, &hinfo->addr, size);
123 if (rc != EOK) {
124 async_answer_0(callid, rc);
125 async_answer_0(iid, rc);
126 return;
127 }
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);
136 if (act_size > size) {
137 async_answer_0(callid, EINVAL);
138 async_answer_0(iid, EINVAL);
139 return;
140 }
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
148 dns_hostinfo_destroy(hinfo);
149}
150
151static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_callid_t iid,
152 ipc_call_t *icall)
153{
154 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
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
164 if (size != sizeof(inet_addr_t)) {
165 async_answer_0(callid, EINVAL);
166 async_answer_0(iid, EINVAL);
167 return;
168 }
169
170 // FIXME locking
171
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);
177}
178
179static void dnsr_set_srvaddr_srv(dnsr_client_t *client, ipc_callid_t iid,
180 ipc_call_t *icall)
181{
182 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
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
192 if (size != sizeof(inet_addr_t)) {
193 async_answer_0(callid, EINVAL);
194 async_answer_0(iid, EINVAL);
195 return;
196 }
197
198 // FIXME locking
199
200 int rc = async_data_write_finalize(callid, &dns_server_addr, size);
201 if (rc != EOK) {
202 async_answer_0(callid, rc);
203 async_answer_0(iid, rc);
204 }
205
206 async_answer_0(iid, (sysarg_t) rc);
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.