1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
// License: see LICENSE file at root directory of `master` branch

//! # UDSx
//!
//! ## Project
//!
//! - Repository: <https://bitbucket.org/haibison/udsx>
//! - License: Nice License 1.0.0 _(see LICENSE file at root directory of `master` branch)_
//! - _This project follows [Semantic Versioning 2.0.0]_
//!
//! ## Features
//!
//! This project provides some extensions for Unix domain sockets, such as [`UdsxUnixStream`][::UdsxUnixStream], [Abstract Linux socket][::als].
//!
//! [Semantic Versioning 2.0.0]: https://semver.org/spec/v2.0.0.html
//!
//! [::als]: als/index.html
//! [::UdsxUnixStream]: trait.UdsxUnixStream.html

#![warn(missing_docs)]

// ╔═════════════════╗
// ║   IDENTIFIERS   ║
// ╚═════════════════╝

macro_rules! code_name  { () => { "udsx" }}
macro_rules! version    { () => { "0.5.0" }}

/// # Crate name
pub const NAME: &str = "UDSx";

/// # Crate code name
pub const CODE_NAME: &str = code_name!();

/// # ID of this crate
pub const ID: &str = concat!(
    "3df68c92-3aef9c8a-620269e5-31b3c45d-0dd34f4c-2a7dce4e-33c12966-4e5631cb-",
    "617e5279-d901a7b2-593e6f06-5e9c1de0-f4af1b84-f3057770-b6558c66-da0d4efe",
);

/// # Crate version
pub const VERSION: &str = version!();

/// # Crate release date (year/month/day)
pub const RELEASE_DATE: (u16, u8, u8) = (2019, 7, 4);

/// # Tag, which can be used for logging...
pub const TAG: &str = concat!(code_name!(), "::3df68c92::", version!());

// ╔════════════════════╗
// ║   IMPLEMENTATION   ║
// ╚════════════════════╝

use std::{
    ffi::CStr,
    io::{Error, ErrorKind},
    mem,
};

#[cfg(unix)]
pub mod als;
pub mod version_info;

#[cfg(unix)]
mod credentials;
#[cfg(unix)]
mod root;

#[cfg(unix)]
pub use credentials::*;
#[cfg(unix)]
pub use root::*;

#[test]
fn test_crate_version() {
    assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
}

/// # Formats errno
unsafe fn format_errno<S>(libc_fn_name: S, errno: Option<i32>) -> String where S: AsRef<str> {
    let libc_fn_name = libc_fn_name.as_ref();
    let errno = errno.unwrap_or_else(|| *libc::__errno_location());
    format!(
        "Failed calling libc::{}{} -- *possible* errno: {:?} -> {:?}",
        libc_fn_name, match libc_fn_name.ends_with(')') { true => concat!(), false => "()" },
        errno, CStr::from_ptr(libc::strerror(errno)),
    )
}

/// # Tries to cast a usize as u32
fn usize_as_u32(n: usize) -> Result<u32> {
    match mem::size_of::<usize>() <= mem::size_of::<u32>() || n <= u32::max_value() as usize {
        true => Ok(n as u32),
        false => Err(Error::new(ErrorKind::InvalidData, format!("Cannot cast {}_usize as u32", n))),
    }
}

/// # Tries to cast a u32 as usize
fn u32_as_usize(n: u32) -> Result<usize> {
    match mem::size_of::<u32>() <= mem::size_of::<usize>() || n <= usize::max_value() as u32 {
        true => Ok(n as usize),
        false => Err(Error::new(ErrorKind::InvalidData, format!("Cannot cast {}_u32 as usize", n))),
    }
}