Skip to content

Commit 704b41d

Browse files
committed
fix gethostname in windows.
1 parent 3d0fc0e commit 704b41d

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rustc-serialize = "0.3"
2020
serde = "0.8"
2121
time = "0.1"
2222
linked-hash-map = "0.0.9"
23+
hostname = "^0.1"
2324

2425
[dependencies.num]
2526
version = "~0.1.27"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern crate rustc_serialize;
5151
extern crate serde;
5252
extern crate time;
5353
extern crate linked_hash_map;
54+
extern crate hostname;
5455

5556
pub use self::bson::{Bson, Document, Array};
5657
pub use self::encoder::{encode_document, to_bson, Encoder, EncoderResult, EncoderError};

src/oid.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_serialize::hex::{self, FromHex, ToHex};
1212
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
1313

1414
use time;
15+
use hostname::get_hostname;
1516

1617
const TIMESTAMP_SIZE: usize = 4;
1718
const MACHINE_ID_SIZE: usize = 3;
@@ -28,9 +29,6 @@ const MAX_U24: usize = 0xFFFFFF;
2829
static OID_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
2930
static 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

Comments
 (0)