summaryrefslogtreecommitdiffhomepage
path: root/talpid-core/src
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-12-01 17:09:13 +0100
committerDavid Lönnhager <david.l@mullvad.net>2021-12-01 19:35:32 +0100
commitbb03ed40533fcec054015f1aa0f279a2be4f9204 (patch)
tree526d19ca29732ac963b49d2945b5cfe51951fd6c /talpid-core/src
parent7942ce17671b1c36cccba79e84606f609e0a0186 (diff)
downloadmullvadvpn-bb03ed40533fcec054015f1aa0f279a2be4f9204.tar.xz
mullvadvpn-bb03ed40533fcec054015f1aa0f279a2be4f9204.zip
Fix deprecated uses and simplify uses of widestring
Diffstat (limited to 'talpid-core/src')
-rw-r--r--talpid-core/src/dns/windows/mod.rs2
-rw-r--r--talpid-core/src/firewall/windows.rs33
-rw-r--r--talpid-core/src/tunnel/openvpn/wintun.rs14
-rw-r--r--talpid-core/src/tunnel/wireguard/wireguard_nt.rs14
4 files changed, 19 insertions, 44 deletions
diff --git a/talpid-core/src/dns/windows/mod.rs b/talpid-core/src/dns/windows/mod.rs
index d3e22111cd..bdfae85fc2 100644
--- a/talpid-core/src/dns/windows/mod.rs
+++ b/talpid-core/src/dns/windows/mod.rs
@@ -120,7 +120,7 @@ impl super::DnsMonitorT for DnsMonitor {
}
fn ip_to_widestring(ip: &IpAddr) -> WideCString {
- WideCString::new(ip.to_string().encode_utf16().collect::<Vec<_>>()).unwrap()
+ WideCString::from_str_truncate(ip.to_string())
}
impl Drop for DnsMonitor {
diff --git a/talpid-core/src/firewall/windows.rs b/talpid-core/src/firewall/windows.rs
index 2889432c28..eedcf1d1c9 100644
--- a/talpid-core/src/firewall/windows.rs
+++ b/talpid-core/src/firewall/windows.rs
@@ -1,12 +1,11 @@
use crate::{logging::windows::log_sink, tunnel::TunnelMetadata};
-use std::{ffi::OsString, iter, net::IpAddr, path::Path, ptr};
+use std::{net::IpAddr, path::Path, ptr};
use self::winfw::*;
use super::{FirewallArguments, FirewallPolicy, FirewallT};
use crate::winnet;
use log::{debug, error, trace};
-use std::os::windows::ffi::OsStrExt;
use talpid_types::{net::Endpoint, tunnel::FirewallPolicyError};
use widestring::WideCString;
@@ -163,8 +162,7 @@ impl Firewall {
protocol: WinFwProt::from(endpoint.protocol),
};
- let mut relay_client: Vec<u16> = relay_client.as_os_str().encode_wide().collect();
- relay_client.push(0u16);
+ let relay_client = WideCString::from_os_str_truncate(relay_client);
let allowed_endpoint_ip = widestring_ip(allowed_endpoint.address.ip());
let winfw_allowed_endpoint = Some(WinFwEndpoint {
@@ -173,9 +171,9 @@ impl Firewall {
protocol: WinFwProt::from(allowed_endpoint.protocol),
});
- let interface_wstr = tunnel_metadata.as_ref().map(|metadata| {
- WideCString::new(metadata.interface.encode_utf16().collect::<Vec<_>>()).unwrap()
- });
+ let interface_wstr = tunnel_metadata
+ .as_ref()
+ .map(|metadata| WideCString::from_str_truncate(&metadata.interface));
let interface_wstr_ptr = if let Some(ref wstr) = interface_wstr {
wstr.as_ptr()
} else {
@@ -210,8 +208,7 @@ impl Firewall {
.ipv6_gateway
.map(|v6_ip| widestring_ip(v6_ip.into()));
- let tunnel_alias =
- WideCString::new(tunnel_metadata.interface.encode_utf16().collect::<Vec<_>>()).unwrap();
+ let tunnel_alias = WideCString::from_str_truncate(&tunnel_metadata.interface);
// ip_str, gateway_str and tunnel_alias have to outlive winfw_relay
let winfw_relay = WinFwEndpoint {
@@ -234,19 +231,10 @@ impl Firewall {
None => ptr::null(),
};
- let mut relay_client: Vec<u16> = relay_client.as_os_str().encode_wide().collect();
- relay_client.push(0u16);
+ let relay_client = WideCString::from_os_str_truncate(relay_client);
- let dns_servers: Vec<Vec<u16>> = dns_servers
- .iter()
- .map(|ip| {
- OsString::from(ip.to_string())
- .as_os_str()
- .encode_wide()
- .chain(iter::once(0u16))
- .collect()
- })
- .collect();
+ let dns_servers: Vec<WideCString> =
+ dns_servers.iter().cloned().map(widestring_ip).collect();
let dns_servers: Vec<*const u16> = dns_servers.iter().map(|ip| ip.as_ptr()).collect();
unsafe {
@@ -301,8 +289,7 @@ impl<T> NullablePointer<T> for Option<T> {
}
fn widestring_ip(ip: IpAddr) -> WideCString {
- let buf = ip.to_string().encode_utf16().collect::<Vec<_>>();
- WideCString::new(buf).unwrap()
+ WideCString::from_str_truncate(ip.to_string())
}
#[allow(non_snake_case)]
diff --git a/talpid-core/src/tunnel/openvpn/wintun.rs b/talpid-core/src/tunnel/openvpn/wintun.rs
index e466f287c5..a61b83643d 100644
--- a/talpid-core/src/tunnel/openvpn/wintun.rs
+++ b/talpid-core/src/tunnel/openvpn/wintun.rs
@@ -2,8 +2,8 @@ use crate::windows::{get_ip_interface_entry, set_ip_interface_entry, AddressFami
use lazy_static::lazy_static;
use std::{
ffi::CStr,
- fmt, io, iter, mem,
- os::windows::{ffi::OsStrExt, io::RawHandle},
+ fmt, io, mem,
+ os::windows::io::RawHandle,
path::Path,
ptr,
sync::{Arc, Mutex},
@@ -295,12 +295,7 @@ impl WintunDll {
}
fn new(resource_dir: &Path) -> io::Result<Self> {
- let wintun_dll: Vec<u16> = resource_dir
- .join("wintun.dll")
- .as_os_str()
- .encode_wide()
- .chain(iter::once(0u16))
- .collect();
+ let wintun_dll = U16CString::from_os_str_truncate(resource_dir.join("wintun.dll"));
let handle = unsafe {
LoadLibraryExW(
@@ -426,8 +421,7 @@ impl WintunDll {
if result == 0 {
return Err(io::Error::last_os_error());
}
- Ok(U16CString::from_vec_with_nul(alias_buffer)
- .map_err(|_| io::Error::new(io::ErrorKind::Other, "missing null terminator"))?)
+ Ok(U16CString::from_vec_truncate(alias_buffer))
}
pub unsafe fn get_adapter_luid(&self, adapter: RawHandle) -> NET_LUID {
diff --git a/talpid-core/src/tunnel/wireguard/wireguard_nt.rs b/talpid-core/src/tunnel/wireguard/wireguard_nt.rs
index f7de82d5be..66b9c086fc 100644
--- a/talpid-core/src/tunnel/wireguard/wireguard_nt.rs
+++ b/talpid-core/src/tunnel/wireguard/wireguard_nt.rs
@@ -10,10 +10,10 @@ use ipnetwork::IpNetwork;
use lazy_static::lazy_static;
use std::{
ffi::CStr,
- fmt, io, iter, mem,
+ fmt, io, mem,
mem::MaybeUninit,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
- os::windows::{ffi::OsStrExt, io::RawHandle},
+ os::windows::io::RawHandle,
path::Path,
ptr,
sync::{Arc, Mutex},
@@ -662,12 +662,7 @@ unsafe impl Sync for WgNtDll {}
impl WgNtDll {
pub fn new(resource_dir: &Path) -> io::Result<Self> {
- let wg_nt_dll: Vec<u16> = resource_dir
- .join("wireguard.dll")
- .as_os_str()
- .encode_wide()
- .chain(iter::once(0u16))
- .collect();
+ let wg_nt_dll = U16CString::from_os_str_truncate(resource_dir.join("wireguard.dll"));
let handle = unsafe {
LoadLibraryExW(
@@ -812,8 +807,7 @@ impl WgNtDll {
if result == 0 {
return Err(io::Error::last_os_error());
}
- Ok(U16CString::from_vec_with_nul(alias_buffer)
- .map_err(|_| io::Error::new(io::ErrorKind::Other, "missing null terminator"))?)
+ Ok(U16CString::from_vec_truncate(alias_buffer))
}
pub unsafe fn get_adapter_luid(&self, adapter: RawHandle) -> NET_LUID {