| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 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 |
|
|---|
| 55 | static void dnsr_client_conn(ipc_call_t *, void *);
|
|---|
| 56 |
|
|---|
| 57 | static errno_t dnsr_init(void)
|
|---|
| 58 | {
|
|---|
| 59 | loc_srv_t *srv;
|
|---|
| 60 | errno_t rc;
|
|---|
| 61 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
|
|---|
| 62 |
|
|---|
| 63 | rc = transport_init();
|
|---|
| 64 | if (rc != EOK) {
|
|---|
| 65 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing transport.");
|
|---|
| 66 | return EIO;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | async_set_fallback_port_handler(dnsr_client_conn, NULL);
|
|---|
| 70 |
|
|---|
| 71 | rc = loc_server_register(NAME, &srv);
|
|---|
| 72 | if (rc != EOK) {
|
|---|
| 73 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
|
|---|
| 74 | transport_fini();
|
|---|
| 75 | return EEXIST;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | service_id_t sid;
|
|---|
| 79 | rc = loc_service_register(srv, SERVICE_NAME_DNSR, &sid);
|
|---|
| 80 | if (rc != EOK) {
|
|---|
| 81 | loc_server_unregister(srv);
|
|---|
| 82 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
|
|---|
| 83 | transport_fini();
|
|---|
| 84 | return EEXIST;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | return EOK;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static void dnsr_name2host_srv(dnsr_client_t *client, ipc_call_t *icall)
|
|---|
| 91 | {
|
|---|
| 92 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
|
|---|
| 93 |
|
|---|
| 94 | ip_ver_t ver = ipc_get_arg1(icall);
|
|---|
| 95 |
|
|---|
| 96 | char *name;
|
|---|
| 97 | errno_t rc = async_data_write_accept((void **) &name, true, 0,
|
|---|
| 98 | DNS_NAME_MAX_SIZE, 0, NULL);
|
|---|
| 99 | if (rc != EOK) {
|
|---|
| 100 | async_answer_0(icall, rc);
|
|---|
| 101 | return;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | dns_host_info_t *hinfo;
|
|---|
| 105 | rc = dns_name2host(name, &hinfo, ver);
|
|---|
| 106 | if (rc != EOK) {
|
|---|
| 107 | async_answer_0(icall, rc);
|
|---|
| 108 | return;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | ipc_call_t call;
|
|---|
| 112 | size_t size;
|
|---|
| 113 | if (!async_data_read_receive(&call, &size)) {
|
|---|
| 114 | async_answer_0(&call, EREFUSED);
|
|---|
| 115 | async_answer_0(icall, EREFUSED);
|
|---|
| 116 | return;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | if (size != sizeof(inet_addr_t)) {
|
|---|
| 120 | async_answer_0(&call, EINVAL);
|
|---|
| 121 | async_answer_0(icall, EINVAL);
|
|---|
| 122 | return;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | rc = async_data_read_finalize(&call, &hinfo->addr, size);
|
|---|
| 126 | if (rc != EOK) {
|
|---|
| 127 | async_answer_0(&call, rc);
|
|---|
| 128 | async_answer_0(icall, rc);
|
|---|
| 129 | return;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | if (!async_data_read_receive(&call, &size)) {
|
|---|
| 133 | async_answer_0(&call, EREFUSED);
|
|---|
| 134 | async_answer_0(icall, EREFUSED);
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | size_t act_size = str_size(hinfo->cname);
|
|---|
| 139 | if (act_size > size) {
|
|---|
| 140 | async_answer_0(&call, EINVAL);
|
|---|
| 141 | async_answer_0(icall, EINVAL);
|
|---|
| 142 | return;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | rc = async_data_read_finalize(&call, hinfo->cname, act_size);
|
|---|
| 146 | if (rc != EOK)
|
|---|
| 147 | async_answer_0(&call, rc);
|
|---|
| 148 |
|
|---|
| 149 | async_answer_0(icall, rc);
|
|---|
| 150 |
|
|---|
| 151 | dns_hostinfo_destroy(hinfo);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_call_t *icall)
|
|---|
| 155 | {
|
|---|
| 156 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
|
|---|
| 157 |
|
|---|
| 158 | ipc_call_t call;
|
|---|
| 159 | size_t size;
|
|---|
| 160 | if (!async_data_read_receive(&call, &size)) {
|
|---|
| 161 | async_answer_0(&call, EREFUSED);
|
|---|
| 162 | async_answer_0(icall, EREFUSED);
|
|---|
| 163 | return;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | if (size != sizeof(inet_addr_t)) {
|
|---|
| 167 | async_answer_0(&call, EINVAL);
|
|---|
| 168 | async_answer_0(icall, EINVAL);
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | // FIXME locking
|
|---|
| 173 |
|
|---|
| 174 | errno_t rc = async_data_read_finalize(&call, &dns_server_addr, size);
|
|---|
| 175 | if (rc != EOK)
|
|---|
| 176 | async_answer_0(&call, rc);
|
|---|
| 177 |
|
|---|
| 178 | async_answer_0(icall, rc);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static void dnsr_set_srvaddr_srv(dnsr_client_t *client, ipc_call_t *icall)
|
|---|
| 182 | {
|
|---|
| 183 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
|
|---|
| 184 |
|
|---|
| 185 | ipc_call_t call;
|
|---|
| 186 | size_t size;
|
|---|
| 187 | if (!async_data_write_receive(&call, &size)) {
|
|---|
| 188 | async_answer_0(&call, EREFUSED);
|
|---|
| 189 | async_answer_0(icall, EREFUSED);
|
|---|
| 190 | return;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | if (size != sizeof(inet_addr_t)) {
|
|---|
| 194 | async_answer_0(&call, EINVAL);
|
|---|
| 195 | async_answer_0(icall, EINVAL);
|
|---|
| 196 | return;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | // FIXME locking
|
|---|
| 200 |
|
|---|
| 201 | errno_t rc = async_data_write_finalize(&call, &dns_server_addr, size);
|
|---|
| 202 | if (rc != EOK) {
|
|---|
| 203 | async_answer_0(&call, rc);
|
|---|
| 204 | async_answer_0(icall, rc);
|
|---|
| 205 | return;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | async_answer_0(icall, rc);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | static void dnsr_client_conn(ipc_call_t *icall, void *arg)
|
|---|
| 212 | {
|
|---|
| 213 | dnsr_client_t client;
|
|---|
| 214 |
|
|---|
| 215 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
|
|---|
| 216 |
|
|---|
| 217 | /* Accept the connection */
|
|---|
| 218 | async_accept_0(icall);
|
|---|
| 219 |
|
|---|
| 220 | while (true) {
|
|---|
| 221 | ipc_call_t call;
|
|---|
| 222 | async_get_call(&call);
|
|---|
| 223 | sysarg_t method = ipc_get_imethod(&call);
|
|---|
| 224 |
|
|---|
| 225 | if (!method) {
|
|---|
| 226 | /* The other side has hung up */
|
|---|
| 227 | async_answer_0(&call, EOK);
|
|---|
| 228 | return;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | switch (method) {
|
|---|
| 232 | case DNSR_NAME2HOST:
|
|---|
| 233 | dnsr_name2host_srv(&client, &call);
|
|---|
| 234 | break;
|
|---|
| 235 | case DNSR_GET_SRVADDR:
|
|---|
| 236 | dnsr_get_srvaddr_srv(&client, &call);
|
|---|
| 237 | break;
|
|---|
| 238 | case DNSR_SET_SRVADDR:
|
|---|
| 239 | dnsr_set_srvaddr_srv(&client, &call);
|
|---|
| 240 | break;
|
|---|
| 241 | default:
|
|---|
| 242 | async_answer_0(&call, EINVAL);
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | int main(int argc, char *argv[])
|
|---|
| 248 | {
|
|---|
| 249 | errno_t rc;
|
|---|
| 250 |
|
|---|
| 251 | printf("%s: DNS Resolution Service\n", NAME);
|
|---|
| 252 |
|
|---|
| 253 | if (log_init(NAME) != EOK) {
|
|---|
| 254 | printf(NAME ": Failed to initialize logging.\n");
|
|---|
| 255 | return 1;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | rc = dnsr_init();
|
|---|
| 259 | if (rc != EOK)
|
|---|
| 260 | return 1;
|
|---|
| 261 |
|
|---|
| 262 | printf(NAME ": Accepting connections.\n");
|
|---|
| 263 | task_retval(0);
|
|---|
| 264 | async_manager();
|
|---|
| 265 |
|
|---|
| 266 | return 0;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /** @}
|
|---|
| 270 | */
|
|---|