Parcourir la source

implement idna encoding

introduces unic-idna as a dependency
file size has incresead (striped, release):

before:   673912
 after:  1476728
Дамјан Георгиевски il y a 4 ans
Parent
commit
b8246366ed
3 fichiers modifiés avec 127 ajouts et 2 suppressions
  1. 110 0
      Cargo.lock
  2. 3 0
      dns/Cargo.toml
  3. 14 2
      dns/src/strings.rs

+ 110 - 0
Cargo.lock

@@ -152,6 +152,7 @@ dependencies = [
  "log",
  "mutagen",
  "pretty_assertions",
+ "unic-idna",
 ]
 
 [[package]]
@@ -306,6 +307,12 @@ dependencies = [
  "cfg-if 0.1.10",
 ]
 
+[[package]]
+name = "matches"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
+
 [[package]]
 name = "miniz_oxide"
 version = "0.4.3"
@@ -653,6 +660,109 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "unic-char-property"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a8c57a407d9b6fa02b4795eb81c5b6652060a15a7903ea981f3d723e6c0be221"
+dependencies = [
+ "unic-char-range",
+]
+
+[[package]]
+name = "unic-char-range"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0398022d5f700414f6b899e10b8348231abf9173fa93144cbc1a43b9793c1fbc"
+
+[[package]]
+name = "unic-common"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "80d7ff825a6a654ee85a63e80f92f054f904f21e7d12da4e22f9834a4aaa35bc"
+
+[[package]]
+name = "unic-idna"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "621e9cf526f2094d2c2ced579766458a92f8f422d6bb934c503ba1a95823a62d"
+dependencies = [
+ "matches",
+ "unic-idna-mapping",
+ "unic-idna-punycode",
+ "unic-normal",
+ "unic-ucd-bidi",
+ "unic-ucd-normal",
+ "unic-ucd-version",
+]
+
+[[package]]
+name = "unic-idna-mapping"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4de70fd4e5331537347a50a0dbc938efb1f127c9f6e5efec980fc90585aa1343"
+dependencies = [
+ "unic-char-property",
+ "unic-char-range",
+ "unic-ucd-version",
+]
+
+[[package]]
+name = "unic-idna-punycode"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06feaedcbf9f1fc259144d833c0d630b8b15207b0486ab817d29258bc89f2f8a"
+
+[[package]]
+name = "unic-normal"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f09d64d33589a94628bc2aeb037f35c2e25f3f049c7348b5aa5580b48e6bba62"
+dependencies = [
+ "unic-ucd-normal",
+]
+
+[[package]]
+name = "unic-ucd-bidi"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1d568b51222484e1f8209ce48caa6b430bf352962b877d592c29ab31fb53d8c"
+dependencies = [
+ "unic-char-property",
+ "unic-char-range",
+ "unic-ucd-version",
+]
+
+[[package]]
+name = "unic-ucd-hangul"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eb1dc690e19010e1523edb9713224cba5ef55b54894fe33424439ec9a40c0054"
+dependencies = [
+ "unic-ucd-version",
+]
+
+[[package]]
+name = "unic-ucd-normal"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "86aed873b8202d22b13859dda5fe7c001d271412c31d411fd9b827e030569410"
+dependencies = [
+ "unic-char-property",
+ "unic-char-range",
+ "unic-ucd-hangul",
+ "unic-ucd-version",
+]
+
+[[package]]
+name = "unic-ucd-version"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96bd2f2237fe450fcd0a1d2f5f4e91711124f7857ba2e964247776ebeeb7b0c4"
+dependencies = [
+ "unic-common",
+]
+
 [[package]]
 name = "unicode-width"
 version = "0.1.8"

+ 3 - 0
dns/Cargo.toml

@@ -19,6 +19,9 @@ base64 = "0.13"
 # testing
 mutagen = { git = "https://github.com/llogiq/mutagen", optional = true }
 
+# idna encoding
+unic-idna = "0.9.0"
+
 [dev-dependencies]
 pretty_assertions = "0.6"
 

+ 14 - 2
dns/src/strings.rs

@@ -4,6 +4,7 @@ use std::convert::TryFrom;
 use std::fmt;
 use std::io::{self, Write};
 
+use unic_idna;
 use byteorder::{ReadBytesExt, WriteBytesExt};
 use log::*;
 
@@ -20,6 +21,11 @@ pub struct Labels {
     segments: Vec<(u8, String)>,
 }
 
+fn label_to_ascii(label: &str) -> Result<String, unic_idna::Errors> {
+    let flags = unic_idna::Flags{use_std3_ascii_rules: true, transitional_processing: false, verify_dns_length: true};
+    unic_idna::to_ascii(label, flags)
+}
+
 impl Labels {
 
     /// Creates a new empty set of labels, which represent the root of the DNS
@@ -38,9 +44,15 @@ impl Labels {
                 continue;
             }
 
-            match u8::try_from(label.len()) {
+            let label_idn = label_to_ascii(label)
+                    .map_err(|e| {
+                        warn!("Could not encode label {:?}: {:?}", label, e);
+                        return label;
+                    })?;
+
+            match u8::try_from(label_idn.len()) {
                 Ok(length) => {
-                    segments.push((length, label.to_owned()));
+                    segments.push((length, label_idn));
                 }
                 Err(e) => {
                     warn!("Could not encode label {:?}: {}", label, e);