From 0f061872b4d2c1605dcc0f1273a2cecbb0f85024 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 22 Apr 2025 17:57:53 +0200 Subject: [PATCH 1/2] Adding upstream version 1.0.2. Signed-off-by: Daniel Baumann --- .clang-format | 6 + .copr/Makefile | 23 ++ .github/FUNDING.yml | 1 + .gitignore | 93 +++++ .gitmodules | 3 + .lgtm.yml | 23 ++ .travis.yml | 26 ++ CHANGES | 42 +++ LICENSE | 674 ++++++++++++++++++++++++++++++++++++ Makefile.am | 32 ++ README.md | 158 +++++++++ autogen.sh | 21 ++ config.rpath | 1 + configure.ac | 91 +++++ fmt.sh | 7 + m4/ax_append_flag.m4 | 50 +++ m4/ax_cflags_warn_all.m4 | 158 +++++++++ m4/ax_compiler_vendor.m4 | 117 +++++++ m4/ax_prepend_flag.m4 | 51 +++ m4/ax_pthread.m4 | 486 ++++++++++++++++++++++++++ m4/ax_require_defined.m4 | 37 ++ m4/dl.sh | 27 ++ rpm/dnsmeter.spec | 115 ++++++ src/Makefile.am | 472 +++++++++++++++++++++++++ src/dns_receiver_thread.cpp | 90 +++++ src/dns_receiver_thread.h | 52 +++ src/dns_sender.cpp | 571 ++++++++++++++++++++++++++++++ src/dns_sender.h | 112 ++++++ src/dns_sender_thread.cpp | 332 ++++++++++++++++++ src/dns_sender_thread.h | 97 ++++++ src/dnsmeter.1.in | 276 +++++++++++++++ src/exceptions.h | 39 +++ src/main.cpp | 40 +++ src/packet.cpp | 220 ++++++++++++ src/packet.h | 62 ++++ src/payload_file.cpp | 172 +++++++++ src/payload_file.h | 46 +++ src/query.cpp | 118 +++++++ src/query.h | 53 +++ src/raw_socket_receiver.cpp | 402 +++++++++++++++++++++ src/raw_socket_receiver.h | 65 ++++ src/raw_socket_sender.cpp | 94 +++++ src/raw_socket_sender.h | 49 +++ src/system_stat.cpp | 362 +++++++++++++++++++ src/system_stat.h | 112 ++++++ src/test/Makefile.am | 7 + src/test/test1.sh | 3 + 47 files changed, 6088 insertions(+) create mode 100644 .clang-format create mode 100644 .copr/Makefile create mode 100644 .github/FUNDING.yml create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .lgtm.yml create mode 100644 .travis.yml create mode 100644 CHANGES create mode 100644 LICENSE create mode 100644 Makefile.am create mode 100644 README.md create mode 100755 autogen.sh create mode 120000 config.rpath create mode 100644 configure.ac create mode 100755 fmt.sh create mode 100644 m4/ax_append_flag.m4 create mode 100644 m4/ax_cflags_warn_all.m4 create mode 100644 m4/ax_compiler_vendor.m4 create mode 100644 m4/ax_prepend_flag.m4 create mode 100644 m4/ax_pthread.m4 create mode 100644 m4/ax_require_defined.m4 create mode 100755 m4/dl.sh create mode 100644 rpm/dnsmeter.spec create mode 100644 src/Makefile.am create mode 100644 src/dns_receiver_thread.cpp create mode 100644 src/dns_receiver_thread.h create mode 100644 src/dns_sender.cpp create mode 100644 src/dns_sender.h create mode 100644 src/dns_sender_thread.cpp create mode 100644 src/dns_sender_thread.h create mode 100644 src/dnsmeter.1.in create mode 100644 src/exceptions.h create mode 100644 src/main.cpp create mode 100644 src/packet.cpp create mode 100644 src/packet.h create mode 100644 src/payload_file.cpp create mode 100644 src/payload_file.h create mode 100644 src/query.cpp create mode 100644 src/query.h create mode 100644 src/raw_socket_receiver.cpp create mode 100644 src/raw_socket_receiver.h create mode 100644 src/raw_socket_sender.cpp create mode 100644 src/raw_socket_sender.h create mode 100644 src/system_stat.cpp create mode 100644 src/system_stat.h create mode 100644 src/test/Makefile.am create mode 100755 src/test/test1.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1bd4430 --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +BasedOnStyle: webkit +IndentWidth: 4 +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: true +AlignOperands: true +SortIncludes: false diff --git a/.copr/Makefile b/.copr/Makefile new file mode 100644 index 0000000..29ed0bc --- /dev/null +++ b/.copr/Makefile @@ -0,0 +1,23 @@ +top=.. + +all: srpm + +prereq: $(top)/rpmbuild + rpm -q git rpm-build >/dev/null || dnf -y install git rpm-build + +update-dist-tools: $(top)/dist-tools + ( cd "$(top)/dist-tools" && git pull ) + +$(top)/dist-tools: + git clone https://github.com/jelu/dist-tools.git "$(top)/dist-tools" + +$(top)/rpmbuild: + mkdir -p "$(top)"/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} + +srpm: prereq update-dist-tools + test -f .gitmodules && git submodule update --init || true + echo "$(spec)" | grep -q "develop.spec" && auto_build_number=`date --utc +%s` message="Auto build `date --utc --iso-8601=seconds`" "$(top)/dist-tools/spec-new-changelog-entry" || true + overwrite=yes nosign=yes "$(top)/dist-tools/create-source-packages" rpm + cp ../*.orig.tar.gz "$(top)/rpmbuild/SOURCES/" + echo "$(spec)" | grep -q "develop.spec" && rpmbuild -bs --define "%_topdir $(top)/rpmbuild" --undefine=dist rpm/*.spec || rpmbuild -bs --define "%_topdir $(top)/rpmbuild" --undefine=dist "$(spec)" + cp "$(top)"/rpmbuild/SRPMS/*.src.rpm "$(outdir)" diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..38cc1c4 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +custom: https://www.dns-oarc.net/donate diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a906e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,93 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# Automake +Makefile.in +aclocal.m4 +ar-lib +autom4te.cache +compile +config.guess +config.sub +configure +depcomp +install-sh +ltmain.sh +m4/libtool.m4 +m4/ltoptions.m4 +m4/ltsugar.m4 +m4/ltversion.m4 +m4/lt~obsolete.m4 +missing +config.h.in +config.h.in~ +test-driver + +# Configure +Makefile +config.log +config.status +libtool +.deps +src/config.h +src/stamp-h1 +build +.dirstamp + +# Project specific files +src/dnsmeter +src/dnsmeter.1 +src/test/test-suite.log +src/test/test*.sh.log +src/test/test*.sh.trs +src/test/*.dist diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..60a913b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src/pplib"] + path = src/pplib + url = https://github.com/DNS-OARC/pplib.git diff --git a/.lgtm.yml b/.lgtm.yml new file mode 100644 index 0000000..e8a57b0 --- /dev/null +++ b/.lgtm.yml @@ -0,0 +1,23 @@ +path_classifiers: + library: + - "src/pplib/*" +extraction: + cpp: + prepare: + packages: + - build-essential + - automake + - autoconf + - libtool + - pkg-config + - libbind-dev + - libssl-dev + - libbz2-dev + - libidn2-dev + - zlib1g-dev + - libpcap-dev + - libpcre3-dev + configure: + command: + - ./autogen.sh + - ./configure diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..502926b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +dist: bionic +addons: + apt: + update: true + packages: + - libbind-dev + - libssl-dev + - libbz2-dev + - libidn2-dev + - zlib1g-dev + - libpcap-dev + - libpcre3-dev +language: cpp +compiler: + - gcc +install: ./autogen.sh +script: + - ./configure --enable-warn-all + - make dist + - tar zxvf *.tar.gz + - cd dnsmeter-*[0-9] + - mkdir build + - cd build + - ../configure --enable-warn-all + - make + - make test diff --git a/CHANGES b/CHANGES new file mode 100644 index 0000000..6dd11e7 --- /dev/null +++ b/CHANGES @@ -0,0 +1,42 @@ +2021-06-02 Jerry Lundström + + Release 1.0.2 + + This release fixes an issue with source port being static when only + using `-q` to generate traffic from one host/IP. The source port is + now randomized for every DNS query. + + Other changes is mainly about build system, packages and fixed issues + detected by code analysis tools. + + 05000cc Typo, random source port + 6a71707 Coverage + 1c724ce SonarCloud + 0776d20 Badges + c274884 LGTM + 5a12c61 COPR + f77efed Build dependency + a359b66 iconv + +2019-10-07 Jerry Lundström + + Release 1.0.1 + + This release fixes a few minor bugs and a dependency issue which made + `dnsmeter` throw an exception when using the `-r` option. + + Bugfixes: + - Use existing `rtt_avg`, was showing total RTT divided by number of threads + - Fix an issue which missed the first 8 bytes of a text payload + + 72197b5 PCAP detect + 665be2d RTT average + 3fe7b66 pplib dependencies, RTT average + e565d42 Funding + 1a1ea40 README + +2019-09-23 Jerry Lundström + + Release 1.0.0 + + Initial release after project was moved from DENIC to DNS-OARC. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..9a00f7e --- /dev/null +++ b/Makefile.am @@ -0,0 +1,32 @@ +# Copyright (c) 2019-2021, OARC, Inc. +# Copyright (c) 2019, DENIC eG +# All rights reserved. +# +# This file is part of dnsmeter. +# +# dnsmeter is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# dnsmeter is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with dnsmeter. If not, see . + +ACLOCAL_AMFLAGS = -I m4 + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in \ + $(srcdir)/src/config.h.in~ \ + $(srcdir)/configure + +SUBDIRS = src + +dist_doc_DATA = CHANGES README.md LICENSE + +EXTRA_DIST = m4 + +test: check diff --git a/README.md b/README.md new file mode 100644 index 0000000..45c5eae --- /dev/null +++ b/README.md @@ -0,0 +1,158 @@ +# dnsmeter + +[![Build Status](https://travis-ci.com/DNS-OARC/dnsmeter.svg?branch=develop)](https://travis-ci.com/DNS-OARC/dnsmeter) [![Total alerts](https://img.shields.io/lgtm/alerts/g/DNS-OARC/dnsmeter.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/DNS-OARC/dnsmeter/alerts/) [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=dns-oarc%3Adnsmeter&metric=bugs)](https://sonarcloud.io/dashboard?id=dns-oarc%3Adnsmeter) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=dns-oarc%3Adnsmeter&metric=security_rating)](https://sonarcloud.io/dashboard?id=dns-oarc%3Adnsmeter) + +`dnsmeter` is a tool for testing performance of a nameserver and the +infrastructure around it. It generates DNS queries and sends them via UDP +to a target nameserver and counts the answers. + +Features: +- payload can be given as a text file or a PCAP file +- can automatically run different load steps, which can be given as a list or ranges +- results per load step can be stored in a CSV file +- sender addresses can be spoofed from a given network or from the addresses found in the PCAP file +- answers are counted, even if source address is spoofed, if answers get routed back to the load generator +- round-trip-times are measured (average, min, mix) +- the amount of DNSSEC queries can be given as percentage of total traffic +- optimized for high amount of packets, on an Intel(R) Xeon(R) CPU E5-2430 v2 @ 2.50GHz it can generate more than 900.000 packets per second +- runs on Linux and FreeBSD + +## Dependencies + +`dnsmeter` requires a couple of libraries beside a normal C++ compiling +environment with autoconf, automake and libtool. + +`dnsmeter` has a non-optional dependency on the PCRE library, `libresolv` +and PCAP library. + +`dnsmeter` also includes [pplib](https://github.com/DNS-OARC/pplib), +collection of C++ functions and classes, and it has non-optional dependency +on OpenSSL, bzip2, IDN2 (or IDN1) library and zlib. + +To install the dependencies under Debian/Ubuntu: +``` +apt-get install -y libssl-dev libbz2-dev libidn2-dev zlib1g-dev libpcap-dev libpcre3-dev gettext +``` + +NOTE: If your system does not have `libidn2-dev`, please use `libidn11-dev` instead. + +To install the dependencies under CentOS (with EPEL enabled): +``` +yum install -y openssl-devel bzip2-devel libidn2-devel zlib-devel libpcap-devel pcre-devel gettext-devel +``` + +NOTE: If your using openSUSE/SLE then bzip2's package is `libbz2-devel`. + +To install the dependencies under FreeBSD 10+ using `pkg`: +``` +pkg install -y openssl libidn2 libpcap pcre gettext +``` + +## Building from source tarball + +The [source tarball from DNS-OARC](https://www.dns-oarc.net/tools/dnsmeter) +comes prepared with `configure`: + +``` +tar zxvf dnsmeter-version.tar.gz +cd dnsmeter-version +./configure [options] +make +make install +``` + +NOTE: If building fails on FreeBSD, try adding these configure +options: `--with-extra-cflags="-I /usr/local/include" --with-extra-ldflags="-L/usr/local/lib"`. + +## Building from Git repository + +If you are building `dnsmeter` from it's Git repository you will first need +to initiate the Git submodules that exists and later create autoconf/automake +files, this will require a build environment with autoconf, automake and +libtool to be installed. + +``` +git clone https://github.com/DNS-OARC/dnsmeter.git +cd dnsmeter +git submodule update --init +./autogen.sh +./configure [options] +make +make install +``` + +## Usage + +Once installed please see `man dnsmeter` for usage. + +## Example + +Lets assume the following scenario: + +- load generator runs on FreeBSD +- network interface an which the traffic goes out and comes back is `igb0` +- source IP on the load generator is 192.168.155.20 +- target nameserver has IP 192.168.0.1, port 53 +- we want to spoof the sender address from the network 10.0.0.0/8 +- the payload file is found here: `/home/testdata/payload.txt` +- the nameserver is running on CentOS and we need to set a route back to the load generator: `ip route add 10.0.0.0/8 via 192.168.155.20` +- we want to test the following load steps: 30000,40000,45000,50000,100000,150000 +- results should be written to `results.csv` +- DNSSEC rate should be 70% + +This makes the following command: + +``` +dnsmeter -p /home/testdata/payload.txt \ + -r 30000,40000,45000,50000,100000,150000 \ + -s 10.0.0.0/8 \ + -z 192.168.0.1:53 \ + -e igb0 \ + -d 70 \ + -c results.csv +``` + +In the second example, we want to use a PCAP file as payload and want +to spoof with the addresses from that file: + +``` +dnsmeter -p /home/testdata/pcap.file1 \ + -r 30000,40000,45000,50000,100000,150000 \ + -s pcap \ + -z 192.168.0.1:53 \ + -e igb0 \ + -c results_pcap.csv +``` + +## Author(s) + +- Patrick Fedick [@pfedick](https://github.com/pfedick) + +## Contributor(s) + +- Jerry Lundström [@jelu](https://github.com/jelu) + +## Copyright + +Copyright (c) 2019-2021, OARC, Inc. + +Copyright (c) 2019, DENIC eG + +All rights reserved. + +``` +This file is part of dnsmeter. + +dnsmeter is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +dnsmeter is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with dnsmeter. If not, see . +``` diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..21da32a --- /dev/null +++ b/autogen.sh @@ -0,0 +1,21 @@ +#!/bin/sh -e +# Copyright (c) 2019-2021, OARC, Inc. +# Copyright (c) 2019, DENIC eG +# All rights reserved. +# +# This file is part of dnsmeter. +# +# dnsmeter is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# dnsmeter is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with dnsmeter. If not, see . + +autoreconf --force --install --no-recursive --include=m4 diff --git a/config.rpath b/config.rpath new file mode 120000 index 0000000..503953f --- /dev/null +++ b/config.rpath @@ -0,0 +1 @@ +src/pplib/autoconf/config.rpath \ No newline at end of file diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..97a5e23 --- /dev/null +++ b/configure.ac @@ -0,0 +1,91 @@ +# Copyright (c) 2019-2021, OARC, Inc. +# Copyright (c) 2019, DENIC eG +# All rights reserved. +# +# This file is part of dnsmeter. +# +# dnsmeter is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# dnsmeter is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with dnsmeter. If not, see . + +AC_PREREQ(2.69) +AC_INIT([dnsmeter], [1.0.2], [admin@dns-oarc.net], [dnsmeter], [https://github.com/DNS-OARC/dnsmeter/issues]) +AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects]) +AC_CONFIG_SRCDIR([src/main.cpp]) +AC_CONFIG_HEADER([src/config.h]) +AC_CONFIG_MACRO_DIR([m4]) + +# Checks for programs. +AC_PROG_CXX +AC_CANONICAL_HOST +LT_INIT([disable-static]) + +# Check --enable-warn-all +AC_ARG_ENABLE([warn-all], [AS_HELP_STRING([--enable-warn-all], [Enable all compiler warnings])], [AX_CFLAGS_WARN_ALL()]) + +# Check --with-extra-cflags +AC_ARG_WITH([extra-cflags], [AS_HELP_STRING([--with-extra-cflags=CFLAGS], [Add extra CFLAGS/CXXFLAGS])], [ + AC_MSG_NOTICE([appending extra CFLAGS/CXXFLAGS... $withval]) + AS_VAR_APPEND(CFLAGS, [" $withval"]) + AS_VAR_APPEND(CXXFLAGS, [" $withval"]) +]) + +# Check --with-extra-ldflags +AC_ARG_WITH([extra-ldflags], [AS_HELP_STRING([--with-extra-ldflags=LDFLAGS], [Add extra LDFLAGS])], [ + AC_MSG_NOTICE([appending extra LDFLAGS... $withval]) + AS_VAR_APPEND(LDFLAGS, [" $withval"]) +]) + +# Check --enable-gcov +AC_ARG_ENABLE([gcov], [AS_HELP_STRING([--enable-gcov], [Enable coverage testing])], [ + coverage_cxxflags="--coverage -g -O0 -fno-inline -fno-inline-small-functions -fno-default-inline" + AC_MSG_NOTICE([enabling coverage testing... $coverage_cxxflags]) + AS_VAR_APPEND(CXXFLAGS, [" $coverage_cxxflags"]) +]) +AM_CONDITIONAL([ENABLE_GCOV], [test "x$enable_gcov" != "xno"]) +AM_EXTRA_RECURSIVE_TARGETS([gcov]) + +# Checks for support. +AX_PTHREAD +AC_CHECK_LIB([pcap], [pcap_open_live], [], [AC_MSG_ERROR([libpcap not found])]) +AC_CHECK_LIB([m], [sqrt]) +AC_CHECK_LIB([bind], [ns_initparse], [], [AC_CHECK_LIB([bind], [__ns_initparse])]) +AC_CHECK_LIB([resolv], [res_mkquery], [], [ + AC_CHECK_LIB([resolv], [__res_mkquery], [], [ + AC_CHECK_LIB([resolv], [res_9_mkquery]) + ]) +]) +AC_CHECK_LIB([idn], [idna_to_ascii_4z]) +AC_CHECK_LIB([idn2], [idn2_to_ascii_4z]) +AC_CHECK_LIB([pcre], [pcre_exec]) + +sinclude(src/pplib/autoconf/iconv.m4) + +# Check for OS specific libraries +case "$host_os" in + freebsd*) + AC_CHECK_LIB([kvm], [kvm_open]) + AM_ICONV + ICONV_CFLAGS="$INCICONV" + ICONV_LIBS="$LIBICONV" + AC_SUBST(ICONV_CFLAGS) + AC_SUBST(ICONV_LIBS) + ;; +esac + +# Output Makefiles +AC_CONFIG_FILES([ + Makefile + src/Makefile + src/test/Makefile +]) +AC_OUTPUT diff --git a/fmt.sh b/fmt.sh new file mode 100755 index 0000000..86e9053 --- /dev/null +++ b/fmt.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +clang-format-4.0 \ + -style=file \ + -i \ + src/*.cpp \ + src/*.h diff --git a/m4/ax_append_flag.m4 b/m4/ax_append_flag.m4 new file mode 100644 index 0000000..dd6d8b6 --- /dev/null +++ b/m4/ax_append_flag.m4 @@ -0,0 +1,50 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_append_flag.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_APPEND_FLAG(FLAG, [FLAGS-VARIABLE]) +# +# DESCRIPTION +# +# FLAG is appended to the FLAGS-VARIABLE shell variable, with a space +# added in between. +# +# If FLAGS-VARIABLE is not specified, the current language's flags (e.g. +# CFLAGS) is used. FLAGS-VARIABLE is not changed if it already contains +# FLAG. If FLAGS-VARIABLE is unset in the shell, it is set to exactly +# FLAG. +# +# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. +# +# LICENSE +# +# Copyright (c) 2008 Guido U. Draheim +# Copyright (c) 2011 Maarten Bosmans +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 8 + +AC_DEFUN([AX_APPEND_FLAG], +[dnl +AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_SET_IF +AS_VAR_PUSHDEF([FLAGS], [m4_default($2,_AC_LANG_PREFIX[FLAGS])]) +AS_VAR_SET_IF(FLAGS,[ + AS_CASE([" AS_VAR_GET(FLAGS) "], + [*" $1 "*], [AC_RUN_LOG([: FLAGS already contains $1])], + [ + AS_VAR_APPEND(FLAGS,[" $1"]) + AC_RUN_LOG([: FLAGS="$FLAGS"]) + ]) + ], + [ + AS_VAR_SET(FLAGS,[$1]) + AC_RUN_LOG([: FLAGS="$FLAGS"]) + ]) +AS_VAR_POPDEF([FLAGS])dnl +])dnl AX_APPEND_FLAG diff --git a/m4/ax_cflags_warn_all.m4 b/m4/ax_cflags_warn_all.m4 new file mode 100644 index 0000000..9235a18 --- /dev/null +++ b/m4/ax_cflags_warn_all.m4 @@ -0,0 +1,158 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_cflags_warn_all.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_CFLAGS_WARN_ALL [(shellvar[, default[, action-if-found[, action-if-not-found]]])] +# AX_CXXFLAGS_WARN_ALL [(shellvar[, default[, action-if-found[, action-if-not-found]]])] +# AX_FCFLAGS_WARN_ALL [(shellvar[, default[, action-if-found[, action-if-not-found]]])] +# +# DESCRIPTION +# +# Specify compiler options that enable most reasonable warnings. For the +# GNU Compiler Collection (GCC), for example, it will be "-Wall". The +# result is added to shellvar, one of CFLAGS, CXXFLAGS or FCFLAGS if the +# first parameter is not specified. +# +# Each of these macros accepts the following optional arguments: +# +# - $1 - shellvar +# shell variable to use (CFLAGS, CXXFLAGS or FCFLAGS if not +# specified, depending on macro) +# +# - $2 - default +# value to use for flags if compiler vendor cannot be determined (by +# default, "") +# +# - $3 - action-if-found +# action to take if the compiler vendor has been successfully +# determined (by default, add the appropriate compiler flags to +# shellvar) +# +# - $4 - action-if-not-found +# action to take if the compiler vendor has not been determined or +# is unknown (by default, add the default flags, or "" if not +# specified, to shellvar) +# +# These macros use AX_COMPILER_VENDOR to determine which flags should be +# returned for a given compiler. Not all compilers currently have flags +# defined for them; patches are welcome. If need be, compiler flags may +# be made language-dependent: use a construct like the following: +# +# [vendor_name], [m4_if(_AC_LANG_PREFIX,[C], VAR="--relevant-c-flags",dnl +# m4_if(_AC_LANG_PREFIX,[CXX], VAR="--relevant-c++-flags",dnl +# m4_if(_AC_LANG_PREFIX,[FC], VAR="--relevant-fortran-flags",dnl +# VAR="$2"; FOUND="no")))], +# +# Note: These macros also depend on AX_PREPEND_FLAG. +# +# LICENSE +# +# Copyright (c) 2008 Guido U. Draheim +# Copyright (c) 2010 Rhys Ulerich +# Copyright (c) 2018 John Zaitseff +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 3 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 25 + +AC_DEFUN([AX_FLAGS_WARN_ALL], [ + AX_REQUIRE_DEFINED([AX_PREPEND_FLAG])dnl + AC_REQUIRE([AX_COMPILER_VENDOR])dnl + + AS_VAR_PUSHDEF([FLAGS], [m4_default($1,_AC_LANG_PREFIX[]FLAGS)])dnl + AS_VAR_PUSHDEF([VAR], [ac_cv_[]_AC_LANG_ABBREV[]flags_warn_all])dnl + AS_VAR_PUSHDEF([FOUND], [ac_save_[]_AC_LANG_ABBREV[]flags_warn_all_found])dnl + + AC_CACHE_CHECK([FLAGS for most reasonable warnings], VAR, [ + VAR="" + FOUND="yes" + dnl Cases are listed in the order found in ax_compiler_vendor.m4 + AS_CASE("$ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor", + [intel], [VAR="-w2"], + [ibm], [VAR="-qsrcmsg -qinfo=all:noppt:noppc:noobs:nocnd"], + [pathscale], [], + [clang], [VAR="-Wall"], + [cray], [VAR="-h msglevel 2"], + [fujitsu], [], + [sdcc], [], + [sx], [VAR="-pvctl[,]fullmsg"], + [portland], [], + [gnu], [VAR="-Wall"], + [sun], [VAR="-v"], + [hp], [VAR="+w1"], + [dec], [VAR="-verbose -w0 -warnprotos"], + [borland], [], + [comeau], [], + [kai], [], + [lcc], [], + [sgi], [VAR="-fullwarn"], + [microsoft], [], + [metrowerks], [], + [watcom], [], + [tcc], [], + [unknown], [ + VAR="$2" + FOUND="no" + ], + [ + AC_MSG_WARN([Unknown compiler vendor returned by [AX_COMPILER_VENDOR]]) + VAR="$2" + FOUND="no" + ] + ) + + AS_IF([test "x$FOUND" = "xyes"], [dnl + m4_default($3, [AS_IF([test "x$VAR" != "x"], [AX_PREPEND_FLAG([$VAR], [FLAGS])])]) + ], [dnl + m4_default($4, [m4_ifval($2, [AX_PREPEND_FLAG([$VAR], [FLAGS])], [true])]) + ])dnl + ])dnl + + AS_VAR_POPDEF([FOUND])dnl + AS_VAR_POPDEF([VAR])dnl + AS_VAR_POPDEF([FLAGS])dnl +])dnl AX_FLAGS_WARN_ALL + +AC_DEFUN([AX_CFLAGS_WARN_ALL], [dnl + AC_LANG_PUSH([C]) + AX_FLAGS_WARN_ALL([$1], [$2], [$3], [$4]) + AC_LANG_POP([C]) +])dnl + +AC_DEFUN([AX_CXXFLAGS_WARN_ALL], [dnl + AC_LANG_PUSH([C++]) + AX_FLAGS_WARN_ALL([$1], [$2], [$3], [$4]) + AC_LANG_POP([C++]) +])dnl + +AC_DEFUN([AX_FCFLAGS_WARN_ALL], [dnl + AC_LANG_PUSH([Fortran]) + AX_FLAGS_WARN_ALL([$1], [$2], [$3], [$4]) + AC_LANG_POP([Fortran]) +])dnl diff --git a/m4/ax_compiler_vendor.m4 b/m4/ax_compiler_vendor.m4 new file mode 100644 index 0000000..f06e865 --- /dev/null +++ b/m4/ax_compiler_vendor.m4 @@ -0,0 +1,117 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_compiler_vendor.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_COMPILER_VENDOR +# +# DESCRIPTION +# +# Determine the vendor of the C, C++ or Fortran compiler. The vendor is +# returned in the cache variable $ax_cv_c_compiler_vendor for C, +# $ax_cv_cxx_compiler_vendor for C++ or $ax_cv_fc_compiler_vendor for +# (modern) Fortran. The value is one of "intel", "ibm", "pathscale", +# "clang" (LLVM), "cray", "fujitsu", "sdcc", "sx", "portland" (PGI), "gnu" +# (GCC), "sun" (Oracle Developer Studio), "hp", "dec", "borland", +# "comeau", "kai", "lcc", "sgi", "microsoft", "metrowerks", "watcom", +# "tcc" (Tiny CC) or "unknown" (if the compiler cannot be determined). +# +# To check for a Fortran compiler, you must first call AC_FC_PP_SRCEXT +# with an appropriate preprocessor-enabled extension. For example: +# +# AC_LANG_PUSH([Fortran]) +# AC_PROG_FC +# AC_FC_PP_SRCEXT([F]) +# AX_COMPILER_VENDOR +# AC_LANG_POP([Fortran]) +# +# LICENSE +# +# Copyright (c) 2008 Steven G. Johnson +# Copyright (c) 2008 Matteo Frigo +# Copyright (c) 2018-19 John Zaitseff +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, either version 3 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 30 + +AC_DEFUN([AX_COMPILER_VENDOR], [dnl + AC_CACHE_CHECK([for _AC_LANG compiler vendor], ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor, [dnl + dnl If you modify this list of vendors, please add similar support + dnl to ax_compiler_version.m4 if at all possible. + dnl + dnl Note: Do NOT check for GCC first since some other compilers + dnl define __GNUC__ to remain compatible with it. Compilers that + dnl are very slow to start (such as Intel) are listed first. + + vendors=" + intel: __ICC,__ECC,__INTEL_COMPILER + ibm: __xlc__,__xlC__,__IBMC__,__IBMCPP__,__ibmxl__ + pathscale: __PATHCC__,__PATHSCALE__ + clang: __clang__ + cray: _CRAYC + fujitsu: __FUJITSU + sdcc: SDCC,__SDCC + sx: _SX + portland: __PGI + gnu: __GNUC__ + sun: __SUNPRO_C,__SUNPRO_CC,__SUNPRO_F90,__SUNPRO_F95 + hp: __HP_cc,__HP_aCC + dec: __DECC,__DECCXX,__DECC_VER,__DECCXX_VER + borland: __BORLANDC__,__CODEGEARC__,__TURBOC__ + comeau: __COMO__ + kai: __KCC + lcc: __LCC__ + sgi: __sgi,sgi + microsoft: _MSC_VER + metrowerks: __MWERKS__ + watcom: __WATCOMC__ + tcc: __TINYC__ + unknown: UNKNOWN + " + for ventest in $vendors; do + case $ventest in + *:) + vendor=$ventest + continue + ;; + *) + vencpp="defined("`echo $ventest | sed 's/,/) || defined(/g'`")" + ;; + esac + + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [[ +#if !($vencpp) + thisisanerror; +#endif + ]])], [break]) + done + + ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor=`echo $vendor | cut -d: -f1` + ]) +])dnl diff --git a/m4/ax_prepend_flag.m4 b/m4/ax_prepend_flag.m4 new file mode 100644 index 0000000..adac8c5 --- /dev/null +++ b/m4/ax_prepend_flag.m4 @@ -0,0 +1,51 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_prepend_flag.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_PREPEND_FLAG(FLAG, [FLAGS-VARIABLE]) +# +# DESCRIPTION +# +# FLAG is added to the front of the FLAGS-VARIABLE shell variable, with a +# space added in between. +# +# If FLAGS-VARIABLE is not specified, the current language's flags (e.g. +# CFLAGS) is used. FLAGS-VARIABLE is not changed if it already contains +# FLAG. If FLAGS-VARIABLE is unset in the shell, it is set to exactly +# FLAG. +# +# NOTE: Implementation based on AX_APPEND_FLAG. +# +# LICENSE +# +# Copyright (c) 2008 Guido U. Draheim +# Copyright (c) 2011 Maarten Bosmans +# Copyright (c) 2018 John Zaitseff +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 2 + +AC_DEFUN([AX_PREPEND_FLAG], +[dnl +AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_SET_IF +AS_VAR_PUSHDEF([FLAGS], [m4_default($2,_AC_LANG_PREFIX[FLAGS])]) +AS_VAR_SET_IF(FLAGS,[ + AS_CASE([" AS_VAR_GET(FLAGS) "], + [*" $1 "*], [AC_RUN_LOG([: FLAGS already contains $1])], + [ + FLAGS="$1 $FLAGS" + AC_RUN_LOG([: FLAGS="$FLAGS"]) + ]) + ], + [ + AS_VAR_SET(FLAGS,[$1]) + AC_RUN_LOG([: FLAGS="$FLAGS"]) + ]) +AS_VAR_POPDEF([FLAGS])dnl +])dnl AX_PREPEND_FLAG diff --git a/m4/ax_pthread.m4 b/m4/ax_pthread.m4 new file mode 100644 index 0000000..4920e07 --- /dev/null +++ b/m4/ax_pthread.m4 @@ -0,0 +1,486 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_pthread.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) +# +# DESCRIPTION +# +# This macro figures out how to build C programs using POSIX threads. It +# sets the PTHREAD_LIBS output variable to the threads library and linker +# flags, and the PTHREAD_CFLAGS output variable to any special C compiler +# flags that are needed. (The user can also force certain compiler +# flags/libs to be tested by setting these environment variables.) +# +# Also sets PTHREAD_CC to any special C compiler that is needed for +# multi-threaded programs (defaults to the value of CC otherwise). (This +# is necessary on AIX to use the special cc_r compiler alias.) +# +# NOTE: You are assumed to not only compile your program with these flags, +# but also to link with them as well. For example, you might link with +# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS +# +# If you are only building threaded programs, you may wish to use these +# variables in your default LIBS, CFLAGS, and CC: +# +# LIBS="$PTHREAD_LIBS $LIBS" +# CFLAGS="$CFLAGS $PTHREAD_CFLAGS" +# CC="$PTHREAD_CC" +# +# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant +# has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to +# that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). +# +# Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the +# PTHREAD_PRIO_INHERIT symbol is defined when compiling with +# PTHREAD_CFLAGS. +# +# ACTION-IF-FOUND is a list of shell commands to run if a threads library +# is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it +# is not found. If ACTION-IF-FOUND is not specified, the default action +# will define HAVE_PTHREAD. +# +# Please let the authors know if this macro fails on any platform, or if +# you have any other suggestions or comments. This macro was based on work +# by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help +# from M. Frigo), as well as ac_pthread and hb_pthread macros posted by +# Alejandro Forero Cuervo to the autoconf macro repository. We are also +# grateful for the helpful feedback of numerous users. +# +# Updated for Autoconf 2.68 by Daniel Richard G. +# +# LICENSE +# +# Copyright (c) 2008 Steven G. Johnson +# Copyright (c) 2011 Daniel Richard G. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, either version 3 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 25 + +AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) +AC_DEFUN([AX_PTHREAD], [ +AC_REQUIRE([AC_CANONICAL_HOST]) +AC_REQUIRE([AC_PROG_CC]) +AC_REQUIRE([AC_PROG_SED]) +AC_LANG_PUSH([C]) +ax_pthread_ok=no + +# We used to check for pthread.h first, but this fails if pthread.h +# requires special compiler flags (e.g. on Tru64 or Sequent). +# It gets checked for in the link test anyway. + +# First of all, check if the user has set any of the PTHREAD_LIBS, +# etcetera environment variables, and if threads linking works using +# them: +if test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then + ax_pthread_save_CC="$CC" + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"]) + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS]) + AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes]) + AC_MSG_RESULT([$ax_pthread_ok]) + if test "x$ax_pthread_ok" = "xno"; then + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" + fi + CC="$ax_pthread_save_CC" + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" +fi + +# We must check for the threads library under a number of different +# names; the ordering is very important because some systems +# (e.g. DEC) have both -lpthread and -lpthreads, where one of the +# libraries is broken (non-POSIX). + +# Create a list of thread flags to try. Items starting with a "-" are +# C compiler flags, and other items are library names, except for "none" +# which indicates that we try without any flags at all, and "pthread-config" +# which is a program returning the flags for the Pth emulation library. + +ax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" + +# The ordering *is* (sometimes) important. Some notes on the +# individual items follow: + +# pthreads: AIX (must check this before -lpthread) +# none: in case threads are in libc; should be tried before -Kthread and +# other compiler flags to prevent continual compiler warnings +# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) +# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64 +# (Note: HP C rejects this with "bad form for `-t' option") +# -pthreads: Solaris/gcc (Note: HP C also rejects) +# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it +# doesn't hurt to check since this sometimes defines pthreads and +# -D_REENTRANT too), HP C (must be checked before -lpthread, which +# is present but should not be used directly; and before -mthreads, +# because the compiler interprets this as "-mt" + "-hreads") +# -mthreads: Mingw32/gcc, Lynx/gcc +# pthread: Linux, etcetera +# --thread-safe: KAI C++ +# pthread-config: use pthread-config program (for GNU Pth library) + +case $host_os in + + freebsd*) + + # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) + # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) + + ax_pthread_flags="-kthread lthread $ax_pthread_flags" + ;; + + hpux*) + + # From the cc(1) man page: "[-mt] Sets various -D flags to enable + # multi-threading and also sets -lpthread." + + ax_pthread_flags="-mt -pthread pthread $ax_pthread_flags" + ;; + + openedition*) + + # IBM z/OS requires a feature-test macro to be defined in order to + # enable POSIX threads at all, so give the user a hint if this is + # not set. (We don't define these ourselves, as they can affect + # other portions of the system API in unpredictable ways.) + + AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING], + [ +# if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS) + AX_PTHREAD_ZOS_MISSING +# endif + ], + [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])]) + ;; + + solaris*) + + # On Solaris (at least, for some versions), libc contains stubbed + # (non-functional) versions of the pthreads routines, so link-based + # tests will erroneously succeed. (N.B.: The stubs are missing + # pthread_cleanup_push, or rather a function called by this macro, + # so we could check for that, but who knows whether they'll stub + # that too in a future libc.) So we'll check first for the + # standard Solaris way of linking pthreads (-mt -lpthread). + + ax_pthread_flags="-mt,pthread pthread $ax_pthread_flags" + ;; +esac + +# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC) + +AS_IF([test "x$GCC" = "xyes"], + [ax_pthread_flags="-pthread -pthreads $ax_pthread_flags"]) + +# The presence of a feature test macro requesting re-entrant function +# definitions is, on some systems, a strong hint that pthreads support is +# correctly enabled + +case $host_os in + darwin* | hpux* | linux* | osf* | solaris*) + ax_pthread_check_macro="_REENTRANT" + ;; + + aix*) + ax_pthread_check_macro="_THREAD_SAFE" + ;; + + *) + ax_pthread_check_macro="--" + ;; +esac +AS_IF([test "x$ax_pthread_check_macro" = "x--"], + [ax_pthread_check_cond=0], + [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"]) + +# Are we compiling with Clang? + +AC_CACHE_CHECK([whether $CC is Clang], + [ax_cv_PTHREAD_CLANG], + [ax_cv_PTHREAD_CLANG=no + # Note that Autoconf sets GCC=yes for Clang as well as GCC + if test "x$GCC" = "xyes"; then + AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG], + [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */ +# if defined(__clang__) && defined(__llvm__) + AX_PTHREAD_CC_IS_CLANG +# endif + ], + [ax_cv_PTHREAD_CLANG=yes]) + fi + ]) +ax_pthread_clang="$ax_cv_PTHREAD_CLANG" + +ax_pthread_clang_warning=no + +# Clang needs special handling, because older versions handle the -pthread +# option in a rather... idiosyncratic way + +if test "x$ax_pthread_clang" = "xyes"; then + + # Clang takes -pthread; it has never supported any other flag + + # (Note 1: This will need to be revisited if a system that Clang + # supports has POSIX threads in a separate library. This tends not + # to be the way of modern systems, but it's conceivable.) + + # (Note 2: On some systems, notably Darwin, -pthread is not needed + # to get POSIX threads support; the API is always present and + # active. We could reasonably leave PTHREAD_CFLAGS empty. But + # -pthread does define _REENTRANT, and while the Darwin headers + # ignore this macro, third-party headers might not.) + + PTHREAD_CFLAGS="-pthread" + PTHREAD_LIBS= + + ax_pthread_ok=yes + + # However, older versions of Clang make a point of warning the user + # that, in an invocation where only linking and no compilation is + # taking place, the -pthread option has no effect ("argument unused + # during compilation"). They expect -pthread to be passed in only + # when source code is being compiled. + # + # Problem is, this is at odds with the way Automake and most other + # C build frameworks function, which is that the same flags used in + # compilation (CFLAGS) are also used in linking. Many systems + # supported by AX_PTHREAD require exactly this for POSIX threads + # support, and in fact it is often not straightforward to specify a + # flag that is used only in the compilation phase and not in + # linking. Such a scenario is extremely rare in practice. + # + # Even though use of the -pthread flag in linking would only print + # a warning, this can be a nuisance for well-run software projects + # that build with -Werror. So if the active version of Clang has + # this misfeature, we search for an option to squash it. + + AC_CACHE_CHECK([whether Clang needs flag to prevent "argument unused" warning when linking with -pthread], + [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG], + [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown + # Create an alternate version of $ac_link that compiles and + # links in two steps (.c -> .o, .o -> exe) instead of one + # (.c -> exe), because the warning occurs only in the second + # step + ax_pthread_save_ac_link="$ac_link" + ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g' + ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"` + ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)" + ax_pthread_save_CFLAGS="$CFLAGS" + for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do + AS_IF([test "x$ax_pthread_try" = "xunknown"], [break]) + CFLAGS="-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS" + ac_link="$ax_pthread_save_ac_link" + AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])], + [ac_link="$ax_pthread_2step_ac_link" + AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])], + [break]) + ]) + done + ac_link="$ax_pthread_save_ac_link" + CFLAGS="$ax_pthread_save_CFLAGS" + AS_IF([test "x$ax_pthread_try" = "x"], [ax_pthread_try=no]) + ax_cv_PTHREAD_CLANG_NO_WARN_FLAG="$ax_pthread_try" + ]) + + case "$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG" in + no | unknown) ;; + *) PTHREAD_CFLAGS="$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS" ;; + esac + +fi # $ax_pthread_clang = yes + +if test "x$ax_pthread_ok" = "xno"; then +for ax_pthread_try_flag in $ax_pthread_flags; do + + case $ax_pthread_try_flag in + none) + AC_MSG_CHECKING([whether pthreads work without any flags]) + ;; + + -mt,pthread) + AC_MSG_CHECKING([whether pthreads work with -mt -lpthread]) + PTHREAD_CFLAGS="-mt" + PTHREAD_LIBS="-lpthread" + ;; + + -*) + AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag]) + PTHREAD_CFLAGS="$ax_pthread_try_flag" + ;; + + pthread-config) + AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no]) + AS_IF([test "x$ax_pthread_config" = "xno"], [continue]) + PTHREAD_CFLAGS="`pthread-config --cflags`" + PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" + ;; + + *) + AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag]) + PTHREAD_LIBS="-l$ax_pthread_try_flag" + ;; + esac + + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + + # Check for various functions. We must include pthread.h, + # since some functions may be macros. (On the Sequent, we + # need a special flag -Kthread to make this header compile.) + # We check for pthread_join because it is in -lpthread on IRIX + # while pthread_create is in libc. We check for pthread_attr_init + # due to DEC craziness with -lpthreads. We check for + # pthread_cleanup_push because it is one of the few pthread + # functions on Solaris that doesn't have a non-functional libc stub. + # We try pthread_create on general principles. + + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include +# if $ax_pthread_check_cond +# error "$ax_pthread_check_macro must be defined" +# endif + static void routine(void *a) { a = 0; } + static void *start_routine(void *a) { return a; }], + [pthread_t th; pthread_attr_t attr; + pthread_create(&th, 0, start_routine, 0); + pthread_join(th, 0); + pthread_attr_init(&attr); + pthread_cleanup_push(routine, 0); + pthread_cleanup_pop(0) /* ; */])], + [ax_pthread_ok=yes], + []) + + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" + + AC_MSG_RESULT([$ax_pthread_ok]) + AS_IF([test "x$ax_pthread_ok" = "xyes"], [break]) + + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" +done +fi + +# Various other checks: +if test "x$ax_pthread_ok" = "xyes"; then + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + + # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. + AC_CACHE_CHECK([for joinable pthread attribute], + [ax_cv_PTHREAD_JOINABLE_ATTR], + [ax_cv_PTHREAD_JOINABLE_ATTR=unknown + for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ], + [int attr = $ax_pthread_attr; return attr /* ; */])], + [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break], + []) + done + ]) + AS_IF([test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xunknown" && \ + test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xPTHREAD_CREATE_JOINABLE" && \ + test "x$ax_pthread_joinable_attr_defined" != "xyes"], + [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], + [$ax_cv_PTHREAD_JOINABLE_ATTR], + [Define to necessary symbol if this constant + uses a non-standard name on your system.]) + ax_pthread_joinable_attr_defined=yes + ]) + + AC_CACHE_CHECK([whether more special flags are required for pthreads], + [ax_cv_PTHREAD_SPECIAL_FLAGS], + [ax_cv_PTHREAD_SPECIAL_FLAGS=no + case $host_os in + solaris*) + ax_cv_PTHREAD_SPECIAL_FLAGS="-D_POSIX_PTHREAD_SEMANTICS" + ;; + esac + ]) + AS_IF([test "x$ax_cv_PTHREAD_SPECIAL_FLAGS" != "xno" && \ + test "x$ax_pthread_special_flags_added" != "xyes"], + [PTHREAD_CFLAGS="$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS" + ax_pthread_special_flags_added=yes]) + + AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT], + [ax_cv_PTHREAD_PRIO_INHERIT], + [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[int i = PTHREAD_PRIO_INHERIT; + return i;]])], + [ax_cv_PTHREAD_PRIO_INHERIT=yes], + [ax_cv_PTHREAD_PRIO_INHERIT=no]) + ]) + AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes" && \ + test "x$ax_pthread_prio_inherit_defined" != "xyes"], + [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.]) + ax_pthread_prio_inherit_defined=yes + ]) + + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" + + # More AIX lossage: compile with *_r variant + if test "x$GCC" != "xyes"; then + case $host_os in + aix*) + AS_CASE(["x/$CC"], + [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6], + [#handle absolute path differently from PATH based program lookup + AS_CASE(["x$CC"], + [x/*], + [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])], + [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])]) + ;; + esac + fi +fi + +test -n "$PTHREAD_CC" || PTHREAD_CC="$CC" + +AC_SUBST([PTHREAD_LIBS]) +AC_SUBST([PTHREAD_CFLAGS]) +AC_SUBST([PTHREAD_CC]) + +# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: +if test "x$ax_pthread_ok" = "xyes"; then + ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1]) + : +else + ax_pthread_ok=no + $2 +fi +AC_LANG_POP +])dnl AX_PTHREAD diff --git a/m4/ax_require_defined.m4 b/m4/ax_require_defined.m4 new file mode 100644 index 0000000..17c3eab --- /dev/null +++ b/m4/ax_require_defined.m4 @@ -0,0 +1,37 @@ +# =========================================================================== +# https://www.gnu.org/software/autoconf-archive/ax_require_defined.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_REQUIRE_DEFINED(MACRO) +# +# DESCRIPTION +# +# AX_REQUIRE_DEFINED is a simple helper for making sure other macros have +# been defined and thus are available for use. This avoids random issues +# where a macro isn't expanded. Instead the configure script emits a +# non-fatal: +# +# ./configure: line 1673: AX_CFLAGS_WARN_ALL: command not found +# +# It's like AC_REQUIRE except it doesn't expand the required macro. +# +# Here's an example: +# +# AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG]) +# +# LICENSE +# +# Copyright (c) 2014 Mike Frysinger +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 2 + +AC_DEFUN([AX_REQUIRE_DEFINED], [dnl + m4_ifndef([$1], [m4_fatal([macro ]$1[ is not defined; is a m4 file missing?])]) +])dnl AX_REQUIRE_DEFINED diff --git a/m4/dl.sh b/m4/dl.sh new file mode 100755 index 0000000..9874dbd --- /dev/null +++ b/m4/dl.sh @@ -0,0 +1,27 @@ +#!/bin/sh -e +# Copyright (c) 2019-2021, OARC, Inc. +# Copyright (c) 2019, DENIC eG +# All rights reserved. +# +# This file is part of dnsmeter. +# +# dnsmeter is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# dnsmeter is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with dnsmeter. If not, see . + +m4_files="ax_append_flag.m4 ax_cflags_warn_all.m4 ax_compiler_vendor.m4 \ + ax_prepend_flag.m4 ax_pthread.m4 ax_require_defined.m4" + +for ax in $m4_files; do + rm -f "$ax" + wget -O "$ax" "http://git.savannah.gnu.org/gitweb/?p=autoconf-archive.git;a=blob_plain;f=m4/$ax" +done diff --git a/rpm/dnsmeter.spec b/rpm/dnsmeter.spec new file mode 100644 index 0000000..35601f9 --- /dev/null +++ b/rpm/dnsmeter.spec @@ -0,0 +1,115 @@ +Name: dnsmeter +Version: 1.0.2 +Release: 1%{?dist} +Summary: DNS performance and infrastructure testing +Group: Productivity/Networking/DNS/Utilities + +License: GPL-3.0 +URL: https://www.dns-oarc.net/tools/dnsmeter +# Source needs to be generated by dist-tools/create-source-packages, see +# https://github.com/jelu/dist-tools +Source0: https://github.com/DNS-OARC/dnsmeter/archive/v%{version}.tar.gz?/%{name}_%{version}.orig.tar.gz + +BuildRequires: autoconf +BuildRequires: automake +BuildRequires: libtool +BuildRequires: gcc-c++ +BuildRequires: bind-devel +BuildRequires: openssl-devel +%if 0%{?suse_version} || 0%{?sle_version} +BuildRequires: libbz2-devel +%else +BuildRequires: bzip2-devel +%endif +BuildRequires: libidn2-devel +BuildRequires: zlib-devel +BuildRequires: libpcap-devel +BuildRequires: pcre-devel +BuildRequires: gettext-devel + +%description +DNSMeter is a tool for testing performance of nameserver and/or +infrastructure around it. +It generates dns queries and sends them via UDP to a target nameserver +and counts the answers. + +Features: +- payload can be given as text file or pcap file +- can automatically run different load steps, which can be given as + list or ranges +- results per load step can be stored in CSV file +- sender address can be spoofed from a given network or from pcap file, + if payload is a pcap file +- answers are counted, even if source address is spoofed, if answers get + routed back to the load generator +- roundtrip-times are measured (average, min, mix) +- amount of DNSSEC queries can be given as percentage of total traffic +- optimized for high amount of packets. On an Intel(R) Xeon(R) CPU E5-2430 + v2 @ 2.50GHz it can generate more than 900.000 packets per second + + +%prep +%setup -q -n %{name}_%{version} + + +%build +sh autogen.sh +%configure +make %{?_smp_mflags} + + +%check +#make test +true + + +%install +rm -rf $RPM_BUILD_ROOT +make install DESTDIR=$RPM_BUILD_ROOT + + +%clean +rm -rf $RPM_BUILD_ROOT + + +%files +%defattr(-,root,root) +%{_bindir}/dnsmeter +%{_datadir}/doc/* +%{_mandir}/man1/* + + +%changelog +* Wed Jun 02 2021 Jerry Lundström 1.0.2-1 +- Release 1.0.2 + * This release fixes an issue with source port being static when only + using `-q` to generate traffic from one host/IP. The source port is + now randomized for every DNS query. + * Other changes is mainly about build system, packages and fixed issues + detected by code analysis tools. + * Commits: + 05000cc Typo, random source port + 6a71707 Coverage + 1c724ce SonarCloud + 0776d20 Badges + c274884 LGTM + 5a12c61 COPR + f77efed Build dependency + a359b66 iconv +* Mon Oct 07 2019 Jerry Lundström 1.0.1-1 +- Release 1.0.1 + * This release fixes a few minor bugs and a dependency issue which made + `dnsmeter` throw an exception when using the `-r` option. + * Bugfixes: + - Use existing `rtt_avg`, was showing total RTT divided by number of threads + - Fix an issue which missed the first 8 bytes of a text payload + * Commits: + 72197b5 PCAP detect + 665be2d RTT average + 3fe7b66 pplib dependencies, RTT average + e565d42 Funding + 1a1ea40 README +* Mon Sep 23 2019 Jerry Lundström 1.0.0-1 +- Release 1.0.0 +* Wed Sep 18 2019 Jerry Lundström 0.9.0-1 +- First package release diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..e39c1e8 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,472 @@ +# Copyright (c) 2019-2021, OARC, Inc. +# Copyright (c) 2019, DENIC eG +# All rights reserved. +# +# This file is part of dnsmeter. +# +# dnsmeter is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# dnsmeter is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with dnsmeter. If not, see . + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in +DISTCLEANFILES = $(srcdir)/pplib/include/ppl7-config.h \ + $(srcdir)/pplib/release/libppl7.a +CLEANFILES = dnsmeter.1 *.gcda *.gcno *.gcov + +SUBDIRS = test + +AM_CXXFLAGS = -I$(srcdir) \ + -I$(top_srcdir) \ + -I$(srcdir)/pplib/include \ + $(PTHREAD_CFLAGS) $(ICONV_CFLAGS) + +bin_PROGRAMS = dnsmeter + +dnsmeter_SOURCES = dns_receiver_thread.cpp dns_sender.cpp \ + dns_sender_thread.cpp main.cpp packet.cpp payload_file.cpp query.cpp \ + raw_socket_receiver.cpp raw_socket_sender.cpp system_stat.cpp +dist_dnsmeter_SOURCES = dns_receiver_thread.h dns_sender.h \ + dns_sender_thread.h exceptions.h packet.h payload_file.h query.h \ + raw_socket_receiver.h raw_socket_sender.h system_stat.h +dnsmeter_LDADD = $(PTHREAD_LIBS) $(ICONV_LIBS) \ + $(srcdir)/pplib/release/libppl7.a + +BUILT_SOURCES = pplib/include/ppl7-config.h + +pplib/include/ppl7-config.h: + cd "$(srcdir)/pplib" && ./configure --disable-oggtest \ +--disable-freetypetest --disable-sdltest --disable-sdlframework \ +--without-jpeg --without-libjpegturbo --without-libpng \ +--without-libmicrohttpd --without-libmcrypt-prefix \ +--without-libtiff --without-libcurl --without-mpg123 \ +--without-lame --without-ogg --without-libcdio --without-libiconv-prefix \ +--without-imlib --without-mysql --without-postgresql \ +--without-sqlite3 --without-libldn --without-nasm + +$(srcdir)/pplib/release/libppl7.a: pplib/include/ppl7-config.h + cd "$(srcdir)/pplib" && make + +man1_MANS = dnsmeter.1 + +dnsmeter.1: dnsmeter.1.in Makefile + sed -e 's,[@]PACKAGE_VERSION[@],$(PACKAGE_VERSION),g' \ +-e 's,[@]PACKAGE_URL[@],$(PACKAGE_URL),g' \ +-e 's,[@]PACKAGE_BUGREPORT[@],$(PACKAGE_BUGREPORT),g' \ +< $(srcdir)/dnsmeter.1.in > dnsmeter.1 + +if ENABLE_GCOV +gcov-local: + for src in $(dnsmeter_SOURCES); do \ + gcov -l -r -s "$(srcdir)" "$$src"; \ + done +endif + +EXTRA_DIST = dnsmeter.1.in \ + pplib/Makefile.in \ + pplib/src/types/ByteArrayPtr.cpp \ + pplib/src/types/ByteArray.cpp \ + pplib/src/types/Array.cpp \ + pplib/src/types/Pointer.cpp \ + pplib/src/types/WideString.cpp \ + pplib/src/types/AssocArray.cpp \ + pplib/src/types/String.cpp \ + pplib/src/types/Variant.cpp \ + pplib/src/types/DateTime.cpp \ + pplib/src/math/crc32.cpp \ + pplib/src/math/calc.cpp \ + pplib/src/math/md5.cpp \ + pplib/src/math/random.cpp \ + pplib/src/database/MySQL.cpp \ + pplib/src/database/ResultSet.cpp \ + pplib/src/database/Database.cpp \ + pplib/src/database/PostgreSQL.cpp \ + pplib/src/database/DBPool.cpp \ + pplib/src/database/DBPoolOfPools.cpp \ + pplib/src/database/Sqlite3.cpp \ + pplib/src/audio/AudioCD.cpp \ + pplib/src/audio/AudioEncoder_MP3.cpp \ + pplib/src/audio/AudioDecoder_Ogg.cpp \ + pplib/src/audio/AudioInfo.cpp \ + pplib/src/audio/Cddb.cpp \ + pplib/src/audio/AudioDecoder_MP3.cpp \ + pplib/src/audio/AudioDecoder_Aiff.cpp \ + pplib/src/audio/AudioEncoder_Wave.cpp \ + pplib/src/audio/Icecast.cpp \ + pplib/src/audio/AudioEncoder_Ogg.cpp \ + pplib/src/audio/ID3Tag.cpp \ + pplib/src/audio/AudioDecoder_Wave.cpp \ + pplib/src/audio/Mp3.cpp \ + pplib/src/audio/AudioEncoder_Aiff.cpp \ + pplib/src/toolkit/Divider.cpp \ + pplib/src/toolkit/WindowManager.cpp \ + pplib/src/toolkit/Window.cpp \ + pplib/src/toolkit/LineInput.cpp \ + pplib/src/toolkit/WindowManager_SDL2.cpp \ + pplib/src/toolkit/Layout.cpp \ + pplib/src/toolkit/Widget.cpp \ + pplib/src/toolkit/Surface.cpp \ + pplib/src/toolkit/Frame.cpp \ + pplib/src/toolkit/Label.cpp \ + pplib/src/toolkit/Button.cpp \ + pplib/src/toolkit/Event.cpp \ + pplib/src/asm/common.asm \ + pplib/src/asm/colors.asm \ + pplib/src/asm/pixel.asm \ + pplib/src/asm/chromakey.asm \ + pplib/src/asm/cpu.asm \ + pplib/src/asm/blt_blend.asm \ + pplib/src/asm/rect.asm \ + pplib/src/asm/fonts.asm \ + pplib/src/asm/blt.asm \ + pplib/src/internet/WikiParser.cpp \ + pplib/src/internet/Webserver.cpp \ + pplib/src/internet/ipaddress.cpp \ + pplib/src/internet/sockaddr.cpp \ + pplib/src/internet/resolver.cpp \ + pplib/src/internet/UDPSocket.cpp \ + pplib/src/internet/ipnetwork.cpp \ + pplib/src/internet/curl.cpp \ + pplib/src/internet/SocketMessage.cpp \ + pplib/src/internet/inet_functions.cpp \ + pplib/src/internet/openssl.cpp \ + pplib/src/internet/TCPSocket.cpp \ + pplib/src/crypto/Digest.cpp \ + pplib/src/crypto/Crypt.cpp \ + pplib/src/crypto/MCrypt.cpp \ + pplib/src/core/Signal.cpp \ + pplib/src/core/Compat.cpp \ + pplib/src/core/Mutex.cpp \ + pplib/src/core/ThreadPool.cpp \ + pplib/src/core/Functions.cpp \ + pplib/src/core/MemoryGroup.cpp \ + pplib/src/core/MemFile.cpp \ + pplib/src/core/StringFunctions.cpp \ + pplib/src/core/Dir.cpp \ + pplib/src/core/File.cpp \ + pplib/src/core/ConfigParser.cpp \ + pplib/src/core/DirEntry.cpp \ + pplib/src/core/Resourcen.cpp \ + pplib/src/core/Exceptions.cpp \ + pplib/src/core/FileObject.cpp \ + pplib/src/core/PerlHelper.cpp \ + pplib/src/core/Json.cpp \ + pplib/src/core/Iconv.cpp \ + pplib/src/core/AVLTree.cpp \ + pplib/src/core/Logger.cpp \ + pplib/src/core/Threads.cpp \ + pplib/src/core/PythonHelper.cpp \ + pplib/src/core/Compression.cpp \ + pplib/src/core/Time.cpp \ + pplib/src/core/cpu.cpp \ + pplib/src/core/MemoryHeap.cpp \ + pplib/src/core/PFPFile.cpp \ + pplib/src/core/Resource.cpp \ + pplib/src/core/GzFile.cpp \ + pplib/src/grafix/Point.cpp \ + pplib/src/grafix/ImageFilter_PPM.cpp \ + pplib/src/grafix/Font5.cpp \ + pplib/src/grafix/ImageFilter_JPEG.cpp \ + pplib/src/grafix/Rect.cpp \ + pplib/src/grafix/DrawableBlit.cpp \ + pplib/src/grafix/Point3D.cpp \ + pplib/src/grafix/RGBFormat.cpp \ + pplib/src/grafix/ImageFilter_GIF.cpp \ + pplib/src/grafix/Fonts.cpp \ + pplib/src/grafix/ImageFilter_TGA.cpp \ + pplib/src/grafix/ImageFilter_PNG.cpp \ + pplib/src/grafix/FontFreeType.cpp \ + pplib/src/grafix/Grafix.cpp \ + pplib/src/grafix/Font6.cpp \ + pplib/src/grafix/DrawablePixel.cpp \ + pplib/src/grafix/ImageFilter_BMP.cpp \ + pplib/src/grafix/Sprite.cpp \ + pplib/src/grafix/ImageFilter_TIFF.cpp \ + pplib/src/grafix/Size.cpp \ + pplib/src/grafix/ImageList.cpp \ + pplib/src/grafix/DrawableLines.cpp \ + pplib/src/grafix/DrawableColor.cpp \ + pplib/src/grafix/DrawableShapes.cpp \ + pplib/src/grafix/Image.cpp \ + pplib/src/grafix/ImageFilter.cpp \ + pplib/src/grafix/ImageFilter_ImageMagick.cpp \ + pplib/src/grafix/Font4.cpp \ + pplib/src/grafix/Drawable.cpp \ + pplib/src/grafix/Color.cpp \ + pplib/genConfigure \ + pplib/genMakefile.in \ + pplib/aclocal.m4 \ + pplib/conf.sh \ + pplib/include/crypto.h \ + pplib/include/ppl7-ppl6compat.h \ + pplib/include/ppl7.h \ + pplib/include/ppl7-tk.h \ + pplib/include/ppl7-visualc-config.h \ + pplib/include/ppl7-config.h.in \ + pplib/include/config.h.in \ + pplib/include/ppl7-inet.h \ + pplib/include/ppl7-audio.h \ + pplib/include/threads.h \ + pplib/include/ppl7-grafix.h \ + pplib/include/socket.h \ + pplib/include/ppl7-crypto.h \ + pplib/include/prolog.h \ + pplib/include/ppl7-types.h \ + pplib/include/ppl7-algorithms.h \ + pplib/include/ppl7-exceptions.h \ + pplib/include/ppl7-db.h \ + pplib/include/compat.h \ + pplib/tests/Makefile.in \ + pplib/tests/src/toolkit.h \ + pplib/tests/src/stringspeed.cpp \ + pplib/tests/src/database/db_mysql.cpp \ + pplib/tests/src/database/db_sqlite.cpp \ + pplib/tests/src/database/db_postgres.cpp \ + pplib/tests/src/audio/id3tag.cpp \ + pplib/tests/src/audio/audio_decoder_aiff.cpp \ + pplib/tests/src/audio/audio_decoder_mp3.cpp \ + pplib/tests/src/audio/audio_encoder_mp3.cpp \ + pplib/tests/src/audio/audioinfo.cpp \ + pplib/tests/src/audio/audio_encoder_aiff.cpp \ + pplib/tests/src/audio/audio_encoder_wave.cpp \ + pplib/tests/src/audio/audio_decoder_wave.cpp \ + pplib/tests/src/ppl6/strings.cpp \ + pplib/tests/src/ppl6/assocarray.cpp \ + pplib/tests/src/crypto/mcrypt.cpp \ + pplib/tests/src/crypto/crypto.cpp \ + pplib/tests/src/crypto/digest.cpp \ + pplib/tests/src/wordlist.cpp \ + pplib/tests/src/core/stringfunctions.cpp \ + pplib/tests/src/core/iconv.cpp \ + pplib/tests/src/core/functions.cpp \ + pplib/tests/src/core/json.cpp \ + pplib/tests/src/core/filestatic.cpp \ + pplib/tests/src/core/strings.cpp \ + pplib/tests/src/core/gzfile.cpp \ + pplib/tests/src/core/pythonhelper.cpp \ + pplib/tests/src/core/memoryheap.cpp \ + pplib/tests/src/core/avltree.cpp \ + pplib/tests/src/core/variant.cpp \ + pplib/tests/src/core/pointer.cpp \ + pplib/tests/src/core/math.cpp \ + pplib/tests/src/core/dir.cpp \ + pplib/tests/src/core/configparser.cpp \ + pplib/tests/src/core/time.cpp \ + pplib/tests/src/core/bytearray.cpp \ + pplib/tests/src/core/datetime.cpp \ + pplib/tests/src/core/memorygroup.cpp \ + pplib/tests/src/core/bytearrayptr.cpp \ + pplib/tests/src/core/widestrings.cpp \ + pplib/tests/src/core/array.cpp \ + pplib/tests/src/core/perlhelper.cpp \ + pplib/tests/src/core/file.cpp \ + pplib/tests/src/core/logger.cpp \ + pplib/tests/src/core/assocarray.cpp \ + pplib/tests/src/core/list.cpp \ + pplib/tests/src/loggertest.cpp \ + pplib/tests/src/grafix/grafix_image.cpp \ + pplib/tests/src/grafix/grafix_rect.cpp \ + pplib/tests/src/grafix/grafix_imagefilter.cpp \ + pplib/tests/src/grafix/grafix_size.cpp \ + pplib/tests/src/grafix/grafix_font.cpp \ + pplib/tests/src/grafix/grafix_rgbformat.cpp \ + pplib/tests/src/grafix/grafix_point3d.cpp \ + pplib/tests/src/grafix/grafix_point.cpp \ + pplib/tests/src/grafix/grafix.cpp \ + pplib/tests/src/grafix/grafix_color.cpp \ + pplib/tests/src/grafix/grafix_drawable.cpp \ + pplib/tests/src/gfxreftest.cpp \ + pplib/tests/src/toolkit.cpp \ + pplib/tests/src/textsnippets.cpp \ + pplib/tests/src/main.cpp \ + pplib/tests/src/threadtest.cpp \ + pplib/tests/src/inet/tcpsocket.cpp \ + pplib/tests/src/inet/ipaddress.cpp \ + pplib/tests/src/inet/wikiparser.cpp \ + pplib/tests/src/inet/sockaddr.cpp \ + pplib/tests/src/inet/resolver.cpp \ + pplib/tests/src/inet/inet.cpp \ + pplib/tests/src/inet/ipnetwork.cpp \ + pplib/tests/create_postgres_db.sh \ + pplib/tests/valgrind.suppressions \ + pplib/tests/gcovr \ + pplib/tests/test.conf \ + pplib/tests/ppl7-tests.h \ + pplib/tests/testdata/test_192cbr_taggedWithCover.mp3 \ + pplib/tests/testdata/test_192vbr.mp3 \ + pplib/tests/testdata/unittest.png \ + pplib/tests/testdata/ppl7-icon-64x64.png \ + pplib/tests/testdata/test_44kHz_tagged.wav \ + pplib/tests/testdata/test.pcx \ + pplib/tests/testdata/unicodeUSASCII.txt \ + pplib/tests/testdata/test-pal-trans.png \ + pplib/tests/testdata/fonts/liberationsans8.fnt5 \ + pplib/tests/testdata/fonts/LiberationSans-Bold.ttf \ + pplib/tests/testdata/fonts/freesans4.fnt5 \ + pplib/tests/testdata/fonts/segoeui4.fnt5 \ + pplib/tests/testdata/filenameUSASCII.txt \ + pplib/tests/testdata/database/postgresql.sql \ + pplib/tests/testdata/database/mysql.sql \ + pplib/tests/testdata/database/sqlite3.sql \ + pplib/tests/testdata/test_192cbr_tagged.mp3 \ + pplib/tests/testdata/test.tga \ + pplib/tests/testdata/test.png \ + pplib/tests/testdata/jsontest2.json \ + pplib/tests/testdata/jsontest4.json \ + pplib/tests/testdata/compression.txt \ + pplib/tests/testdata/mpg123.h \ + pplib/tests/testdata/test.gif \ + pplib/tests/testdata/cover.jpg \ + pplib/tests/testdata/unittest.bmp \ + pplib/tests/testdata/screenshot1.png \ + pplib/tests/testdata/lame.h \ + pplib/tests/testdata/example.conf \ + pplib/tests/testdata/test.tif \ + pplib/tests/testdata/dirwalk/testfile.txt \ + pplib/tests/testdata/dirwalk/zfile.txt \ + pplib/tests/testdata/dirwalk/file1.txt \ + pplib/tests/testdata/dirwalk/LICENSE.TXT \ + pplib/tests/testdata/dirwalk/file3.txt \ + pplib/tests/testdata/dirwalk/file4äöü.txt \ + pplib/tests/testdata/dirwalk/afile.txt \ + pplib/tests/testdata/dirwalk/file2.txt \ + pplib/tests/testdata/test.jpg \ + pplib/tests/testdata/reference.png \ + pplib/tests/testdata/lame_API.txt \ + pplib/tests/testdata/filenameUTF8äöü.txt \ + pplib/tests/testdata/tags_at_eof.aiff \ + pplib/tests/testdata/test_44kHz.aiff \ + pplib/tests/testdata/test_44kHz_tagged.aiff \ + pplib/tests/testdata/compression.txt.gz \ + pplib/tests/testdata/test_44kHz_taggedWithCover.aiff \ + pplib/tests/testdata/test_192cbr.mp3 \ + pplib/tests/testdata/test_44kHz.wav \ + pplib/tests/testdata/test.ppm \ + pplib/tests/testdata/jsontest1.json \ + pplib/tests/testdata/unicodeUtf8äöü.txt \ + pplib/tests/testdata/jsontest3.json \ + pplib/tests/testdata/test_320cbr.mp3 \ + pplib/tests/testdata/test.bmp \ + pplib/tests/testdata/test-pal.png \ + pplib/LICENSE.TXT \ + pplib/configure \ + pplib/autoconf/iconv.m4 \ + pplib/autoconf/imlib.m4 \ + pplib/autoconf/libidn.m4 \ + pplib/autoconf/imagemagick.m4 \ + pplib/autoconf/freetds.m4 \ + pplib/autoconf/ax_lib_gcrypt.m4 \ + pplib/autoconf/checkfuncs.m4 \ + pplib/autoconf/libtiff.m4 \ + pplib/autoconf/ldns.m4 \ + pplib/autoconf/bzip2.m4 \ + pplib/autoconf/ax_lib_crypto.m4 \ + pplib/autoconf/ax_lib_sqlite3.m4 \ + pplib/autoconf/lib-link.m4 \ + pplib/autoconf/ax_check_compiler_flags.m4 \ + pplib/autoconf/ax_path_lib_pcre.m4 \ + pplib/autoconf/nasm.m4 \ + pplib/autoconf/missing \ + pplib/autoconf/libcdio.m4 \ + pplib/autoconf/libjpeg-turbo.m4 \ + pplib/autoconf/libmicrohttpd.m4 \ + pplib/autoconf/ax_gcc_x86_cpuid.m4 \ + pplib/autoconf/ogg.m4 \ + pplib/autoconf/compile \ + pplib/autoconf/ax_pthread.m4 \ + pplib/autoconf/sdl2.m4 \ + pplib/autoconf/ax_cxx_compile_stdcxx.m4 \ + pplib/autoconf/ax_check_openssl.m4 \ + pplib/autoconf/ax_lib_mysql.m4 \ + pplib/autoconf/ax_lib_postgresql.m4 \ + pplib/autoconf/ax_have_qt.m4 \ + pplib/autoconf/ax_gcc_archflag.m4 \ + pplib/autoconf/gtest.m4 \ + pplib/autoconf/ax_check_zlib.m4 \ + pplib/autoconf/shout.m4 \ + pplib/autoconf/jpeg.m4 \ + pplib/autoconf/libcurl.m4 \ + pplib/autoconf/config.rpath \ + pplib/autoconf/ax_cc_maxopt.m4 \ + pplib/autoconf/freetype.m4 \ + pplib/autoconf/install-sh \ + pplib/autoconf/config.sub \ + pplib/autoconf/lame.m4 \ + pplib/autoconf/libbind.m4 \ + pplib/autoconf/config.guess \ + pplib/autoconf/libidn2.m4 \ + pplib/autoconf/png.m4 \ + pplib/autoconf/mpg123.m4 \ + pplib/autoconf/libmcrypt.m4 \ + pplib/autoconf/ax_cxx_compile_stdcxx_11.m4 \ + pplib/TODO.TXT \ + pplib/docs/threads.dox \ + pplib/docs/credits.dox \ + pplib/docs/CWikiParser_diagram.png \ + pplib/docs/groups.dox \ + pplib/docs/formate.dox \ + pplib/docs/pcrenote.dox \ + pplib/docs/CWStringSetFunctions.dox \ + pplib/docs/pregexpr.dox \ + pplib/docs/verwendung.dox \ + pplib/docs/win32-vc9-clr.png \ + pplib/docs/header-bg.png \ + pplib/docs/ppl7style.css \ + pplib/docs/CDrawable.png \ + pplib/docs/Class_Point.png \ + pplib/docs/datentypen.dox \ + pplib/docs/main.dox \ + pplib/docs/DoxygenLayout.xml \ + pplib/docs/sprintf.dox \ + pplib/docs/strftime.dox \ + pplib/docs/examples/nasm.rules \ + pplib/docs/examples/db_examples.cpp \ + pplib/docs/examples/CThread_ThreadMain.cpp \ + pplib/docs/examples/IcecastExample.cpp \ + pplib/docs/examples/ppl7.m4 \ + pplib/docs/installation.dox \ + pplib/docs/gtest.dox \ + pplib/docs/sockets.dox \ + pplib/docs/icecast_status.png \ + pplib/docs/formate/PFPVersion1.dox \ + pplib/docs/formate/PFPVersion2.dox \ + pplib/docs/formate/SocketMessage.dox \ + pplib/docs/formate/PFPVersion3.dox \ + pplib/docs/formate/PPLResVersion6.dox \ + pplib/docs/structures.dox \ + pplib/docs/header.html \ + pplib/docs/win32-vc9-rl.png \ + pplib/docs/datenbanken.dox \ + pplib/Doxyfile \ + pplib/configure.ac \ + pplib/HISTORY.TXT \ + pplib/acinclude.m4 \ + pplib/ppl7-config.in \ + pplib/resource/ppl7-icon-16x16.png \ + pplib/resource/ppl7-icon-64x64.png \ + pplib/resource/toolbar.png \ + pplib/resource/wikistyle.css \ + pplib/resource/liberationsans2.fnt6 \ + pplib/resource/resourcen.lst \ + pplib/resource/ppl7.ico \ + pplib/resource/ppl7-icon-256x256.png \ + pplib/resource/ppl7-icon-32x32.png \ + pplib/resource/ButtonSymbols.png \ + pplib/resource/ppl7-icon-128x128.png \ + pplib/resource/ppl7-icon-48x48.png \ + pplib/resource/cursor.png \ + pplib/resource/makefonts.sh \ + pplib/resource/mimetypes.png \ + pplib/resource/liberationmono2.fnt6 \ + pplib/resource/res.h \ + pplib/resource/SymbolsAlpha.png \ + pplib/resource/icons.png \ + pplib/README.TXT diff --git a/src/dns_receiver_thread.cpp b/src/dns_receiver_thread.cpp new file mode 100644 index 0000000..91ac8b2 --- /dev/null +++ b/src/dns_receiver_thread.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "dns_receiver_thread.h" + +DNSReceiverThread::DNSReceiverThread() +{ +} + +DNSReceiverThread::~DNSReceiverThread() +{ +} + +void DNSReceiverThread::setInterface(const ppl7::String& Device) +{ + Socket.initInterface(Device); +} + +void DNSReceiverThread::setSource(const ppl7::IPAddress& ip, int port) +{ + Socket.setSource(ip, port); +} + +void DNSReceiverThread::run() +{ + counter.clear(); + while (1) { + if (Socket.socketReady()) + Socket.receive(counter); + if (this->threadShouldStop()) + break; + } +} + +ppluint64 DNSReceiverThread::getPacketsReceived() const +{ + return counter.num_pkgs; +} + +ppluint64 DNSReceiverThread::getBytesReceived() const +{ + return counter.bytes_rcv; +} + +double DNSReceiverThread::getDuration() const +{ + return counter.rtt_total; +} + +double DNSReceiverThread::getRoundTripTimeAverage() const +{ + if (counter.num_pkgs) + return counter.rtt_total / counter.num_pkgs; //NOSONAR + return 0.0f; +} + +double DNSReceiverThread::getRoundTripTimeMin() const +{ + return counter.rtt_min; +} + +double DNSReceiverThread::getRoundTripTimeMax() const +{ + return counter.rtt_max; +} + +const RawSocketReceiver::Counter& DNSReceiverThread::getCounter() const +{ + return counter; +} diff --git a/src/dns_receiver_thread.h b/src/dns_receiver_thread.h new file mode 100644 index 0000000..b0a4abf --- /dev/null +++ b/src/dns_receiver_thread.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "raw_socket_receiver.h" + +#include +#include + +#ifndef __dnsmeter_dns_receiver_thread_h +#define __dnsmeter_dns_receiver_thread_h + +class DNSReceiverThread : public ppl7::Thread { +private: + RawSocketReceiver Socket; + RawSocketReceiver::Counter counter; + +public: + DNSReceiverThread(); + ~DNSReceiverThread(); + void setInterface(const ppl7::String& Device); + void setSource(const ppl7::IPAddress& ip, int port); + void run(); + + ppluint64 getPacketsReceived() const; + ppluint64 getBytesReceived() const; + + double getDuration() const; + double getRoundTripTimeAverage() const; + double getRoundTripTimeMin() const; + double getRoundTripTimeMax() const; + const RawSocketReceiver::Counter& getCounter() const; +}; + +#endif diff --git a/src/dns_sender.cpp b/src/dns_sender.cpp new file mode 100644 index 0000000..ccb7cf3 --- /dev/null +++ b/src/dns_sender.cpp @@ -0,0 +1,571 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "dns_sender.h" +#include "exceptions.h" +#include "dns_sender_thread.h" + +#include +#include + +static const char* rcode_names[] = { + "OK", "FORMAT", "SRVFAIL", "NAME", "NOTIMPL", "REFUSED", + "YXDOMAIN", "YXRRSET", "NXRRSET", "NOTAUTH", "NOTZONE", + "11", "12", "13", "14", "15", + NULL +}; + +bool stopFlag = false; + +void sighandler(int sig) +{ + stopFlag = true; + printf("Stopping...\n"); +} + +void DNSSender::help() +{ + ppl7::String name, underline; + name.setf("dnsmeter %s", PACKAGE_VERSION); + underline.repeat("=", name.size()); + name.printnl(); + underline.printnl(); + printf("\nUsage:\n" + " -h shows this help\n" + " -q HOST hostname or IP address of sender if you don't want to spoof\n" + " (see -s)\n" + " -s NET|pcap spoof sender address. Use random IP from the given network\n" + " (example: 192.168.0.0/16). Only works when running as root!\n" + " If payload is a pcap file, you can use \"-s pcap\" to use the\n" + " source addresses and ports from the pcap file.\n" + " -e ETH interface on which the packet receiver should listen\n" + " (FreeBSD only)\n" + " -z HOST:PORT hostname or IP address and port of the target nameserver\n" + " -p FILE file with queries/payload or pcap file\n" + " -l # runtime in seconds (default=10 seconds)\n" + " -t # timeout in seconds (default=2 seconds)\n" + " -n # number of worker threads (default=1)\n" + " -r # queryrate (Default=as much as possible)\n" + " can be a single value, a comma separated list (rate,rate,...)\n" + " or a range and a step value (start - end, step)\n" + " -d # amount of queries in percent on which the DNSSEC-flags are set\n" + " (default=0)\n" + " -c FILE CSV-file for results\n" + " --ignore answers are ignored and therefor not counted. In this mode\n" + " the tool only generates traffic." + "\n"); +} + +DNSSender::Results::Results() +{ + queryrate = 0; + counter_send = 0; + counter_received = 0; + bytes_send = 0; + bytes_received = 0; + counter_errors = 0; + packages_lost = 0; + counter_0bytes = 0; + for (int i = 0; i < 255; i++) + counter_errorcodes[i] = 0; + rtt_avg = 0.0f; + rtt_total = 0.0f; + rtt_min = 0.0f; + rtt_max = 0.0f; + for (int i = 0; i < 16; i++) + rcodes[i] = 0; + truncated = 0; +} + +void DNSSender::Results::clear() +{ + queryrate = 0; + counter_send = 0; + counter_received = 0; + bytes_send = 0; + bytes_received = 0; + counter_errors = 0; + packages_lost = 0; + counter_0bytes = 0; + for (int i = 0; i < 255; i++) + counter_errorcodes[i] = 0; + rtt_avg = 0.0f; + rtt_total = 0.0f; + rtt_min = 0.0f; + rtt_max = 0.0f; + for (int i = 0; i < 16; i++) + rcodes[i] = 0; + truncated = 0; +} + +DNSSender::Results operator-(const DNSSender::Results& second, const DNSSender::Results& first) +{ + DNSSender::Results r; + r.queryrate = second.queryrate - first.queryrate; + r.counter_send = second.counter_send - first.counter_send; + r.counter_received = second.counter_received - first.counter_received; + r.bytes_send = second.bytes_send - first.bytes_send; + r.bytes_received = second.bytes_received - first.bytes_received; + r.counter_errors = second.counter_errors - first.counter_errors; + r.packages_lost = second.packages_lost - first.packages_lost; + r.counter_0bytes = second.counter_0bytes - first.counter_0bytes; + for (int i = 0; i < 255; i++) + r.counter_errorcodes[i] = second.counter_errorcodes[i] - first.counter_errorcodes[i]; + r.rtt_total = second.rtt_total - first.rtt_total; + if (r.counter_received) + r.rtt_avg = r.rtt_total / r.counter_received; //NOSONAR + else + r.rtt_avg = 0.0; + r.rtt_min = second.rtt_min - first.rtt_min; + r.rtt_max = second.rtt_max - first.rtt_max; + + for (int i = 0; i < 16; i++) + r.rcodes[i] = second.rcodes[i] - first.rcodes[i]; + r.truncated = second.truncated - first.truncated; + return r; +} + +DNSSender::DNSSender() +{ + ppl7::InitSockets(); + Runtime = 10; + Timeout = 2; + ThreadCount = 1; + Timeslices = 1.0f; + ignoreResponses = false; + DnssecRate = 0; + TargetPort = 53; + spoofingEnabled = false; + Receiver = NULL; + spoofFromPcap = false; +} + +DNSSender::~DNSSender() +{ + if (Receiver) + delete Receiver; +} + +ppl7::Array DNSSender::getQueryRates(const ppl7::String& QueryRates) +{ + ppl7::Array rates; + if (QueryRates.isEmpty()) { + rates.add("0"); + } else { + ppl7::Array matches; + if (QueryRates.pregMatch("/^([0-9]+)-([0-9]+),([0-9]+)$", matches)) { + for (ppluint64 i = matches[1].toUnsignedInt64(); i <= matches[2].toUnsignedInt64(); i += matches[3].toUnsignedInt64()) { + rates.addf("%llu", i); + } + } else { + rates.explode(QueryRates, ","); + } + } + return rates; +} + +void DNSSender::getTarget(int argc, char** argv) +{ + if (!ppl7::HaveArgv(argc, argv, "-z")) { + throw MissingCommandlineParameter("target IP/hostname or port missing (-z IP:PORT)"); + } + ppl7::String Tmp = ppl7::GetArgv(argc, argv, "-z"); + ppl7::Array Tok(Tmp, ":"); + if (Tok.size() != 2) { + if (Tok.size() != 1) + throw InvalidCommandlineParameter("-z IP:PORT"); + TargetPort = 53; + } else { + TargetPort = Tok[1].toInt(); + } + if (TargetPort < 1 || TargetPort > 65535) + throw InvalidCommandlineParameter("-z IP:PORT, Invalid Port"); + std::list Result; + size_t num = ppl7::GetHostByName(Tok[0], Result, ppl7::af_inet); + if (!num) + throw InvalidCommandlineParameter("-z IP:PORT, Invalid IP or could not resolve Hostname"); + TargetIP = Result.front(); + //printf ("num=%d, %s\n",num, (const char*)TargetIP.toString()); +} + +void DNSSender::getSource(int argc, char** argv) +{ + if (ppl7::HaveArgv(argc, argv, "-s")) { + ppl7::String Tmp = ppl7::GetArgv(argc, argv, "-s").toLowerCase(); + if (Tmp == "pcap") { + spoofFromPcap = true; + } else { + SourceNet.set(Tmp); + if (SourceNet.family() != ppl7::IPAddress::IPv4) + throw UnsupportedIPFamily("only IPv4 works"); + } + spoofingEnabled = true; + } else { + ppl7::String Tmp = ppl7::GetArgv(argc, argv, "-q"); + std::list Result; + size_t num = ppl7::GetHostByName(Tmp, Result, ppl7::af_inet); + if (!num) + throw InvalidCommandlineParameter("-q HOST, Invalid IP or could not resolve Hostname"); + SourceIP = Result.front(); + if (SourceIP.family() != ppl7::IPAddress::IPv4) + throw UnsupportedIPFamily("only IPv4 works"); + spoofingEnabled = false; + } +} + +int DNSSender::getParameter(int argc, char** argv) +{ + if (ppl7::HaveArgv(argc, argv, "-q") && ppl7::HaveArgv(argc, argv, "-s")) { + printf("ERROR: could not use parameters -q and -s together\n\n"); + help(); + return 1; + } + if ((!ppl7::HaveArgv(argc, argv, "-q")) && (!ppl7::HaveArgv(argc, argv, "-s"))) { + printf("ERROR: source IP/hostname or network for source address spoofing missing (-q IP | -s NETWORK)\n\n"); + help(); + return 1; + } + ignoreResponses = ppl7::HaveArgv(argc, argv, "--ignore"); + + if (ppl7::HaveArgv(argc, argv, "-e")) { + InterfaceName = ppl7::GetArgv(argc, argv, "-e"); + } + + try { + getTarget(argc, argv); + getSource(argc, argv); + } catch (const ppl7::Exception& e) { + printf("ERROR: missing or invalid parameter\n"); + e.print(); + printf("\n"); + help(); + return 1; + } + + Runtime = ppl7::GetArgv(argc, argv, "-l").toInt(); + Timeout = ppl7::GetArgv(argc, argv, "-t").toInt(); + ThreadCount = ppl7::GetArgv(argc, argv, "-n").toInt(); + ppl7::String QueryRates = ppl7::GetArgv(argc, argv, "-r"); + CSVFileName = ppl7::GetArgv(argc, argv, "-c"); + QueryFilename = ppl7::GetArgv(argc, argv, "-p"); + if (ppl7::HaveArgv(argc, argv, "-d")) { + DnssecRate = ppl7::GetArgv(argc, argv, "-d").toInt(); + if (DnssecRate < 0 || DnssecRate > 100) { + printf("ERROR: DNSSEC-Rate must be an integer between 0 and 100 (-d #)\n\n"); + help(); + return 1; + } + } + if (!ThreadCount) + ThreadCount = 1; + if (!Runtime) + Runtime = 10; + if (!Timeout) + Timeout = 2; + if (QueryFilename.isEmpty()) { + printf("ERROR: Payload-File is missing (-p FILENAME)\n\n"); + help(); + return 1; + } + rates = getQueryRates(QueryRates); + return 0; +} + +int DNSSender::openFiles() +{ + if (CSVFileName.notEmpty()) { + try { + openCSVFile(CSVFileName); + } catch (const ppl7::Exception& e) { + printf("ERROR: could not open CSV-file for writing\n"); + e.print(); + return 1; + } + } + try { + payload.openQueryFile(QueryFilename); + } catch (const ppl7::Exception& e) { + printf("ERROR: could not open payload file or it does not contain any queries\n"); + e.print(); + return 1; + } + return 0; +} + +int DNSSender::main(int argc, char** argv) +{ + if (ppl7::HaveArgv(argc, argv, "-h") || ppl7::HaveArgv(argc, argv, "--help") || argc < 2) { + help(); + return 0; + } + if (getParameter(argc, argv) != 0) + return 1; + if (openFiles() != 0) + return 1; + + signal(SIGINT, sighandler); + signal(SIGKILL, sighandler); + + DNSSender::Results results; + try { + if (!ignoreResponses) { + Receiver = new DNSReceiverThread(); + Receiver->setSource(TargetIP, TargetPort); + try { + Receiver->setInterface(InterfaceName); + } catch (const ppl7::Exception& e) { + printf("ERROR: could not bind on device [%s]\n", (const char*)InterfaceName); + e.print(); + printf("\n"); + help(); + return 1; + } + } + prepareThreads(); + for (size_t i = 0; i < rates.size(); i++) { + results.queryrate = rates[i].toInt(); + run(rates[i].toInt()); + getResults(results); + presentResults(results); + saveResultsToCsv(results); + } + threadpool.destroyAllThreads(); + } catch (const ppl7::OperationInterruptedException&) { + getResults(results); + presentResults(results); + saveResultsToCsv(results); + } catch (const ppl7::Exception& e) { + e.print(); + return 1; + } + return 0; +} + +void DNSSender::prepareThreads() +{ + for (int i = 0; i < ThreadCount; i++) { + DNSSenderThread* thread = new DNSSenderThread(); + thread->setDestination(TargetIP, TargetPort); + thread->setRuntime(Runtime); + thread->setTimeout(Timeout); + thread->setTimeslice(Timeslices); + thread->setDNSSECRate(DnssecRate); + thread->setVerbose(false); + thread->setPayload(payload); + if (spoofingEnabled) { + if (spoofFromPcap) + thread->setSourcePcap(); + else + thread->setSourceNet(SourceNet); + } else { + thread->setSourceIP(SourceIP); + } + threadpool.addThread(thread); + } +} + +void DNSSender::openCSVFile(const ppl7::String& Filename) +{ + CSVFile.open(Filename, ppl7::File::APPEND); + if (CSVFile.size() == 0) { + CSVFile.putsf("#QPS Send; QPS Received; QPS Errors; Lostrate; " + "rtt_avg; rtt_min; rtt_max;" + "\n"); + CSVFile.flush(); + } +} + +void DNSSender::showCurrentStats(ppl7::ppl_time_t start_time) +{ + DNSSender::Results result, diff; + ppl7::ppl_time_t runtime = ppl7::GetTime() - start_time; + getResults(result); + diff = result - vis_prev_results; + vis_prev_results = result; + + int h = (int)(runtime / 3600); + runtime -= h * 3600; + int m = (int)(runtime / 60); + int s = runtime - (m * 60); + + printf("%02d:%02d:%02d Queries send: %7llu, rcv: %7llu, ", h, m, s, + diff.counter_send, diff.counter_received); + printf("Data send: %6llu KB, rcv: %6llu KB", diff.bytes_send / 1024, diff.bytes_received / 1024); + printf("\n"); +} + +void DNSSender::calcTimeslice(int queryrate) +{ + Timeslices = (1000.0f / queryrate) * ThreadCount; //NOSONAR + //if (Zeitscheibe<1.0f) Zeitscheibe=1.0f; + if (Timeslices < 0.1f) + Timeslices = 0.1f; +} + +void DNSSender::run(int queryrate) +{ + printf("###############################################################################\n"); + if (queryrate) { + calcTimeslice(queryrate); + printf("# Start Session with Threads: %d, Queryrate: %d, Timeslot: %0.6f ms\n", + ThreadCount, queryrate, Timeslices); + } else { + printf("# Start Session with Threads: %d, Queryrate: unlimited\n", + ThreadCount); + } + + ppl7::ThreadPool::iterator it; + for (it = threadpool.begin(); it != threadpool.end(); ++it) { + ((DNSSenderThread*)(*it))->setQueryRate(queryrate / ThreadCount); + ((DNSSenderThread*)(*it))->setTimeslice(Timeslices); + } + vis_prev_results.clear(); + sampleSensorData(sys1); + if (Receiver) + Receiver->threadStart(); + threadpool.startThreads(); + ppl7::ppl_time_t start = ppl7::GetTime(); + ppl7::ppl_time_t report = start + 1; + ppl7::MSleep(500); + while (threadpool.running() == true && stopFlag == false) { + ppl7::MSleep(100); + ppl7::ppl_time_t now = ppl7::GetTime(); + if (now >= report) { + report = now + 1; + showCurrentStats(start); + } + } + if (Receiver) + Receiver->threadStop(); + sampleSensorData(sys2); + if (stopFlag == true) { + threadpool.stopThreads(); + throw ppl7::OperationInterruptedException("test aborted"); + } +} + +void DNSSender::getResults(DNSSender::Results& result) +{ + ppl7::ThreadPool::iterator it; + result.clear(); + + for (it = threadpool.begin(); it != threadpool.end(); ++it) { + result.counter_send += ((DNSSenderThread*)(*it))->getPacketsSend(); + result.bytes_send += ((DNSSenderThread*)(*it))->getBytesSend(); + result.counter_errors += ((DNSSenderThread*)(*it))->getErrors(); + result.counter_0bytes += ((DNSSenderThread*)(*it))->getCounter0Bytes(); + for (int i = 0; i < 255; i++) + result.counter_errorcodes[i] += ((DNSSenderThread*)(*it))->getCounterErrorCode(i); + } + if (Receiver) { + const RawSocketReceiver::Counter& counter = Receiver->getCounter(); + result.counter_received = counter.num_pkgs; + result.bytes_received = counter.bytes_rcv; + result.rtt_total = counter.rtt_total; + if (counter.num_pkgs) + result.rtt_avg = counter.rtt_total / counter.num_pkgs; //NOSONAR + else + result.rtt_avg = 0.0; + result.rtt_min = counter.rtt_min; + result.rtt_max = counter.rtt_max; + for (int i = 0; i < 16; i++) + result.rcodes[i] = counter.rcodes[i]; + result.truncated = counter.truncated; + } + + result.packages_lost = result.counter_send - result.counter_received; + if (result.counter_received > result.counter_send) + result.packages_lost = 0; +} + +void DNSSender::saveResultsToCsv(const DNSSender::Results& result) +{ + + if (CSVFile.isOpen()) { + CSVFile.putsf("%llu;%llu;%llu;%0.3f;%0.4f;%0.4f;%0.4f;\n", + (ppluint64)((double)result.counter_send / (double)Runtime), + (ppluint64)((double)result.counter_received / (double)Runtime), + (ppluint64)((double)result.counter_errors / (double)Runtime), + (double)result.packages_lost * 100.0 / (double)result.counter_send, + result.rtt_avg * 1000.0, + result.rtt_min * 1000.0, + result.rtt_max * 1000.0); + CSVFile.flush(); + } +} + +void DNSSender::presentResults(const DNSSender::Results& result) +{ + printf("===============================================================================\n"); + const SystemStat::Interface& net1 = sys1.interfaces[InterfaceName]; + const SystemStat::Interface& net2 = sys2.interfaces[InterfaceName]; + SystemStat::Network transmit = SystemStat::Network::getDelta(net1.transmit, net2.transmit); + SystemStat::Network received = SystemStat::Network::getDelta(net1.receive, net2.receive); + printf("network if %s Pkt send: %lu, rcv: %lu, Data send: %lu KB, rcv: %lu KB\n", + (const char*)InterfaceName, + transmit.packets, received.packets, transmit.bytes / 1024, received.bytes / 1024); + + ppluint64 qps_send = (ppluint64)((double)result.counter_send / (double)Runtime); + ppluint64 bps_send = (ppluint64)((double)result.bytes_send / (double)Runtime); + ppluint64 qps_received = (ppluint64)((double)result.counter_received / (double)Runtime); + ppluint64 bps_received = (ppluint64)((double)result.bytes_received / (double)Runtime); + + printf("DNS Queries send: %10llu, Qps: %7llu, Data send: %7llu KB = %6llu MBit\n", + result.counter_send, qps_send, result.bytes_send / 1024, bps_send / (1024 * 1024)); + + printf("DNS Queries rcv: %10llu, Qps: %7llu, Data rcv: %7llu KB = %6llu MBit\n", + result.counter_received, qps_received, result.bytes_received / 1024, bps_received / (1024 * 1024)); + + printf("DNS Queries lost: %10llu = %0.3f %%\n", result.packages_lost, + (double)result.packages_lost * 100.0 / (double)result.counter_send); + + printf("DNS rtt average: %0.4f ms, " + "min: %0.4f ms, " + "max: %0.4f ms\n", + result.rtt_avg * 1000.0, + result.rtt_min * 1000.0, + result.rtt_max * 1000.0); + printf("DNS truncated: %llu\nDNS RCODES: ", result.truncated); + for (int i = 0; i < 15; i++) { + if (result.rcodes[i]) { + printf("%s: %llu, ", rcode_names[i], result.rcodes[i]); + } + } + printf("\n"); + + if (result.counter_errors) { + printf("Errors: %10llu, Qps: %10llu\n", result.counter_errors, + (ppluint64)((double)result.counter_errors / (double)Runtime)); + } + if (result.counter_0bytes) { + printf("Errors 0Byte: %10llu, Qps: %10llu\n", result.counter_0bytes, + (ppluint64)((double)result.counter_0bytes / (double)Runtime)); + } + for (int i = 0; i < 255; i++) { + if (result.counter_errorcodes[i] > 0) { + printf("Errors %3d: %10llu, Qps: %10llu [%s]\n", i, result.counter_errorcodes[i], + (ppluint64)((double)result.counter_errorcodes[i] / (double)Runtime), + strerror(i)); + } + } +} diff --git a/src/dns_sender.h b/src/dns_sender.h new file mode 100644 index 0000000..d01408e --- /dev/null +++ b/src/dns_sender.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "dns_receiver_thread.h" +#include "payload_file.h" +#include "system_stat.h" + +#include + +#ifndef __dnsmeter_dns_sender_h +#define __dnsmeter_dns_sender_h + +class DNSSender { +private: +#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L + DNSSender& operator=(const DNSSender& other); + DNSSender(DNSSender &&other) noexcept; + DNSSender const & operator=(DNSSender &&other); +#endif + +public: + class Results { + public: + int queryrate; + ppluint64 counter_send; + ppluint64 counter_received; + ppluint64 bytes_send; + ppluint64 bytes_received; + ppluint64 counter_errors; + ppluint64 packages_lost; + ppluint64 counter_0bytes; + ppluint64 counter_errorcodes[255]; + ppluint64 rcodes[16]; + ppluint64 truncated; + double rtt_total; + double rtt_avg; + double rtt_min; + double rtt_max; + Results(); + void clear(); + }; + +private: + ppl7::ThreadPool threadpool; + ppl7::IPAddress TargetIP; + ppl7::IPAddress SourceIP; + ppl7::IPNetwork SourceNet; + ppl7::String CSVFileName; + ppl7::String QueryFilename; + ppl7::File CSVFile; + ppl7::Array rates; + ppl7::String InterfaceName; + PayloadFile payload; + DNSReceiverThread* Receiver; + DNSSender::Results vis_prev_results; + SystemStat sys1, sys2; + + int TargetPort; + int Runtime; + int Timeout; + int ThreadCount; + int DnssecRate; + float Timeslices; + bool ignoreResponses; + bool spoofingEnabled; + bool spoofFromPcap; + + void openCSVFile(const ppl7::String& Filename); + void run(int queryrate); + void presentResults(const DNSSender::Results& result); + void saveResultsToCsv(const DNSSender::Results& result); + void prepareThreads(); + void getResults(DNSSender::Results& result); + ppl7::Array getQueryRates(const ppl7::String& QueryRates); + void readSourceIPList(const ppl7::String& filename); + + void getTarget(int argc, char** argv); + void getSource(int argc, char** argv); + int getParameter(int argc, char** argv); + int openFiles(); + void calcTimeslice(int queryrate); + + void showCurrentStats(ppl7::ppl_time_t start_time); + +public: + DNSSender(); + ~DNSSender(); + void help(); + int main(int argc, char** argv); +}; + +DNSSender::Results operator-(const DNSSender::Results& first, const DNSSender::Results& second); + +#endif diff --git a/src/dns_sender_thread.cpp b/src/dns_sender_thread.cpp new file mode 100644 index 0000000..b5c560a --- /dev/null +++ b/src/dns_sender_thread.cpp @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "dns_sender_thread.h" +#include "query.h" +#include "exceptions.h" + +#include +#include +#include +#include +#include +#include +#include + +DNSSenderThread::DNSSenderThread() +{ + buffer = (unsigned char*)malloc(4096); + if (!buffer) + throw ppl7::OutOfMemoryException(); + Timeslice = 0.0f; + runtime = 10; + timeout = 5; + queryrate = 0; + counter_packets_send = 0; + counter_bytes_send = 0; + errors = 0; + counter_0bytes = 0; + duration = 0.0; + for (int i = 0; i < 255; i++) + counter_errorcodes[i] = 0; + verbose = false; + spoofingEnabled = false; + DnssecRate = 0; + dnsseccounter = 0; + payload = NULL; + spoofing_net_start = 0; + spoofing_net_size = 0; + payloadIsPcap = false; + spoofingFromPcap = false; +} + +DNSSenderThread::~DNSSenderThread() +{ + free(buffer); +} + +void DNSSenderThread::setDestination(const ppl7::IPAddress& ip, int port) +{ + Socket.setDestination(ip, port); + pkt.setDestination(ip, port); +} + +void DNSSenderThread::setPayload(PayloadFile& payload) +{ + this->payload = &payload; + this->payloadIsPcap = payload.isPcap(); +} + +void DNSSenderThread::setRuntime(int seconds) +{ + runtime = seconds; +} + +void DNSSenderThread::setTimeout(int seconds) +{ + timeout = seconds; +} + +void DNSSenderThread::setDNSSECRate(int rate) +{ + DnssecRate = rate; +} + +void DNSSenderThread::setQueryRate(ppluint64 qps) +{ + queryrate = qps; +} + +void DNSSenderThread::setTimeslice(float ms) +{ + if (ms == 0.0f || ms > 1000.0f) + throw ppl7::InvalidArgumentsException(); + //if ((1000 % ms)!=0) throw ppl7::InvalidArgumentsException(); + Timeslice = (double)ms / 1000; +} + +void DNSSenderThread::setSourceIP(const ppl7::IPAddress& ip) +{ + sourceip = ip; + spoofingEnabled = false; +} + +void DNSSenderThread::setSourceNet(const ppl7::IPNetwork& net) +{ + sourcenet = net; + spoofingEnabled = true; + spoofing_net_start = ntohl(*(in_addr_t*)net.first().addr()); + spoofing_net_size = powl(2, 32 - net.prefixlen()); +} + +void DNSSenderThread::setSourcePcap() +{ + spoofingEnabled = true; + spoofingFromPcap = true; +} + +void DNSSenderThread::setVerbose(bool verbose) +{ + this->verbose = verbose; +} + +#define PCAP_HEADER_SIZE 14 + sizeof(struct ip) + sizeof(struct udphdr) + +void DNSSenderThread::sendPacket() +{ + size_t query_size; + while (1) { + try { + const ppl7::ByteArrayPtr& bap = payload->getQuery(); + query_size = bap.size(); + if (payloadIsPcap) { + query_size -= PCAP_HEADER_SIZE; + memcpy(buffer, ((const char*)bap.ptr()) + PCAP_HEADER_SIZE, query_size); + } else { + memcpy(buffer, bap.ptr(), query_size); + dnsseccounter += DnssecRate; + if (dnsseccounter >= 100) { + query_size = AddDnssecToQuery(buffer, 4096, query_size); + dnsseccounter -= 100; + } + } + pkt.setPayload(buffer, query_size); + if (spoofingEnabled) { + if (spoofingFromPcap) { + pkt.useSourceFromPcap((const char*)bap.ptr(), bap.size()); + } else { + pkt.randomSourceIP(spoofing_net_start, spoofing_net_size); + pkt.randomSourcePort(); + } + } else { + pkt.randomSourcePort(); + } + pkt.setDnsId(getQueryTimestamp()); + ssize_t n = Socket.send(pkt); + if (n > 0 && (size_t)n == pkt.size()) { + counter_packets_send++; + counter_bytes_send += pkt.size(); + } else if (n < 0) { + if (errno < 255) + counter_errorcodes[errno]++; + errors++; + } else { + counter_0bytes++; + } + return; + } catch (const UnknownRRType& exp) { + continue; + } catch (const InvalidDNSQuery& exp) { + continue; + } + } +} + +void DNSSenderThread::run() +{ + if (!payload) + throw ppl7::NullPointerException("payload not set!"); + if (!spoofingEnabled) { + pkt.setSource(sourceip, 0x4567); + } + dnsseccounter = 0; + counter_packets_send = 0; + counter_bytes_send = 0; + counter_0bytes = 0; + errors = 0; + duration = 0.0; + for (int i = 0; i < 255; i++) + counter_errorcodes[i] = 0; + double start = ppl7::GetMicrotime(); + if (queryrate > 0) { + runWithRateLimit(); + } else { + runWithoutRateLimit(); + } + duration = ppl7::GetMicrotime() - start; + waitForTimeout(); +} + +void DNSSenderThread::runWithoutRateLimit() +{ + double start = ppl7::GetMicrotime(); + double end = start + (double)runtime; + double now; + int pc = 0; + while (1) { + sendPacket(); + pc++; + if (pc > 10000) { + pc = 0; + if (this->threadShouldStop()) + break; + now = ppl7::GetMicrotime(); + if (now > end) + break; + } + } +} + +static inline double getNsec() +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return (double)ts.tv_sec + ((double)ts.tv_nsec / 1000000000.0); +} + +void DNSSenderThread::runWithRateLimit() +{ + struct timespec ts; + ppluint64 total_timeslices = runtime * 1000 / (Timeslice * 1000.0); + ppluint64 queries_rest = runtime * queryrate; + ppl7::SockAddr addr = Socket.getSockAddr(); + verbose = true; + if (verbose) { + //printf ("qps=%d, runtime=%d\n",queryrate, runtime); + printf("runtime: %d s, timeslice: %0.6f s, total timeslices: %llu, Qpts: %llu, Source: %s:%d\n", + runtime, Timeslice, total_timeslices, + queries_rest / total_timeslices, + (const char*)addr.toIPAddress().toString(), addr.port()); + } + double now = getNsec(); + double next_timeslice = now; + double next_checktime = now + 0.1; + + double start = ppl7::GetMicrotime(); + double end = start + (double)runtime; + double total_idle = 0.0; + + for (ppluint64 z = 0; z < total_timeslices; z++) { + next_timeslice += Timeslice; + ppluint64 timeslices_rest = total_timeslices - z; + ppluint64 queries_per_timeslice = queries_rest / timeslices_rest; + if (timeslices_rest == 1) + queries_per_timeslice = queries_rest; + for (ppluint64 i = 0; i < queries_per_timeslice; i++) { + sendPacket(); + } + + queries_rest -= queries_per_timeslice; + while ((now = getNsec()) < next_timeslice) { + if (now < next_timeslice) { + total_idle += next_timeslice - now; + ts.tv_sec = 0; + ts.tv_nsec = (next_timeslice - now) * 1000000000; + nanosleep(&ts, NULL); + } + } + if (now > next_checktime) { + next_checktime = now + 0.1; + if (this->threadShouldStop()) + break; + if (ppl7::GetMicrotime() >= end) + break; + //printf ("Zeitscheiben rest: %llu\n", z); + } + } + if (verbose) { + //printf ("total idle: %0.6f\n",total_idle); + } +} + +void DNSSenderThread::waitForTimeout() +{ + double start = ppl7::GetMicrotime(); + double end = start + (double)timeout; + double now, next_checktime = start + 0.1; + while ((now = ppl7::GetMicrotime()) < end) { + if (now > next_checktime) { + next_checktime = now + 0.1; + if (this->threadShouldStop()) + break; + } + ppl7::MSleep(10); + } +} + +ppluint64 DNSSenderThread::getPacketsSend() const +{ + return counter_packets_send; +} + +ppluint64 DNSSenderThread::getBytesSend() const +{ + return counter_bytes_send; +} + +ppluint64 DNSSenderThread::getErrors() const +{ + return errors; +} + +ppluint64 DNSSenderThread::getCounter0Bytes() const +{ + return counter_0bytes; +} + +ppluint64 DNSSenderThread::getCounterErrorCode(int err) const +{ + if (err < 255) + return counter_errorcodes[err]; + return 0; +} diff --git a/src/dns_sender_thread.h b/src/dns_sender_thread.h new file mode 100644 index 0000000..e2b251a --- /dev/null +++ b/src/dns_sender_thread.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "raw_socket_sender.h" +#include "payload_file.h" + +#include + +#ifndef __dnsmeter_dns_sender_thread_h +#define __dnsmeter_dns_sender_thread_h + +class DNSSenderThread : public ppl7::Thread { +private: +#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L + DNSSenderThread& operator=(const DNSSenderThread& other); + DNSSenderThread(DNSSenderThread &&other) noexcept; + DNSSenderThread const & operator=(DNSSenderThread &&other); +#endif + + RawSocketSender Socket; + Packet pkt; + + ppl7::IPAddress destination; + ppl7::IPAddress sourceip; + ppl7::IPNetwork sourcenet; + + PayloadFile* payload; + unsigned char* buffer; + ppluint64 queryrate; + ppluint64 counter_packets_send, errors, counter_0bytes; + ppluint64 counter_bytes_send; + ppluint64 counter_errorcodes[255]; + + unsigned int spoofing_net_start; + unsigned int spoofing_net_size; + + int runtime; + int timeout; + int DnssecRate; + int dnsseccounter; + double Timeslice; + + double duration; + bool spoofingEnabled; + bool verbose; + bool payloadIsPcap; + bool spoofingFromPcap; + + void sendPacket(); + void waitForTimeout(); + bool socketReady(); + + void runWithoutRateLimit(); + void runWithRateLimit(); + +public: + DNSSenderThread(); + ~DNSSenderThread(); + void setDestination(const ppl7::IPAddress& ip, int port); + void setSourceIP(const ppl7::IPAddress& ip); + void setSourceNet(const ppl7::IPNetwork& net); + void setSourcePcap(); + void setRandomSource(const ppl7::IPNetwork& net); + void setRuntime(int seconds); + void setTimeout(int seconds); + void setDNSSECRate(int rate); + void setQueryRate(ppluint64 qps); + void setTimeslice(float ms); + void setVerbose(bool verbose); + void setPayload(PayloadFile& payload); + void run(); + ppluint64 getPacketsSend() const; + ppluint64 getBytesSend() const; + ppluint64 getErrors() const; + ppluint64 getCounter0Bytes() const; + ppluint64 getCounterErrorCode(int err) const; +}; + +#endif diff --git a/src/dnsmeter.1.in b/src/dnsmeter.1.in new file mode 100644 index 0000000..96a4a4c --- /dev/null +++ b/src/dnsmeter.1.in @@ -0,0 +1,276 @@ +.\" Copyright (c) 2019-2021, OARC, Inc. +.\" Copyright (c) 2019, DENIC eG +.\" All rights reserved. +.\" +.\" This file is part of dnsmeter. +.\" +.\" dnsmeter is free software: you can redistribute it and/or modify +.\" it under the terms of the GNU General Public License as published by +.\" the Free Software Foundation, either version 3 of the License, or +.\" (at your option) any later version. +.\" +.\" dnsmeter is distributed in the hope that it will be useful, +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +.\" GNU General Public License for more details. +.\" +.\" You should have received a copy of the GNU General Public License +.\" along with dnsmeter. If not, see . +.TH dnsmeter 1 "@PACKAGE_VERSION@" "dnsmeter" +.SH NAME +dnsmeter \- DNS performance and infrastructure testing +.SH SYNOPSIS +.hy 0 +.ad l +\fBdnsmeter\fR\ [\fB\-h\fR] +[\fB\-q\ \fIHOST\fR] +[\fB\-s\ \fINET|pcap\fR] +[\fB\-e\ \fIETH\fR] +[\fB\-z\ \fIHOST:PORT\fR] +[\fB\-p\ \fIFILE\fR] +[\fB\-l\ \fI#\fR] +[\fB\-t\ \fI#\fR] +[\fB\-n\ \fI#\fR] +[\fB\-r\ \fI#\fR] +[\fB\-d\ \fI#\fR] +[\fB\-c\ \fIFILE\fR] +[\fB\--ignore\fR] +.ad +.hy +.SH DESCRIPTION +DNSMeter is a tool for testing performance of nameserver and/or +infrastructure around it. +It generates dns queries and sends them via UDP to a target nameserver +and counts the answers. + +Features: +.br +- payload can be given as text file or PCAP file +.br +- can automatically run different load steps, which can be given as +list or ranges +.br +- results per load step can be stored in CSV file +.br +- sender address can be spoofed from a given network or from PCAP file, +if payload is a PCAP file +.br +- answers are counted, even if source address is spoofed, if answers get +routed back to the load generator +.br +- roundtrip-times are measured (average, min, mix) +.br +- amount of DNSSEC queries can be given as percentage of total traffic +.br +- optimized for high amount of packets. On an Intel(R) Xeon(R) CPU E5-2430 +v2 @ 2.50GHz it can generate more than 900.000 packets per second +.SH OPTIONS +.TP +.B -h +Show option help. +.TP +.BI -q \ HOST +Hostname or IP address of sender if you don't want to spoof (see +.IR -s ). +.TP +.BI -s \ NET|pcap +Spoof sender address. +Use random IP from the given network (example: +.IR 192.168.0.0/16 ). +Only works when running as root! +If payload is a PCAP file, you can use +.BI -s pcap +to use the source addresses and ports from the PCAP file. +.TP +.BI -e \ ETH +Interface on which the packet receiver should listen (FreeBSD only). +.TP +.BI -z \ HOST:PORT +Hostname or IP address and port of the target nameserver. +.TP +.BI -p \ FILE +File with queries/payload or PCAP file. +.TP +.BI -l \ # +Runtime in seconds (default=10 seconds). +.TP +.BI -t \ # +Timeout in seconds (default=2 seconds). +.TP +.BI -n \ # +Number of worker threads (default=1). +.TP +.BI -r \ # +Query rate (Default=as much as possible) can be a single value, a comma +separated list (rate,rate,...) or a range and a step value (start - end, +step). +.TP +.BI -d \ # +Amount of queries in percent on which the DNSSEC-flags are set (default=0). +.TP +.BI -c \ FILE +CSV-file for results. +.TP +.B --ignore +Answers are ignored and therefor not counted. +In this mode the tool only generates traffic. +.SH USAGE +This section contains additional usage information not covered by +the options documentation. + +.BI -q \ HOST +| +.BI -s \ NETWORK +| +.BI -s \ pcap + +Source IP, hostname or network from which the packets should be send. +If you dont't want to spoof, use +.I -q +with a single IP address or hostname. +Use +.I -s +followed by a network, if you want to spoof the source address. +.B dnsmeter +will generated random IP addresses inside this network. +Example: +.B -s +.IR 10.0.0.0/8 . + +If payload is a PCAP file, you can use the source addresses and ports +from the PCAP file, if you use +.B -s +.IR pcap . + +.BI -e \ ETH + +Ignored on Linux, but on FreeBSD you have to enter the name of the +network interface on which the tool should listen for the answers. + +.BI -p \ FILE + +File with payload in text format or PCAP file. +When using a text format each line must contain one query with name +and record type. + +Example: + + www.denic.de A + denic.de NS + ... + +.IR NOTE : +the file should not be too big, because it is completely +loaded into memory and pre-compiled to DNS query packets. + +.BI -n \ # + +Number of worker threads, recommendation: +.br +- less than 200000 packets per second: 1 Thread +.br +- 200000 - 500000 packets per second: 2 Threads +.br +- more than 500000 packets per second: 4 Threads + +.BI NOTE : +this is CPU dependent! +If you have a fast CPU, you may need lesser threads, on a slow CPU you +may need more threads. +Don't use more threads than cores available on your CPU, minus one! + +.BI -r \ #[,#,#] + +Query rate or load steps. +Can be a single value if you want to test a specific query rate, a comma +separated list or a range with step with. + +Examples: +.br +- Single value: -r 100000 +.br +- a list of query rates: -r 10000,20000,30000,40000,50000,60000 +.br +- a range with step: -r 10000-200000,10000 + +.BI -d \ # + +Amount of DNSSEC queries in percentage between 0 and 100. +Is ignored, if using PCAP file as payload. + +.BI -c \ FILENAME + +Filename for results in CSV format. + +.BI NOTE : +if file exists, results are appended! + +.SH EXAMPLE + +Lets assume the following scenario: +.br +- load generator runs on FreeBSD +.br +- network interface an which the traffic goes out and comes back is "igb0" +.br +- source ip on the load generator is 192.168.155.20 +.br +- target nameserver has ip 192.168.0.1, port 53 +.br +- we want to spoof the sender address from the network 10.0.0.0/8 +.br +- the payload file is found here: /home/testdata/payload.txt +.br +- the nameserver is running on CentOS and we need to set a route back to the load generator: +.br + ip route add 10.0.0.0/8 via 192.168.155.20 +.br +- we want to test the following load steps: 30000,40000,45000,50000,100000,150000 +.br +- results should be written to results.csv +.br +- DNSSEC rate should be 70% + +This makes the following command: + + dnsmeter -p /home/testdata/payload.txt \\ + -r 30000,40000,45000,50000,100000,150000 \\ + -s 10.0.0.0/8 \\ + -z 192.168.0.1:53 \\ + -e igb0 \\ + -d 70 \\ + -c results.csv + +In the second example, we want to use a PCAP file as payload and want +to spoof with the addresses from that file: + + dnsmeter -p /home/testdata/pcap.file1 \\ + -r 30000,40000,45000,50000,100000,150000 \\ + -s pcap \\ + -z 192.168.0.1:53 \\ + -e igb0 \\ + -c results_pcap.csv + +.SH AUTHOR +Patrick Fedick +.RI ( https://github.com/pfedick ) +.LP +Maintained by DNS-OARC +.LP +.RS +.I https://www.dns-oarc.net/ +.RE +.LP +.SH BUGS +For issues and feature requests please use: +.LP +.RS +\fI@PACKAGE_URL@\fP +.RE +.LP +For question and help please use: +.LP +.RS +\fI@PACKAGE_BUGREPORT@\fP +.RE +.LP diff --git a/src/exceptions.h b/src/exceptions.h new file mode 100644 index 0000000..c116021 --- /dev/null +++ b/src/exceptions.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include + +#ifndef __dnsmeter_exceptions_h +#define __dnsmeter_exceptions_h + +PPL7EXCEPTION(MissingCommandlineParameter, Exception); +PPL7EXCEPTION(InvalidCommandlineParameter, Exception); +PPL7EXCEPTION(InvalidDNSQuery, Exception); +PPL7EXCEPTION(UnknownRRType, Exception); +PPL7EXCEPTION(BufferOverflow, Exception); +PPL7EXCEPTION(UnknownDestination, Exception); +PPL7EXCEPTION(InvalidQueryFile, Exception); +PPL7EXCEPTION(UnsupportedIPFamily, Exception); +PPL7EXCEPTION(FailedToInitializePacketfilter, Exception); +PPL7EXCEPTION(KernelAccessFailed, Exception); +PPL7EXCEPTION(SystemCallFailed, Exception); + +#endif diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..36e5bbf --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "dns_sender.h" + +#include +#include +#include + +int main(int argc, char** argv) +{ + res_init(); + // For unknown reason, res_mkquery is much slower (factor 3) when not + // setting the following options: + _res.options |= RES_USE_EDNS0; + _res.options |= RES_USE_DNSSEC; + + DNSSender Sender; + return Sender.main(argc, argv); +} diff --git a/src/packet.cpp b/src/packet.cpp new file mode 100644 index 0000000..2e242eb --- /dev/null +++ b/src/packet.cpp @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "packet.h" +#include "exceptions.h" +#include "query.h" + +#define __FAVOR_BSD 1 +#include +#include +#include +#include +#include + +#define USZ sizeof(struct udphdr) +#define ISZ sizeof(struct ip) +#define HDRSZ ISZ + USZ +#define MAXPACKETSIZE 4096 + +static unsigned short in_cksum(unsigned short* addr, int len) +{ + int nleft = len; + unsigned short* w = addr; + int sum = 0; + unsigned short answer = 0; + while (nleft > 1) { + sum += *w++; + nleft -= 2; + } + if (nleft == 1) { + *(unsigned char*)(&answer) = *(unsigned char*)w; + sum += answer; + } + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + answer = ~sum; + return (answer); +} + +static unsigned short udp_cksum(const struct ip* iphdr, const struct udphdr* udp, const unsigned char* payload, size_t payload_size) +{ + unsigned char cbuf[MAXPACKETSIZE]; + memset(cbuf, 0, sizeof(cbuf)); + unsigned char* p = (unsigned char*)cbuf; + *(unsigned int*)p = iphdr->ip_src.s_addr; + p += sizeof(unsigned int); + *(unsigned int*)p = iphdr->ip_dst.s_addr; + p += sizeof(unsigned int); + *(unsigned char*)p++ = 0; + *(unsigned char*)p++ = iphdr->ip_p; + *(unsigned short*)p = udp->uh_ulen; + p += sizeof(unsigned short); + memcpy(p, udp, USZ); + p += USZ; + memcpy(p, payload, payload_size); + return in_cksum((unsigned short*)cbuf, sizeof(uint) * 3 + sizeof(struct udphdr) + payload_size + (payload_size % 2)); +} + +Packet::Packet() +{ + buffersize = MAXPACKETSIZE; + payload_size = 0; + buffer = (unsigned char*)calloc(1, buffersize); + if (!buffer) + throw ppl7::OutOfMemoryException(); + + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + + iphdr->ip_hl = ISZ >> 2; + iphdr->ip_v = IPVERSION; + iphdr->ip_tos = 0; + iphdr->ip_off = 0; + iphdr->ip_ttl = 64; + iphdr->ip_p = IPPROTO_UDP; + iphdr->ip_sum = 0; + iphdr->ip_id = 0; + iphdr->ip_src.s_addr = 0; + iphdr->ip_dst.s_addr = 0; + iphdr->ip_len = htons(HDRSZ + payload_size); + iphdr->ip_sum = in_cksum((unsigned short*)iphdr, ISZ); + + udp->uh_ulen = htons(USZ + payload_size); + chksum_valid = false; +} + +Packet::~Packet() +{ + free(buffer); +} + +void Packet::setSource(const ppl7::IPAddress& ip_addr, int port) +{ + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + iphdr->ip_src.s_addr = *(in_addr_t*)ip_addr.addr(); + udp->uh_sport = htons(port); + chksum_valid = false; +} + +void Packet::randomSourcePort() +{ + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + udp->uh_sport = htons(ppl7::rand(1024, 65535)); + chksum_valid = false; +} + +void Packet::randomSourceIP(const ppl7::IPNetwork& net) +{ + struct ip* iphdr = (struct ip*)buffer; + in_addr_t start = ntohl(*(in_addr_t*)net.first().addr()); + size_t size = powl(2, 32 - net.prefixlen()); + iphdr->ip_src.s_addr = htonl(ppl7::rand(start, start + size - 1)); + chksum_valid = false; +} + +void Packet::randomSourceIP(unsigned int start, unsigned int size) +{ + struct ip* iphdr = (struct ip*)buffer; + iphdr->ip_src.s_addr = htonl(ppl7::rand(start, start + size - 1)); + chksum_valid = false; +} + +void Packet::useSourceFromPcap(const char* pkt, size_t size) +{ + const struct ip* s_iphdr = (const struct ip*)(pkt + 14); + const struct udphdr* s_udp = (const struct udphdr*)(pkt + 14 + sizeof(struct ip)); + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + iphdr->ip_src.s_addr = s_iphdr->ip_src.s_addr; + udp->uh_sport = s_udp->uh_sport; +} + +void Packet::setDestination(const ppl7::IPAddress& ip_addr, int port) +{ + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + iphdr->ip_dst.s_addr = *(in_addr_t*)ip_addr.addr(); + udp->uh_dport = htons(port); + chksum_valid = false; +} + +void Packet::setIpId(unsigned short id) +{ + struct ip* iphdr = (struct ip*)buffer; + iphdr->ip_id = htons(id); + chksum_valid = false; +} + +void Packet::setDnsId(unsigned short id) +{ + *((unsigned short*)(buffer + HDRSZ)) = htons(id); + chksum_valid = false; +} + +void Packet::setPayload(const void* payload, size_t size) +{ + if (size + HDRSZ > MAXPACKETSIZE) + throw BufferOverflow("%zd > %zd", size, MAXPACKETSIZE - HDRSZ); + memcpy(buffer + HDRSZ, payload, size); + payload_size = size; + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + iphdr->ip_len = htons(HDRSZ + payload_size); + udp->uh_ulen = htons(USZ + payload_size); + chksum_valid = false; +} + +void Packet::setPayloadDNSQuery(const ppl7::String& query, bool dnssec) +{ + payload_size = MakeQuery(query, buffer + HDRSZ, buffersize - HDRSZ, dnssec); + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + iphdr->ip_len = htons(HDRSZ + payload_size); + udp->uh_ulen = htons(USZ + payload_size); + chksum_valid = false; +} + +void Packet::updateChecksums() +{ + struct ip* iphdr = (struct ip*)buffer; + struct udphdr* udp = (struct udphdr*)(buffer + ISZ); + iphdr->ip_sum = 0; + iphdr->ip_sum = in_cksum((unsigned short*)iphdr, ISZ); + udp->uh_sum = 0; + udp->uh_sum = udp_cksum(iphdr, udp, buffer + HDRSZ, payload_size); + chksum_valid = true; +} + +size_t Packet::size() const +{ + return HDRSZ + payload_size; +} + +unsigned char* Packet::ptr() +{ + if (!chksum_valid) + updateChecksums(); + return buffer; +} diff --git a/src/packet.h b/src/packet.h new file mode 100644 index 0000000..91650c7 --- /dev/null +++ b/src/packet.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include +#include + +#ifndef __dnsmeter_packet_h +#define __dnsmeter_packet_h + +class Packet { +private: +#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L + Packet& operator=(const Packet& other); + Packet(Packet &&other) noexcept; + Packet const & operator=(Packet &&other); +#endif + + unsigned char* buffer; + int buffersize; + int payload_size; + bool chksum_valid; + + void updateChecksums(); + +public: + Packet(); + ~Packet(); + void setSource(const ppl7::IPAddress& ip_addr, int port); + void setDestination(const ppl7::IPAddress& ip_addr, int port); + void setPayload(const void* payload, size_t size); + void setPayloadDNSQuery(const ppl7::String& query, bool dnssec = false); + void setDnsId(unsigned short id); + void setIpId(unsigned short id); + + void randomSourceIP(const ppl7::IPNetwork& net); + void randomSourceIP(unsigned int start, unsigned int size); + void randomSourcePort(); + void useSourceFromPcap(const char* pkt, size_t size); + + size_t size() const; + unsigned char* ptr(); +}; + +#endif diff --git a/src/payload_file.cpp b/src/payload_file.cpp new file mode 100644 index 0000000..5c42a1e --- /dev/null +++ b/src/payload_file.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "payload_file.h" +#include "exceptions.h" +#include "query.h" + +#define __FAVOR_BSD 1 +#include +#include +#include +#include +#include + +#pragma pack(push) /* push current alignment to stack */ +#pragma pack(1) /* set alignment to 1 byte boundary */ +struct ETHER { + unsigned char destination[6]; + unsigned char source[6]; + unsigned short type; +}; +#pragma pack(pop) /* restore original alignment from stack */ + +PayloadFile::PayloadFile() +{ + validLinesInQueryFile = 0; + payloadIsPcap = false; +} + +bool PayloadFile::detectPcap(ppl7::File& ff) +{ + unsigned char buffer[8]; + if (ff.read(buffer, 8) != 8) { + ff.seek(0); + return false; + } + ff.seek(0); + unsigned int magic = ppl7::Peek32(buffer + 0); + if (magic == 0xa1b2c3d4 || magic == 0xa1b23c4d) + return true; + if (magic == 0xd4c3b2a1 || magic == 0x4d3cb2a1) + return true; + return false; +} + +void PayloadFile::openQueryFile(const ppl7::String& Filename) +{ + if (Filename.isEmpty()) + throw InvalidQueryFile("File not given"); + ppl7::File QueryFile; + QueryFile.open(Filename, ppl7::File::READ); + if (QueryFile.size() == 0) { + throw InvalidQueryFile("File is empty [%s]", (const char*)Filename); + } + printf("INFO: Loading and precompile payload. This could take some time...\n"); + if (detectPcap(QueryFile)) { + loadAndCompilePcapFile(Filename); + } else { + loadAndCompile(QueryFile); + } + printf("INFO: %llu queries loaded\n", validLinesInQueryFile); + it = querycache.begin(); +} + +void PayloadFile::loadAndCompile(ppl7::File& ff) +{ + ppl7::ByteArray buf(4096); + ppl7::String buffer; + validLinesInQueryFile = 0; + unsigned char* compiled_query = (unsigned char*)buf.ptr(); + while (1) { + try { + if (ff.eof()) + throw ppl7::EndOfFileException(); + ff.gets(buffer, 1024); + buffer.trim(); + if (buffer.isEmpty()) + continue; + if (buffer.c_str()[0] == '#') + continue; + try { + // Precompile Query + int size = MakeQuery(buffer, compiled_query, 4096, false); + querycache.push_back(ppl7::ByteArray(compiled_query, size)); + validLinesInQueryFile++; + } catch (...) { + // ignore invalid queries + continue; + } + } catch (const ppl7::EndOfFileException&) { + if (validLinesInQueryFile == 0) { + throw InvalidQueryFile("No valid Queries found in Queryfile"); + } + return; + } + } +} + +void PayloadFile::loadAndCompilePcapFile(const ppl7::String& Filename) +{ + char errorbuffer[PCAP_ERRBUF_SIZE]; + struct pcap_pkthdr hdr; + payloadIsPcap = true; + validLinesInQueryFile = 0; + pcap_t* pp = pcap_open_offline((const char*)Filename, errorbuffer); + if (!pp) + throw InvalidQueryFile("%s", errorbuffer); + ppluint64 pkts_total = 0; + const u_char* pkt; + while ((pkt = pcap_next(pp, &hdr)) != NULL) { + pkts_total++; + //printf ("len=%d, caplen=%d\n",hdr.len,hdr.caplen); + const struct ETHER* eth = (const struct ETHER*)pkt; + if (hdr.caplen > 4096) + continue; + if (eth->type != htons(0x0800)) + continue; + const struct ip* iphdr = (const struct ip*)(pkt + 14); + if (iphdr->ip_v != 4) + continue; + const struct udphdr* udp = (const struct udphdr*)(pkt + 14 + sizeof(struct ip)); + if (udp->uh_dport != htons(53)) + continue; + const struct DNS_HEADER* dns = (const struct DNS_HEADER*)(pkt + 14 + sizeof(struct ip) + sizeof(struct udphdr)); + if (dns->qr != 0 || dns->opcode != 0) + continue; + querycache.push_back(ppl7::ByteArray(pkt, hdr.caplen)); + validLinesInQueryFile++; + } + printf("Packets read from pcap file: %llu, valid UDP DNS queries: %llu\n", + pkts_total, validLinesInQueryFile); + pcap_close(pp); + if (validLinesInQueryFile == 0) { + throw InvalidQueryFile("No valid Queries found in pcap file [%s]", (const char*)Filename); + } +} + +const ppl7::ByteArrayPtr& PayloadFile::getQuery() +{ + QueryMutex.lock(); + const ppl7::ByteArrayPtr& bap = *it; + ++it; + if (it == querycache.end()) + it = querycache.begin(); + QueryMutex.unlock(); + return bap; +} + +bool PayloadFile::isPcap() +{ + return payloadIsPcap; +} diff --git a/src/payload_file.h b/src/payload_file.h new file mode 100644 index 0000000..154f771 --- /dev/null +++ b/src/payload_file.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include +#include + +#ifndef __dnsmeter_payload_file_h +#define __dnsmeter_payload_file_h + +class PayloadFile { +private: + ppl7::Mutex QueryMutex; + ppluint64 validLinesInQueryFile; + std::list querycache; + std::list::const_iterator it; + bool payloadIsPcap; + bool detectPcap(ppl7::File& ff); + void loadAndCompile(ppl7::File& ff); + void loadAndCompilePcapFile(const ppl7::String& Filename); + +public: + PayloadFile(); + void openQueryFile(const ppl7::String& Filename); + const ppl7::ByteArrayPtr& getQuery(); + bool isPcap(); +}; + +#endif diff --git a/src/query.cpp b/src/query.cpp new file mode 100644 index 0000000..716a438 --- /dev/null +++ b/src/query.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "query.h" +#include "exceptions.h" + +#include +#include +#include +#include +#include + +static const char* rr_types[] = { + "A", "AAAA", "MX", "NS", "DS", "DNSKEY", "TXT", "SOA", "NAPTR", "RRSIG", + "NSEC", "NSEC3", "NSEC3PARAM", "PTR", "SRV", + "CNAME", "TSIG", "*", "ANY", "AXFR", "IXFR", + "SPF", "A6", "HINFO", "WKS", "NULL", + NULL +}; + +static int rr_code[] = { + 1, 28, 15, 2, 43, 48, 16, 6, 35, 46, + 47, 50, 51, 12, 33, + 5, 250, 255, 255, 252, 251, + 99, 38, 13, 11, 10, + 0 +}; + +#pragma pack(push) /* push current alignment to stack */ +#pragma pack(1) /* set alignment to 1 byte boundary */ +struct DNS_OPT { + unsigned char name; + unsigned short type; + unsigned short udp_payload_size; + unsigned char extended_rcode; + unsigned char edns0_version; + unsigned short z; + unsigned short data_length; +}; +#pragma pack(pop) /* restore original alignment from stack */ + +int MakeQuery(const ppl7::String& query, unsigned char* buffer, size_t buffersize, bool dnssec, int udp_payload_size) +{ + ppl7::Array tok(query, " "); + if (tok.size() != 2) + throw InvalidDNSQuery(query); + ppl7::String Type = tok[1].toUpperCase(); + + int t = 0; + const char* str = Type.c_str(); + while (rr_types[t] != NULL) { + if (!strcmp(str, rr_types[t])) { + int bytes = res_mkquery(QUERY, + (const char*)tok[0], + C_IN, + rr_code[t], + NULL, 0, NULL, buffer, (int)buffersize); + if (bytes < 0) + throw InvalidDNSQuery("%s", hstrerror(h_errno)); + if (!dnssec) + return bytes; + return AddDnssecToQuery(buffer, buffersize, bytes, udp_payload_size); + } + t++; + } + throw UnknownRRType(tok[1]); +} + +int AddDnssecToQuery(unsigned char* buffer, size_t buffersize, int querysize, int udp_payload_size) +{ + DNS_HEADER* dns = (DNS_HEADER*)buffer; + dns->ad = 1; + dns->add_count = htons(1); + DNS_OPT* opt = (DNS_OPT*)(buffer + querysize); + memset(opt, 0, 11); + opt->type = htons(41); + opt->udp_payload_size = htons(udp_payload_size); + opt->z = htons(0x8000); // DO-bit + return querysize + 11; +} + +unsigned short getQueryTimestamp() +{ + struct timeval tp; + if (gettimeofday(&tp, NULL) == 0) { + return (tp.tv_sec % 6) * 10000 + (tp.tv_usec / 100); + } + return 0; +} + +double getQueryRTT(unsigned short start) +{ + unsigned short now = getQueryTimestamp(); + unsigned short diff = now - start; + if (now < start) + diff = 60000 - start + now; + return (double)(diff) / 10000.0f; +} diff --git a/src/query.h b/src/query.h new file mode 100644 index 0000000..fcc3f31 --- /dev/null +++ b/src/query.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include + +#ifndef __dnsmeter_query_h +#define __dnsmeter_query_h + +struct DNS_HEADER { + unsigned short id; // identification number + + unsigned char rd : 1; // recursion desired + unsigned char tc : 1; // truncated message + unsigned char aa : 1; // authoritive answer + unsigned char opcode : 4; // purpose of message + unsigned char qr : 1; // query/response flag + + unsigned char rcode : 4; // response code + unsigned char cd : 1; // checking disabled + unsigned char ad : 1; // authenticated data + unsigned char z : 1; // its z! reserved + unsigned char ra : 1; // recursion available + + unsigned short q_count; // number of question entries + unsigned short ans_count; // number of answer entries + unsigned short auth_count; // number of authority entries + unsigned short add_count; // number of resource entries +}; + +int MakeQuery(const ppl7::String& query, unsigned char* buffer, size_t buffersize, bool dnssec = false, int udp_payload_size = 4096); +int AddDnssecToQuery(unsigned char* buffer, size_t buffersize, int querysize, int udp_payload_size = 4096); +unsigned short getQueryTimestamp(); +double getQueryRTT(unsigned short start); + +#endif diff --git a/src/raw_socket_receiver.cpp b/src/raw_socket_receiver.cpp new file mode 100644 index 0000000..7df16a7 --- /dev/null +++ b/src/raw_socket_receiver.cpp @@ -0,0 +1,402 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "raw_socket_receiver.h" +#include "query.h" +#include "exceptions.h" + +#define __FAVOR_BSD 1 +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __OpenBSD__ +#error "Raw socket receiver not implemented for OpenBSD" +#endif +#ifdef __FreeBSD__ +#define DNSMETER_USE_BPF 1 +#include +#include +#include +#include +#include +#include +#endif + +#pragma pack(push) /* push current alignment to stack */ +#pragma pack(1) /* set alignment to 1 byte boundary */ +struct ETHER { + unsigned char destination[6]; + unsigned char source[6]; + unsigned short type; +}; +#pragma pack(pop) /* restore original alignment from stack */ + +#ifdef DNSMETER_USE_BPF +static int open_bpf() +{ + int sd; + for (int i = 0; i < 255; i++) { + ppl7::String Device; + Device.setf("/dev/bpf%d", i); + sd = open((const char*)Device, O_RDWR); + if (sd >= 0) { + return sd; + } + } + ppl7::throwExceptionFromErrno(errno, "Could not create RawReceiverSocket"); + return -1; +} + +bool tryAllocZeroCopyBuffer(int sd, struct bpf_zbuf* zbuf, size_t size) +{ + zbuf->bz_buflen = size; + zbuf->bz_bufa = malloc(zbuf->bz_buflen); + if (!zbuf->bz_bufa) { + throw ppl7::OutOfMemoryException(); + } + zbuf->bz_bufb = malloc(zbuf->bz_buflen); + if (!zbuf->bz_bufb) { + free(zbuf->bz_bufa); + throw ppl7::OutOfMemoryException(); + } + memset(zbuf->bz_bufa, 0, zbuf->bz_buflen); + memset(zbuf->bz_bufb, 0, zbuf->bz_buflen); + + if (ioctl(sd, BIOCSETZBUF, zbuf) < 0) { + free(zbuf->bz_bufa); + free(zbuf->bz_bufb); + return false; + } + return true; +} + +void initZeroCopyBuffer(int sd, struct bpf_zbuf* zbuf) +{ + unsigned int bufmode = BPF_BUFMODE_ZBUF; + if (ioctl(sd, BIOCSETBUFMODE, &bufmode) < 0) { + ppl7::throwExceptionFromErrno(errno, "BIOCSETBUFMODE with BPF_BUFMODE_ZBUF failed"); + } + unsigned int tstype = BPF_T_MICROTIME; + if (ioctl(sd, BIOCSTSTAMP, &tstype) < 0) { + ppl7::throwExceptionFromErrno(errno, "BIOCSTSTAMP"); + } + if (tryAllocZeroCopyBuffer(sd, zbuf, 8192)) + return; + if (tryAllocZeroCopyBuffer(sd, zbuf, 4096)) + return; + throw FailedToInitializePacketfilter("Could not configure ZeroCopy-Buffer (BIOCSETZBUF)"); +} + +void initBufferedMode(int sd, unsigned int buflen) +{ + unsigned int bufmode = BPF_BUFMODE_BUFFER; + if (ioctl(sd, BIOCSETBUFMODE, &bufmode) < 0) { + ppl7::throwExceptionFromErrno(errno, "BIOCSETBUFMODE with BPF_BUFMODE_BUFFER failed"); + } + if (ioctl(sd, BIOCSBLEN, &buflen) < 0) { + ppl7::throwExceptionFromErrno(errno, "BIOCSBLEN failed"); + } +} + +#endif + +RawSocketReceiver::Counter::Counter() +{ + num_pkgs = 0; + bytes_rcv = 0; + truncated = 0; + for (int i = 0; i < 15; i++) + rcodes[i] = 0; + rtt_total = 0.0f; + rtt_min = 0.0f; + rtt_max = 0.0f; +} + +void RawSocketReceiver::Counter::clear() +{ + num_pkgs = 0; + bytes_rcv = 0; + truncated = 0; + for (int i = 0; i < 15; i++) + rcodes[i] = 0; + rtt_total = 0.0f; + rtt_min = 0.0f; + rtt_max = 0.0f; +} + +RawSocketReceiver::RawSocketReceiver() +{ + SourceIP.set("0.0.0.0"); + SourcePort = 0; + buflen = 4096; + sd = -1; + buffer = NULL; +#ifdef DNSMETER_USE_BPF + useZeroCopyBuffer = false; + sd = open_bpf(); + buffer = (unsigned char*)malloc(sizeof(struct bpf_zbuf)); + if (!buffer) { + close(sd); + throw ppl7::OutOfMemoryException(); + } + struct bpf_zbuf* zbuf = (struct bpf_zbuf*)buffer; + + try { + initZeroCopyBuffer(sd, zbuf); + useZeroCopyBuffer = true; + buflen = zbuf->bz_buflen; + printf("INFO: using fast bpf zero copy buffer for packet capturing\n"); + return; + } catch (const ppl7::Exception& ex) { + useZeroCopyBuffer = false; + free(buffer); + } + buflen = 8192; + buffer = (unsigned char*)malloc(buflen); + if (!buffer) { + close(sd); + throw ppl7::OutOfMemoryException(); + } + try { + initBufferedMode(sd, buflen); + printf("INFO: using normal bpf buffered mode for packet capturing\n"); + int ret = fcntl(sd, F_SETFL, fcntl(sd, F_GETFL, 0) | O_NONBLOCK); // NON-Blocking + if (ret < 0) + ppl7::throwExceptionFromErrno(errno, "Could not set bpf into non blocking mode"); + + } catch (const ppl7::Exception& ex) { + free(buffer); + close(sd); + throw; + } + +#else + buffer = (unsigned char*)malloc(buflen); + if (!buffer) + throw ppl7::OutOfMemoryException(); + if ((sd = socket(AF_PACKET, SOCK_RAW, htons(0x0800))) == -1) { + int e = errno; + free(buffer); + ppl7::throwExceptionFromErrno(e, "Could not create RawReceiverSocket"); + } + int ret = fcntl(sd, F_SETFL, fcntl(sd, F_GETFL, 0) | O_NONBLOCK); // NON-Blocking + if (ret < 0) { + int e = errno; + close(sd); + free(buffer); + ppl7::throwExceptionFromErrno(e, "Could not set bpf into non blocking mode"); + } + +#endif +} + +RawSocketReceiver::~RawSocketReceiver() +{ + close(sd); +#ifdef DNSMETER_USE_BPF + if (useZeroCopyBuffer) { + struct bpf_zbuf* zbuf = (struct bpf_zbuf*)buffer; + free(zbuf->bz_bufa); + free(zbuf->bz_bufb); + } +#endif + free(buffer); +} + +void RawSocketReceiver::initInterface(const ppl7::String& Device) +{ +#ifdef DNSMETER_USE_BPF + struct ifreq ifreq; + strcpy((char*)ifreq.ifr_name, (const char*)Device); + if (ioctl(sd, BIOCSETIF, &ifreq) < 0) { + ppl7::throwExceptionFromErrno(errno, "Could not bind RawReceiverSocket on interface (BIOCSETIF)"); + } + unsigned int promiscuous_mode = 1; + if (ioctl(sd, BIOCPROMISC, &promiscuous_mode) < 0) { + ppl7::throwExceptionFromErrno(errno, "Could not set Interface into promiscuous mode (BIOCPROMISC)"); + } +#endif +} + +void RawSocketReceiver::setSource(const ppl7::IPAddress& ip_addr, int port) +{ + SourceIP = ip_addr; + SourcePort = htons(port); +#ifdef DNSMETER_USE_BPF + // Install packet filter in bpf + int sip = htonl(*(int*)SourceIP.addr()); + struct bpf_insn insns[] = { + // load halfword at position 12 from packet into register + BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), + // is it 0x800? if no, jump over 5 instructions, else jump over 0 + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x0800, 0, 7), + // source ip + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, 26), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (unsigned int)sip, 0, 5), + + // udp? + BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 17, 0, 3), + + // source port + BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 34), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (unsigned int)port, 0, 1), + + /* if we reach here, return -1 which will allow the packet to be read */ + BPF_STMT(BPF_RET + BPF_K, (u_int)-1), + /* if we reach here, return 0 which will ignore the packet */ + BPF_STMT(BPF_RET + BPF_K, 0), + }; + struct bpf_program bpf_program = { + 10, + (struct bpf_insn*)&insns + }; + if (ioctl(sd, BIOCSETF, (struct bpf_program*)&bpf_program) < 0) { + throw FailedToInitializePacketfilter(); + } +#endif +} + +bool RawSocketReceiver::socketReady() +{ +// #ifdef DNSMETER_USE_BPF +// if (useZeroCopyBuffer) return true; +// #endif + fd_set rset; + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 100; + FD_ZERO(&rset); + FD_SET(sd, &rset); // Wir wollen nur prüfen, ob wir lesen können + int ret = select(sd + 1, &rset, NULL, NULL, &timeout); + if (ret < 0) + return false; + if (FD_ISSET(sd, &rset)) { + return true; + } + return false; +} + +static void count_packet(RawSocketReceiver::Counter& counter, unsigned char* buffer, size_t size) +{ + counter.num_pkgs++; + counter.bytes_rcv += size; + struct DNS_HEADER* dns = (struct DNS_HEADER*)(buffer + 14 + sizeof(struct ip) + sizeof(struct udphdr)); + double rd = getQueryRTT(ntohs(dns->id)); + counter.rtt_total += rd; + if (rd < counter.rtt_min || counter.rtt_min == 0) + counter.rtt_min = rd; + if (rd > counter.rtt_max) + counter.rtt_max = rd; + if (dns->rcode < 16) + counter.rcodes[dns->rcode]++; + if (dns->tc) + counter.truncated++; +} + +#ifdef DNSMETER_USE_BPF +/* + * Return ownership of a buffer to the kernel for reuse. + */ +static void buffer_acknowledge(struct bpf_zbuf_header* bzh) +{ + atomic_store_rel_int(&bzh->bzh_user_gen, bzh->bzh_kernel_gen); +} + +static int buffer_check(struct bpf_zbuf_header* bzh) +{ + return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen)); +} + +static void read_buffer(unsigned char* ptr, size_t size, RawSocketReceiver::Counter& counter) +{ + size_t done = 0; + while (done < size) { + struct bpf_hdr* bpfh = (struct bpf_hdr*)ptr; + if (bpfh->bh_caplen == 0 || bpfh->bh_hdrlen == 0) + break; + size_t chunk_size = BPF_WORDALIGN(bpfh->bh_caplen + bpfh->bh_hdrlen); + count_packet(counter, ptr + bpfh->bh_hdrlen, chunk_size - bpfh->bh_datalen); + ptr += chunk_size; + done += chunk_size; + } +} + +static void read_zbuffer(struct bpf_zbuf_header* zhdr, RawSocketReceiver::Counter& counter) +{ + size_t size = zhdr->bzh_kernel_len - sizeof(struct bpf_zbuf_header); + unsigned char* ptr = (unsigned char*)zhdr + sizeof(struct bpf_zbuf_header); + read_buffer(ptr, size, counter); + buffer_acknowledge(zhdr); +} +void RawSocketReceiver::receive(RawSocketReceiver::Counter& counter) +{ + if (useZeroCopyBuffer) { + struct bpf_zbuf* zbuf = (struct bpf_zbuf*)buffer; + struct bpf_zbuf_header* zhdr = NULL; + if (buffer_check((struct bpf_zbuf_header*)zbuf->bz_bufa)) { + zhdr = ((struct bpf_zbuf_header*)zbuf->bz_bufa); + read_zbuffer(zhdr, counter); + } + if (buffer_check((struct bpf_zbuf_header*)zbuf->bz_bufb)) { + zhdr = ((struct bpf_zbuf_header*)zbuf->bz_bufb); + read_zbuffer(zhdr, counter); + } + } else { + ssize_t bufused = read(sd, buffer, buflen); + if (bufused < 34) + return; + read_buffer(buffer, bufused, counter); + } +} + +#else +void RawSocketReceiver::receive(Counter& counter) +{ + unsigned char* ptr = buffer; + ssize_t bufused = recvfrom(sd, buffer, buflen, 0, NULL, NULL); + if (bufused < 34) + return; + struct ETHER* eth = (struct ETHER*)ptr; + //ppl7::HexDump(ptr,bufused); + //printf ("sizeof ETHER=%d, type=%X\n",sizeof(struct ETHER),eth->type); + if (eth->type != htons(0x0800)) + return; + struct ip* iphdr = (struct ip*)(ptr + 14); + if (iphdr->ip_v != 4) + return; + if (iphdr->ip_src.s_addr != *(in_addr_t*)SourceIP.addr()) + return; + + struct udphdr* udp = (struct udphdr*)(ptr + 14 + sizeof(struct ip)); + if (udp->uh_sport != SourcePort) + return; + count_packet(counter, ptr, bufused); +} +#endif diff --git a/src/raw_socket_receiver.h b/src/raw_socket_receiver.h new file mode 100644 index 0000000..f8e38b2 --- /dev/null +++ b/src/raw_socket_receiver.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include +#include + +#ifndef __dnsmeter_raw_socket_receiver_h +#define __dnsmeter_raw_socket_receiver_h + +class RawSocketReceiver { +private: +#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L + RawSocketReceiver& operator=(const RawSocketReceiver& other); + RawSocketReceiver(RawSocketReceiver &&other) noexcept; + RawSocketReceiver const & operator=(RawSocketReceiver &&other); +#endif + + ppl7::IPAddress SourceIP; + unsigned char* buffer; + int buflen; + int sd; + unsigned short SourcePort; +#ifdef __FreeBSD__ + bool useZeroCopyBuffer; +#endif + +public: + class Counter { + public: + Counter(); + void clear(); + ppluint64 num_pkgs; + ppluint64 bytes_rcv; + ppluint64 rcodes[16]; + ppluint64 truncated; + double rtt_total, rtt_min, rtt_max; + }; + + RawSocketReceiver(); + ~RawSocketReceiver(); + void initInterface(const ppl7::String& Device); + bool socketReady(); + void setSource(const ppl7::IPAddress& ip_addr, int port); + void receive(Counter& counter); +}; + +#endif diff --git a/src/raw_socket_sender.cpp b/src/raw_socket_sender.cpp new file mode 100644 index 0000000..f290828 --- /dev/null +++ b/src/raw_socket_sender.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "raw_socket_sender.h" +#include "exceptions.h" + +#include +#include +#include +#include + +RawSocketSender::RawSocketSender() +{ + buffer = calloc(1, sizeof(struct sockaddr_in)); + if (!buffer) + throw ppl7::OutOfMemoryException(); + struct sockaddr_in* dest = (struct sockaddr_in*)buffer; + dest->sin_addr.s_addr = -1; + if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) { + free(buffer); + ppl7::throwExceptionFromErrno(errno, "Could not create RawSocket"); + } + unsigned int set = 1; + if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &set, sizeof(set)) < 0) { + close(sd); + free(buffer); + ppl7::throwExceptionFromErrno(errno, "Could not set socket option IP_HDRINCL"); + } +} + +RawSocketSender::~RawSocketSender() +{ + close(sd); + free(buffer); +} + +void RawSocketSender::setDestination(const ppl7::IPAddress& ip_addr, int port) +{ + if (ip_addr.family() != ppl7::IPAddress::IPv4) + throw UnsupportedIPFamily("Only IPv4 is supported"); + ip_addr.toSockAddr(buffer, sizeof(struct sockaddr_in)); + ((struct sockaddr_in*)buffer)->sin_port = htons(port); +} + +ssize_t RawSocketSender::send(Packet& pkt) +{ + struct sockaddr_in* dest = (struct sockaddr_in*)buffer; + if (dest->sin_addr.s_addr == (unsigned int)-1) + throw UnknownDestination(); + return sendto(sd, pkt.ptr(), pkt.size(), 0, + (const struct sockaddr*)dest, sizeof(struct sockaddr_in)); +} + +ppl7::SockAddr RawSocketSender::getSockAddr() const +{ + return ppl7::SockAddr(buffer, sizeof(struct sockaddr_in)); +} + +bool RawSocketSender::socketReady() +{ + fd_set wset; + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 100; + FD_ZERO(&wset); + FD_SET(sd, &wset); // Wir wollen nur prüfen, ob wir schreiben können + int ret = select(sd + 1, NULL, &wset, NULL, &timeout); + if (ret < 0) + return false; + if (FD_ISSET(sd, &wset)) { + return true; + } + return false; +} diff --git a/src/raw_socket_sender.h b/src/raw_socket_sender.h new file mode 100644 index 0000000..aa2e780 --- /dev/null +++ b/src/raw_socket_sender.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "packet.h" + +#include + +#ifndef __dnsmeter_raw_socket_sender_h +#define __dnsmeter_raw_socket_sender_h + +class RawSocketSender { +private: +#if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L + RawSocketSender& operator=(const RawSocketSender& other); + RawSocketSender(RawSocketSender &&other) noexcept; + RawSocketSender const & operator=(RawSocketSender &&other); +#endif + + void* buffer; + int sd; + +public: + RawSocketSender(); + ~RawSocketSender(); + void setDestination(const ppl7::IPAddress& ip_addr, int port); + ssize_t send(Packet& pkt); + ppl7::SockAddr getSockAddr() const; + bool socketReady(); +}; + +#endif diff --git a/src/system_stat.cpp b/src/system_stat.cpp new file mode 100644 index 0000000..49d064c --- /dev/null +++ b/src/system_stat.cpp @@ -0,0 +1,362 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include "config.h" + +#include "system_stat.h" +#include "exceptions.h" + +#ifdef __linux__ +#include +#endif +#include +#include +#include +#ifdef __FreeBSD__ +#include +#include +#include +#include +#include +#include +#include +#endif + +// ########################################################### Linux specific +#ifdef __linux__ +static void sampleCpuUsage(SystemStat::Cpu& stat) +{ + FILE* fp = fopen("/proc/stat", "r"); + if (fscanf(fp, "%*s %d %d %d %d %d", &stat.user, &stat.nice, &stat.system, &stat.idle, &stat.iowait) != 5) { + // ignore + } + fclose(fp); +} + +static void sampleSysinfo(SystemStat::Sysinfo& stat) +{ + struct sysinfo info; + if (0 != sysinfo(&info)) { + return; + } + stat.uptime = info.uptime; + stat.freeswap = info.freeswap * info.mem_unit; + stat.freeram = info.freeram * info.mem_unit; + stat.bufferram = info.bufferram * info.mem_unit; + stat.totalram = info.totalram * info.mem_unit; + stat.totalswap = info.totalswap * info.mem_unit; + stat.sharedram = info.sharedram * info.mem_unit; + stat.procs = info.procs; +} + +static void sampleNetwork(std::map& interfaces, SystemStat::Interface& total) +{ + total.receive.clear(); + total.transmit.clear(); + + ppl7::String buffer; + ppl7::File ff("/proc/net/dev"); + while (!ff.eof()) { + ff.gets(buffer, 2048); + buffer.trim(); + ssize_t t = buffer.instr(":"); + if (t > 1) { + SystemStat::Interface nif; + nif.Name.set(buffer, t); + buffer.replace("\t", " "); + ppl7::Array tok = ppl7::StrTok(buffer, " "); + nif.receive.bytes = tok[1].toUnsignedLong(); + nif.receive.packets = tok[2].toUnsignedLong(); + nif.receive.errs = tok[3].toUnsignedLong(); + nif.receive.drop = tok[4].toUnsignedLong(); + + nif.transmit.bytes = tok[9].toUnsignedLong(); + nif.transmit.packets = tok[10].toUnsignedLong(); + nif.transmit.errs = tok[11].toUnsignedLong(); + nif.transmit.drop = tok[12].toUnsignedLong(); + + total.receive.bytes += nif.receive.bytes; + total.receive.packets += nif.receive.packets; + total.receive.errs += nif.receive.errs; + total.receive.drop += nif.receive.drop; + + total.transmit.bytes += nif.transmit.bytes; + total.transmit.packets += nif.transmit.packets; + total.transmit.errs += nif.transmit.errs; + total.transmit.drop += nif.transmit.drop; + interfaces.insert(std::pair(nif.Name, nif)); + } + } +} + +// ########################################################### FreeBSD specific +#elif defined __FreeBSD__ +static kvm_t* kd = NULL; +#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) + +static void +getsysctl(const char* name, void* ptr, size_t len) +{ + size_t nlen = len; + if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { + throw SystemCallFailed("sysctlbyname(%s...) failed: %s\n", name, + strerror(errno)); + } + if (nlen != len) { + throw SystemCallFailed("sysctlbyname(%s...) expected %lu, got %lu\n", + name, (unsigned long)len, (unsigned long)nlen); + } +} + +static void sampleCpuUsage(SystemStat::Cpu& stat) +{ + size_t cp_size = sizeof(long) * CPUSTATES * 8; + long* cp_times = (long*)malloc(cp_size); + if (sysctlbyname("kern.cp_time", cp_times, &cp_size, NULL, 0) < 0) { + perror("sysctlbyname"); + free(cp_times); + } + stat.user = (int)cp_times[0]; + stat.nice = (int)cp_times[1]; + stat.system = (int)cp_times[2]; + stat.iowait = (int)cp_times[3]; + stat.idle = (int)cp_times[4]; +} + +static int +swapmode(long* retavail, long* retfree) +{ + int n; + int pagesize = getpagesize(); + struct kvm_swap swapary[1]; + + *retavail = 0; + *retfree = 0; + +#define CONVERT(v) ((quad_t)(v)*pagesize) + + n = kvm_getswapinfo(kd, swapary, 1, 0); + if (n < 0 || swapary[0].ksw_total == 0) + return (0); + + *retavail = CONVERT(swapary[0].ksw_total); + *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); + + n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total); + return (n); +} + +static void sampleSysinfo(SystemStat::Sysinfo& stat) +{ + struct timespec uptime; + int pagesize = getpagesize(); + if (clock_gettime(CLOCK_UPTIME, &uptime) == 0) + stat.uptime = uptime.tv_sec; + swapmode(&stat.totalswap, &stat.freeswap); + int tmp; + long tmp_l; + GETSYSCTL("vm.stats.vm.v_free_count", tmp); + stat.freeram = tmp * pagesize; + GETSYSCTL("hw.physmem", tmp_l); + stat.totalram = tmp_l; +} + +static void sampleNetwork(std::map& interfaces, SystemStat::Interface& total) +{ + total.receive.clear(); + total.transmit.clear(); + +#define IFA_STAT(s) (((struct if_data*)ifa->ifa_data)->ifi_##s) + + struct ifaddrs* ifap = NULL; + if (getifaddrs(&ifap) != 0) { + throw SystemCallFailed("FreeBSD, getifaddrs: %s", strerror(errno)); + } + for (struct ifaddrs* ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family != AF_LINK) + continue; + SystemStat::Interface nif; + nif.Name.setf("%s", ifa->ifa_name); + + nif.receive.bytes = IFA_STAT(ibytes); + nif.receive.packets = IFA_STAT(ipackets); + nif.receive.errs = IFA_STAT(ierrors); + nif.receive.drop = IFA_STAT(iqdrops); + + nif.transmit.bytes = IFA_STAT(obytes); + nif.transmit.packets = IFA_STAT(opackets); + nif.transmit.errs = IFA_STAT(oerrors); + nif.transmit.drop = IFA_STAT(oqdrops); + + total.receive.bytes += nif.receive.bytes; + total.receive.packets += nif.receive.packets; + total.receive.errs += nif.receive.errs; + total.receive.drop += nif.receive.drop; + + total.transmit.bytes += nif.transmit.bytes; + total.transmit.packets += nif.transmit.packets; + total.transmit.errs += nif.transmit.errs; + total.transmit.drop += nif.transmit.drop; + interfaces.insert(std::pair(nif.Name, nif)); + } + freeifaddrs(ifap); +} + +static void exit_kvm() +{ + if (kd) + kvm_close(kd); +} + +#endif + +void sampleSensorData(SystemStat& stat) +{ +#ifdef __FreeBSD__ + if (!kd) { + kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); + if (!kd) + throw KernelAccessFailed("FreeBSD kvm_open failed"); + atexit(exit_kvm); + } +#endif + stat.sampleTime = ppl7::GetMicrotime(); + sampleCpuUsage(stat.cpu); + sampleSysinfo(stat.sysinfo); + sampleNetwork(stat.interfaces, stat.net_total); +} + +double SystemStat::Cpu::getUsage(const SystemStat::Cpu& sample1, const SystemStat::Cpu& sample2) +{ + return 100.0 * (double)((sample2.user + sample2.nice + sample2.system) - (sample1.user + sample1.nice + sample1.system)) / (double)((sample2.user + sample2.nice + sample2.system + sample2.idle) - (sample1.user + sample1.nice + sample1.system + sample1.idle)); +} + +static unsigned long delta_with_overflow(unsigned long sample1, unsigned long sample2) +{ + if (sample2 >= sample1) + return sample2 - sample1; + return ULONG_MAX - sample1 + sample2; +} + +SystemStat::Network SystemStat::Network::getDelta(const SystemStat::Network& sample1, const SystemStat::Network& sample2) +{ + return SystemStat::Network(delta_with_overflow(sample1.bytes, sample2.bytes), + delta_with_overflow(sample1.packets, sample2.packets), + delta_with_overflow(sample1.errs, sample2.errs), + delta_with_overflow(sample1.drop, sample2.drop)); +} + +void SystemStat::exportToArray(ppl7::AssocArray& data) const +{ + data.setf("sampleTime", "%0.6f", sampleTime); + data.setf("net_total/receive/bytes", "%lu", net_total.receive.bytes); + data.setf("net_total/receive/packets", "%lu", net_total.receive.packets); + data.setf("net_total/receive/errs", "%lu", net_total.receive.errs); + data.setf("net_total/receive/drop", "%lu", net_total.receive.drop); + data.setf("net_total/transmit/bytes", "%lu", net_total.transmit.bytes); + data.setf("net_total/transmit/packets", "%lu", net_total.transmit.packets); + data.setf("net_total/transmit/errs", "%lu", net_total.transmit.errs); + data.setf("net_total/transmit/drop", "%lu", net_total.transmit.drop); + std::map::const_iterator it; + for (it = interfaces.begin(); it != interfaces.end(); ++it) { + ppl7::AssocArray d; + const SystemStat::Interface& nif = it->second; + d.setf("receive/bytes", "%lu", nif.receive.bytes); + d.setf("receive/packets", "%lu", nif.receive.packets); + d.setf("receive/errs", "%lu", nif.receive.errs); + d.setf("receive/drop", "%lu", nif.receive.drop); + d.setf("transmit/bytes", "%lu", nif.transmit.bytes); + d.setf("transmit/packets", "%lu", nif.transmit.packets); + d.setf("transmit/errs", "%lu", nif.transmit.errs); + d.setf("transmit/drop", "%lu", nif.transmit.drop); + d.set("name", nif.Name); + ppl7::String key; + key.setf("interface/%s", (const char*)nif.Name); + data.set(key, d); + } + + data.setf("cpu/user", "%d", cpu.user); + data.setf("cpu/nice", "%d", cpu.nice); + data.setf("cpu/system", "%d", cpu.system); + data.setf("cpu/idle", "%d", cpu.idle); + data.setf("cpu/iowait", "%d", cpu.iowait); + + data.setf("sysinfo/uptime", "%ld", sysinfo.uptime); + data.setf("sysinfo/freeswap", "%ld", sysinfo.freeswap); + data.setf("sysinfo/totalswap", "%ld", sysinfo.totalswap); + data.setf("sysinfo/freeram", "%ld", sysinfo.freeram); + data.setf("sysinfo/bufferram", "%ld", sysinfo.bufferram); + data.setf("sysinfo/totalram", "%ld", sysinfo.totalram); + data.setf("sysinfo/sharedram", "%ld", sysinfo.sharedram); + data.setf("sysinfo/procs", "%d", sysinfo.procs); +} + +void SystemStat::importFromArray(const ppl7::AssocArray& data) +{ + sampleTime = data.getString("sampleTime").toDouble(); + net_total.receive.bytes = data.getString("net_total/receive/bytes").toUnsignedLong(); + net_total.receive.packets = data.getString("net_total/receive/packets").toUnsignedLong(); + net_total.receive.errs = data.getString("net_total/receive/errs").toUnsignedLong(); + net_total.receive.drop = data.getString("net_total/receive/drop").toUnsignedLong(); + net_total.transmit.bytes = data.getString("net_total/transmit/bytes").toUnsignedLong(); + net_total.transmit.packets = data.getString("net_total/transmit/packets").toUnsignedLong(); + net_total.transmit.errs = data.getString("net_total/transmit/errs").toUnsignedLong(); + net_total.transmit.drop = data.getString("net_total/transmit/drop").toUnsignedLong(); + + const ppl7::AssocArray& data_if_list = data.getAssocArray("interface"); + ppl7::AssocArray::Iterator it; + data_if_list.reset(it); + while (data_if_list.getNext(it)) { + Interface nif; + nif.Name = it.key(); + const ppl7::AssocArray& d = it.value().toAssocArray(); + nif.receive.bytes = d.getString("receive/bytes").toUnsignedLong(); + nif.receive.packets = d.getString("receive/packets").toUnsignedLong(); + nif.receive.errs = d.getString("receive/errs").toUnsignedLong(); + nif.receive.drop = d.getString("receive/drop").toUnsignedLong(); + nif.transmit.bytes = d.getString("transmit/bytes").toUnsignedLong(); + nif.transmit.packets = d.getString("transmit/packets").toUnsignedLong(); + nif.transmit.errs = d.getString("transmit/errs").toUnsignedLong(); + nif.transmit.drop = d.getString("transmit/drop").toUnsignedLong(); + interfaces.insert(std::pair(nif.Name, nif)); + } + cpu.user = data.getString("cpu/user").toInt(); + cpu.nice = data.getString("cpu/nice").toInt(); + cpu.system = data.getString("cpu/system").toInt(); + cpu.idle = data.getString("cpu/idle").toInt(); + cpu.iowait = data.getString("cpu/iowait").toInt(); + + sysinfo.uptime = data.getString("sysinfo/uptime").toLong(); + sysinfo.freeswap = data.getString("sysinfo/freeswap").toLong(); + sysinfo.totalswap = data.getString("sysinfo/totalswap").toLong(); + sysinfo.freeram = data.getString("sysinfo/freeram").toLong(); + sysinfo.bufferram = data.getString("sysinfo/bufferram").toLong(); + sysinfo.totalram = data.getString("sysinfo/totalram").toLong(); + sysinfo.sharedram = data.getString("sysinfo/sharedram").toLong(); + sysinfo.procs = data.getString("sysinfo/procs").toInt(); +} + +void SystemStat::print() const +{ + ppl7::AssocArray a; + exportToArray(a); + a.list(); +} diff --git a/src/system_stat.h b/src/system_stat.h new file mode 100644 index 0000000..6f0f5fa --- /dev/null +++ b/src/system_stat.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2019-2021, OARC, Inc. + * Copyright (c) 2019, DENIC eG + * All rights reserved. + * + * This file is part of dnsmeter. + * + * dnsmeter is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dnsmeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dnsmeter. If not, see . + */ + +#include + +#ifndef __dnsmeter_system_stat_h +#define __dnsmeter_system_stat_h + +class SystemStat { +public: + class Network { + public: + unsigned long bytes; + unsigned long packets; + unsigned long errs; + unsigned long drop; + Network() + { + bytes = packets = errs = drop = 0; + } + Network(unsigned long bytes, unsigned long packets, unsigned long errs, unsigned long drop) + { + this->bytes = bytes; + this->packets = packets; + this->errs = errs; + this->drop = drop; + } + void clear() + { + bytes = packets = errs = drop = 0; + } + void print() + { + printf("Network bytes: %lu, packets: %lu, errs: %lu, drop: %lu\n", + bytes, packets, errs, drop); + } + + static Network getDelta(const Network& sample1, const Network& sample2); + }; + + class Cpu { + public: + Cpu() + { + user = nice = system = idle = iowait = 0; + } + int user; + int nice; + int system; + int idle; + int iowait; + + static double getUsage(const SystemStat::Cpu& sample1, const SystemStat::Cpu& sample2); + }; + + class Sysinfo { + public: + Sysinfo() + { + uptime = freeswap = totalswap = freeram = bufferram = totalram = sharedram = 0; + procs = 0; + } + long uptime; + long freeswap; + long totalswap; + long freeram; + long bufferram; + long totalram; + long sharedram; + int procs; + }; + + class Interface { + public: + ppl7::String Name; + Network receive; + Network transmit; + }; + + double sampleTime; + + Cpu cpu; + Sysinfo sysinfo; + Interface net_total; + std::map interfaces; + + void exportToArray(ppl7::AssocArray& data) const; + void importFromArray(const ppl7::AssocArray& data); + void print() const; +}; + +void sampleSensorData(SystemStat& stat); + +#endif diff --git a/src/test/Makefile.am b/src/test/Makefile.am new file mode 100644 index 0000000..0a805cf --- /dev/null +++ b/src/test/Makefile.am @@ -0,0 +1,7 @@ +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +CLEANFILES = test*.log test*.trs + +TESTS = test1.sh + +EXTRA_DIST = $(TESTS) diff --git a/src/test/test1.sh b/src/test/test1.sh new file mode 100755 index 0000000..e935378 --- /dev/null +++ b/src/test/test1.sh @@ -0,0 +1,3 @@ +#!/bin/sh -xe + +../dnsmeter -h From 179944575d0988f6f93a98e377ab1725e16d5f9e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 22 Apr 2025 18:23:53 +0200 Subject: [PATCH 2/2] Adding debian version 1.0.2-1. Signed-off-by: Daniel Baumann --- debian/changelog | 5 ++++ debian/control | 43 +++++++++++++++++++++++++++++++++++ debian/copyright | 54 ++++++++++++++++++++++++++++++++++++++++++++ debian/rules | 9 ++++++++ debian/source/format | 1 + debian/watch | 2 ++ 6 files changed, 114 insertions(+) create mode 100644 debian/changelog create mode 100644 debian/control create mode 100644 debian/copyright create mode 100755 debian/rules create mode 100644 debian/source/format create mode 100644 debian/watch diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..b961e5c --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +dnsmeter (1.0.2-1) unstable; urgency=low + + * Initial upload to sid (Closes: #991249). + + -- Daniel Baumann Tue, 22 Apr 2025 17:58:35 +0200 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..5e0c97a --- /dev/null +++ b/debian/control @@ -0,0 +1,43 @@ +Source: dnsmeter +Section: utils +Priority: optional +Maintainer: Daniel Baumann +Build-Depends: + debhelper-compat (= 13), + gettext, + libbz2-dev, + libidn2-dev, + libpcap-dev, + libpcre2-dev, + libssl-dev, + zlib1g-dev, +Rules-Requires-Root: no +Standards-Version: 4.7.2 +Homepage: https://www.dns-oarc.net/tools/dnsmeter +Vcs-Browser: https://forgejo.debian.net/dns-oarc/dnsmeter +Vcs-Git: https://forgejo.debian.net/dns-oarc/dnsmeter + +Package: dnsmeter +Section: utils +Architecture: any +Depends: + ${misc:Depends}, + ${shlibs:Depends}, +Description: DNS Performance and Infrastructure Testing Tool + dnsmeter is a tool for testing performance of a nameserver and the + infrastructure around it. It generates DNS queries and sends them via UDP to a + target nameserver and counts the answers. + . + Features: + . + * payload can be given as a text file or a PCAP file + * can automatically run different load steps, which can be given as a list + or ranges + * results per load step can be stored in a CSV file + * sender addresses can be spoofed from a given network or from the addresses + found in the PCAP file + * answers are counted, even if source address is spoofed, if answers get + routed back to the load generator + * round-trip-times are measured (average, min, mix) + * the amount of DNSSEC queries can be given as percentage of total traffic + * optimized for high amount of packets diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..2f05538 --- /dev/null +++ b/debian/copyright @@ -0,0 +1,54 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: dnsmeter +Upstream-Contact: https://github.com/DNS-OARC/dnsmeter/issues +Source: https://github.com/DNS-OARC/dnsmeter/tags + +Files: * +Copyright: 2019-2021 OARC, Inc. + 2019 DENIC eG +License: GPL-3 + +Files: src/pplib/* +Copyright: 2013-2019 Patrick Fedick +License: BSD-2-Clause + +Files: debian/* +Copyright: 2021-2022 Daniel Baumann +License: GPL-3 + +License: BSD-2-Clause + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + . + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + . + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +License: GPL-3 + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 3 of the License. + . + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + . + You should have received a copy of the GNU General Public License + along with this program. If not, see . + . + The complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..3a36da6 --- /dev/null +++ b/debian/rules @@ -0,0 +1,9 @@ +#!/usr/bin/make -f + +%: + dh ${@} + +execute_after_dh_auto_install: + # removing unneeded files + rm -f debian/dnsmeter/usr/share/doc/dnsmeter/LICENSE + rm -f debian/dnsmeter/usr/share/doc/dnsmeter/README.md diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/debian/watch b/debian/watch new file mode 100644 index 0000000..0cfbbec --- /dev/null +++ b/debian/watch @@ -0,0 +1,2 @@ +version=4 +https://github.com/DNS-OARC/dnsmeter/tags .*/v?(\d.*)@ARCHIVE_EXT@