1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.Resolver;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.InetAddress;
30 private import gio.c.functions;
31 public  import gio.c.types;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.ListG;
35 private import glib.Str;
36 private import gobject.ObjectG;
37 private import gobject.Signals;
38 public  import gtkc.giotypes;
39 private import std.algorithm;
40 
41 
42 /**
43  * #GResolver provides cancellable synchronous and asynchronous DNS
44  * resolution, for hostnames (g_resolver_lookup_by_address(),
45  * g_resolver_lookup_by_name() and their async variants) and SRV
46  * (service) records (g_resolver_lookup_service()).
47  * 
48  * #GNetworkAddress and #GNetworkService provide wrappers around
49  * #GResolver functionality that also implement #GSocketConnectable,
50  * making it easy to connect to a remote host/service.
51  */
52 public class Resolver : ObjectG
53 {
54 	/** the main Gtk struct */
55 	protected GResolver* gResolver;
56 
57 	/** Get the main Gtk struct */
58 	public GResolver* getResolverStruct(bool transferOwnership = false)
59 	{
60 		if (transferOwnership)
61 			ownedRef = false;
62 		return gResolver;
63 	}
64 
65 	/** the main Gtk struct as a void* */
66 	protected override void* getStruct()
67 	{
68 		return cast(void*)gResolver;
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GResolver* gResolver, bool ownedRef = false)
75 	{
76 		this.gResolver = gResolver;
77 		super(cast(GObject*)gResolver, ownedRef);
78 	}
79 
80 
81 	/** */
82 	public static GType getType()
83 	{
84 		return g_resolver_get_type();
85 	}
86 
87 	/**
88 	 * Frees @addresses (which should be the return value from
89 	 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
90 	 * (This is a convenience method; you can also simply free the results
91 	 * by hand.)
92 	 *
93 	 * Params:
94 	 *     addresses = a #GList of #GInetAddress
95 	 *
96 	 * Since: 2.22
97 	 */
98 	public static void freeAddresses(ListG addresses)
99 	{
100 		g_resolver_free_addresses((addresses is null) ? null : addresses.getListGStruct());
101 	}
102 
103 	/**
104 	 * Frees @targets (which should be the return value from
105 	 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
106 	 * (This is a convenience method; you can also simply free the
107 	 * results by hand.)
108 	 *
109 	 * Params:
110 	 *     targets = a #GList of #GSrvTarget
111 	 *
112 	 * Since: 2.22
113 	 */
114 	public static void freeTargets(ListG targets)
115 	{
116 		g_resolver_free_targets((targets is null) ? null : targets.getListGStruct());
117 	}
118 
119 	/**
120 	 * Gets the default #GResolver. You should unref it when you are done
121 	 * with it. #GResolver may use its reference count as a hint about how
122 	 * many threads it should allocate for concurrent DNS resolutions.
123 	 *
124 	 * Returns: the default #GResolver.
125 	 *
126 	 * Since: 2.22
127 	 */
128 	public static Resolver getDefault()
129 	{
130 		auto p = g_resolver_get_default();
131 
132 		if(p is null)
133 		{
134 			return null;
135 		}
136 
137 		return ObjectG.getDObject!(Resolver)(cast(GResolver*) p, true);
138 	}
139 
140 	/**
141 	 * Synchronously reverse-resolves @address to determine its
142 	 * associated hostname.
143 	 *
144 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to
145 	 * a value from #GResolverError.
146 	 *
147 	 * If @cancellable is non-%NULL, it can be used to cancel the
148 	 * operation, in which case @error (if non-%NULL) will be set to
149 	 * %G_IO_ERROR_CANCELLED.
150 	 *
151 	 * Params:
152 	 *     address = the address to reverse-resolve
153 	 *     cancellable = a #GCancellable, or %NULL
154 	 *
155 	 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
156 	 *     form), or %NULL on error.
157 	 *
158 	 * Since: 2.22
159 	 *
160 	 * Throws: GException on failure.
161 	 */
162 	public string lookupByAddress(InetAddress address, Cancellable cancellable)
163 	{
164 		GError* err = null;
165 
166 		auto retStr = g_resolver_lookup_by_address(gResolver, (address is null) ? null : address.getInetAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
167 
168 		if (err !is null)
169 		{
170 			throw new GException( new ErrorG(err) );
171 		}
172 
173 		scope(exit) Str.freeString(retStr);
174 		return Str.toString(retStr);
175 	}
176 
177 	/**
178 	 * Begins asynchronously reverse-resolving @address to determine its
179 	 * associated hostname, and eventually calls @callback, which must
180 	 * call g_resolver_lookup_by_address_finish() to get the final result.
181 	 *
182 	 * Params:
183 	 *     address = the address to reverse-resolve
184 	 *     cancellable = a #GCancellable, or %NULL
185 	 *     callback = callback to call after resolution completes
186 	 *     userData = data for @callback
187 	 *
188 	 * Since: 2.22
189 	 */
190 	public void lookupByAddressAsync(InetAddress address, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
191 	{
192 		g_resolver_lookup_by_address_async(gResolver, (address is null) ? null : address.getInetAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
193 	}
194 
195 	/**
196 	 * Retrieves the result of a previous call to
197 	 * g_resolver_lookup_by_address_async().
198 	 *
199 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
200 	 * a value from #GResolverError. If the operation was cancelled,
201 	 * @error will be set to %G_IO_ERROR_CANCELLED.
202 	 *
203 	 * Params:
204 	 *     result = the result passed to your #GAsyncReadyCallback
205 	 *
206 	 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
207 	 *     form), or %NULL on error.
208 	 *
209 	 * Since: 2.22
210 	 *
211 	 * Throws: GException on failure.
212 	 */
213 	public string lookupByAddressFinish(AsyncResultIF result)
214 	{
215 		GError* err = null;
216 
217 		auto retStr = g_resolver_lookup_by_address_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
218 
219 		if (err !is null)
220 		{
221 			throw new GException( new ErrorG(err) );
222 		}
223 
224 		scope(exit) Str.freeString(retStr);
225 		return Str.toString(retStr);
226 	}
227 
228 	/**
229 	 * Synchronously resolves @hostname to determine its associated IP
230 	 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
231 	 * the textual form of an IP address (in which case this just becomes
232 	 * a wrapper around g_inet_address_new_from_string()).
233 	 *
234 	 * On success, g_resolver_lookup_by_name() will return a non-empty #GList of
235 	 * #GInetAddress, sorted in order of preference and guaranteed to not
236 	 * contain duplicates. That is, if using the result to connect to
237 	 * @hostname, you should attempt to connect to the first address
238 	 * first, then the second if the first fails, etc. If you are using
239 	 * the result to listen on a socket, it is appropriate to add each
240 	 * result using e.g. g_socket_listener_add_address().
241 	 *
242 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
243 	 * value from #GResolverError and %NULL will be returned.
244 	 *
245 	 * If @cancellable is non-%NULL, it can be used to cancel the
246 	 * operation, in which case @error (if non-%NULL) will be set to
247 	 * %G_IO_ERROR_CANCELLED.
248 	 *
249 	 * If you are planning to connect to a socket on the resolved IP
250 	 * address, it may be easier to create a #GNetworkAddress and use its
251 	 * #GSocketConnectable interface.
252 	 *
253 	 * Params:
254 	 *     hostname = the hostname to look up
255 	 *     cancellable = a #GCancellable, or %NULL
256 	 *
257 	 * Returns: a non-empty #GList
258 	 *     of #GInetAddress, or %NULL on error. You
259 	 *     must unref each of the addresses and free the list when you are
260 	 *     done with it. (You can use g_resolver_free_addresses() to do this.)
261 	 *
262 	 * Since: 2.22
263 	 *
264 	 * Throws: GException on failure.
265 	 */
266 	public ListG lookupByName(string hostname, Cancellable cancellable)
267 	{
268 		GError* err = null;
269 
270 		auto p = g_resolver_lookup_by_name(gResolver, Str.toStringz(hostname), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
271 
272 		if (err !is null)
273 		{
274 			throw new GException( new ErrorG(err) );
275 		}
276 
277 		if(p is null)
278 		{
279 			return null;
280 		}
281 
282 		return new ListG(cast(GList*) p, true);
283 	}
284 
285 	/**
286 	 * Begins asynchronously resolving @hostname to determine its
287 	 * associated IP address(es), and eventually calls @callback, which
288 	 * must call g_resolver_lookup_by_name_finish() to get the result.
289 	 * See g_resolver_lookup_by_name() for more details.
290 	 *
291 	 * Params:
292 	 *     hostname = the hostname to look up the address of
293 	 *     cancellable = a #GCancellable, or %NULL
294 	 *     callback = callback to call after resolution completes
295 	 *     userData = data for @callback
296 	 *
297 	 * Since: 2.22
298 	 */
299 	public void lookupByNameAsync(string hostname, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
300 	{
301 		g_resolver_lookup_by_name_async(gResolver, Str.toStringz(hostname), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
302 	}
303 
304 	/**
305 	 * Retrieves the result of a call to
306 	 * g_resolver_lookup_by_name_async().
307 	 *
308 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
309 	 * a value from #GResolverError. If the operation was cancelled,
310 	 * @error will be set to %G_IO_ERROR_CANCELLED.
311 	 *
312 	 * Params:
313 	 *     result = the result passed to your #GAsyncReadyCallback
314 	 *
315 	 * Returns: a #GList
316 	 *     of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
317 	 *     for more details.
318 	 *
319 	 * Since: 2.22
320 	 *
321 	 * Throws: GException on failure.
322 	 */
323 	public ListG lookupByNameFinish(AsyncResultIF result)
324 	{
325 		GError* err = null;
326 
327 		auto p = g_resolver_lookup_by_name_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
328 
329 		if (err !is null)
330 		{
331 			throw new GException( new ErrorG(err) );
332 		}
333 
334 		if(p is null)
335 		{
336 			return null;
337 		}
338 
339 		return new ListG(cast(GList*) p, true);
340 	}
341 
342 	/**
343 	 * Synchronously performs a DNS record lookup for the given @rrname and returns
344 	 * a list of records as #GVariant tuples. See #GResolverRecordType for
345 	 * information on what the records contain for each @record_type.
346 	 *
347 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to
348 	 * a value from #GResolverError and %NULL will be returned.
349 	 *
350 	 * If @cancellable is non-%NULL, it can be used to cancel the
351 	 * operation, in which case @error (if non-%NULL) will be set to
352 	 * %G_IO_ERROR_CANCELLED.
353 	 *
354 	 * Params:
355 	 *     rrname = the DNS name to lookup the record for
356 	 *     recordType = the type of DNS record to lookup
357 	 *     cancellable = a #GCancellable, or %NULL
358 	 *
359 	 * Returns: a non-empty #GList of
360 	 *     #GVariant, or %NULL on error. You must free each of the records and the list
361 	 *     when you are done with it. (You can use g_list_free_full() with
362 	 *     g_variant_unref() to do this.)
363 	 *
364 	 * Since: 2.34
365 	 *
366 	 * Throws: GException on failure.
367 	 */
368 	public ListG lookupRecords(string rrname, GResolverRecordType recordType, Cancellable cancellable)
369 	{
370 		GError* err = null;
371 
372 		auto p = g_resolver_lookup_records(gResolver, Str.toStringz(rrname), recordType, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
373 
374 		if (err !is null)
375 		{
376 			throw new GException( new ErrorG(err) );
377 		}
378 
379 		if(p is null)
380 		{
381 			return null;
382 		}
383 
384 		return new ListG(cast(GList*) p, true);
385 	}
386 
387 	/**
388 	 * Begins asynchronously performing a DNS lookup for the given
389 	 * @rrname, and eventually calls @callback, which must call
390 	 * g_resolver_lookup_records_finish() to get the final result. See
391 	 * g_resolver_lookup_records() for more details.
392 	 *
393 	 * Params:
394 	 *     rrname = the DNS name to lookup the record for
395 	 *     recordType = the type of DNS record to lookup
396 	 *     cancellable = a #GCancellable, or %NULL
397 	 *     callback = callback to call after resolution completes
398 	 *     userData = data for @callback
399 	 *
400 	 * Since: 2.34
401 	 */
402 	public void lookupRecordsAsync(string rrname, GResolverRecordType recordType, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
403 	{
404 		g_resolver_lookup_records_async(gResolver, Str.toStringz(rrname), recordType, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
405 	}
406 
407 	/**
408 	 * Retrieves the result of a previous call to
409 	 * g_resolver_lookup_records_async(). Returns a non-empty list of records as
410 	 * #GVariant tuples. See #GResolverRecordType for information on what the
411 	 * records contain.
412 	 *
413 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
414 	 * a value from #GResolverError. If the operation was cancelled,
415 	 * @error will be set to %G_IO_ERROR_CANCELLED.
416 	 *
417 	 * Params:
418 	 *     result = the result passed to your #GAsyncReadyCallback
419 	 *
420 	 * Returns: a non-empty #GList of
421 	 *     #GVariant, or %NULL on error. You must free each of the records and the list
422 	 *     when you are done with it. (You can use g_list_free_full() with
423 	 *     g_variant_unref() to do this.)
424 	 *
425 	 * Since: 2.34
426 	 *
427 	 * Throws: GException on failure.
428 	 */
429 	public ListG lookupRecordsFinish(AsyncResultIF result)
430 	{
431 		GError* err = null;
432 
433 		auto p = g_resolver_lookup_records_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
434 
435 		if (err !is null)
436 		{
437 			throw new GException( new ErrorG(err) );
438 		}
439 
440 		if(p is null)
441 		{
442 			return null;
443 		}
444 
445 		return new ListG(cast(GList*) p, true);
446 	}
447 
448 	/**
449 	 * Synchronously performs a DNS SRV lookup for the given @service and
450 	 * @protocol in the given @domain and returns an array of #GSrvTarget.
451 	 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
452 	 * @service and @protocol arguments do not include the leading underscore
453 	 * that appears in the actual DNS entry.
454 	 *
455 	 * On success, g_resolver_lookup_service() will return a non-empty #GList of
456 	 * #GSrvTarget, sorted in order of preference. (That is, you should
457 	 * attempt to connect to the first target first, then the second if
458 	 * the first fails, etc.)
459 	 *
460 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to
461 	 * a value from #GResolverError and %NULL will be returned.
462 	 *
463 	 * If @cancellable is non-%NULL, it can be used to cancel the
464 	 * operation, in which case @error (if non-%NULL) will be set to
465 	 * %G_IO_ERROR_CANCELLED.
466 	 *
467 	 * If you are planning to connect to the service, it is usually easier
468 	 * to create a #GNetworkService and use its #GSocketConnectable
469 	 * interface.
470 	 *
471 	 * Params:
472 	 *     service = the service type to look up (eg, "ldap")
473 	 *     protocol = the networking protocol to use for @service (eg, "tcp")
474 	 *     domain = the DNS domain to look up the service in
475 	 *     cancellable = a #GCancellable, or %NULL
476 	 *
477 	 * Returns: a non-empty #GList of
478 	 *     #GSrvTarget, or %NULL on error. You must free each of the targets and the
479 	 *     list when you are done with it. (You can use g_resolver_free_targets() to do
480 	 *     this.)
481 	 *
482 	 * Since: 2.22
483 	 *
484 	 * Throws: GException on failure.
485 	 */
486 	public ListG lookupService(string service, string protocol, string domain, Cancellable cancellable)
487 	{
488 		GError* err = null;
489 
490 		auto p = g_resolver_lookup_service(gResolver, Str.toStringz(service), Str.toStringz(protocol), Str.toStringz(domain), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
491 
492 		if (err !is null)
493 		{
494 			throw new GException( new ErrorG(err) );
495 		}
496 
497 		if(p is null)
498 		{
499 			return null;
500 		}
501 
502 		return new ListG(cast(GList*) p, true);
503 	}
504 
505 	/**
506 	 * Begins asynchronously performing a DNS SRV lookup for the given
507 	 * @service and @protocol in the given @domain, and eventually calls
508 	 * @callback, which must call g_resolver_lookup_service_finish() to
509 	 * get the final result. See g_resolver_lookup_service() for more
510 	 * details.
511 	 *
512 	 * Params:
513 	 *     service = the service type to look up (eg, "ldap")
514 	 *     protocol = the networking protocol to use for @service (eg, "tcp")
515 	 *     domain = the DNS domain to look up the service in
516 	 *     cancellable = a #GCancellable, or %NULL
517 	 *     callback = callback to call after resolution completes
518 	 *     userData = data for @callback
519 	 *
520 	 * Since: 2.22
521 	 */
522 	public void lookupServiceAsync(string service, string protocol, string domain, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
523 	{
524 		g_resolver_lookup_service_async(gResolver, Str.toStringz(service), Str.toStringz(protocol), Str.toStringz(domain), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
525 	}
526 
527 	/**
528 	 * Retrieves the result of a previous call to
529 	 * g_resolver_lookup_service_async().
530 	 *
531 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
532 	 * a value from #GResolverError. If the operation was cancelled,
533 	 * @error will be set to %G_IO_ERROR_CANCELLED.
534 	 *
535 	 * Params:
536 	 *     result = the result passed to your #GAsyncReadyCallback
537 	 *
538 	 * Returns: a non-empty #GList of
539 	 *     #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
540 	 *     details.
541 	 *
542 	 * Since: 2.22
543 	 *
544 	 * Throws: GException on failure.
545 	 */
546 	public ListG lookupServiceFinish(AsyncResultIF result)
547 	{
548 		GError* err = null;
549 
550 		auto p = g_resolver_lookup_service_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
551 
552 		if (err !is null)
553 		{
554 			throw new GException( new ErrorG(err) );
555 		}
556 
557 		if(p is null)
558 		{
559 			return null;
560 		}
561 
562 		return new ListG(cast(GList*) p, true);
563 	}
564 
565 	/**
566 	 * Sets @resolver to be the application's default resolver (reffing
567 	 * @resolver, and unreffing the previous default resolver, if any).
568 	 * Future calls to g_resolver_get_default() will return this resolver.
569 	 *
570 	 * This can be used if an application wants to perform any sort of DNS
571 	 * caching or "pinning"; it can implement its own #GResolver that
572 	 * calls the original default resolver for DNS operations, and
573 	 * implements its own cache policies on top of that, and then set
574 	 * itself as the default resolver for all later code to use.
575 	 *
576 	 * Since: 2.22
577 	 */
578 	public void setDefault()
579 	{
580 		g_resolver_set_default(gResolver);
581 	}
582 
583 	/**
584 	 * Emitted when the resolver notices that the system resolver
585 	 * configuration has changed.
586 	 */
587 	gulong addOnReload(void delegate(Resolver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
588 	{
589 		return Signals.connect(this, "reload", dlg, connectFlags ^ ConnectFlags.SWAPPED);
590 	}
591 }