// Copyright (C) 2019 - Will Glozer. All rights reserved. use anyhow::Result; use nell::{Family, Message, Netlink, Socket}; use nell::api::*; use nell::ffi::*; fn main() -> Result<()> { let mut sock = Socket::new(Family::ROUTE)?; let groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR; sock.bind(0, groups)?; let mut msg = Message::<()>::new(NLMSG_DONE); msg.set_flags(NLM_F_ACK); sock.send(&msg)?; assert!(sock.recv::<()>()? == Netlink::Ack); while let Netlink::Msg(msg) = sock.recv::<()>()? { match (msg.nlmsg_type(), msg.any()) { (RTM_NEWLINK, Any::IFInfo(msg)) => println!("+ {:?}", link(&msg)?), (RTM_DELLINK, Any::IFInfo(msg)) => println!("- {:?}", link(&msg)?), (RTM_NEWADDR, Any::IFAddr(msg)) => println!("+ {:?}", addr(&msg)?), (RTM_DELADDR, Any::IFAddr(msg)) => println!("- {:?}", addr(&msg)?), (RTM_NEWNEIGH, Any::Peer(msg)) => println!("+ {:?}", peer(&msg)?), (RTM_DELNEIGH, Any::Peer(msg)) => println!("- {:?}", peer(&msg)?), (RTM_NEWROUTE, Any::Route(msg)) => println!("+ {:?}", route(&msg)?), (RTM_DELROUTE, Any::Route(msg)) => println!("- {:?}", route(&msg)?), other => println!("unexpected: {:?}", other), } } Ok(()) }