Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit a347395

Browse files
authored
gio: Merge pull request #315 from GuillaumeGomez/inet-addr
Inet addr
2 parents f813f55 + f0f0815 commit a347395

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/inet_address.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use gio_sys;
2+
use glib::object::IsA;
23
use glib::translate::*;
34
use InetAddress;
5+
use InetAddressExt;
46
use SocketFamily;
57

8+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
9+
610
#[derive(Debug)]
711
pub enum InetAddressBytes<'a> {
812
V4(&'a [u8; 4]),
@@ -22,13 +26,11 @@ impl<'a> InetAddressBytes<'a> {
2226

2327
impl InetAddress {
2428
pub fn from_bytes(inet_address_bytes: InetAddressBytes) -> Self {
25-
use self::InetAddressBytes::*;
26-
2729
let bytes = inet_address_bytes.deref();
2830

2931
let family = match inet_address_bytes {
30-
V4(_) => SocketFamily::Ipv4,
31-
V6(_) => SocketFamily::Ipv6,
32+
InetAddressBytes::V4(_) => SocketFamily::Ipv4,
33+
InetAddressBytes::V6(_) => SocketFamily::Ipv6,
3234
};
3335
unsafe {
3436
from_glib_full(gio_sys::g_inet_address_new_from_bytes(
@@ -38,3 +40,49 @@ impl InetAddress {
3840
}
3941
}
4042
}
43+
44+
pub trait InetAddressExtManual {
45+
fn to_bytes<'a>(&'a self) -> Option<InetAddressBytes<'a>>;
46+
}
47+
48+
impl<O: IsA<InetAddress>> InetAddressExtManual for O {
49+
/// Returns `None` in case the address has a native size different than 4 and 16.
50+
fn to_bytes<'a>(&'a self) -> Option<InetAddressBytes<'a>> {
51+
let size = self.get_native_size();
52+
unsafe {
53+
let bytes = gio_sys::g_inet_address_to_bytes(self.as_ref().to_glib_none().0);
54+
if size == 4 {
55+
Some(InetAddressBytes::V4(&*(bytes as *const [u8; 4])))
56+
} else if size == 16 {
57+
Some(InetAddressBytes::V6(&*(bytes as *const [u8; 16])))
58+
} else {
59+
None
60+
}
61+
}
62+
}
63+
}
64+
65+
impl From<IpAddr> for InetAddress {
66+
fn from(addr: IpAddr) -> Self {
67+
match addr {
68+
IpAddr::V4(v4) => InetAddress::from_bytes(InetAddressBytes::V4(&v4.octets())),
69+
IpAddr::V6(v6) => InetAddress::from_bytes(InetAddressBytes::V6(&v6.octets())),
70+
}
71+
}
72+
}
73+
74+
impl Into<IpAddr> for InetAddress {
75+
fn into(self) -> IpAddr {
76+
let size = self.get_native_size();
77+
unsafe {
78+
let bytes = gio_sys::g_inet_address_to_bytes(self.to_glib_none().0);
79+
if size == 4 {
80+
IpAddr::V4(Ipv4Addr::from(*(bytes as *const [u8; 4])))
81+
} else if size == 16 {
82+
IpAddr::V6(Ipv6Addr::from(*(bytes as *const [u16; 8])))
83+
} else {
84+
panic!("Unknown IP kind");
85+
}
86+
}
87+
}
88+
}

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub use data_input_stream::DataInputStreamExtManual;
1818
#[cfg(any(all(not(windows), not(target_os = "macos")), feature = "dox"))]
1919
pub use desktop_app_info::DesktopAppInfoExtManual;
2020
pub use file::FileExtManual;
21+
pub use inet_address::InetAddressExtManual;
2122
pub use input_stream::InputStreamExtManual;
2223
pub use io_stream::IOStreamExtManual;
2324
#[cfg(any(feature = "v2_44", feature = "dox"))]

0 commit comments

Comments
 (0)