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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/*-
* cdns-rs - a simple sync/async DNS query library
* Copyright (C) 2020 Aleksandr Morozov, RELKOM s.r.o
* Copyright (C) 2021 Aleksandr Morozov
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
use std::
{
ffi::{CStr, CString},
mem::MaybeUninit,
net::{IpAddr, Ipv4Addr, Ipv6Addr}
};
use crate::{error::*, internal_error, internal_error_map};
/// This file contains code which should be manually ported to other
/// OSes because it is OS specific or there is no support in rust's libc.
/*
pub const IFNAMSIZ: usize = 16;
#[repr(C)]
#[derive(Copy, Clone)]
pub union ifr_ifrn_u
{
pub ifrn_name: [u8; IFNAMSIZ],
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifmap
{
pub mem_start: libc::c_long,
pub mem_end: libc::c_long,
pub base_addr: libc::c_short,
pub irq: u8,
pub dma: u8,
pub port: u8
// 3 bytes spare
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union ifsu {
pub raw_hdlc_proto: *mut libc::c_void,
pub cisco: *mut libc::c_void,
pub fr: *mut libc::c_void,
pub fr_pvc: *mut libc::c_void,
pub fr_pvc_info: *mut libc::c_void,
pub sync: *mut libc::c_void,
pub te1: *mut libc::c_void,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct if_settings
{
pub type_: libc::c_uint,
pub size: libc::c_uint,
pub ifsu: ifsu,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union ifr_ifru_u
{
pub ifru_addr: libc::sockaddr,
pub ifru_dstaddr: libc::sockaddr,
pub ifru_broadaddr: libc::sockaddr,
pub ifru_netmask: libc::sockaddr,
pub ifru_hwaddr: libc::sockaddr,
pub ifru_flags: libc::c_short,
pub ifru_ivalue: libc::c_int,
pub ifru_mtu: libc::c_int,
pub ifru_map: ifmap,
pub ifru_slave: [i8; IFNAMSIZ],
pub ifru_newname: [i8; IFNAMSIZ],
pub ifru_data: *const libc::c_void,
pub ifru_settings: if_settings
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ifreq
{
pub ifr_name: ifr_ifrn_u,
pub ifr_ifru: ifr_ifru_u
}*/
/// A network interface info
pub struct IfInfo
{
/// A pointer to the start of the linked list with IF data
if_inf: *mut libc::ifaddrs,
}
impl Drop for IfInfo
{
fn drop(&mut self)
{
if self.if_inf.is_null() == true
{
return;
}
unsafe
{
libc::freeifaddrs(self.if_inf);
}
}
}
impl IfInfo
{
const NI_MAXHOST: usize = 1025;
/// Creates an instance with pointer to the linked list with interfaces in system.
pub unsafe
fn get_interfaces_info() -> CDnsResult<Self>
{
let mut addrs = MaybeUninit::<*mut libc::ifaddrs>::uninit();
let ifaddrs = libc::getifaddrs(addrs.as_mut_ptr());
if ifaddrs == -1
{
internal_error!(CDnsErrorType::InternalError, "{}", std::io::Error::last_os_error());
}
return Ok(
Self { if_inf: addrs.assume_init() }
);
}
/// Returns the interface IP address if inteface exists.
pub unsafe
fn get_ifr_ip(&self, ifr: &str, ip_addr: &IpAddr) -> CDnsResult<Option<IpAddr>>
{
if self.if_inf.is_null() == true
{
panic!("if_inf is NULL");
}
let c_ifr = CString::new(ifr).map_err(|e| internal_error_map!(CDnsErrorType::InternalError, "{}", e))?;
let mut tmp = self.if_inf;
while tmp.is_null() == false
{
if (*tmp).ifa_name.is_null() == true
{
tmp = (*tmp).ifa_next;
continue;
}
// convert raw char ptr to cstr
let name = CStr::from_ptr((*tmp).ifa_name);
// match title
if name != c_ifr.as_c_str()
{
tmp = (*tmp).ifa_next;
continue;
}
let addr = (*tmp).ifa_addr;
if addr.is_null() == false
{
let mut host = [0_u8; libc::NI_MAXHOST as usize];
let fam = (*addr).sa_family;
if fam == libc::AF_INET as u16 && ip_addr.is_ipv4() == true
{
let s =
libc::getnameinfo(
addr,
std::mem::size_of::<libc::sockaddr_in>() as u32,
host.as_mut_ptr() as *mut i8,
libc::NI_MAXHOST as u32,
std::ptr::null_mut(),
0,
libc::NI_NUMERICHOST
);
if s != 0
{
internal_error!(CDnsErrorType::InternalError, "{}", std::io::Error::last_os_error());
}
let c =
CStr::from_ptr(host.as_ptr() as *const i8)
.to_str()
.map_err(|e| internal_error_map!(CDnsErrorType::InternalError, "{}", e))?;
let ip4: Ipv4Addr = c.parse().map_err(|e| internal_error_map!(CDnsErrorType::InternalError, "{}", e))?;
return Ok(Some(IpAddr::from(ip4)));
}
else if fam == libc::AF_INET6 as u16 && ip_addr.is_ipv6() == true
{
let s =
libc::getnameinfo(
addr,
std::mem::size_of::<libc::sockaddr_in6>() as u32,
host.as_mut_ptr() as *mut i8,
libc::NI_MAXHOST as u32,
std::ptr::null_mut(),
0,
libc::NI_NUMERICHOST
);
if s != 0
{
internal_error!(CDnsErrorType::InternalError, "{}", std::io::Error::last_os_error());
}
let c =
CStr::from_ptr(host.as_ptr() as *const i8)
.to_str()
.map_err(|e| internal_error_map!(CDnsErrorType::InternalError, "{}", e))?;
let c2 =
match c.split_once("%")
{
Some((ip, _)) => ip,
None => c
};
let ip6: Ipv6Addr = c2.parse().map_err(|e| internal_error_map!(CDnsErrorType::InternalError, "{}", e))?;
return Ok(Some(IpAddr::from(Ipv6Addr::from(ip6))));
}
}
// next
tmp = (*tmp).ifa_next;
}
return Ok(None);
}
}