1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::SocketConnectable;
use glib::object::IsA;
use glib::translate::*;
use std::fmt;
use std::ptr;

glib::wrapper! {
    /// [`NetworkAddress`][crate::NetworkAddress] provides an easy way to resolve a hostname and
    /// then attempt to connect to that host, handling the possibility of
    /// multiple IP addresses and multiple address families.
    ///
    /// The enumeration results of resolved addresses *may* be cached as long
    /// as this object is kept alive which may have unexpected results if
    /// alive for too long.
    ///
    /// See [`SocketConnectable`][crate::SocketConnectable] for an example of using the connectable
    /// interface.
    ///
    /// # Implements
    ///
    /// [`NetworkAddressExt`][trait@crate::prelude::NetworkAddressExt], [`trait@glib::ObjectExt`], [`SocketConnectableExt`][trait@crate::prelude::SocketConnectableExt]
    #[doc(alias = "GNetworkAddress")]
    pub struct NetworkAddress(Object<ffi::GNetworkAddress, ffi::GNetworkAddressClass>) @implements SocketConnectable;

    match fn {
        type_ => || ffi::g_network_address_get_type(),
    }
}

impl NetworkAddress {
    pub const NONE: Option<&'static NetworkAddress> = None;

    /// Creates a new [`SocketConnectable`][crate::SocketConnectable] for connecting to the given
    /// `hostname` and `port`.
    ///
    /// Note that depending on the configuration of the machine, a
    /// `hostname` of `localhost` may refer to the IPv4 loopback address
    /// only, or to both IPv4 and IPv6; use
    /// [`new_loopback()`][Self::new_loopback()] to create a [`NetworkAddress`][crate::NetworkAddress] that
    /// is guaranteed to resolve to both addresses.
    /// ## `hostname`
    /// the hostname
    /// ## `port`
    /// the port
    ///
    /// # Returns
    ///
    /// the new [`NetworkAddress`][crate::NetworkAddress]
    #[doc(alias = "g_network_address_new")]
    pub fn new(hostname: &str, port: u16) -> NetworkAddress {
        unsafe { from_glib_full(ffi::g_network_address_new(hostname.to_glib_none().0, port)) }
    }

    /// Creates a new [`SocketConnectable`][crate::SocketConnectable] for connecting to the local host
    /// over a loopback connection to the given `port`. This is intended for
    /// use in connecting to local services which may be running on IPv4 or
    /// IPv6.
    ///
    /// The connectable will return IPv4 and IPv6 loopback addresses,
    /// regardless of how the host resolves `localhost`. By contrast,
    /// [`new()`][Self::new()] will often only return an IPv4 address when
    /// resolving `localhost`, and an IPv6 address for `localhost6`.
    ///
    /// [`NetworkAddressExt::hostname()`][crate::prelude::NetworkAddressExt::hostname()] will always return `localhost` for
    /// a [`NetworkAddress`][crate::NetworkAddress] created with this constructor.
    /// ## `port`
    /// the port
    ///
    /// # Returns
    ///
    /// the new [`NetworkAddress`][crate::NetworkAddress]
    #[doc(alias = "g_network_address_new_loopback")]
    pub fn new_loopback(port: u16) -> NetworkAddress {
        unsafe { from_glib_full(ffi::g_network_address_new_loopback(port)) }
    }

    /// Creates a new [`SocketConnectable`][crate::SocketConnectable] for connecting to the given
    /// `hostname` and `port`. May fail and return [`None`] in case
    /// parsing `host_and_port` fails.
    ///
    /// `host_and_port` may be in any of a number of recognised formats; an IPv6
    /// address, an IPv4 address, or a domain name (in which case a DNS
    /// lookup is performed). Quoting with [] is supported for all address
    /// types. A port override may be specified in the usual way with a
    /// colon.
    ///
    /// If no port is specified in `host_and_port` then `default_port` will be
    /// used as the port number to connect to.
    ///
    /// In general, `host_and_port` is expected to be provided by the user
    /// (allowing them to give the hostname, and a port override if necessary)
    /// and `default_port` is expected to be provided by the application.
    ///
    /// (The port component of `host_and_port` can also be specified as a
    /// service name rather than as a numeric port, but this functionality
    /// is deprecated, because it depends on the contents of /etc/services,
    /// which is generally quite sparse on platforms other than Linux.)
    /// ## `host_and_port`
    /// the hostname and optionally a port
    /// ## `default_port`
    /// the default port if not in `host_and_port`
    ///
    /// # Returns
    ///
    /// the new
    ///  [`NetworkAddress`][crate::NetworkAddress], or [`None`] on error
    #[doc(alias = "g_network_address_parse")]
    pub fn parse(host_and_port: &str, default_port: u16) -> Result<NetworkAddress, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ffi::g_network_address_parse(
                host_and_port.to_glib_none().0,
                default_port,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Creates a new [`SocketConnectable`][crate::SocketConnectable] for connecting to the given
    /// `uri`. May fail and return [`None`] in case parsing `uri` fails.
    ///
    /// Using this rather than [`new()`][Self::new()] or
    /// [`parse()`][Self::parse()] allows [`SocketClient`][crate::SocketClient] to determine
    /// when to use application-specific proxy protocols.
    /// ## `uri`
    /// the hostname and optionally a port
    /// ## `default_port`
    /// The default port if none is found in the URI
    ///
    /// # Returns
    ///
    /// the new
    ///  [`NetworkAddress`][crate::NetworkAddress], or [`None`] on error
    #[doc(alias = "g_network_address_parse_uri")]
    pub fn parse_uri(uri: &str, default_port: u16) -> Result<NetworkAddress, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret =
                ffi::g_network_address_parse_uri(uri.to_glib_none().0, default_port, &mut error);
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }
}

unsafe impl Send for NetworkAddress {}
unsafe impl Sync for NetworkAddress {}

/// Trait containing all [`struct@NetworkAddress`] methods.
///
/// # Implementors
///
/// [`NetworkAddress`][struct@crate::NetworkAddress]
pub trait NetworkAddressExt: 'static {
    /// Gets `self`'s hostname. This might be either UTF-8 or ASCII-encoded,
    /// depending on what `self` was created with.
    ///
    /// # Returns
    ///
    /// `self`'s hostname
    #[doc(alias = "g_network_address_get_hostname")]
    #[doc(alias = "get_hostname")]
    fn hostname(&self) -> glib::GString;

    /// Gets `self`'s port number
    ///
    /// # Returns
    ///
    /// `self`'s port (which may be 0)
    #[doc(alias = "g_network_address_get_port")]
    #[doc(alias = "get_port")]
    fn port(&self) -> u16;

    /// Gets `self`'s scheme
    ///
    /// # Returns
    ///
    /// `self`'s scheme ([`None`] if not built from URI)
    #[doc(alias = "g_network_address_get_scheme")]
    #[doc(alias = "get_scheme")]
    fn scheme(&self) -> Option<glib::GString>;
}

impl<O: IsA<NetworkAddress>> NetworkAddressExt for O {
    fn hostname(&self) -> glib::GString {
        unsafe {
            from_glib_none(ffi::g_network_address_get_hostname(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn port(&self) -> u16 {
        unsafe { ffi::g_network_address_get_port(self.as_ref().to_glib_none().0) }
    }

    fn scheme(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::g_network_address_get_scheme(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl fmt::Display for NetworkAddress {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("NetworkAddress")
    }
}