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