Struct netlink_sys::SocketAddr

source ·
pub struct SocketAddr(/* private fields */);
Expand description

The address of a netlink socket

A netlink address is made of two parts: the unicast address of the socket, called port number or PID, and the multicast address called group ID. In this library, we’ve chosen to stick to the “port number” terminology, since PID can be confused with process ID. However, the netlink man page mostly uses PID.

§Port number

Sockets in kernel space have 0 as a port number. For sockets opened by a user-space process, the port number can either be assigned by the process itself, or by the kernel. The only constraint is that this port number must be unique: two netlink sockets created by a given process must have a different port number. However, netlinks sockets created by different processes can have the same port number.

§Port number assigned by the kernel

One way to set the port number is to let the kernel assign it, by calling Socket::bind with a port number set to 0. The kernel will usually use the process ID as port number for the first netlink socket created by the process, which is why the socket port number is also called PID. For example:

use std::process;
use netlink_sys::{
    protocols::NETLINK_ROUTE,
    SocketAddr, Socket,
};

let mut socket = Socket::new(NETLINK_ROUTE).unwrap();
// The first parameter is the port number. By setting it to 0 we ask the kernel to pick a port for us
let mut addr = SocketAddr::new(0, 0);
socket.bind(&addr).unwrap();
// Retrieve the socket address
socket.get_address(&mut addr).unwrap();
// the socket port number should be equal to the process ID, but there is no guarantee
println!("socket port number = {}, process ID = {}", addr.port_number(), process::id());

let mut socket2 = Socket::new(NETLINK_ROUTE).unwrap();
let mut addr2 = SocketAddr::new(0, 0);
socket2.bind(&addr2).unwrap();
socket2.get_address(&mut addr2).unwrap();
// the unicast address picked by the kernel for the second socket should be different
assert!(addr.port_number() != addr2.port_number());

Note that it’s a little tedious to create a socket address, call bind and then retrive the address with Socket::get_address. To avoid this boilerplate you can use Socket::bind_auto:

use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
use std::process;

let mut socket = Socket::new(NETLINK_ROUTE).unwrap();
let addr = socket.bind_auto().unwrap();
println!("socket port number = {}", addr.port_number());

§Setting the port number manually

The application can also pick the port number by calling Socket::bind with an address with a non-zero port number. However, it must ensure that this number is unique for each socket created. For instance:

use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};
use std::process;

let mut socket = Socket::new(NETLINK_ROUTE).unwrap();
// set the socket port number to 2
let mut addr = SocketAddr::new(2, 0);
socket.bind(&addr).unwrap();
// Retrieve the socket address
socket.get_address(&mut addr).unwrap();
assert_eq!(2, addr.port_number());

// Creating a second socket with the same port number fails
let mut socket2 = Socket::new(NETLINK_ROUTE).unwrap();
let mut addr2 = SocketAddr::new(2, 0);
socket2.bind(&addr2).unwrap_err();

Implementations§

source§

impl SocketAddr

source

pub fn new(port_number: u32, multicast_groups: u32) -> Self

Create a new socket address for with th

source

pub fn port_number(&self) -> u32

Get the unicast address of this socket

source

pub fn multicast_groups(&self) -> u32

Get the multicast groups of this socket

Trait Implementations§

source§

impl Clone for SocketAddr

source§

fn clone(&self) -> SocketAddr

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for SocketAddr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for SocketAddr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for SocketAddr

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for SocketAddr

source§

fn eq(&self, other: &SocketAddr) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for SocketAddr

source§

impl Eq for SocketAddr

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.