Struct netlink_sys::Socket [−][src]
pub struct Socket(_);
Expand description
A netlink socket.
Example
In this example we:
- open a new socket
- send a message to the kernel
- read the reponse
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
use std::process;
// open a new socket for the NETLINK_ROUTE subsystem (see "man 7 rtnetlink")
let mut socket = Socket::new(NETLINK_ROUTE).unwrap();
// address of the remote peer we'll send a message to. This particular address is for the kernel
let kernel_addr = SocketAddr::new(0, 0);
// this is a valid message for listing the network links on the system
let pkt = vec![
0x14, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x03, 0xfd, 0xfe, 0x38, 0x5c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
];
// send the message to the kernel
let n_sent = socket.send_to(&pkt[..], &kernel_addr, 0).unwrap();
assert_eq!(n_sent, pkt.len());
// buffer for receiving the response
let mut buf = vec![0; 4096];
loop {
// receive a datagram
let (n_received, sender_addr) = socket.recv_from(&mut &mut buf[..], 0).unwrap();
assert_eq!(sender_addr, kernel_addr);
println!("received datagram {:?}", &buf[..n_received]);
if buf[4] == 2 && buf[5] == 0 {
println!("the kernel responded with an error");
return;
}
if buf[4] == 3 && buf[5] == 0 {
println!("end of dump");
return;
}
}
Implementations
Open a new socket for the given netlink subsystem. protocol
must be one of the
netlink_sys::protocols
constants.
Bind the socket to the given address
Bind the socket to an address assigned by the kernel, and return that address.
Get the socket address
Make this socket non-blocking
Connect the socket to the given address. Netlink is a connection-less protocol, so a socket can communicate with
multiple peers with the Socket::send_to
and Socket::recv_from
methods. However, if the socket only needs
to communicate with one peer, it is convenient not to have to bother with the peer address. This is what
connect
is for. After calling connect
, Socket::send
and Socket::recv
respectively send and receive
datagrams to and from remote_addr
.
Examples
In this example we:
- open a socket
- connect it to the kernel with
Socket::connect
- send a request to the kernel with
Socket::send
- read the response (which can span over several messages)
Socket::recv
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
use std::process;
let mut socket = Socket::new(NETLINK_ROUTE).unwrap();
let _ = socket.bind_auto().unwrap();
let kernel_addr = SocketAddr::new(0, 0);
socket.connect(&kernel_addr).unwrap();
// This is a valid message for listing the network links on the system
let msg = vec![
0x14, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x03, 0xfd, 0xfe, 0x38, 0x5c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
];
let n_sent = socket.send(&msg[..], 0).unwrap();
assert_eq!(n_sent, msg.len());
// buffer for receiving the response
let mut buf = vec![0; 4096];
loop {
let mut n_received = socket.recv(&mut &mut buf[..], 0).unwrap();
println!("received {:?}", &buf[..n_received]);
if buf[4] == 2 && buf[5] == 0 {
println!("the kernel responded with an error");
return;
}
if buf[4] == 3 && buf[5] == 0 {
println!("end of dump");
return;
}
}
Read a datagram from the socket and return the number of bytes that have been read and the address of the
sender. The data being read is copied into buf
. If buf
is too small, the datagram is truncated. The
supported flags are the MSG_*
described in man 2 recvmsg
Warning
In datagram oriented protocols, recv
and recvfrom
receive normally only ONE datagram, but this seems not to
be always true for netlink sockets: with some protocols like NETLINK_AUDIT
, multiple netlink packets can be
read with a single call.
For a connected socket, recv
reads a datagram from the socket. The sender is the remote peer the socket is
connected to (see Socket::connect
). See also Socket::recv_from
Receive a full message. Unlike Socket::recv_from
, which truncates messages that exceed the length of the
buffer passed as argument, this method always reads a whole message, no matter its size.
Send the given buffer buf
to the remote peer with address addr
. The supported flags are the MSG_*
values
documented in man 2 send
.
For a connected socket, send
sends the given buffer buf
to the remote peer the socket is connected to. See
also Socket::connect
and Socket::send_to
.
NETLINK_BROADCAST_ERROR
(since Linux 2.6.30). When not set, netlink_broadcast()
only
reports ESRCH
errors and silently ignore NOBUFS
errors.
NETLINK_NO_ENOBUFS
(since Linux 2.6.30). This flag can be used by unicast and broadcast
listeners to avoid receiving ENOBUFS
errors.
NETLINK_LISTEN_ALL_NSID
(since Linux 4.2). When set, this socket will receive netlink
notifications from all network namespaces that have an nsid assigned into the network
namespace where the socket has been opened. The nsid is sent to user space via an ancillary
data.
NETLINK_CAP_ACK
(since Linux 4.2). The kernel may fail to allocate the necessary room
for the acknowledgment message back to user space. This option trims off the payload of
the original netlink message. The netlink message header is still included, so the user can
guess from the sequence number which message triggered the acknowledgment.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Socket
impl UnwindSafe for Socket
Blanket Implementations
Mutably borrows from an owned value. Read more