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 	protected override void setStruct(GObject* obj)
72 	{
73 		gResolver = cast(GResolver*)obj;
74 		super.setStruct(obj);
75 	}
76 
77 	/**
78 	 * Sets our main struct and passes it to the parent class.
79 	 */
80 	public this (GResolver* gResolver, bool ownedRef = false)
81 	{
82 		this.gResolver = gResolver;
83 		super(cast(GObject*)gResolver, ownedRef);
84 	}
85 
86 
87 	/** */
88 	public static GType getType()
89 	{
90 		return g_resolver_get_type();
91 	}
92 
93 	/**
94 	 * Frees @addresses (which should be the return value from
95 	 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_finish()).
96 	 * (This is a convenience method; you can also simply free the results
97 	 * by hand.)
98 	 *
99 	 * Params:
100 	 *     addresses = a #GList of #GInetAddress
101 	 *
102 	 * Since: 2.22
103 	 */
104 	public static void freeAddresses(ListG addresses)
105 	{
106 		g_resolver_free_addresses((addresses is null) ? null : addresses.getListGStruct());
107 	}
108 
109 	/**
110 	 * Frees @targets (which should be the return value from
111 	 * g_resolver_lookup_service() or g_resolver_lookup_service_finish()).
112 	 * (This is a convenience method; you can also simply free the
113 	 * results by hand.)
114 	 *
115 	 * Params:
116 	 *     targets = a #GList of #GSrvTarget
117 	 *
118 	 * Since: 2.22
119 	 */
120 	public static void freeTargets(ListG targets)
121 	{
122 		g_resolver_free_targets((targets is null) ? null : targets.getListGStruct());
123 	}
124 
125 	/**
126 	 * Gets the default #GResolver. You should unref it when you are done
127 	 * with it. #GResolver may use its reference count as a hint about how
128 	 * many threads it should allocate for concurrent DNS resolutions.
129 	 *
130 	 * Returns: the default #GResolver.
131 	 *
132 	 * Since: 2.22
133 	 */
134 	public static Resolver getDefault()
135 	{
136 		auto p = g_resolver_get_default();
137 
138 		if(p is null)
139 		{
140 			return null;
141 		}
142 
143 		return ObjectG.getDObject!(Resolver)(cast(GResolver*) p, true);
144 	}
145 
146 	/**
147 	 * Synchronously reverse-resolves @address to determine its
148 	 * associated hostname.
149 	 *
150 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to
151 	 * a value from #GResolverError.
152 	 *
153 	 * If @cancellable is non-%NULL, it can be used to cancel the
154 	 * operation, in which case @error (if non-%NULL) will be set to
155 	 * %G_IO_ERROR_CANCELLED.
156 	 *
157 	 * Params:
158 	 *     address = the address to reverse-resolve
159 	 *     cancellable = a #GCancellable, or %NULL
160 	 *
161 	 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
162 	 *     form), or %NULL on error.
163 	 *
164 	 * Since: 2.22
165 	 *
166 	 * Throws: GException on failure.
167 	 */
168 	public string lookupByAddress(InetAddress address, Cancellable cancellable)
169 	{
170 		GError* err = null;
171 
172 		auto retStr = g_resolver_lookup_by_address(gResolver, (address is null) ? null : address.getInetAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
173 
174 		if (err !is null)
175 		{
176 			throw new GException( new ErrorG(err) );
177 		}
178 
179 		scope(exit) Str.freeString(retStr);
180 		return Str.toString(retStr);
181 	}
182 
183 	/**
184 	 * Begins asynchronously reverse-resolving @address to determine its
185 	 * associated hostname, and eventually calls @callback, which must
186 	 * call g_resolver_lookup_by_address_finish() to get the final result.
187 	 *
188 	 * Params:
189 	 *     address = the address to reverse-resolve
190 	 *     cancellable = a #GCancellable, or %NULL
191 	 *     callback = callback to call after resolution completes
192 	 *     userData = data for @callback
193 	 *
194 	 * Since: 2.22
195 	 */
196 	public void lookupByAddressAsync(InetAddress address, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
197 	{
198 		g_resolver_lookup_by_address_async(gResolver, (address is null) ? null : address.getInetAddressStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
199 	}
200 
201 	/**
202 	 * Retrieves the result of a previous call to
203 	 * g_resolver_lookup_by_address_async().
204 	 *
205 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
206 	 * a value from #GResolverError. If the operation was cancelled,
207 	 * @error will be set to %G_IO_ERROR_CANCELLED.
208 	 *
209 	 * Params:
210 	 *     result = the result passed to your #GAsyncReadyCallback
211 	 *
212 	 * Returns: a hostname (either ASCII-only, or in ASCII-encoded
213 	 *     form), or %NULL on error.
214 	 *
215 	 * Since: 2.22
216 	 *
217 	 * Throws: GException on failure.
218 	 */
219 	public string lookupByAddressFinish(AsyncResultIF result)
220 	{
221 		GError* err = null;
222 
223 		auto retStr = g_resolver_lookup_by_address_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
224 
225 		if (err !is null)
226 		{
227 			throw new GException( new ErrorG(err) );
228 		}
229 
230 		scope(exit) Str.freeString(retStr);
231 		return Str.toString(retStr);
232 	}
233 
234 	/**
235 	 * Synchronously resolves @hostname to determine its associated IP
236 	 * address(es). @hostname may be an ASCII-only or UTF-8 hostname, or
237 	 * the textual form of an IP address (in which case this just becomes
238 	 * a wrapper around g_inet_address_new_from_string()).
239 	 *
240 	 * On success, g_resolver_lookup_by_name() will return a non-empty #GList of
241 	 * #GInetAddress, sorted in order of preference and guaranteed to not
242 	 * contain duplicates. That is, if using the result to connect to
243 	 * @hostname, you should attempt to connect to the first address
244 	 * first, then the second if the first fails, etc. If you are using
245 	 * the result to listen on a socket, it is appropriate to add each
246 	 * result using e.g. g_socket_listener_add_address().
247 	 *
248 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to a
249 	 * value from #GResolverError and %NULL will be returned.
250 	 *
251 	 * If @cancellable is non-%NULL, it can be used to cancel the
252 	 * operation, in which case @error (if non-%NULL) will be set to
253 	 * %G_IO_ERROR_CANCELLED.
254 	 *
255 	 * If you are planning to connect to a socket on the resolved IP
256 	 * address, it may be easier to create a #GNetworkAddress and use its
257 	 * #GSocketConnectable interface.
258 	 *
259 	 * Params:
260 	 *     hostname = the hostname to look up
261 	 *     cancellable = a #GCancellable, or %NULL
262 	 *
263 	 * Returns: a non-empty #GList
264 	 *     of #GInetAddress, or %NULL on error. You
265 	 *     must unref each of the addresses and free the list when you are
266 	 *     done with it. (You can use g_resolver_free_addresses() to do this.)
267 	 *
268 	 * Since: 2.22
269 	 *
270 	 * Throws: GException on failure.
271 	 */
272 	public ListG lookupByName(string hostname, Cancellable cancellable)
273 	{
274 		GError* err = null;
275 
276 		auto p = g_resolver_lookup_by_name(gResolver, Str.toStringz(hostname), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
277 
278 		if (err !is null)
279 		{
280 			throw new GException( new ErrorG(err) );
281 		}
282 
283 		if(p is null)
284 		{
285 			return null;
286 		}
287 
288 		return new ListG(cast(GList*) p, true);
289 	}
290 
291 	/**
292 	 * Begins asynchronously resolving @hostname to determine its
293 	 * associated IP address(es), and eventually calls @callback, which
294 	 * must call g_resolver_lookup_by_name_finish() to get the result.
295 	 * See g_resolver_lookup_by_name() for more details.
296 	 *
297 	 * Params:
298 	 *     hostname = the hostname to look up the address of
299 	 *     cancellable = a #GCancellable, or %NULL
300 	 *     callback = callback to call after resolution completes
301 	 *     userData = data for @callback
302 	 *
303 	 * Since: 2.22
304 	 */
305 	public void lookupByNameAsync(string hostname, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
306 	{
307 		g_resolver_lookup_by_name_async(gResolver, Str.toStringz(hostname), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
308 	}
309 
310 	/**
311 	 * Retrieves the result of a call to
312 	 * g_resolver_lookup_by_name_async().
313 	 *
314 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
315 	 * a value from #GResolverError. If the operation was cancelled,
316 	 * @error will be set to %G_IO_ERROR_CANCELLED.
317 	 *
318 	 * Params:
319 	 *     result = the result passed to your #GAsyncReadyCallback
320 	 *
321 	 * Returns: a #GList
322 	 *     of #GInetAddress, or %NULL on error. See g_resolver_lookup_by_name()
323 	 *     for more details.
324 	 *
325 	 * Since: 2.22
326 	 *
327 	 * Throws: GException on failure.
328 	 */
329 	public ListG lookupByNameFinish(AsyncResultIF result)
330 	{
331 		GError* err = null;
332 
333 		auto p = g_resolver_lookup_by_name_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
334 
335 		if (err !is null)
336 		{
337 			throw new GException( new ErrorG(err) );
338 		}
339 
340 		if(p is null)
341 		{
342 			return null;
343 		}
344 
345 		return new ListG(cast(GList*) p, true);
346 	}
347 
348 	/**
349 	 * Synchronously performs a DNS record lookup for the given @rrname and returns
350 	 * a list of records as #GVariant tuples. See #GResolverRecordType for
351 	 * information on what the records contain for each @record_type.
352 	 *
353 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to
354 	 * a value from #GResolverError and %NULL will be returned.
355 	 *
356 	 * If @cancellable is non-%NULL, it can be used to cancel the
357 	 * operation, in which case @error (if non-%NULL) will be set to
358 	 * %G_IO_ERROR_CANCELLED.
359 	 *
360 	 * Params:
361 	 *     rrname = the DNS name to lookup the record for
362 	 *     recordType = the type of DNS record to lookup
363 	 *     cancellable = a #GCancellable, or %NULL
364 	 *
365 	 * Returns: a non-empty #GList of
366 	 *     #GVariant, or %NULL on error. You must free each of the records and the list
367 	 *     when you are done with it. (You can use g_list_free_full() with
368 	 *     g_variant_unref() to do this.)
369 	 *
370 	 * Since: 2.34
371 	 *
372 	 * Throws: GException on failure.
373 	 */
374 	public ListG lookupRecords(string rrname, GResolverRecordType recordType, Cancellable cancellable)
375 	{
376 		GError* err = null;
377 
378 		auto p = g_resolver_lookup_records(gResolver, Str.toStringz(rrname), recordType, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
379 
380 		if (err !is null)
381 		{
382 			throw new GException( new ErrorG(err) );
383 		}
384 
385 		if(p is null)
386 		{
387 			return null;
388 		}
389 
390 		return new ListG(cast(GList*) p, true);
391 	}
392 
393 	/**
394 	 * Begins asynchronously performing a DNS lookup for the given
395 	 * @rrname, and eventually calls @callback, which must call
396 	 * g_resolver_lookup_records_finish() to get the final result. See
397 	 * g_resolver_lookup_records() for more details.
398 	 *
399 	 * Params:
400 	 *     rrname = the DNS name to lookup the record for
401 	 *     recordType = the type of DNS record to lookup
402 	 *     cancellable = a #GCancellable, or %NULL
403 	 *     callback = callback to call after resolution completes
404 	 *     userData = data for @callback
405 	 *
406 	 * Since: 2.34
407 	 */
408 	public void lookupRecordsAsync(string rrname, GResolverRecordType recordType, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
409 	{
410 		g_resolver_lookup_records_async(gResolver, Str.toStringz(rrname), recordType, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
411 	}
412 
413 	/**
414 	 * Retrieves the result of a previous call to
415 	 * g_resolver_lookup_records_async(). Returns a non-empty list of records as
416 	 * #GVariant tuples. See #GResolverRecordType for information on what the
417 	 * records contain.
418 	 *
419 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
420 	 * a value from #GResolverError. If the operation was cancelled,
421 	 * @error will be set to %G_IO_ERROR_CANCELLED.
422 	 *
423 	 * Params:
424 	 *     result = the result passed to your #GAsyncReadyCallback
425 	 *
426 	 * Returns: a non-empty #GList of
427 	 *     #GVariant, or %NULL on error. You must free each of the records and the list
428 	 *     when you are done with it. (You can use g_list_free_full() with
429 	 *     g_variant_unref() to do this.)
430 	 *
431 	 * Since: 2.34
432 	 *
433 	 * Throws: GException on failure.
434 	 */
435 	public ListG lookupRecordsFinish(AsyncResultIF result)
436 	{
437 		GError* err = null;
438 
439 		auto p = g_resolver_lookup_records_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
440 
441 		if (err !is null)
442 		{
443 			throw new GException( new ErrorG(err) );
444 		}
445 
446 		if(p is null)
447 		{
448 			return null;
449 		}
450 
451 		return new ListG(cast(GList*) p, true);
452 	}
453 
454 	/**
455 	 * Synchronously performs a DNS SRV lookup for the given @service and
456 	 * @protocol in the given @domain and returns an array of #GSrvTarget.
457 	 * @domain may be an ASCII-only or UTF-8 hostname. Note also that the
458 	 * @service and @protocol arguments do not include the leading underscore
459 	 * that appears in the actual DNS entry.
460 	 *
461 	 * On success, g_resolver_lookup_service() will return a non-empty #GList of
462 	 * #GSrvTarget, sorted in order of preference. (That is, you should
463 	 * attempt to connect to the first target first, then the second if
464 	 * the first fails, etc.)
465 	 *
466 	 * If the DNS resolution fails, @error (if non-%NULL) will be set to
467 	 * a value from #GResolverError and %NULL will be returned.
468 	 *
469 	 * If @cancellable is non-%NULL, it can be used to cancel the
470 	 * operation, in which case @error (if non-%NULL) will be set to
471 	 * %G_IO_ERROR_CANCELLED.
472 	 *
473 	 * If you are planning to connect to the service, it is usually easier
474 	 * to create a #GNetworkService and use its #GSocketConnectable
475 	 * interface.
476 	 *
477 	 * Params:
478 	 *     service = the service type to look up (eg, "ldap")
479 	 *     protocol = the networking protocol to use for @service (eg, "tcp")
480 	 *     domain = the DNS domain to look up the service in
481 	 *     cancellable = a #GCancellable, or %NULL
482 	 *
483 	 * Returns: a non-empty #GList of
484 	 *     #GSrvTarget, or %NULL on error. You must free each of the targets and the
485 	 *     list when you are done with it. (You can use g_resolver_free_targets() to do
486 	 *     this.)
487 	 *
488 	 * Since: 2.22
489 	 *
490 	 * Throws: GException on failure.
491 	 */
492 	public ListG lookupService(string service, string protocol, string domain, Cancellable cancellable)
493 	{
494 		GError* err = null;
495 
496 		auto p = g_resolver_lookup_service(gResolver, Str.toStringz(service), Str.toStringz(protocol), Str.toStringz(domain), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
497 
498 		if (err !is null)
499 		{
500 			throw new GException( new ErrorG(err) );
501 		}
502 
503 		if(p is null)
504 		{
505 			return null;
506 		}
507 
508 		return new ListG(cast(GList*) p, true);
509 	}
510 
511 	/**
512 	 * Begins asynchronously performing a DNS SRV lookup for the given
513 	 * @service and @protocol in the given @domain, and eventually calls
514 	 * @callback, which must call g_resolver_lookup_service_finish() to
515 	 * get the final result. See g_resolver_lookup_service() for more
516 	 * details.
517 	 *
518 	 * Params:
519 	 *     service = the service type to look up (eg, "ldap")
520 	 *     protocol = the networking protocol to use for @service (eg, "tcp")
521 	 *     domain = the DNS domain to look up the service in
522 	 *     cancellable = a #GCancellable, or %NULL
523 	 *     callback = callback to call after resolution completes
524 	 *     userData = data for @callback
525 	 *
526 	 * Since: 2.22
527 	 */
528 	public void lookupServiceAsync(string service, string protocol, string domain, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
529 	{
530 		g_resolver_lookup_service_async(gResolver, Str.toStringz(service), Str.toStringz(protocol), Str.toStringz(domain), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
531 	}
532 
533 	/**
534 	 * Retrieves the result of a previous call to
535 	 * g_resolver_lookup_service_async().
536 	 *
537 	 * If the DNS resolution failed, @error (if non-%NULL) will be set to
538 	 * a value from #GResolverError. If the operation was cancelled,
539 	 * @error will be set to %G_IO_ERROR_CANCELLED.
540 	 *
541 	 * Params:
542 	 *     result = the result passed to your #GAsyncReadyCallback
543 	 *
544 	 * Returns: a non-empty #GList of
545 	 *     #GSrvTarget, or %NULL on error. See g_resolver_lookup_service() for more
546 	 *     details.
547 	 *
548 	 * Since: 2.22
549 	 *
550 	 * Throws: GException on failure.
551 	 */
552 	public ListG lookupServiceFinish(AsyncResultIF result)
553 	{
554 		GError* err = null;
555 
556 		auto p = g_resolver_lookup_service_finish(gResolver, (result is null) ? null : result.getAsyncResultStruct(), &err);
557 
558 		if (err !is null)
559 		{
560 			throw new GException( new ErrorG(err) );
561 		}
562 
563 		if(p is null)
564 		{
565 			return null;
566 		}
567 
568 		return new ListG(cast(GList*) p, true);
569 	}
570 
571 	/**
572 	 * Sets @resolver to be the application's default resolver (reffing
573 	 * @resolver, and unreffing the previous default resolver, if any).
574 	 * Future calls to g_resolver_get_default() will return this resolver.
575 	 *
576 	 * This can be used if an application wants to perform any sort of DNS
577 	 * caching or "pinning"; it can implement its own #GResolver that
578 	 * calls the original default resolver for DNS operations, and
579 	 * implements its own cache policies on top of that, and then set
580 	 * itself as the default resolver for all later code to use.
581 	 *
582 	 * Since: 2.22
583 	 */
584 	public void setDefault()
585 	{
586 		g_resolver_set_default(gResolver);
587 	}
588 
589 	protected class OnReloadDelegateWrapper
590 	{
591 		void delegate(Resolver) dlg;
592 		gulong handlerId;
593 
594 		this(void delegate(Resolver) dlg)
595 		{
596 			this.dlg = dlg;
597 			onReloadListeners ~= this;
598 		}
599 
600 		void remove(OnReloadDelegateWrapper source)
601 		{
602 			foreach(index, wrapper; onReloadListeners)
603 			{
604 				if (wrapper.handlerId == source.handlerId)
605 				{
606 					onReloadListeners[index] = null;
607 					onReloadListeners = std.algorithm.remove(onReloadListeners, index);
608 					break;
609 				}
610 			}
611 		}
612 	}
613 	OnReloadDelegateWrapper[] onReloadListeners;
614 
615 	/**
616 	 * Emitted when the resolver notices that the system resolver
617 	 * configuration has changed.
618 	 */
619 	gulong addOnReload(void delegate(Resolver) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
620 	{
621 		auto wrapper = new OnReloadDelegateWrapper(dlg);
622 		wrapper.handlerId = Signals.connectData(
623 			this,
624 			"reload",
625 			cast(GCallback)&callBackReload,
626 			cast(void*)wrapper,
627 			cast(GClosureNotify)&callBackReloadDestroy,
628 			connectFlags);
629 		return wrapper.handlerId;
630 	}
631 
632 	extern(C) static void callBackReload(GResolver* resolverStruct, OnReloadDelegateWrapper wrapper)
633 	{
634 		wrapper.dlg(wrapper.outer);
635 	}
636 
637 	extern(C) static void callBackReloadDestroy(OnReloadDelegateWrapper wrapper, GClosure* closure)
638 	{
639 		wrapper.remove(wrapper);
640 	}
641 }