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