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.SocketConnectable;
28 private import gio.SocketConnectableIF;
29 private import gio.SocketConnectableT;
30 private import glib.ConstructionException;
31 private import glib.ErrorG;
32 private import glib.GException;
33 private import glib.Str;
34 private import gobject.ObjectG;
35 private import gtkc.gio;
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()
54 	{
55 		return gNetworkAddress;
56 	}
57 
58 	/** the main Gtk struct as a void* */
59 	protected override void* getStruct()
60 	{
61 		return cast(void*)gNetworkAddress;
62 	}
63 
64 	protected override void setStruct(GObject* obj)
65 	{
66 		gNetworkAddress = cast(GNetworkAddress*)obj;
67 		super.setStruct(obj);
68 	}
69 
70 	/**
71 	 * Sets our main struct and passes it to the parent class.
72 	 */
73 	public this (GNetworkAddress* gNetworkAddress, bool ownedRef = false)
74 	{
75 		this.gNetworkAddress = gNetworkAddress;
76 		super(cast(GObject*)gNetworkAddress, ownedRef);
77 	}
78 
79 	// add the SocketConnectable capabilities
80 	mixin SocketConnectableT!(GNetworkAddress);
81 
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 	 * Return: 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 	 * Return: 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 	 * Return: the new #GNetworkAddress, or %NULL on error
185 	 *
186 	 * Since: 2.22
187 	 *
188 	 * Throws: GException on failure.
189 	 */
190 	public static SocketConnectableIF parse(string hostAndPort, ushort defaultPort)
191 	{
192 		GError* err = null;
193 		
194 		auto p = g_network_address_parse(Str.toStringz(hostAndPort), defaultPort, &err);
195 		
196 		if (err !is null)
197 		{
198 			throw new GException( new ErrorG(err) );
199 		}
200 		
201 		if(p is null)
202 		{
203 			return null;
204 		}
205 		
206 		return ObjectG.getDObject!(SocketConnectable, SocketConnectableIF)(cast(GSocketConnectable*) p);
207 	}
208 
209 	/**
210 	 * Creates a new #GSocketConnectable for connecting to the given
211 	 * @uri. May fail and return %NULL in case parsing @uri fails.
212 	 *
213 	 * Using this rather than g_network_address_new() or
214 	 * g_network_address_parse() allows #GSocketClient to determine
215 	 * when to use application-specific proxy protocols.
216 	 *
217 	 * Params:
218 	 *     uri = the hostname and optionally a port
219 	 *     defaultPort = The default port if none is found in the URI
220 	 *
221 	 * Return: the new #GNetworkAddress, or %NULL on error
222 	 *
223 	 * Since: 2.26
224 	 *
225 	 * Throws: GException on failure.
226 	 */
227 	public static SocketConnectableIF parseUri(string uri, ushort defaultPort)
228 	{
229 		GError* err = null;
230 		
231 		auto p = g_network_address_parse_uri(Str.toStringz(uri), defaultPort, &err);
232 		
233 		if (err !is null)
234 		{
235 			throw new GException( new ErrorG(err) );
236 		}
237 		
238 		if(p is null)
239 		{
240 			return null;
241 		}
242 		
243 		return ObjectG.getDObject!(SocketConnectable, SocketConnectableIF)(cast(GSocketConnectable*) p);
244 	}
245 
246 	/**
247 	 * Gets @addr's hostname. This might be either UTF-8 or ASCII-encoded,
248 	 * depending on what @addr was created with.
249 	 *
250 	 * Return: @addr's hostname
251 	 *
252 	 * Since: 2.22
253 	 */
254 	public string getHostname()
255 	{
256 		return Str.toString(g_network_address_get_hostname(gNetworkAddress));
257 	}
258 
259 	/**
260 	 * Gets @addr's port number
261 	 *
262 	 * Return: @addr's port (which may be 0)
263 	 *
264 	 * Since: 2.22
265 	 */
266 	public ushort getPort()
267 	{
268 		return g_network_address_get_port(gNetworkAddress);
269 	}
270 
271 	/**
272 	 * Gets @addr's scheme
273 	 *
274 	 * Return: @addr's scheme (%NULL if not built from URI)
275 	 *
276 	 * Since: 2.26
277 	 */
278 	public string getScheme()
279 	{
280 		return Str.toString(g_network_address_get_scheme(gNetworkAddress));
281 	}
282 }