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