├── .clang-format ├── .github └── workflows │ └── semgrep.yml ├── .gitignore ├── .travis.yml ├── AUTHORS ├── CMakeLists.txt ├── CMakeOptions.txt ├── COPYING ├── ChangeLog ├── Makefile.am ├── NEWS ├── README ├── README.rst ├── ci ├── build_openssl.sh └── gen-certificate.sh ├── cmake ├── ExtractValidFlags.cmake ├── FindCUnit.cmake ├── FindLibev.cmake └── Version.cmake ├── cmakeconfig.h.in ├── configure.ac ├── examples ├── .gitignore ├── CMakeLists.txt ├── Makefile.am ├── client.cc ├── client.h ├── crypto.cc ├── crypto.h ├── crypto_openssl.cc ├── debug.cc ├── debug.h ├── examplestest.cc ├── http.cc ├── http.h ├── keylog.cc ├── keylog.h ├── network.h ├── server.cc ├── server.h ├── shared.h ├── template.h ├── util.cc ├── util.h ├── util_test.cc └── util_test.h ├── lib ├── .gitignore ├── CMakeLists.txt ├── Makefile.am ├── includes │ ├── CMakeLists.txt │ ├── Makefile.am │ └── ngtcp2 │ │ ├── ngtcp2.h │ │ └── version.h.in ├── libngtcp2.pc.in ├── ngtcp2_acktr.c ├── ngtcp2_acktr.h ├── ngtcp2_buf.c ├── ngtcp2_buf.h ├── ngtcp2_cid.c ├── ngtcp2_cid.h ├── ngtcp2_conn.c ├── ngtcp2_conn.h ├── ngtcp2_conv.c ├── ngtcp2_conv.h ├── ngtcp2_crypto.c ├── ngtcp2_crypto.h ├── ngtcp2_err.c ├── ngtcp2_err.h ├── ngtcp2_gaptr.c ├── ngtcp2_gaptr.h ├── ngtcp2_idtr.c ├── ngtcp2_idtr.h ├── ngtcp2_ksl.c ├── ngtcp2_ksl.h ├── ngtcp2_log.c ├── ngtcp2_log.h ├── ngtcp2_macro.h ├── ngtcp2_map.c ├── ngtcp2_map.h ├── ngtcp2_mem.c ├── ngtcp2_mem.h ├── ngtcp2_pkt.c ├── ngtcp2_pkt.h ├── ngtcp2_ppe.c ├── ngtcp2_ppe.h ├── ngtcp2_pq.c ├── ngtcp2_pq.h ├── ngtcp2_psl.c ├── ngtcp2_psl.h ├── ngtcp2_range.c ├── ngtcp2_range.h ├── ngtcp2_ringbuf.c ├── ngtcp2_ringbuf.h ├── ngtcp2_rob.c ├── ngtcp2_rob.h ├── ngtcp2_rtb.c ├── ngtcp2_rtb.h ├── ngtcp2_str.c ├── ngtcp2_str.h ├── ngtcp2_strm.c └── ngtcp2_strm.h ├── m4 ├── ax_check_compile_flag.m4 └── ax_cxx_compile_stdcxx.m4 ├── tests ├── .gitignore ├── CMakeLists.txt ├── Makefile.am ├── main.c ├── ngtcp2_acktr_test.c ├── ngtcp2_acktr_test.h ├── ngtcp2_conn_test.c ├── ngtcp2_conn_test.h ├── ngtcp2_conv_test.c ├── ngtcp2_conv_test.h ├── ngtcp2_crypto_test.c ├── ngtcp2_crypto_test.h ├── ngtcp2_idtr_test.c ├── ngtcp2_idtr_test.h ├── ngtcp2_ksl_test.c ├── ngtcp2_ksl_test.h ├── ngtcp2_map_test.c ├── ngtcp2_map_test.h ├── ngtcp2_pkt_test.c ├── ngtcp2_pkt_test.h ├── ngtcp2_psl_test.c ├── ngtcp2_psl_test.h ├── ngtcp2_range_test.c ├── ngtcp2_range_test.h ├── ngtcp2_ringbuf_test.c ├── ngtcp2_ringbuf_test.h ├── ngtcp2_rob_test.c ├── ngtcp2_rob_test.h ├── ngtcp2_rtb_test.c ├── ngtcp2_rtb_test.h ├── ngtcp2_test_helper.c └── ngtcp2_test_helper.h └── third-party ├── CMakeLists.txt ├── Makefile.am └── http-parser ├── .gitignore ├── .mailmap ├── .travis.yml ├── AUTHORS ├── LICENSE-MIT ├── Makefile ├── README.md ├── bench.c ├── contrib ├── parsertrace.c └── url_parser.c ├── http_parser.c ├── http_parser.gyp ├── http_parser.h └── test.c /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | AccessModifierOffset: -2 4 | AlignAfterOpenBracket: Align 5 | AlignConsecutiveAssignments: false 6 | AlignConsecutiveDeclarations: false 7 | AlignEscapedNewlines: Right 8 | AlignOperands: true 9 | AlignTrailingComments: true 10 | AllowAllParametersOfDeclarationOnNextLine: true 11 | AllowShortBlocksOnASingleLine: false 12 | AllowShortCaseLabelsOnASingleLine: false 13 | AllowShortFunctionsOnASingleLine: All 14 | AllowShortIfStatementsOnASingleLine: false 15 | AllowShortLoopsOnASingleLine: false 16 | AlwaysBreakAfterDefinitionReturnType: None 17 | AlwaysBreakAfterReturnType: None 18 | AlwaysBreakBeforeMultilineStrings: false 19 | AlwaysBreakTemplateDeclarations: false 20 | BinPackArguments: true 21 | BinPackParameters: true 22 | BraceWrapping: 23 | AfterClass: false 24 | AfterControlStatement: false 25 | AfterEnum: false 26 | AfterFunction: false 27 | AfterNamespace: false 28 | AfterObjCDeclaration: false 29 | AfterStruct: false 30 | AfterUnion: false 31 | AfterExternBlock: false 32 | BeforeCatch: false 33 | BeforeElse: false 34 | IndentBraces: false 35 | SplitEmptyFunction: true 36 | SplitEmptyRecord: true 37 | SplitEmptyNamespace: true 38 | BreakBeforeBinaryOperators: None 39 | BreakBeforeBraces: Attach 40 | BreakBeforeInheritanceComma: false 41 | BreakBeforeTernaryOperators: true 42 | BreakConstructorInitializersBeforeComma: false 43 | BreakConstructorInitializers: BeforeColon 44 | BreakAfterJavaFieldAnnotations: false 45 | BreakStringLiterals: true 46 | ColumnLimit: 80 47 | CommentPragmas: '^ IWYU pragma:' 48 | CompactNamespaces: false 49 | ConstructorInitializerAllOnOneLineOrOnePerLine: true 50 | ConstructorInitializerIndentWidth: 4 51 | ContinuationIndentWidth: 4 52 | Cpp11BracedListStyle: true 53 | DerivePointerAlignment: false 54 | DisableFormat: false 55 | ExperimentalAutoDetectBinPacking: false 56 | FixNamespaceComments: true 57 | ForEachMacros: 58 | - foreach 59 | - Q_FOREACH 60 | - BOOST_FOREACH 61 | IncludeBlocks: Preserve 62 | IncludeCategories: 63 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 64 | Priority: 2 65 | - Regex: '^(<|"(gtest|isl|json)/)' 66 | Priority: 3 67 | - Regex: '.*' 68 | Priority: 1 69 | IncludeIsMainRegex: '$' 70 | IndentCaseLabels: false 71 | IndentPPDirectives: AfterHash 72 | IndentWidth: 2 73 | IndentWrappedFunctionNames: false 74 | JavaScriptQuotes: Leave 75 | JavaScriptWrapImports: true 76 | KeepEmptyLinesAtTheStartOfBlocks: true 77 | MacroBlockBegin: '' 78 | MacroBlockEnd: '' 79 | MaxEmptyLinesToKeep: 1 80 | NamespaceIndentation: None 81 | ObjCBlockIndentWidth: 2 82 | ObjCSpaceAfterProperty: false 83 | ObjCSpaceBeforeProtocolList: true 84 | PenaltyBreakAssignment: 2 85 | PenaltyBreakBeforeFirstCallParameter: 19 86 | PenaltyBreakComment: 300 87 | PenaltyBreakFirstLessLess: 120 88 | PenaltyBreakString: 1000 89 | PenaltyExcessCharacter: 1000000 90 | PenaltyReturnTypeOnItsOwnLine: 60 91 | PointerAlignment: Right 92 | RawStringFormats: 93 | - Delimiter: pb 94 | Language: TextProto 95 | BasedOnStyle: google 96 | ReflowComments: true 97 | SortIncludes: false 98 | SortUsingDeclarations: true 99 | SpaceAfterCStyleCast: false 100 | SpaceAfterTemplateKeyword: true 101 | SpaceBeforeAssignmentOperators: true 102 | SpaceBeforeParens: ControlStatements 103 | SpaceInEmptyParentheses: false 104 | SpacesBeforeTrailingComments: 1 105 | SpacesInAngles: false 106 | SpacesInContainerLiterals: true 107 | SpacesInCStyleCastParentheses: false 108 | SpacesInParentheses: false 109 | SpacesInSquareBrackets: false 110 | Standard: Cpp11 111 | TabWidth: 8 112 | UseTab: Never 113 | ... 114 | 115 | -------------------------------------------------------------------------------- /.github/workflows/semgrep.yml: -------------------------------------------------------------------------------- 1 | 2 | on: 3 | pull_request: {} 4 | workflow_dispatch: {} 5 | push: 6 | branches: 7 | - main 8 | - master 9 | schedule: 10 | - cron: '0 0 * * *' 11 | name: Semgrep config 12 | jobs: 13 | semgrep: 14 | name: semgrep/ci 15 | runs-on: ubuntu-20.04 16 | env: 17 | SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} 18 | SEMGREP_URL: https://cloudflare.semgrep.dev 19 | SEMGREP_APP_URL: https://cloudflare.semgrep.dev 20 | SEMGREP_VERSION_CHECK_URL: https://cloudflare.semgrep.dev/api/check-version 21 | container: 22 | image: returntocorp/semgrep 23 | steps: 24 | - uses: actions/checkout@v3 25 | - run: semgrep ci 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # emacs backup file 2 | *~ 3 | 4 | # autotools 5 | *.la 6 | *.lo 7 | *.m4 8 | *.o 9 | *.pyc 10 | .deps/ 11 | .libs/ 12 | INSTALL 13 | Makefile 14 | Makefile.in 15 | autom4te.cache/ 16 | compile 17 | config.guess 18 | config.h 19 | config.h.in 20 | config.log 21 | config.status 22 | config.sub 23 | configure 24 | depcomp 25 | install-sh 26 | libtool 27 | ltmain.sh 28 | missing 29 | stamp-h1 30 | test-driver 31 | 32 | # test logs generated by `make check` 33 | *.log 34 | *.trs 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | language: cpp 3 | os: 4 | - osx 5 | - linux 6 | compiler: 7 | - clang 8 | - gcc 9 | sudo: required 10 | env: 11 | - CI_BUILD=cmake 12 | - CI_BUILD=autotools 13 | matrix: 14 | exclude: 15 | # Exclude gcc build (Need some work) with osx 16 | - os: osx 17 | compiler: gcc 18 | addons: 19 | apt: 20 | sources: 21 | - ubuntu-toolchain-r-test 22 | - llvm-toolchain-trusty-5.0 23 | packages: 24 | - g++-7 25 | - clang-5.0 26 | - autoconf 27 | - automake 28 | - autotools-dev 29 | - libtool 30 | - pkg-config 31 | - libcunit1-dev 32 | - libssl-dev 33 | - libev-dev 34 | - libcunit1-dev 35 | - cmake 36 | - cmake-data 37 | before_install: 38 | - $CC --version 39 | - if [ "$TRAVIS_OS_NAME" == "linux" ]; then CMAKE_OPTS=" -DENABLE_ASAN=1" AUTOTOOLS_OPTS=" --enable-asan"; fi 40 | - if [ "$TRAVIS_OS_NAME" == "linux" ]; then if [ "$CXX" = "g++" ]; then export CXX="g++-7" CC="gcc-7" EXTRA_LDFLAGS="-fuse-ld=gold"; else export CXX="clang++-5.0" CC="clang-5.0"; fi; fi 41 | - $CC --version 42 | - cmake --version 43 | before_script: 44 | # First build external lib 45 | - ./ci/build_openssl.sh 46 | - if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew install libev cunit; fi 47 | # configure ngtcp2 48 | - if [ "$CI_BUILD" == "autotools" ]; then autoreconf -i; fi 49 | - export PKG_CONFIG_PATH=$PWD/../openssl/build/lib/pkgconfig LDFLAGS="$EXTRA_LDFLAGS -Wl,-rpath,$PWD/../openssl/build/lib" 50 | - if [ "$CI_BUILD" == "autotools" ]; then ./configure --enable-werror $AUTOTOOLS_OPTS; fi 51 | # Set CMAKE_LIBRARY_ARCHITECTURE to workaround failure to parse implicit link information from GCC 5 52 | - if [ "$CI_BUILD" == "cmake" ]; then cmake $CMAKE_OPTS -DCMAKE_LIBRARY_ARCHITECTURE=x86_64-linux-gnu; fi 53 | script: 54 | # Now build ngtcp2 examples and test 55 | - make 56 | - make check 57 | - ./ci/gen-certificate.sh 58 | - ./examples/server localhost 4433 cert/server.key cert/server.crt & 59 | - ./examples/client localhost 4433 60 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Tatsuhiro Tsujikawa 2 | -------------------------------------------------------------------------------- /CMakeOptions.txt: -------------------------------------------------------------------------------- 1 | # Features that can be enabled for cmake (see CMakeLists.txt) 2 | 3 | option(ENABLE_WERROR "Make compiler warnings fatal" OFF) 4 | option(ENABLE_DEBUG "Turn on debug output") 5 | option(ENABLE_ASAN "Enable AddressSanitizer (ASAN)" OFF) 6 | 7 | # vim: ft=cmake: 8 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2016 ngtcp2 contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudflare/ngtcp2/e4d0b67e78c0cedc32f1c6e5c48e75c4d4282ff7/ChangeLog -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2016 ngtcp2 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | SUBDIRS = lib tests third-party examples 24 | 25 | ACLOCAL_AMFLAGS = -I m4 26 | 27 | # Format source files using clang-format. Don't format source files 28 | # under third-party directory since we are not responsible for thier 29 | # coding style. 30 | clang-format: 31 | CLANGFORMAT=`git config --get clangformat.binary`; \ 32 | test -z $${CLANGFORMAT} && CLANGFORMAT="clang-format"; \ 33 | $${CLANGFORMAT} -i lib/*.{c,h} lib/includes/ngtcp2/*.h \ 34 | examples/*.{cc,h} 35 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cloudflare/ngtcp2/e4d0b67e78c0cedc32f1c6e5c48e75c4d4282ff7/NEWS -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | See README.rst 2 | -------------------------------------------------------------------------------- /ci/build_openssl.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #build last openssl master (for Travis) 3 | 4 | cd .. 5 | git clone --depth 1 -b quic-draft-13 https://github.com/tatsuhiro-t/openssl 6 | cd openssl 7 | ./config enable-tls1_3 --prefix=$PWD/build 8 | make -j$(nproc) 9 | make install_sw 10 | cd .. 11 | -------------------------------------------------------------------------------- /ci/gen-certificate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Generate a self-signed certificate for testing purposes. 3 | 4 | mkdir -p cert 5 | keyfile=cert/server.key 6 | certfile=cert/server.crt 7 | 8 | openssl req -newkey rsa:2048 -x509 -nodes -keyout "$keyfile" -new -out "$certfile" -subj /CN=localhost 9 | -------------------------------------------------------------------------------- /cmake/ExtractValidFlags.cmake: -------------------------------------------------------------------------------- 1 | # Convenience function that checks the availability of certain 2 | # C or C++ compiler flags and returns valid ones as a string. 3 | 4 | include(CheckCCompilerFlag) 5 | include(CheckCXXCompilerFlag) 6 | 7 | function(extract_valid_c_flags varname) 8 | set(valid_flags) 9 | foreach(flag IN LISTS ARGN) 10 | string(REGEX REPLACE "[^a-zA-Z0-9_]+" "_" flag_var ${flag}) 11 | set(flag_var "C_FLAG_${flag_var}") 12 | check_c_compiler_flag("${flag}" "${flag_var}") 13 | if(${flag_var}) 14 | set(valid_flags "${valid_flags} ${flag}") 15 | endif() 16 | endforeach() 17 | set(${varname} "${valid_flags}" PARENT_SCOPE) 18 | endfunction() 19 | 20 | function(extract_valid_cxx_flags varname) 21 | set(valid_flags) 22 | foreach(flag IN LISTS ARGN) 23 | string(REGEX REPLACE "[^a-zA-Z0-9_]+" "_" flag_var ${flag}) 24 | set(flag_var "CXX_FLAG_${flag_var}") 25 | check_cxx_compiler_flag("${flag}" "${flag_var}") 26 | if(${flag_var}) 27 | set(valid_flags "${valid_flags} ${flag}") 28 | endif() 29 | endforeach() 30 | set(${varname} "${valid_flags}" PARENT_SCOPE) 31 | endfunction() 32 | -------------------------------------------------------------------------------- /cmake/FindCUnit.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find cunit 2 | # Once done this will define 3 | # CUNIT_FOUND - System has cunit 4 | # CUNIT_INCLUDE_DIRS - The cunit include directories 5 | # CUNIT_LIBRARIES - The libraries needed to use cunit 6 | 7 | find_package(PkgConfig QUIET) 8 | pkg_check_modules(PC_CUNIT QUIET cunit) 9 | 10 | find_path(CUNIT_INCLUDE_DIR 11 | NAMES CUnit/CUnit.h 12 | HINTS ${PC_CUNIT_INCLUDE_DIRS} 13 | ) 14 | find_library(CUNIT_LIBRARY 15 | NAMES cunit 16 | HINTS ${PC_CUNIT_LIBRARY_DIRS} 17 | ) 18 | 19 | if(CUNIT_INCLUDE_DIR) 20 | set(_version_regex "^#define[ \t]+CU_VERSION[ \t]+\"([^\"]+)\".*") 21 | file(STRINGS "${CUNIT_INCLUDE_DIR}/CUnit/CUnit.h" 22 | CUNIT_VERSION REGEX "${_version_regex}") 23 | string(REGEX REPLACE "${_version_regex}" "\\1" 24 | CUNIT_VERSION "${CUNIT_VERSION}") 25 | unset(_version_regex) 26 | endif() 27 | 28 | include(FindPackageHandleStandardArgs) 29 | # handle the QUIETLY and REQUIRED arguments and set CUNIT_FOUND to TRUE 30 | # if all listed variables are TRUE and the requested version matches. 31 | find_package_handle_standard_args(CUnit REQUIRED_VARS 32 | CUNIT_LIBRARY CUNIT_INCLUDE_DIR 33 | VERSION_VAR CUNIT_VERSION) 34 | 35 | if(CUNIT_FOUND) 36 | set(CUNIT_LIBRARIES ${CUNIT_LIBRARY}) 37 | set(CUNIT_INCLUDE_DIRS ${CUNIT_INCLUDE_DIR}) 38 | endif() 39 | 40 | mark_as_advanced(CUNIT_INCLUDE_DIR CUNIT_LIBRARY) 41 | -------------------------------------------------------------------------------- /cmake/FindLibev.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find libev 2 | # Once done this will define 3 | # LIBEV_FOUND - System has libev 4 | # LIBEV_INCLUDE_DIRS - The libev include directories 5 | # LIBEV_LIBRARIES - The libraries needed to use libev 6 | 7 | find_path(LIBEV_INCLUDE_DIR 8 | NAMES ev.h 9 | ) 10 | find_library(LIBEV_LIBRARY 11 | NAMES ev 12 | ) 13 | 14 | if(LIBEV_INCLUDE_DIR) 15 | file(STRINGS "${LIBEV_INCLUDE_DIR}/ev.h" 16 | LIBEV_VERSION_MAJOR REGEX "^#define[ \t]+EV_VERSION_MAJOR[ \t]+[0-9]+") 17 | file(STRINGS "${LIBEV_INCLUDE_DIR}/ev.h" 18 | LIBEV_VERSION_MINOR REGEX "^#define[ \t]+EV_VERSION_MINOR[ \t]+[0-9]+") 19 | string(REGEX REPLACE "[^0-9]+" "" LIBEV_VERSION_MAJOR "${LIBEV_VERSION_MAJOR}") 20 | string(REGEX REPLACE "[^0-9]+" "" LIBEV_VERSION_MINOR "${LIBEV_VERSION_MINOR}") 21 | set(LIBEV_VERSION "${LIBEV_VERSION_MAJOR}.${LIBEV_VERSION_MINOR}") 22 | unset(LIBEV_VERSION_MINOR) 23 | unset(LIBEV_VERSION_MAJOR) 24 | endif() 25 | 26 | include(FindPackageHandleStandardArgs) 27 | # handle the QUIETLY and REQUIRED arguments and set LIBEV_FOUND to TRUE 28 | # if all listed variables are TRUE and the requested version matches. 29 | find_package_handle_standard_args(Libev REQUIRED_VARS 30 | LIBEV_LIBRARY LIBEV_INCLUDE_DIR 31 | VERSION_VAR LIBEV_VERSION) 32 | 33 | if(LIBEV_FOUND) 34 | set(LIBEV_LIBRARIES ${LIBEV_LIBRARY}) 35 | set(LIBEV_INCLUDE_DIRS ${LIBEV_INCLUDE_DIR}) 36 | endif() 37 | 38 | mark_as_advanced(LIBEV_INCLUDE_DIR LIBEV_LIBRARY) 39 | -------------------------------------------------------------------------------- /cmake/Version.cmake: -------------------------------------------------------------------------------- 1 | # Converts a version such as 1.2.255 to 0x0102ff 2 | function(HexVersion version_hex_var major minor patch) 3 | math(EXPR version_dec "${major} * 256 * 256 + ${minor} * 256 + ${patch}") 4 | set(version_hex "0x") 5 | foreach(i RANGE 5 0 -1) 6 | math(EXPR num "(${version_dec} >> (4 * ${i})) & 15") 7 | string(SUBSTRING "0123456789abcdef" ${num} 1 num_hex) 8 | set(version_hex "${version_hex}${num_hex}") 9 | endforeach() 10 | set(${version_hex_var} "${version_hex}" PARENT_SCOPE) 11 | endfunction() 12 | -------------------------------------------------------------------------------- /cmakeconfig.h.in: -------------------------------------------------------------------------------- 1 | 2 | /* Define to `int' if does not define. */ 3 | #cmakedefine ssize_t @ssize_t@ 4 | 5 | /* Define to 1 to enable debug output. */ 6 | #cmakedefine DEBUGBUILD 1 7 | 8 | /* Define to 1 if you have the header file. */ 9 | #cmakedefine HAVE_ARPA_INET_H 1 10 | 11 | /* Define to 1 if you have the header file. */ 12 | #cmakedefine HAVE_STDDEF_H 1 13 | 14 | /* Define to 1 if you have the header file. */ 15 | #cmakedefine HAVE_STDINT_H 1 16 | 17 | /* Define to 1 if you have the header file. */ 18 | #cmakedefine HAVE_STDLIB_H 1 19 | 20 | /* Define to 1 if you have the header file. */ 21 | #cmakedefine HAVE_STRING_H 1 22 | 23 | /* Define to 1 if you have the header file. */ 24 | #cmakedefine HAVE_UNISTD_H 1 25 | -------------------------------------------------------------------------------- /examples/.gitignore: -------------------------------------------------------------------------------- 1 | client 2 | server 3 | examplestest 4 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2017 ngtcp2 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | if(LIBEV_FOUND AND OPENSSL_FOUND) 25 | include_directories( 26 | ${CMAKE_SOURCE_DIR}/lib/includes 27 | ${CMAKE_BINARY_DIR}/lib/includes 28 | ${CMAKE_SOURCE_DIR}/third-party 29 | 30 | ${OPENSSL_INCLUDE_DIRS} 31 | ${LIBEV_INCLUDE_DIRS} 32 | ) 33 | 34 | link_libraries( 35 | ngtcp2 36 | ${OPENSSL_LIBRARIES} 37 | ${LIBEV_LIBRARIES} 38 | ) 39 | 40 | set(client_SOURCES 41 | client.cc 42 | debug.cc 43 | util.cc 44 | keylog.cc 45 | crypto_openssl.cc 46 | crypto.cc 47 | ) 48 | 49 | set(server_SOURCES 50 | server.cc 51 | debug.cc 52 | util.cc 53 | keylog.cc 54 | crypto_openssl.cc 55 | crypto.cc 56 | http.cc 57 | ) 58 | 59 | add_executable(client ${client_SOURCES} $) 60 | add_executable(server ${server_SOURCES} $) 61 | set_target_properties(client PROPERTIES 62 | COMPILE_FLAGS "${WARNCXXFLAGS}" 63 | CXX_STANDARD 14 64 | CXX_STANDARD_REQUIRED ON 65 | ) 66 | set_target_properties(server PROPERTIES 67 | COMPILE_FLAGS "${WARNCXXFLAGS}" 68 | CXX_STANDARD 14 69 | CXX_STANDARD_REQUIRED ON 70 | ) 71 | 72 | # TODO prevent client and example servers from being installed? 73 | else() 74 | message(WARNING "Examples are disabled due to lack of good libev or OpenSSL") 75 | endif() 76 | -------------------------------------------------------------------------------- /examples/Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2017 ngtcp2 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | AM_CFLAGS = $(WARNCFLAGS) $(DEBUGCFLAGS) 25 | AM_CXXFLAGS = $(WARNCXXFLAGS) $(DEBUGCFLAGS) 26 | AM_CPPFLAGS = \ 27 | -I$(top_srcdir)/lib/includes \ 28 | -I$(top_builddir)/lib/includes \ 29 | -I$(top_srcdir)/third-party \ 30 | @OPENSSL_CFLAGS@ \ 31 | @LIBEV_CFLAGS@ \ 32 | @DEFS@ 33 | AM_LDFLAGS = -no-install 34 | LDADD = $(top_builddir)/lib/libngtcp2.la \ 35 | $(top_builddir)/third-party/libhttp-parser.la \ 36 | @OPENSSL_LIBS@ \ 37 | @LIBEV_LIBS@ 38 | 39 | noinst_PROGRAMS = client server 40 | 41 | client_SOURCES = client.cc client.h \ 42 | template.h \ 43 | debug.cc debug.h \ 44 | util.cc util.h \ 45 | keylog.cc keylog.h \ 46 | shared.h \ 47 | crypto_openssl.cc \ 48 | crypto.cc 49 | 50 | server_SOURCES = server.cc server.h \ 51 | template.h \ 52 | debug.cc debug.h \ 53 | util.cc util.h \ 54 | keylog.cc keylog.h \ 55 | shared.h \ 56 | crypto_openssl.cc \ 57 | crypto.cc \ 58 | http.cc http.h 59 | 60 | if HAVE_CUNIT 61 | check_PROGRAMS = examplestest 62 | examplestest_SOURCES = examplestest.cc \ 63 | util_test.cc util_test.h util.cc util.h 64 | examplestest_LDADD = ${LDADD} @CUNIT_LIBS@ 65 | 66 | TESTS = examplestest 67 | endif # HAVE_CUNIT 68 | -------------------------------------------------------------------------------- /examples/debug.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "debug.h" 26 | 27 | #include 28 | 29 | #include "util.h" 30 | 31 | namespace ngtcp2 { 32 | 33 | namespace debug { 34 | 35 | namespace { 36 | auto randgen = util::make_mt19937(); 37 | } // namespace 38 | 39 | namespace { 40 | auto color_output = false; 41 | } // namespace 42 | 43 | void set_color_output(bool f) { color_output = f; } 44 | 45 | namespace { 46 | auto *outfile = stderr; 47 | } // namespace 48 | 49 | int handshake_completed(ngtcp2_conn *conn, void *user_data) { 50 | fprintf(outfile, "QUIC handshake has completed\n"); 51 | return 0; 52 | } 53 | 54 | bool packet_lost(double prob) { 55 | auto p = std::uniform_real_distribution<>(0, 1)(randgen); 56 | return p < prob; 57 | } 58 | 59 | void print_crypto_data(const uint8_t *data, size_t datalen) { 60 | fprintf(outfile, "Ordered CRYPTO data\n"); 61 | util::hexdump(outfile, data, datalen); 62 | } 63 | 64 | void print_stream_data(uint64_t stream_id, const uint8_t *data, 65 | size_t datalen) { 66 | fprintf(outfile, "Ordered STREAM data stream_id=0x%" PRIx64 "\n", stream_id); 67 | util::hexdump(outfile, data, datalen); 68 | } 69 | 70 | void print_initial_secret(const uint8_t *data, size_t len) { 71 | fprintf(outfile, "initial_secret=%s\n", util::format_hex(data, len).c_str()); 72 | } 73 | 74 | void print_client_in_secret(const uint8_t *data, size_t len) { 75 | fprintf(outfile, "client_in_secret=%s\n", 76 | util::format_hex(data, len).c_str()); 77 | } 78 | 79 | void print_server_in_secret(const uint8_t *data, size_t len) { 80 | fprintf(outfile, "server_in_secret=%s\n", 81 | util::format_hex(data, len).c_str()); 82 | } 83 | 84 | void print_handshake_secret(const uint8_t *data, size_t len) { 85 | fprintf(outfile, "handshake_secret=%s\n", 86 | util::format_hex(data, len).c_str()); 87 | } 88 | 89 | void print_client_hs_secret(const uint8_t *data, size_t len) { 90 | fprintf(outfile, "client_hs_secret=%s\n", 91 | util::format_hex(data, len).c_str()); 92 | } 93 | 94 | void print_server_hs_secret(const uint8_t *data, size_t len) { 95 | fprintf(outfile, "server_hs_secret=%s\n", 96 | util::format_hex(data, len).c_str()); 97 | } 98 | 99 | void print_client_0rtt_secret(const uint8_t *data, size_t len) { 100 | fprintf(outfile, "client_0rtt_secret=%s\n", 101 | util::format_hex(data, len).c_str()); 102 | } 103 | 104 | void print_client_1rtt_secret(const uint8_t *data, size_t len) { 105 | fprintf(outfile, "client_1rtt_secret=%s\n", 106 | util::format_hex(data, len).c_str()); 107 | } 108 | 109 | void print_server_1rtt_secret(const uint8_t *data, size_t len) { 110 | fprintf(outfile, "server_1rtt_secret=%s\n", 111 | util::format_hex(data, len).c_str()); 112 | } 113 | 114 | void print_client_pp_key(const uint8_t *data, size_t len) { 115 | fprintf(outfile, "+ client_pp_key=%s\n", util::format_hex(data, len).c_str()); 116 | } 117 | 118 | void print_server_pp_key(const uint8_t *data, size_t len) { 119 | fprintf(outfile, "+ server_pp_key=%s\n", util::format_hex(data, len).c_str()); 120 | } 121 | 122 | void print_client_pp_iv(const uint8_t *data, size_t len) { 123 | fprintf(outfile, "+ client_pp_iv=%s\n", util::format_hex(data, len).c_str()); 124 | } 125 | 126 | void print_server_pp_iv(const uint8_t *data, size_t len) { 127 | fprintf(outfile, "+ server_pp_iv=%s\n", util::format_hex(data, len).c_str()); 128 | } 129 | 130 | void print_client_pp_pn(const uint8_t *data, size_t len) { 131 | fprintf(outfile, "+ client_pp_pn=%s\n", util::format_hex(data, len).c_str()); 132 | } 133 | 134 | void print_server_pp_pn(const uint8_t *data, size_t len) { 135 | fprintf(outfile, "+ server_pp_pn=%s\n", util::format_hex(data, len).c_str()); 136 | } 137 | 138 | void log_printf(void *user_data, const char *fmt, ...) { 139 | va_list ap; 140 | 141 | va_start(ap, fmt); 142 | vfprintf(stderr, fmt, ap); 143 | va_end(ap); 144 | } 145 | 146 | } // namespace debug 147 | 148 | } // namespace ngtcp2 149 | -------------------------------------------------------------------------------- /examples/debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef DEBUG_H 26 | #define DEBUG_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // HAVE_CONFIG_H 31 | 32 | // For travis and PRIu64 33 | #define __STDC_FORMAT_MACROS 34 | #include 35 | 36 | #include 37 | 38 | #include 39 | 40 | namespace ngtcp2 { 41 | 42 | namespace debug { 43 | 44 | void set_color_output(bool f); 45 | 46 | int handshake_completed(ngtcp2_conn *conn, void *user_data); 47 | 48 | bool packet_lost(double prob); 49 | 50 | void print_crypto_data(const uint8_t *data, size_t datalen); 51 | 52 | void print_stream_data(uint64_t stream_id, const uint8_t *data, size_t datalen); 53 | 54 | void print_initial_secret(const uint8_t *data, size_t len); 55 | 56 | void print_client_in_secret(const uint8_t *data, size_t len); 57 | void print_server_in_secret(const uint8_t *data, size_t len); 58 | 59 | void print_handshake_secret(const uint8_t *data, size_t len); 60 | 61 | void print_client_hs_secret(const uint8_t *data, size_t len); 62 | void print_server_hs_secret(const uint8_t *data, size_t len); 63 | 64 | void print_client_0rtt_secret(const uint8_t *data, size_t len); 65 | 66 | void print_client_1rtt_secret(const uint8_t *data, size_t len); 67 | void print_server_1rtt_secret(const uint8_t *data, size_t len); 68 | 69 | void print_client_pp_key(const uint8_t *data, size_t len); 70 | void print_server_pp_key(const uint8_t *data, size_t len); 71 | 72 | void print_client_pp_iv(const uint8_t *data, size_t len); 73 | void print_server_pp_iv(const uint8_t *data, size_t len); 74 | 75 | void print_client_pp_pn(const uint8_t *data, size_t len); 76 | void print_server_pp_pn(const uint8_t *data, size_t len); 77 | 78 | void log_printf(void *user_data, const char *fmt, ...); 79 | 80 | } // namespace debug 81 | 82 | } // namespace ngtcp2 83 | 84 | #endif // DEBUG_H 85 | -------------------------------------------------------------------------------- /examples/examplestest.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * Copyright (c) 2013 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifdef HAVE_CONFIG_H 27 | # include 28 | #endif // HAVE_CONFIG_H 29 | 30 | #include 31 | #include 32 | #include 33 | // include test cases' include files here 34 | #include "util_test.h" 35 | 36 | static int init_suite1(void) { return 0; } 37 | 38 | static int clean_suite1(void) { return 0; } 39 | 40 | int main(int argc, char *argv[]) { 41 | CU_pSuite pSuite = NULL; 42 | unsigned int num_tests_failed; 43 | 44 | // initialize the CUnit test registry 45 | if (CUE_SUCCESS != CU_initialize_registry()) 46 | return CU_get_error(); 47 | 48 | // add a suite to the registry 49 | pSuite = CU_add_suite("TestSuite", init_suite1, clean_suite1); 50 | if (NULL == pSuite) { 51 | CU_cleanup_registry(); 52 | return CU_get_error(); 53 | } 54 | 55 | // add the tests to the suite 56 | if (!CU_add_test(pSuite, "util_format_duration", 57 | ngtcp2::test_util_format_duration)) { 58 | CU_cleanup_registry(); 59 | return CU_get_error(); 60 | } 61 | 62 | // Run all tests using the CUnit Basic interface 63 | CU_basic_set_mode(CU_BRM_VERBOSE); 64 | CU_basic_run_tests(); 65 | num_tests_failed = CU_get_number_of_tests_failed(); 66 | CU_cleanup_registry(); 67 | if (CU_get_error() == CUE_SUCCESS) { 68 | return num_tests_failed; 69 | } else { 70 | printf("CUnit Error: %s\n", CU_get_error_msg()); 71 | return CU_get_error(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /examples/http.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2012 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "http.h" 27 | 28 | namespace ngtcp2 { 29 | 30 | namespace http { 31 | 32 | std::string get_reason_phrase(unsigned int status_code) { 33 | switch (status_code) { 34 | case 100: 35 | return "Continue"; 36 | case 101: 37 | return "Switching Protocols"; 38 | case 200: 39 | return "OK"; 40 | case 201: 41 | return "Created"; 42 | case 202: 43 | return "Accepted"; 44 | case 203: 45 | return "Non-Authoritative Information"; 46 | case 204: 47 | return "No Content"; 48 | case 205: 49 | return "Reset Content"; 50 | case 206: 51 | return "Partial Content"; 52 | case 300: 53 | return "Multiple Choices"; 54 | case 301: 55 | return "Moved Permanently"; 56 | case 302: 57 | return "Found"; 58 | case 303: 59 | return "See Other"; 60 | case 304: 61 | return "Not Modified"; 62 | case 305: 63 | return "Use Proxy"; 64 | // case 306: return "(Unused)"; 65 | case 307: 66 | return "Temporary Redirect"; 67 | case 308: 68 | return "Permanent Redirect"; 69 | case 400: 70 | return "Bad Request"; 71 | case 401: 72 | return "Unauthorized"; 73 | case 402: 74 | return "Payment Required"; 75 | case 403: 76 | return "Forbidden"; 77 | case 404: 78 | return "Not Found"; 79 | case 405: 80 | return "Method Not Allowed"; 81 | case 406: 82 | return "Not Acceptable"; 83 | case 407: 84 | return "Proxy Authentication Required"; 85 | case 408: 86 | return "Request Timeout"; 87 | case 409: 88 | return "Conflict"; 89 | case 410: 90 | return "Gone"; 91 | case 411: 92 | return "Length Required"; 93 | case 412: 94 | return "Precondition Failed"; 95 | case 413: 96 | return "Payload Too Large"; 97 | case 414: 98 | return "URI Too Long"; 99 | case 415: 100 | return "Unsupported Media Type"; 101 | case 416: 102 | return "Requested Range Not Satisfiable"; 103 | case 417: 104 | return "Expectation Failed"; 105 | case 421: 106 | return "Misdirected Request"; 107 | case 426: 108 | return "Upgrade Required"; 109 | case 428: 110 | return "Precondition Required"; 111 | case 429: 112 | return "Too Many Requests"; 113 | case 431: 114 | return "Request Header Fields Too Large"; 115 | case 451: 116 | return "Unavailable For Legal Reasons"; 117 | case 500: 118 | return "Internal Server Error"; 119 | case 501: 120 | return "Not Implemented"; 121 | case 502: 122 | return "Bad Gateway"; 123 | case 503: 124 | return "Service Unavailable"; 125 | case 504: 126 | return "Gateway Timeout"; 127 | case 505: 128 | return "HTTP Version Not Supported"; 129 | case 511: 130 | return "Network Authentication Required"; 131 | default: 132 | return ""; 133 | } 134 | } 135 | 136 | } // namespace http 137 | 138 | } // namespace ngtcp2 139 | -------------------------------------------------------------------------------- /examples/http.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef HTTP_H 26 | #define HTTP_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // HAVE_CONFIG_H 31 | 32 | #include 33 | 34 | namespace ngtcp2 { 35 | 36 | namespace http { 37 | 38 | std::string get_reason_phrase(unsigned int status_code); 39 | 40 | } // namespace http 41 | 42 | } // namespace ngtcp2 43 | 44 | #endif // HTTP_H 45 | -------------------------------------------------------------------------------- /examples/keylog.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include 26 | 27 | #include "keylog.h" 28 | #include "util.h" 29 | 30 | namespace ngtcp2 { 31 | 32 | namespace keylog { 33 | 34 | void log_secret(SSL *ssl, int name, const unsigned char *secret, 35 | size_t secretlen) { 36 | if (auto keylog_cb = SSL_CTX_get_keylog_callback(SSL_get_SSL_CTX(ssl))) { 37 | unsigned char crandom[32]; 38 | if (SSL_get_client_random(ssl, crandom, 32) != 32) { 39 | return; 40 | } 41 | std::string line; 42 | switch (name) { 43 | case SSL_KEY_CLIENT_EARLY_TRAFFIC: 44 | line = "QUIC_CLIENT_EARLY_TRAFFIC_SECRET"; 45 | break; 46 | case SSL_KEY_CLIENT_HANDSHAKE_TRAFFIC: 47 | line = "QUIC_CLIENT_HANDSHAKE_TRAFFIC_SECRET"; 48 | break; 49 | case SSL_KEY_CLIENT_APPLICATION_TRAFFIC: 50 | line = "QUIC_CLIENT_TRAFFIC_SECRET_0"; 51 | break; 52 | case SSL_KEY_SERVER_HANDSHAKE_TRAFFIC: 53 | line = "QUIC_SERVER_HANDSHAKE_TRAFFIC_SECRET"; 54 | break; 55 | case SSL_KEY_SERVER_APPLICATION_TRAFFIC: 56 | line = "QUIC_SERVER_TRAFFIC_SECRET_0"; 57 | break; 58 | default: 59 | return; 60 | } 61 | line += " " + util::format_hex(crandom, 32); 62 | line += " " + util::format_hex(secret, secretlen); 63 | keylog_cb(ssl, line.c_str()); 64 | } 65 | } 66 | 67 | } // namespace keylog 68 | 69 | } // namespace ngtcp2 70 | -------------------------------------------------------------------------------- /examples/keylog.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef KEYLOG_H 26 | #define KEYLOG_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // HAVE_CONFIG_H 31 | 32 | #include 33 | 34 | #include 35 | 36 | namespace ngtcp2 { 37 | 38 | namespace keylog { 39 | 40 | void log_secret(SSL *ssl, int name, const unsigned char *secret, 41 | size_t secretlen); 42 | 43 | } // namespace keylog 44 | 45 | } // namespace ngtcp2 46 | 47 | #endif // KEYLOG_H 48 | -------------------------------------------------------------------------------- /examples/network.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2016 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NETWORK_H 27 | #define NETWORK_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif // HAVE_CONFIG_H 32 | 33 | #include 34 | #ifdef HAVE_SYS_SOCKET_H 35 | # include 36 | #endif // HAVE_SYS_SOCKET_H 37 | #include 38 | #ifdef HAVE_NETINET_IN_H 39 | # include 40 | #endif // HAVE_NETINET_IN_H 41 | #ifdef HAVE_ARPA_INET_H 42 | # include 43 | #endif // HAVE_ARPA_INET_H 44 | 45 | namespace ngtcp2 { 46 | 47 | enum network_error { 48 | NETWORK_ERR_OK = 0, 49 | NETWORK_ERR_SEND_FATAL = -10, 50 | NETWORK_ERR_SEND_NON_FATAL = -11, 51 | NETWORK_ERR_CLOSE_WAIT = -12, 52 | }; 53 | 54 | union sockaddr_union { 55 | sockaddr_storage storage; 56 | sockaddr sa; 57 | sockaddr_in6 in6; 58 | sockaddr_in in; 59 | }; 60 | 61 | struct Address { 62 | socklen_t len; 63 | union sockaddr_union su; 64 | }; 65 | 66 | } // namespace ngtcp2 67 | 68 | #endif // NETWORK_H 69 | -------------------------------------------------------------------------------- /examples/shared.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef SHARED_H 26 | #define SHARED_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif // HAVE_CONFIG_H 31 | 32 | #include 33 | 34 | namespace ngtcp2 { 35 | 36 | constexpr uint16_t NGTCP2_APP_NOERROR = 0xff00; 37 | constexpr uint16_t NGTCP2_APP_PROTO = 0xff01; 38 | 39 | } // namespace ngtcp2 40 | 41 | #endif // SHARED_H 42 | -------------------------------------------------------------------------------- /examples/template.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2015 ngttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef TEMPLATE_H 27 | #define TEMPLATE_H 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | // inspired by , but our 34 | // template can take functions returning other than void. 35 | template struct Defer { 36 | Defer(F &&f, T &&... t) 37 | : f(std::bind(std::forward(f), std::forward(t)...)) {} 38 | Defer(Defer &&o) noexcept : f(std::move(o.f)) {} 39 | ~Defer() { f(); } 40 | 41 | using ResultType = typename std::result_of::type( 42 | typename std::decay::type...)>::type; 43 | std::function f; 44 | }; 45 | 46 | template Defer defer(F &&f, T &&... t) { 47 | return Defer(std::forward(f), std::forward(t)...); 48 | } 49 | 50 | template constexpr size_t array_size(T (&)[N]) { 51 | return N; 52 | } 53 | 54 | template constexpr size_t str_size(T (&)[N]) { 55 | return N - 1; 56 | } 57 | 58 | // User-defined literals for K, M, and G (powers of 1024) 59 | 60 | constexpr unsigned long long operator"" _k(unsigned long long k) { 61 | return k * 1024; 62 | } 63 | 64 | constexpr unsigned long long operator"" _m(unsigned long long m) { 65 | return m * 1024 * 1024; 66 | } 67 | 68 | constexpr unsigned long long operator"" _g(unsigned long long g) { 69 | return g * 1024 * 1024 * 1024; 70 | } 71 | 72 | #endif // TEMPLATE_H 73 | -------------------------------------------------------------------------------- /examples/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2012 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef UTIL_H 27 | #define UTIL_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif // HAVE_CONFIG_H 32 | 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | #include 39 | 40 | namespace ngtcp2 { 41 | 42 | namespace util { 43 | 44 | std::string format_hex(uint8_t c); 45 | 46 | std::string format_hex(const uint8_t *s, size_t len); 47 | 48 | std::string format_hex(const std::string &s); 49 | 50 | template std::string format_hex(const uint8_t (&s)[N]) { 51 | return format_hex(s, N); 52 | } 53 | 54 | // format_duration formats |ns| in human readable manner. |ns| must 55 | // be nanoseconds resolution. This function uses the largest unit so 56 | // that the integral part is strictly more than zero, and the 57 | // precision is at most 2 digits. For example, 1234 is formatted as 58 | // "1.23us". The largest unit is seconds. 59 | std::string format_duration(uint64_t ns); 60 | 61 | std::mt19937 make_mt19937(); 62 | 63 | ngtcp2_tstamp timestamp(struct ev_loop *loop); 64 | 65 | bool numeric_host(const char *hostname); 66 | 67 | bool numeric_host(const char *hostname, int family); 68 | 69 | // Dumps |src| of length |len| in the format similar to `hexdump -C`. 70 | void hexdump(FILE *out, const uint8_t *src, size_t len); 71 | 72 | inline char lowcase(char c) { 73 | constexpr static unsigned char tbl[] = { 74 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 75 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 76 | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 77 | 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 78 | 60, 61, 62, 63, 64, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 79 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 80 | 'z', 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 81 | 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 82 | 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 83 | 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 84 | 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 85 | 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 86 | 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 87 | 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 88 | 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 89 | 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 90 | 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 91 | 255, 92 | }; 93 | return tbl[static_cast(c)]; 94 | } 95 | 96 | struct CaseCmp { 97 | bool operator()(char lhs, char rhs) const { 98 | return lowcase(lhs) == lowcase(rhs); 99 | } 100 | }; 101 | 102 | template 103 | bool istarts_with(InputIterator1 first1, InputIterator1 last1, 104 | InputIterator2 first2, InputIterator2 last2) { 105 | if (last1 - first1 < last2 - first2) { 106 | return false; 107 | } 108 | return std::equal(first2, last2, first1, CaseCmp()); 109 | } 110 | 111 | template bool istarts_with(const S &a, const T &b) { 112 | return istarts_with(a.begin(), a.end(), b.begin(), b.end()); 113 | } 114 | 115 | template 116 | bool istarts_with_l(const T &a, const CharT (&b)[N]) { 117 | return istarts_with(a.begin(), a.end(), b, b + N - 1); 118 | } 119 | 120 | // make_cid_key returns the key for |cid|. 121 | std::string make_cid_key(const ngtcp2_cid *cid); 122 | 123 | } // namespace util 124 | 125 | } // namespace ngtcp2 126 | 127 | #endif // UTIL_H 128 | -------------------------------------------------------------------------------- /examples/util_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "util_test.h" 26 | 27 | #include 28 | 29 | #include "util.h" 30 | 31 | namespace ngtcp2 { 32 | 33 | void test_util_format_duration() { 34 | CU_ASSERT("0ns" == util::format_duration(0)); 35 | CU_ASSERT("999ns" == util::format_duration(999)); 36 | CU_ASSERT("1.00us" == util::format_duration(1000)); 37 | CU_ASSERT("1.00us" == util::format_duration(1004)); 38 | CU_ASSERT("1.00us" == util::format_duration(1005)); 39 | CU_ASSERT("1.02us" == util::format_duration(1015)); 40 | CU_ASSERT("2.00us" == util::format_duration(1999)); 41 | CU_ASSERT("1.00ms" == util::format_duration(999999)); 42 | CU_ASSERT("3.50ms" == util::format_duration(3500111)); 43 | CU_ASSERT("9999.99s" == util::format_duration(9999990000000llu)); 44 | } 45 | 46 | } // namespace ngtcp2 47 | -------------------------------------------------------------------------------- /examples/util_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef UTIL_TEST_H 26 | #define UTIL_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include "config.h" 30 | #endif // HAVE_CONFIG_H 31 | 32 | namespace ngtcp2 { 33 | 34 | void test_util_format_duration(); 35 | 36 | } // namespace ngtcp2 37 | 38 | #endif // UTIL_TEST_H 39 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | includes/ngtcp2/version.h 2 | libngtcp2.pc 3 | -------------------------------------------------------------------------------- /lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # ngtcp2 3 | 4 | # Copyright (c) 2016 ngtcp2 5 | 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | add_subdirectory(includes) 26 | 27 | include_directories( 28 | "${CMAKE_CURRENT_SOURCE_DIR}/includes" 29 | "${CMAKE_CURRENT_BINARY_DIR}/includes" 30 | ) 31 | 32 | add_definitions(-DBUILDING_NGTCP2) 33 | 34 | set(ngtcp2_SOURCES 35 | ngtcp2_pkt.c 36 | ngtcp2_conv.c 37 | ngtcp2_str.c 38 | ngtcp2_buf.c 39 | ngtcp2_conn.c 40 | ngtcp2_mem.c 41 | ngtcp2_pq.c 42 | ngtcp2_map.c 43 | ngtcp2_rob.c 44 | ngtcp2_ppe.c 45 | ngtcp2_crypto.c 46 | ngtcp2_err.c 47 | ngtcp2_range.c 48 | ngtcp2_acktr.c 49 | ngtcp2_rtb.c 50 | ngtcp2_strm.c 51 | ngtcp2_idtr.c 52 | ngtcp2_gaptr.c 53 | ngtcp2_ringbuf.c 54 | ngtcp2_log.c 55 | ngtcp2_cid.c 56 | ngtcp2_psl.c 57 | ngtcp2_ksl.c 58 | ) 59 | 60 | # Public shared library 61 | add_library(ngtcp2 SHARED ${ngtcp2_SOURCES}) 62 | set_target_properties(ngtcp2 PROPERTIES 63 | COMPILE_FLAGS "${WARNCFLAGS}" 64 | VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION} 65 | C_VISIBILITY_PRESET hidden 66 | ) 67 | 68 | if(HAVE_CUNIT) 69 | # Static library (for unittests because of symbol visibility) 70 | add_library(ngtcp2_static STATIC ${ngtcp2_SOURCES}) 71 | set_target_properties(ngtcp2_static PROPERTIES 72 | COMPILE_FLAGS "${WARNCFLAGS}" 73 | VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION} 74 | ARCHIVE_OUTPUT_NAME ngtcp2 75 | ) 76 | target_compile_definitions(ngtcp2_static PUBLIC "-DNGTCP2_STATICLIB") 77 | endif() 78 | 79 | install(TARGETS ngtcp2 80 | DESTINATION "${CMAKE_INSTALL_LIBDIR}") 81 | 82 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libngtcp2.pc" 83 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 84 | -------------------------------------------------------------------------------- /lib/Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2016 ngtcp2 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | SUBDIRS = includes 24 | 25 | AM_CFLAGS = $(WARNCFLAGS) $(DEBUGCFLAGS) $(EXTRACFLAG) 26 | AM_CPPFLAGS = -I$(srcdir)/includes -I$(builddir)/includes -DBUILDING_NGTCP2 27 | 28 | pkgconfigdir = $(libdir)/pkgconfig 29 | pkgconfig_DATA = libngtcp2.pc 30 | DISTCLEANFILES = $(pkgconfig_DATA) 31 | 32 | lib_LTLIBRARIES = libngtcp2.la 33 | 34 | OBJECTS = \ 35 | ngtcp2_pkt.c \ 36 | ngtcp2_conv.c \ 37 | ngtcp2_str.c \ 38 | ngtcp2_buf.c \ 39 | ngtcp2_conn.c \ 40 | ngtcp2_mem.c \ 41 | ngtcp2_pq.c \ 42 | ngtcp2_map.c \ 43 | ngtcp2_rob.c \ 44 | ngtcp2_ppe.c \ 45 | ngtcp2_crypto.c \ 46 | ngtcp2_err.c \ 47 | ngtcp2_range.c \ 48 | ngtcp2_acktr.c \ 49 | ngtcp2_rtb.c \ 50 | ngtcp2_strm.c \ 51 | ngtcp2_idtr.c \ 52 | ngtcp2_gaptr.c \ 53 | ngtcp2_ringbuf.c \ 54 | ngtcp2_log.c \ 55 | ngtcp2_cid.c \ 56 | ngtcp2_psl.c \ 57 | ngtcp2_ksl.c 58 | 59 | HFILES = \ 60 | ngtcp2_pkt.h \ 61 | ngtcp2_conv.h \ 62 | ngtcp2_str.h \ 63 | ngtcp2_buf.h \ 64 | ngtcp2_conn.h \ 65 | ngtcp2_mem.h \ 66 | ngtcp2_pq.h \ 67 | ngtcp2_map.h \ 68 | ngtcp2_rob.h \ 69 | ngtcp2_ppe.h \ 70 | ngtcp2_crypto.h \ 71 | ngtcp2_err.h \ 72 | ngtcp2_range.h \ 73 | ngtcp2_acktr.h \ 74 | ngtcp2_rtb.h \ 75 | ngtcp2_strm.h \ 76 | ngtcp2_idtr.h \ 77 | ngtcp2_gaptr.h \ 78 | ngtcp2_ringbuf.h \ 79 | ngtcp2_log.h \ 80 | ngtcp2_cid.h \ 81 | ngtcp2_psl.h \ 82 | ngtcp2_ksl.h \ 83 | ngtcp2_macro.h 84 | 85 | libngtcp2_la_SOURCES = $(HFILES) $(OBJECTS) 86 | libngtcp2_la_LDFLAGS = -no-undefined \ 87 | -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) 88 | -------------------------------------------------------------------------------- /lib/includes/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | install(FILES 2 | ngtcp2/ngtcp2.h 3 | "${CMAKE_CURRENT_BINARY_DIR}/ngtcp2/version.h" 4 | DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ngtcp2") 5 | -------------------------------------------------------------------------------- /lib/includes/Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2016 ngtcp2 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | nobase_include_HEADERS = ngtcp2/ngtcp2.h ngtcp2/version.h 25 | -------------------------------------------------------------------------------- /lib/includes/ngtcp2/version.h.in: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2016 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef VERSION_H 26 | #define VERSION_H 27 | 28 | /** 29 | * @macro 30 | * 31 | * Version number of the ngtcp2 library release. 32 | */ 33 | #define NGTCP2_VERSION "@PACKAGE_VERSION@" 34 | 35 | /** 36 | * @macro 37 | * 38 | * Numerical representation of the version number of the ngtcp2 39 | * library release. This is a 24 bit number with 8 bits for major 40 | * number, 8 bits for minor and 8 bits for patch. Version 1.2.3 41 | * becomes 0x010203. 42 | */ 43 | #define NGTCP2_VERSION_NUM @PACKAGE_VERSION_NUM@ 44 | 45 | #endif /* VERSION_H */ 46 | -------------------------------------------------------------------------------- /lib/libngtcp2.pc.in: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2016 ngtcp2 contributors 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | prefix=@prefix@ 24 | exec_prefix=@exec_prefix@ 25 | libdir=@libdir@ 26 | includedir=@includedir@ 27 | 28 | Name: libngtcp2 29 | Description: ngtcp2 library 30 | URL: https://github.com/ngtcp2/ngtcp2 31 | Version: @VERSION@ 32 | Libs: -L${libdir} -lngtcp2 33 | Cflags: -I${includedir} 34 | -------------------------------------------------------------------------------- /lib/ngtcp2_buf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_buf.h" 26 | 27 | void ngtcp2_buf_init(ngtcp2_buf *buf, uint8_t *begin, size_t len) { 28 | buf->begin = buf->pos = buf->last = begin; 29 | buf->end = begin + len; 30 | } 31 | 32 | size_t ngtcp2_buf_left(ngtcp2_buf *buf) { 33 | return (size_t)(buf->end - buf->last); 34 | } 35 | 36 | size_t ngtcp2_buf_len(ngtcp2_buf *buf) { 37 | return (size_t)(buf->last - buf->pos); 38 | } 39 | -------------------------------------------------------------------------------- /lib/ngtcp2_buf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_BUF_H 26 | #define NGTCP2_BUF_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | typedef struct { 35 | /* begin points to the beginning of the buffer. */ 36 | uint8_t *begin; 37 | /* end points to the one beyond of the last byte of the buffer */ 38 | uint8_t *end; 39 | /* pos pointers to the start of data. Typically, this points to the 40 | point that next data should be read. Initially, it points to 41 | |begin|. */ 42 | uint8_t *pos; 43 | /* last points to the one beyond of the last data of the buffer. 44 | Typically, new data is written at this point. Initially, it 45 | points to |begin|. */ 46 | uint8_t *last; 47 | } ngtcp2_buf; 48 | 49 | /* 50 | * ngtcp2_buf_init initializes |buf| with the given buffer. 51 | */ 52 | void ngtcp2_buf_init(ngtcp2_buf *buf, uint8_t *begin, size_t len); 53 | 54 | /* 55 | * ngtcp2_buf_left returns the number of additional bytes which can be 56 | * written to the underlying buffer. In other words, it returns 57 | * buf->end - buf->last. 58 | */ 59 | size_t ngtcp2_buf_left(ngtcp2_buf *buf); 60 | 61 | /* 62 | * ngtcp2_buf_len returns the number of bytes left to read. In other 63 | * words, it returns buf->last - buf->pos. 64 | */ 65 | size_t ngtcp2_buf_len(ngtcp2_buf *buf); 66 | 67 | #endif /* NGTCP2_BUF_H */ 68 | -------------------------------------------------------------------------------- /lib/ngtcp2_cid.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_cid.h" 26 | 27 | #include 28 | #include 29 | 30 | #include "ngtcp2_str.h" 31 | 32 | void ngtcp2_cid_zero(ngtcp2_cid *cid) { cid->datalen = 0; } 33 | 34 | void ngtcp2_cid_init(ngtcp2_cid *cid, const uint8_t *data, size_t datalen) { 35 | assert(datalen <= NGTCP2_MAX_CIDLEN); 36 | 37 | cid->datalen = datalen; 38 | if (datalen) { 39 | ngtcp2_cpymem(cid->data, data, datalen); 40 | } 41 | } 42 | 43 | int ngtcp2_cid_eq(const ngtcp2_cid *cid, const ngtcp2_cid *other) { 44 | return cid->datalen == other->datalen && 45 | 0 == memcmp(cid->data, other->data, cid->datalen); 46 | } 47 | 48 | int ngtcp2_cid_empty(const ngtcp2_cid *cid) { return cid->datalen == 0; } 49 | -------------------------------------------------------------------------------- /lib/ngtcp2_cid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_CID_H 26 | #define NGTCP2_CID_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | /* ngtcp2_cid_zero makes |cid| zero-length. */ 35 | void ngtcp2_cid_zero(ngtcp2_cid *cid); 36 | 37 | /* 38 | * ngtcp2_cid_eq returns nonzero if |cid| and |other| share the same 39 | * connection ID. 40 | */ 41 | int ngtcp2_cid_eq(const ngtcp2_cid *cid, const ngtcp2_cid *other); 42 | 43 | /* 44 | * ngtcp2_cid_empty returns nonzero if |cid| includes empty connection 45 | * ID. 46 | */ 47 | int ngtcp2_cid_empty(const ngtcp2_cid *cid); 48 | 49 | #endif /* NGTCP2_CID_H */ 50 | -------------------------------------------------------------------------------- /lib/ngtcp2_crypto.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_CRYPTO_H 26 | #define NGTCP2_CRYPTO_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include "ngtcp2_mem.h" 35 | 36 | /* NGTCP2_INITIAL_AEAD_OVERHEAD is an overhead of AEAD used by Initial 37 | packets. Because QUIC uses AEAD_AES_128_GCM, the overhead is 16 38 | bytes. */ 39 | #define NGTCP2_INITIAL_AEAD_OVERHEAD 16 40 | 41 | /* NGTCP2_MAX_AEAD_OVERHEAD is expected maximum AEAD overhead. */ 42 | #define NGTCP2_MAX_AEAD_OVERHEAD 16 43 | 44 | /* NGTCP2_PN_SAMPLELEN is the number bytes sampled when encoding a 45 | packet number. */ 46 | #define NGTCP2_PN_SAMPLELEN 16 47 | 48 | typedef struct { 49 | const uint8_t *key; 50 | size_t keylen; 51 | const uint8_t *iv; 52 | size_t ivlen; 53 | const uint8_t *pn; 54 | size_t pnlen; 55 | } ngtcp2_crypto_km; 56 | 57 | int ngtcp2_crypto_km_new(ngtcp2_crypto_km **pckm, const uint8_t *key, 58 | size_t keylen, const uint8_t *iv, size_t ivlen, 59 | const uint8_t *pn, size_t pnlen, ngtcp2_mem *mem); 60 | 61 | void ngtcp2_crypto_km_del(ngtcp2_crypto_km *ckm, ngtcp2_mem *mem); 62 | 63 | typedef struct { 64 | const ngtcp2_crypto_km *ckm; 65 | size_t aead_overhead; 66 | ngtcp2_encrypt encrypt; 67 | ngtcp2_decrypt decrypt; 68 | ngtcp2_encrypt_pn encrypt_pn; 69 | void *user_data; 70 | } ngtcp2_crypto_ctx; 71 | 72 | void ngtcp2_crypto_create_nonce(uint8_t *dest, const uint8_t *iv, size_t ivlen, 73 | uint64_t pkt_num); 74 | 75 | #endif /* NGTCP2_CRYPTO_H */ 76 | -------------------------------------------------------------------------------- /lib/ngtcp2_err.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_err.h" 26 | 27 | const char *ngtcp2_strerror(int liberr) { 28 | switch (liberr) { 29 | case 0: 30 | return "NO_ERROR"; 31 | case NGTCP2_ERR_INVALID_ARGUMENT: 32 | return "ERR_INVALID_ARGUMENT"; 33 | case NGTCP2_ERR_UNKNOWN_PKT_TYPE: 34 | return "ERR_UNKNOWN_PKT_TYPE"; 35 | case NGTCP2_ERR_NOBUF: 36 | return "ERR_NOBUF"; 37 | case NGTCP2_ERR_BAD_PKT_HASH: 38 | return "ERR_BAD_PKT_HASH"; 39 | case NGTCP2_ERR_PROTO: 40 | return "ERR_PROTO"; 41 | case NGTCP2_ERR_INVALID_STATE: 42 | return "ERR_INVALID_STATE"; 43 | case NGTCP2_ERR_ACK_FRAME: 44 | return "ERR_ACK_FRAME"; 45 | case NGTCP2_ERR_STREAM_ID_BLOCKED: 46 | return "ERR_STREAM_ID_BLOCKED"; 47 | case NGTCP2_ERR_STREAM_IN_USE: 48 | return "ERR_STREAM_IN_USE"; 49 | case NGTCP2_ERR_STREAM_DATA_BLOCKED: 50 | return "ERR_STREAM_DATA_BLOCKED"; 51 | case NGTCP2_ERR_FLOW_CONTROL: 52 | return "ERR_FLOW_CONTROL"; 53 | case NGTCP2_ERR_PKT_TIMEOUT: 54 | return "ERR_PKT_TIMEOUT"; 55 | case NGTCP2_ERR_STREAM_ID: 56 | return "ERR_STREAM_ID"; 57 | case NGTCP2_ERR_FINAL_OFFSET: 58 | return "ERR_FINAL_OFFSET"; 59 | case NGTCP2_ERR_CRYPTO: 60 | return "ERR_CRYPTO"; 61 | case NGTCP2_ERR_PKT_NUM_EXHAUSTED: 62 | return "ERR_PKT_NUM_EXHAUSTED"; 63 | case NGTCP2_ERR_NOMEM: 64 | return "ERR_NOMEM"; 65 | case NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM: 66 | return "ERR_REQUIRED_TRANSPORT_PARAM"; 67 | case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM: 68 | return "ERR_MALFORMED_TRANSPORT_PARAM"; 69 | case NGTCP2_ERR_FRAME_ENCODING: 70 | return "ERR_FRAME_ENCODING"; 71 | case NGTCP2_ERR_TLS_DECRYPT: 72 | return "ERR_TLS_DECRYPT"; 73 | case NGTCP2_ERR_STREAM_SHUT_WR: 74 | return "ERR_STREAM_SHUT_WR"; 75 | case NGTCP2_ERR_STREAM_NOT_FOUND: 76 | return "ERR_STREAM_NOT_FOUND"; 77 | case NGTCP2_ERR_VERSION_NEGOTIATION: 78 | return "ERR_VERSION_NEGOTIATION"; 79 | case NGTCP2_ERR_STREAM_STATE: 80 | return "ERR_STREAM_STATE"; 81 | case NGTCP2_ERR_NOKEY: 82 | return "ERR_NOKEY"; 83 | case NGTCP2_ERR_EARLY_DATA_REJECTED: 84 | return "ERR_EARLY_DATA_REJECTED"; 85 | case NGTCP2_ERR_RECV_VERSION_NEGOTIATION: 86 | return "ERR_RECV_VERSION_NEGOTIATION"; 87 | case NGTCP2_ERR_CLOSING: 88 | return "ERR_CLOSING"; 89 | case NGTCP2_ERR_DRAINING: 90 | return "ERR_DRAINING"; 91 | case NGTCP2_ERR_PKT_ENCODING: 92 | return "ERR_PKT_ENCODING"; 93 | case NGTCP2_ERR_CONGESTION: 94 | return "ERR_CONGESTION"; 95 | case NGTCP2_ERR_CALLBACK_FAILURE: 96 | return "ERR_CALLBACK_FAILURE"; 97 | case NGTCP2_ERR_INTERNAL: 98 | return "ERR_INTERNAL"; 99 | default: 100 | return "(unknown)"; 101 | } 102 | } 103 | 104 | int ngtcp2_err_is_fatal(int liberr) { return liberr < NGTCP2_ERR_FATAL; } 105 | 106 | uint16_t ngtcp2_err_infer_quic_transport_error_code(int liberr) { 107 | switch (liberr) { 108 | case 0: 109 | return NGTCP2_NO_ERROR; 110 | case NGTCP2_ERR_ACK_FRAME: 111 | case NGTCP2_ERR_FRAME_ENCODING: 112 | return NGTCP2_FRAME_ENCODING_ERROR; 113 | case NGTCP2_ERR_FLOW_CONTROL: 114 | return NGTCP2_FLOW_CONTROL_ERROR; 115 | case NGTCP2_ERR_STREAM_ID: 116 | return NGTCP2_STREAM_ID_ERROR; 117 | case NGTCP2_ERR_FINAL_OFFSET: 118 | return NGTCP2_FINAL_OFFSET_ERROR; 119 | case NGTCP2_ERR_REQUIRED_TRANSPORT_PARAM: 120 | return NGTCP2_TRANSPORT_PARAMETER_ERROR; 121 | case NGTCP2_ERR_INVALID_ARGUMENT: 122 | return NGTCP2_INTERNAL_ERROR; 123 | case NGTCP2_ERR_STREAM_STATE: 124 | return NGTCP2_STREAM_STATE_ERROR; 125 | case NGTCP2_ERR_VERSION_NEGOTIATION: 126 | return NGTCP2_VERSION_NEGOTIATION_ERROR; 127 | default: 128 | return NGTCP2_PROTOCOL_VIOLATION; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /lib/ngtcp2_err.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_ERR_H 26 | #define NGTCP2_ERR_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #endif /* NGTCP2_ERR_H */ 35 | -------------------------------------------------------------------------------- /lib/ngtcp2_gaptr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_gaptr.h" 26 | 27 | #include 28 | #include 29 | 30 | #include "ngtcp2_macro.h" 31 | 32 | int ngtcp2_gaptr_gap_new(ngtcp2_gaptr_gap **pg, uint64_t begin, uint64_t end, 33 | ngtcp2_mem *mem) { 34 | *pg = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_gaptr_gap)); 35 | if (*pg == NULL) { 36 | return NGTCP2_ERR_NOMEM; 37 | } 38 | 39 | ngtcp2_range_init(&(*pg)->range, begin, end); 40 | (*pg)->next = NULL; 41 | 42 | return 0; 43 | } 44 | 45 | void ngtcp2_gaptr_gap_del(ngtcp2_gaptr_gap *g, ngtcp2_mem *mem) { 46 | ngtcp2_mem_free(mem, g); 47 | } 48 | 49 | int ngtcp2_gaptr_init(ngtcp2_gaptr *gaptr, ngtcp2_mem *mem) { 50 | int rv; 51 | 52 | rv = ngtcp2_gaptr_gap_new(&gaptr->gap, 0, UINT64_MAX, mem); 53 | if (rv != 0) { 54 | return rv; 55 | } 56 | 57 | gaptr->mem = mem; 58 | 59 | return 0; 60 | } 61 | 62 | void ngtcp2_gaptr_free(ngtcp2_gaptr *gaptr) { 63 | ngtcp2_gaptr_gap *g, *ng; 64 | 65 | if (gaptr == NULL) { 66 | return; 67 | } 68 | 69 | for (g = gaptr->gap; g;) { 70 | ng = g->next; 71 | ngtcp2_gaptr_gap_del(g, gaptr->mem); 72 | g = ng; 73 | } 74 | } 75 | 76 | static void insert_gap(ngtcp2_gaptr_gap **pg, ngtcp2_gaptr_gap *g) { 77 | g->next = (*pg)->next; 78 | (*pg)->next = g; 79 | } 80 | 81 | static void remove_gap(ngtcp2_gaptr_gap **pg, ngtcp2_mem *mem) { 82 | ngtcp2_gaptr_gap *g = *pg; 83 | *pg = g->next; 84 | ngtcp2_gaptr_gap_del(g, mem); 85 | } 86 | 87 | int ngtcp2_gaptr_push(ngtcp2_gaptr *gaptr, uint64_t offset, size_t datalen) { 88 | int rv; 89 | ngtcp2_gaptr_gap **pg; 90 | ngtcp2_range m, l, r, q = {offset, offset + datalen}; 91 | 92 | for (pg = &gaptr->gap; *pg;) { 93 | m = ngtcp2_range_intersect(&q, &(*pg)->range); 94 | if (ngtcp2_range_len(&m)) { 95 | if (ngtcp2_range_eq(&(*pg)->range, &m)) { 96 | remove_gap(pg, gaptr->mem); 97 | continue; 98 | } 99 | ngtcp2_range_cut(&l, &r, &(*pg)->range, &m); 100 | if (ngtcp2_range_len(&l)) { 101 | (*pg)->range = l; 102 | 103 | if (ngtcp2_range_len(&r)) { 104 | ngtcp2_gaptr_gap *ng; 105 | rv = ngtcp2_gaptr_gap_new(&ng, r.begin, r.end, gaptr->mem); 106 | if (rv != 0) { 107 | return rv; 108 | } 109 | insert_gap(pg, ng); 110 | pg = &((*pg)->next); 111 | } 112 | } else if (ngtcp2_range_len(&r)) { 113 | (*pg)->range = r; 114 | } 115 | } 116 | if (ngtcp2_range_not_after(&q, &(*pg)->range)) { 117 | break; 118 | } 119 | pg = &((*pg)->next); 120 | } 121 | return 0; 122 | } 123 | 124 | uint64_t ngtcp2_gaptr_first_gap_offset(ngtcp2_gaptr *gaptr) { 125 | if (gaptr->gap) { 126 | return gaptr->gap->range.begin; 127 | } 128 | return UINT64_MAX; 129 | } 130 | -------------------------------------------------------------------------------- /lib/ngtcp2_gaptr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_GAPTR_H 26 | #define NGTCP2_GAPTR_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include "ngtcp2_mem.h" 35 | #include "ngtcp2_range.h" 36 | 37 | struct ngtcp2_gaptr_gap; 38 | typedef struct ngtcp2_gaptr_gap ngtcp2_gaptr_gap; 39 | 40 | /* 41 | * ngtcp2_gaptr_gap represents the gap, which is the range of stream 42 | * data that is not received yet. 43 | */ 44 | struct ngtcp2_gaptr_gap { 45 | /* next points to the next gap. This singly linked list is ordered 46 | by range.begin in the increasing order, and they never 47 | overlap. */ 48 | ngtcp2_gaptr_gap *next; 49 | /* range is the range of this gap. */ 50 | ngtcp2_range range; 51 | }; 52 | 53 | /* 54 | * ngtcp2_gaptr_gap_new allocates new ngtcp2_gaptr_gap object, and 55 | * assigns its pointer to |*pg|. The caller should call 56 | * ngtcp2_gaptr_gap_del to delete it when it is no longer used. The 57 | * range of the gap is [begin, end). |mem| is custom memory allocator 58 | * to allocate memory. 59 | * 60 | * This function returns 0 if it succeeds, or one of the following 61 | * negative error codes: 62 | * 63 | * NGTCP2_ERR_NOMEM 64 | * Out of memory. 65 | */ 66 | int ngtcp2_gaptr_gap_new(ngtcp2_gaptr_gap **pg, uint64_t begin, uint64_t end, 67 | ngtcp2_mem *mem); 68 | 69 | /* 70 | * ngtcp2_gaptr_gap_del deallocates |g|. It deallocates the memory 71 | * pointed by |g| it self. |mem| is custom memory allocator to 72 | * deallocate memory. 73 | */ 74 | void ngtcp2_gaptr_gap_del(ngtcp2_gaptr_gap *g, ngtcp2_mem *mem); 75 | 76 | /* 77 | * ngtcp2_gaptr maintains the gap in the range [0, UINT64_MAX). 78 | */ 79 | typedef struct { 80 | /* gap maintains the range of offset which is not received 81 | yet. Initially, its range is [0, UINT64_MAX). */ 82 | ngtcp2_gaptr_gap *gap; 83 | /* mem is custom memory allocator */ 84 | ngtcp2_mem *mem; 85 | } ngtcp2_gaptr; 86 | 87 | /* 88 | * ngtcp2_gaptr_init initializes |gaptr|. 89 | * 90 | * This function returns 0 if it succeeds, or one of the following 91 | * negative error codes: 92 | * 93 | * NGTCP2_ERR_NOMEM 94 | * Out of memory. 95 | */ 96 | int ngtcp2_gaptr_init(ngtcp2_gaptr *gaptr, ngtcp2_mem *mem); 97 | 98 | /* 99 | * ngtcp2_gaptr_free frees resources allocated for |gaptr|. 100 | */ 101 | void ngtcp2_gaptr_free(ngtcp2_gaptr *gaptr); 102 | 103 | /* 104 | * ngtcp2_gaptr_push adds new data of length |datalen| at the stream 105 | * offset |offset|. 106 | * 107 | * This function returns 0 if it succeeds, or one of the following 108 | * negative error codes: 109 | * 110 | * NGTCP2_ERR_NOMEM 111 | * Out of memory 112 | */ 113 | int ngtcp2_gaptr_push(ngtcp2_gaptr *gaptr, uint64_t offset, size_t datalen); 114 | 115 | /* 116 | * ngtcp2_gaptr_first_gap_offset returns the offset to the first gap. 117 | * If there is no gap, it returns UINT64_MAX. 118 | */ 119 | uint64_t ngtcp2_gaptr_first_gap_offset(ngtcp2_gaptr *gaptr); 120 | 121 | #endif /* NGTCP2_GAPTR_H */ 122 | -------------------------------------------------------------------------------- /lib/ngtcp2_idtr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_idtr.h" 26 | 27 | #include 28 | 29 | int ngtcp2_idtr_gap_new(ngtcp2_idtr_gap **pg, uint64_t begin, uint64_t end, 30 | ngtcp2_mem *mem) { 31 | *pg = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_idtr_gap)); 32 | if (*pg == NULL) { 33 | return NGTCP2_ERR_NOMEM; 34 | } 35 | 36 | ngtcp2_range_init(&(*pg)->range, begin, end); 37 | (*pg)->next = NULL; 38 | 39 | return 0; 40 | } 41 | 42 | void ngtcp2_idtr_gap_del(ngtcp2_idtr_gap *g, ngtcp2_mem *mem) { 43 | ngtcp2_mem_free(mem, g); 44 | } 45 | 46 | int ngtcp2_idtr_init(ngtcp2_idtr *idtr, int server, ngtcp2_mem *mem) { 47 | int rv; 48 | 49 | rv = ngtcp2_idtr_gap_new(&idtr->gap, 0, UINT64_MAX, mem); 50 | if (rv != 0) { 51 | return rv; 52 | } 53 | 54 | idtr->server = server; 55 | idtr->mem = mem; 56 | 57 | return 0; 58 | } 59 | 60 | void ngtcp2_idtr_free(ngtcp2_idtr *idtr) { 61 | ngtcp2_idtr_gap *g, *next; 62 | 63 | if (idtr == NULL) { 64 | return; 65 | } 66 | 67 | for (g = idtr->gap; g;) { 68 | next = g->next; 69 | ngtcp2_idtr_gap_del(g, idtr->mem); 70 | g = next; 71 | } 72 | } 73 | 74 | /* 75 | * id_from_stream_id translates |stream_id| to id space used by 76 | * ngtcp2_idtr. 77 | */ 78 | static uint64_t id_from_stream_id(uint64_t stream_id) { return stream_id >> 2; } 79 | 80 | int ngtcp2_idtr_open(ngtcp2_idtr *idtr, uint64_t stream_id) { 81 | ngtcp2_idtr_gap *g, **pg; 82 | int rv; 83 | uint64_t q; 84 | 85 | assert((idtr->server && (stream_id % 2)) || 86 | (!idtr->server && (stream_id % 2)) == 0); 87 | 88 | q = id_from_stream_id(stream_id); 89 | 90 | for (pg = &idtr->gap; *pg; pg = &(*pg)->next) { 91 | if (q < (*pg)->range.begin) { 92 | return NGTCP2_ERR_STREAM_IN_USE; 93 | } 94 | if ((*pg)->range.end <= q) { 95 | continue; 96 | } 97 | if (q == (*pg)->range.begin) { 98 | if (ngtcp2_range_len(&(*pg)->range) == 1) { 99 | g = *pg; 100 | *pg = (*pg)->next; 101 | ngtcp2_idtr_gap_del(g, idtr->mem); 102 | return 0; 103 | } 104 | ++(*pg)->range.begin; 105 | return 0; 106 | } 107 | 108 | rv = ngtcp2_idtr_gap_new(&g, (*pg)->range.begin, q, idtr->mem); 109 | if (rv != 0) { 110 | return rv; 111 | } 112 | 113 | (*pg)->range.begin = q + 1; 114 | 115 | g->next = *pg; 116 | *pg = g; 117 | 118 | return 0; 119 | } 120 | 121 | return NGTCP2_ERR_STREAM_IN_USE; 122 | } 123 | 124 | int ngtcp2_idtr_is_open(ngtcp2_idtr *idtr, uint64_t stream_id) { 125 | ngtcp2_idtr_gap **pg; 126 | uint64_t q; 127 | 128 | assert((idtr->server && (stream_id % 2)) || 129 | (!idtr->server && (stream_id % 2)) == 0); 130 | 131 | q = id_from_stream_id(stream_id); 132 | 133 | for (pg = &idtr->gap; *pg; pg = &(*pg)->next) { 134 | if (q < (*pg)->range.begin) { 135 | return NGTCP2_ERR_STREAM_IN_USE; 136 | } 137 | if ((*pg)->range.end <= q) { 138 | continue; 139 | } 140 | break; 141 | } 142 | return 0; 143 | } 144 | 145 | uint64_t ngtcp2_idtr_first_gap(ngtcp2_idtr *idtr) { 146 | if (idtr->gap) { 147 | return idtr->gap->range.begin; 148 | } 149 | return UINT64_MAX; 150 | } 151 | -------------------------------------------------------------------------------- /lib/ngtcp2_idtr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_IDTR_H 26 | #define NGTCP2_IDTR_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include "ngtcp2_mem.h" 35 | #include "ngtcp2_range.h" 36 | 37 | struct ngtcp2_idtr_gap; 38 | typedef struct ngtcp2_idtr_gap ngtcp2_idtr_gap; 39 | 40 | /* 41 | * ngtcp2_idtr_gap represents the gap, which is the range of ID that 42 | * is not used yet. 43 | */ 44 | struct ngtcp2_idtr_gap { 45 | /* next points to the next gap. This singly linked list is ordered 46 | by range.begin in the increasing order, and they never 47 | overlap. */ 48 | ngtcp2_idtr_gap *next; 49 | /* range is the range of this gap. */ 50 | ngtcp2_range range; 51 | }; 52 | 53 | /* 54 | * ngtcp2_idtr_gap_new allocates new ngtcp2_idtr_gap object, and 55 | * assigns its pointer to |*pg|. The caller should call 56 | * ngtcp2_idtr_gap_del to delete it when it is no longer used. The 57 | * range of the gap is [begin, end). |mem| is custom memory allocator 58 | * to allocate memory. 59 | * 60 | * This function returns 0 if it succeeds, or one of the following 61 | * negative error codes: 62 | * 63 | * NGTCP2_ERR_NOMEM 64 | * Out of memory. 65 | */ 66 | int ngtcp2_idtr_gap_new(ngtcp2_idtr_gap **pg, uint64_t begin, uint64_t end, 67 | ngtcp2_mem *mem); 68 | 69 | /* 70 | * ngtcp2_idtr_gap_del deallocates |g|. It deallocates the memory 71 | * pointed by |g| it self. |mem| is custom memory allocator to 72 | * deallocate memory. 73 | */ 74 | void ngtcp2_idtr_gap_del(ngtcp2_idtr_gap *g, ngtcp2_mem *mem); 75 | 76 | /* 77 | * ngtcp2_idtr tracks the usage of stream ID. 78 | */ 79 | typedef struct { 80 | /* gap maintains the range of ID which is not used yet. Initially, 81 | its range is [0, UINT64_MAX). */ 82 | ngtcp2_idtr_gap *gap; 83 | /* server is nonzero if this object records server initiated stream 84 | ID. */ 85 | int server; 86 | /* mem is custom memory allocator */ 87 | ngtcp2_mem *mem; 88 | } ngtcp2_idtr; 89 | 90 | /* 91 | * ngtcp2_idtr_init initializes |idtr|. |chunk| is the size of buffer 92 | * per chunk. 93 | * 94 | * If this object records server initiated ID (even number), set 95 | * |server| to nonzero. 96 | * 97 | * This function returns 0 if it succeeds, or one of the following 98 | * negative error codes: 99 | * 100 | * NGTCP2_ERR_NOMEM 101 | * Out of memory. 102 | */ 103 | int ngtcp2_idtr_init(ngtcp2_idtr *idtr, int server, ngtcp2_mem *mem); 104 | 105 | /* 106 | * ngtcp2_idtr_free frees resources allocated for |idtr|. 107 | */ 108 | void ngtcp2_idtr_free(ngtcp2_idtr *idtr); 109 | 110 | /* 111 | * ngtcp2_idtr_open claims that |stream_id| is in used. 112 | * 113 | * It returns 0 if it succeeds, or one of the following negative error 114 | * codes: 115 | * 116 | * NGTCP2_ERR_STREAM_IN_USE 117 | * ID has already been used. 118 | */ 119 | int ngtcp2_idtr_open(ngtcp2_idtr *idtr, uint64_t stream_id); 120 | 121 | /* 122 | * ngtcp2_idtr_open tells whether ID |stream_id| is in used or not. 123 | * 124 | * It returns 0 if it succeeds, or one of the following negative error 125 | * codes: 126 | * 127 | * NGTCP2_ERR_STREAM_IN_USE 128 | * ID has already been used. 129 | */ 130 | int ngtcp2_idtr_is_open(ngtcp2_idtr *idtr, uint64_t stream_id); 131 | 132 | /* 133 | * ngtcp2_idtr_first_gap returns the first id of first gap. If there 134 | * is no gap, it returns UINT64_MAX. The returned id is an id space 135 | * used in this object internally, and not stream ID. 136 | */ 137 | uint64_t ngtcp2_idtr_first_gap(ngtcp2_idtr *idtr); 138 | 139 | #endif /* NGTCP2_IDTR_H */ 140 | -------------------------------------------------------------------------------- /lib/ngtcp2_log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_LOG_H 26 | #define NGTCP2_LOG_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | typedef enum { 35 | NGTCP2_LOG_EVENT_NONE, 36 | /* packet event */ 37 | NGTCP2_LOG_EVENT_PKT, 38 | /* frame event */ 39 | NGTCP2_LOG_EVENT_FRM, 40 | /* recovery event */ 41 | NGTCP2_LOG_EVENT_RCV, 42 | /* crypto event */ 43 | NGTCP2_LOG_EVENT_CRY, 44 | /* connection (catch-all) event */ 45 | NGTCP2_LOG_EVENT_CON, 46 | } ngtcp2_log_event; 47 | 48 | struct ngtcp2_log { 49 | /* log_printf is a sink to write log. NULL means no logging 50 | output. */ 51 | ngtcp2_printf log_printf; 52 | /* ts is the time point used to write time delta in the log. */ 53 | ngtcp2_tstamp ts; 54 | /* last_ts is the most recent time point that this object is 55 | told. */ 56 | ngtcp2_tstamp last_ts; 57 | /* user_data is user-defined opaque data which is passed to 58 | log_pritnf. */ 59 | void *user_data; 60 | /* scid is SCID encoded as NULL-terminated hex string. */ 61 | uint8_t scid[NGTCP2_MAX_CIDLEN * 2 + 1]; 62 | }; 63 | 64 | typedef struct ngtcp2_log ngtcp2_log; 65 | 66 | void ngtcp2_log_init(ngtcp2_log *log, const ngtcp2_cid *scid, 67 | ngtcp2_printf log_printf, ngtcp2_tstamp ts, 68 | void *user_data); 69 | 70 | void ngtcp2_log_rx_fr(ngtcp2_log *log, const ngtcp2_pkt_hd *hd, 71 | const ngtcp2_frame *fr); 72 | void ngtcp2_log_tx_fr(ngtcp2_log *log, const ngtcp2_pkt_hd *hd, 73 | const ngtcp2_frame *fr); 74 | 75 | void ngtcp2_log_info(ngtcp2_log *log, ngtcp2_log_event ev, const char *fmt, 76 | ...); 77 | 78 | void ngtcp2_log_rx_vn(ngtcp2_log *log, const ngtcp2_pkt_hd *hd, 79 | const uint32_t *sv, size_t nsv); 80 | 81 | void ngtcp2_log_rx_sr(ngtcp2_log *log, const ngtcp2_pkt_hd *hd, 82 | const ngtcp2_pkt_stateless_reset *sr); 83 | 84 | void ngtcp2_log_remote_tp(ngtcp2_log *log, uint8_t exttype, 85 | const ngtcp2_transport_params *params); 86 | 87 | void ngtcp2_log_pkt_lost(ngtcp2_log *log, const ngtcp2_pkt_hd *hd, 88 | ngtcp2_tstamp sent_ts); 89 | 90 | void ngtcp2_log_rx_pkt_hd(ngtcp2_log *log, const ngtcp2_pkt_hd *hd); 91 | 92 | #endif /* NGTCP2_LOG_H */ 93 | -------------------------------------------------------------------------------- /lib/ngtcp2_macro.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_MACRO_H 26 | #define NGTCP2_MACRO_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include 35 | 36 | #define ngtcp2_min(A, B) ((A) < (B) ? (A) : (B)) 37 | #define ngtcp2_max(A, B) ((A) > (B) ? (A) : (B)) 38 | 39 | #define ngtcp2_struct_of(ptr, type, member) \ 40 | ((type *)(void *)((char *)(ptr)-offsetof(type, member))) 41 | 42 | /* ngtcp2_list_insert inserts |T| before |*PD|. The contract is that 43 | this is singly linked list, and the next element is pointed by next 44 | field of the previous element. |PD| must be a pointer to the 45 | pointer to the next field of the previous element of |*PD|: if C is 46 | the previous element of |PD|, PD = &C->next. */ 47 | #define ngtcp2_list_insert(T, PD) \ 48 | do { \ 49 | (T)->next = *(PD); \ 50 | *(PD) = (T); \ 51 | } while (0) 52 | 53 | /* ngtcp2_list_remove removes |*PT| from singly linked list. The 54 | contract is the same as ngtcp2_list_insert. |PT| must be a pointer 55 | to the pointer to the next field of the previous element of |*PT|. 56 | Please be aware that |*PT|->next is not modified by this macro. */ 57 | #define ngtcp2_list_remove(PT) \ 58 | do { \ 59 | *(PT) = (*(PT))->next; \ 60 | } while (0) 61 | 62 | #endif /* NGTCP2_MACRO_H */ 63 | -------------------------------------------------------------------------------- /lib/ngtcp2_map.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2012 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGTCP2_MAP_H 27 | #define NGTCP2_MAP_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* HAVE_CONFIG_H */ 32 | 33 | #include 34 | 35 | #include "ngtcp2_mem.h" 36 | 37 | /* Implementation of unordered map */ 38 | 39 | typedef uint64_t key_type; 40 | 41 | typedef struct ngtcp2_map_entry { 42 | struct ngtcp2_map_entry *next; 43 | key_type key; 44 | } ngtcp2_map_entry; 45 | 46 | typedef struct { 47 | ngtcp2_map_entry **table; 48 | ngtcp2_mem *mem; 49 | size_t size; 50 | uint32_t tablelen; 51 | } ngtcp2_map; 52 | 53 | /* 54 | * Initializes the map |map|. 55 | * 56 | * This function returns 0 if it succeeds, or one of the following 57 | * negative error codes: 58 | * 59 | * NGTCP2_ERR_NOMEM 60 | * Out of memory 61 | */ 62 | int ngtcp2_map_init(ngtcp2_map *map, ngtcp2_mem *mem); 63 | 64 | /* 65 | * Deallocates any resources allocated for |map|. The stored entries 66 | * are not freed by this function. Use ngtcp2_map_each_free() to free 67 | * each entries. 68 | */ 69 | void ngtcp2_map_free(ngtcp2_map *map); 70 | 71 | /* 72 | * Deallocates each entries using |func| function and any resources 73 | * allocated for |map|. The |func| function is responsible for freeing 74 | * given the |entry| object. The |ptr| will be passed to the |func| as 75 | * send argument. The return value of the |func| will be ignored. 76 | */ 77 | void ngtcp2_map_each_free(ngtcp2_map *map, 78 | int (*func)(ngtcp2_map_entry *entry, void *ptr), 79 | void *ptr); 80 | 81 | /* 82 | * Initializes the |entry| with the |key|. All entries to be inserted 83 | * to the map must be initialized with this function. 84 | */ 85 | void ngtcp2_map_entry_init(ngtcp2_map_entry *entry, key_type key); 86 | 87 | /* 88 | * Inserts the new |entry| with the key |entry->key| to the map |map|. 89 | * 90 | * This function returns 0 if it succeeds, or one of the following 91 | * negative error codes: 92 | * 93 | * NGTCP2_ERR_INVALID_ARGUMENT 94 | * The item associated by |key| already exists. 95 | * NGTCP2_ERR_NOMEM 96 | * Out of memory 97 | */ 98 | int ngtcp2_map_insert(ngtcp2_map *map, ngtcp2_map_entry *entry); 99 | 100 | /* 101 | * Returns the entry associated by the key |key|. If there is no such 102 | * entry, this function returns NULL. 103 | */ 104 | ngtcp2_map_entry *ngtcp2_map_find(ngtcp2_map *map, key_type key); 105 | 106 | /* 107 | * Removes the entry associated by the key |key| from the |map|. The 108 | * removed entry is not freed by this function. 109 | * 110 | * This function returns 0 if it succeeds, or one of the following 111 | * negative error codes: 112 | * 113 | * NGTCP2_ERR_INVALID_ARGUMENT 114 | * The entry associated by |key| does not exist. 115 | */ 116 | int ngtcp2_map_remove(ngtcp2_map *map, key_type key); 117 | 118 | /* 119 | * Returns the number of items stored in the map |map|. 120 | */ 121 | size_t ngtcp2_map_size(ngtcp2_map *map); 122 | 123 | /* 124 | * Applies the function |func| to each entry in the |map| with the 125 | * optional user supplied pointer |ptr|. 126 | * 127 | * If the |func| returns 0, this function calls the |func| with the 128 | * next entry. If the |func| returns nonzero, it will not call the 129 | * |func| for further entries and return the return value of the 130 | * |func| immediately. Thus, this function returns 0 if all the 131 | * invocations of the |func| return 0, or nonzero value which the last 132 | * invocation of |func| returns. 133 | * 134 | * Don't use this function to free each entry. Use 135 | * ngtcp2_map_each_free() instead. 136 | */ 137 | int ngtcp2_map_each(ngtcp2_map *map, 138 | int (*func)(ngtcp2_map_entry *entry, void *ptr), void *ptr); 139 | 140 | #endif /* NGTCP2_MAP_H */ 141 | -------------------------------------------------------------------------------- /lib/ngtcp2_mem.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2014 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "ngtcp2_mem.h" 27 | 28 | static void *default_malloc(size_t size, void *mem_user_data) { 29 | (void)mem_user_data; 30 | 31 | return malloc(size); 32 | } 33 | 34 | static void default_free(void *ptr, void *mem_user_data) { 35 | (void)mem_user_data; 36 | 37 | free(ptr); 38 | } 39 | 40 | static void *default_calloc(size_t nmemb, size_t size, void *mem_user_data) { 41 | (void)mem_user_data; 42 | 43 | return calloc(nmemb, size); 44 | } 45 | 46 | static void *default_realloc(void *ptr, size_t size, void *mem_user_data) { 47 | (void)mem_user_data; 48 | 49 | return realloc(ptr, size); 50 | } 51 | 52 | static ngtcp2_mem mem_default = {NULL, default_malloc, default_free, 53 | default_calloc, default_realloc}; 54 | 55 | ngtcp2_mem *ngtcp2_mem_default(void) { return &mem_default; } 56 | 57 | void *ngtcp2_mem_malloc(ngtcp2_mem *mem, size_t size) { 58 | return mem->malloc(size, mem->mem_user_data); 59 | } 60 | 61 | void ngtcp2_mem_free(ngtcp2_mem *mem, void *ptr) { 62 | mem->free(ptr, mem->mem_user_data); 63 | } 64 | 65 | void ngtcp2_mem_free2(ngtcp2_free free_func, void *ptr, void *mem_user_data) { 66 | free_func(ptr, mem_user_data); 67 | } 68 | 69 | void *ngtcp2_mem_calloc(ngtcp2_mem *mem, size_t nmemb, size_t size) { 70 | return mem->calloc(nmemb, size, mem->mem_user_data); 71 | } 72 | 73 | void *ngtcp2_mem_realloc(ngtcp2_mem *mem, void *ptr, size_t size) { 74 | return mem->realloc(ptr, size, mem->mem_user_data); 75 | } 76 | -------------------------------------------------------------------------------- /lib/ngtcp2_mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2014 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGTCP2_MEM_H 27 | #define NGTCP2_MEM_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* HAVE_CONFIG_H */ 32 | 33 | #include 34 | 35 | /* The default, system standard memory allocator */ 36 | ngtcp2_mem *ngtcp2_mem_default(void); 37 | 38 | /* Convenient wrapper functions to call allocator function in 39 | |mem|. */ 40 | void *ngtcp2_mem_malloc(ngtcp2_mem *mem, size_t size); 41 | void ngtcp2_mem_free(ngtcp2_mem *mem, void *ptr); 42 | void ngtcp2_mem_free2(ngtcp2_free free_func, void *ptr, void *mem_user_data); 43 | void *ngtcp2_mem_calloc(ngtcp2_mem *mem, size_t nmemb, size_t size); 44 | void *ngtcp2_mem_realloc(ngtcp2_mem *mem, void *ptr, size_t size); 45 | 46 | #endif /* NGTCP2_MEM_H */ 47 | -------------------------------------------------------------------------------- /lib/ngtcp2_ppe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_PPE_H 26 | #define NGTCP2_PPE_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include "ngtcp2_buf.h" 35 | #include "ngtcp2_crypto.h" 36 | 37 | /* 38 | * ngtcp2_ppe is the Protected Packet Encoder. 39 | */ 40 | typedef struct { 41 | ngtcp2_buf buf; 42 | ngtcp2_crypto_ctx *ctx; 43 | /* hdlen is the number of bytes for packet header written in buf. */ 44 | size_t hdlen; 45 | /* len_offset is the offset to Length field. */ 46 | size_t len_offset; 47 | /* pkt_num_offset is the offset to packet number field. */ 48 | size_t pkt_num_offset; 49 | /* pkt_numlen is the number of bytes used to encode a packet 50 | number */ 51 | size_t pkt_numlen; 52 | /* sample_offset is the offset to sample for packet number 53 | encryption. */ 54 | size_t sample_offset; 55 | /* pkt_num is the packet number written in buf. */ 56 | uint64_t pkt_num; 57 | /* nonce is the buffer to store nonce. It should be equal or longer 58 | than then length of IV. */ 59 | uint8_t nonce[32]; 60 | } ngtcp2_ppe; 61 | 62 | /* 63 | * ngtcp2_ppe_init initializes |ppe| with the given buffer. 64 | */ 65 | void ngtcp2_ppe_init(ngtcp2_ppe *ppe, uint8_t *out, size_t outlen, 66 | ngtcp2_crypto_ctx *cctx); 67 | 68 | /* 69 | * ngtcp2_ppe_encode_hd encodes |hd|. 70 | * 71 | * This function returns 0 if it succeeds, or one of the following 72 | * negative error codes: 73 | * 74 | * NGTCP2_ERR_NOBUF 75 | * The buffer is too small. 76 | */ 77 | int ngtcp2_ppe_encode_hd(ngtcp2_ppe *ppe, const ngtcp2_pkt_hd *hd); 78 | 79 | /* 80 | * ngtcp2_ppe_encode_frame encodes |fr|. 81 | * 82 | * This function returns 0 if it succeeds, or one of the following 83 | * negative error codes: 84 | * 85 | * NGTCP2_ERR_NOBUF 86 | * The buffer is too small. 87 | */ 88 | int ngtcp2_ppe_encode_frame(ngtcp2_ppe *ppe, ngtcp2_frame *fr); 89 | 90 | /* 91 | * ngtcp2_ppe_final encrypts QUIC packet payload. If |**ppkt| is not 92 | * NULL, the pointer to the packet is assigned to it. 93 | * 94 | * This function returns the length of QUIC packet, including header, 95 | * and payload if it succeeds, or one of the following negative error 96 | * codes: 97 | * 98 | * NGTCP2_ERR_CALLBACK_FAILURE 99 | * User-defined callback function failed. 100 | */ 101 | ssize_t ngtcp2_ppe_final(ngtcp2_ppe *ppe, const uint8_t **ppkt); 102 | 103 | /* 104 | * ngtcp2_ppe_left returns the number of bytes left to write 105 | * additional frames. This does not count AEAD overhead. 106 | */ 107 | size_t ngtcp2_ppe_left(ngtcp2_ppe *ppe); 108 | 109 | /** 110 | * @function 111 | * 112 | * `ngtcp2_ppe_padding` encodes PADDING frames to the end of the 113 | * buffer. This function returns the number of bytes padded. 114 | */ 115 | size_t ngtcp2_ppe_padding(ngtcp2_ppe *ppe); 116 | 117 | #endif /* NGTCP2_PPE_H */ 118 | -------------------------------------------------------------------------------- /lib/ngtcp2_pq.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2012 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include "ngtcp2_pq.h" 27 | 28 | #include 29 | 30 | #include "ngtcp2_macro.h" 31 | 32 | void ngtcp2_pq_init(ngtcp2_pq *pq, ngtcp2_less less, ngtcp2_mem *mem) { 33 | pq->mem = mem; 34 | pq->capacity = 0; 35 | pq->q = NULL; 36 | pq->length = 0; 37 | pq->less = less; 38 | } 39 | 40 | void ngtcp2_pq_free(ngtcp2_pq *pq) { 41 | ngtcp2_mem_free(pq->mem, pq->q); 42 | pq->q = NULL; 43 | } 44 | 45 | static void swap(ngtcp2_pq *pq, size_t i, size_t j) { 46 | ngtcp2_pq_entry *a = pq->q[i]; 47 | ngtcp2_pq_entry *b = pq->q[j]; 48 | 49 | pq->q[i] = b; 50 | b->index = i; 51 | pq->q[j] = a; 52 | a->index = j; 53 | } 54 | 55 | static void bubble_up(ngtcp2_pq *pq, size_t index) { 56 | size_t parent; 57 | while (index != 0) { 58 | parent = (index - 1) / 2; 59 | if (!pq->less(pq->q[index], pq->q[parent])) { 60 | return; 61 | } 62 | swap(pq, parent, index); 63 | index = parent; 64 | } 65 | } 66 | 67 | int ngtcp2_pq_push(ngtcp2_pq *pq, ngtcp2_pq_entry *item) { 68 | if (pq->capacity <= pq->length) { 69 | void *nq; 70 | size_t ncapacity; 71 | 72 | ncapacity = ngtcp2_max(4, (pq->capacity * 2)); 73 | 74 | nq = ngtcp2_mem_realloc(pq->mem, pq->q, 75 | ncapacity * sizeof(ngtcp2_pq_entry *)); 76 | if (nq == NULL) { 77 | return NGTCP2_ERR_NOMEM; 78 | } 79 | pq->capacity = ncapacity; 80 | pq->q = nq; 81 | } 82 | pq->q[pq->length] = item; 83 | item->index = pq->length; 84 | ++pq->length; 85 | bubble_up(pq, pq->length - 1); 86 | return 0; 87 | } 88 | 89 | ngtcp2_pq_entry *ngtcp2_pq_top(ngtcp2_pq *pq) { 90 | if (pq->length == 0) { 91 | return NULL; 92 | } else { 93 | return pq->q[0]; 94 | } 95 | } 96 | 97 | static void bubble_down(ngtcp2_pq *pq, size_t index) { 98 | size_t i, j, minindex; 99 | for (;;) { 100 | j = index * 2 + 1; 101 | minindex = index; 102 | for (i = 0; i < 2; ++i, ++j) { 103 | if (j >= pq->length) { 104 | break; 105 | } 106 | if (pq->less(pq->q[j], pq->q[minindex])) { 107 | minindex = j; 108 | } 109 | } 110 | if (minindex == index) { 111 | return; 112 | } 113 | swap(pq, index, minindex); 114 | index = minindex; 115 | } 116 | } 117 | 118 | void ngtcp2_pq_pop(ngtcp2_pq *pq) { 119 | if (pq->length > 0) { 120 | pq->q[0] = pq->q[pq->length - 1]; 121 | pq->q[0]->index = 0; 122 | --pq->length; 123 | bubble_down(pq, 0); 124 | } 125 | } 126 | 127 | void ngtcp2_pq_remove(ngtcp2_pq *pq, ngtcp2_pq_entry *item) { 128 | assert(pq->q[item->index] == item); 129 | 130 | if (item->index == 0) { 131 | ngtcp2_pq_pop(pq); 132 | return; 133 | } 134 | 135 | if (item->index == pq->length - 1) { 136 | --pq->length; 137 | return; 138 | } 139 | 140 | pq->q[item->index] = pq->q[pq->length - 1]; 141 | pq->q[item->index]->index = item->index; 142 | --pq->length; 143 | 144 | if (pq->less(item, pq->q[item->index])) { 145 | bubble_down(pq, item->index); 146 | } else { 147 | bubble_up(pq, item->index); 148 | } 149 | } 150 | 151 | int ngtcp2_pq_empty(ngtcp2_pq *pq) { return pq->length == 0; } 152 | 153 | size_t ngtcp2_pq_size(ngtcp2_pq *pq) { return pq->length; } 154 | 155 | void ngtcp2_pq_update(ngtcp2_pq *pq, ngtcp2_pq_item_cb fun, void *arg) { 156 | size_t i; 157 | int rv = 0; 158 | if (pq->length == 0) { 159 | return; 160 | } 161 | for (i = 0; i < pq->length; ++i) { 162 | rv |= (*fun)(pq->q[i], arg); 163 | } 164 | if (rv) { 165 | for (i = pq->length; i > 0; --i) { 166 | bubble_down(pq, i - 1); 167 | } 168 | } 169 | } 170 | 171 | int ngtcp2_pq_each(ngtcp2_pq *pq, ngtcp2_pq_item_cb fun, void *arg) { 172 | size_t i; 173 | 174 | if (pq->length == 0) { 175 | return 0; 176 | } 177 | for (i = 0; i < pq->length; ++i) { 178 | if ((*fun)(pq->q[i], arg)) { 179 | return 1; 180 | } 181 | } 182 | return 0; 183 | } 184 | -------------------------------------------------------------------------------- /lib/ngtcp2_pq.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2012 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGTCP2_PQ_H 27 | #define NGTCP2_PQ_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | # include 31 | #endif /* HAVE_CONFIG_H */ 32 | 33 | #include 34 | 35 | #include "ngtcp2_mem.h" 36 | 37 | /* Implementation of priority queue */ 38 | 39 | /* "less" function, return nonzero if |lhs| is less than |rhs|. */ 40 | typedef int (*ngtcp2_less)(const void *lhs, const void *rhs); 41 | 42 | typedef struct { 43 | size_t index; 44 | } ngtcp2_pq_entry; 45 | 46 | typedef struct { 47 | /* The pointer to the pointer to the item stored */ 48 | ngtcp2_pq_entry **q; 49 | /* Memory allocator */ 50 | ngtcp2_mem *mem; 51 | /* The number of items stored */ 52 | size_t length; 53 | /* The maximum number of items this pq can store. This is 54 | automatically extended when length is reached to this value. */ 55 | size_t capacity; 56 | /* The less function between items */ 57 | ngtcp2_less less; 58 | } ngtcp2_pq; 59 | 60 | /* 61 | * Initializes priority queue |pq| with compare function |cmp|. 62 | */ 63 | void ngtcp2_pq_init(ngtcp2_pq *pq, ngtcp2_less less, ngtcp2_mem *mem); 64 | 65 | /* 66 | * Deallocates any resources allocated for |pq|. The stored items are 67 | * not freed by this function. 68 | */ 69 | void ngtcp2_pq_free(ngtcp2_pq *pq); 70 | 71 | /* 72 | * Adds |item| to the priority queue |pq|. 73 | * 74 | * This function returns 0 if it succeeds, or one of the following 75 | * negative error codes: 76 | * 77 | * NGTCP2_ERR_NOMEM 78 | * Out of memory. 79 | */ 80 | int ngtcp2_pq_push(ngtcp2_pq *pq, ngtcp2_pq_entry *item); 81 | 82 | /* 83 | * Returns item at the top of the queue |pq|. If the queue is empty, 84 | * this function returns NULL. 85 | */ 86 | ngtcp2_pq_entry *ngtcp2_pq_top(ngtcp2_pq *pq); 87 | 88 | /* 89 | * Pops item at the top of the queue |pq|. The popped item is not 90 | * freed by this function. 91 | */ 92 | void ngtcp2_pq_pop(ngtcp2_pq *pq); 93 | 94 | /* 95 | * Returns nonzero if the queue |pq| is empty. 96 | */ 97 | int ngtcp2_pq_empty(ngtcp2_pq *pq); 98 | 99 | /* 100 | * Returns the number of items in the queue |pq|. 101 | */ 102 | size_t ngtcp2_pq_size(ngtcp2_pq *pq); 103 | 104 | typedef int (*ngtcp2_pq_item_cb)(ngtcp2_pq_entry *item, void *arg); 105 | 106 | /* 107 | * Updates each item in |pq| using function |fun| and re-construct 108 | * priority queue. The |fun| must return non-zero if it modifies the 109 | * item in a way that it affects ordering in the priority queue. The 110 | * |arg| is passed to the 2nd parameter of |fun|. 111 | */ 112 | void ngtcp2_pq_update(ngtcp2_pq *pq, ngtcp2_pq_item_cb fun, void *arg); 113 | 114 | /* 115 | * Applys |fun| to each item in |pq|. The |arg| is passed as arg 116 | * parameter to callback function. This function must not change the 117 | * ordering key. If the return value from callback is nonzero, this 118 | * function returns 1 immediately without iterating remaining items. 119 | * Otherwise this function returns 0. 120 | */ 121 | int ngtcp2_pq_each(ngtcp2_pq *pq, ngtcp2_pq_item_cb fun, void *arg); 122 | 123 | /* 124 | * Removes |item| from priority queue. 125 | */ 126 | void ngtcp2_pq_remove(ngtcp2_pq *pq, ngtcp2_pq_entry *item); 127 | 128 | #endif /* NGTCP2_PQ_H */ 129 | -------------------------------------------------------------------------------- /lib/ngtcp2_range.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_range.h" 26 | #include "ngtcp2_macro.h" 27 | 28 | void ngtcp2_range_init(ngtcp2_range *r, uint64_t begin, uint64_t end) { 29 | r->begin = begin; 30 | r->end = end; 31 | } 32 | 33 | ngtcp2_range ngtcp2_range_intersect(const ngtcp2_range *a, 34 | const ngtcp2_range *b) { 35 | ngtcp2_range r = {0, 0}; 36 | uint64_t begin = ngtcp2_max(a->begin, b->begin); 37 | uint64_t end = ngtcp2_min(a->end, b->end); 38 | if (begin < end) { 39 | ngtcp2_range_init(&r, begin, end); 40 | } 41 | return r; 42 | } 43 | 44 | uint64_t ngtcp2_range_len(const ngtcp2_range *r) { return r->end - r->begin; } 45 | 46 | int ngtcp2_range_eq(const ngtcp2_range *a, const ngtcp2_range *b) { 47 | return a->begin == b->begin && a->end == b->end; 48 | } 49 | 50 | void ngtcp2_range_cut(ngtcp2_range *left, ngtcp2_range *right, 51 | const ngtcp2_range *a, const ngtcp2_range *b) { 52 | /* Assume that b is included in a */ 53 | left->begin = a->begin; 54 | left->end = b->begin; 55 | right->begin = b->end; 56 | right->end = a->end; 57 | } 58 | 59 | int ngtcp2_range_not_after(const ngtcp2_range *a, const ngtcp2_range *b) { 60 | return a->end <= b->end; 61 | } 62 | -------------------------------------------------------------------------------- /lib/ngtcp2_range.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_RANGE_H 26 | #define NGTCP2_RANGE_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | /* 35 | * ngtcp2_range represents half-closed range [begin, end). 36 | */ 37 | typedef struct { 38 | uint64_t begin; 39 | uint64_t end; 40 | } ngtcp2_range; 41 | 42 | /* 43 | * ngtcp2_range_init initializes |r| with the range [|begin|, |end|). 44 | */ 45 | void ngtcp2_range_init(ngtcp2_range *r, uint64_t begin, uint64_t end); 46 | 47 | /* 48 | * ngtcp2_range_intersect returns the intersection of |a| and |b|. If 49 | * they do not overlap, it returns empty range. 50 | */ 51 | ngtcp2_range ngtcp2_range_intersect(const ngtcp2_range *a, 52 | const ngtcp2_range *b); 53 | 54 | /* 55 | * ngtcp2_range_len returns the length of |r|. 56 | */ 57 | uint64_t ngtcp2_range_len(const ngtcp2_range *r); 58 | 59 | /* 60 | * ngtcp2_range_eq returns nonzero if |a| equals |b|, such that 61 | * a->begin == b->begin, and a->end == b->end hold. 62 | */ 63 | int ngtcp2_range_eq(const ngtcp2_range *a, const ngtcp2_range *b); 64 | 65 | /* 66 | * ngtcp2_range_cut returns the left and right range after removing 67 | * |b| from |a|. This function assumes that |a| completely includes 68 | * |b|. In other words, a->begin <= b->begin and b->end <= a->end 69 | * hold. 70 | */ 71 | void ngtcp2_range_cut(ngtcp2_range *left, ngtcp2_range *right, 72 | const ngtcp2_range *a, const ngtcp2_range *b); 73 | 74 | /* 75 | * ngtcp2_range_not_after returns nonzero if the right edge of |a| 76 | * does not go beyond of the right edge of |b|. 77 | */ 78 | int ngtcp2_range_not_after(const ngtcp2_range *a, const ngtcp2_range *b); 79 | 80 | #endif /* NGTCP2_RANGE_H */ 81 | -------------------------------------------------------------------------------- /lib/ngtcp2_ringbuf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_ringbuf.h" 26 | 27 | #include 28 | 29 | #include "ngtcp2_macro.h" 30 | 31 | int ngtcp2_ringbuf_init(ngtcp2_ringbuf *rb, size_t nmemb, size_t size, 32 | ngtcp2_mem *mem) { 33 | assert(1 == __builtin_popcount((unsigned int)nmemb)); 34 | 35 | rb->buf = ngtcp2_mem_malloc(mem, nmemb * size); 36 | if (rb->buf == NULL) { 37 | return NGTCP2_ERR_NOMEM; 38 | } 39 | 40 | rb->mem = mem; 41 | rb->nmemb = nmemb; 42 | rb->size = size; 43 | rb->first = 0; 44 | rb->len = 0; 45 | 46 | return 0; 47 | } 48 | 49 | void ngtcp2_ringbuf_free(ngtcp2_ringbuf *rb) { 50 | if (rb == NULL) { 51 | return; 52 | } 53 | 54 | ngtcp2_mem_free(rb->mem, rb->buf); 55 | } 56 | 57 | void *ngtcp2_ringbuf_push_front(ngtcp2_ringbuf *rb) { 58 | rb->first = (rb->first - 1) & (rb->nmemb - 1); 59 | rb->len = ngtcp2_min(rb->nmemb, rb->len + 1); 60 | 61 | return (void *)&rb->buf[rb->first * rb->size]; 62 | } 63 | 64 | void *ngtcp2_ringbuf_push_back(ngtcp2_ringbuf *rb) { 65 | size_t offset = (rb->first + rb->len) & (rb->nmemb - 1); 66 | 67 | if (rb->len == rb->nmemb) { 68 | rb->first = (rb->first + 1) & (rb->nmemb - 1); 69 | } else { 70 | ++rb->len; 71 | } 72 | 73 | return (void *)&rb->buf[offset * rb->size]; 74 | } 75 | 76 | void ngtcp2_ringbuf_pop_front(ngtcp2_ringbuf *rb) { 77 | rb->first = (rb->first + 1) & (rb->nmemb - 1); 78 | --rb->len; 79 | } 80 | 81 | void ngtcp2_ringbuf_resize(ngtcp2_ringbuf *rb, size_t len) { 82 | assert(len <= rb->nmemb); 83 | rb->len = len; 84 | } 85 | 86 | void *ngtcp2_ringbuf_get(ngtcp2_ringbuf *rb, size_t offset) { 87 | assert(offset < rb->len); 88 | offset = (rb->first + offset) & (rb->nmemb - 1); 89 | return &rb->buf[offset * rb->size]; 90 | } 91 | 92 | size_t ngtcp2_ringbuf_len(ngtcp2_ringbuf *rb) { return rb->len; } 93 | 94 | int ngtcp2_ringbuf_full(ngtcp2_ringbuf *rb) { return rb->len == rb->nmemb; } 95 | -------------------------------------------------------------------------------- /lib/ngtcp2_ringbuf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_RINGBUF_H 26 | #define NGTCP2_RINGBUF_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include "ngtcp2_mem.h" 35 | 36 | typedef struct { 37 | /* buf points to the underlying buffer. */ 38 | uint8_t *buf; 39 | ngtcp2_mem *mem; 40 | /* nmemb is the number of elements that can be stored in this ring 41 | buffer. */ 42 | size_t nmemb; 43 | /* size is the size of each element. */ 44 | size_t size; 45 | /* first is the offset to the first element. */ 46 | size_t first; 47 | /* len is the number of elements actually stored. */ 48 | size_t len; 49 | } ngtcp2_ringbuf; 50 | 51 | /* 52 | * ngtcp2_ringbuf_init initializes |rb|. |nmemb| is the number of 53 | * elements that can be stored in this buffer. |size| is the size of 54 | * each element. |size| must be power of 2. 55 | * 56 | * This function returns 0 if it succeeds, or one of the following 57 | * negative error codes: 58 | * 59 | * NGTCP2_ERR_NOMEM 60 | * Out of memory. 61 | */ 62 | int ngtcp2_ringbuf_init(ngtcp2_ringbuf *rb, size_t nmemb, size_t size, 63 | ngtcp2_mem *mem); 64 | 65 | /* 66 | * ngtcp2_ringbuf_free frees resources allocated for |rb|. This 67 | * function does not free the memory pointed by |rb|. 68 | */ 69 | void ngtcp2_ringbuf_free(ngtcp2_ringbuf *rb); 70 | 71 | /* ngtcp2_ringbuf_push_front moves the offset to the first element in 72 | the buffer backward, and returns the pointer to the element. 73 | Caller can store data to the buffer pointed by the returned 74 | pointer. If this action exceeds the capacity of the ring buffer, 75 | the last element is silently overwritten, and rb->len remains 76 | unchanged. */ 77 | void *ngtcp2_ringbuf_push_front(ngtcp2_ringbuf *rb); 78 | 79 | /* ngtcp2_ringbuf_push_back moves the offset to the last element in 80 | the buffer forward, and returns the pointer to the element. Caller 81 | can store data to the buffer pointed by the returned pointer. If 82 | this action exceeds the capacity of the ring buffer, the first 83 | element is silently overwritten, and rb->len remains unchanged. */ 84 | void *ngtcp2_ringbuf_push_back(ngtcp2_ringbuf *rb); 85 | 86 | /* 87 | * ngtcp2_ringbuf_pop_front removes first element in |rb|. 88 | */ 89 | void ngtcp2_ringbuf_pop_front(ngtcp2_ringbuf *rb); 90 | 91 | /* ngtcp2_ringbuf_resize changes the number of elements stored. This 92 | does not change the capacity of the underlying buffer. */ 93 | void ngtcp2_ringbuf_resize(ngtcp2_ringbuf *rb, size_t len); 94 | 95 | /* ngtcp2_ringbuf_get returns the pointer to the element at 96 | |offset|. */ 97 | void *ngtcp2_ringbuf_get(ngtcp2_ringbuf *rb, size_t offset); 98 | 99 | /* ngtcp2_ringbuf_len returns the number of elements stored. */ 100 | size_t ngtcp2_ringbuf_len(ngtcp2_ringbuf *rb); 101 | 102 | /* ngtcp2_ringbuf_full returns nonzero if |rb| is full. */ 103 | int ngtcp2_ringbuf_full(ngtcp2_ringbuf *rb); 104 | 105 | #endif /* NGTCP2_RINGBUF_H */ 106 | -------------------------------------------------------------------------------- /lib/ngtcp2_str.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_str.h" 26 | 27 | #include 28 | 29 | uint8_t *ngtcp2_cpymem(uint8_t *dest, const uint8_t *src, size_t n) { 30 | memcpy(dest, src, n); 31 | return dest + n; 32 | } 33 | 34 | #define LOWER_XDIGITS "0123456789abcdef" 35 | 36 | uint8_t *ngtcp2_encode_hex(uint8_t *dest, const uint8_t *data, size_t len) { 37 | size_t i; 38 | uint8_t *p = dest; 39 | 40 | for (i = 0; i < len; ++i) { 41 | *p++ = (uint8_t)LOWER_XDIGITS[data[i] >> 4]; 42 | *p++ = (uint8_t)LOWER_XDIGITS[data[i] & 0xf]; 43 | } 44 | 45 | *p = '\0'; 46 | 47 | return dest; 48 | } 49 | -------------------------------------------------------------------------------- /lib/ngtcp2_str.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_STR_H 26 | #define NGTCP2_STR_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | /* ngtcp2_array is a fixed size array. */ 35 | typedef struct { 36 | /* base points to the beginning of the buffer. */ 37 | uint8_t *base; 38 | /* len is the capacity of the buffer. */ 39 | size_t len; 40 | } ngtcp2_array; 41 | 42 | uint8_t *ngtcp2_cpymem(uint8_t *dest, const uint8_t *src, size_t n); 43 | 44 | /* 45 | * ngtcp2_encode_hex encodes |data| of length |len| in hex string. It 46 | * writes additional NULL bytes at the end of the buffer. The buffer 47 | * pointed by |dest| must have at least |len| * 2 + 1 bytes space. 48 | * This function returns |dest|. 49 | */ 50 | uint8_t *ngtcp2_encode_hex(uint8_t *dest, const uint8_t *data, size_t len); 51 | 52 | #endif /* NGTCP2_STR_H */ 53 | -------------------------------------------------------------------------------- /lib/ngtcp2_strm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_strm.h" 26 | 27 | #include 28 | 29 | int ngtcp2_strm_init(ngtcp2_strm *strm, uint64_t stream_id, uint32_t flags, 30 | uint64_t max_rx_offset, uint64_t max_tx_offset, 31 | void *stream_user_data, ngtcp2_mem *mem) { 32 | int rv; 33 | 34 | strm->tx_offset = 0; 35 | strm->last_rx_offset = 0; 36 | strm->nbuffered = 0; 37 | strm->stream_id = stream_id; 38 | strm->flags = flags; 39 | strm->stream_user_data = stream_user_data; 40 | strm->max_rx_offset = strm->unsent_max_rx_offset = max_rx_offset; 41 | strm->max_tx_offset = max_tx_offset; 42 | strm->me.key = stream_id; 43 | strm->me.next = NULL; 44 | strm->mem = mem; 45 | strm->fc_pprev = NULL; 46 | strm->fc_next = NULL; 47 | /* Initializing to 0 is a bit controversial because application 48 | error code 0 is STOPPING. But STOPPING is only sent with 49 | RST_STREAM in response to STOP_SENDING, and it is not used to 50 | indicate the cause of closure. So effectively, 0 means "no 51 | error." */ 52 | strm->app_error_code = 0; 53 | memset(&strm->tx_buf, 0, sizeof(strm->tx_buf)); 54 | 55 | rv = ngtcp2_gaptr_init(&strm->acked_tx_offset, mem); 56 | if (rv != 0) { 57 | goto fail_gaptr_init; 58 | } 59 | 60 | rv = ngtcp2_rob_init(&strm->rob, 8 * 1024, mem); 61 | if (rv != 0) { 62 | goto fail_rob_init; 63 | } 64 | 65 | return 0; 66 | 67 | fail_rob_init: 68 | ngtcp2_gaptr_free(&strm->acked_tx_offset); 69 | fail_gaptr_init: 70 | return rv; 71 | } 72 | 73 | void ngtcp2_strm_free(ngtcp2_strm *strm) { 74 | if (strm == NULL) { 75 | return; 76 | } 77 | 78 | ngtcp2_rob_free(&strm->rob); 79 | ngtcp2_gaptr_free(&strm->acked_tx_offset); 80 | } 81 | 82 | uint64_t ngtcp2_strm_rx_offset(ngtcp2_strm *strm) { 83 | return ngtcp2_rob_first_gap_offset(&strm->rob); 84 | } 85 | 86 | int ngtcp2_strm_recv_reordering(ngtcp2_strm *strm, const uint8_t *data, 87 | size_t datalen, uint64_t offset) { 88 | return ngtcp2_rob_push(&strm->rob, offset, data, datalen); 89 | } 90 | 91 | void ngtcp2_strm_shutdown(ngtcp2_strm *strm, uint32_t flags) { 92 | strm->flags |= flags & NGTCP2_STRM_FLAG_SHUT_RDWR; 93 | } 94 | -------------------------------------------------------------------------------- /lib/ngtcp2_strm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_STRM_H 26 | #define NGTCP2_STRM_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | # include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | #include 33 | 34 | #include "ngtcp2_rob.h" 35 | #include "ngtcp2_buf.h" 36 | #include "ngtcp2_map.h" 37 | #include "ngtcp2_gaptr.h" 38 | 39 | typedef enum { 40 | NGTCP2_STRM_FLAG_NONE = 0, 41 | /* NGTCP2_STRM_FLAG_SHUT_RD indicates that further reception of 42 | stream data is not allowed. */ 43 | NGTCP2_STRM_FLAG_SHUT_RD = 0x01, 44 | /* NGTCP2_STRM_FLAG_SHUT_WR indicates that further transmission of 45 | stream data is not allowed. */ 46 | NGTCP2_STRM_FLAG_SHUT_WR = 0x02, 47 | NGTCP2_STRM_FLAG_SHUT_RDWR = 48 | NGTCP2_STRM_FLAG_SHUT_RD | NGTCP2_STRM_FLAG_SHUT_WR, 49 | /* NGTCP2_STRM_FLAG_SENT_RST indicates that RST_STREAM is sent from 50 | the local endpoint. In this case, NGTCP2_STRM_FLAG_SHUT_WR is 51 | also set. */ 52 | NGTCP2_STRM_FLAG_SENT_RST = 0x04, 53 | /* NGTCP2_STRM_FLAG_SENT_RST indicates that RST_STREAM is received 54 | from the remote endpoint. In this case, NGTCP2_STRM_FLAG_SHUT_RD 55 | is also set. */ 56 | NGTCP2_STRM_FLAG_RECV_RST = 0x08, 57 | /* NGTCP2_STRM_FLAG_STOP_SENDING indicates that STOP_SENDING is sent 58 | from the local endpoint. */ 59 | NGTCP2_STRM_FLAG_STOP_SENDING = 0x10, 60 | } ngtcp2_strm_flags; 61 | 62 | struct ngtcp2_strm; 63 | 64 | typedef struct ngtcp2_strm ngtcp2_strm; 65 | 66 | struct ngtcp2_strm { 67 | ngtcp2_map_entry me; 68 | uint64_t tx_offset; 69 | ngtcp2_gaptr acked_tx_offset; 70 | /* max_tx_offset is the maximum offset that local endpoint can send 71 | for this stream. */ 72 | uint64_t max_tx_offset; 73 | /* last_rx_offset is the largest offset of stream data received for 74 | this stream. */ 75 | uint64_t last_rx_offset; 76 | ngtcp2_rob rob; 77 | /* max_rx_offset is the maximum offset that remote endpoint can send 78 | to this stream. */ 79 | uint64_t max_rx_offset; 80 | /* unsent_max_rx_offset is the maximum offset that remote endpoint 81 | can send to this stream, and it is not notified to the remote 82 | endpoint. unsent_max_rx_offset >= max_rx_offset must be hold. */ 83 | uint64_t unsent_max_rx_offset; 84 | ngtcp2_mem *mem; 85 | size_t nbuffered; 86 | ngtcp2_buf tx_buf; 87 | uint64_t stream_id; 88 | void *stream_user_data; 89 | /* flags is bit-wise OR of zero or more of ngtcp2_strm_flags. */ 90 | uint32_t flags; 91 | ngtcp2_strm **fc_pprev, *fc_next; 92 | /* app_error_code is an error code the local endpoint sent in 93 | RST_STREAM or STOP_SENDING. */ 94 | uint16_t app_error_code; 95 | }; 96 | 97 | /* 98 | * ngtcp2_strm_init initializes |strm|. 99 | * 100 | * This function returns 0 if it succeeds, or one of the following 101 | * negative error codes: 102 | * 103 | * NGTCP2_ERR_NOMEM 104 | * Out of memory 105 | */ 106 | int ngtcp2_strm_init(ngtcp2_strm *strm, uint64_t stream_id, uint32_t flags, 107 | uint64_t max_rx_offset, uint64_t max_tx_offset, 108 | void *stream_user_data, ngtcp2_mem *mem); 109 | 110 | /* 111 | * ngtcp2_strm_free deallocates memory allocated for |strm|. This 112 | * function does not free the memory pointed by |strm| itself. 113 | */ 114 | void ngtcp2_strm_free(ngtcp2_strm *strm); 115 | 116 | /* 117 | * ngtcp2_strm_rx_offset returns the minimum offset of stream data 118 | * which is not received yet. 119 | */ 120 | uint64_t ngtcp2_strm_rx_offset(ngtcp2_strm *strm); 121 | 122 | /* 123 | * ngtcp2_strm_recv_reordering handles reordered data. 124 | * 125 | * It returns 0 if it succeeds, or one of the following negative error 126 | * codes: 127 | * 128 | * NGTCP2_ERR_NOMEM 129 | * Out of memory 130 | */ 131 | int ngtcp2_strm_recv_reordering(ngtcp2_strm *strm, const uint8_t *data, 132 | size_t datalen, uint64_t offset); 133 | 134 | /* 135 | * ngtcp2_strm_shutdown shutdowns |strm|. |flags| should be 136 | * NGTCP2_STRM_FLAG_SHUT_RD, and/or NGTCP2_STRM_FLAG_SHUT_WR. 137 | */ 138 | void ngtcp2_strm_shutdown(ngtcp2_strm *strm, uint32_t flags); 139 | 140 | #endif /* NGTCP2_STRM_H */ 141 | -------------------------------------------------------------------------------- /m4/ax_check_compile_flag.m4: -------------------------------------------------------------------------------- 1 | # =========================================================================== 2 | # http://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html 3 | # =========================================================================== 4 | # 5 | # SYNOPSIS 6 | # 7 | # AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT]) 8 | # 9 | # DESCRIPTION 10 | # 11 | # Check whether the given FLAG works with the current language's compiler 12 | # or gives an error. (Warnings, however, are ignored) 13 | # 14 | # ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on 15 | # success/failure. 16 | # 17 | # If EXTRA-FLAGS is defined, it is added to the current language's default 18 | # flags (e.g. CFLAGS) when the check is done. The check is thus made with 19 | # the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to 20 | # force the compiler to issue an error when a bad flag is given. 21 | # 22 | # INPUT gives an alternative input source to AC_COMPILE_IFELSE. 23 | # 24 | # NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this 25 | # macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG. 26 | # 27 | # LICENSE 28 | # 29 | # Copyright (c) 2008 Guido U. Draheim 30 | # Copyright (c) 2011 Maarten Bosmans 31 | # 32 | # This program is free software: you can redistribute it and/or modify it 33 | # under the terms of the GNU General Public License as published by the 34 | # Free Software Foundation, either version 3 of the License, or (at your 35 | # option) any later version. 36 | # 37 | # This program is distributed in the hope that it will be useful, but 38 | # WITHOUT ANY WARRANTY; without even the implied warranty of 39 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 40 | # Public License for more details. 41 | # 42 | # You should have received a copy of the GNU General Public License along 43 | # with this program. If not, see . 44 | # 45 | # As a special exception, the respective Autoconf Macro's copyright owner 46 | # gives unlimited permission to copy, distribute and modify the configure 47 | # scripts that are the output of Autoconf when processing the Macro. You 48 | # need not follow the terms of the GNU General Public License when using 49 | # or distributing such scripts, even though portions of the text of the 50 | # Macro appear in them. The GNU General Public License (GPL) does govern 51 | # all other use of the material that constitutes the Autoconf Macro. 52 | # 53 | # This special exception to the GPL applies to versions of the Autoconf 54 | # Macro released by the Autoconf Archive. When you make and distribute a 55 | # modified version of the Autoconf Macro, you may extend this special 56 | # exception to the GPL to apply to your modified version as well. 57 | 58 | #serial 4 59 | 60 | AC_DEFUN([AX_CHECK_COMPILE_FLAG], 61 | [AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF 62 | AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl 63 | AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [ 64 | ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS 65 | _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1" 66 | AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])], 67 | [AS_VAR_SET(CACHEVAR,[yes])], 68 | [AS_VAR_SET(CACHEVAR,[no])]) 69 | _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags]) 70 | AS_VAR_IF(CACHEVAR,yes, 71 | [m4_default([$2], :)], 72 | [m4_default([$3], :)]) 73 | AS_VAR_POPDEF([CACHEVAR])dnl 74 | ])dnl AX_CHECK_COMPILE_FLAGS 75 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2016 ngtcp2 contributors 4 | # Copyright (c) 2012 nghttp2 contributors 5 | 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | if(HAVE_CUNIT) 26 | include_directories( 27 | "${CMAKE_SOURCE_DIR}/lib" 28 | "${CMAKE_SOURCE_DIR}/lib/includes" 29 | "${CMAKE_BINARY_DIR}/lib/includes" 30 | ${CUNIT_INCLUDE_DIRS} 31 | ) 32 | 33 | set(main_SOURCES 34 | main.c 35 | ngtcp2_pkt_test.c 36 | ngtcp2_range_test.c 37 | ngtcp2_rob_test.c 38 | ngtcp2_acktr_test.c 39 | ngtcp2_map_test.c 40 | ngtcp2_crypto_test.c 41 | ngtcp2_rtb_test.c 42 | ngtcp2_idtr_test.c 43 | ngtcp2_conn_test.c 44 | ngtcp2_ringbuf_test.c 45 | ngtcp2_conv_test.c 46 | ngtcp2_test_helper.c 47 | ngtcp2_psl_test.c 48 | ngtcp2_ksl_test.c 49 | ) 50 | 51 | add_executable(main EXCLUDE_FROM_ALL 52 | ${main_SOURCES} 53 | ) 54 | target_include_directories(main PRIVATE ${CUNIT_INCLUDE_DIRS}) 55 | # FIXME enable and fix warnings 56 | #set_target_properties(main PROPERTIES COMPILE_FLAGS "${WARNCFLAGS}") 57 | target_link_libraries(main 58 | ngtcp2_static 59 | ${CUNIT_LIBRARIES} 60 | ) 61 | add_test(main main) 62 | add_dependencies(check main) 63 | endif() 64 | -------------------------------------------------------------------------------- /tests/Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2016 ngtcp2 contributors 4 | # Copyright (c) 2012 nghttp2 contributors 5 | 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | if HAVE_CUNIT 26 | 27 | check_PROGRAMS = main 28 | 29 | OBJECTS = \ 30 | main.c \ 31 | ngtcp2_pkt_test.c \ 32 | ngtcp2_range_test.c \ 33 | ngtcp2_rob_test.c \ 34 | ngtcp2_acktr_test.c \ 35 | ngtcp2_map_test.c \ 36 | ngtcp2_crypto_test.c \ 37 | ngtcp2_rtb_test.c \ 38 | ngtcp2_idtr_test.c \ 39 | ngtcp2_conn_test.c \ 40 | ngtcp2_ringbuf_test.c \ 41 | ngtcp2_conv_test.c \ 42 | ngtcp2_psl_test.c \ 43 | ngtcp2_ksl_test.c \ 44 | ngtcp2_test_helper.c 45 | HFILES= \ 46 | ngtcp2_pkt_test.h \ 47 | ngtcp2_range_test.h \ 48 | ngtcp2_rob_test.h \ 49 | ngtcp2_acktr_test.h \ 50 | ngtcp2_map_test.h \ 51 | ngtcp2_crypto_test.h \ 52 | ngtcp2_rtb_test.h \ 53 | ngtcp2_idtr_test.h \ 54 | ngtcp2_conn_test.h \ 55 | ngtcp2_ringbuf_test.h \ 56 | ngtcp2_conv_test.h \ 57 | ngtcp2_psl_test.h \ 58 | ngtcp2_ksl_test.h \ 59 | ngtcp2_test_helper.h 60 | 61 | main_SOURCES = $(HFILES) $(OBJECTS) 62 | 63 | # With static lib disabled and symbol hiding enabled, we have to link object 64 | # files directly because the tests use symbols not included in public API. 65 | main_LDADD = ${top_builddir}/lib/.libs/*.o 66 | main_LDADD += @CUNIT_LIBS@ 67 | main_LDFLAGS = -static 68 | 69 | AM_CFLAGS = $(WARNCFLAGS) \ 70 | -I${top_srcdir}/lib \ 71 | -I${top_srcdir}/lib/includes \ 72 | -I${top_builddir}/lib/includes \ 73 | @CUNIT_CFLAGS@ @DEFS@ 74 | AM_LDFLAGS = -no-install 75 | 76 | TESTS = main 77 | 78 | endif # HAVE_CUNIT 79 | -------------------------------------------------------------------------------- /tests/ngtcp2_acktr_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_ACKTR_TEST_H 26 | #define NGTCP2_ACKTR_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_acktr_add(void); 33 | void test_ngtcp2_acktr_eviction(void); 34 | void test_ngtcp2_acktr_forget(void); 35 | void test_ngtcp2_acktr_recv_ack(void); 36 | 37 | #endif /* NGTCP2_ACKTR_TEST_H */ 38 | -------------------------------------------------------------------------------- /tests/ngtcp2_conn_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_CONN_TEST_H 26 | #define NGTCP2_CONN_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_conn_stream_open_close(void); 33 | void test_ngtcp2_conn_stream_rx_flow_control(void); 34 | void test_ngtcp2_conn_stream_rx_flow_control_error(void); 35 | void test_ngtcp2_conn_stream_tx_flow_control(void); 36 | void test_ngtcp2_conn_rx_flow_control(void); 37 | void test_ngtcp2_conn_rx_flow_control_error(void); 38 | void test_ngtcp2_conn_tx_flow_control(void); 39 | void test_ngtcp2_conn_shutdown_stream_write(void); 40 | void test_ngtcp2_conn_recv_rst_stream(void); 41 | void test_ngtcp2_conn_recv_stop_sending(void); 42 | void test_ngtcp2_conn_recv_conn_id_omitted(void); 43 | void test_ngtcp2_conn_short_pkt_type(void); 44 | void test_ngtcp2_conn_recv_stateless_reset(void); 45 | void test_ngtcp2_conn_recv_server_stateless_retry(void); 46 | void test_ngtcp2_conn_recv_delayed_handshake_pkt(void); 47 | void test_ngtcp2_conn_recv_max_stream_id(void); 48 | void test_ngtcp2_conn_handshake(void); 49 | void test_ngtcp2_conn_handshake_error(void); 50 | void test_ngtcp2_conn_client_handshake(void); 51 | void test_ngtcp2_conn_retransmit_protected(void); 52 | void test_ngtcp2_conn_send_max_stream_data(void); 53 | void test_ngtcp2_conn_recv_stream_data(void); 54 | void test_ngtcp2_conn_recv_ping(void); 55 | void test_ngtcp2_conn_recv_max_stream_data(void); 56 | void test_ngtcp2_conn_send_early_data(void); 57 | void test_ngtcp2_conn_recv_early_data(void); 58 | void test_ngtcp2_conn_recv_compound_pkt(void); 59 | void test_ngtcp2_conn_pkt_payloadlen(void); 60 | 61 | #endif /* NGTCP2_CONN_TEST_H */ 62 | -------------------------------------------------------------------------------- /tests/ngtcp2_conv_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_CONV_TEST_H 26 | #define NGTCP2_CONV_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_get_varint(void); 33 | void test_ngtcp2_get_varint_len(void); 34 | void test_ngtcp2_put_varint_len(void); 35 | void test_ngtcp2_nth_server_bidi_id(void); 36 | void test_ngtcp2_nth_server_uni_id(void); 37 | void test_ngtcp2_nth_client_bidi_id(void); 38 | void test_ngtcp2_nth_client_uni_id(void); 39 | 40 | #endif /* NGTCP2_CONV_TEST_H */ 41 | -------------------------------------------------------------------------------- /tests/ngtcp2_crypto_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_CRYPTO_TEST_H 26 | #define NGTCP2_CRYPTO_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_encode_transport_params(void); 33 | 34 | #endif /* NGTCP2_CRYPTO_TEST_H */ 35 | -------------------------------------------------------------------------------- /tests/ngtcp2_idtr_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_idtr_test.h" 26 | 27 | #include 28 | 29 | #include "ngtcp2_idtr.h" 30 | #include "ngtcp2_test_helper.h" 31 | #include "ngtcp2_mem.h" 32 | 33 | static uint64_t stream_id_from_id(uint64_t id) { return id * 4; } 34 | 35 | void test_ngtcp2_idtr_open(void) { 36 | ngtcp2_mem *mem = ngtcp2_mem_default(); 37 | ngtcp2_idtr idtr; 38 | int rv; 39 | ngtcp2_idtr_gap *g; 40 | 41 | rv = ngtcp2_idtr_init(&idtr, 0, mem); 42 | 43 | CU_ASSERT(0 == rv); 44 | 45 | rv = ngtcp2_idtr_open(&idtr, stream_id_from_id(0)); 46 | 47 | CU_ASSERT(0 == rv); 48 | CU_ASSERT(1 == idtr.gap->range.begin); 49 | CU_ASSERT(UINT64_MAX == idtr.gap->range.end); 50 | CU_ASSERT(NULL == idtr.gap->next); 51 | 52 | rv = ngtcp2_idtr_open(&idtr, stream_id_from_id(1000000007)); 53 | 54 | CU_ASSERT(0 == rv); 55 | 56 | g = idtr.gap; 57 | 58 | CU_ASSERT(1 == idtr.gap->range.begin); 59 | CU_ASSERT(1000000007 == g->range.end); 60 | 61 | g = g->next; 62 | 63 | CU_ASSERT(1000000008 == g->range.begin); 64 | CU_ASSERT(UINT64_MAX == g->range.end); 65 | CU_ASSERT(NULL == g->next); 66 | 67 | rv = ngtcp2_idtr_open(&idtr, stream_id_from_id(0)); 68 | 69 | CU_ASSERT(NGTCP2_ERR_STREAM_IN_USE == rv); 70 | 71 | rv = ngtcp2_idtr_open(&idtr, stream_id_from_id(1000000007)); 72 | 73 | CU_ASSERT(NGTCP2_ERR_STREAM_IN_USE == rv); 74 | 75 | ngtcp2_idtr_free(&idtr); 76 | } 77 | -------------------------------------------------------------------------------- /tests/ngtcp2_idtr_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_IDTR_TEST_H 26 | #define NGTCP2_IDTR_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_idtr_open(void); 33 | 34 | #endif /* NGTCP2_IDTR_TEST_H */ 35 | -------------------------------------------------------------------------------- /tests/ngtcp2_ksl_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_KSL_TEST_H 26 | #define NGTCP2_KSL_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_ksl_insert(void); 33 | 34 | #endif /* NGTCP2_KSL_TEST_H */ 35 | -------------------------------------------------------------------------------- /tests/ngtcp2_map_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * Copyright (c) 2012 nghttp2 contributors 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * "Software"), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef NGTCP2_MAP_TEST_H 27 | #define NGTCP2_MAP_TEST_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | #include 31 | #endif /* HAVE_CONFIG_H */ 32 | 33 | void test_ngtcp2_map(void); 34 | void test_ngtcp2_map_functional(void); 35 | void test_ngtcp2_map_each_free(void); 36 | 37 | #endif /* NGTCP2_MAP_TEST_H */ 38 | -------------------------------------------------------------------------------- /tests/ngtcp2_pkt_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_PKT_TEST_H 26 | #define NGTCP2_PKT_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_pkt_decode_hd_long(void); 33 | void test_ngtcp2_pkt_decode_hd_short(void); 34 | void test_ngtcp2_pkt_decode_stream_frame(void); 35 | void test_ngtcp2_pkt_decode_ack_frame(void); 36 | void test_ngtcp2_pkt_decode_padding_frame(void); 37 | void test_ngtcp2_pkt_encode_stream_frame(void); 38 | void test_ngtcp2_pkt_encode_ack_frame(void); 39 | void test_ngtcp2_pkt_encode_rst_stream_frame(void); 40 | void test_ngtcp2_pkt_encode_connection_close_frame(void); 41 | void test_ngtcp2_pkt_encode_application_close_frame(void); 42 | void test_ngtcp2_pkt_encode_max_data_frame(void); 43 | void test_ngtcp2_pkt_encode_max_stream_data_frame(void); 44 | void test_ngtcp2_pkt_encode_max_stream_id_frame(void); 45 | void test_ngtcp2_pkt_encode_ping_frame(void); 46 | void test_ngtcp2_pkt_encode_blocked_frame(void); 47 | void test_ngtcp2_pkt_encode_stream_blocked_frame(void); 48 | void test_ngtcp2_pkt_encode_stream_id_blocked_frame(void); 49 | void test_ngtcp2_pkt_encode_new_connection_id_frame(void); 50 | void test_ngtcp2_pkt_encode_stop_sending_frame(void); 51 | void test_ngtcp2_pkt_encode_path_challenge_frame(void); 52 | void test_ngtcp2_pkt_encode_path_response_frame(void); 53 | void test_ngtcp2_pkt_encode_crypto_frame(void); 54 | void test_ngtcp2_pkt_adjust_pkt_num(void); 55 | void test_ngtcp2_pkt_validate_ack(void); 56 | void test_ngtcp2_pkt_write_stateless_reset(void); 57 | void test_ngtcp2_pkt_write_version_negotiation(void); 58 | 59 | #endif /* NGTCP2_PKT_TEST_H */ 60 | -------------------------------------------------------------------------------- /tests/ngtcp2_psl_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2018 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_PSL_TEST_H 26 | #define NGTCP2_PSL_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_psl_insert(void); 33 | 34 | #endif /* NGTCP2_PSL_TEST_H */ 35 | -------------------------------------------------------------------------------- /tests/ngtcp2_range_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_range_test.h" 26 | 27 | #include 28 | 29 | #include "ngtcp2_range.h" 30 | #include "ngtcp2_test_helper.h" 31 | 32 | void test_ngtcp2_range_intersect(void) { 33 | ngtcp2_range a, b, c; 34 | 35 | ngtcp2_range_init(&a, 0, UINT64_MAX); 36 | ngtcp2_range_init(&b, 0, 1000000007); 37 | c = ngtcp2_range_intersect(&a, &b); 38 | 39 | CU_ASSERT(0 == c.begin); 40 | CU_ASSERT(1000000007 == c.end); 41 | 42 | ngtcp2_range_init(&a, 1000000007, UINT64_MAX); 43 | ngtcp2_range_init(&b, 0, UINT64_MAX); 44 | c = ngtcp2_range_intersect(&a, &b); 45 | 46 | CU_ASSERT(1000000007 == c.begin); 47 | CU_ASSERT(UINT64_MAX == c.end); 48 | 49 | ngtcp2_range_init(&a, 0, UINT64_MAX); 50 | ngtcp2_range_init(&b, 33333, 55555); 51 | c = ngtcp2_range_intersect(&a, &b); 52 | 53 | CU_ASSERT(33333 == c.begin); 54 | CU_ASSERT(55555 == c.end); 55 | 56 | ngtcp2_range_init(&a, 0, 1000000009); 57 | ngtcp2_range_init(&b, 1000000007, UINT64_MAX); 58 | c = ngtcp2_range_intersect(&a, &b); 59 | 60 | CU_ASSERT(1000000007 == c.begin); 61 | CU_ASSERT(1000000009 == c.end); 62 | } 63 | 64 | void test_ngtcp2_range_cut(void) { 65 | ngtcp2_range a, b, l, r; 66 | 67 | ngtcp2_range_init(&a, 0, UINT64_MAX); 68 | ngtcp2_range_init(&b, 1000000007, 1000000009); 69 | ngtcp2_range_cut(&l, &r, &a, &b); 70 | 71 | CU_ASSERT(0 == l.begin); 72 | CU_ASSERT(1000000007 == l.end); 73 | CU_ASSERT(1000000009 == r.begin); 74 | CU_ASSERT(UINT64_MAX == r.end); 75 | 76 | ngtcp2_range_init(&a, 0, UINT64_MAX); 77 | ngtcp2_range_init(&b, 0, 1000000007); 78 | ngtcp2_range_cut(&l, &r, &a, &b); 79 | 80 | CU_ASSERT(0 == ngtcp2_range_len(&l)); 81 | CU_ASSERT(1000000007 == r.begin); 82 | CU_ASSERT(UINT64_MAX == r.end); 83 | 84 | ngtcp2_range_init(&a, 0, UINT64_MAX); 85 | ngtcp2_range_init(&b, 1000000009, UINT64_MAX); 86 | ngtcp2_range_cut(&l, &r, &a, &b); 87 | 88 | CU_ASSERT(0 == l.begin); 89 | CU_ASSERT(1000000009 == l.end); 90 | CU_ASSERT(0 == ngtcp2_range_len(&r)); 91 | } 92 | 93 | void test_ngtcp2_range_not_after(void) { 94 | ngtcp2_range a, b; 95 | 96 | ngtcp2_range_init(&a, 1, 1000000007); 97 | ngtcp2_range_init(&b, 0, 1000000007); 98 | 99 | CU_ASSERT(ngtcp2_range_not_after(&a, &b)); 100 | 101 | ngtcp2_range_init(&a, 1, 1000000008); 102 | ngtcp2_range_init(&b, 0, 1000000007); 103 | 104 | CU_ASSERT(!ngtcp2_range_not_after(&a, &b)); 105 | } 106 | -------------------------------------------------------------------------------- /tests/ngtcp2_range_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_RANGE_TEST_H 26 | #define NGTCP2_RANGE_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_range_intersect(void); 33 | void test_ngtcp2_range_cut(void); 34 | void test_ngtcp2_range_not_after(void); 35 | 36 | #endif /* NGTCP2_RANGE_TEST_H */ 37 | -------------------------------------------------------------------------------- /tests/ngtcp2_ringbuf_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #include "ngtcp2_ringbuf_test.h" 26 | 27 | #include 28 | 29 | #include "ngtcp2_ringbuf.h" 30 | #include "ngtcp2_test_helper.h" 31 | 32 | typedef struct { 33 | int32_t a; 34 | uint64_t b; 35 | } ints; 36 | 37 | void test_ngtcp2_ringbuf_push_front(void) { 38 | ngtcp2_ringbuf rb; 39 | ngtcp2_mem *mem = ngtcp2_mem_default(); 40 | size_t i; 41 | 42 | ngtcp2_ringbuf_init(&rb, 64, sizeof(ints), mem); 43 | 44 | for (i = 0; i < 64; ++i) { 45 | ints *p = ngtcp2_ringbuf_push_front(&rb); 46 | p->a = (int32_t)(i + 1); 47 | p->b = (i + 1) * 10; 48 | } 49 | 50 | CU_ASSERT(64 == ngtcp2_ringbuf_len(&rb)); 51 | 52 | for (i = 0; i < 64; ++i) { 53 | ints *p = ngtcp2_ringbuf_get(&rb, i); 54 | CU_ASSERT((int32_t)(64 - i) == p->a); 55 | CU_ASSERT((64 - i) * 10 == p->b); 56 | } 57 | 58 | ngtcp2_ringbuf_push_front(&rb); 59 | 60 | CU_ASSERT(64 == ngtcp2_ringbuf_len(&rb)); 61 | CU_ASSERT((int32_t)64 == ((ints *)ngtcp2_ringbuf_get(&rb, 1))->a); 62 | 63 | ngtcp2_ringbuf_free(&rb); 64 | } 65 | 66 | void test_ngtcp2_ringbuf_pop_front(void) { 67 | ngtcp2_ringbuf rb; 68 | ngtcp2_mem *mem = ngtcp2_mem_default(); 69 | size_t i; 70 | 71 | ngtcp2_ringbuf_init(&rb, 4, sizeof(ints), mem); 72 | 73 | for (i = 0; i < 5; ++i) { 74 | ints *p = ngtcp2_ringbuf_push_front(&rb); 75 | p->a = (int32_t)i; 76 | } 77 | 78 | CU_ASSERT(4 == ngtcp2_ringbuf_len(&rb)); 79 | 80 | for (i = 4; i >= 1; --i) { 81 | ints *p = ngtcp2_ringbuf_get(&rb, 0); 82 | 83 | CU_ASSERT((int32_t)i == p->a); 84 | 85 | ngtcp2_ringbuf_pop_front(&rb); 86 | } 87 | 88 | CU_ASSERT(0 == ngtcp2_ringbuf_len(&rb)); 89 | 90 | ngtcp2_ringbuf_free(&rb); 91 | } 92 | -------------------------------------------------------------------------------- /tests/ngtcp2_ringbuf_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_RINGBUF_TEST_H 26 | #define NGTCP2_RINGBUF_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_ringbuf_push_front(void); 33 | void test_ngtcp2_ringbuf_pop_front(void); 34 | 35 | #endif /* NGTCP2_RINGBUF_TEST_H */ 36 | -------------------------------------------------------------------------------- /tests/ngtcp2_rob_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_ROB_TEST_H 26 | #define NGTCP2_ROB_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_rob_push(void); 33 | void test_ngtcp2_rob_push_random(void); 34 | void test_ngtcp2_rob_data_at(void); 35 | void test_ngtcp2_rob_remove_prefix(void); 36 | 37 | #endif /* NGTCP2_ROB_TEST_H */ 38 | -------------------------------------------------------------------------------- /tests/ngtcp2_rtb_test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ngtcp2 3 | * 4 | * Copyright (c) 2017 ngtcp2 contributors 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | */ 25 | #ifndef NGTCP2_RTB_TEST_H 26 | #define NGTCP2_RTB_TEST_H 27 | 28 | #ifdef HAVE_CONFIG_H 29 | #include 30 | #endif /* HAVE_CONFIG_H */ 31 | 32 | void test_ngtcp2_rtb_add(void); 33 | void test_ngtcp2_rtb_recv_ack(void); 34 | void test_ngtcp2_rtb_insert_range(void); 35 | 36 | #endif /* NGTCP2_RTB_TEST_H */ 37 | -------------------------------------------------------------------------------- /third-party/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2017 ngtcp2 contributors 4 | # Copyright (c) 2014 nghttp2 contributors 5 | 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | set(LIBHTTP_PARSER_SOURCES 26 | http-parser/http_parser.c 27 | ) 28 | add_library(http-parser OBJECT ${LIBHTTP_PARSER_SOURCES}) 29 | set_target_properties(http-parser PROPERTIES 30 | POSITION_INDEPENDENT_CODE ON) 31 | -------------------------------------------------------------------------------- /third-party/Makefile.am: -------------------------------------------------------------------------------- 1 | # ngtcp2 2 | 3 | # Copyright (c) 2017 ngtcp2 contributors 4 | # Copyright (c) 2014 nghttp2 contributors 5 | 6 | # Permission is hereby granted, free of charge, to any person obtaining 7 | # a copy of this software and associated documentation files (the 8 | # "Software"), to deal in the Software without restriction, including 9 | # without limitation the rights to use, copy, modify, merge, publish, 10 | # distribute, sublicense, and/or sell copies of the Software, and to 11 | # permit persons to whom the Software is furnished to do so, subject to 12 | # the following conditions: 13 | 14 | # The above copyright notice and this permission notice shall be 15 | # included in all copies or substantial portions of the Software. 16 | 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | AM_CPPFLAGS = @DEFS@ 26 | 27 | noinst_LTLIBRARIES = libhttp-parser.la 28 | libhttp_parser_la_SOURCES = \ 29 | http-parser/http_parser.c \ 30 | http-parser/http_parser.h 31 | -------------------------------------------------------------------------------- /third-party/http-parser/.gitignore: -------------------------------------------------------------------------------- 1 | /out/ 2 | core 3 | tags 4 | *.o 5 | test 6 | test_g 7 | test_fast 8 | bench 9 | url_parser 10 | parsertrace 11 | parsertrace_g 12 | *.mk 13 | *.Makefile 14 | *.so.* 15 | *.exe.* 16 | *.exe 17 | *.a 18 | 19 | 20 | # Visual Studio uglies 21 | *.suo 22 | *.sln 23 | *.vcxproj 24 | *.vcxproj.filters 25 | *.vcxproj.user 26 | *.opensdf 27 | *.ncrunchsolution* 28 | *.sdf 29 | *.vsp 30 | *.psess 31 | -------------------------------------------------------------------------------- /third-party/http-parser/.mailmap: -------------------------------------------------------------------------------- 1 | # update AUTHORS with: 2 | # git log --all --reverse --format='%aN <%aE>' | perl -ne 'BEGIN{print "# Authors ordered by first contribution.\n"} print unless $h{$_}; $h{$_} = 1' > AUTHORS 3 | Ryan Dahl 4 | Salman Haq 5 | Simon Zimmermann 6 | Thomas LE ROUX LE ROUX Thomas 7 | Thomas LE ROUX Thomas LE ROUX 8 | Fedor Indutny 9 | -------------------------------------------------------------------------------- /third-party/http-parser/.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | compiler: 4 | - clang 5 | - gcc 6 | 7 | script: 8 | - "make" 9 | 10 | notifications: 11 | email: false 12 | irc: 13 | - "irc.freenode.net#node-ci" 14 | -------------------------------------------------------------------------------- /third-party/http-parser/AUTHORS: -------------------------------------------------------------------------------- 1 | # Authors ordered by first contribution. 2 | Ryan Dahl 3 | Jeremy Hinegardner 4 | Sergey Shepelev 5 | Joe Damato 6 | tomika 7 | Phoenix Sol 8 | Cliff Frey 9 | Ewen Cheslack-Postava 10 | Santiago Gala 11 | Tim Becker 12 | Jeff Terrace 13 | Ben Noordhuis 14 | Nathan Rajlich 15 | Mark Nottingham 16 | Aman Gupta 17 | Tim Becker 18 | Sean Cunningham 19 | Peter Griess 20 | Salman Haq 21 | Cliff Frey 22 | Jon Kolb 23 | Fouad Mardini 24 | Paul Querna 25 | Felix Geisendörfer 26 | koichik 27 | Andre Caron 28 | Ivo Raisr 29 | James McLaughlin 30 | David Gwynne 31 | Thomas LE ROUX 32 | Randy Rizun 33 | Andre Louis Caron 34 | Simon Zimmermann 35 | Erik Dubbelboer 36 | Martell Malone 37 | Bertrand Paquet 38 | BogDan Vatra 39 | Peter Faiman 40 | Corey Richardson 41 | Tóth Tamás 42 | Cam Swords 43 | Chris Dickinson 44 | Uli Köhler 45 | Charlie Somerville 46 | Patrik Stutz 47 | Fedor Indutny 48 | runner 49 | Alexis Campailla 50 | David Wragg 51 | Vinnie Falco 52 | Alex Butum 53 | Rex Feng 54 | Alex Kocharin 55 | Mark Koopman 56 | Helge Heß 57 | Alexis La Goutte 58 | George Miroshnykov 59 | Maciej Małecki 60 | Marc O'Morain 61 | Jeff Pinner 62 | Timothy J Fontaine 63 | Akagi201 64 | Romain Giraud 65 | Jay Satiro 66 | Arne Steen 67 | Kjell Schubert 68 | Olivier Mengué 69 | -------------------------------------------------------------------------------- /third-party/http-parser/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright 2 | Igor Sysoev. 3 | 4 | Additional changes are licensed under the same terms as NGINX and 5 | copyright Joyent, Inc. and other Node contributors. All rights reserved. 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to 9 | deal in the Software without restriction, including without limitation the 10 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11 | sell copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /third-party/http-parser/bench.c: -------------------------------------------------------------------------------- 1 | /* Copyright Fedor Indutny. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | #include "http_parser.h" 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | static const char data[] = 28 | "POST /joyent/http-parser HTTP/1.1\r\n" 29 | "Host: github.com\r\n" 30 | "DNT: 1\r\n" 31 | "Accept-Encoding: gzip, deflate, sdch\r\n" 32 | "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n" 33 | "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) " 34 | "AppleWebKit/537.36 (KHTML, like Gecko) " 35 | "Chrome/39.0.2171.65 Safari/537.36\r\n" 36 | "Accept: text/html,application/xhtml+xml,application/xml;q=0.9," 37 | "image/webp,*/*;q=0.8\r\n" 38 | "Referer: https://github.com/joyent/http-parser\r\n" 39 | "Connection: keep-alive\r\n" 40 | "Transfer-Encoding: chunked\r\n" 41 | "Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n"; 42 | static const size_t data_len = sizeof(data) - 1; 43 | 44 | static int on_info(http_parser* p) { 45 | return 0; 46 | } 47 | 48 | 49 | static int on_data(http_parser* p, const char *at, size_t length) { 50 | return 0; 51 | } 52 | 53 | static http_parser_settings settings = { 54 | .on_message_begin = on_info, 55 | .on_headers_complete = on_info, 56 | .on_message_complete = on_info, 57 | .on_header_field = on_data, 58 | .on_header_value = on_data, 59 | .on_url = on_data, 60 | .on_status = on_data, 61 | .on_body = on_data 62 | }; 63 | 64 | int bench(int iter_count, int silent) { 65 | struct http_parser parser; 66 | int i; 67 | int err; 68 | struct timeval start; 69 | struct timeval end; 70 | float rps; 71 | 72 | if (!silent) { 73 | err = gettimeofday(&start, NULL); 74 | assert(err == 0); 75 | } 76 | 77 | for (i = 0; i < iter_count; i++) { 78 | size_t parsed; 79 | http_parser_init(&parser, HTTP_REQUEST); 80 | 81 | parsed = http_parser_execute(&parser, &settings, data, data_len); 82 | assert(parsed == data_len); 83 | } 84 | 85 | if (!silent) { 86 | err = gettimeofday(&end, NULL); 87 | assert(err == 0); 88 | 89 | fprintf(stdout, "Benchmark result:\n"); 90 | 91 | rps = (float) (end.tv_sec - start.tv_sec) + 92 | (end.tv_usec - start.tv_usec) * 1e-6f; 93 | fprintf(stdout, "Took %f seconds to run\n", rps); 94 | 95 | rps = (float) iter_count / rps; 96 | fprintf(stdout, "%f req/sec\n", rps); 97 | fflush(stdout); 98 | } 99 | 100 | return 0; 101 | } 102 | 103 | int main(int argc, char** argv) { 104 | if (argc == 2 && strcmp(argv[1], "infinite") == 0) { 105 | for (;;) 106 | bench(5000000, 1); 107 | return 0; 108 | } else { 109 | return bench(5000000, 0); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /third-party/http-parser/contrib/parsertrace.c: -------------------------------------------------------------------------------- 1 | /* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev 2 | * 3 | * Additional changes are licensed under the same terms as NGINX and 4 | * copyright Joyent, Inc. and other Node contributors. All rights reserved. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to 8 | * deal in the Software without restriction, including without limitation the 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 | * sell copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 | * IN THE SOFTWARE. 23 | */ 24 | 25 | /* Dump what the parser finds to stdout as it happen */ 26 | 27 | #include "http_parser.h" 28 | #include 29 | #include 30 | #include 31 | 32 | int on_message_begin(http_parser* _) { 33 | (void)_; 34 | printf("\n***MESSAGE BEGIN***\n\n"); 35 | return 0; 36 | } 37 | 38 | int on_headers_complete(http_parser* _) { 39 | (void)_; 40 | printf("\n***HEADERS COMPLETE***\n\n"); 41 | return 0; 42 | } 43 | 44 | int on_message_complete(http_parser* _) { 45 | (void)_; 46 | printf("\n***MESSAGE COMPLETE***\n\n"); 47 | return 0; 48 | } 49 | 50 | int on_url(http_parser* _, const char* at, size_t length) { 51 | (void)_; 52 | printf("Url: %.*s\n", (int)length, at); 53 | return 0; 54 | } 55 | 56 | int on_header_field(http_parser* _, const char* at, size_t length) { 57 | (void)_; 58 | printf("Header field: %.*s\n", (int)length, at); 59 | return 0; 60 | } 61 | 62 | int on_header_value(http_parser* _, const char* at, size_t length) { 63 | (void)_; 64 | printf("Header value: %.*s\n", (int)length, at); 65 | return 0; 66 | } 67 | 68 | int on_body(http_parser* _, const char* at, size_t length) { 69 | (void)_; 70 | printf("Body: %.*s\n", (int)length, at); 71 | return 0; 72 | } 73 | 74 | void usage(const char* name) { 75 | fprintf(stderr, 76 | "Usage: %s $type $filename\n" 77 | " type: -x, where x is one of {r,b,q}\n" 78 | " parses file as a Response, reQuest, or Both\n", 79 | name); 80 | exit(EXIT_FAILURE); 81 | } 82 | 83 | int main(int argc, char* argv[]) { 84 | enum http_parser_type file_type; 85 | 86 | if (argc != 3) { 87 | usage(argv[0]); 88 | } 89 | 90 | char* type = argv[1]; 91 | if (type[0] != '-') { 92 | usage(argv[0]); 93 | } 94 | 95 | switch (type[1]) { 96 | /* in the case of "-", type[1] will be NUL */ 97 | case 'r': 98 | file_type = HTTP_RESPONSE; 99 | break; 100 | case 'q': 101 | file_type = HTTP_REQUEST; 102 | break; 103 | case 'b': 104 | file_type = HTTP_BOTH; 105 | break; 106 | default: 107 | usage(argv[0]); 108 | } 109 | 110 | char* filename = argv[2]; 111 | FILE* file = fopen(filename, "r"); 112 | if (file == NULL) { 113 | perror("fopen"); 114 | goto fail; 115 | } 116 | 117 | fseek(file, 0, SEEK_END); 118 | long file_length = ftell(file); 119 | if (file_length == -1) { 120 | perror("ftell"); 121 | goto fail; 122 | } 123 | fseek(file, 0, SEEK_SET); 124 | 125 | char* data = malloc(file_length); 126 | if (fread(data, 1, file_length, file) != (size_t)file_length) { 127 | fprintf(stderr, "couldn't read entire file\n"); 128 | free(data); 129 | goto fail; 130 | } 131 | 132 | http_parser_settings settings; 133 | memset(&settings, 0, sizeof(settings)); 134 | settings.on_message_begin = on_message_begin; 135 | settings.on_url = on_url; 136 | settings.on_header_field = on_header_field; 137 | settings.on_header_value = on_header_value; 138 | settings.on_headers_complete = on_headers_complete; 139 | settings.on_body = on_body; 140 | settings.on_message_complete = on_message_complete; 141 | 142 | http_parser parser; 143 | http_parser_init(&parser, file_type); 144 | size_t nparsed = http_parser_execute(&parser, &settings, data, file_length); 145 | free(data); 146 | 147 | if (nparsed != (size_t)file_length) { 148 | fprintf(stderr, 149 | "Error: %s (%s)\n", 150 | http_errno_description(HTTP_PARSER_ERRNO(&parser)), 151 | http_errno_name(HTTP_PARSER_ERRNO(&parser))); 152 | goto fail; 153 | } 154 | 155 | return EXIT_SUCCESS; 156 | 157 | fail: 158 | fclose(file); 159 | return EXIT_FAILURE; 160 | } 161 | -------------------------------------------------------------------------------- /third-party/http-parser/contrib/url_parser.c: -------------------------------------------------------------------------------- 1 | #include "http_parser.h" 2 | #include 3 | #include 4 | 5 | void 6 | dump_url (const char *url, const struct http_parser_url *u) 7 | { 8 | unsigned int i; 9 | 10 | printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port); 11 | for (i = 0; i < UF_MAX; i++) { 12 | if ((u->field_set & (1 << i)) == 0) { 13 | printf("\tfield_data[%u]: unset\n", i); 14 | continue; 15 | } 16 | 17 | printf("\tfield_data[%u]: off: %u, len: %u, part: %.*s\n", 18 | i, 19 | u->field_data[i].off, 20 | u->field_data[i].len, 21 | u->field_data[i].len, 22 | url + u->field_data[i].off); 23 | } 24 | } 25 | 26 | int main(int argc, char ** argv) { 27 | struct http_parser_url u; 28 | int len, connect, result; 29 | 30 | if (argc != 3) { 31 | printf("Syntax : %s connect|get url\n", argv[0]); 32 | return 1; 33 | } 34 | len = strlen(argv[2]); 35 | connect = strcmp("connect", argv[1]) == 0 ? 1 : 0; 36 | printf("Parsing %s, connect %d\n", argv[2], connect); 37 | 38 | http_parser_url_init(&u); 39 | result = http_parser_parse_url(argv[2], len, connect, &u); 40 | if (result != 0) { 41 | printf("Parse error : %d\n", result); 42 | return result; 43 | } 44 | printf("Parse ok, result : \n"); 45 | dump_url(argv[2], &u); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /third-party/http-parser/http_parser.gyp: -------------------------------------------------------------------------------- 1 | # This file is used with the GYP meta build system. 2 | # http://code.google.com/p/gyp/ 3 | # To build try this: 4 | # svn co http://gyp.googlecode.com/svn/trunk gyp 5 | # ./gyp/gyp -f make --depth=`pwd` http_parser.gyp 6 | # ./out/Debug/test 7 | { 8 | 'target_defaults': { 9 | 'default_configuration': 'Debug', 10 | 'configurations': { 11 | # TODO: hoist these out and put them somewhere common, because 12 | # RuntimeLibrary MUST MATCH across the entire project 13 | 'Debug': { 14 | 'defines': [ 'DEBUG', '_DEBUG' ], 15 | 'cflags': [ '-Wall', '-Wextra', '-O0', '-g', '-ftrapv' ], 16 | 'msvs_settings': { 17 | 'VCCLCompilerTool': { 18 | 'RuntimeLibrary': 1, # static debug 19 | }, 20 | }, 21 | }, 22 | 'Release': { 23 | 'defines': [ 'NDEBUG' ], 24 | 'cflags': [ '-Wall', '-Wextra', '-O3' ], 25 | 'msvs_settings': { 26 | 'VCCLCompilerTool': { 27 | 'RuntimeLibrary': 0, # static release 28 | }, 29 | }, 30 | } 31 | }, 32 | 'msvs_settings': { 33 | 'VCCLCompilerTool': { 34 | }, 35 | 'VCLibrarianTool': { 36 | }, 37 | 'VCLinkerTool': { 38 | 'GenerateDebugInformation': 'true', 39 | }, 40 | }, 41 | 'conditions': [ 42 | ['OS == "win"', { 43 | 'defines': [ 44 | 'WIN32' 45 | ], 46 | }] 47 | ], 48 | }, 49 | 50 | 'targets': [ 51 | { 52 | 'target_name': 'http_parser', 53 | 'type': 'static_library', 54 | 'include_dirs': [ '.' ], 55 | 'direct_dependent_settings': { 56 | 'defines': [ 'HTTP_PARSER_STRICT=0' ], 57 | 'include_dirs': [ '.' ], 58 | }, 59 | 'defines': [ 'HTTP_PARSER_STRICT=0' ], 60 | 'sources': [ './http_parser.c', ], 61 | 'conditions': [ 62 | ['OS=="win"', { 63 | 'msvs_settings': { 64 | 'VCCLCompilerTool': { 65 | # Compile as C++. http_parser.c is actually C99, but C++ is 66 | # close enough in this case. 67 | 'CompileAs': 2, 68 | }, 69 | }, 70 | }] 71 | ], 72 | }, 73 | 74 | { 75 | 'target_name': 'http_parser_strict', 76 | 'type': 'static_library', 77 | 'include_dirs': [ '.' ], 78 | 'direct_dependent_settings': { 79 | 'defines': [ 'HTTP_PARSER_STRICT=1' ], 80 | 'include_dirs': [ '.' ], 81 | }, 82 | 'defines': [ 'HTTP_PARSER_STRICT=1' ], 83 | 'sources': [ './http_parser.c', ], 84 | 'conditions': [ 85 | ['OS=="win"', { 86 | 'msvs_settings': { 87 | 'VCCLCompilerTool': { 88 | # Compile as C++. http_parser.c is actually C99, but C++ is 89 | # close enough in this case. 90 | 'CompileAs': 2, 91 | }, 92 | }, 93 | }] 94 | ], 95 | }, 96 | 97 | { 98 | 'target_name': 'test-nonstrict', 99 | 'type': 'executable', 100 | 'dependencies': [ 'http_parser' ], 101 | 'sources': [ 'test.c' ] 102 | }, 103 | 104 | { 105 | 'target_name': 'test-strict', 106 | 'type': 'executable', 107 | 'dependencies': [ 'http_parser_strict' ], 108 | 'sources': [ 'test.c' ] 109 | } 110 | ] 111 | } 112 | --------------------------------------------------------------------------------