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