@@ -12,6 +12,7 @@ use rustc_serialize::hex::{self, FromHex, ToHex};
12
12
use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
13
13
14
14
use time;
15
+ use hostname:: get_hostname;
15
16
16
17
const TIMESTAMP_SIZE : usize = 4 ;
17
18
const MACHINE_ID_SIZE : usize = 3 ;
@@ -28,9 +29,6 @@ const MAX_U24: usize = 0xFFFFFF;
28
29
static OID_COUNTER : AtomicUsize = ATOMIC_USIZE_INIT ;
29
30
static mut MACHINE_BYTES : Option < [ u8 ; 3 ] > = None ;
30
31
31
- extern "C" {
32
- fn gethostname ( name : * mut libc:: c_char , size : libc:: size_t ) -> libc:: c_int ;
33
- }
34
32
35
33
/// Errors that can occur during OID construction and generation.
36
34
#[ derive( Debug ) ]
@@ -205,22 +203,14 @@ impl ObjectId {
205
203
}
206
204
}
207
205
208
- // Retrieve hostname through libc
209
- let len = 255 ;
210
- let mut buf = Vec :: < u8 > :: with_capacity ( len) ;
211
- let ptr = buf. as_mut_ptr ( ) ;
212
- let err = unsafe { gethostname ( ptr as * mut libc:: c_char , len as libc:: size_t ) } as i32 ;
213
-
214
- if err != 0 {
206
+ let hostname = get_hostname ( ) ;
207
+ if hostname. is_none ( ) {
215
208
return Err ( Error :: HostnameError ) ;
216
209
}
217
210
218
- // Convert bytes into string
219
- let s = String :: from_utf8_lossy ( & buf) ;
220
-
221
211
// Hash hostname string
222
212
let mut md5 = Md5 :: new ( ) ;
223
- md5. input_str ( & s . into_owned ( ) [ .. ] ) ;
213
+ md5. input_str ( hostname . unwrap ( ) . as_str ( ) ) ;
224
214
let hash = md5. result_str ( ) ;
225
215
226
216
// Re-convert string to bytes and grab first three
0 commit comments