4
4
use { Error , Result } ;
5
5
use errno:: Errno ;
6
6
use features;
7
- use libc:: { self , c_void, c_int, socklen_t, size_t, pid_t , uid_t , gid_t } ;
8
- use std:: { mem, ptr, slice} ;
7
+ use libc:: { self , c_void, c_int, socklen_t, size_t} ;
8
+ use std:: { fmt , mem, ptr, slice} ;
9
9
use std:: os:: unix:: io:: RawFd ;
10
10
use sys:: time:: TimeVal ;
11
11
use sys:: uio:: IoVec ;
12
12
13
13
mod addr;
14
14
mod ffi;
15
- mod multicast;
16
15
pub mod sockopt;
17
16
18
17
/*
@@ -34,22 +33,14 @@ pub use self::addr::{
34
33
pub use :: sys:: socket:: addr:: netlink:: NetlinkAddr ;
35
34
36
35
pub use libc:: {
37
- in_addr,
38
- in6_addr,
36
+ sa_family_t,
39
37
sockaddr,
40
38
sockaddr_in,
41
39
sockaddr_in6,
40
+ sockaddr_storage,
42
41
sockaddr_un,
43
- sa_family_t,
44
- } ;
45
-
46
- pub use self :: multicast:: {
47
- ip_mreq,
48
- ipv6_mreq,
49
42
} ;
50
43
51
- pub use libc:: sockaddr_storage;
52
-
53
44
/// These constants are used to specify the communication semantics
54
45
/// when creating a socket with [`socket()`](fn.socket.html)
55
46
#[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
@@ -77,6 +68,7 @@ pub enum SockType {
77
68
/// Constants used in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
78
69
/// to specify the protocol to use.
79
70
#[ repr( i32 ) ]
71
+ #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
80
72
pub enum SockProtocol {
81
73
/// TCP protocol ([ip(7)](http://man7.org/linux/man-pages/man7/ip.7.html))
82
74
Tcp = libc:: IPPROTO_TCP ,
@@ -174,6 +166,121 @@ libc_bitflags!{
174
166
}
175
167
}
176
168
169
+ cfg_if ! {
170
+ if #[ cfg( all( target_os = "linux" , not( target_arch = "arm" ) ) ) ] {
171
+ /// Unix credentials of the sending process.
172
+ ///
173
+ /// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets.
174
+ #[ repr( C ) ]
175
+ #[ derive( Clone , Copy ) ]
176
+ pub struct UnixCredentials ( libc:: ucred) ;
177
+
178
+ impl UnixCredentials {
179
+ /// Returns the process identifier
180
+ pub fn pid( & self ) -> libc:: pid_t {
181
+ self . 0 . pid
182
+ }
183
+
184
+ /// Returns the user identifier
185
+ pub fn uid( & self ) -> libc:: uid_t {
186
+ self . 0 . uid
187
+ }
188
+
189
+ /// Returns the group identifier
190
+ pub fn gid( & self ) -> libc:: gid_t {
191
+ self . 0 . gid
192
+ }
193
+ }
194
+
195
+ impl PartialEq for UnixCredentials {
196
+ fn eq( & self , other: & Self ) -> bool {
197
+ self . 0 . pid == other. 0 . pid && self . 0 . uid == other. 0 . uid && self . 0 . gid == other. 0 . gid
198
+ }
199
+ }
200
+ impl Eq for UnixCredentials { }
201
+
202
+ impl fmt:: Debug for UnixCredentials {
203
+ fn fmt( & self , f: & mut fmt:: Formatter ) -> fmt:: Result {
204
+ f. debug_struct( "UnixCredentials" )
205
+ . field( "pid" , & self . 0 . pid)
206
+ . field( "uid" , & self . 0 . uid)
207
+ . field( "gid" , & self . 0 . gid)
208
+ . finish( )
209
+ }
210
+ }
211
+ }
212
+ }
213
+
214
+ /// Request for multicast socket operations
215
+ ///
216
+ /// This is a wrapper type around `ip_mreq`.
217
+ #[ repr( C ) ]
218
+ #[ derive( Clone , Copy ) ]
219
+ pub struct IpMembershipRequest ( libc:: ip_mreq ) ;
220
+
221
+ impl IpMembershipRequest {
222
+ /// Instantiate a new `IpMembershipRequest`
223
+ ///
224
+ /// If `interface` is `None`, then `Ipv4Addr::any()` will be used for the interface.
225
+ pub fn new ( group : Ipv4Addr , interface : Option < Ipv4Addr > ) -> Self {
226
+ IpMembershipRequest ( libc:: ip_mreq {
227
+ imr_multiaddr : group. 0 ,
228
+ imr_interface : interface. unwrap_or ( Ipv4Addr :: any ( ) ) . 0 ,
229
+ } )
230
+ }
231
+ }
232
+
233
+ impl PartialEq for IpMembershipRequest {
234
+ fn eq ( & self , other : & Self ) -> bool {
235
+ self . 0 . imr_multiaddr . s_addr == other. 0 . imr_multiaddr . s_addr
236
+ && self . 0 . imr_interface . s_addr == other. 0 . imr_interface . s_addr
237
+ }
238
+ }
239
+ impl Eq for IpMembershipRequest { }
240
+
241
+ impl fmt:: Debug for IpMembershipRequest {
242
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
243
+ f. debug_struct ( "IpMembershipRequest" )
244
+ . field ( "imr_multiaddr" , & self . 0 . imr_multiaddr . s_addr )
245
+ . field ( "imr_interface" , & self . 0 . imr_interface . s_addr )
246
+ . finish ( )
247
+ }
248
+ }
249
+
250
+ /// Request for ipv6 multicast socket operations
251
+ ///
252
+ /// This is a wrapper type around `ipv6_mreq`.
253
+ #[ repr( C ) ]
254
+ #[ derive( Clone , Copy ) ]
255
+ pub struct Ipv6MembershipRequest ( libc:: ipv6_mreq ) ;
256
+
257
+ impl Ipv6MembershipRequest {
258
+ /// Instantiate a new `Ipv6MembershipRequest`
259
+ pub fn new ( group : Ipv6Addr ) -> Self {
260
+ Ipv6MembershipRequest ( libc:: ipv6_mreq {
261
+ ipv6mr_multiaddr : group. 0 ,
262
+ ipv6mr_interface : 0 ,
263
+ } )
264
+ }
265
+ }
266
+
267
+ impl PartialEq for Ipv6MembershipRequest {
268
+ fn eq ( & self , other : & Self ) -> bool {
269
+ self . 0 . ipv6mr_multiaddr . s6_addr == other. 0 . ipv6mr_multiaddr . s6_addr &&
270
+ self . 0 . ipv6mr_interface == other. 0 . ipv6mr_interface
271
+ }
272
+ }
273
+ impl Eq for Ipv6MembershipRequest { }
274
+
275
+ impl fmt:: Debug for Ipv6MembershipRequest {
276
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
277
+ f. debug_struct ( "Ipv6MembershipRequest" )
278
+ . field ( "ipv6mr_multiaddr" , & self . 0 . ipv6mr_multiaddr . s6_addr )
279
+ . field ( "ipv6mr_interface" , & self . 0 . ipv6mr_interface )
280
+ . finish ( )
281
+ }
282
+ }
283
+
177
284
/// Copy the in-memory representation of src into the byte slice dst,
178
285
/// updating the slice to point to the remainder of dst only. Unsafe
179
286
/// because it exposes all bytes in src, which may be UB if some of them
@@ -799,21 +906,6 @@ pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result<usize> {
799
906
Errno :: result ( ret) . map ( |r| r as usize )
800
907
}
801
908
802
- #[ repr( C ) ]
803
- #[ derive( Clone , Copy , Debug ) ]
804
- pub struct linger {
805
- pub l_onoff : c_int ,
806
- pub l_linger : c_int
807
- }
808
-
809
- #[ repr( C ) ]
810
- #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
811
- pub struct ucred {
812
- pid : pid_t ,
813
- uid : uid_t ,
814
- gid : gid_t ,
815
- }
816
-
817
909
/*
818
910
*
819
911
* ===== Socket Options =====
@@ -825,13 +917,14 @@ pub struct ucred {
825
917
///
826
918
/// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html)
827
919
#[ repr( i32 ) ]
920
+ #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
828
921
pub enum SockLevel {
829
922
Socket = libc:: SOL_SOCKET ,
830
923
Tcp = libc:: IPPROTO_TCP ,
831
924
Ip = libc:: IPPROTO_IP ,
832
925
Ipv6 = libc:: IPPROTO_IPV6 ,
833
926
Udp = libc:: IPPROTO_UDP ,
834
- #[ cfg( any( target_os = "linux " , target_os = "android " ) ) ]
927
+ #[ cfg( any( target_os = "android " , target_os = "linux " ) ) ]
835
928
Netlink = libc:: SOL_NETLINK ,
836
929
}
837
930
@@ -938,7 +1031,7 @@ pub unsafe fn sockaddr_storage_to_addr(
938
1031
}
939
1032
940
1033
941
- #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
1034
+ #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
942
1035
pub enum Shutdown {
943
1036
/// Further receptions will be disallowed.
944
1037
Read ,
0 commit comments