From b01f0fa048b9041881396659215cca7e9001704f Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Sun, 14 Oct 2018 10:53:51 +0200 Subject: [PATCH] Move DNS related code to subdirectory --- .gitignore | 2 - Makefile | 31 ++---------- dns/.gitignore | 3 ++ dns/Makefile | 37 ++++++++++++++ dns.hpp => dns/dns.hpp | 34 +++---------- dns-test.cpp => dns/parse-test.cpp | 2 +- dns.cpp => dns/parser.cpp | 2 +- dns/parser.hpp | 50 +++++++++++++++++++ .../test-cases}/any.example.org.bm | 0 .../dnsmasq-bug-2.73rc6-5d07d77e75.bm | 0 .../off-by-one-edns-option-length.bm | 0 .../test-cases}/overlong-domain-name-label.bm | 0 .../rdata-name-beyond-rdata-length.bm | 0 13 files changed, 102 insertions(+), 59 deletions(-) create mode 100644 dns/.gitignore create mode 100644 dns/Makefile rename dns.hpp => dns/dns.hpp (94%) rename dns-test.cpp => dns/parse-test.cpp (99%) rename dns.cpp => dns/parser.cpp (99%) create mode 100644 dns/parser.hpp rename {test-cases => dns/test-cases}/any.example.org.bm (100%) rename {test-cases => dns/test-cases}/dnsmasq-bug-2.73rc6-5d07d77e75.bm (100%) rename {test-cases => dns/test-cases}/off-by-one-edns-option-length.bm (100%) rename {test-cases => dns/test-cases}/overlong-domain-name-label.bm (100%) rename {test-cases => dns/test-cases}/rdata-name-beyond-rdata-length.bm (100%) diff --git a/.gitignore b/.gitignore index 4e561c8..053e33a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,3 @@ /[Bb]uild/ *.[od] /callback -/dns-test -/dns-test-afl diff --git a/Makefile b/Makefile index 3673d2f..8b20687 100644 --- a/Makefile +++ b/Makefile @@ -1,40 +1,17 @@ -AFL_CXX ?= afl-g++ CXX ?= g++ -CPPFLAGS ?= -isystem $(HOME)/git/GSL/include CXXFLAGS ?= -Wall -Wextra -Werror -std=c++17 -O3 -ggdb3 -fstack-protector-strong -fsanitize=address,undefined -BIN = callback dns-test dns-test-afl -dns_test_SRC = dns-test.cpp dns.cpp -DEP = callback.d $(dns_test_SRC:.cpp=.d) -OBJ = $(dns_test_SRC:.cpp=.o) $(dns_test_SRC:.cpp=.afl.o) -TEST_CASES_BIN = $(patsubst test-cases/%.bm,test-cases-bin/%.bin,$(wildcard test-cases/*.bm)) +BIN = callback +DEP = callback.d -all: $(BIN) $(TEST_CASES_BIN) +all: $(BIN) clean: - $(RM) $(DEP) $(OBJ) $(BIN) $(TEST_CASES_BIN) - -%.o: %.cpp - $(CXX) $(CPPFLAGS) $(CXXFLAGS) -MD -MT $@ -MF $(@:.o=.d) -o $@ -c $< - -%.afl.o: %.cpp - AFL_HARDEN=1 $(AFL_CXX) $(CPPFLAGS) $(CXXFLAGS) -MD -MT $@ -MF $(@:.afl.o=.d) -o $@ -c $< + $(RM) $(DEP) $(BIN) callback: callback.hpp $(CXX) $(CPPFLAGS) -DTEST -x c++ $(CXXFLAGS) -MD -MT $@ -MF $(@).d -o $@ $< -test-cases-bin: - mkdir test-cases-bin - -test-cases-bin/%.bin: test-cases/%.bm test-cases-bin - binmake $< $@ - -dns-test: $(dns_test_SRC:.cpp=.o) - $(CXX) $(CXXFLAGS) -o $@ $^ - -dns-test-afl: $(dns_test_SRC:.cpp=.afl.o) - AFL_HARDEN=1 $(AFL_CXX) $(CXXFLAGS) -o $@ $^ - .PHONY: all clean -include $(DEP) diff --git a/dns/.gitignore b/dns/.gitignore new file mode 100644 index 0000000..fa4465d --- /dev/null +++ b/dns/.gitignore @@ -0,0 +1,3 @@ +/parse-test +/parse-test-afl +/test-cases-bin diff --git a/dns/Makefile b/dns/Makefile new file mode 100644 index 0000000..77afb6c --- /dev/null +++ b/dns/Makefile @@ -0,0 +1,37 @@ +AFL_CXX ?= afl-g++ +CXX ?= g++ +CPPFLAGS ?= -isystem $(HOME)/git/GSL/include -I.. +CXXFLAGS ?= -Wall -Wextra -Werror -std=c++17 -O3 -ggdb3 -fstack-protector-strong -fsanitize=address,undefined + +BIN = parse-test parse-test-afl +parse_test_SRC = parse-test.cpp parser.cpp +DEP = $(parse_test_SRC:.cpp=.d) +OBJ = $(parse_test_SRC:.cpp=.o) $(parse_test_SRC:.cpp=.afl.o) +TEST_CASES_BIN = $(patsubst test-cases/%.bm,test-cases-bin/%.bin,$(wildcard test-cases/*.bm)) + +all: $(BIN) $(TEST_CASES_BIN) + +clean: + $(RM) $(DEP) $(OBJ) $(BIN) $(TEST_CASES_BIN) + +%.o: %.cpp + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -MD -MT $@ -MF $(@:.o=.d) -o $@ -c $< + +%.afl.o: %.cpp + AFL_HARDEN=1 $(AFL_CXX) $(CPPFLAGS) $(CXXFLAGS) -MD -MT $@ -MF $(@:.afl.o=.d) -o $@ -c $< + +test-cases-bin: + mkdir test-cases-bin + +test-cases-bin/%.bin: test-cases/%.bm test-cases-bin + binmake $< $@ + +parse-test: $(parse_test_SRC:.cpp=.o) + $(CXX) $(CXXFLAGS) -o $@ $^ + +parse-test-afl: $(parse_test_SRC:.cpp=.afl.o) + AFL_HARDEN=1 $(AFL_CXX) $(CXXFLAGS) -o $@ $^ + +.PHONY: all clean + +-include $(DEP) diff --git a/dns.hpp b/dns/dns.hpp similarity index 94% rename from dns.hpp rename to dns/dns.hpp index 3135280..c537579 100644 --- a/dns.hpp +++ b/dns/dns.hpp @@ -16,8 +16,8 @@ * . */ -#ifndef INCLUDED_DNS_HPP -#define INCLUDED_DNS_HPP +#ifndef INCLUDED_DNS_DNS_HPP +#define INCLUDED_DNS_DNS_HPP #include #include @@ -498,6 +498,9 @@ namespace dns std::vector authority; std::vector additional; }; + + template + using expected = ::util::expected; } namespace std @@ -508,29 +511,4 @@ namespace std struct is_error_code_enum<::dns::parser_error> : public true_type {}; } -namespace dns -{ - template - using expected = ::util::expected; - - expected parse(gsl::span frame); - - inline expected> - parse(const std::uint8_t* const first, const std::uint8_t* const last) - { - using ::util::unexpected; - - if (std::distance(first, last) < 2) - return unexpected(parser_error::not_enough_data); - const std::uint16_t len = *first << 8U | (*std::next(first) & 0xffU); - if (std::distance(first, last) < 2 + len) - return unexpected(parser_error::not_enough_data); - const auto next = std::next(first, 2 + len); - if (auto msg = parse(gsl::span{std::next(first, 2), next}); msg) - return std::make_pair(std::move(*msg), next); - else - return unexpected(std::move(msg).error()); - } -} - -#endif /* INCLUDED_DNS_HPP */ +#endif /* INCLUDED_DNS_DNS_HPP */ diff --git a/dns-test.cpp b/dns/parse-test.cpp similarity index 99% rename from dns-test.cpp rename to dns/parse-test.cpp index b86a4f8..da691b3 100644 --- a/dns-test.cpp +++ b/dns/parse-test.cpp @@ -1,4 +1,4 @@ -#include "dns.hpp" +#include #include "overload.hpp" #include #include diff --git a/dns.cpp b/dns/parser.cpp similarity index 99% rename from dns.cpp rename to dns/parser.cpp index e191df1..8ad7a88 100644 --- a/dns.cpp +++ b/dns/parser.cpp @@ -1,10 +1,10 @@ +#include "parser.hpp" #include #include #include #include #include #include -#include "dns.hpp" #include "monads.hpp" namespace dns diff --git a/dns/parser.hpp b/dns/parser.hpp new file mode 100644 index 0000000..a05270a --- /dev/null +++ b/dns/parser.hpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2018 Giel van Schijndel + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + */ + +#ifndef INCLUDED_DNS_PARSER_HPP +#define INCLUDED_DNS_PARSER_HPP + +#include "dns.hpp" +#include "expected.hpp" +#include +#include +#include + +namespace dns +{ + expected parse(gsl::span frame); + + inline expected> + parse(const std::uint8_t* const first, const std::uint8_t* const last) + { + using ::util::unexpected; + + if (std::distance(first, last) < 2) + return unexpected(parser_error::not_enough_data); + const std::uint16_t len = *first << 8U | (*std::next(first) & 0xffU); + if (std::distance(first, last) < 2 + len) + return unexpected(parser_error::not_enough_data); + const auto next = std::next(first, 2 + len); + if (auto msg = parse(gsl::span{std::next(first, 2), next}); msg) + return std::make_pair(std::move(*msg), next); + else + return unexpected(std::move(msg).error()); + } +} + +#endif /* INCLUDED_DNS_PARSER_HPP */ diff --git a/test-cases/any.example.org.bm b/dns/test-cases/any.example.org.bm similarity index 100% rename from test-cases/any.example.org.bm rename to dns/test-cases/any.example.org.bm diff --git a/test-cases/dnsmasq-bug-2.73rc6-5d07d77e75.bm b/dns/test-cases/dnsmasq-bug-2.73rc6-5d07d77e75.bm similarity index 100% rename from test-cases/dnsmasq-bug-2.73rc6-5d07d77e75.bm rename to dns/test-cases/dnsmasq-bug-2.73rc6-5d07d77e75.bm diff --git a/test-cases/off-by-one-edns-option-length.bm b/dns/test-cases/off-by-one-edns-option-length.bm similarity index 100% rename from test-cases/off-by-one-edns-option-length.bm rename to dns/test-cases/off-by-one-edns-option-length.bm diff --git a/test-cases/overlong-domain-name-label.bm b/dns/test-cases/overlong-domain-name-label.bm similarity index 100% rename from test-cases/overlong-domain-name-label.bm rename to dns/test-cases/overlong-domain-name-label.bm diff --git a/test-cases/rdata-name-beyond-rdata-length.bm b/dns/test-cases/rdata-name-beyond-rdata-length.bm similarity index 100% rename from test-cases/rdata-name-beyond-rdata-length.bm rename to dns/test-cases/rdata-name-beyond-rdata-length.bm