@@ -1003,6 +1003,12 @@ pub mod netlink {
10031003 use {
10041004 super :: { new_raw_protocol, Protocol } ,
10051005 crate :: backend:: c,
1006+ crate :: backend:: net:: read_sockaddr:: read_sockaddr_netlink,
1007+ crate :: net:: {
1008+ addr:: { call_with_sockaddr, SocketAddrArg , SocketAddrOpaque } ,
1009+ SocketAddrAny ,
1010+ } ,
1011+ core:: mem,
10061012 } ;
10071013
10081014 /// `NETLINK_UNUSED`
@@ -1112,6 +1118,83 @@ pub mod netlink {
11121118 /// `NETLINK_GET_STRICT_CHK`
11131119 #[ cfg( linux_kernel) ]
11141120 pub const GET_STRICT_CHK : Protocol = Protocol ( new_raw_protocol ( c:: NETLINK_GET_STRICT_CHK as _ ) ) ;
1121+
1122+ /// A Netlink socket address.
1123+ ///
1124+ /// Used to bind to a Netlink socket.
1125+ ///
1126+ /// Not ABI compatible with `struct sockaddr_nl`
1127+ #[ derive( Clone , Copy , PartialEq , PartialOrd , Eq , Ord , Hash , Debug ) ]
1128+ #[ cfg( linux_kernel) ]
1129+ pub struct SocketAddrNetlink {
1130+ /// Port ID
1131+ pid : u32 ,
1132+
1133+ /// Multicast groups mask
1134+ groups : u32 ,
1135+ }
1136+
1137+ #[ cfg( linux_kernel) ]
1138+ impl SocketAddrNetlink {
1139+ /// Construct a netlink address
1140+ #[ inline]
1141+ pub fn new ( pid : u32 , groups : u32 ) -> Self {
1142+ Self { pid, groups }
1143+ }
1144+
1145+ /// Return port id.
1146+ #[ inline]
1147+ pub fn pid ( & self ) -> u32 {
1148+ self . pid
1149+ }
1150+
1151+ /// Set port id.
1152+ #[ inline]
1153+ pub fn set_pid ( & mut self , pid : u32 ) {
1154+ self . pid = pid;
1155+ }
1156+
1157+ /// Return multicast groups mask.
1158+ #[ inline]
1159+ pub fn groups ( & self ) -> u32 {
1160+ self . groups
1161+ }
1162+
1163+ /// Set multicast groups mask.
1164+ #[ inline]
1165+ pub fn set_groups ( & mut self , groups : u32 ) {
1166+ self . groups = groups;
1167+ }
1168+ }
1169+
1170+ #[ cfg( linux_kernel) ]
1171+ #[ allow( unsafe_code) ]
1172+ unsafe impl SocketAddrArg for SocketAddrNetlink {
1173+ fn with_sockaddr < R > ( & self , f : impl FnOnce ( * const SocketAddrOpaque , usize ) -> R ) -> R {
1174+ let mut addr: c:: sockaddr_nl = unsafe { mem:: zeroed ( ) } ;
1175+ addr. nl_family = c:: AF_NETLINK as _ ;
1176+ addr. nl_pid = self . pid ;
1177+ addr. nl_groups = self . groups ;
1178+ call_with_sockaddr ( & addr, f)
1179+ }
1180+ }
1181+
1182+ #[ cfg( linux_kernel) ]
1183+ impl From < SocketAddrNetlink > for SocketAddrAny {
1184+ #[ inline]
1185+ fn from ( from : SocketAddrNetlink ) -> Self {
1186+ from. as_any ( )
1187+ }
1188+ }
1189+
1190+ #[ cfg( linux_kernel) ]
1191+ impl TryFrom < SocketAddrAny > for SocketAddrNetlink {
1192+ type Error = crate :: io:: Errno ;
1193+
1194+ fn try_from ( addr : SocketAddrAny ) -> Result < Self , Self :: Error > {
1195+ read_sockaddr_netlink ( & addr)
1196+ }
1197+ }
11151198}
11161199
11171200/// `ETH_P_*` constants.
0 commit comments