Skip to content

Commit

Permalink
Merge pull request #1 from SamuelYvon/win-support
Browse files Browse the repository at this point in the history
DRAFT: Win support
  • Loading branch information
SamuelYvon authored Mar 1, 2023
2 parents 6c4e722 + 788d072 commit b3dcd3f
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 48 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ jobs:
with:
command: build
args: --release -o dist
- uses: actions/setup-python@v4
with:
python-version: |
3.7
3.8
3.9
3.10
3.11
- name: Test wheels
run: |
pip install nox
nox
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ windows = { version = "0.42.0", features = [
"Win32_Security",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
"Win32_NetworkManagement_Ndis",
"Win32_Networking_WinSock",
"Win32_NetworkManagement_NetManagement",
"Win32_NetworkManagement_IpHelper",
"Win32_Networking"] }
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ the following is exposed:
The result will be the default gateway for each interface type. The result may be an empty dict if no default
route is set.

The level of completness differs a little bit with the original version; some address families might not yet
be available and `PEER` addresses are not reported for now. If you need a feature, open an issue and I will
do my best to add it.

### `AF_` Constants

In the previous version of `netifaces` the `AF_` constants' value were assigned
Expand Down
13 changes: 6 additions & 7 deletions netifaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
netifaces(2), netifaces reborn
See https://github.com/SamuelYvon/netifaces-2
"""

from pathlib import Path
from typing import List, cast

Expand All @@ -29,6 +28,7 @@
AF_IUCV,
AF_KCM,
AF_KEY,
AF_LINK,
AF_LLC,
AF_LOCAL,
AF_MAX,
Expand Down Expand Up @@ -115,10 +115,10 @@
"AF_XDP",
"AF_MCTP",
"AF_MAX",
"AF_LINK",
]


_ROUTE_FILE = Path("/proc/net/route")
_NIX_ROUTE_FILE = Path("/proc/net/route")


def interfaces() -> List[InterfaceName]:
Expand All @@ -139,13 +139,14 @@ def ifaddresses(if_name: str) -> Addresses:
:return a map of network addresses indexed by network address type.
The values are the addresses, indexed by their roles
"""

return cast(Addresses, _ifaddresses(if_name))


def _parse_route_file() -> GatewaysTable:
from .routes import routes_parse

route_content = _ROUTE_FILE.read_text()
route_content = _NIX_ROUTE_FILE.read_text()
return routes_parse(route_content)


Expand All @@ -156,13 +157,11 @@ def gateways() -> GatewaysTable:
:return a routing table
"""

if _ROUTE_FILE.exists():
if _NIX_ROUTE_FILE.exists():
return _parse_route_file()
else:
raise NotImplementedError("No implementation for `gateways()` yet")

return {}


def default_gateway() -> DefaultGatewayEntry:
"""
Expand Down
2 changes: 2 additions & 0 deletions netifaces/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
AF_XDP = 44
AF_MCTP = 45
AF_MAX = 46
AF_LINK = -1000 # Windows Link Layer as defined by netifaces(1)


class InterfaceType(IntEnum):
Expand Down Expand Up @@ -104,6 +105,7 @@ class InterfaceType(IntEnum):
AF_XDP = AF_XDP
AF_MCTP = AF_MCTP
AF_MAX = AF_MAX
AF_LINK = AF_LINK


InterfaceName = str
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#![allow(non_snake_case)]

extern crate core;

use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use std::fmt::Write;

mod types;

#[cfg(not(target_family = "windows"))]
mod linux;

#[cfg(not(target_family = "windows"))]
use linux::{linux_ifaddresses as ifaddresses, linux_interfaces as interfaces};

#[cfg(target_family = "windows")]
mod win;

#[cfg(target_family = "windows")]
use win::{windows_ifaddresses as ifaddresses, windows_interfaces as interfaces};

/// Given an u32 in little endian, return the String representation
/// of it into the colloquial IPV4 string format
pub fn ip_to_string(ip: u32) -> String {
let mut s = String::new();

Expand All @@ -30,6 +38,21 @@ pub fn ip_to_string(ip: u32) -> String {
s
}

/// Given the bytes that makes up a mac address, return the String
/// representation as it would be expected in the colloquial form.
pub fn mac_to_string(mac: &[u8; 8]) -> String {
let mut s = String::new();

for i in 0..mac.len() {
write!(&mut s, "{:X?}", mac[i]).unwrap();
if i + 1 < mac.len() {
s.push(':');
}
}

s
}

#[pyfunction]
pub fn _ip_to_string(ip: u32) -> String {
ip_to_string(ip)
Expand Down
4 changes: 2 additions & 2 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn add_to_types_mat(
af_class: u8,
addr: &dyn Display,
class: &str,
types_mat: &mut HashMap<u32, Vec<AddrPairs>>,
types_mat: &mut HashMap<i32, Vec<AddrPairs>>,
any: &mut bool,
) {
let e = types_mat.entry(af_class.into()).or_insert_with(Vec::new);
Expand All @@ -38,7 +38,7 @@ fn add_to_types_mat(
}

pub fn linux_ifaddresses(if_name: &str) -> Result<IfAddrs, Box<dyn std::error::Error>> {
let mut types_mat: HashMap<u32, Vec<AddrPairs>> = HashMap::new();
let mut types_mat: HashMap<i32, Vec<AddrPairs>> = HashMap::new();
let if_addrs = nix::ifaddrs::getifaddrs()?;

for if_addr in if_addrs {
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::HashMap;

pub type AddrPairs = HashMap<String, String>;
pub type IfAddrs = HashMap<u32, Vec<AddrPairs>>;
pub type IfAddrs = HashMap<i32, Vec<AddrPairs>>;

pub const ADDR_ADDR: &str = "addr";
pub const MASK_ADDR: &str = "mask";
Expand Down
Loading

0 comments on commit b3dcd3f

Please sign in to comment.