├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── error.rs ├── lib.rs └── protocol ├── cid.rs ├── codec.rs ├── mod.rs ├── pn.rs └── varint.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "backtrace" 3 | version = "0.3.8" 4 | source = "registry+https://github.com/rust-lang/crates.io-index" 5 | dependencies = [ 6 | "backtrace-sys 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", 7 | "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 8 | "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", 10 | "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 11 | ] 12 | 13 | [[package]] 14 | name = "backtrace-sys" 15 | version = "0.1.22" 16 | source = "registry+https://github.com/rust-lang/crates.io-index" 17 | dependencies = [ 18 | "cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", 19 | "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", 20 | "pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", 21 | ] 22 | 23 | [[package]] 24 | name = "base64" 25 | version = "0.9.1" 26 | source = "registry+https://github.com/rust-lang/crates.io-index" 27 | dependencies = [ 28 | "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 29 | "safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 30 | ] 31 | 32 | [[package]] 33 | name = "bitflags" 34 | version = "1.0.3" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | 37 | [[package]] 38 | name = "byteorder" 39 | version = "1.2.3" 40 | source = "registry+https://github.com/rust-lang/crates.io-index" 41 | 42 | [[package]] 43 | name = "bytes" 44 | version = "0.4.8" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | dependencies = [ 47 | "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 48 | "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 49 | ] 50 | 51 | [[package]] 52 | name = "cc" 53 | version = "1.0.15" 54 | source = "registry+https://github.com/rust-lang/crates.io-index" 55 | 56 | [[package]] 57 | name = "cfg-if" 58 | version = "0.1.3" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | 61 | [[package]] 62 | name = "cloudabi" 63 | version = "0.0.3" 64 | source = "registry+https://github.com/rust-lang/crates.io-index" 65 | dependencies = [ 66 | "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", 67 | ] 68 | 69 | [[package]] 70 | name = "failure" 71 | version = "0.1.1" 72 | source = "registry+https://github.com/rust-lang/crates.io-index" 73 | dependencies = [ 74 | "backtrace 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 75 | "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 76 | ] 77 | 78 | [[package]] 79 | name = "failure_derive" 80 | version = "0.1.1" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | dependencies = [ 83 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 84 | "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", 85 | "synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", 86 | ] 87 | 88 | [[package]] 89 | name = "fuchsia-zircon" 90 | version = "0.3.3" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | dependencies = [ 93 | "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", 94 | "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 95 | ] 96 | 97 | [[package]] 98 | name = "fuchsia-zircon-sys" 99 | version = "0.3.3" 100 | source = "registry+https://github.com/rust-lang/crates.io-index" 101 | 102 | [[package]] 103 | name = "iovec" 104 | version = "0.1.2" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | dependencies = [ 107 | "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", 108 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 109 | ] 110 | 111 | [[package]] 112 | name = "lazy_static" 113 | version = "1.0.1" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | 116 | [[package]] 117 | name = "libc" 118 | version = "0.2.41" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | 121 | [[package]] 122 | name = "log" 123 | version = "0.4.1" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | dependencies = [ 126 | "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 127 | ] 128 | 129 | [[package]] 130 | name = "pkg-config" 131 | version = "0.3.11" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | 134 | [[package]] 135 | name = "quic" 136 | version = "0.0.3" 137 | dependencies = [ 138 | "bytes 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", 139 | "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 140 | "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 141 | "rand 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 142 | "rustls 0.12.0 (git+https://github.com/ctz/rustls?branch=jbp-tls13-draft-28)", 143 | ] 144 | 145 | [[package]] 146 | name = "quote" 147 | version = "0.3.15" 148 | source = "registry+https://github.com/rust-lang/crates.io-index" 149 | 150 | [[package]] 151 | name = "rand" 152 | version = "0.5.0" 153 | source = "registry+https://github.com/rust-lang/crates.io-index" 154 | dependencies = [ 155 | "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", 156 | "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 157 | "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", 158 | "rand_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 159 | "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 160 | ] 161 | 162 | [[package]] 163 | name = "rand_core" 164 | version = "0.2.0" 165 | source = "registry+https://github.com/rust-lang/crates.io-index" 166 | 167 | [[package]] 168 | name = "ring" 169 | version = "0.13.0-alpha3" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | dependencies = [ 172 | "cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", 173 | "lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 174 | "libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)", 175 | "untrusted 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", 176 | ] 177 | 178 | [[package]] 179 | name = "rustc-demangle" 180 | version = "0.1.8" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | 183 | [[package]] 184 | name = "rustls" 185 | version = "0.12.0" 186 | source = "git+https://github.com/ctz/rustls?branch=jbp-tls13-draft-28#40f60b57756044ddac02dfa6049a8cfefad163f0" 187 | dependencies = [ 188 | "base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", 189 | "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 190 | "ring 0.13.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)", 191 | "sct 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 192 | "untrusted 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", 193 | "webpki 0.18.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)", 194 | ] 195 | 196 | [[package]] 197 | name = "safemem" 198 | version = "0.2.0" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | 201 | [[package]] 202 | name = "sct" 203 | version = "0.3.0" 204 | source = "registry+https://github.com/rust-lang/crates.io-index" 205 | dependencies = [ 206 | "ring 0.13.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)", 207 | "untrusted 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", 208 | ] 209 | 210 | [[package]] 211 | name = "syn" 212 | version = "0.11.11" 213 | source = "registry+https://github.com/rust-lang/crates.io-index" 214 | dependencies = [ 215 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 216 | "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", 217 | "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 218 | ] 219 | 220 | [[package]] 221 | name = "synom" 222 | version = "0.11.3" 223 | source = "registry+https://github.com/rust-lang/crates.io-index" 224 | dependencies = [ 225 | "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 226 | ] 227 | 228 | [[package]] 229 | name = "synstructure" 230 | version = "0.6.1" 231 | source = "registry+https://github.com/rust-lang/crates.io-index" 232 | dependencies = [ 233 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 234 | "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", 235 | ] 236 | 237 | [[package]] 238 | name = "unicode-xid" 239 | version = "0.0.4" 240 | source = "registry+https://github.com/rust-lang/crates.io-index" 241 | 242 | [[package]] 243 | name = "untrusted" 244 | version = "0.6.1" 245 | source = "registry+https://github.com/rust-lang/crates.io-index" 246 | 247 | [[package]] 248 | name = "webpki" 249 | version = "0.18.0-alpha3" 250 | source = "registry+https://github.com/rust-lang/crates.io-index" 251 | dependencies = [ 252 | "ring 0.13.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)", 253 | "untrusted 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", 254 | ] 255 | 256 | [[package]] 257 | name = "winapi" 258 | version = "0.2.8" 259 | source = "registry+https://github.com/rust-lang/crates.io-index" 260 | 261 | [[package]] 262 | name = "winapi" 263 | version = "0.3.4" 264 | source = "registry+https://github.com/rust-lang/crates.io-index" 265 | dependencies = [ 266 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 267 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 268 | ] 269 | 270 | [[package]] 271 | name = "winapi-i686-pc-windows-gnu" 272 | version = "0.4.0" 273 | source = "registry+https://github.com/rust-lang/crates.io-index" 274 | 275 | [[package]] 276 | name = "winapi-x86_64-pc-windows-gnu" 277 | version = "0.4.0" 278 | source = "registry+https://github.com/rust-lang/crates.io-index" 279 | 280 | [metadata] 281 | "checksum backtrace 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dbdd17cd962b570302f5297aea8648d5923e22e555c2ed2d8b2e34eca646bf6d" 282 | "checksum backtrace-sys 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5fd343a2466c4603f76f38de264bc0526cffc7fa38ba52fb9f13237eccc1ced2" 283 | "checksum base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9263aa6a38da271eec5c91a83ce1e800f093c8535788d403d626d8d5c3f8f007" 284 | "checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" 285 | "checksum byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "74c0b906e9446b0a2e4f760cdb3fa4b2c48cdc6db8766a845c54b6ff063fd2e9" 286 | "checksum bytes 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7dd32989a66957d3f0cba6588f15d4281a733f4e9ffc43fcd2385f57d3bf99ff" 287 | "checksum cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0ebb87d1116151416c0cf66a0e3fb6430cccd120fd6300794b4dfaa050ac40ba" 288 | "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" 289 | "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" 290 | "checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82" 291 | "checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b" 292 | "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" 293 | "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" 294 | "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" 295 | "checksum lazy_static 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e6412c5e2ad9584b0b8e979393122026cdd6d2a80b933f890dcd694ddbe73739" 296 | "checksum libc 0.2.41 (registry+https://github.com/rust-lang/crates.io-index)" = "ac8ebf8343a981e2fa97042b14768f02ed3e1d602eac06cae6166df3c8ced206" 297 | "checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" 298 | "checksum pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "110d5ee3593dbb73f56294327fe5668bcc997897097cbc76b51e7aed3f52452f" 299 | "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" 300 | "checksum rand 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a89abf8d34faf9783692392dca7bcdc6e82fa84eca86ccb6301ec87f3497185" 301 | "checksum rand_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1b7a5f27547c49e5ccf8a586db3f3782fd93cf849780b21853b9d981db203302" 302 | "checksum ring 0.13.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)" = "caa01a2423071513d705e3214187b944be268b34fb44ae7b07eba76ad9e6b01d" 303 | "checksum rustc-demangle 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "76d7ba1feafada44f2d38eed812bd2489a03c0f5abb975799251518b68848649" 304 | "checksum rustls 0.12.0 (git+https://github.com/ctz/rustls?branch=jbp-tls13-draft-28)" = "" 305 | "checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" 306 | "checksum sct 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4540aed8d71a5de961a8902cf356e28122bd62695eb5be1c214f84d8704097c" 307 | "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" 308 | "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" 309 | "checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd" 310 | "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" 311 | "checksum untrusted 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70afa43c8c5d23a53a3c39ec9b56232c5badc19f6bb5ad529c1d6448a7241365" 312 | "checksum webpki 0.18.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)" = "30cf7434bea34e094993720093b0f0ef4117d3edd977e5bd234de72e6d4c354e" 313 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 314 | "checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" 315 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 316 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 317 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "quic" 3 | version = "0.0.3" 4 | authors = ["xushuai ", "cssivision "] 5 | description = "QUIC rust implemention" 6 | homepage = "https://github.com/quic-rs/quic" 7 | repository = "https://github.com/quic-rs/quic" 8 | license = "MIT/Apache-2.0" 9 | readme = "README.md" 10 | 11 | [dependencies] 12 | bytes = "^0.4.0" 13 | failure = "^0.1.0" 14 | failure_derive = "^0.1.0" 15 | rand = "^0.5.0" 16 | 17 | [dependencies.rustls] 18 | git = "https://github.com/ctz/rustls" 19 | branch = "jbp-tls13-draft-28" 20 | features = ["quic"] 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quick UDP Internet Connections 2 | 3 | https://www.chromium.org/quic 4 | 5 | ## Specs (maybe changed that depends on the update of drafts) 6 | 7 | - Protocol target to [draft-ietf-quic-transport-12](https://tools.ietf.org/html/draft-ietf-quic-transport-12) 8 | - TLS1.3 target to [draft-ietf-quic-tls-12](https://tools.ietf.org/html/draft-ietf-quic-tls-12) -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | use failure::Context; 2 | 3 | #[derive(Debug)] 4 | pub struct Error(Context); 5 | 6 | #[derive(Debug, Fail)] 7 | pub enum ErrorKind { 8 | #[fail(display = "{}", _0)] 9 | Io(#[cause] ::std::io::Error), 10 | } 11 | 12 | pub type Result = ::std::result::Result; 13 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(integer_atomics)] 2 | #![feature(exclusive_range_pattern)] 3 | #![feature(range_contains)] 4 | 5 | extern crate failure; 6 | #[macro_use] 7 | extern crate failure_derive; 8 | extern crate bytes; 9 | extern crate rand; 10 | extern crate rustls; 11 | 12 | mod error; 13 | mod protocol; 14 | -------------------------------------------------------------------------------- /src/protocol/cid.rs: -------------------------------------------------------------------------------- 1 | use super::{Decoder, Encoder}; 2 | use bytes::{Buf, BufMut, IntoBuf}; 3 | use error::{Error, ErrorKind, Result}; 4 | use rand; 5 | use std; 6 | 7 | const DEFAULT_CID_LENGTH: usize = 8; 8 | const MIN_CID_LENGTH: usize = 4; 9 | const MAX_CID_LENGTH: usize = 18; 10 | const CID_LENGTH_RANGE: std::ops::RangeInclusive = MIN_CID_LENGTH..=MAX_CID_LENGTH; 11 | 12 | #[derive(Debug, Eq, PartialEq)] 13 | pub struct ConnectionID { 14 | len: usize, 15 | data: [u8; MAX_CID_LENGTH], 16 | } 17 | 18 | impl ConnectionID { 19 | pub fn new(len: usize) -> ConnectionID { 20 | assert!(CID_LENGTH_RANGE.contains(&len)); 21 | ConnectionID { 22 | len: len, 23 | data: [0; MAX_CID_LENGTH], 24 | } 25 | } 26 | 27 | pub fn generate() -> ConnectionID { 28 | rand::random() 29 | } 30 | 31 | pub fn len(&self) -> usize { 32 | // just safe to minus 3 because of the len field cannot be less than MIN_CID_LENGTH 33 | self.len - 3 34 | } 35 | } 36 | 37 | impl rand::distributions::Distribution for rand::distributions::Standard { 38 | fn sample(&self, rng: &mut R) -> ConnectionID { 39 | let mut cid = ConnectionID::new(DEFAULT_CID_LENGTH); 40 | rng.fill_bytes(&mut cid); 41 | cid 42 | } 43 | } 44 | 45 | impl std::ops::Deref for ConnectionID { 46 | type Target = [u8]; 47 | fn deref(&self) -> &Self::Target { 48 | &self.data[..self.len] 49 | } 50 | } 51 | 52 | impl std::ops::DerefMut for ConnectionID { 53 | fn deref_mut(&mut self) -> &mut Self::Target { 54 | &mut self.data[..self.len] 55 | } 56 | } 57 | 58 | impl Decoder for ConnectionID { 59 | fn decode(&mut self, src: &mut T) -> Result { 60 | src.copy_to_slice(self); 61 | Ok(self.len) 62 | } 63 | } 64 | 65 | impl Encoder for ConnectionID { 66 | fn encode(&self, dst: &mut T) -> Result { 67 | dst.put_slice(self); 68 | Ok(self.len) 69 | } 70 | } 71 | 72 | #[cfg(test)] 73 | mod tests { 74 | use super::*; 75 | 76 | #[test] 77 | fn rand_test() { 78 | let cid = ConnectionID::generate(); 79 | println!("{:?}", cid); 80 | } 81 | 82 | #[test] 83 | fn range_test() { 84 | assert_eq!(CID_LENGTH_RANGE.contains(&MIN_CID_LENGTH), true); 85 | assert_eq!(CID_LENGTH_RANGE.contains(&8), true); 86 | assert_eq!(CID_LENGTH_RANGE.contains(&MAX_CID_LENGTH), true); 87 | 88 | assert_eq!(CID_LENGTH_RANGE.contains(&0), false); 89 | assert_eq!(CID_LENGTH_RANGE.contains(&20), false); 90 | } 91 | 92 | #[test] 93 | fn decode_test() { 94 | let mut cid = ConnectionID::new(8); 95 | let mut input = vec![ 96 | 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 97 | 0b11111111, 98 | ]; 99 | cid.decode(&mut input.clone().into_buf()); 100 | assert_eq!(&*cid, input.as_slice()); 101 | } 102 | 103 | #[test] 104 | fn encode_test() { 105 | let mut cid = ConnectionID::new(8); 106 | (&mut cid.data[..cid.len]).clone_from_slice( 107 | vec![ 108 | 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 109 | 0b11111111, 110 | ].as_slice(), 111 | ); 112 | let mut input = vec![]; 113 | cid.encode(&mut input); 114 | assert_eq!(&*cid, input.as_slice()); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/protocol/codec.rs: -------------------------------------------------------------------------------- 1 | use bytes::{Buf, BufMut}; 2 | use error::Result; 3 | 4 | pub(super) trait Encoder { 5 | fn encode(&self, dst: &mut T) -> Result; 6 | } 7 | 8 | pub(super) trait Decoder: Sized { 9 | fn decode(&mut self, src: &mut T) -> Result; 10 | } 11 | -------------------------------------------------------------------------------- /src/protocol/mod.rs: -------------------------------------------------------------------------------- 1 | mod cid; 2 | mod codec; 3 | mod header; 4 | mod pn; 5 | mod varint; 6 | 7 | pub use self::cid::ConnectionID; 8 | 9 | use self::codec::{Decoder, Encoder}; 10 | use self::pn::{Generater, PackageNumber}; 11 | use self::varint::VarInt; 12 | 13 | pub const VERSION: u32 = 0xff00000C; 14 | -------------------------------------------------------------------------------- /src/protocol/pn.rs: -------------------------------------------------------------------------------- 1 | use super::{Decoder, Encoder}; 2 | use bytes::{Buf, BufMut, IntoBuf}; 3 | use error::{Error, ErrorKind, Result}; 4 | use std::{ 5 | self, sync::atomic::{AtomicU32, Ordering}, 6 | }; 7 | 8 | const MAX_PN_1: u32 = 0b01111111; 9 | const MAX_PN_2: u32 = 0b00111111_11111111; 10 | const MAX_PN_4: u32 = 0b00111111_11111111_11111111_11111111; 11 | 12 | const PN_1_FLAG: u8 = 0b0; 13 | const PN_2_FLAG: u8 = 0b10; 14 | const PN_4_FLAG: u8 = 0b11; 15 | 16 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] 17 | pub(crate) struct PackageNumber(u32); 18 | 19 | impl std::convert::Into for PackageNumber { 20 | fn into(self) -> u32 { 21 | self.0 22 | } 23 | } 24 | 25 | #[derive(Debug)] 26 | pub(crate) struct Generater(AtomicU32); 27 | 28 | impl Generater { 29 | // new create a Generater from 0 30 | pub fn new() -> Generater { 31 | Generater(0.into()) 32 | } 33 | 34 | // next get the package number which increase by 1 each call. 35 | // return None when reach the MAX_PN_4, then must close the connection. 36 | pub fn next(&self) -> Option { 37 | let pn = self.0.fetch_add(1, Ordering::SeqCst); 38 | match pn { 39 | 0..MAX_PN_4 => Some(PackageNumber(pn)), 40 | _ => None, 41 | } 42 | } 43 | } 44 | 45 | impl Encoder for PackageNumber { 46 | fn encode(&self, dst: &mut T) -> Result { 47 | Ok(match self.0 { 48 | 0..=MAX_PN_1 => { 49 | dst.put_uint_be((self.0 | ((PN_1_FLAG as u32) << 7)) as u64, 1); 50 | 1 51 | } 52 | 0..=MAX_PN_2 => { 53 | dst.put_uint_be((self.0 | ((PN_2_FLAG as u32) << 14)) as u64, 2); 54 | 2 55 | } 56 | 0..=MAX_PN_4 => { 57 | dst.put_uint_be((self.0 | ((PN_4_FLAG as u32) << 30)) as u64, 4); 58 | 4 59 | } 60 | v => panic!( 61 | "package number {} has overfolwn, maximum is {}", 62 | v, MAX_PN_4 63 | ), 64 | }) 65 | } 66 | } 67 | 68 | impl Decoder for PackageNumber { 69 | fn decode(&mut self, src: &mut T) -> Result { 70 | let first = src.get_u8(); 71 | let (v, n) = match first >> 7 { 72 | PN_1_FLAG => ((first as u32) & MAX_PN_1, 1), 73 | _ => match first >> 6 { 74 | PN_2_FLAG => (((first as u32) << 8 | (src.get_u8() as u32)) & MAX_PN_2, 2), 75 | PN_4_FLAG => ( 76 | ((first as u32) << 24 | (src.get_uint_be(3) as u32)) & MAX_PN_4, 77 | 4, 78 | ), 79 | _ => unreachable!(), 80 | }, 81 | }; 82 | self.0 = v; 83 | Ok(n) 84 | } 85 | } 86 | 87 | #[cfg(test)] 88 | mod tests { 89 | use super::*; 90 | 91 | fn decode_pn(input: Vec) -> (PackageNumber, usize) { 92 | let mut v = PackageNumber(0); 93 | let n = v.decode(&mut input.into_buf()).unwrap(); 94 | (v, n) 95 | } 96 | 97 | #[test] 98 | fn decode_pn1_test() { 99 | assert_eq!(decode_pn(vec![0b00000011]), (PackageNumber(3), 1)); 100 | } 101 | 102 | #[test] 103 | fn decode_pn2_test() { 104 | assert_eq!( 105 | decode_pn(vec![0b10000001, 0b00000001]), 106 | (PackageNumber(257), 2) 107 | ); 108 | } 109 | 110 | #[test] 111 | fn decode_pn4_test() { 112 | assert_eq!( 113 | decode_pn(vec![0b11000001, 0b00000001, 0b00000001, 0b0000000]), 114 | (PackageNumber(16843008), 4) 115 | ); 116 | } 117 | 118 | fn encode_pn(input: PackageNumber) -> Vec { 119 | let mut dst = vec![]; 120 | input.encode(&mut dst).unwrap(); 121 | dst 122 | } 123 | 124 | #[test] 125 | fn encode_pn1_test() { 126 | assert_eq!(encode_pn(PackageNumber(3)), vec![0b00000011]); 127 | } 128 | 129 | #[test] 130 | fn encode_pn2_test() { 131 | assert_eq!(encode_pn(PackageNumber(257)), vec![0b10000001, 0b00000001]); 132 | } 133 | 134 | #[test] 135 | fn encode_pn4_test() { 136 | assert_eq!( 137 | encode_pn(PackageNumber(16843009)), 138 | vec![0b11000001, 0b00000001, 0b00000001, 0b0000001], 139 | ); 140 | } 141 | 142 | #[test] 143 | fn builder_test() { 144 | let b = Generater::new(); 145 | assert_eq!(b.next(), Some(PackageNumber(0))); 146 | assert_eq!(b.next(), Some(PackageNumber(1))); 147 | assert_eq!(b.next(), Some(PackageNumber(2))); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/protocol/varint.rs: -------------------------------------------------------------------------------- 1 | use super::{Decoder, Encoder}; 2 | use bytes::{Buf, BufMut, IntoBuf}; 3 | use error::{Error, ErrorKind, Result}; 4 | use std; 5 | 6 | const MAX_INT_1: u64 = 0b00111111; 7 | const MAX_INT_2: u64 = 0b00111111_11111111; 8 | const MAX_INT_4: u64 = 0b00111111_11111111_11111111_11111111; 9 | const MAX_INT_8: u64 = 0b00111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111; 10 | 11 | const INT_1_FLAG: u8 = 0b00; 12 | const INT_2_FLAG: u8 = 0b01; 13 | const INT_4_FLAG: u8 = 0b10; 14 | const INT_8_FLAG: u8 = 0b11; 15 | 16 | #[derive(Debug, Default, PartialEq, Eq)] 17 | pub struct VarInt(u64); 18 | 19 | impl std::convert::From for VarInt { 20 | fn from(v: u64) -> VarInt { 21 | VarInt(v) 22 | } 23 | } 24 | 25 | impl Encoder for VarInt { 26 | fn encode(&self, dst: &mut T) -> Result { 27 | Ok(match self.0 { 28 | 0..=MAX_INT_1 => { 29 | dst.put_uint_be(self.0 | (INT_1_FLAG as u64) << 6, 1); 30 | 1 31 | } 32 | 0..=MAX_INT_2 => { 33 | dst.put_uint_be(self.0 | ((INT_2_FLAG as u64) << 14), 2); 34 | 2 35 | } 36 | 0..=MAX_INT_4 => { 37 | dst.put_uint_be(self.0 | ((INT_4_FLAG as u64) << 30), 4); 38 | 4 39 | } 40 | 0..=MAX_INT_8 => { 41 | dst.put_uint_be(self.0 | ((INT_8_FLAG as u64) << 62), 8); 42 | 8 43 | } 44 | v => panic!( 45 | "variable-length integer {} has overflown, maximum is {}", 46 | v, MAX_INT_8 47 | ), 48 | }) 49 | } 50 | } 51 | 52 | impl Decoder for VarInt { 53 | fn decode(&mut self, src: &mut T) -> Result { 54 | let first = src.get_u8(); 55 | let (v, n) = match first >> 6 { 56 | INT_1_FLAG => ((first as u64) & MAX_INT_1, 1), 57 | INT_2_FLAG => (((first as u64) << 8 | src.get_uint_be(1)) & MAX_INT_2, 2), 58 | INT_4_FLAG => (((first as u64) << 24 | src.get_uint_be(3)) & MAX_INT_4, 4), 59 | INT_8_FLAG => (((first as u64) << 56 | src.get_uint_be(7)) & MAX_INT_8, 8), 60 | _ => unreachable!(), 61 | }; 62 | self.0 = v; 63 | Ok(n) 64 | } 65 | } 66 | 67 | #[cfg(test)] 68 | mod tests { 69 | use super::*; 70 | 71 | fn decode_var_int(input: Vec) -> (VarInt, usize) { 72 | let mut v: VarInt = 0.into(); 73 | let n = v.decode(&mut input.into_buf()).unwrap(); 74 | (v, n) 75 | } 76 | 77 | #[test] 78 | fn decode_var_int1_test() { 79 | assert_eq!(decode_var_int(vec![0b00000011]), (VarInt(3), 1)); 80 | } 81 | 82 | #[test] 83 | fn decode_var_int2_test() { 84 | assert_eq!( 85 | decode_var_int(vec![0b01000001, 0b00000001]), 86 | (VarInt(257), 2) 87 | ); 88 | } 89 | 90 | #[test] 91 | fn decode_var_int4_test() { 92 | assert_eq!( 93 | decode_var_int(vec![0b10000001, 0b00000001, 0b00000001, 0b0000000]), 94 | (VarInt(16843008), 4) 95 | ); 96 | } 97 | 98 | #[test] 99 | fn decode_var_int8_test() { 100 | assert_eq!( 101 | decode_var_int(vec![ 102 | 0b11000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 103 | 0b00000001, 104 | ]), 105 | (VarInt(72340172838076673u64), 8) 106 | ); 107 | } 108 | 109 | fn encode_var_int(input: VarInt) -> Vec { 110 | let mut dst = vec![]; 111 | input.encode(&mut dst).unwrap(); 112 | dst 113 | } 114 | 115 | #[test] 116 | fn encode_var_int1_test() { 117 | assert_eq!(encode_var_int(3.into()), vec![0b00000011]); 118 | } 119 | 120 | #[test] 121 | fn encode_var_int2_test() { 122 | assert_eq!(encode_var_int(257.into()), vec![0b01000001, 0b00000001]); 123 | } 124 | 125 | #[test] 126 | fn encode_var_int4_test() { 127 | assert_eq!( 128 | encode_var_int(16843009.into()), 129 | vec![0b10000001, 0b00000001, 0b00000001, 0b0000001], 130 | ); 131 | } 132 | 133 | #[test] 134 | fn encode_var_int8_test() { 135 | assert_eq!( 136 | encode_var_int(72340172838076673u64.into()), 137 | vec![ 138 | 0b11000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 139 | 0b00000001, 140 | ], 141 | ); 142 | } 143 | } 144 | --------------------------------------------------------------------------------