| 1 | use std::{io::ErrorKind, os::fd::RawFd}; |
| 2 | |
| 3 | use libc::recv; |
| 4 | use tokio::io::{unix::AsyncFd, Interest}; |
| 5 | |
| 6 | use crate::{cerr, control_message::zeroed_sockaddr_storage}; |
| 7 | |
| 8 | pub struct ChangeDetector { |
| 9 | fd: AsyncFd<RawFd>, |
| 10 | } |
| 11 | |
| 12 | impl ChangeDetector { |
| 13 | pub fn new() -> std::io::Result<Self> { |
| 14 | const _: () = assert!( |
| 15 | std::mem::size_of::<libc::sockaddr_storage>() |
| 16 | >= std::mem::size_of::<libc::sockaddr_nl>() |
| 17 | ); |
| 18 | const _: () = assert!( |
| 19 | std::mem::align_of::<libc::sockaddr_storage>() |
| 20 | >= std::mem::align_of::<libc::sockaddr_nl>() |
| 21 | ); |
| 22 | |
| 23 | let mut address_buf = zeroed_sockaddr_storage(); |
| 24 | // Safety: the above assertions guarantee that alignment and size are correct. |
| 25 | // the resulting reference won't outlast the function, and result lives the entire |
| 26 | // duration of the function |
| 27 | let address = unsafe { |
| 28 | &mut *(&mut address_buf as *mut libc::sockaddr_storage as *mut libc::sockaddr_nl) |
| 29 | }; |
| 30 | |
| 31 | address.nl_family = libc::AF_NETLINK as _; |
| 32 | address.nl_groups = |
| 33 | (libc::RTMGRP_IPV4_IFADDR | libc::RTMGRP_IPV6_IFADDR | libc::RTMGRP_LINK) as _; |
| 34 | |
| 35 | // Safety: calling socket is safe |
| 36 | let fd = |
| 37 | cerr(unsafe { libc::socket(libc::AF_NETLINK, libc::SOCK_RAW, libc::NETLINK_ROUTE) })?; |
| 38 | // Safety: address is valid for the duration of the call |
| 39 | cerr(unsafe { |
| 40 | libc::bind( |
| 41 | fd, |
| 42 | address as *mut _ as *mut _, |
| 43 | std::mem::size_of_val(address) as _, |
| 44 | ) |
| 45 | })?; |
| 46 | |
| 47 | let nonblocking = 1 as libc::c_int; |
| 48 | // Safety: nonblocking lives for the duration of the call, and is 4 bytes long as expected for FIONBIO |
| 49 | cerr(unsafe { libc::ioctl(fd, libc::FIONBIO, &nonblocking) })?; |
| 50 | |
| 51 | Ok(ChangeDetector { |
| 52 | fd: AsyncFd::new(fd)?, |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | fn empty(fd: i32) { |
| 57 | loop { |
| 58 | // Safety: buf is valid for the duration of the call, and it's length is passed as the len argument |
| 59 | let mut buf = [0u8; 16]; |
| 60 | match cerr(unsafe { |
| 61 | recv( |
| 62 | fd, |
| 63 | &mut buf as *mut _ as *mut _, |
| 64 | std::mem::size_of_val(&buf) as _, |
| 65 | 0, |
| 66 | ) as _ |
| 67 | }) { |
| 68 | Ok(_) => continue, |
| 69 | Err(e) if e.kind() == ErrorKind::WouldBlock => break, |
| 70 | Err(e) => { |
| 71 | tracing::error!("Could not receive on change socket: {}", e); |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub async fn wait_for_change(&mut self) { |
| 79 | if let Err(e) = self |
| 80 | .fd |
| 81 | .async_io(Interest::READABLE, |fd| { |
| 82 | // Safety: buf is valid for the duration of the call, and it's length is passed as the len argument |
| 83 | let mut buf = [0u8; 16]; |
| 84 | cerr(unsafe { |
| 85 | recv( |
| 86 | *fd, |
| 87 | &mut buf as *mut _ as *mut _, |
| 88 | std::mem::size_of_val(&buf) as _, |
| 89 | 0, |
| 90 | ) as _ |
| 91 | })?; |
| 92 | Self::empty(*fd); |
| 93 | Ok(()) |
| 94 | }) |
| 95 | .await |
| 96 | { |
| 97 | tracing::error!("Could not receive on change socket: {}", e); |
| 98 | } |
| 99 | } |
| 100 | } |