#[inline(always)]
pub(crate) fn new_socket(domain: c_int, type_: c_int, protocol: c_int, non_blocking: bool) -> Result<RawFd, CreationError>
{
let flags = if non_blocking
{
type_ | SOCK_CLOEXEC | SOCK_NONBLOCK
}
else
{
type_ | SOCK_CLOEXEC
};
let result = unsafe { socket(domain, flags, protocol) };
if likely!(result >= 0)
{
Ok(result)
}
else if likely!(result == -1)
{
use self::CreationError::*;
Err
(
match errno().0
{
EMFILE => PerProcessLimitOnNumberOfFileDescriptorsWouldBeExceeded,
ENFILE => SystemWideLimitOnTotalNumberOfFileDescriptorsWouldBeExceeded,
ENOBUFS | ENOMEM => KernelWouldBeOutOfMemory,
EINVAL => panic!("Invalid arguments"),
EACCES => panic!("Permission denined"),
EAFNOSUPPORT => panic!("The implementation does not support the specified address family"),
EPROTONOSUPPORT => panic!("The protocol type or the specified protocol is not supported within this domain"),
_ => unreachable_code(format_args!("")),
}
)
}
else
{
unreachable_code(format_args!("Unexpected result {} from socket()", result))
}
}