11//! This crate provides high level SCTP networking.
22//! Currently it only supports basic SCTP features like multi-homing
33//! in one-to-one and one-to-many associations.
4- //! SCTP notifications and working directly on associtaion is not supported yet
4+ //! SCTP notifications and working directly on association is not supported yet
55//! but is in the TODO list.
66
77extern crate sctp_sys;
@@ -20,19 +20,26 @@ use std::os::unix::io::{AsRawFd, RawFd, FromRawFd};
2020#[ cfg( target_os="windows" ) ]
2121use std:: os:: windows:: io:: { AsRawHandle , RawHandle , FromRawHandle } ;
2222
23- /// Socket buffer type
24- pub enum SoBuffer {
25- /// RCV buffer
23+ /// Socket direction
24+ pub enum SoDirection {
25+ /// RCV direction
2626 Receive ,
27- /// SND buffer
27+ /// SND direction
2828 Send
2929}
3030
31- impl SoBuffer {
32- fn optname ( & self ) -> libc:: c_int {
31+ impl SoDirection {
32+ fn buffer_opt ( & self ) -> libc:: c_int {
3333 return match * self {
34- SoBuffer :: Receive => libc:: SO_RCVBUF ,
35- SoBuffer :: Send => libc:: SO_SNDBUF
34+ SoDirection :: Receive => libc:: SO_RCVBUF ,
35+ SoDirection :: Send => libc:: SO_SNDBUF
36+ } ;
37+ }
38+
39+ fn timeout_opt ( & self ) -> libc:: c_int {
40+ return match * self {
41+ SoDirection :: Receive => libc:: SO_RCVTIMEO ,
42+ SoDirection :: Send => libc:: SO_SNDTIMEO
3643 } ;
3744 }
3845}
@@ -108,18 +115,24 @@ impl SctpStream {
108115 return Ok ( val == 1 ) ;
109116 }
110117
111- /// Set the socket buffer size for the buffer specifid by `buf `.
112- /// Linux system will double the provided size
113- pub fn set_buffer_size ( & self , buf : SoBuffer , size : usize ) -> Result < ( ) > {
114- return self . 0 . setsockopt ( libc:: SOL_SOCKET , buf . optname ( ) , & ( size as libc:: c_int ) ) ;
118+ /// Set the socket buffer size for the direction specified by `dir `.
119+ /// Linux systems will double the provided size
120+ pub fn set_buffer_size ( & self , dir : SoDirection , size : usize ) -> Result < ( ) > {
121+ return self . 0 . setsockopt ( libc:: SOL_SOCKET , dir . buffer_opt ( ) , & ( size as libc:: c_int ) ) ;
115122 }
116123
117- /// Get the socket buffer size for the buffer specifid by `buf `
118- pub fn get_buffer_size ( & self , buf : SoBuffer ) -> Result < ( usize ) > {
119- let val: u32 = try!( self . 0 . getsockopt ( libc:: SOL_SOCKET , buf . optname ( ) ) ) ;
124+ /// Get the socket buffer size for the direction specified by `dir `
125+ pub fn get_buffer_size ( & self , dir : SoDirection ) -> Result < ( usize ) > {
126+ let val: u32 = try!( self . 0 . getsockopt ( libc:: SOL_SOCKET , dir . buffer_opt ( ) ) ) ;
120127 return Ok ( val as usize ) ;
121128 }
122129
130+ /// Set `timeout` in seconds for operation `dir` (either receive or send)
131+ pub fn set_timeout ( & self , dir : SoDirection , timeout : i32 ) -> Result < ( ) > {
132+ let tval = libc:: timeval { tv_sec : timeout as libc:: c_int , tv_usec : 0 } ;
133+ return self . 0 . setsockopt ( libc:: SOL_SOCKET , dir. timeout_opt ( ) , & tval) ;
134+ }
135+
123136 /// Try to clone the SctpStream. On success, returns a new stream
124137 /// wrapping a new socket handler
125138 pub fn try_clone ( & self ) -> Result < SctpStream > {
0 commit comments