From bbc24ea90eb9814db71a1297c4deb4eef91f9c2d Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 20 Aug 2014 17:55:44 -0700 Subject: Start binding Darwin --- src/sys/socket.rs | 202 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 136 insertions(+), 66 deletions(-) (limited to 'src/sys/socket.rs') diff --git a/src/sys/socket.rs b/src/sys/socket.rs index 9f523ef3..46db7ab4 100644 --- a/src/sys/socket.rs +++ b/src/sys/socket.rs @@ -1,12 +1,12 @@ -#![cfg(target_os = "linux")] - use std::{mem, ptr}; -use libc::{c_int, c_void, sockaddr, socklen_t}; +use libc::{c_int, socklen_t}; use fcntl::Fd; use errno::{SysResult, SysError, from_ffi}; pub use libc::{in_addr, sockaddr_in, sockaddr_in6, sockaddr_un, sa_family_t}; +pub use self::consts::*; + mod ffi { use libc::{c_int, c_void, sockaddr, socklen_t}; pub use libc::{socket, listen, bind, accept, connect, setsockopt}; @@ -27,22 +27,7 @@ mod ffi { } } -pub type AddressFamily = c_int; - -pub static AF_UNIX: AddressFamily = 1; -pub static AF_LOCAL: AddressFamily = AF_UNIX; -pub static AF_INET: AddressFamily = 2; -pub static AF_INET6: AddressFamily = 10; - -pub type SockType = c_int; - -pub static SOCK_STREAM: SockType = 1; -pub static SOCK_DGRAM: SockType = 1; -pub static SOCK_SEQPACKET: SockType = 1; -pub static SOCK_RAW: SockType = 1; -pub static SOCK_RDM: SockType = 1; - -// Extra flags - Linux 2.6.27 +// Extra flags - Supported by Linux 2.6.27, normalized on other platforms bitflags!( flags SockFlag: c_int { static SOCK_NONBLOCK = 0o0004000, @@ -50,6 +35,138 @@ bitflags!( } ) +pub enum SockAddr { + SockIpV4(sockaddr_in), + SockIpV6(sockaddr_in6), + SockUnix(sockaddr_un) +} + +#[cfg(target_os = "linux")] +mod consts { + use libc::{c_int}; + + pub type AddressFamily = c_int; + + pub static AF_UNIX: AddressFamily = 1; + pub static AF_LOCAL: AddressFamily = AF_UNIX; + pub static AF_INET: AddressFamily = 2; + pub static AF_INET6: AddressFamily = 10; + + pub type SockType = c_int; + + pub static SOCK_STREAM: SockType = 1; + pub static SOCK_DGRAM: SockType = 2; + pub static SOCK_SEQPACKET: SockType = 5; + pub static SOCK_RAW: SockType = 3; + pub static SOCK_RDM: SockType = 4; + + pub type SockLevel = c_int; + + pub static SOL_IP: SockLevel = 0; + pub static SOL_SOCKET: SockLevel = 1; + pub static SOL_TCP: SockLevel = 6; + pub static SOL_UDP: SockLevel = 17; + pub static SOL_IPV6: SockLevel = 41; + + pub type SockOpt = c_int; + + pub static SO_ACCEPTCONN: SockOpt = 30; + pub static SO_BINDTODEVICE: SockOpt = 25; + pub static SO_BROADCAST: SockOpt = 6; + pub static SO_BSDCOMPAT: SockOpt = 14; + pub static SO_DEBUG: SockOpt = 1; + pub static SO_DOMAIN: SockOpt = 39; + pub static SO_ERROR: SockOpt = 4; + pub static SO_DONTROUTE: SockOpt = 5; + pub static SO_KEEPALIVE: SockOpt = 9; + pub static SO_LINGER: SockOpt = 13; + pub static SO_MARK: SockOpt = 36; + pub static SO_OOBINLINE: SockOpt = 10; + pub static SO_PASSCRED: SockOpt = 16; + pub static SO_PEEK_OFF: SockOpt = 42; + pub static SO_PEERCRED: SockOpt = 17; + pub static SO_PRIORITY: SockOpt = 12; + pub static SO_PROTOCOL: SockOpt = 38; + pub static SO_RCVBUF: SockOpt = 8; + pub static SO_RCVBUFFORCE: SockOpt = 33; + pub static SO_RCVLOWAT: SockOpt = 18; + pub static SO_SNDLOWAT: SockOpt = 19; + pub static SO_RCVTIMEO: SockOpt = 20; + pub static SO_SNDTIMEO: SockOpt = 21; + pub static SO_REUSEADDR: SockOpt = 2; + pub static SO_RXQ_OVFL: SockOpt = 40; + pub static SO_SNDBUF: SockOpt = 7; + pub static SO_SNDBUFFORCE: SockOpt = 32; + pub static SO_TIMESTAMP: SockOpt = 29; + pub static SO_TYPE: SockOpt = 3; + pub static SO_BUSY_POLL: SockOpt = 46; +} + +#[cfg(target_os = "macos")] +#[cfg(target_os = "ios")] +mod consts { + use libc::{c_int}; + + pub type AddressFamily = c_int; + + pub static AF_UNIX: AddressFamily = 1; + pub static AF_LOCAL: AddressFamily = AF_UNIX; + pub static AF_INET: AddressFamily = 2; + pub static AF_INET6: AddressFamily = 30; + + pub type SockType = c_int; + + pub static SOCK_STREAM: SockType = 1; + pub static SOCK_DGRAM: SockType = 2; + pub static SOCK_SEQPACKET: SockType = 5; + pub static SOCK_RAW: SockType = 3; + pub static SOCK_RDM: SockType = 4; + + pub type SockLevel = c_int; + + pub static SOL_SOCKET: SockLevel = 0xffff; + + pub type SockOpt = c_int; + + pub static SO_ACCEPTCONN: SockOpt = 0x0002; + pub static SO_BROADCAST: SockOpt = 0x0020; + pub static SO_DEBUG: SockOpt = 0x0001; + pub static SO_DONTTRUNC: SockOpt = 0x2000; + pub static SO_ERROR: SockOpt = 0x1007; + pub static SO_DONTROUTE: SockOpt = 0x0010; + pub static SO_KEEPALIVE: SockOpt = 0x0008; + pub static SO_LABEL: SockOpt = 0x1010; + pub static SO_LINGER: SockOpt = 0x0080; + pub static SO_NREAD: SockOpt = 0x1020; + pub static SO_NKE: SockOpt = 0x1021; + pub static SO_NOSIGPIPE: SockOpt = 0x1022; + pub static SO_NOADDRERR: SockOpt = 0x1023; + pub static SO_NOTIFYCONFLICT: SockOpt = 0x1026; + pub static SO_NP_EXTENSIONS: SockOpt = 0x1083; + pub static SO_NWRITE: SockOpt = 0x1024; + pub static SO_OOBINLINE: SockOpt = 0x0100; + pub static SO_PEERLABEL: SockOpt = 0x1011; + pub static SO_RCVBUF: SockOpt = 0x1002; + pub static SO_RCVLOWAT: SockOpt = 0x1004; + pub static SO_SNDLOWAT: SockOpt = 0x1003; + pub static SO_RCVTIMEO: SockOpt = 0x1006; + pub static SO_SNDTIMEO: SockOpt = 0x1005; + pub static SO_RANDOMPORT: SockOpt = 0x1082; + pub static SO_RESTRICTIONS: SockOpt = 0x1081; + pub static SO_RESTRICT_DENYIN: SockOpt = 0x00000001; + pub static SO_RESTRICT_DENYOUT: SockOpt = 0x00000002; + pub static SO_REUSEADDR: SockOpt = 0x0004; + pub static SO_REUSESHAREUID: SockOpt = 0x1025; + pub static SO_SNDBUF: SockOpt = 0x1001; + pub static SO_TIMESTAMP: SockOpt = 0x0400; + pub static SO_TIMESTAMP_MONOTONIC: SockOpt = 0x0800; + pub static SO_TYPE: SockOpt = 0x1008; + pub static SO_WANTMORE: SockOpt = 0x4000; + pub static SO_WANTOOBFLAG: SockOpt = 0x8000; + #[allow(type_overflow)] + pub static SO_RESTRICT_DENYSET: SockOpt = 0x80000000; +} + pub fn socket(domain: AddressFamily, ty: SockType, flags: SockFlag) -> SysResult { // TODO: Check the kernel version let res = unsafe { ffi::socket(domain, ty | flags.bits(), 0) }; @@ -66,12 +183,6 @@ pub fn listen(sockfd: Fd, backlog: uint) -> SysResult<()> { from_ffi(res) } -pub enum SockAddr { - SockIpV4(sockaddr_in), - SockIpV6(sockaddr_in6), - SockUnix(sockaddr_un) -} - pub fn bind(sockfd: Fd, addr: &SockAddr) -> SysResult<()> { let res = unsafe { match *addr { @@ -117,47 +228,6 @@ pub fn connect(sockfd: Fd, addr: &SockAddr) -> SysResult<()> { from_ffi(res) } -pub type SockLevel = c_int; - -pub static SOL_IP: SockLevel = 0; -pub static SOL_SOCKET: SockLevel = 1; -pub static SOL_TCP: SockLevel = 6; -pub static SOL_UDP: SockLevel = 17; -pub static SOL_IPV6: SockLevel = 41; - -pub type SockOpt = c_int; - -pub static SO_ACCEPTCONN: SockOpt = 30; -pub static SO_BINDTODEVICE: SockOpt = 25; -pub static SO_BROADCAST: SockOpt = 6; -pub static SO_BSDCOMPAT: SockOpt = 14; -pub static SO_DEBUG: SockOpt = 1; -pub static SO_DOMAIN: SockOpt = 39; -pub static SO_ERROR: SockOpt = 4; -pub static SO_DONTROUTE: SockOpt = 5; -pub static SO_KEEPALIVE: SockOpt = 9; -pub static SO_LINGER: SockOpt = 13; -pub static SO_MARK: SockOpt = 36; -pub static SO_OOBINLINE: SockOpt = 10; -pub static SO_PASSCRED: SockOpt = 16; -pub static SO_PEEK_OFF: SockOpt = 42; -pub static SO_PEERCRED: SockOpt = 17; -pub static SO_PRIORITY: SockOpt = 12; -pub static SO_PROTOCOL: SockOpt = 38; -pub static SO_RCVBUF: SockOpt = 8; -pub static SO_RCVBUFFORCE: SockOpt = 33; -pub static SO_RCVLOWAT: SockOpt = 18; -pub static SO_SNDLOWAT: SockOpt = 19; -pub static SO_RCVTIMEO: SockOpt = 20; -pub static SO_SNDTIMEO: SockOpt = 21; -pub static SO_REUSEADDR: SockOpt = 2; -pub static SO_RXQ_OVFL: SockOpt = 40; -pub static SO_SNDBUF: SockOpt = 7; -pub static SO_SNDBUFFORCE: SockOpt = 32; -pub static SO_TIMESTAMP: SockOpt = 29; -pub static SO_TYPE: SockOpt = 3; -pub static SO_BUSY_POLL: SockOpt = 46; - pub fn getsockopt(fd: Fd, level: SockLevel, opt: SockOpt, val: &mut T) -> SysResult { let mut len = mem::size_of::() as socklen_t; -- cgit v1.2.3