@@ -12,6 +12,7 @@ use rustc_serialize::hex::{self, FromHex, ToHex};
1212use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
1313
1414use time;
15+ use hostname:: get_hostname;
1516
1617const TIMESTAMP_SIZE : usize = 4 ;
1718const MACHINE_ID_SIZE : usize = 3 ;
@@ -28,9 +29,6 @@ const MAX_U24: usize = 0xFFFFFF;
2829static OID_COUNTER : AtomicUsize = ATOMIC_USIZE_INIT ;
2930static mut MACHINE_BYTES : Option < [ u8 ; 3 ] > = None ;
3031
31- extern "C" {
32- fn gethostname ( name : * mut libc:: c_char , size : libc:: size_t ) -> libc:: c_int ;
33- }
3432
3533/// Errors that can occur during OID construction and generation.
3634#[ derive( Debug ) ]
@@ -205,22 +203,14 @@ impl ObjectId {
205203 }
206204 }
207205
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 ( ) {
215208 return Err ( Error :: HostnameError ) ;
216209 }
217210
218- // Convert bytes into string
219- let s = String :: from_utf8_lossy ( & buf) ;
220-
221211 // Hash hostname string
222212 let mut md5 = Md5 :: new ( ) ;
223- md5. input_str ( & s . into_owned ( ) [ .. ] ) ;
213+ md5. input_str ( hostname . unwrap ( ) . as_str ( ) ) ;
224214 let hash = md5. result_str ( ) ;
225215
226216 // Re-convert string to bytes and grab first three
0 commit comments