Created
November 25, 2017 02:57
-
-
Save sajattack/9790f48ebebab935ade7290ede3a10ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libc_fn!(unsafe getprotoent() -> Result <*const protoent> { | |
if protodb == 0 { | |
protodb = syscall::open("/etc/protocols", syscall::O_RDONLY).unwrap(); | |
LINE = RawLineBuffer::new(protodb); | |
} | |
let mut r = match LINE.next() { | |
Some(Ok(s)) => s, | |
Some(Err(_)) => panic!(), | |
None => panic!() | |
}; | |
let mut iter: SplitWhitespace = r.split_whitespace(); | |
let mut p: Vec<u8> = r.as_bytes().to_vec(); | |
let mut proto_name: Vec<u8> = iter.next().unwrap().as_bytes().to_vec(); | |
proto_name.push(b'\0'); | |
let mut num = iter.next().unwrap().as_bytes().to_vec(); | |
num.push(b'\0'); | |
PROTO_NUM = Some(libc::atoi(num.as_slice().as_ptr() as *mut i8)); | |
let mut proto_aliases: Vec<Vec<u8>> = Vec::new(); | |
loop { | |
let mut alias = match iter.next() { | |
Some(s) => s.as_bytes().to_vec(), | |
None => break | |
}; | |
alias.push(b'\0'); | |
proto_aliases.push(alias); | |
} | |
//push a 0 so C doesn't segfault when it tries to read the next entry | |
proto_aliases.push(vec![b'\0']); | |
PROTO_ENTRY = protoent { | |
p_name: proto_name.as_slice().as_ptr() as *const c_char, | |
//surprisingly C doesn't care that we're giving it a pointer to a Vec | |
p_aliases: proto_aliases.as_slice().as_ptr() as *const *const i8, | |
p_proto: PROTO_NUM.unwrap() | |
}; | |
PROTO_ALIASES = Some(proto_aliases); | |
PROTO_NAME = Some(proto_name); | |
Ok(&PROTO_ENTRY as *const protoent) | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment