source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c@ 59ff52d

Last change on this file since 59ff52d was 59ff52d, checked in by Jakub Jermar <jakub@…>, 7 years ago

Add async_accept_0() for accepting connections

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