2 * Copyright (c) 2013 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 #include <fibril_synch.h>
33 #include <inet/dnsr.h>
35 #include <ipc/services.h>
40 static FIBRIL_MUTEX_INITIALIZE(dnsr_sess_mutex
);
42 static async_sess_t
*dnsr_sess
= NULL
;
44 static async_exch_t
*dnsr_exchange_begin(void)
46 fibril_mutex_lock(&dnsr_sess_mutex
);
48 if (dnsr_sess
== NULL
) {
49 service_id_t dnsr_svc
;
51 (void) loc_service_get_id(SERVICE_NAME_DNSR
, &dnsr_svc
,
54 dnsr_sess
= loc_service_connect(dnsr_svc
, INTERFACE_DNSR
,
58 async_sess_t
*sess
= dnsr_sess
;
59 fibril_mutex_unlock(&dnsr_sess_mutex
);
61 return async_exchange_begin(sess
);
64 static void dnsr_exchange_end(async_exch_t
*exch
)
66 async_exchange_end(exch
);
69 errno_t
dnsr_name2host(const char *name
, dnsr_hostinfo_t
**rinfo
, ip_ver_t ver
)
71 async_exch_t
*exch
= dnsr_exchange_begin();
74 aid_t req
= async_send_1(exch
, DNSR_NAME2HOST
, (sysarg_t
) ver
,
77 errno_t rc
= async_data_write_start(exch
, name
, str_size(name
));
79 async_exchange_end(exch
);
84 dnsr_hostinfo_t
*info
= calloc(1, sizeof(dnsr_hostinfo_t
));
88 ipc_call_t answer_addr
;
89 aid_t req_addr
= async_data_read(exch
, &info
->addr
,
90 sizeof(inet_addr_t
), &answer_addr
);
93 async_wait_for(req_addr
, &retval_addr
);
95 if (retval_addr
!= EOK
) {
96 async_exchange_end(exch
);
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
,
107 dnsr_exchange_end(exch
);
109 errno_t retval_cname
;
110 async_wait_for(req_cname
, &retval_cname
);
112 if (retval_cname
!= EOK
) {
119 async_wait_for(req
, &retval
);
127 size_t act_size
= IPC_GET_ARG2(answer_cname
);
128 assert(act_size
<= DNSR_NAME_MAX_SIZE
);
130 cname_buf
[act_size
] = '\0';
132 info
->cname
= str_dup(cname_buf
);
134 if (info
->cname
== NULL
) {
143 void dnsr_hostinfo_destroy(dnsr_hostinfo_t
*info
)
152 errno_t
dnsr_get_srvaddr(inet_addr_t
*srvaddr
)
154 async_exch_t
*exch
= dnsr_exchange_begin();
157 aid_t req
= async_send_0(exch
, DNSR_GET_SRVADDR
, &answer
);
158 errno_t rc
= async_data_read_start(exch
, srvaddr
, sizeof(inet_addr_t
));
160 loc_exchange_end(exch
);
168 async_wait_for(req
, &retval
);
173 errno_t
dnsr_set_srvaddr(inet_addr_t
*srvaddr
)
175 async_exch_t
*exch
= dnsr_exchange_begin();
178 aid_t req
= async_send_0(exch
, DNSR_SET_SRVADDR
, &answer
);
179 errno_t rc
= async_data_write_start(exch
, srvaddr
, sizeof(inet_addr_t
));
181 loc_exchange_end(exch
);
189 async_wait_for(req
, &retval
);