source: mainline/uspace/lib/c/generic/dnsr.c@ ab6edb6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ab6edb6 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[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]40static FIBRIL_MUTEX_INITIALIZE(dnsr_sess_mutex);
41
[31e9fe0]42static async_sess_t *dnsr_sess = NULL;
43
[fff7ef4]44static async_exch_t *dnsr_exchange_begin(void)
[31e9fe0]45{
[fff7ef4]46 fibril_mutex_lock(&dnsr_sess_mutex);
[a35b458]47
[fff7ef4]48 if (dnsr_sess == NULL) {
[3e66428]49 service_id_t dnsr_svc;
[a35b458]50
[fff7ef4]51 (void) loc_service_get_id(SERVICE_NAME_DNSR, &dnsr_svc,
52 IPC_FLAG_BLOCKING);
[a35b458]53
[f9b2cb4c]54 dnsr_sess = loc_service_connect(dnsr_svc, INTERFACE_DNSR,
[fff7ef4]55 IPC_FLAG_BLOCKING);
56 }
[a35b458]57
[3e66428]58 async_sess_t *sess = dnsr_sess;
[fff7ef4]59 fibril_mutex_unlock(&dnsr_sess_mutex);
[a35b458]60
[fff7ef4]61 return async_exchange_begin(sess);
62}
63
64static void dnsr_exchange_end(async_exch_t *exch)
65{
66 async_exchange_end(exch);
[31e9fe0]67}
68
[b7fd2a0]69errno_t dnsr_name2host(const char *name, dnsr_hostinfo_t **rinfo, ip_ver_t ver)
[31e9fe0]70{
[fff7ef4]71 async_exch_t *exch = dnsr_exchange_begin();
[a35b458]72
[31e9fe0]73 ipc_call_t answer;
[e948fde]74 aid_t req = async_send_1(exch, DNSR_NAME2HOST, (sysarg_t) ver,
[0aa70f4]75 &answer);
[a35b458]76
[b7fd2a0]77 errno_t rc = async_data_write_start(exch, name, str_size(name));
[02a09ed]78 if (rc != EOK) {
79 async_exchange_end(exch);
80 async_forget(req);
81 return rc;
82 }
[401840b]83
84 dnsr_hostinfo_t *info = calloc(1, sizeof(dnsr_hostinfo_t));
85 if (info == NULL)
86 return ENOMEM;
[a35b458]87
[02a09ed]88 ipc_call_t answer_addr;
89 aid_t req_addr = async_data_read(exch, &info->addr,
90 sizeof(inet_addr_t), &answer_addr);
[a35b458]91
[b7fd2a0]92 errno_t retval_addr;
[02a09ed]93 async_wait_for(req_addr, &retval_addr);
[a35b458]94
[02a09ed]95 if (retval_addr != EOK) {
96 async_exchange_end(exch);
97 async_forget(req);
[401840b]98 free(info);
[25a179e]99 return retval_addr;
[02a09ed]100 }
[a35b458]101
[02a09ed]102 ipc_call_t answer_cname;
103 char cname_buf[DNSR_NAME_MAX_SIZE + 1];
104 aid_t req_cname = async_data_read(exch, cname_buf, DNSR_NAME_MAX_SIZE,
105 &answer_cname);
[a35b458]106
[fff7ef4]107 dnsr_exchange_end(exch);
[a35b458]108
[b7fd2a0]109 errno_t retval_cname;
[02a09ed]110 async_wait_for(req_cname, &retval_cname);
[a35b458]111
[02a09ed]112 if (retval_cname != EOK) {
[31e9fe0]113 async_forget(req);
[401840b]114 free(info);
[25a179e]115 return retval_cname;
[31e9fe0]116 }
[a35b458]117
[b7fd2a0]118 errno_t retval;
[31e9fe0]119 async_wait_for(req, &retval);
[a35b458]120
[401840b]121 if (retval != EOK) {
122 async_forget(req);
123 free(info);
[25a179e]124 return retval;
[401840b]125 }
[a35b458]126
[02a09ed]127 size_t act_size = IPC_GET_ARG2(answer_cname);
[959d2ec]128 assert(act_size <= DNSR_NAME_MAX_SIZE);
[a35b458]129
[959d2ec]130 cname_buf[act_size] = '\0';
[a35b458]131
[959d2ec]132 info->cname = str_dup(cname_buf);
[a35b458]133
[554e70f]134 if (info->cname == NULL) {
135 free(info);
136 return ENOMEM;
137 }
138
[31e9fe0]139 *rinfo = info;
140 return EOK;
141}
142
143void dnsr_hostinfo_destroy(dnsr_hostinfo_t *info)
144{
[c55cbbf]145 if (info == NULL)
146 return;
[a35b458]147
[959d2ec]148 free(info->cname);
[31e9fe0]149 free(info);
150}
151
[b7fd2a0]152errno_t dnsr_get_srvaddr(inet_addr_t *srvaddr)
[31e9fe0]153{
[fff7ef4]154 async_exch_t *exch = dnsr_exchange_begin();
[a35b458]155
[3e66428]156 ipc_call_t answer;
157 aid_t req = async_send_0(exch, DNSR_GET_SRVADDR, &answer);
[b7fd2a0]158 errno_t rc = async_data_read_start(exch, srvaddr, sizeof(inet_addr_t));
[a35b458]159
[3e66428]160 loc_exchange_end(exch);
[a35b458]161
[3e66428]162 if (rc != EOK) {
163 async_forget(req);
[31e9fe0]164 return rc;
[3e66428]165 }
[a35b458]166
[b7fd2a0]167 errno_t retval;
[3e66428]168 async_wait_for(req, &retval);
[a35b458]169
[25a179e]170 return retval;
[31e9fe0]171}
172
[b7fd2a0]173errno_t dnsr_set_srvaddr(inet_addr_t *srvaddr)
[31e9fe0]174{
[fff7ef4]175 async_exch_t *exch = dnsr_exchange_begin();
[a35b458]176
[3e66428]177 ipc_call_t answer;
178 aid_t req = async_send_0(exch, DNSR_SET_SRVADDR, &answer);
[b7fd2a0]179 errno_t rc = async_data_write_start(exch, srvaddr, sizeof(inet_addr_t));
[a35b458]180
[3e66428]181 loc_exchange_end(exch);
[a35b458]182
[3e66428]183 if (rc != EOK) {
184 async_forget(req);
[31e9fe0]185 return rc;
[3e66428]186 }
[a35b458]187
[b7fd2a0]188 errno_t retval;
[3e66428]189 async_wait_for(req, &retval);
[a35b458]190
[25a179e]191 return retval;
[31e9fe0]192}
193
194/** @}
195 */
Note: See TracBrowser for help on using the repository browser.