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