11use  crate :: errors:: SdError ; 
22use  nix:: mqueue:: mq_getattr; 
3- use  nix:: sys:: socket:: getsockname ; 
3+ use  nix:: sys:: socket:: sockopt :: SockType ; 
44use  nix:: sys:: socket:: SockAddr ; 
5+ use  nix:: sys:: socket:: { self ,  getsockname,  getsockopt} ; 
56use  nix:: sys:: stat:: fstat; 
67use  std:: convert:: TryFrom ; 
78use  std:: env; 
@@ -25,6 +26,12 @@ pub trait IsType {
2526    /// Returns true if a file descriptor is a `PF_UNIX` socket. 
2627     fn  is_unix ( & self )  -> bool ; 
2728
29+     /// Returns true if a file descriptor is a `SOCK_STREAM` socket. 
30+      fn  is_stream ( & self )  -> bool ; 
31+ 
32+     /// Returns true if a file descriptor is a `SOCK_DGRAM` socket. 
33+      fn  is_dgram ( & self )  -> bool ; 
34+ 
2835    /// Returns true if a file descriptor is a POSIX message queue descriptor. 
2936     fn  is_mq ( & self )  -> bool ; 
3037} 
@@ -45,8 +52,10 @@ enum SocketFd {
4552     Special ( RawFd ) , 
4653    /// A `PF_INET` socket, such as UDP/TCP sockets. 
4754     Inet ( RawFd ) , 
48-     /// A `PF_UNIX` socket (see `man 7 unix`). 
49-      Unix ( RawFd ) , 
55+     /// A `PF_UNIX` stream socket (see `man 7 unix`). 
56+      UnixStream ( RawFd ) , 
57+     /// A `PF_UNIX` datagram socket (see `man 7 unix`). 
58+      UnixDgram ( RawFd ) , 
5059    /// A POSIX message queue (see `man 7 mq_overview`). 
5160     Mq ( RawFd ) , 
5261    /// An unknown descriptor (possibly invalid, use with caution). 
@@ -70,7 +79,22 @@ impl IsType for FileDescriptor {
7079
7180    fn  is_unix ( & self )  -> bool  { 
7281        match  self . 0  { 
73-             SocketFd :: Unix ( _)  => true , 
82+             SocketFd :: UnixStream ( _)  => true , 
83+             SocketFd :: UnixDgram ( _)  => true , 
84+             _ => false , 
85+         } 
86+     } 
87+ 
88+     fn  is_stream ( & self )  -> bool  { 
89+         match  self . 0  { 
90+             SocketFd :: UnixStream ( _)  => true , 
91+             _ => false , 
92+         } 
93+     } 
94+ 
95+     fn  is_dgram ( & self )  -> bool  { 
96+         match  self . 0  { 
97+             SocketFd :: UnixDgram ( _)  => true , 
7498            _ => false , 
7599        } 
76100    } 
@@ -225,6 +249,16 @@ impl IsType for RawFd {
225249        } 
226250    } 
227251
252+     fn  is_stream ( & self )  -> bool  { 
253+         let  sock_type = getsockopt ( * self ,  SockType ) . expect ( "Getsockopt" ) ; 
254+         sock_type == socket:: SockType :: Stream 
255+     } 
256+ 
257+     fn  is_dgram ( & self )  -> bool  { 
258+         let  sock_type = getsockopt ( * self ,  SockType ) . expect ( "Getsockopt" ) ; 
259+         sock_type == socket:: SockType :: Datagram 
260+     } 
261+ 
228262    fn  is_mq ( & self )  -> bool  { 
229263        mq_getattr ( * self ) . is_ok ( ) 
230264    } 
@@ -240,8 +274,10 @@ impl TryFrom<RawFd> for FileDescriptor {
240274            return  Ok ( FileDescriptor ( SocketFd :: Special ( value) ) ) ; 
241275        }  else  if  value. is_inet ( )  { 
242276            return  Ok ( FileDescriptor ( SocketFd :: Inet ( value) ) ) ; 
243-         }  else  if  value. is_unix ( )  { 
244-             return  Ok ( FileDescriptor ( SocketFd :: Unix ( value) ) ) ; 
277+         }  else  if  value. is_unix ( )  && value. is_stream ( )  { 
278+             return  Ok ( FileDescriptor ( SocketFd :: UnixStream ( value) ) ) ; 
279+         }  else  if  value. is_unix ( )  && value. is_dgram ( )  { 
280+             return  Ok ( FileDescriptor ( SocketFd :: UnixDgram ( value) ) ) ; 
245281        }  else  if  value. is_mq ( )  { 
246282            return  Ok ( FileDescriptor ( SocketFd :: Mq ( value) ) ) ; 
247283        } 
@@ -261,7 +297,8 @@ impl IntoRawFd for FileDescriptor {
261297            SocketFd :: Fifo ( fd)  => fd, 
262298            SocketFd :: Special ( fd)  => fd, 
263299            SocketFd :: Inet ( fd)  => fd, 
264-             SocketFd :: Unix ( fd)  => fd, 
300+             SocketFd :: UnixStream ( fd)  => fd, 
301+             SocketFd :: UnixDgram ( fd)  => fd, 
265302            SocketFd :: Mq ( fd)  => fd, 
266303            SocketFd :: Unknown ( fd)  => fd, 
267304        } 
@@ -274,7 +311,7 @@ mod tests {
274311
275312    #[ test]  
276313    fn  test_socketype_is_unix ( )  { 
277-         let  sock = FileDescriptor ( SocketFd :: Unix ( 0i32 ) ) ; 
314+         let  sock = FileDescriptor ( SocketFd :: UnixStream ( 0i32 ) ) ; 
278315        assert ! ( sock. is_unix( ) ) ; 
279316    } 
280317
0 commit comments