[31e9fe0] | 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"
|
---|
[48171fc4] | 49 | #include "transport.h"
|
---|
[31e9fe0] | 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 | {
|
---|
[48171fc4] | 57 | int rc;
|
---|
[31e9fe0] | 58 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_init()");
|
---|
| 59 |
|
---|
[48171fc4] | 60 | rc = transport_init();
|
---|
| 61 | if (rc != EOK) {
|
---|
| 62 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing tarnsport.");
|
---|
| 63 | return EIO;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[31e9fe0] | 66 | async_set_client_connection(dnsr_client_conn);
|
---|
| 67 |
|
---|
[48171fc4] | 68 | rc = loc_server_register(NAME);
|
---|
[31e9fe0] | 69 | if (rc != EOK) {
|
---|
| 70 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
|
---|
[48171fc4] | 71 | transport_fini();
|
---|
[31e9fe0] | 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);
|
---|
[48171fc4] | 79 | transport_fini();
|
---|
[31e9fe0] | 80 | return EEXIST;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return EOK;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static 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 | int rc;
|
---|
| 92 |
|
---|
| 93 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
|
---|
| 94 |
|
---|
| 95 | rc = async_data_write_accept((void **) &name, true, 0,
|
---|
| 96 | DNS_NAME_MAX_SIZE, 0, NULL);
|
---|
| 97 | if (rc != EOK) {
|
---|
| 98 | async_answer_0(callid, rc);
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | rc = dns_name2host(name, &hinfo);
|
---|
| 103 | if (rc != EOK) {
|
---|
| 104 | async_answer_0(callid, rc);
|
---|
| 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | async_answer_1(callid, EOK, hinfo->addr.ipv4);
|
---|
| 109 |
|
---|
| 110 | dns_hostinfo_destroy(hinfo);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_callid_t callid,
|
---|
| 114 | ipc_call_t *call)
|
---|
| 115 | {
|
---|
| 116 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");
|
---|
[5d1cb8a] | 117 | async_answer_1(callid, EOK, dns_server_addr.ipv4);
|
---|
[31e9fe0] | 118 | }
|
---|
| 119 |
|
---|
| 120 | static void dnsr_set_srvaddr_srv(dnsr_client_t *client, ipc_callid_t callid,
|
---|
| 121 | ipc_call_t *call)
|
---|
| 122 | {
|
---|
| 123 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_set_srvaddr_srv()");
|
---|
| 124 |
|
---|
[5d1cb8a] | 125 | dns_server_addr.ipv4 = IPC_GET_ARG1(*call);
|
---|
[31e9fe0] | 126 |
|
---|
| 127 | async_answer_0(callid, EOK);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static void dnsr_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 131 | {
|
---|
| 132 | dnsr_client_t client;
|
---|
| 133 |
|
---|
| 134 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dnsr_conn()");
|
---|
| 135 |
|
---|
| 136 | /* Accept the connection */
|
---|
| 137 | async_answer_0(iid, EOK);
|
---|
| 138 |
|
---|
| 139 | while (true) {
|
---|
| 140 | ipc_call_t call;
|
---|
| 141 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 142 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 143 |
|
---|
| 144 | if (!method) {
|
---|
| 145 | /* The other side has hung up */
|
---|
| 146 | async_answer_0(callid, EOK);
|
---|
| 147 | return;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | switch (method) {
|
---|
| 151 | case DNSR_NAME2HOST:
|
---|
| 152 | dnsr_name2host_srv(&client, callid, &call);
|
---|
| 153 | break;
|
---|
| 154 | case DNSR_GET_SRVADDR:
|
---|
| 155 | dnsr_get_srvaddr_srv(&client, callid, &call);
|
---|
| 156 | break;
|
---|
| 157 | case DNSR_SET_SRVADDR:
|
---|
| 158 | dnsr_set_srvaddr_srv(&client, callid, &call);
|
---|
| 159 | break;
|
---|
| 160 | default:
|
---|
| 161 | async_answer_0(callid, EINVAL);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | int main(int argc, char *argv[])
|
---|
| 167 | {
|
---|
| 168 | int rc;
|
---|
| 169 |
|
---|
| 170 | printf("%s: DNS Resolution Service\n", NAME);
|
---|
| 171 |
|
---|
| 172 | if (log_init(NAME) != EOK) {
|
---|
| 173 | printf(NAME ": Failed to initialize logging.\n");
|
---|
| 174 | return 1;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | rc = dnsr_init();
|
---|
| 178 | if (rc != EOK)
|
---|
| 179 | return 1;
|
---|
| 180 |
|
---|
| 181 | printf(NAME ": Accepting connections.\n");
|
---|
| 182 | task_retval(0);
|
---|
| 183 | async_manager();
|
---|
| 184 |
|
---|
| 185 | return 0;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /** @}
|
---|
| 189 | */
|
---|