[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 | #include <async.h>
|
---|
| 30 | #include <assert.h>
|
---|
| 31 | #include <errno.h>
|
---|
[fff7ef4] | 32 | #include <fibril_synch.h>
|
---|
[31e9fe0] | 33 | #include <inet/dnsr.h>
|
---|
| 34 | #include <ipc/dnsr.h>
|
---|
| 35 | #include <ipc/services.h>
|
---|
| 36 | #include <loc.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include <str.h>
|
---|
| 39 |
|
---|
[fff7ef4] | 40 | static FIBRIL_MUTEX_INITIALIZE(dnsr_sess_mutex);
|
---|
| 41 |
|
---|
[31e9fe0] | 42 | static async_sess_t *dnsr_sess = NULL;
|
---|
| 43 |
|
---|
[fff7ef4] | 44 | static async_exch_t *dnsr_exchange_begin(void)
|
---|
[31e9fe0] | 45 | {
|
---|
[fff7ef4] | 46 | async_sess_t *sess;
|
---|
[31e9fe0] | 47 | service_id_t dnsr_svc;
|
---|
| 48 |
|
---|
[fff7ef4] | 49 | fibril_mutex_lock(&dnsr_sess_mutex);
|
---|
[31e9fe0] | 50 |
|
---|
[fff7ef4] | 51 | if (dnsr_sess == NULL) {
|
---|
| 52 | (void) loc_service_get_id(SERVICE_NAME_DNSR, &dnsr_svc,
|
---|
| 53 | IPC_FLAG_BLOCKING);
|
---|
| 54 |
|
---|
| 55 | dnsr_sess = loc_service_connect(EXCHANGE_SERIALIZE, dnsr_svc,
|
---|
| 56 | IPC_FLAG_BLOCKING);
|
---|
| 57 | }
|
---|
[31e9fe0] | 58 |
|
---|
[fff7ef4] | 59 | sess = dnsr_sess;
|
---|
| 60 | fibril_mutex_unlock(&dnsr_sess_mutex);
|
---|
[31e9fe0] | 61 |
|
---|
[fff7ef4] | 62 | return async_exchange_begin(sess);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | static void dnsr_exchange_end(async_exch_t *exch)
|
---|
| 66 | {
|
---|
| 67 | async_exchange_end(exch);
|
---|
[31e9fe0] | 68 | }
|
---|
| 69 |
|
---|
[287d729] | 70 | int dnsr_name2host(const char *name, dnsr_hostinfo_t **rinfo)
|
---|
[31e9fe0] | 71 | {
|
---|
[fff7ef4] | 72 | async_exch_t *exch = dnsr_exchange_begin();
|
---|
[959d2ec] | 73 | char cname_buf[DNSR_NAME_MAX_SIZE + 1];
|
---|
| 74 | ipc_call_t cnreply;
|
---|
| 75 | size_t act_size;
|
---|
[31e9fe0] | 76 | dnsr_hostinfo_t *info;
|
---|
| 77 |
|
---|
| 78 | ipc_call_t answer;
|
---|
| 79 | aid_t req = async_send_0(exch, DNSR_NAME2HOST, &answer);
|
---|
| 80 | sysarg_t retval = async_data_write_start(exch, name, str_size(name));
|
---|
[959d2ec] | 81 | aid_t cnreq = async_data_read(exch, cname_buf, DNSR_NAME_MAX_SIZE,
|
---|
| 82 | &cnreply);
|
---|
[31e9fe0] | 83 |
|
---|
[fff7ef4] | 84 | dnsr_exchange_end(exch);
|
---|
[31e9fe0] | 85 |
|
---|
| 86 | if (retval != EOK) {
|
---|
| 87 | async_forget(req);
|
---|
[959d2ec] | 88 | async_forget(cnreq);
|
---|
[31e9fe0] | 89 | return retval;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | async_wait_for(req, &retval);
|
---|
[959d2ec] | 93 | if (retval != EOK) {
|
---|
| 94 | async_forget(cnreq);
|
---|
| 95 | return EIO;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | async_wait_for(cnreq, &retval);
|
---|
[31e9fe0] | 99 | if (retval != EOK)
|
---|
| 100 | return EIO;
|
---|
| 101 |
|
---|
| 102 | info = calloc(1, sizeof(dnsr_hostinfo_t));
|
---|
| 103 | if (info == NULL)
|
---|
| 104 | return ENOMEM;
|
---|
| 105 |
|
---|
[959d2ec] | 106 | act_size = IPC_GET_ARG2(cnreply);
|
---|
| 107 | assert(act_size <= DNSR_NAME_MAX_SIZE);
|
---|
| 108 | cname_buf[act_size] = '\0';
|
---|
| 109 |
|
---|
| 110 | info->cname = str_dup(cname_buf);
|
---|
[31e9fe0] | 111 | info->addr.ipv4 = IPC_GET_ARG1(answer);
|
---|
| 112 |
|
---|
| 113 | *rinfo = info;
|
---|
| 114 | return EOK;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | void dnsr_hostinfo_destroy(dnsr_hostinfo_t *info)
|
---|
| 118 | {
|
---|
[c55cbbf] | 119 | if (info == NULL)
|
---|
| 120 | return;
|
---|
| 121 |
|
---|
[959d2ec] | 122 | free(info->cname);
|
---|
[31e9fe0] | 123 | free(info);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | int dnsr_get_srvaddr(inet_addr_t *srvaddr)
|
---|
| 127 | {
|
---|
| 128 | sysarg_t addr;
|
---|
[fff7ef4] | 129 | async_exch_t *exch = dnsr_exchange_begin();
|
---|
[31e9fe0] | 130 |
|
---|
| 131 | int rc = async_req_0_1(exch, DNSR_GET_SRVADDR, &addr);
|
---|
[fff7ef4] | 132 | dnsr_exchange_end(exch);
|
---|
[31e9fe0] | 133 |
|
---|
| 134 | if (rc != EOK)
|
---|
| 135 | return rc;
|
---|
| 136 |
|
---|
| 137 | srvaddr->ipv4 = addr;
|
---|
| 138 | return EOK;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | int dnsr_set_srvaddr(inet_addr_t *srvaddr)
|
---|
| 142 | {
|
---|
[fff7ef4] | 143 | async_exch_t *exch = dnsr_exchange_begin();
|
---|
[31e9fe0] | 144 |
|
---|
| 145 | int rc = async_req_1_0(exch, DNSR_SET_SRVADDR, srvaddr->ipv4);
|
---|
[fff7ef4] | 146 | dnsr_exchange_end(exch);
|
---|
[31e9fe0] | 147 |
|
---|
| 148 | if (rc != EOK)
|
---|
| 149 | return rc;
|
---|
| 150 |
|
---|
| 151 | return EOK;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** @}
|
---|
| 155 | */
|
---|