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