Skip to content

Commit

Permalink
libc::sa_family_t issues for linux and macos
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Oct 10, 2023
1 parent 89293ef commit 3222828
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
21 changes: 3 additions & 18 deletions src/platform/posix/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,19 @@
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.

use std::mem;
use std::net::Ipv4Addr;
use std::ptr;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use libc::c_uchar;
#[cfg(any(target_os = "linux", target_os = "android"))]
use libc::c_ushort;

use libc::AF_INET as _AF_INET;
use libc::{in_addr, sockaddr, sockaddr_in};
use std::{mem, net::Ipv4Addr, ptr};

use crate::error::*;

/// A wrapper for `sockaddr_in`.
#[derive(Copy, Clone)]
pub struct SockAddr(sockaddr_in);

#[cfg(any(target_os = "linux", target_os = "android"))]
const AF_INET: c_ushort = _AF_INET as c_ushort;

#[cfg(any(target_os = "macos", target_os = "ios"))]
const AF_INET: c_uchar = _AF_INET as c_uchar;

impl SockAddr {
/// Create a new `SockAddr` from a generic `sockaddr`.
pub fn new(value: &sockaddr) -> Result<Self> {
if value.sa_family != AF_INET {
if value.sa_family != libc::AF_INET as libc::sa_family_t {
return Err(Error::InvalidAddress);
}

Expand All @@ -64,7 +49,7 @@ impl From<Ipv4Addr> for SockAddr {
let octets = ip.octets();
let mut addr = unsafe { mem::zeroed::<sockaddr_in>() };

addr.sin_family = AF_INET;
addr.sin_family = libc::AF_INET as libc::sa_family_t;
addr.sin_port = 0;
addr.sin_addr = in_addr {
s_addr: u32::from_ne_bytes(octets),
Expand Down
2 changes: 1 addition & 1 deletion src/platform/windows/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Device {

let address = config.address.unwrap_or(Ipv4Addr::new(10, 1, 0, 2));
let mask = config.netmask.unwrap_or(Ipv4Addr::new(255, 255, 255, 0));
let gateway = config.destination.map_or(None, |a| Some(IpAddr::V4(a)));
let gateway = config.destination.map(IpAddr::from);
adapter.set_network_addresses_tuple(IpAddr::V4(address), IpAddr::V4(mask), gateway)?;
let mtu = config.mtu.unwrap_or(1500) as usize;

Expand Down

0 comments on commit 3222828

Please sign in to comment.