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 "qapi/qapi-types-sockets.h"
25 #include "qom/object.h"
28 #define TYPE_QIO_DNS_RESOLVER "qio-dns-resolver"
29 typedef struct QIODNSResolver QIODNSResolver
;
30 typedef struct QIODNSResolverClass QIODNSResolverClass
;
31 DECLARE_OBJ_CHECKERS(QIODNSResolver
, QIODNSResolverClass
,
32 QIO_DNS_RESOLVER
, TYPE_QIO_DNS_RESOLVER
)
38 * The QIODNSResolver class provides a framework for doing
39 * DNS resolution on SocketAddress objects, independently
43 * <title>Resolving addresses synchronously</title>
45 * int mylisten(SocketAddress *addr, Error **errp) {
46 * QIODNSResolver *resolver = qio_dns_resolver_get_instance();
47 * SocketAddress **rawaddrs = NULL;
48 * size_t nrawaddrs = 0;
50 * QIOChannel **socks = NULL;
53 * if (qio_dns_resolver_lookup_sync(dns, addr, &nrawaddrs,
54 * &rawaddrs, errp) < 0) {
58 * for (i = 0; i < nrawaddrs; i++) {
59 * QIOChannel *sock = qio_channel_new();
60 * Error *local_err = NULL;
61 * qio_channel_listen_sync(sock, rawaddrs[i], &local_err);
63 * error_propagate(&err, local_err);
65 * socks = g_renew(QIOChannelSocket *, socks, nsocks + 1);
66 * socks[nsocks++] = sock;
68 * qapi_free_SocketAddress(rawaddrs[i]);
73 * error_propagate(errp, err);
82 * <title>Resolving addresses asynchronously</title>
84 * typedef struct MyListenData {
86 * QIOChannelSocket **socks;
90 * void mylistenresult(QIOTask *task, void *opaque) {
91 * MyListenData *data = opaque;
92 * QIODNSResolver *resolver =
93 * QIO_DNS_RESOLVER(qio_task_get_source(task);
94 * SocketAddress **rawaddrs = NULL;
95 * size_t nrawaddrs = 0;
98 * if (qio_task_propagate_error(task, &data->err)) {
102 * qio_dns_resolver_lookup_result(resolver, task,
103 * &nrawaddrs, &rawaddrs);
105 * for (i = 0; i < nrawaddrs; i++) {
106 * QIOChannel *sock = qio_channel_new();
107 * Error *local_err = NULL;
108 * qio_channel_listen_sync(sock, rawaddrs[i], &local_err);
110 * error_propagate(&err, local_err);
112 * socks = g_renew(QIOChannelSocket *, socks, nsocks + 1);
113 * socks[nsocks++] = sock;
115 * qapi_free_SocketAddress(rawaddrs[i]);
120 * error_propagate(&data->err, err);
126 * void mylisten(SocketAddress *addr, MyListenData *data) {
127 * QIODNSResolver *resolver = qio_dns_resolver_get_instance();
128 * qio_dns_resolver_lookup_async(dns, addr,
129 * mylistenresult, data, NULL);
134 struct QIODNSResolver
{
138 struct QIODNSResolverClass
{
144 * qio_dns_resolver_get_instance:
146 * Get the singleton dns resolver instance. The caller
147 * does not own a reference on the returned object.
149 * Returns: the single dns resolver instance
151 QIODNSResolver
*qio_dns_resolver_get_instance(void);
154 * qio_dns_resolver_lookup_sync:
155 * @resolver: the DNS resolver instance
156 * @addr: the address to resolve
157 * @naddr: pointer to hold number of resolved addresses
158 * @addrs: pointer to hold resolved addresses
159 * @errp: pointer to NULL initialized error object
161 * This will attempt to resolve the address provided
162 * in @addr. If resolution succeeds, @addrs will be filled
163 * with all the resolved addresses. @naddrs will specify
164 * the number of entries allocated in @addrs. The caller
165 * is responsible for freeing each entry in @addrs, as
166 * well as @addrs itself. @naddrs is guaranteed to be
167 * greater than zero on success.
169 * DNS resolution will be done synchronously so execution
170 * of the caller may be blocked for an arbitrary length
173 * Returns: 0 if resolution was successful, -1 on error
175 int qio_dns_resolver_lookup_sync(QIODNSResolver
*resolver
,
178 SocketAddress
***addrs
,
182 * qio_dns_resolver_lookup_async:
183 * @resolver: the DNS resolver instance
184 * @addr: the address to resolve
185 * @func: the callback to invoke on lookup completion
186 * @opaque: data blob to pass to @func
187 * @notify: the callback to free @opaque, or NULL
189 * This will attempt to resolve the address provided
190 * in @addr. The callback @func will be invoked when
191 * resolution has either completed or failed. On
192 * success, the @func should call the method
193 * qio_dns_resolver_lookup_result() to obtain the
196 * DNS resolution will be done asynchronously so execution
197 * of the caller will not be blocked.
199 void qio_dns_resolver_lookup_async(QIODNSResolver
*resolver
,
203 GDestroyNotify notify
);
206 * qio_dns_resolver_lookup_result:
207 * @resolver: the DNS resolver instance
208 * @task: the task object to get results for
209 * @naddr: pointer to hold number of resolved addresses
210 * @addrs: pointer to hold resolved addresses
212 * This method should be called from the callback passed
213 * to qio_dns_resolver_lookup_async() in order to obtain
214 * results. @addrs will be filled with all the resolved
215 * addresses. @naddrs will specify the number of entries
216 * allocated in @addrs. The caller is responsible for
217 * freeing each entry in @addrs, as well as @addrs itself.
219 void qio_dns_resolver_lookup_result(QIODNSResolver
*resolver
,
222 SocketAddress
***addrs
);
224 #endif /* QIO_DNS_RESOLVER_H */