Fix interface address get/set

This commit is contained in:
Dennis Schwerdel 2022-09-23 23:05:22 +02:00
parent 2906ca5f40
commit ca4ad2769d
2 changed files with 32 additions and 9 deletions

View File

@ -8,6 +8,7 @@
- [x] Sync traffic stats - [x] Sync traffic stats
- [x] Sync forwarding table - [x] Sync forwarding table
- [ ] Fix WS Proxy code - [ ] Fix WS Proxy code
- [ ] Fix Ctrl-C
- [x] Fix auto-claim IP
## REST API ## REST API

View File

@ -3,18 +3,38 @@
// This software is licensed under GPL-3 or newer (see LICENSE.md) // This software is licensed under GPL-3 or newer (see LICENSE.md)
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{cmp, collections::VecDeque, convert::TryInto, fmt, fs::{self, File}, io::{self, Cursor, Read, Write, Error as IoError, BufReader, BufRead}, net::{Ipv4Addr, UdpSocket}, os::unix::io::AsRawFd, str, str::FromStr, sync::Arc, time::Duration}; use std::{
cmp,
collections::VecDeque,
convert::TryInto,
fmt,
fs::{self, File},
io::{self, BufRead, BufReader, Cursor, Error as IoError, Read, Write},
net::{Ipv4Addr, UdpSocket},
os::unix::io::AsRawFd,
str,
str::FromStr,
sync::Arc,
time::Duration,
};
use timeout_io::Reader; use timeout_io::Reader;
use crate::{crypto, error::Error, util::MsgBuffer}; use crate::{crypto, error::Error, util::MsgBuffer};
static TUNSETIFF: libc::c_ulong = 1074025674; static TUNSETIFF: libc::c_ulong = 1074025674;
#[repr(C)]
#[derive(Copy, Clone)]
struct IfReqDataAddr {
af: libc::c_int,
addr: Ipv4Addr
}
#[repr(C)] #[repr(C)]
union IfReqData { union IfReqData {
flags: libc::c_short, flags: libc::c_short,
value: libc::c_int, value: libc::c_int,
addr: (libc::c_short, Ipv4Addr), addr: IfReqDataAddr,
_dummy: [u8; 24], _dummy: [u8; 24],
} }
@ -382,11 +402,11 @@ fn get_device_addr(ifname: &str) -> io::Result<Ipv4Addr> {
let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCGIFADDR.try_into().unwrap(), &mut ifreq) }; let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCGIFADDR.try_into().unwrap(), &mut ifreq) };
match res { match res {
0 => { 0 => {
let af = unsafe { ifreq.data.addr.0 }; let af = unsafe { ifreq.data.addr.af };
if af as libc::c_int != libc::AF_INET { if af as libc::c_int != libc::AF_INET {
return Err(io::Error::new(io::ErrorKind::AddrNotAvailable, "Invalid address family".to_owned())); return Err(io::Error::new(io::ErrorKind::AddrNotAvailable, "Invalid address family".to_owned()));
} }
let ip = unsafe { ifreq.data.addr.1 }; let ip = unsafe { ifreq.data.addr.addr };
Ok(ip) Ok(ip)
} }
_ => Err(IoError::last_os_error()), _ => Err(IoError::last_os_error()),
@ -397,7 +417,8 @@ fn get_device_addr(ifname: &str) -> io::Result<Ipv4Addr> {
fn set_device_addr(ifname: &str, addr: Ipv4Addr) -> io::Result<()> { fn set_device_addr(ifname: &str, addr: Ipv4Addr) -> io::Result<()> {
let sock = UdpSocket::bind("0.0.0.0:0")?; let sock = UdpSocket::bind("0.0.0.0:0")?;
let mut ifreq = IfReq::new(ifname); let mut ifreq = IfReq::new(ifname);
ifreq.data.addr = (libc::AF_INET as libc::c_short, addr); ifreq.data.addr.af = libc::AF_INET as libc::c_int;
ifreq.data.addr.addr = addr;
let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCSIFADDR.try_into().unwrap(), &mut ifreq) }; let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCSIFADDR.try_into().unwrap(), &mut ifreq) };
match res { match res {
0 => Ok(()), 0 => Ok(()),
@ -413,11 +434,11 @@ fn get_device_netmask(ifname: &str) -> io::Result<Ipv4Addr> {
let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCGIFNETMASK.try_into().unwrap(), &mut ifreq) }; let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCGIFNETMASK.try_into().unwrap(), &mut ifreq) };
match res { match res {
0 => { 0 => {
let af = unsafe { ifreq.data.addr.0 }; let af = unsafe { ifreq.data.addr.af };
if af as libc::c_int != libc::AF_INET { if af as libc::c_int != libc::AF_INET {
return Err(io::Error::new(io::ErrorKind::AddrNotAvailable, "Invalid address family".to_owned())); return Err(io::Error::new(io::ErrorKind::AddrNotAvailable, "Invalid address family".to_owned()));
} }
let ip = unsafe { ifreq.data.addr.1 }; let ip = unsafe { ifreq.data.addr.addr };
Ok(ip) Ok(ip)
} }
_ => Err(IoError::last_os_error()), _ => Err(IoError::last_os_error()),
@ -428,7 +449,8 @@ fn get_device_netmask(ifname: &str) -> io::Result<Ipv4Addr> {
fn set_device_netmask(ifname: &str, addr: Ipv4Addr) -> io::Result<()> { fn set_device_netmask(ifname: &str, addr: Ipv4Addr) -> io::Result<()> {
let sock = UdpSocket::bind("0.0.0.0:0")?; let sock = UdpSocket::bind("0.0.0.0:0")?;
let mut ifreq = IfReq::new(ifname); let mut ifreq = IfReq::new(ifname);
ifreq.data.addr = (libc::AF_INET as libc::c_short, addr); ifreq.data.addr.af = libc::AF_INET as libc::c_int;
ifreq.data.addr.addr = addr;
let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCSIFNETMASK.try_into().unwrap(), &mut ifreq) }; let res = unsafe { libc::ioctl(sock.as_raw_fd(), libc::SIOCSIFNETMASK.try_into().unwrap(), &mut ifreq) };
match res { match res {
0 => Ok(()), 0 => Ok(()),