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 	public static GType getType()
84 	{
85 		return g_network_address_get_type();
86 	}
87 
88 	/**
89 	 * Creates a new #GSocketConnectable for connecting to the given
90 	 * @hostname and @port.
91 	 *
92 	 * Note that depending on the configuration of the machine, a
93 	 * @hostname of `localhost` may refer to the IPv4 loopback address
94 	 * only, or to both IPv4 and IPv6; use
95 	 * g_network_address_new_loopback() to create a #GNetworkAddress that
96 	 * is guaranteed to resolve to both addresses.
97 	 *
98 	 * Params:
99 	 *     hostname = the hostname
100 	 *     port = the port
101 	 *
102 	 * Return: the new #GNetworkAddress
103 	 *
104 	 * Since: 2.22
105 	 *
106 	 * Throws: ConstructionException GTK+ fails to create the object.
107 	 */
108 	public this(string hostname, ushort port)
109 	{
110 		auto p = g_network_address_new(Str.toStringz(hostname), port);
111 		
112 		if(p is null)
113 		{
114 			throw new ConstructionException("null returned by new");
115 		}
116 		
117 		this(cast(GNetworkAddress*) p, true);
118 	}
119 
120 	/**
121 	 * Creates a new #GSocketConnectable for connecting to the local host
122 	 * over a loopback connection to the given @port. This is intended for
123 	 * use in connecting to local services which may be running on IPv4 or
124 	 * IPv6.
125 	 *
126 	 * The connectable will return IPv4 and IPv6 loopback addresses,
127 	 * regardless of how the host resolves `localhost`. By contrast,
128 	 * g_network_address_new() will often only return an IPv4 address when
129 	 * resolving `localhost`, and an IPv6 address for `localhost6`.
130 	 *
131 	 * g_network_address_get_hostname() will always return `localhost` for
132 	 * #GNetworkAddresses created with this constructor.
133 	 *
134 	 * Params:
135 	 *     port = the port
136 	 *
137 	 * Return: the new #GNetworkAddress
138 	 *
139 	 * Since: 2.44
140 	 *
141 	 * Throws: ConstructionException GTK+ fails to create the object.
142 	 */
143 	public this(ushort port)
144 	{
145 		auto p = g_network_address_new_loopback(port);
146 		
147 		if(p is null)
148 		{
149 			throw new ConstructionException("null returned by new_loopback");
150 		}
151 		
152 		this(cast(GNetworkAddress*) p, true);
153 	}
154 
155 	/**
156 	 * Creates a new #GSocketConnectable for connecting to the given
157 	 * @hostname and @port. May fail and return %NULL in case
158 	 * parsing @host_and_port fails.
159 	 *
160 	 * @host_and_port may be in any of a number of recognised formats; an IPv6
161 	 * address, an IPv4 address, or a domain name (in which case a DNS
162 	 * lookup is performed). Quoting with [] is supported for all address
163 	 * types. A port override may be specified in the usual way with a
164 	 * colon.
165 	 *
166 	 * If no port is specified in @host_and_port then @default_port will be
167 	 * used as the port number to connect to.
168 	 *
169 	 * In general, @host_and_port is expected to be provided by the user
170 	 * (allowing them to give the hostname, and a port overide if necessary)
171 	 * and @default_port is expected to be provided by the application.
172 	 *
173 	 * (The port component of @host_and_port can also be specified as a
174 	 * service name rather than as a numeric port, but this functionality
175 	 * is deprecated, because it depends on the contents of /etc/services,
176 	 * which is generally quite sparse on platforms other than Linux.)
177 	 *
178 	 * Params:
179 	 *     hostAndPort = the hostname and optionally a port
180 	 *     defaultPort = the default port if not in @host_and_port
181 	 *
182 	 * Return: the new
183 	 *     #GNetworkAddress, or %NULL on error
184 	 *
185 	 * Since: 2.22
186 	 *
187 	 * Throws: GException on failure.
188 	 */
189 	public static NetworkAddress parse(string hostAndPort, ushort defaultPort)
190 	{
191 		GError* err = null;
192 		
193 		auto p = g_network_address_parse(Str.toStringz(hostAndPort), defaultPort, &err);
194 		
195 		if (err !is null)
196 		{
197 			throw new GException( new ErrorG(err) );
198 		}
199 		
200 		if(p is null)
201 		{
202 			return null;
203 		}
204 		
205 		return ObjectG.getDObject!(NetworkAddress)(cast(GNetworkAddress*) p, true);
206 	}
207 
208 	/**
209 	 * Creates a new #GSocketConnectable for connecting to the given
210 	 * @uri. May fail and return %NULL in case parsing @uri fails.
211 	 *
212 	 * Using this rather than g_network_address_new() or
213 	 * g_network_address_parse() allows #GSocketClient to determine
214 	 * when to use application-specific proxy protocols.
215 	 *
216 	 * Params:
217 	 *     uri = the hostname and optionally a port
218 	 *     defaultPort = The default port if none is found in the URI
219 	 *
220 	 * Return: the new
221 	 *     #GNetworkAddress, or %NULL on error
222 	 *
223 	 * Since: 2.26
224 	 *
225 	 * Throws: GException on failure.
226 	 */
227 	public static NetworkAddress 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!(NetworkAddress)(cast(GNetworkAddress*) p, true);
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 }