4 * Copyright (c) 2016-2017 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #ifndef QIO_DNS_RESOLVER_H
22 #define QIO_DNS_RESOLVER_H
24 #include "qemu-common.h"
25 #include "qom/object.h"
28 #define TYPE_QIO_DNS_RESOLVER "qio-dns-resolver"
29 #define QIO_DNS_RESOLVER(obj) \
30 OBJECT_CHECK(QIODNSResolver, (obj), TYPE_QIO_DNS_RESOLVER)
31 #define QIO_DNS_RESOLVER_CLASS(klass) \
32 OBJECT_CLASS_CHECK(QIODNSResolverClass, klass, TYPE_QIO_DNS_RESOLVER)
33 #define QIO_DNS_RESOLVER_GET_CLASS(obj) \
34 OBJECT_GET_CLASS(QIODNSResolverClass, obj, TYPE_QIO_DNS_RESOLVER)
36 typedef struct QIODNSResolver QIODNSResolver
;
37 typedef struct QIODNSResolverClass QIODNSResolverClass
;
42 * The QIODNSResolver class provides a framework for doing
43 * DNS resolution on SocketAddress objects, independently
47 * <title>Resolving addresses synchronously</title>
49 * int mylisten(SocketAddress *addr, Error **errp) {
50 * QIODNSResolver *resolver = qio_dns_resolver_get_instance();
51 * SocketAddress **rawaddrs = NULL;
52 * size_t nrawaddrs = 0;
54 * QIOChannel **socks = NULL;
57 * if (qio_dns_resolver_lookup_sync(dns, addr, &nrawaddrs,
58 * &rawaddrs, errp) < 0) {
62 * for (i = 0; i < nrawaddrs; i++) {
63 * QIOChannel *sock = qio_channel_new();
64 * Error *local_err = NULL;
65 * qio_channel_listen_sync(sock, rawaddrs[i], &local_err);
67 * error_propagate(&err, local_err);
69 * socks = g_renew(QIOChannelSocket *, socks, nsocks + 1);
70 * socks[nsocks++] = sock;
72 * qapi_free_SocketAddress(rawaddrs[i]);
77 * error_propagate(errp, err);
86 * <title>Resolving addresses asynchronously</title>
88 * typedef struct MyListenData {
90 * QIOChannelSocket **socks;
94 * void mylistenresult(QIOTask *task, void *opaque) {
95 * MyListenData *data = opaque;
96 * QIODNSResolver *resolver =
97 * QIO_DNS_RESOLVER(qio_task_get_source(task);
98 * SocketAddress **rawaddrs = NULL;
99 * size_t nrawaddrs = 0;
102 * if (qio_task_propagate_error(task, &data->err)) {
106 * qio_dns_resolver_lookup_result(resolver, task,
107 * &nrawaddrs, &rawaddrs);
109 * for (i = 0; i < nrawaddrs; i++) {
110 * QIOChannel *sock = qio_channel_new();
111 * Error *local_err = NULL;
112 * qio_channel_listen_sync(sock, rawaddrs[i], &local_err);
114 * error_propagate(&err, local_err);
116 * socks = g_renew(QIOChannelSocket *, socks, nsocks + 1);
117 * socks[nsocks++] = sock;
119 * qapi_free_SocketAddress(rawaddrs[i]);
124 * error_propagate(&data->err, err);
130 * void mylisten(SocketAddress *addr, MyListenData *data) {
131 * QIODNSResolver *resolver = qio_dns_resolver_get_instance();
132 * qio_dns_resolver_lookup_async(dns, addr,
133 * mylistenresult, data, NULL);
138 struct QIODNSResolver
{
142 struct QIODNSResolverClass
{
148 * qio_dns_resolver_get_instance:
150 * Get the singleton dns resolver instance. The caller
151 * does not own a reference on the returned object.
153 * Returns: the single dns resolver instance
155 QIODNSResolver
*qio_dns_resolver_get_instance(void);
158 * qio_dns_resolver_lookup_sync:
159 * @resolver: the DNS resolver instance
160 * @addr: the address to resolve
161 * @naddr: pointer to hold number of resolved addresses
162 * @addrs: pointer to hold resolved addresses
163 * @errp: pointer to NULL initialized error object
165 * This will attempt to resolve the address provided
166 * in @addr. If resolution succeeds, @addrs will be filled
167 * with all the resolved addresses. @naddrs will specify
168 * the number of entries allocated in @addrs. The caller
169 * is responsible for freeing each entry in @addrs, as
170 * well as @addrs itself. @naddrs is guaranteed to be
171 * greater than zero on success.
173 * DNS resolution will be done synchronously so execution
174 * of the caller may be blocked for an arbitrary length
177 * Returns: 0 if resolution was successful, -1 on error
179 int qio_dns_resolver_lookup_sync(QIODNSResolver
*resolver
,
182 SocketAddress
***addrs
,
186 * qio_dns_resolver_lookup_async:
187 * @resolver: the DNS resolver instance
188 * @addr: the address to resolve
189 * @func: the callback to invoke on lookup completion
190 * @opaque: data blob to pass to @func
191 * @notify: the callback to free @opaque, or NULL
193 * This will attempt to resolve the address provided
194 * in @addr. The callback @func will be invoked when
195 * resolution has either completed or failed. On
196 * success, the @func should call the method
197 * qio_dns_resolver_lookup_result() to obtain the
200 * DNS resolution will be done asynchronously so execution
201 * of the caller will not be blocked.
203 void qio_dns_resolver_lookup_async(QIODNSResolver
*resolver
,
207 GDestroyNotify notify
);
210 * qio_dns_resolver_lookup_result:
211 * @resolver: the DNS resolver instance
212 * @task: the task object to get results for
213 * @naddr: pointer to hold number of resolved addresses
214 * @addrs: pointer to hold resolved addresses
216 * This method should be called from the callback passed
217 * to qio_dns_resolver_lookup_async() in order to obtain
218 * results. @addrs will be filled with all the resolved
219 * addresses. @naddrs will specify the number of entries
220 * allocated in @addrs. The caller is responsible for
221 * freeing each entry in @addrs, as well as @addrs itself.
223 void qio_dns_resolver_lookup_result(QIODNSResolver
*resolver
,
226 SocketAddress
***addrs
);
228 #endif /* QIO_DNS_RESOLVER_H */