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.NetworkAddress;
26 
27 private import gio.SocketConnectableIF;
28 private import gio.SocketConnectableT;
29 private import gio.c.functions;
30 public  import gio.c.types;
31 private import glib.ConstructionException;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.Str;
35 private import gobject.ObjectG;
36 public  import gtkc.giotypes;
37 
38 
39 /**
40  * #GNetworkAddress provides an easy way to resolve a hostname and
41  * then attempt to connect to that host, handling the possibility of
42  * multiple IP addresses and multiple address families.
43  * 
44  * See #GSocketConnectable for and example of using the connectable
45  * interface.
46  */
47 public class NetworkAddress : ObjectG, SocketConnectableIF
48 {
49 	/** the main Gtk struct */
50 	protected GNetworkAddress* gNetworkAddress;
51 
52 	/** Get the main Gtk struct */
53 	public GNetworkAddress* getNetworkAddressStruct(bool transferOwnership = false)
54 	{
55 		if (transferOwnership)
56 			ownedRef = false;
57 		return gNetworkAddress;
58 	}
59 
60 	/** the main Gtk struct as a void* */
61 	protected override void* getStruct()
62 	{
63 		return cast(void*)gNetworkAddress;
64 	}
65 
66 	/**
67 	 * Sets our main struct and passes it to the parent class.
68 	 */
69 	public this (GNetworkAddress* gNetworkAddress, bool ownedRef = false)
70 	{
71 		this.gNetworkAddress = gNetworkAddress;
72 		super(cast(GObject*)gNetworkAddress, ownedRef);
73 	}
74 
75 	// add the SocketConnectable capabilities
76 	mixin SocketConnectableT!(GNetworkAddress);
77 
78 
79 	/** */
80 	public static GType getType()
81 	{
82 		return g_network_address_get_type();
83 	}
84 
85 	/**
86 	 * Creates a new #GSocketConnectable for connecting to the given
87 	 * @hostname and @port.
88 	 *
89 	 * Note that depending on the configuration of the machine, a
90 	 * @hostname of `localhost` may refer to the IPv4 loopback address
91 	 * only, or to both IPv4 and IPv6; use
92 	 * g_network_address_new_loopback() to create a #GNetworkAddress that
93 	 * is guaranteed to resolve to both addresses.
94 	 *
95 	 * Params:
96 	 *     hostname = the hostname
97 	 *     port = the port
98 	 *
99 	 * Returns: the new #GNetworkAddress
100 	 *
101 	 * Since: 2.22
102 	 *
103 	 * Throws: ConstructionException GTK+ fails to create the object.
104 	 */
105 	public this(string hostname, ushort port)
106 	{
107 		auto p = g_network_address_new(Str.toStringz(hostname), port);
108 
109 		if(p is null)
110 		{
111 			throw new ConstructionException("null returned by new");
112 		}
113 
114 		this(cast(GNetworkAddress*) p, true);
115 	}
116 
117 	/**
118 	 * Creates a new #GSocketConnectable for connecting to the local host
119 	 * over a loopback connection to the given @port. This is intended for
120 	 * use in connecting to local services which may be running on IPv4 or
121 	 * IPv6.
122 	 *
123 	 * The connectable will return IPv4 and IPv6 loopback addresses,
124 	 * regardless of how the host resolves `localhost`. By contrast,
125 	 * g_network_address_new() will often only return an IPv4 address when
126 	 * resolving `localhost`, and an IPv6 address for `localhost6`.
127 	 *
128 	 * g_network_address_get_hostname() will always return `localhost` for
129 	 * #GNetworkAddresses created with this constructor.
130 	 *
131 	 * Params:
132 	 *     port = the port
133 	 *
134 	 * Returns: the new #GNetworkAddress
135 	 *
136 	 * Since: 2.44
137 	 *
138 	 * Throws: ConstructionException GTK+ fails to create the object.
139 	 */
140 	public this(ushort port)
141 	{
142 		auto p = g_network_address_new_loopback(port);
143 
144 		if(p is null)
145 		{
146 			throw new ConstructionException("null returned by new_loopback");
147 		}
148 
149 		this(cast(GNetworkAddress*) p, true);
150 	}
151 
152 	/**
153 	 * Creates a new #GSocketConnectable for connecting to the given
154 	 * @hostname and @port. May fail and return %NULL in case
155 	 * parsing @host_and_port fails.
156 	 *
157 	 * @host_and_port may be in any of a number of recognised formats; an IPv6
158 	 * address, an IPv4 address, or a domain name (in which case a DNS
159 	 * lookup is performed). Quoting with [] is supported for all address
160 	 * types. A port override may be specified in the usual way with a
161 	 * colon.
162 	 *
163 	 * If no port is specified in @host_and_port then @default_port will be
164 	 * used as the port number to connect to.
165 	 *
166 	 * In general, @host_and_port is expected to be provided by the user
167 	 * (allowing them to give the hostname, and a port override if necessary)
168 	 * and @default_port is expected to be provided by the application.
169 	 *
170 	 * (The port component of @host_and_port can also be specified as a
171 	 * service name rather than as a numeric port, but this functionality
172 	 * is deprecated, because it depends on the contents of /etc/services,
173 	 * which is generally quite sparse on platforms other than Linux.)
174 	 *
175 	 * Params:
176 	 *     hostAndPort = the hostname and optionally a port
177 	 *     defaultPort = the default port if not in @host_and_port
178 	 *
179 	 * Returns: the new
180 	 *     #GNetworkAddress, or %NULL on error
181 	 *
182 	 * Since: 2.22
183 	 *
184 	 * Throws: GException on failure.
185 	 */
186 	public static NetworkAddress parse(string hostAndPort, ushort defaultPort)
187 	{
188 		GError* err = null;
189 
190 		auto p = g_network_address_parse(Str.toStringz(hostAndPort), defaultPort, &err);
191 
192 		if (err !is null)
193 		{
194 			throw new GException( new ErrorG(err) );
195 		}
196 
197 		if(p is null)
198 		{
199 			return null;
200 		}
201 
202 		return ObjectG.getDObject!(NetworkAddress)(cast(GNetworkAddress*) p, true);
203 	}
204 
205 	/**
206 	 * Creates a new #GSocketConnectable for connecting to the given
207 	 * @uri. May fail and return %NULL in case parsing @uri fails.
208 	 *
209 	 * Using this rather than g_network_address_new() or
210 	 * g_network_address_parse() allows #GSocketClient to determine
211 	 * when to use application-specific proxy protocols.
212 	 *
213 	 * Params:
214 	 *     uri = the hostname and optionally a port
215 	 *     defaultPort = The default port if none is found in the URI
216 	 *
217 	 * Returns: the new
218 	 *     #GNetworkAddress, or %NULL on error
219 	 *
220 	 * Since: 2.26
221 	 *
222 	 * Throws: GException on failure.
223 	 */
224 	public static NetworkAddress parseUri(string uri, ushort defaultPort)
225 	{
226 		GError* err = null;
227 
228 		auto p = g_network_address_parse_uri(Str.toStringz(uri), defaultPort, &err);
229 
230 		if (err !is null)
231 		{
232 			throw new GException( new ErrorG(err) );
233 		}
234 
235 		if(p is null)
236 		{
237 			return null;
238 		}
239 
240 		return ObjectG.getDObject!(NetworkAddress)(cast(GNetworkAddress*) p, true);
241 	}
242 
243 	/**
244 	 * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded,
245 	 * depending on what @addr was created with.
246 	 *
247 	 * Returns: @addr's hostname
248 	 *
249 	 * Since: 2.22
250 	 */
251 	public string getHostname()
252 	{
253 		return Str.toString(g_network_address_get_hostname(gNetworkAddress));
254 	}
255 
256 	/**
257 	 * Gets @addr's port number
258 	 *
259 	 * Returns: @addr's port (which may be 0)
260 	 *
261 	 * Since: 2.22
262 	 */
263 	public ushort getPort()
264 	{
265 		return g_network_address_get_port(gNetworkAddress);
266 	}
267 
268 	/**
269 	 * Gets @addr's scheme
270 	 *
271 	 * Returns: @addr's scheme (%NULL if not built from URI)
272 	 *
273 	 * Since: 2.26
274 	 */
275 	public string getScheme()
276 	{
277 		return Str.toString(g_network_address_get_scheme(gNetworkAddress));
278 	}
279 }