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