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