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