4 * Copyright (c) 2016 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 #include "qemu/osdep.h"
22 #include "io/dns-resolver.h"
23 #include "qapi/clone-visitor.h"
24 #include "qemu/sockets.h"
25 #include "qapi/error.h"
26 #include "qemu/cutils.h"
28 #ifndef AI_NUMERICSERV
29 # define AI_NUMERICSERV 0
32 static QIODNSResolver
*instance
;
33 static GOnce instance_init
= G_ONCE_INIT
;
35 static gpointer
qio_dns_resolve_init_instance(gpointer unused G_GNUC_UNUSED
)
37 instance
= QIO_DNS_RESOLVER(object_new(TYPE_QIO_DNS_RESOLVER
));
41 QIODNSResolver
*qio_dns_resolver_get_instance(void)
43 g_once(&instance_init
, qio_dns_resolve_init_instance
, NULL
);
47 static int qio_dns_resolver_lookup_sync_inet(QIODNSResolver
*resolver
,
50 SocketAddress
***addrs
,
53 struct addrinfo ai
, *res
, *e
;
54 InetSocketAddress
*iaddr
= &addr
->u
.inet
;
56 char uaddr
[INET6_ADDRSTRLEN
+ 1];
65 memset(&ai
, 0, sizeof(ai
));
66 ai
.ai_flags
= AI_PASSIVE
;
67 if (iaddr
->has_numeric
&& iaddr
->numeric
) {
68 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
70 ai
.ai_family
= inet_ai_family_from_address(iaddr
, &err
);
71 ai
.ai_socktype
= SOCK_STREAM
;
74 error_propagate(errp
, err
);
78 if (iaddr
->host
== NULL
) {
79 error_setg(errp
, "host not specified");
82 if (iaddr
->port
!= NULL
) {
83 pstrcpy(port
, sizeof(port
), iaddr
->port
);
88 rc
= getaddrinfo(strlen(iaddr
->host
) ? iaddr
->host
: NULL
,
89 strlen(port
) ? port
: NULL
, &ai
, &res
);
91 error_setg(errp
, "address resolution failed for %s:%s: %s",
92 iaddr
->host
, port
, gai_strerror(rc
));
96 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
100 *addrs
= g_new0(SocketAddress
*, *naddrs
);
102 /* create socket + bind */
103 for (i
= 0, e
= res
; e
!= NULL
; i
++, e
= e
->ai_next
) {
104 SocketAddress
*newaddr
= g_new0(SocketAddress
, 1);
106 newaddr
->type
= SOCKET_ADDRESS_TYPE_INET
;
108 getnameinfo((struct sockaddr
*)e
->ai_addr
, e
->ai_addrlen
,
109 uaddr
, INET6_ADDRSTRLEN
, uport
, 32,
110 NI_NUMERICHOST
| NI_NUMERICSERV
);
112 newaddr
->u
.inet
= (InetSocketAddress
){
113 .host
= g_strdup(uaddr
),
114 .port
= g_strdup(uport
),
117 .has_to
= iaddr
->has_to
,
119 .has_ipv4
= iaddr
->has_ipv4
,
121 .has_ipv6
= iaddr
->has_ipv6
,
125 (*addrs
)[i
] = newaddr
;
132 static int qio_dns_resolver_lookup_sync_nop(QIODNSResolver
*resolver
,
135 SocketAddress
***addrs
,
139 *addrs
= g_new0(SocketAddress
*, 1);
140 (*addrs
)[0] = QAPI_CLONE(SocketAddress
, addr
);
146 int qio_dns_resolver_lookup_sync(QIODNSResolver
*resolver
,
149 SocketAddress
***addrs
,
152 switch (addr
->type
) {
153 case SOCKET_ADDRESS_TYPE_INET
:
154 return qio_dns_resolver_lookup_sync_inet(resolver
,
160 case SOCKET_ADDRESS_TYPE_UNIX
:
161 case SOCKET_ADDRESS_TYPE_VSOCK
:
162 case SOCKET_ADDRESS_TYPE_FD
:
163 return qio_dns_resolver_lookup_sync_nop(resolver
,
175 struct QIODNSResolverLookupData
{
177 SocketAddress
**addrs
;
182 static void qio_dns_resolver_lookup_data_free(gpointer opaque
)
184 struct QIODNSResolverLookupData
*data
= opaque
;
187 qapi_free_SocketAddress(data
->addr
);
188 for (i
= 0; i
< data
->naddrs
; i
++) {
189 qapi_free_SocketAddress(data
->addrs
[i
]);
197 static void qio_dns_resolver_lookup_worker(QIOTask
*task
,
200 QIODNSResolver
*resolver
= QIO_DNS_RESOLVER(qio_task_get_source(task
));
201 struct QIODNSResolverLookupData
*data
= opaque
;
204 qio_dns_resolver_lookup_sync(resolver
,
210 qio_task_set_error(task
, err
);
212 qio_task_set_result_pointer(task
, opaque
, NULL
);
215 object_unref(OBJECT(resolver
));
219 void qio_dns_resolver_lookup_async(QIODNSResolver
*resolver
,
223 GDestroyNotify notify
)
226 struct QIODNSResolverLookupData
*data
=
227 g_new0(struct QIODNSResolverLookupData
, 1);
229 data
->addr
= QAPI_CLONE(SocketAddress
, addr
);
231 task
= qio_task_new(OBJECT(resolver
), func
, opaque
, notify
);
233 qio_task_run_in_thread(task
,
234 qio_dns_resolver_lookup_worker
,
236 qio_dns_resolver_lookup_data_free
);
240 void qio_dns_resolver_lookup_result(QIODNSResolver
*resolver
,
243 SocketAddress
***addrs
)
245 struct QIODNSResolverLookupData
*data
=
246 qio_task_get_result_pointer(task
);
255 *naddrs
= data
->naddrs
;
256 *addrs
= g_new0(SocketAddress
*, data
->naddrs
);
257 for (i
= 0; i
< data
->naddrs
; i
++) {
258 (*addrs
)[i
] = QAPI_CLONE(SocketAddress
, data
->addrs
[i
]);
263 static const TypeInfo qio_dns_resolver_info
= {
264 .parent
= TYPE_OBJECT
,
265 .name
= TYPE_QIO_DNS_RESOLVER
,
266 .instance_size
= sizeof(QIODNSResolver
),
267 .class_size
= sizeof(QIODNSResolverClass
),
271 static void qio_dns_resolver_register_types(void)
273 type_register_static(&qio_dns_resolver_info
);
277 type_init(qio_dns_resolver_register_types
);