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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![feature(libc)]

extern crate libc;
extern crate rustc_serialize;
extern crate rand;
extern crate eui48;

use libc::{c_char};
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::time::{SystemTime, UNIX_EPOCH};
use std::ffi::CStr;
use rand::Rng;
use rustc_serialize::hex::FromHex;
use eui48::{MacAddress, Eui48};

//------------------------------------------------------------------------------

#[no_mangle]
pub extern fn uuid_gen_new(ptr: *const c_char) -> *mut UUIDGen {
    let eui_cstr = unsafe {
        assert!(!ptr.is_null());
        CStr::from_ptr(ptr)
    };
    let mut eui: [u8; 6] = Default::default();
    eui.copy_from_slice(&eui_cstr.to_bytes()[0..6]);
    Box::into_raw(Box::new(UUIDGen::new(eui)))
}

#[no_mangle]
pub extern fn uuid_gen_free(ptr: *mut UUIDGen) {
    if ptr.is_null() { return }
    unsafe {
        Box::from_raw(ptr);
    }
}

#[no_mangle]
pub extern fn uuid_gen_nonce64(gen_ptr: *mut UUIDGen, nonce_ptr: *mut u8) {
    let gen = unsafe {
        assert!(!gen_ptr.is_null());
        &mut *gen_ptr
    };
    let nonce64: [u8; 8] = gen.nonce64();
    let nonce64: &[u8] = &nonce64[0..8];
    unsafe { std::ptr::copy(&(nonce64)[0], nonce_ptr, 8) }
}

#[no_mangle]
pub extern fn uuid_gen_uuid128(gen_ptr: *mut UUIDGen, uuid_ptr: *mut u8) {
    let gen = unsafe {
        assert!(!gen_ptr.is_null());
        &mut *gen_ptr
    };
    let uuid128: [u8; 16] = gen.uuid128();
    let uuid128: &[u8] = &uuid128[0..16];
    unsafe { std::ptr::copy(&(uuid128)[0], uuid_ptr, 16) }
}

//------------------------------------------------------------------------------

#[derive(Debug)]
pub struct UUIDGen(MacAddress);

impl UUIDGen {

    pub fn new(eui: Eui48) -> UUIDGen {
        UUIDGen(MacAddress::new(eui))
    }

    // Generates a 64-bit nonce. This should not be used as a UUID.
    pub fn nonce64(&self) -> [u8; 8] {
        let nanosec_bytes = nanosecs_since_unix_epoch();

        let mac_bytes = self.0.as_bytes();
        let mut bytes = [0; 8];
        bytes[0] = nanosec_bytes[0] ^ mac_bytes[0];
        bytes[1] = nanosec_bytes[1] ^ mac_bytes[1];
        bytes[2] = nanosec_bytes[2] ^ mac_bytes[2];
        bytes[3] = nanosec_bytes[3] ^ mac_bytes[3];
        bytes[4] = nanosec_bytes[4] ^ mac_bytes[4];
        bytes[5] = nanosec_bytes[5] ^ mac_bytes[5];
        bytes[6] = nanosec_bytes[6];
        bytes[7] = nanosec_bytes[7];
        bytes
    }

    // A variant of the v1 UUID (128-bit).
    pub fn uuid128(&self) -> [u8; 16] {
        let nanosec_bytes = nanosecs_since_unix_epoch();

        let mut rng = rand::thread_rng();
        let r = rng.gen::<[u8; 8]>();

        let mac_bytes = self.0.as_bytes();
        let mut bytes = [0; 16];
        bytes[0] = nanosec_bytes[0];
        bytes[1] = nanosec_bytes[1];
        bytes[2] = nanosec_bytes[2];
        bytes[3] = nanosec_bytes[3];
        bytes[4] = nanosec_bytes[4];
        bytes[5] = nanosec_bytes[5];
        bytes[6] = nanosec_bytes[6];
        bytes[7] = nanosec_bytes[7];
        bytes[8] = mac_bytes[0];
        bytes[9] = mac_bytes[1];
        bytes[10] = mac_bytes[2];
        bytes[11] = mac_bytes[3];
        bytes[12] = mac_bytes[4];
        bytes[13] = mac_bytes[5];
        bytes[14] = r[6];
        bytes[15] = r[7];
        bytes
    }
}

//------------------------------------------------------------------------------
// Internal
//------------------------------------------------------------------------------

// Takes the inverse of the little_endian function on a 64-bit integer
// in order to obtain 8 bytes.
fn little_endian_inv64(x: u64) -> [u8; 8] {
    let mut bytes: [u8; 8] = [0; 8];
    bytes[0] = (x & 0xFF) as u8;
    for i in 1..8 {
        bytes[i] = ((x & (0xFF << i * 8)) >> i * 8) as u8;
    }
    bytes
}

// 100-nanosecond intervals since UNIX_EPOCH.
fn nanosecs_since_unix_epoch() -> [u8; 8] {
    let now = SystemTime::now();
    let duration = now.duration_since(UNIX_EPOCH)
        .expect("Failed to get duration since unix epoch.");

    // seconds to 100-nanosecond interval bytes
    let secs: u64 = duration.as_secs() as u64 * 1_000_000_0;
    let nanosecs: u64 = secs + (duration.subsec_nanos() as u64 / 1_00);

    little_endian_inv64(nanosecs)
}

// Linux only interface Eui48 read.
pub fn read_interface_eui(iface: &str) -> Eui48 {
    let path = Path::new("/sys/class/net").join(Path::new(iface)).join("address");

    let f = File::open(path)
        .expect("Network interface not found.");

    let mut reader = BufReader::new(f);
    let mut line = String::new();

    reader.read_line(&mut line)
        .expect("Unable to read iface.");

    let mut eui: Eui48 = [0; 6];
    for i in 0..6 {
        let byte: String = line.drain(0..2).collect();
        line.drain(0..1);
        let byte_v = byte.from_hex()
            .expect(format!("Failed to decode mac_address byte {}", i).as_str());
        eui[i] = byte_v[0];
    }

    eui
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    // use super::*;

    #[test]
    fn uuid_gen() {
        // ...
    }
}