source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c@ 08e103d4

Last change on this file since 08e103d4 was 08e103d4, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use clearer naming for string length functions

This and the following commit change the names of functions, as well as
their documentation, to use unambiguous terms "bytes" and "code points"
instead of ambiguous terms "size", "length", and "characters".

  • 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 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_bytes(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 return;
204 }
205
206 async_answer_0(icall, rc);
207}
208
209static void dnsr_client_conn(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_accept_0(icall);
217
218 while (true) {
219 ipc_call_t call;
220 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(&call, EOK);
226 return;
227 }
228
229 switch (method) {
230 case DNSR_NAME2HOST:
231 dnsr_name2host_srv(&client, &call);
232 break;
233 case DNSR_GET_SRVADDR:
234 dnsr_get_srvaddr_srv(&client, &call);
235 break;
236 case DNSR_SET_SRVADDR:
237 dnsr_set_srvaddr_srv(&client, &call);
238 break;
239 default:
240 async_answer_0(&call, EINVAL);
241 }
242 }
243}
244
245int main(int argc, char *argv[])
246{
247 errno_t 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.