Skip to content

Commit

Permalink
Added more methods to SctpEndpoint and SctpListener
Browse files Browse the repository at this point in the history
  • Loading branch information
pierresy committed Aug 2, 2015
1 parent 2ae0cf8 commit 6aaf1b8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn main() {
match SctpListener::bindx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
Ok(serv) => {
println!("bound to {:?}", serv.local_addrs().unwrap());
// serv.set_timeout(5).unwrap();
match serv.accept() {
Err(e) => println!("{:?}", e.kind()),
Ok((peer, _)) => {
Expand Down
53 changes: 48 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This crate provides high level SCTP networking.
//! Currently it only supports basic SCTP features like multi-homing
//! in one-to-one and one-to-many associations.
//! SCTP notifications and working directly on association is not supported yet
//! SCTP notifications and working directly on associations is not supported yet
//! but is in the TODO list.
extern crate sctp_sys;
Expand Down Expand Up @@ -129,7 +129,7 @@ impl SctpStream {

/// Set `timeout` in seconds for operation `dir` (either receive or send)
pub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()> {
// Workaround: Use onf long instead of time_t which does not compile in windows x86_64
// Workaround: Use of long instead of libc::time_t which does not compile in windows x86_64
let tval = libc::timeval { tv_sec: timeout as libc::c_long, tv_usec: 0 };
return self.0.setsockopt(libc::SOL_SOCKET, dir.timeout_opt(), &tval);
}
Expand Down Expand Up @@ -229,11 +229,47 @@ impl SctpEndpoint {
return self.0.sendmsg(msg, Some(address), stream, 0);
}

/// Get local socket addresses on which this socket is bound
/// Get local socket addresses to which this socket is bound
pub fn local_addrs(&self) -> Result<Vec<SocketAddr>> {
return self.0.local_addrs(0);
}

/// Shuts down the read, write, or both halves of this connection
pub fn shutdown(&self, how: Shutdown) -> Result<()> {
return self.0.shutdown(how);
}

/// Set or unset SCTP_NODELAY option
pub fn set_nodelay(&self, nodelay: bool) -> Result<()> {
let val: libc::c_int = if nodelay { 1 } else { 0 };
return self.0.setsockopt(SOL_SCTP, sctp_sys::SCTP_NODELAY, &val);
}

/// Verify if SCTP_NODELAY option is activated for this socket
pub fn has_nodelay(&self) -> Result<bool> {
let val: libc::c_int = try!(self.0.sctp_opt_info(sctp_sys::SCTP_NODELAY, 0));
return Ok(val == 1);
}

/// Set the socket buffer size for the direction specified by `dir`.
/// Linux systems will double the provided size
pub fn set_buffer_size(&self, dir: SoDirection, size: usize) -> Result<()> {
return self.0.setsockopt(libc::SOL_SOCKET, dir.buffer_opt(), &(size as libc::c_int));
}

/// Get the socket buffer size for the direction specified by `dir`
pub fn get_buffer_size(&self, dir: SoDirection) -> Result<(usize)> {
let val: u32 = try!(self.0.getsockopt(libc::SOL_SOCKET, dir.buffer_opt()));
return Ok(val as usize);
}

/// Set `timeout` in seconds for operation `dir` (either receive or send)
pub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()> {
// Workaround: Use of long instead of libc::time_t which does not compile in windows x86_64
let tval = libc::timeval { tv_sec: timeout as libc::c_long, tv_usec: 0 };
return self.0.setsockopt(libc::SOL_SOCKET, dir.timeout_opt(), &tval);
}

/// Try to clone this socket
pub fn try_clone(&self) -> Result<SctpEndpoint> {
return Ok(SctpEndpoint(try!(self.0.try_clone())));
Expand Down Expand Up @@ -321,16 +357,23 @@ impl SctpListener {
return Ok((SctpStream(sock), addr));
}

/// Iterate over new connections.
/// Iterate over new connections
pub fn incoming(&self) -> Incoming {
return Incoming(self);
}

/// Get the listener ocal addresses
/// Get the listener local addresses
pub fn local_addrs(&self) -> Result<Vec<SocketAddr>> {
return self.0.local_addrs(0);
}

/// Set `timeout` in seconds on accept
pub fn set_timeout(&self, timeout: i32) -> Result<()> {
// Workaround: Use of long instead of libc::time_t which does not compile in windows x86_64
let tval = libc::timeval { tv_sec: timeout as libc::c_long, tv_usec: 0 };
return self.0.setsockopt(libc::SOL_SOCKET, libc::SO_RCVTIMEO, &tval);
}

/// Try to clone this listener
pub fn try_clone(&self) -> Result<SctpListener> {
return Ok(SctpListener(try!(self.0.try_clone())));
Expand Down
2 changes: 1 addition & 1 deletion src/sctpsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl RawSocketAddr for SocketAddr {
}


/// An SCTP socket, of any kind
/// A High level wrapper around SCTP socket, of any kind
pub struct SctpSocket(SOCKET);

impl SctpSocket {
Expand Down

0 comments on commit 6aaf1b8

Please sign in to comment.