source: mainline/uspace/lib/c/generic/dnsr.c@ 0aa70f4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0aa70f4 was 0aa70f4, checked in by Martin Decky <martin@…>, 12 years ago

support for IPv6 DNS name resolution (AAAA)
if the desired address family of the DNS query is not explicitly specified, then IPv6 addresses take precendece over IPv4 addresses

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