source: mainline/uspace/srv/net/dnsrsrv/dnsrsrv.c@ 959d2ec

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

Return canonical name to caller.

  • Property mode set to 100644
File size: 5.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 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 callid,
87 ipc_call_t *call)
88{
89 char *name;
90 dns_host_info_t *hinfo;
91 ipc_callid_t rcallid;
92 size_t size;
93 sysarg_t retval;
94 size_t act_size;
95 int rc;
96
97 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
98
99 rc = async_data_write_accept((void **) &name, true, 0,
100 DNS_NAME_MAX_SIZE, 0, NULL);
101 if (rc != EOK) {
102 async_answer_0(callid, rc);
103 return;
104 }
105
106 if (!async_data_read_receive(&rcallid, &size)) {
107 async_answer_0(rcallid, EREFUSED);
108 async_answer_0(callid, EREFUSED);
109 return;
110 }
111
112 rc = dns_name2host(name, &hinfo);
113 if (rc != EOK) {
114 async_answer_0(rcallid, rc);
115 async_answer_0(callid, rc);
116 return;
117 }
118
119 act_size = str_size(hinfo->cname);
120 if (act_size > size) {
121 async_answer_0(rcallid, EOVERFLOW);
122 async_answer_0(callid, EOVERFLOW);
123 return;
124 }
125
126 retval = async_data_read_finalize(rcallid, hinfo->cname, act_size);
127 async_answer_1(callid, retval, hinfo->addr.ipv4);
128
129 dns_hostinfo_destroy(hinfo);
130}
131
132static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_callid_t callid,
133 ipc_call_t *call)
134{
135 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
136 async_answer_1(callid, EOK, dns_server_addr.ipv4);
137}
138
139static void dnsr_set_srvaddr_srv(dnsr_client_t *client, ipc_callid_t callid,
140 ipc_call_t *call)
141{
142 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
143
144 dns_server_addr.ipv4 = IPC_GET_ARG1(*call);
145
146 async_answer_0(callid, EOK);
147}
148
149static void dnsr_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
150{
151 dnsr_client_t client;
152
153 log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
154
155 /* Accept the connection */
156 async_answer_0(iid, EOK);
157
158 while (true) {
159 ipc_call_t call;
160 ipc_callid_t callid = async_get_call(&call);
161 sysarg_t method = IPC_GET_IMETHOD(call);
162
163 if (!method) {
164 /* The other side has hung up */
165 async_answer_0(callid, EOK);
166 return;
167 }
168
169 switch (method) {
170 case DNSR_NAME2HOST:
171 dnsr_name2host_srv(&client, callid, &call);
172 break;
173 case DNSR_GET_SRVADDR:
174 dnsr_get_srvaddr_srv(&client, callid, &call);
175 break;
176 case DNSR_SET_SRVADDR:
177 dnsr_set_srvaddr_srv(&client, callid, &call);
178 break;
179 default:
180 async_answer_0(callid, EINVAL);
181 }
182 }
183}
184
185int main(int argc, char *argv[])
186{
187 int rc;
188
189 printf("%s: DNS Resolution Service\n", NAME);
190
191 if (log_init(NAME) != EOK) {
192 printf(NAME ": Failed to initialize logging.\n");
193 return 1;
194 }
195
196 rc = dnsr_init();
197 if (rc != EOK)
198 return 1;
199
200 printf(NAME ": Accepting connections.\n");
201 task_retval(0);
202 async_manager();
203
204 return 0;
205}
206
207/** @}
208 */
Note: See TracBrowser for help on using the repository browser.