mirror of https://github.com/dswd/vpncloud.git
Using slice operations instead of loops
This commit is contained in:
parent
fe7b0f0007
commit
15cd399827
|
@ -15,6 +15,7 @@ This project follows [semantic versioning](http://semver.org).
|
|||
- [changed] Only calling crypto_init once
|
||||
- [changed] Passing listen address as &str
|
||||
- [changed] Using FNV hash for better performance
|
||||
- [changed] Using slice operations instead of loops
|
||||
- [fixed] Fixed wrong hex address formatting
|
||||
- [fixed] Fixed peer exchange for more than 65000 peers
|
||||
- [fixed] Initializing crypto for benchmarks
|
||||
|
|
|
@ -212,9 +212,7 @@ impl Crypto {
|
|||
match method {
|
||||
CryptoMethod::ChaCha20 => {
|
||||
let mut crypto_key = [0; crypto_aead_chacha20poly1305_ietf_KEYBYTES];
|
||||
for i in 0..crypto_key.len() {
|
||||
crypto_key[i] = key[i];
|
||||
}
|
||||
crypto_key.clone_from_slice(&key[..crypto_aead_chacha20poly1305_ietf_KEYBYTES]);
|
||||
let mut nonce = [0u8; crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
|
||||
unsafe { randombytes_buf(nonce.as_mut_ptr(), nonce.len()) };
|
||||
Crypto::ChaCha20Poly1305{key: crypto_key, nonce: nonce}
|
||||
|
|
|
@ -32,10 +32,8 @@ impl Protocol for Frame {
|
|||
let mut dst = [0; 16];
|
||||
src[0] = data[pos]; src[1] = data[pos+1];
|
||||
dst[0] = data[pos]; dst[1] = data[pos+1];
|
||||
for i in 0..6 {
|
||||
src[i+2] = src_data[i];
|
||||
dst[i+2] = dst_data[i];
|
||||
}
|
||||
src[2..8].clone_from_slice(&src_data);
|
||||
dst[2..8].clone_from_slice(&dst_data);
|
||||
Ok((Address{data: src, len: 8}, Address{data: dst, len: 8}))
|
||||
} else {
|
||||
let src = try!(Address::read_from_fixed(&src_data, 6));
|
||||
|
|
|
@ -70,9 +70,7 @@ impl Table for RoutingTable {
|
|||
let group_len = prefix_len as usize / 8;
|
||||
assert!(group_len <= 16);
|
||||
let mut group_bytes = Vec::with_capacity(group_len);
|
||||
for i in 0..group_len {
|
||||
group_bytes.push(addr.data[i]);
|
||||
}
|
||||
group_bytes.extend_from_slice(&addr.data[0..group_len]);
|
||||
let routing_entry = RoutingEntry{address: address, bytes: addr.data, prefix_len: prefix_len};
|
||||
match self.0.entry(group_bytes) {
|
||||
hash_map::Entry::Occupied(mut entry) => entry.get_mut().push(routing_entry),
|
||||
|
|
13
src/types.rs
13
src/types.rs
|
@ -41,9 +41,7 @@ impl Address {
|
|||
return Err(Error::ParseError("Address too short"));
|
||||
}
|
||||
let mut bytes = [0; 16];
|
||||
for i in 0..len {
|
||||
bytes[i] = data[i];
|
||||
}
|
||||
bytes[0..len].clone_from_slice(&data[0..len]);
|
||||
Ok(Address{data: bytes, len: len as u8})
|
||||
}
|
||||
|
||||
|
@ -51,9 +49,8 @@ impl Address {
|
|||
pub fn write_to(&self, data: &mut[u8]) -> usize {
|
||||
assert!(data.len() >= self.len as usize + 1);
|
||||
data[0] = self.len;
|
||||
for i in 0..self.len as usize {
|
||||
data[i+1] = self.data[i];
|
||||
}
|
||||
let len = self.len as usize;
|
||||
data[1..len+1].clone_from_slice(&self.data[0..len]);
|
||||
self.len as usize + 1
|
||||
}
|
||||
}
|
||||
|
@ -106,9 +103,7 @@ impl FromStr for Address {
|
|||
if let Ok(addr) = Ipv4Addr::from_str(text) {
|
||||
let ip = addr.octets();
|
||||
let mut res = [0; 16];
|
||||
for i in 0..4 {
|
||||
res[i] = ip[i];
|
||||
}
|
||||
res[0..4].clone_from_slice(&ip);
|
||||
return Ok(Address{data: res, len: 4});
|
||||
}
|
||||
if let Ok(addr) = Ipv6Addr::from_str(text) {
|
||||
|
|
|
@ -36,9 +36,7 @@ impl TopHeader {
|
|||
return Err(Error::ParseError("Empty message"));
|
||||
}
|
||||
let mut header = TopHeader::default();
|
||||
for i in 0..3 {
|
||||
header.magic[i] = data[i];
|
||||
}
|
||||
header.magic.clone_from_slice(&data[0..3]);
|
||||
header.version = data[3];
|
||||
header.crypto_method = data[4];
|
||||
header.flags = data[6];
|
||||
|
@ -47,9 +45,7 @@ impl TopHeader {
|
|||
}
|
||||
|
||||
pub fn write_to(&self, data: &mut [u8]) -> usize {
|
||||
for i in 0..3 {
|
||||
data[i] = self.magic[i];
|
||||
}
|
||||
data[0..3].clone_from_slice(&self.magic);
|
||||
data[3] = self.version;
|
||||
data[4] = self.crypto_method;
|
||||
data[6] = self.flags;
|
||||
|
@ -179,9 +175,7 @@ pub fn decode<'a>(data: &'a mut [u8], crypto: &mut Crypto) -> Result<(Options, M
|
|||
let stage = data[pos];
|
||||
pos += 1;
|
||||
let mut node_id = [0; NODE_ID_BYTES];
|
||||
for i in 0..NODE_ID_BYTES {
|
||||
node_id[i] = data[pos+i];
|
||||
}
|
||||
node_id.clone_from_slice(&data[pos..pos+NODE_ID_BYTES]);
|
||||
pos += NODE_ID_BYTES;
|
||||
let count = data[pos] as usize;
|
||||
pos += 1;
|
||||
|
@ -225,9 +219,7 @@ pub fn encode<'a>(options: &Options, msg: &'a mut Message, mut buf: &'a mut [u8]
|
|||
pos += 1;
|
||||
for addr in v4addrs {
|
||||
let ip = addr.ip().octets();
|
||||
for i in 0..4 {
|
||||
buf[pos+i] = ip[i];
|
||||
}
|
||||
buf[pos..pos+4].clone_from_slice(&ip);
|
||||
pos += 4;
|
||||
Encoder::write_u16(addr.port(), &mut buf[pos..]);
|
||||
pos += 2;
|
||||
|
@ -250,9 +242,7 @@ pub fn encode<'a>(options: &Options, msg: &'a mut Message, mut buf: &'a mut [u8]
|
|||
assert!(buf.len() >= pos + 2 + NODE_ID_BYTES);
|
||||
buf[pos] = stage;
|
||||
pos += 1;
|
||||
for i in 0..NODE_ID_BYTES {
|
||||
buf[pos+i] = node_id[i];
|
||||
}
|
||||
buf[pos..pos+NODE_ID_BYTES].clone_from_slice(node_id);
|
||||
pos += NODE_ID_BYTES;
|
||||
assert!(ranges.len() <= 255);
|
||||
buf[pos] = ranges.len() as u8;
|
||||
|
|
Loading…
Reference in New Issue