[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 | fibril_mutex_lock(&dnsr_sess_mutex);
|
---|
[3e66428] | 47 |
|
---|
[fff7ef4] | 48 | if (dnsr_sess == NULL) {
|
---|
[3e66428] | 49 | service_id_t dnsr_svc;
|
---|
| 50 |
|
---|
[fff7ef4] | 51 | (void) loc_service_get_id(SERVICE_NAME_DNSR, &dnsr_svc,
|
---|
| 52 | IPC_FLAG_BLOCKING);
|
---|
[3e66428] | 53 |
|
---|
[fff7ef4] | 54 | dnsr_sess = loc_service_connect(EXCHANGE_SERIALIZE, dnsr_svc,
|
---|
| 55 | IPC_FLAG_BLOCKING);
|
---|
| 56 | }
|
---|
[3e66428] | 57 |
|
---|
| 58 | async_sess_t *sess = dnsr_sess;
|
---|
[fff7ef4] | 59 | fibril_mutex_unlock(&dnsr_sess_mutex);
|
---|
[3e66428] | 60 |
|
---|
[fff7ef4] | 61 | return async_exchange_begin(sess);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | static void dnsr_exchange_end(async_exch_t *exch)
|
---|
| 65 | {
|
---|
| 66 | async_exchange_end(exch);
|
---|
[31e9fe0] | 67 | }
|
---|
| 68 |
|
---|
[287d729] | 69 | int dnsr_name2host(const char *name, dnsr_hostinfo_t **rinfo)
|
---|
[31e9fe0] | 70 | {
|
---|
[02a09ed] | 71 | dnsr_hostinfo_t *info = calloc(1, sizeof(dnsr_hostinfo_t));
|
---|
| 72 | if (info == NULL)
|
---|
| 73 | return ENOMEM;
|
---|
| 74 |
|
---|
[fff7ef4] | 75 | async_exch_t *exch = dnsr_exchange_begin();
|
---|
[02a09ed] | 76 |
|
---|
[31e9fe0] | 77 | ipc_call_t answer;
|
---|
| 78 | aid_t req = async_send_0(exch, DNSR_NAME2HOST, &answer);
|
---|
[02a09ed] | 79 |
|
---|
| 80 | int rc = async_data_write_start(exch, name, str_size(name));
|
---|
| 81 | if (rc != EOK) {
|
---|
| 82 | async_exchange_end(exch);
|
---|
| 83 | async_forget(req);
|
---|
| 84 | return rc;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | ipc_call_t answer_addr;
|
---|
| 88 | aid_t req_addr = async_data_read(exch, &info->addr,
|
---|
| 89 | sizeof(inet_addr_t), &answer_addr);
|
---|
| 90 |
|
---|
| 91 | sysarg_t retval_addr;
|
---|
| 92 | async_wait_for(req_addr, &retval_addr);
|
---|
| 93 |
|
---|
| 94 | if (retval_addr != EOK) {
|
---|
| 95 | async_exchange_end(exch);
|
---|
| 96 | async_forget(req);
|
---|
| 97 | return (int) retval_addr;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | ipc_call_t answer_cname;
|
---|
| 101 | char cname_buf[DNSR_NAME_MAX_SIZE + 1];
|
---|
| 102 | aid_t req_cname = async_data_read(exch, cname_buf, DNSR_NAME_MAX_SIZE,
|
---|
| 103 | &answer_cname);
|
---|
| 104 |
|
---|
[fff7ef4] | 105 | dnsr_exchange_end(exch);
|
---|
[02a09ed] | 106 |
|
---|
| 107 | sysarg_t retval_cname;
|
---|
| 108 | async_wait_for(req_cname, &retval_cname);
|
---|
| 109 |
|
---|
| 110 | if (retval_cname != EOK) {
|
---|
[31e9fe0] | 111 | async_forget(req);
|
---|
[02a09ed] | 112 | return (int) retval_cname;
|
---|
[31e9fe0] | 113 | }
|
---|
[02a09ed] | 114 |
|
---|
| 115 | sysarg_t retval;
|
---|
[31e9fe0] | 116 | async_wait_for(req, &retval);
|
---|
[02a09ed] | 117 |
|
---|
[31e9fe0] | 118 | if (retval != EOK)
|
---|
[02a09ed] | 119 | return (int) retval;
|
---|
| 120 |
|
---|
| 121 | size_t act_size = IPC_GET_ARG2(answer_cname);
|
---|
[959d2ec] | 122 | assert(act_size <= DNSR_NAME_MAX_SIZE);
|
---|
[02a09ed] | 123 |
|
---|
[959d2ec] | 124 | cname_buf[act_size] = '\0';
|
---|
[02a09ed] | 125 |
|
---|
[959d2ec] | 126 | info->cname = str_dup(cname_buf);
|
---|
[02a09ed] | 127 |
|
---|
[31e9fe0] | 128 | *rinfo = info;
|
---|
| 129 | return EOK;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void dnsr_hostinfo_destroy(dnsr_hostinfo_t *info)
|
---|
| 133 | {
|
---|
[c55cbbf] | 134 | if (info == NULL)
|
---|
| 135 | return;
|
---|
[3e66428] | 136 |
|
---|
[959d2ec] | 137 | free(info->cname);
|
---|
[31e9fe0] | 138 | free(info);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[a2e3ee6] | 141 | int dnsr_get_srvaddr(inet_addr_t *srvaddr)
|
---|
[31e9fe0] | 142 | {
|
---|
[fff7ef4] | 143 | async_exch_t *exch = dnsr_exchange_begin();
|
---|
[3e66428] | 144 |
|
---|
| 145 | ipc_call_t answer;
|
---|
| 146 | aid_t req = async_send_0(exch, DNSR_GET_SRVADDR, &answer);
|
---|
[a2e3ee6] | 147 | int rc = async_data_read_start(exch, srvaddr, sizeof(inet_addr_t));
|
---|
[3e66428] | 148 |
|
---|
| 149 | loc_exchange_end(exch);
|
---|
| 150 |
|
---|
| 151 | if (rc != EOK) {
|
---|
| 152 | async_forget(req);
|
---|
[31e9fe0] | 153 | return rc;
|
---|
[3e66428] | 154 | }
|
---|
| 155 |
|
---|
| 156 | sysarg_t retval;
|
---|
| 157 | async_wait_for(req, &retval);
|
---|
| 158 |
|
---|
| 159 | return (int) retval;
|
---|
[31e9fe0] | 160 | }
|
---|
| 161 |
|
---|
[a2e3ee6] | 162 | int dnsr_set_srvaddr(inet_addr_t *srvaddr)
|
---|
[31e9fe0] | 163 | {
|
---|
[fff7ef4] | 164 | async_exch_t *exch = dnsr_exchange_begin();
|
---|
[3e66428] | 165 |
|
---|
| 166 | ipc_call_t answer;
|
---|
| 167 | aid_t req = async_send_0(exch, DNSR_SET_SRVADDR, &answer);
|
---|
[a2e3ee6] | 168 | int rc = async_data_write_start(exch, srvaddr, sizeof(inet_addr_t));
|
---|
[3e66428] | 169 |
|
---|
| 170 | loc_exchange_end(exch);
|
---|
| 171 |
|
---|
| 172 | if (rc != EOK) {
|
---|
| 173 | async_forget(req);
|
---|
[31e9fe0] | 174 | return rc;
|
---|
[3e66428] | 175 | }
|
---|
| 176 |
|
---|
| 177 | sysarg_t retval;
|
---|
| 178 | async_wait_for(req, &retval);
|
---|
| 179 |
|
---|
| 180 | return (int) retval;
|
---|
[31e9fe0] | 181 | }
|
---|
| 182 |
|
---|
| 183 | /** @}
|
---|
| 184 | */
|
---|