Skip to content

Commit

Permalink
Move DNS related code to subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
muggenhor committed Oct 14, 2018
1 parent 77931c6 commit b01f0fa
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 59 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
/[Bb]uild/
*.[od]
/callback
/dns-test
/dns-test-afl
31 changes: 4 additions & 27 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions dns/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/parse-test
/parse-test-afl
/test-cases-bin
37 changes: 37 additions & 0 deletions dns/Makefile
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 6 additions & 28 deletions dns.hpp → dns/dns.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* <https://www.gnu.org/licenses/>.
*/

#ifndef INCLUDED_DNS_HPP
#define INCLUDED_DNS_HPP
#ifndef INCLUDED_DNS_DNS_HPP
#define INCLUDED_DNS_DNS_HPP

#include <chrono>
#include <cstdint>
Expand Down Expand Up @@ -498,6 +498,9 @@ namespace dns
std::vector<rr> authority;
std::vector<rr> additional;
};

template <typename T>
using expected = ::util::expected<T, std::error_code>;
}

namespace std
Expand All @@ -508,29 +511,4 @@ namespace std
struct is_error_code_enum<::dns::parser_error> : public true_type {};
}

namespace dns
{
template <typename T>
using expected = ::util::expected<T, std::error_code>;

expected<message> parse(gsl::span<const std::uint8_t> frame);

inline expected<std::pair<message, const std::uint8_t*>>
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 */
2 changes: 1 addition & 1 deletion dns-test.cpp → dns/parse-test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "dns.hpp"
#include <dns/parser.hpp>
#include "overload.hpp"
#include <fstream>
#include <iomanip>
Expand Down
2 changes: 1 addition & 1 deletion dns.cpp → dns/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "parser.hpp"
#include <cassert>
#include <climits>
#include <cstddef>
#include <ostream>
#include <optional>
#include <system_error>
#include "dns.hpp"
#include "monads.hpp"

namespace dns
Expand Down
50 changes: 50 additions & 0 deletions dns/parser.hpp
Original file line number Diff line number Diff line change
@@ -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
* <https://www.gnu.org/licenses/>.
*/

#ifndef INCLUDED_DNS_PARSER_HPP
#define INCLUDED_DNS_PARSER_HPP

#include "dns.hpp"
#include "expected.hpp"
#include <gsl/span>
#include <iterator>
#include <utility>

namespace dns
{
expected<message> parse(gsl::span<const std::uint8_t> frame);

inline expected<std::pair<message, const std::uint8_t*>>
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 */
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit b01f0fa

Please sign in to comment.