Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active August 28, 2019 10:02
Show Gist options
  • Save LuoZijun/d60b59b28cdd5746d729fd708a2de8a3 to your computer and use it in GitHub Desktop.
Save LuoZijun/d60b59b28cdd5746d729fd708a2de8a3 to your computer and use it in GitHub Desktop.
#![allow(non_camel_case_types, non_snake_case)]
pub const ANY_SIZE: usize = 1;
pub type DWORD = u32;
pub type USHORT = u16;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct MIB_IPADDRROW_XP {
pub dwAddr: DWORD,
pub dwIndex: u32,
pub dwMask: DWORD,
pub dwBCastAddr: DWORD,
pub dwReasmSize: DWORD,
pub unused1: USHORT,
pub wType: USHORT,
}
pub type MIB_IPADDRROW = MIB_IPADDRROW_XP;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct MIB_IPADDRTABLE {
pub dwNumEntries: DWORD,
pub table: [MIB_IPADDRROW; ANY_SIZE],
}
pub struct MIB_IPADDRTABLE_ITER {
offset: usize,
len: usize,
ptr: *const MIB_IPADDRROW,
}
impl MIB_IPADDRTABLE {
pub fn iter(&self) -> MIB_IPADDRTABLE_ITER {
MIB_IPADDRTABLE_ITER {
offset: 0,
len: self.dwNumEntries as usize,
ptr: unsafe {
(self as *const Self as *const u8)
.add(std::mem::size_of::<DWORD>())
as *const MIB_IPADDRROW
}
}
}
}
impl Iterator for MIB_IPADDRTABLE_ITER {
type Item = MIB_IPADDRROW;
fn next(&mut self) -> Option<Self::Item> {
if self.offset >= self.len {
return None
}
let val: MIB_IPADDRROW_XP = unsafe { (*self.ptr) };
unsafe {
// + align ?
self.ptr = self.ptr.add(std::mem::size_of::<MIB_IPADDRROW_XP>())
}
self.offset += 1;
Some(val)
}
}
fn main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment