1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SPDX-License-Identifier: MIT
use ;
/// 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`][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:
///
/// ```rust
/// 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`][get_addr]. To avoid
/// this boilerplate you can use [`Socket::bind_auto`][bind_auto]:
///
/// ```rust
/// 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:
///
/// ```rust
/// 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();
/// ```
///
/// [bind]: crate::Socket::bind
/// [bind_auto]: crate::Socket::bind_auto
/// [get_addr]: crate::Socket::get_address
sockaddr_nl);